xunchang | 37304f3 | 2019-03-12 12:40:14 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2019 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agree to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #include <stdio.h> |
| 18 | |
| 19 | #include <functional> |
| 20 | #include <string> |
| 21 | #include <vector> |
| 22 | |
| 23 | #include <android-base/file.h> |
| 24 | #include <gtest/gtest.h> |
| 25 | #include <openssl/sha.h> |
| 26 | #include <ziparchive/zip_writer.h> |
| 27 | |
| 28 | #include "common/test_constants.h" |
| 29 | #include "package.h" |
| 30 | |
| 31 | class PackageTest : public ::testing::Test { |
| 32 | protected: |
| 33 | void SetUp() override; |
| 34 | |
| 35 | // A list of package classes for test, including MemoryPackage and FilePackage. |
| 36 | std::vector<std::unique_ptr<Package>> packages_; |
| 37 | |
| 38 | TemporaryFile temp_file_; // test package file. |
| 39 | std::string file_content_; // actual bytes of the package file. |
| 40 | }; |
| 41 | |
| 42 | void PackageTest::SetUp() { |
| 43 | std::vector<std::string> entries = { "file1.txt", "file2.txt", "dir1/file3.txt" }; |
| 44 | FILE* file_ptr = fdopen(temp_file_.release(), "wb"); |
| 45 | ZipWriter writer(file_ptr); |
| 46 | for (const auto& entry : entries) { |
| 47 | ASSERT_EQ(0, writer.StartEntry(entry.c_str(), ZipWriter::kCompress)); |
| 48 | ASSERT_EQ(0, writer.WriteBytes(entry.c_str(), entry.size())); |
| 49 | ASSERT_EQ(0, writer.FinishEntry()); |
| 50 | } |
| 51 | writer.Finish(); |
| 52 | ASSERT_EQ(0, fclose(file_ptr)); |
| 53 | |
| 54 | ASSERT_TRUE(android::base::ReadFileToString(temp_file_.path, &file_content_)); |
| 55 | auto memory_package = Package::CreateMemoryPackage(temp_file_.path, nullptr); |
| 56 | ASSERT_TRUE(memory_package); |
| 57 | packages_.emplace_back(std::move(memory_package)); |
| 58 | |
| 59 | auto file_package = Package::CreateFilePackage(temp_file_.path, nullptr); |
| 60 | ASSERT_TRUE(file_package); |
| 61 | packages_.emplace_back(std::move(file_package)); |
| 62 | } |
| 63 | |
| 64 | TEST_F(PackageTest, ReadFullyAtOffset_success) { |
| 65 | for (const auto& package : packages_) { |
| 66 | std::vector<uint8_t> buffer(file_content_.size()); |
| 67 | ASSERT_TRUE(package->ReadFullyAtOffset(buffer.data(), file_content_.size(), 0)); |
| 68 | ASSERT_EQ(file_content_, std::string(buffer.begin(), buffer.end())); |
| 69 | |
| 70 | ASSERT_TRUE(package->ReadFullyAtOffset(buffer.data(), file_content_.size() - 10, 10)); |
| 71 | ASSERT_EQ(file_content_.substr(10), std::string(buffer.begin(), buffer.end() - 10)); |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | TEST_F(PackageTest, ReadFullyAtOffset_failure) { |
| 76 | for (const auto& package : packages_) { |
| 77 | std::vector<uint8_t> buffer(file_content_.size()); |
| 78 | // Out of bound read. |
| 79 | ASSERT_FALSE(package->ReadFullyAtOffset(buffer.data(), file_content_.size(), 10)); |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | TEST_F(PackageTest, UpdateHashAtOffset_sha1_hash) { |
| 84 | // Check that the hash matches for first half of the file. |
| 85 | uint64_t hash_size = file_content_.size() / 2; |
| 86 | std::vector<uint8_t> expected_sha(SHA_DIGEST_LENGTH); |
| 87 | SHA1(reinterpret_cast<uint8_t*>(file_content_.data()), hash_size, expected_sha.data()); |
| 88 | |
| 89 | for (const auto& package : packages_) { |
| 90 | SHA_CTX ctx; |
| 91 | SHA1_Init(&ctx); |
| 92 | std::vector<HasherUpdateCallback> hashers{ std::bind(&SHA1_Update, &ctx, std::placeholders::_1, |
| 93 | std::placeholders::_2) }; |
| 94 | package->UpdateHashAtOffset(hashers, 0, hash_size); |
| 95 | |
| 96 | std::vector<uint8_t> calculated_sha(SHA_DIGEST_LENGTH); |
| 97 | SHA1_Final(calculated_sha.data(), &ctx); |
| 98 | ASSERT_EQ(expected_sha, calculated_sha); |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | TEST_F(PackageTest, GetZipArchiveHandle_extract_entry) { |
| 103 | for (const auto& package : packages_) { |
| 104 | ZipArchiveHandle zip = package->GetZipArchiveHandle(); |
| 105 | ASSERT_TRUE(zip); |
| 106 | |
| 107 | // Check that we can extract one zip entry. |
| 108 | std::string entry_name = "dir1/file3.txt"; |
| 109 | ZipString path(entry_name.c_str()); |
| 110 | ZipEntry entry; |
| 111 | ASSERT_EQ(0, FindEntry(zip, path, &entry)); |
| 112 | |
| 113 | std::vector<uint8_t> extracted(entry_name.size()); |
| 114 | ASSERT_EQ(0, ExtractToMemory(zip, &entry, extracted.data(), extracted.size())); |
| 115 | ASSERT_EQ(entry_name, std::string(extracted.begin(), extracted.end())); |
| 116 | } |
| 117 | } |