From 2078b22e4145fef2648cc714eae6588353940c4b Mon Sep 17 00:00:00 2001 From: Tianjie Xu Date: Wed, 22 Mar 2017 12:27:26 -0700 Subject: Add the missing sr-Latn into png files and rename the png locale header Switch the locale header in the png files from Locale.toString() to Locale.toLanguageTag(). For example, en_US --> en-us and sr__#Latn --> sr-Latn. Also clean up recovery a bit to expect the new locale format. Bug: 35215015 Test: sr-Latn shows correctly under graphic tests && recovery tests pass Change-Id: Ic62bab7756cdc6e5f98f26076f7c2dd046f811db --- tests/unit/locale_test.cpp | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) (limited to 'tests/unit') diff --git a/tests/unit/locale_test.cpp b/tests/unit/locale_test.cpp index f73235005..cdaba0e8b 100644 --- a/tests/unit/locale_test.cpp +++ b/tests/unit/locale_test.cpp @@ -19,14 +19,15 @@ #include "minui/minui.h" TEST(LocaleTest, Misc) { - EXPECT_TRUE(matches_locale("zh_CN", "zh_CN_#Hans")); - EXPECT_TRUE(matches_locale("zh", "zh_CN_#Hans")); - EXPECT_FALSE(matches_locale("zh_HK", "zh_CN_#Hans")); - EXPECT_TRUE(matches_locale("en_GB", "en_GB")); - EXPECT_TRUE(matches_locale("en", "en_GB")); - EXPECT_FALSE(matches_locale("en_GB", "en")); - EXPECT_FALSE(matches_locale("en_GB", "en_US")); - EXPECT_FALSE(matches_locale("en_US", "")); - // Empty locale prefix in the PNG file will match the input locale. - EXPECT_TRUE(matches_locale("", "en_US")); + EXPECT_TRUE(matches_locale("zh-CN", "zh-Hans-CN")); + EXPECT_TRUE(matches_locale("zh", "zh-Hans-CN")); + EXPECT_FALSE(matches_locale("zh-HK", "zh-Hans-CN")); + EXPECT_TRUE(matches_locale("en-GB", "en-GB")); + EXPECT_TRUE(matches_locale("en", "en-GB")); + EXPECT_FALSE(matches_locale("en-GB", "en")); + EXPECT_FALSE(matches_locale("en-GB", "en-US")); + EXPECT_FALSE(matches_locale("en-US", "")); + // Empty locale prefix in the PNG file will match the input locale. + EXPECT_TRUE(matches_locale("", "en-US")); + EXPECT_TRUE(matches_locale("sr-Latn", "sr-Latn-BA")); } -- cgit v1.2.3 From 8f23757ad42352f57db0b2ab9fc8b6e92eeaedd9 Mon Sep 17 00:00:00 2001 From: Tao Bao Date: Sun, 26 Mar 2017 13:36:49 -0700 Subject: Move parse_range() and range_overlaps() into RangeSet. Also move RangeSet into a header file to make it testable, and add unit tests. In RangeSet::Parse() (the former parse_range()), use libbase logging to do assertions. This has the same effect as the previous exit(EXIT_FAILURE) to terminate the updater process and abort an update. The difference lies in the exit status code (i.e. WEXITSTATUS(status) in install.cpp), which changes from 1 (i.e. EXIT_FAILURE) to 0. Test: recovery_unit_test Test: Apply an incremental update with the new updater. Change-Id: Ie8393c78b0d8ae0fd5f0ca0646d871308d71fff0 --- tests/unit/rangeset_test.cpp | 84 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 84 insertions(+) create mode 100644 tests/unit/rangeset_test.cpp (limited to 'tests/unit') diff --git a/tests/unit/rangeset_test.cpp b/tests/unit/rangeset_test.cpp new file mode 100644 index 000000000..e66da20e4 --- /dev/null +++ b/tests/unit/rangeset_test.cpp @@ -0,0 +1,84 @@ +/* + * 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 +#include + +#include + +#include + +#include "updater/rangeset.h" + +TEST(RangeSetTest, Parse_smoke) { + RangeSet rs = RangeSet::Parse("2,1,10"); + ASSERT_EQ(static_cast(1), rs.count); + ASSERT_EQ((std::vector{ 1, 10 }), rs.pos); + ASSERT_EQ(static_cast(9), rs.size); + + RangeSet rs2 = RangeSet::Parse("4,15,20,1,10"); + ASSERT_EQ(static_cast(2), rs2.count); + ASSERT_EQ((std::vector{ 15, 20, 1, 10 }), rs2.pos); + ASSERT_EQ(static_cast(14), rs2.size); + + // Leading zeros are fine. But android::base::ParseUint() doesn't like trailing zeros like "10 ". + ASSERT_EQ(rs, RangeSet::Parse(" 2, 1, 10")); + ASSERT_EXIT(RangeSet::Parse("2,1,10 "), ::testing::KilledBySignal(SIGABRT), ""); +} + +TEST(RangeSetTest, Parse_InvalidCases) { + // Insufficient number of tokens. + ASSERT_EXIT(RangeSet::Parse(""), ::testing::KilledBySignal(SIGABRT), ""); + ASSERT_EXIT(RangeSet::Parse("2,1"), ::testing::KilledBySignal(SIGABRT), ""); + + // The first token (i.e. the number of following tokens) is invalid. + ASSERT_EXIT(RangeSet::Parse("a,1,1"), ::testing::KilledBySignal(SIGABRT), ""); + ASSERT_EXIT(RangeSet::Parse("3,1,1"), ::testing::KilledBySignal(SIGABRT), ""); + ASSERT_EXIT(RangeSet::Parse("-3,1,1"), ::testing::KilledBySignal(SIGABRT), ""); + ASSERT_EXIT(RangeSet::Parse("2,1,2,3"), ::testing::KilledBySignal(SIGABRT), ""); + + // Invalid tokens. + ASSERT_EXIT(RangeSet::Parse("2,1,10a"), ::testing::KilledBySignal(SIGABRT), ""); + ASSERT_EXIT(RangeSet::Parse("2,,10"), ::testing::KilledBySignal(SIGABRT), ""); + + // Empty or negative range. + ASSERT_EXIT(RangeSet::Parse("2,2,2"), ::testing::KilledBySignal(SIGABRT), ""); + ASSERT_EXIT(RangeSet::Parse("2,2,1"), ::testing::KilledBySignal(SIGABRT), ""); +} + +TEST(RangeSetTest, Overlaps) { + RangeSet r1 = RangeSet::Parse("2,1,6"); + RangeSet r2 = RangeSet::Parse("2,5,10"); + ASSERT_TRUE(r1.Overlaps(r2)); + ASSERT_TRUE(r2.Overlaps(r1)); + + r2 = RangeSet::Parse("2,6,10"); + ASSERT_FALSE(r1.Overlaps(r2)); + ASSERT_FALSE(r2.Overlaps(r1)); + + ASSERT_FALSE(RangeSet::Parse("2,3,5").Overlaps(RangeSet::Parse("2,5,7"))); + ASSERT_FALSE(RangeSet::Parse("2,5,7").Overlaps(RangeSet::Parse("2,3,5"))); +} + +TEST(RangeSetTest, GetBlockNumber) { + RangeSet rs = RangeSet::Parse("2,1,10"); + ASSERT_EQ(static_cast(1), rs.GetBlockNumber(0)); + ASSERT_EQ(static_cast(6), rs.GetBlockNumber(5)); + ASSERT_EQ(static_cast(9), rs.GetBlockNumber(8)); + + // Out of bound. + ASSERT_EXIT(rs.GetBlockNumber(9), ::testing::KilledBySignal(SIGABRT), ""); +} -- cgit v1.2.3 From bf5b77dbf73eef715bb49b43e113833efbcb2994 Mon Sep 17 00:00:00 2001 From: Tao Bao Date: Thu, 30 Mar 2017 16:57:29 -0700 Subject: Change the internal representation in RangeSet. This CL makes the following changes to RangeSet: - Uses std::pair to represent a Range; - Uses std::vector to represent a RangeSet; - Provides const iterators (forward and reverse); - Provides const accessor; - 'blocks()' returns the number of blocks (formerly 'size'); - 'size()' returns the number of Range's (formerly 'count'). Test: recovery_unit_test Test: Apply an incremental update with the new updater. Change-Id: Ia1fbb343370a152e1f7aa050cf914c2da09b1396 --- tests/unit/rangeset_test.cpp | 40 ++++++++++++++++++++++++++++++++++------ 1 file changed, 34 insertions(+), 6 deletions(-) (limited to 'tests/unit') diff --git a/tests/unit/rangeset_test.cpp b/tests/unit/rangeset_test.cpp index e66da20e4..3c6d77ef5 100644 --- a/tests/unit/rangeset_test.cpp +++ b/tests/unit/rangeset_test.cpp @@ -25,14 +25,15 @@ TEST(RangeSetTest, Parse_smoke) { RangeSet rs = RangeSet::Parse("2,1,10"); - ASSERT_EQ(static_cast(1), rs.count); - ASSERT_EQ((std::vector{ 1, 10 }), rs.pos); - ASSERT_EQ(static_cast(9), rs.size); + ASSERT_EQ(static_cast(1), rs.size()); + ASSERT_EQ((Range{ 1, 10 }), rs[0]); + ASSERT_EQ(static_cast(9), rs.blocks()); RangeSet rs2 = RangeSet::Parse("4,15,20,1,10"); - ASSERT_EQ(static_cast(2), rs2.count); - ASSERT_EQ((std::vector{ 15, 20, 1, 10 }), rs2.pos); - ASSERT_EQ(static_cast(14), rs2.size); + ASSERT_EQ(static_cast(2), rs2.size()); + ASSERT_EQ((Range{ 15, 20 }), rs2[0]); + ASSERT_EQ((Range{ 1, 10 }), rs2[1]); + ASSERT_EQ(static_cast(14), rs2.blocks()); // Leading zeros are fine. But android::base::ParseUint() doesn't like trailing zeros like "10 ". ASSERT_EQ(rs, RangeSet::Parse(" 2, 1, 10")); @@ -82,3 +83,30 @@ TEST(RangeSetTest, GetBlockNumber) { // Out of bound. ASSERT_EXIT(rs.GetBlockNumber(9), ::testing::KilledBySignal(SIGABRT), ""); } + +TEST(RangeSetTest, equality) { + ASSERT_EQ(RangeSet::Parse("2,1,6"), RangeSet::Parse("2,1,6")); + + ASSERT_NE(RangeSet::Parse("2,1,6"), RangeSet::Parse("2,1,7")); + ASSERT_NE(RangeSet::Parse("2,1,6"), RangeSet::Parse("2,2,7")); + + // The orders of Range's matter. "4,1,5,8,10" != "4,8,10,1,5". + ASSERT_NE(RangeSet::Parse("4,1,5,8,10"), RangeSet::Parse("4,8,10,1,5")); +} + +TEST(RangeSetTest, iterators) { + RangeSet rs = RangeSet::Parse("4,1,5,8,10"); + std::vector ranges; + for (const auto& range : rs) { + ranges.push_back(range); + } + ASSERT_EQ((std::vector{ Range{ 1, 5 }, Range{ 8, 10 } }), ranges); + + ranges.clear(); + + // Reverse iterators. + for (auto it = rs.crbegin(); it != rs.crend(); it++) { + ranges.push_back(*it); + } + ASSERT_EQ((std::vector{ Range{ 8, 10 }, Range{ 1, 5 } }), ranges); +} -- cgit v1.2.3 From b656a154ea497c1a179079f082b0a0701453bec5 Mon Sep 17 00:00:00 2001 From: Tao Bao Date: Tue, 18 Apr 2017 23:54:29 -0700 Subject: Move sysMapFile and sysReleaseMap into MemMapping class. Test: recovery_component_test Test: recovery_unit_test Test: Apply an OTA on angler. Change-Id: I7170f03e4ce1fe06184ca1d7bcce0a695f33ac4d --- tests/unit/sysutil_test.cpp | 60 +++++++++++++++++++-------------------------- tests/unit/zip_test.cpp | 5 ++-- 2 files changed, 27 insertions(+), 38 deletions(-) (limited to 'tests/unit') diff --git a/tests/unit/sysutil_test.cpp b/tests/unit/sysutil_test.cpp index f4699664b..434ee25bf 100644 --- a/tests/unit/sysutil_test.cpp +++ b/tests/unit/sysutil_test.cpp @@ -27,27 +27,23 @@ TEST(SysUtilTest, InvalidArgs) { MemMapping mapping; // Invalid argument. - ASSERT_EQ(-1, sysMapFile(nullptr, &mapping)); - ASSERT_EQ(-1, sysMapFile("/somefile", nullptr)); + ASSERT_FALSE(mapping.MapFile("")); } -TEST(SysUtilTest, sysMapFileRegularFile) { +TEST(SysUtilTest, MapFileRegularFile) { TemporaryFile temp_file1; std::string content = "abc"; ASSERT_TRUE(android::base::WriteStringToFile(content, temp_file1.path)); - // sysMapFile() should map the file to one range. + // MemMapping::MapFile() should map the file to one range. MemMapping mapping; - ASSERT_EQ(0, sysMapFile(temp_file1.path, &mapping)); + ASSERT_TRUE(mapping.MapFile(temp_file1.path)); ASSERT_NE(nullptr, mapping.addr); ASSERT_EQ(content.size(), mapping.length); - ASSERT_EQ(1U, mapping.ranges.size()); - - sysReleaseMap(&mapping); - ASSERT_EQ(0U, mapping.ranges.size()); + ASSERT_EQ(1U, mapping.ranges()); } -TEST(SysUtilTest, sysMapFileBlockMap) { +TEST(SysUtilTest, MapFileBlockMap) { // Create a file that has 10 blocks. TemporaryFile package; std::string content; @@ -63,78 +59,72 @@ TEST(SysUtilTest, sysMapFileBlockMap) { std::string block_map_content = std::string(package.path) + "\n40960 4096\n1\n0 10\n"; ASSERT_TRUE(android::base::WriteStringToFile(block_map_content, block_map_file.path)); - ASSERT_EQ(0, sysMapFile(filename.c_str(), &mapping)); + ASSERT_TRUE(mapping.MapFile(filename)); ASSERT_EQ(file_size, mapping.length); - ASSERT_EQ(1U, mapping.ranges.size()); + ASSERT_EQ(1U, mapping.ranges()); // It's okay to not have the trailing '\n'. block_map_content = std::string(package.path) + "\n40960 4096\n1\n0 10"; ASSERT_TRUE(android::base::WriteStringToFile(block_map_content, block_map_file.path)); - ASSERT_EQ(0, sysMapFile(filename.c_str(), &mapping)); + ASSERT_TRUE(mapping.MapFile(filename)); ASSERT_EQ(file_size, mapping.length); - ASSERT_EQ(1U, mapping.ranges.size()); + ASSERT_EQ(1U, mapping.ranges()); // Or having multiple trailing '\n's. block_map_content = std::string(package.path) + "\n40960 4096\n1\n0 10\n\n\n"; ASSERT_TRUE(android::base::WriteStringToFile(block_map_content, block_map_file.path)); - ASSERT_EQ(0, sysMapFile(filename.c_str(), &mapping)); + ASSERT_TRUE(mapping.MapFile(filename)); ASSERT_EQ(file_size, mapping.length); - ASSERT_EQ(1U, mapping.ranges.size()); + ASSERT_EQ(1U, mapping.ranges()); // Multiple ranges. block_map_content = std::string(package.path) + "\n40960 4096\n3\n0 3\n3 5\n5 10\n"; ASSERT_TRUE(android::base::WriteStringToFile(block_map_content, block_map_file.path)); - ASSERT_EQ(0, sysMapFile(filename.c_str(), &mapping)); + ASSERT_TRUE(mapping.MapFile(filename)); ASSERT_EQ(file_size, mapping.length); - ASSERT_EQ(3U, mapping.ranges.size()); - - sysReleaseMap(&mapping); - ASSERT_EQ(0U, mapping.ranges.size()); + ASSERT_EQ(3U, mapping.ranges()); } -TEST(SysUtilTest, sysMapFileBlockMapInvalidBlockMap) { +TEST(SysUtilTest, MapFileBlockMapInvalidBlockMap) { MemMapping mapping; TemporaryFile temp_file; std::string filename = std::string("@") + temp_file.path; // Block map file is too short. ASSERT_TRUE(android::base::WriteStringToFile("/somefile\n", temp_file.path)); - ASSERT_EQ(-1, sysMapFile(filename.c_str(), &mapping)); + ASSERT_FALSE(mapping.MapFile(filename)); ASSERT_TRUE(android::base::WriteStringToFile("/somefile\n4096 4096\n0\n", temp_file.path)); - ASSERT_EQ(-1, sysMapFile(filename.c_str(), &mapping)); + ASSERT_FALSE(mapping.MapFile(filename)); // Block map file has unexpected number of lines. ASSERT_TRUE(android::base::WriteStringToFile("/somefile\n4096 4096\n1\n", temp_file.path)); - ASSERT_EQ(-1, sysMapFile(filename.c_str(), &mapping)); + ASSERT_FALSE(mapping.MapFile(filename)); ASSERT_TRUE(android::base::WriteStringToFile("/somefile\n4096 4096\n2\n0 1\n", temp_file.path)); - ASSERT_EQ(-1, sysMapFile(filename.c_str(), &mapping)); + ASSERT_FALSE(mapping.MapFile(filename)); // Invalid size/blksize/range_count. ASSERT_TRUE(android::base::WriteStringToFile("/somefile\nabc 4096\n1\n0 1\n", temp_file.path)); - ASSERT_EQ(-1, sysMapFile(filename.c_str(), &mapping)); + ASSERT_FALSE(mapping.MapFile(filename)); ASSERT_TRUE(android::base::WriteStringToFile("/somefile\n4096 4096\n\n0 1\n", temp_file.path)); - ASSERT_EQ(-1, sysMapFile(filename.c_str(), &mapping)); + ASSERT_FALSE(mapping.MapFile(filename)); // size/blksize/range_count don't match. ASSERT_TRUE(android::base::WriteStringToFile("/somefile\n0 4096\n1\n0 1\n", temp_file.path)); - ASSERT_EQ(-1, sysMapFile(filename.c_str(), &mapping)); + ASSERT_FALSE(mapping.MapFile(filename)); ASSERT_TRUE(android::base::WriteStringToFile("/somefile\n4096 0\n1\n0 1\n", temp_file.path)); - ASSERT_EQ(-1, sysMapFile(filename.c_str(), &mapping)); + ASSERT_FALSE(mapping.MapFile(filename)); ASSERT_TRUE(android::base::WriteStringToFile("/somefile\n4096 4096\n0\n0 1\n", temp_file.path)); - ASSERT_EQ(-1, sysMapFile(filename.c_str(), &mapping)); + ASSERT_FALSE(mapping.MapFile(filename)); // Invalid block dev path. ASSERT_TRUE(android::base::WriteStringToFile("/doesntexist\n4096 4096\n1\n0 1\n", temp_file.path)); - ASSERT_EQ(-1, sysMapFile(filename.c_str(), &mapping)); - - sysReleaseMap(&mapping); - ASSERT_EQ(0U, mapping.ranges.size()); + ASSERT_FALSE(mapping.MapFile(filename)); } diff --git a/tests/unit/zip_test.cpp b/tests/unit/zip_test.cpp index 4a1a49b97..df4e38cae 100644 --- a/tests/unit/zip_test.cpp +++ b/tests/unit/zip_test.cpp @@ -66,9 +66,9 @@ TEST(ZipTest, ExtractPackageRecursive) { } TEST(ZipTest, OpenFromMemory) { - MemMapping map; std::string zip_path = from_testdata_base("ziptest_dummy-update.zip"); - ASSERT_EQ(0, sysMapFile(zip_path.c_str(), &map)); + MemMapping map; + ASSERT_TRUE(map.MapFile(zip_path)); // Map an update package into memory and open the archive from there. ZipArchiveHandle handle; @@ -85,6 +85,5 @@ TEST(ZipTest, OpenFromMemory) { ASSERT_EQ(0, ExtractEntryToFile(handle, &binary_entry, tmp_binary.fd)); CloseArchive(handle); - sysReleaseMap(&map); } -- cgit v1.2.3 From 53c38b15381ace565227e49104a6fd64c4c28dcc Mon Sep 17 00:00:00 2001 From: Tianjie Xu Date: Wed, 10 May 2017 14:47:45 -0700 Subject: kill package_extract_dir It's only used by file-based OTA which has been deprecated for O. Test: mma Change-Id: I439c93155ca94554d827142c99aa6c0845cc7561 --- tests/unit/zip_test.cpp | 37 --------- tests/unit/ziputil_test.cpp | 191 -------------------------------------------- 2 files changed, 228 deletions(-) delete mode 100644 tests/unit/ziputil_test.cpp (limited to 'tests/unit') diff --git a/tests/unit/zip_test.cpp b/tests/unit/zip_test.cpp index df4e38cae..827668521 100644 --- a/tests/unit/zip_test.cpp +++ b/tests/unit/zip_test.cpp @@ -24,47 +24,10 @@ #include #include #include -#include #include #include "common/test_constants.h" -TEST(ZipTest, ExtractPackageRecursive) { - std::string zip_path = from_testdata_base("ziptest_valid.zip"); - ZipArchiveHandle handle; - ASSERT_EQ(0, OpenArchive(zip_path.c_str(), &handle)); - - // Extract the whole package into a temp directory. - TemporaryDir td; - ASSERT_NE(nullptr, td.path); - ExtractPackageRecursive(handle, "", td.path, nullptr, nullptr); - - // Make sure all the files are extracted correctly. - std::string path(td.path); - ASSERT_EQ(0, access((path + "/a.txt").c_str(), F_OK)); - ASSERT_EQ(0, access((path + "/b.txt").c_str(), F_OK)); - ASSERT_EQ(0, access((path + "/b/c.txt").c_str(), F_OK)); - ASSERT_EQ(0, access((path + "/b/d.txt").c_str(), F_OK)); - - // The content of the file is the same as expected. - std::string content1; - ASSERT_TRUE(android::base::ReadFileToString(path + "/a.txt", &content1)); - ASSERT_EQ(kATxtContents, content1); - - std::string content2; - ASSERT_TRUE(android::base::ReadFileToString(path + "/b/d.txt", &content2)); - ASSERT_EQ(kDTxtContents, content2); - - CloseArchive(handle); - - // Clean up. - ASSERT_EQ(0, unlink((path + "/a.txt").c_str())); - ASSERT_EQ(0, unlink((path + "/b.txt").c_str())); - ASSERT_EQ(0, unlink((path + "/b/c.txt").c_str())); - ASSERT_EQ(0, unlink((path + "/b/d.txt").c_str())); - ASSERT_EQ(0, rmdir((path + "/b").c_str())); -} - TEST(ZipTest, OpenFromMemory) { std::string zip_path = from_testdata_base("ziptest_dummy-update.zip"); MemMapping map; diff --git a/tests/unit/ziputil_test.cpp b/tests/unit/ziputil_test.cpp deleted file mode 100644 index 14e541690..000000000 --- a/tests/unit/ziputil_test.cpp +++ /dev/null @@ -1,191 +0,0 @@ -/* - * Copyright 2016 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 -#include -#include - -#include - -#include -#include -#include -#include -#include - -#include "common/test_constants.h" - -TEST(ZipUtilTest, invalid_args) { - std::string zip_path = from_testdata_base("ziptest_valid.zip"); - ZipArchiveHandle handle; - ASSERT_EQ(0, OpenArchive(zip_path.c_str(), &handle)); - - // zip_path must be a relative path. - ASSERT_FALSE(ExtractPackageRecursive(handle, "/a/b", "/tmp", nullptr, nullptr)); - - // dest_path must be an absolute path. - ASSERT_FALSE(ExtractPackageRecursive(handle, "a/b", "tmp", nullptr, nullptr)); - ASSERT_FALSE(ExtractPackageRecursive(handle, "a/b", "", nullptr, nullptr)); - - CloseArchive(handle); -} - -TEST(ZipUtilTest, extract_all) { - std::string zip_path = from_testdata_base("ziptest_valid.zip"); - ZipArchiveHandle handle; - ASSERT_EQ(0, OpenArchive(zip_path.c_str(), &handle)); - - // Extract the whole package into a temp directory. - TemporaryDir td; - ExtractPackageRecursive(handle, "", td.path, nullptr, nullptr); - - // Make sure all the files are extracted correctly. - std::string path(td.path); - ASSERT_EQ(0, access((path + "/a.txt").c_str(), F_OK)); - ASSERT_EQ(0, access((path + "/b.txt").c_str(), F_OK)); - ASSERT_EQ(0, access((path + "/b/c.txt").c_str(), F_OK)); - ASSERT_EQ(0, access((path + "/b/d.txt").c_str(), F_OK)); - - // The content of the file is the same as expected. - std::string content1; - ASSERT_TRUE(android::base::ReadFileToString(path + "/a.txt", &content1)); - ASSERT_EQ(kATxtContents, content1); - - std::string content2; - ASSERT_TRUE(android::base::ReadFileToString(path + "/b/d.txt", &content2)); - ASSERT_EQ(kDTxtContents, content2); - - // Clean up the temp files under td. - ASSERT_EQ(0, unlink((path + "/a.txt").c_str())); - ASSERT_EQ(0, unlink((path + "/b.txt").c_str())); - ASSERT_EQ(0, unlink((path + "/b/c.txt").c_str())); - ASSERT_EQ(0, unlink((path + "/b/d.txt").c_str())); - ASSERT_EQ(0, rmdir((path + "/b").c_str())); - - CloseArchive(handle); -} - -TEST(ZipUtilTest, extract_prefix_with_slash) { - std::string zip_path = from_testdata_base("ziptest_valid.zip"); - ZipArchiveHandle handle; - ASSERT_EQ(0, OpenArchive(zip_path.c_str(), &handle)); - - // Extract all the entries starting with "b/". - TemporaryDir td; - ExtractPackageRecursive(handle, "b/", td.path, nullptr, nullptr); - - // Make sure all the files with "b/" prefix are extracted correctly. - std::string path(td.path); - ASSERT_EQ(0, access((path + "/c.txt").c_str(), F_OK)); - ASSERT_EQ(0, access((path + "/d.txt").c_str(), F_OK)); - - // And the rest are not extracted. - ASSERT_EQ(-1, access((path + "/a.txt").c_str(), F_OK)); - ASSERT_EQ(ENOENT, errno); - ASSERT_EQ(-1, access((path + "/b.txt").c_str(), F_OK)); - ASSERT_EQ(ENOENT, errno); - - // The content of the file is the same as expected. - std::string content1; - ASSERT_TRUE(android::base::ReadFileToString(path + "/c.txt", &content1)); - ASSERT_EQ(kCTxtContents, content1); - - std::string content2; - ASSERT_TRUE(android::base::ReadFileToString(path + "/d.txt", &content2)); - ASSERT_EQ(kDTxtContents, content2); - - // Clean up the temp files under td. - ASSERT_EQ(0, unlink((path + "/c.txt").c_str())); - ASSERT_EQ(0, unlink((path + "/d.txt").c_str())); - - CloseArchive(handle); -} - -TEST(ZipUtilTest, extract_prefix_without_slash) { - std::string zip_path = from_testdata_base("ziptest_valid.zip"); - ZipArchiveHandle handle; - ASSERT_EQ(0, OpenArchive(zip_path.c_str(), &handle)); - - // Extract all the file entries starting with "b/". - TemporaryDir td; - ExtractPackageRecursive(handle, "b", td.path, nullptr, nullptr); - - // Make sure all the files with "b/" prefix are extracted correctly. - std::string path(td.path); - ASSERT_EQ(0, access((path + "/c.txt").c_str(), F_OK)); - ASSERT_EQ(0, access((path + "/d.txt").c_str(), F_OK)); - - // And the rest are not extracted. - ASSERT_EQ(-1, access((path + "/a.txt").c_str(), F_OK)); - ASSERT_EQ(ENOENT, errno); - ASSERT_EQ(-1, access((path + "/b.txt").c_str(), F_OK)); - ASSERT_EQ(ENOENT, errno); - - // The content of the file is the same as expected. - std::string content1; - ASSERT_TRUE(android::base::ReadFileToString(path + "/c.txt", &content1)); - ASSERT_EQ(kCTxtContents, content1); - - std::string content2; - ASSERT_TRUE(android::base::ReadFileToString(path + "/d.txt", &content2)); - ASSERT_EQ(kDTxtContents, content2); - - // Clean up the temp files under td. - ASSERT_EQ(0, unlink((path + "/c.txt").c_str())); - ASSERT_EQ(0, unlink((path + "/d.txt").c_str())); - - CloseArchive(handle); -} - -TEST(ZipUtilTest, set_timestamp) { - std::string zip_path = from_testdata_base("ziptest_valid.zip"); - ZipArchiveHandle handle; - ASSERT_EQ(0, OpenArchive(zip_path.c_str(), &handle)); - - // Set the timestamp to 8/1/2008. - constexpr struct utimbuf timestamp = { 1217592000, 1217592000 }; - - // Extract all the entries starting with "b/". - TemporaryDir td; - ExtractPackageRecursive(handle, "b", td.path, ×tamp, nullptr); - - // Make sure all the files with "b/" prefix are extracted correctly. - std::string path(td.path); - std::string file_c = path + "/c.txt"; - std::string file_d = path + "/d.txt"; - ASSERT_EQ(0, access(file_c.c_str(), F_OK)); - ASSERT_EQ(0, access(file_d.c_str(), F_OK)); - - // Verify the timestamp. - timespec time; - time.tv_sec = 1217592000; - time.tv_nsec = 0; - - struct stat sb; - ASSERT_EQ(0, stat(file_c.c_str(), &sb)) << strerror(errno); - ASSERT_EQ(time.tv_sec, static_cast(sb.st_atime)); - ASSERT_EQ(time.tv_sec, static_cast(sb.st_mtime)); - - ASSERT_EQ(0, stat(file_d.c_str(), &sb)) << strerror(errno); - ASSERT_EQ(time.tv_sec, static_cast(sb.st_atime)); - ASSERT_EQ(time.tv_sec, static_cast(sb.st_mtime)); - - // Clean up the temp files under td. - ASSERT_EQ(0, unlink(file_c.c_str())); - ASSERT_EQ(0, unlink(file_d.c_str())); - - CloseArchive(handle); -} -- cgit v1.2.3