| /* | |
| * Copyright (C) 2017 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 agreed 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 <stdlib.h> | |
| #include "test/Test.h" | |
| namespace aapt { | |
| using ArchiveTest = TestDirectoryFixture; | |
| constexpr size_t kTestDataLength = 100; | |
| class TestData : public io::MallocData { | |
| public: | |
| TestData(std::unique_ptr<uint8_t[]>& data, size_t size) | |
| : MallocData(std::move(data), size) {} | |
| bool HadError() const override { return !error_.empty(); } | |
| std::string GetError() const override { return error_; } | |
| std::string error_; | |
| }; | |
| class TzSetter { | |
| public: | |
| explicit TzSetter(const std::string& new_tz) { | |
| old_tz_ = getenv("TZ"); | |
| new_tz_ = "TZ=" + new_tz; | |
| putenv(const_cast<char*>(new_tz_.c_str())); | |
| tzset(); | |
| } | |
| ~TzSetter() { | |
| if (old_tz_) { | |
| putenv(old_tz_); | |
| } else { | |
| putenv(const_cast<char*>("TZ")); | |
| } | |
| tzset(); | |
| } | |
| private: | |
| char* old_tz_; | |
| std::string new_tz_; | |
| }; | |
| std::unique_ptr<uint8_t[]> MakeTestArray() { | |
| auto array = std::make_unique<uint8_t[]>(kTestDataLength); | |
| for (int index = 0; index < kTestDataLength; ++index) { | |
| array[index] = static_cast<uint8_t>(rand()); | |
| } | |
| return array; | |
| } | |
| std::unique_ptr<IArchiveWriter> MakeDirectoryWriter(const std::string& output_path) { | |
| file::mkdirs(output_path); | |
| StdErrDiagnostics diag; | |
| return CreateDirectoryArchiveWriter(&diag, output_path); | |
| } | |
| std::unique_ptr<IArchiveWriter> MakeZipFileWriter(const std::string& output_path) { | |
| file::mkdirs(std::string(file::GetStem(output_path))); | |
| std::remove(output_path.c_str()); | |
| StdErrDiagnostics diag; | |
| return CreateZipFileArchiveWriter(&diag, output_path); | |
| } | |
| void VerifyDirectory(const std::string& path, const std::string& file, const uint8_t array[]) { | |
| std::string file_path = file::BuildPath({path, file}); | |
| auto buffer = std::make_unique<char[]>(kTestDataLength); | |
| std::ifstream stream(file_path); | |
| stream.read(buffer.get(), kTestDataLength); | |
| for (int index = 0; index < kTestDataLength; ++index) { | |
| ASSERT_EQ(array[index], static_cast<uint8_t>(buffer[index])); | |
| } | |
| } | |
| void VerifyZipFile(const std::string& output_path, const std::string& file, const uint8_t array[]) { | |
| std::unique_ptr<io::ZipFileCollection> zip = io::ZipFileCollection::Create(output_path, nullptr); | |
| std::unique_ptr<android::InputStream> stream = zip->FindFile(file)->OpenInputStream(); | |
| std::vector<uint8_t> buffer; | |
| const void* data; | |
| size_t size; | |
| while (stream->Next(&data, &size)) { | |
| auto pointer = static_cast<const uint8_t*>(data); | |
| buffer.insert(buffer.end(), pointer, pointer + size); | |
| } | |
| for (int index = 0; index < kTestDataLength; ++index) { | |
| ASSERT_EQ(array[index], buffer[index]); | |
| } | |
| } | |
| void VerifyZipFileTimestamps(const std::string& output_path) { | |
| std::unique_ptr<io::ZipFileCollection> zip = io::ZipFileCollection::Create(output_path, nullptr); | |
| auto it = zip->Iterator(); | |
| while (it->HasNext()) { | |
| auto file = it->Next(); | |
| struct tm modification_time; | |
| ASSERT_TRUE(file->GetModificationTime(&modification_time)); | |
| EXPECT_EQ(modification_time.tm_year, 80); | |
| EXPECT_EQ(modification_time.tm_mon, 0); | |
| EXPECT_EQ(modification_time.tm_mday, 1); | |
| EXPECT_EQ(modification_time.tm_hour, 0); | |
| EXPECT_EQ(modification_time.tm_min, 0); | |
| EXPECT_EQ(modification_time.tm_sec, 0); | |
| } | |
| } | |
| TEST_F(ArchiveTest, DirectoryWriteEntrySuccess) { | |
| std::string output_path = GetTestPath("output"); | |
| std::unique_ptr<IArchiveWriter> writer = MakeDirectoryWriter(output_path); | |
| std::unique_ptr<uint8_t[]> data1 = MakeTestArray(); | |
| std::unique_ptr<uint8_t[]> data2 = MakeTestArray(); | |
| ASSERT_TRUE(writer->StartEntry("test1", 0)); | |
| ASSERT_TRUE(writer->Write(static_cast<const void*>(data1.get()), kTestDataLength)); | |
| ASSERT_TRUE(writer->FinishEntry()); | |
| ASSERT_FALSE(writer->HadError()); | |
| ASSERT_TRUE(writer->StartEntry("test2", 0)); | |
| ASSERT_TRUE(writer->Write(static_cast<const void*>(data2.get()), kTestDataLength)); | |
| ASSERT_TRUE(writer->FinishEntry()); | |
| ASSERT_FALSE(writer->HadError()); | |
| writer.reset(); | |
| VerifyDirectory(output_path, "test1", data1.get()); | |
| VerifyDirectory(output_path, "test2", data2.get()); | |
| } | |
| TEST_F(ArchiveTest, DirectoryWriteFileSuccess) { | |
| std::string output_path = GetTestPath("output"); | |
| std::unique_ptr<IArchiveWriter> writer = MakeDirectoryWriter(output_path); | |
| std::unique_ptr<uint8_t[]> data1 = MakeTestArray(); | |
| auto data1_copy = std::make_unique<uint8_t[]>(kTestDataLength); | |
| std::copy(data1.get(), data1.get() + kTestDataLength, data1_copy.get()); | |
| std::unique_ptr<uint8_t[]> data2 = MakeTestArray(); | |
| auto data2_copy = std::make_unique<uint8_t[]>(kTestDataLength); | |
| std::copy(data2.get(), data2.get() + kTestDataLength, data2_copy.get()); | |
| auto input1 = std::make_unique<TestData>(data1_copy, kTestDataLength); | |
| auto input2 = std::make_unique<TestData>(data2_copy, kTestDataLength); | |
| ASSERT_TRUE(writer->WriteFile("test1", 0, input1.get())); | |
| ASSERT_FALSE(writer->HadError()); | |
| ASSERT_TRUE(writer->WriteFile("test2", 0, input2.get())); | |
| ASSERT_FALSE(writer->HadError()); | |
| writer.reset(); | |
| VerifyDirectory(output_path, "test1", data1.get()); | |
| VerifyDirectory(output_path, "test2", data2.get()); | |
| } | |
| TEST_F(ArchiveTest, DirectoryWriteFileError) { | |
| std::string output_path = GetTestPath("output"); | |
| std::unique_ptr<IArchiveWriter> writer = MakeDirectoryWriter(output_path); | |
| std::unique_ptr<uint8_t[]> data = MakeTestArray(); | |
| auto input = std::make_unique<TestData>(data, kTestDataLength); | |
| input->error_ = "DirectoryWriteFileError"; | |
| ASSERT_FALSE(writer->WriteFile("test", 0, input.get())); | |
| ASSERT_TRUE(writer->HadError()); | |
| ASSERT_EQ("DirectoryWriteFileError", writer->GetError()); | |
| } | |
| TEST_F(ArchiveTest, ZipFileWriteEntrySuccess) { | |
| std::string output_path = GetTestPath("output.apk"); | |
| std::unique_ptr<IArchiveWriter> writer = MakeZipFileWriter(output_path); | |
| std::unique_ptr<uint8_t[]> data1 = MakeTestArray(); | |
| std::unique_ptr<uint8_t[]> data2 = MakeTestArray(); | |
| ASSERT_TRUE(writer->StartEntry("test1", 0)); | |
| ASSERT_TRUE(writer->Write(static_cast<const void*>(data1.get()), kTestDataLength)); | |
| ASSERT_TRUE(writer->FinishEntry()); | |
| ASSERT_FALSE(writer->HadError()); | |
| ASSERT_TRUE(writer->StartEntry("test2", 0)); | |
| ASSERT_TRUE(writer->Write(static_cast<const void*>(data2.get()), kTestDataLength)); | |
| ASSERT_TRUE(writer->FinishEntry()); | |
| ASSERT_FALSE(writer->HadError()); | |
| writer.reset(); | |
| VerifyZipFile(output_path, "test1", data1.get()); | |
| VerifyZipFile(output_path, "test2", data2.get()); | |
| } | |
| TEST_F(ArchiveTest, ZipFileWriteFileSuccess) { | |
| std::string output_path = GetTestPath("output.apk"); | |
| std::unique_ptr<IArchiveWriter> writer = MakeZipFileWriter(output_path); | |
| std::unique_ptr<uint8_t[]> data1 = MakeTestArray(); | |
| auto data1_copy = std::make_unique<uint8_t[]>(kTestDataLength); | |
| std::copy(data1.get(), data1.get() + kTestDataLength, data1_copy.get()); | |
| std::unique_ptr<uint8_t[]> data2 = MakeTestArray(); | |
| auto data2_copy = std::make_unique<uint8_t[]>(kTestDataLength); | |
| std::copy(data2.get(), data2.get() + kTestDataLength, data2_copy.get()); | |
| auto input1 = std::make_unique<TestData>(data1_copy, kTestDataLength); | |
| auto input2 = std::make_unique<TestData>(data2_copy, kTestDataLength); | |
| ASSERT_TRUE(writer->WriteFile("test1", 0, input1.get())); | |
| ASSERT_FALSE(writer->HadError()); | |
| ASSERT_TRUE(writer->WriteFile("test2", 0, input2.get())); | |
| ASSERT_FALSE(writer->HadError()); | |
| writer.reset(); | |
| VerifyZipFile(output_path, "test1", data1.get()); | |
| VerifyZipFile(output_path, "test2", data2.get()); | |
| } | |
| TEST_F(ArchiveTest, ZipFileWriteFileError) { | |
| std::string output_path = GetTestPath("output.apk"); | |
| std::unique_ptr<IArchiveWriter> writer = MakeZipFileWriter(output_path); | |
| std::unique_ptr<uint8_t[]> data = MakeTestArray(); | |
| auto input = std::make_unique<TestData>(data, kTestDataLength); | |
| input->error_ = "ZipFileWriteFileError"; | |
| ASSERT_FALSE(writer->WriteFile("test", 0, input.get())); | |
| ASSERT_TRUE(writer->HadError()); | |
| ASSERT_EQ("ZipFileWriteFileError", writer->GetError()); | |
| } | |
| TEST_F(ArchiveTest, ZipFileTimeZoneUTC) { | |
| TzSetter tz("UTC0"); | |
| std::string output_path = GetTestPath("output.apk"); | |
| std::unique_ptr<IArchiveWriter> writer = MakeZipFileWriter(output_path); | |
| std::unique_ptr<uint8_t[]> data1 = MakeTestArray(); | |
| std::unique_ptr<uint8_t[]> data2 = MakeTestArray(); | |
| ASSERT_TRUE(writer->StartEntry("test1", 0)); | |
| ASSERT_TRUE(writer->Write(static_cast<const void*>(data1.get()), kTestDataLength)); | |
| ASSERT_TRUE(writer->FinishEntry()); | |
| ASSERT_FALSE(writer->HadError()); | |
| ASSERT_TRUE(writer->StartEntry("test2", 0)); | |
| ASSERT_TRUE(writer->Write(static_cast<const void*>(data2.get()), kTestDataLength)); | |
| ASSERT_TRUE(writer->FinishEntry()); | |
| ASSERT_FALSE(writer->HadError()); | |
| writer.reset(); | |
| // All zip file entries must have the same timestamp, regardless of time zone. See: b/277978832 | |
| VerifyZipFileTimestamps(output_path); | |
| } | |
| TEST_F(ArchiveTest, ZipFileTimeZoneWestOfUTC) { | |
| TzSetter tz("PST8"); | |
| std::string output_path = GetTestPath("output.apk"); | |
| std::unique_ptr<IArchiveWriter> writer = MakeZipFileWriter(output_path); | |
| std::unique_ptr<uint8_t[]> data1 = MakeTestArray(); | |
| std::unique_ptr<uint8_t[]> data2 = MakeTestArray(); | |
| ASSERT_TRUE(writer->StartEntry("test1", 0)); | |
| ASSERT_TRUE(writer->Write(static_cast<const void*>(data1.get()), kTestDataLength)); | |
| ASSERT_TRUE(writer->FinishEntry()); | |
| ASSERT_FALSE(writer->HadError()); | |
| ASSERT_TRUE(writer->StartEntry("test2", 0)); | |
| ASSERT_TRUE(writer->Write(static_cast<const void*>(data2.get()), kTestDataLength)); | |
| ASSERT_TRUE(writer->FinishEntry()); | |
| ASSERT_FALSE(writer->HadError()); | |
| writer.reset(); | |
| // All zip file entries must have the same timestamp, regardless of time zone. See: b/277978832 | |
| VerifyZipFileTimestamps(output_path); | |
| } | |
| TEST_F(ArchiveTest, ZipFileTimeZoneEastOfUTC) { | |
| TzSetter tz("EET-2"); | |
| std::string output_path = GetTestPath("output.apk"); | |
| std::unique_ptr<IArchiveWriter> writer = MakeZipFileWriter(output_path); | |
| std::unique_ptr<uint8_t[]> data1 = MakeTestArray(); | |
| std::unique_ptr<uint8_t[]> data2 = MakeTestArray(); | |
| ASSERT_TRUE(writer->StartEntry("test1", 0)); | |
| ASSERT_TRUE(writer->Write(static_cast<const void*>(data1.get()), kTestDataLength)); | |
| ASSERT_TRUE(writer->FinishEntry()); | |
| ASSERT_FALSE(writer->HadError()); | |
| ASSERT_TRUE(writer->StartEntry("test2", 0)); | |
| ASSERT_TRUE(writer->Write(static_cast<const void*>(data2.get()), kTestDataLength)); | |
| ASSERT_TRUE(writer->FinishEntry()); | |
| ASSERT_FALSE(writer->HadError()); | |
| writer.reset(); | |
| // All zip file entries must have the same timestamp, regardless of time zone. See: b/277978832 | |
| VerifyZipFileTimestamps(output_path); | |
| } | |
| TEST_F(ArchiveTest, ZipFileWriteRawEntrySuccess) { | |
| std::string output_path = GetTestPath("output.apk"); | |
| std::unique_ptr<IArchiveWriter> writer = MakeZipFileWriter(output_path); | |
| std::vector<uint8_t> raw_payload = {0x01, 0x02, 0x03, 0x04}; | |
| auto raw_data = std::make_unique<uint8_t[]>(raw_payload.size()); | |
| std::copy(raw_payload.begin(), raw_payload.end(), raw_data.get()); | |
| auto input = std::make_unique<TestData>(raw_data, raw_payload.size()); | |
| uint16_t method = 8; | |
| uint16_t gpbf = 2 | 0x0008; // High compression with data descriptor flag | |
| uint32_t uncompressed_size = 100; | |
| uint32_t compressed_size = raw_payload.size(); | |
| uint32_t crc32 = 0x12345678; | |
| ::ZipEntry64 info{}; | |
| info.method = method; | |
| info.gpbf = gpbf; | |
| info.uncompressed_length = uncompressed_size; | |
| info.compressed_length = compressed_size; | |
| info.crc32 = crc32; | |
| ASSERT_TRUE(writer->WriteFileRaw("raw_test", &info, input.get())); | |
| ASSERT_FALSE(writer->HadError()); | |
| writer.reset(); | |
| std::unique_ptr<io::ZipFileCollection> zip = io::ZipFileCollection::Create(output_path, nullptr); | |
| ASSERT_NE(zip, nullptr); | |
| io::IFile* file = zip->FindFile("raw_test"); | |
| ASSERT_NE(file, nullptr); | |
| auto zip_file = static_cast<io::ZipFile*>(file); | |
| const ::ZipEntry64* entry = zip_file->GetZipEntry(); | |
| // libziparchive's StartRawEntryWithTime clears the data descriptor flag. | |
| uint16_t expected_gpbf = gpbf & ~0x0008; | |
| EXPECT_EQ(entry->method, method); | |
| EXPECT_EQ(entry->gpbf, expected_gpbf); | |
| EXPECT_EQ(entry->uncompressed_length, uncompressed_size); | |
| EXPECT_EQ(entry->compressed_length, compressed_size); | |
| EXPECT_EQ(entry->crc32, crc32); | |
| } | |
| TEST_F(ArchiveTest, ZipFileWriteMinimumCompression) { | |
| std::string output_path = GetTestPath("output.apk"); | |
| std::unique_ptr<IArchiveWriter> writer = MakeZipFileWriter(output_path); | |
| // Mock data is made to be highly compressible to yield significant gains upon compression. | |
| // Otherwise the test file will be rewound and stored as per the behavior in | |
| // ZipFileWriter.WriteFile. | |
| std::unique_ptr<uint8_t[]> data = std::make_unique<uint8_t[]>(kTestDataLength); | |
| std::fill(data.get(), data.get() + kTestDataLength, 0); | |
| auto input = std::make_unique<TestData>(data, kTestDataLength); | |
| // Write file with kCompressOThroughput | |
| ASSERT_TRUE(writer->WriteFile("test_min", ArchiveEntry::kCompressOThroughput, input.get())); | |
| ASSERT_FALSE(writer->HadError()); | |
| writer.reset(); | |
| // Read the written ZIP file back | |
| std::unique_ptr<io::ZipFileCollection> zip = io::ZipFileCollection::Create(output_path, nullptr); | |
| ASSERT_NE(zip, nullptr); | |
| io::IFile* file = zip->FindFile("test_min"); | |
| ASSERT_NE(file, nullptr); | |
| auto zip_file = static_cast<io::ZipFile*>(file); | |
| const ::ZipEntry64* entry = zip_file->GetZipEntry(); | |
| // Verify standard ZIP fields | |
| EXPECT_EQ(entry->method, 8); // Deflate method | |
| int deflate_level = (entry->gpbf >> 1) & 0x3; | |
| EXPECT_EQ(deflate_level, 3); // GPB bits 1 and 2 set -> Super Fast (kGPBCompressionMinimum) | |
| // Verify compression level | |
| EXPECT_TRUE(zip_file->UsesMinCompression()); | |
| EXPECT_FALSE(zip_file->UsesMaxCompression()); | |
| } | |
| TEST_F(ArchiveTest, ZipFileCompressionLevelDetection) { | |
| std::string output_path = GetTestPath("output.apk"); | |
| std::unique_ptr<IArchiveWriter> writer = MakeZipFileWriter(output_path); | |
| std::unique_ptr<uint8_t[]> data = std::make_unique<uint8_t[]>(kTestDataLength); | |
| std::fill(data.get(), data.get() + kTestDataLength, 0); | |
| auto data_copy1 = std::make_unique<uint8_t[]>(kTestDataLength); | |
| std::copy(data.get(), data.get() + kTestDataLength, data_copy1.get()); | |
| auto input_stored = std::make_unique<TestData>(data_copy1, kTestDataLength); | |
| auto data_copy2 = std::make_unique<uint8_t[]>(kTestDataLength); | |
| std::copy(data.get(), data.get() + kTestDataLength, data_copy2.get()); | |
| auto input_max = std::make_unique<TestData>(data_copy2, kTestDataLength); | |
| auto data_copy3 = std::make_unique<uint8_t[]>(kTestDataLength); | |
| std::copy(data.get(), data.get() + kTestDataLength, data_copy3.get()); | |
| auto input_min = std::make_unique<TestData>(data_copy3, kTestDataLength); | |
| // 1. Stored (no compression) | |
| ASSERT_TRUE(writer->WriteFile("stored", 0, input_stored.get())); | |
| // 2. Max compression | |
| ASSERT_TRUE(writer->WriteFile("max", ArchiveEntry::kCompress, input_max.get())); | |
| // 3. Min compression | |
| ASSERT_TRUE(writer->WriteFile("min", ArchiveEntry::kCompressOThroughput, input_min.get())); | |
| ASSERT_FALSE(writer->HadError()); | |
| writer.reset(); | |
| // Read back and verify | |
| std::unique_ptr<io::ZipFileCollection> zip = io::ZipFileCollection::Create(output_path, nullptr); | |
| ASSERT_NE(zip, nullptr); | |
| // Verify Stored | |
| { | |
| io::IFile* file = zip->FindFile("stored"); | |
| ASSERT_NE(file, nullptr); | |
| EXPECT_FALSE(file->WasCompressed()); | |
| EXPECT_FALSE(file->UsesMinCompression()); | |
| EXPECT_FALSE(file->UsesMaxCompression()); | |
| } | |
| // Verify Max | |
| { | |
| io::IFile* file = zip->FindFile("max"); | |
| ASSERT_NE(file, nullptr); | |
| EXPECT_TRUE(file->WasCompressed()); | |
| EXPECT_FALSE(file->UsesMinCompression()); | |
| EXPECT_TRUE(file->UsesMaxCompression()); | |
| } | |
| // Verify Min | |
| { | |
| io::IFile* file = zip->FindFile("min"); | |
| ASSERT_NE(file, nullptr); | |
| EXPECT_TRUE(file->WasCompressed()); | |
| EXPECT_TRUE(file->UsesMinCompression()); | |
| EXPECT_FALSE(file->UsesMaxCompression()); | |
| } | |
| } | |
| } // namespace aapt |