summaryrefslogtreecommitdiffstats
path: root/tests/unit/package_test.cpp
blob: 66882bb405e0785b80b09b12f9caa9923a1b3f2a (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
/*
 * Copyright (C) 2019 The Android Open Source Project
 *
 * Licensed under the Apache License, version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agree to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <stdio.h>

#include <functional>
#include <string>
#include <vector>

#include <android-base/file.h>
#include <gtest/gtest.h>
#include <openssl/sha.h>
#include <ziparchive/zip_writer.h>

#include "common/test_constants.h"
#include "otautil/package.h"

class PackageTest : public ::testing::Test {
 protected:
  void SetUp() override;

  // A list of package classes for test, including MemoryPackage and FilePackage.
  std::vector<std::unique_ptr<Package>> packages_;

  TemporaryFile temp_file_;   // test package file.
  std::string file_content_;  // actual bytes of the package file.
};

void PackageTest::SetUp() {
  std::vector<std::string> entries = { "file1.txt", "file2.txt", "dir1/file3.txt" };
  FILE* file_ptr = fdopen(temp_file_.release(), "wb");
  ZipWriter writer(file_ptr);
  for (const auto& entry : entries) {
    ASSERT_EQ(0, writer.StartEntry(entry.c_str(), ZipWriter::kCompress));
    ASSERT_EQ(0, writer.WriteBytes(entry.c_str(), entry.size()));
    ASSERT_EQ(0, writer.FinishEntry());
  }
  writer.Finish();
  ASSERT_EQ(0, fclose(file_ptr));

  ASSERT_TRUE(android::base::ReadFileToString(temp_file_.path, &file_content_));
  auto memory_package = Package::CreateMemoryPackage(temp_file_.path, nullptr);
  ASSERT_TRUE(memory_package);
  packages_.emplace_back(std::move(memory_package));

  auto file_package = Package::CreateFilePackage(temp_file_.path, nullptr);
  ASSERT_TRUE(file_package);
  packages_.emplace_back(std::move(file_package));
}

TEST_F(PackageTest, ReadFullyAtOffset_success) {
  for (const auto& package : packages_) {
    std::vector<uint8_t> buffer(file_content_.size());
    ASSERT_TRUE(package->ReadFullyAtOffset(buffer.data(), file_content_.size(), 0));
    ASSERT_EQ(file_content_, std::string(buffer.begin(), buffer.end()));

    ASSERT_TRUE(package->ReadFullyAtOffset(buffer.data(), file_content_.size() - 10, 10));
    ASSERT_EQ(file_content_.substr(10), std::string(buffer.begin(), buffer.end() - 10));
  }
}

TEST_F(PackageTest, ReadFullyAtOffset_failure) {
  for (const auto& package : packages_) {
    std::vector<uint8_t> buffer(file_content_.size());
    // Out of bound read.
    ASSERT_FALSE(package->ReadFullyAtOffset(buffer.data(), file_content_.size(), 10));
  }
}

TEST_F(PackageTest, UpdateHashAtOffset_sha1_hash) {
  // Check that the hash matches for first half of the file.
  uint64_t hash_size = file_content_.size() / 2;
  std::vector<uint8_t> expected_sha(SHA_DIGEST_LENGTH);
  SHA1(reinterpret_cast<uint8_t*>(file_content_.data()), hash_size, expected_sha.data());

  for (const auto& package : packages_) {
    SHA_CTX ctx;
    SHA1_Init(&ctx);
    std::vector<HasherUpdateCallback> hashers{ std::bind(&SHA1_Update, &ctx, std::placeholders::_1,
                                                         std::placeholders::_2) };
    package->UpdateHashAtOffset(hashers, 0, hash_size);

    std::vector<uint8_t> calculated_sha(SHA_DIGEST_LENGTH);
    SHA1_Final(calculated_sha.data(), &ctx);
    ASSERT_EQ(expected_sha, calculated_sha);
  }
}

TEST_F(PackageTest, GetZipArchiveHandle_extract_entry) {
  for (const auto& package : packages_) {
    ZipArchiveHandle zip = package->GetZipArchiveHandle();
    ASSERT_TRUE(zip);

    // Check that we can extract one zip entry.
    std::string_view entry_name = "dir1/file3.txt";
    ZipEntry64 entry;
    ASSERT_EQ(0, FindEntry(zip, entry_name, &entry));

    std::vector<uint8_t> extracted(entry_name.size());
    ASSERT_EQ(0, ExtractToMemory(zip, &entry, extracted.data(), extracted.size()));
    ASSERT_EQ(entry_name, std::string(extracted.begin(), extracted.end()));
  }
}