Merge "zip_archive: Fix tests broken by 1f93d71022cca7bb6bb9eec49."

am: 492de535c4

Change-Id: I0e6de0b6f71626fefbb04befd2e8e84f439ae1fe
This commit is contained in:
Narayan Kamath 2017-12-22 10:47:05 +00:00 committed by android-build-merger
commit fede183c53

View file

@ -64,11 +64,6 @@ static int32_t OpenArchiveWrapper(const std::string& name, ZipArchiveHandle* han
return OpenArchive(abs_path.c_str(), handle); return OpenArchive(abs_path.c_str(), handle);
} }
static void AssertNameEquals(const std::string& name_str, const ZipString& name) {
ASSERT_EQ(name_str.size(), name.name_length);
ASSERT_EQ(0, memcmp(name_str.c_str(), name.name, name.name_length));
}
static void SetZipString(ZipString* zip_str, const std::string& str) { static void SetZipString(ZipString* zip_str, const std::string& str) {
zip_str->name = reinterpret_cast<const uint8_t*>(str.c_str()); zip_str->name = reinterpret_cast<const uint8_t*>(str.c_str());
zip_str->name_length = str.size(); zip_str->name_length = str.size();
@ -117,132 +112,60 @@ TEST(ziparchive, OpenDoNotAssumeFdOwnership) {
close(fd); close(fd);
} }
TEST(ziparchive, Iteration) { static void AssertIterationOrder(const ZipString* prefix, const ZipString* suffix,
const std::vector<std::string>& expected_names_sorted) {
ZipArchiveHandle handle; ZipArchiveHandle handle;
ASSERT_EQ(0, OpenArchiveWrapper(kValidZip, &handle)); ASSERT_EQ(0, OpenArchiveWrapper(kValidZip, &handle));
void* iteration_cookie; void* iteration_cookie;
ASSERT_EQ(0, StartIteration(handle, &iteration_cookie, nullptr, nullptr)); ASSERT_EQ(0, StartIteration(handle, &iteration_cookie, prefix, suffix));
ZipEntry data; ZipEntry data;
std::vector<std::string> names;
ZipString name; ZipString name;
for (size_t i = 0; i < expected_names_sorted.size(); ++i) {
// b/c.txt ASSERT_EQ(0, Next(iteration_cookie, &data, &name));
ASSERT_EQ(0, Next(iteration_cookie, &data, &name)); names.push_back(std::string(reinterpret_cast<const char*>(name.name), name.name_length));
AssertNameEquals("b/c.txt", name); }
// b/d.txt
ASSERT_EQ(0, Next(iteration_cookie, &data, &name));
AssertNameEquals("b/d.txt", name);
// a.txt
ASSERT_EQ(0, Next(iteration_cookie, &data, &name));
AssertNameEquals("a.txt", name);
// b.txt
ASSERT_EQ(0, Next(iteration_cookie, &data, &name));
AssertNameEquals("b.txt", name);
// b/
ASSERT_EQ(0, Next(iteration_cookie, &data, &name));
AssertNameEquals("b/", name);
// End of iteration. // End of iteration.
ASSERT_EQ(-1, Next(iteration_cookie, &data, &name)); ASSERT_EQ(-1, Next(iteration_cookie, &data, &name));
CloseArchive(handle); CloseArchive(handle);
// Assert that the names are as expected.
std::sort(names.begin(), names.end());
ASSERT_EQ(expected_names_sorted, names);
}
TEST(ziparchive, Iteration) {
static const std::vector<std::string> kExpectedMatchesSorted = {"a.txt", "b.txt", "b/", "b/c.txt",
"b/d.txt"};
AssertIterationOrder(nullptr, nullptr, kExpectedMatchesSorted);
} }
TEST(ziparchive, IterationWithPrefix) { TEST(ziparchive, IterationWithPrefix) {
ZipArchiveHandle handle;
ASSERT_EQ(0, OpenArchiveWrapper(kValidZip, &handle));
void* iteration_cookie;
ZipString prefix("b/"); ZipString prefix("b/");
ASSERT_EQ(0, StartIteration(handle, &iteration_cookie, &prefix, nullptr)); static const std::vector<std::string> kExpectedMatchesSorted = {"b/", "b/c.txt", "b/d.txt"};
ZipEntry data; AssertIterationOrder(&prefix, nullptr, kExpectedMatchesSorted);
ZipString name;
// b/c.txt
ASSERT_EQ(0, Next(iteration_cookie, &data, &name));
AssertNameEquals("b/c.txt", name);
// b/d.txt
ASSERT_EQ(0, Next(iteration_cookie, &data, &name));
AssertNameEquals("b/d.txt", name);
// b/
ASSERT_EQ(0, Next(iteration_cookie, &data, &name));
AssertNameEquals("b/", name);
// End of iteration.
ASSERT_EQ(-1, Next(iteration_cookie, &data, &name));
CloseArchive(handle);
} }
TEST(ziparchive, IterationWithSuffix) { TEST(ziparchive, IterationWithSuffix) {
ZipArchiveHandle handle;
ASSERT_EQ(0, OpenArchiveWrapper(kValidZip, &handle));
void* iteration_cookie;
ZipString suffix(".txt"); ZipString suffix(".txt");
ASSERT_EQ(0, StartIteration(handle, &iteration_cookie, nullptr, &suffix)); static const std::vector<std::string> kExpectedMatchesSorted = {"a.txt", "b.txt", "b/c.txt",
"b/d.txt"};
ZipEntry data; AssertIterationOrder(nullptr, &suffix, kExpectedMatchesSorted);
ZipString name;
// b/c.txt
ASSERT_EQ(0, Next(iteration_cookie, &data, &name));
AssertNameEquals("b/c.txt", name);
// b/d.txt
ASSERT_EQ(0, Next(iteration_cookie, &data, &name));
AssertNameEquals("b/d.txt", name);
// a.txt
ASSERT_EQ(0, Next(iteration_cookie, &data, &name));
AssertNameEquals("a.txt", name);
// b.txt
ASSERT_EQ(0, Next(iteration_cookie, &data, &name));
AssertNameEquals("b.txt", name);
// End of iteration.
ASSERT_EQ(-1, Next(iteration_cookie, &data, &name));
CloseArchive(handle);
} }
TEST(ziparchive, IterationWithPrefixAndSuffix) { TEST(ziparchive, IterationWithPrefixAndSuffix) {
ZipArchiveHandle handle;
ASSERT_EQ(0, OpenArchiveWrapper(kValidZip, &handle));
void* iteration_cookie;
ZipString prefix("b"); ZipString prefix("b");
ZipString suffix(".txt"); ZipString suffix(".txt");
ASSERT_EQ(0, StartIteration(handle, &iteration_cookie, &prefix, &suffix)); static const std::vector<std::string> kExpectedMatchesSorted = {"b.txt", "b/c.txt", "b/d.txt"};
ZipEntry data; AssertIterationOrder(&prefix, &suffix, kExpectedMatchesSorted);
ZipString name;
// b/c.txt
ASSERT_EQ(0, Next(iteration_cookie, &data, &name));
AssertNameEquals("b/c.txt", name);
// b/d.txt
ASSERT_EQ(0, Next(iteration_cookie, &data, &name));
AssertNameEquals("b/d.txt", name);
// b.txt
ASSERT_EQ(0, Next(iteration_cookie, &data, &name));
AssertNameEquals("b.txt", name);
// End of iteration.
ASSERT_EQ(-1, Next(iteration_cookie, &data, &name));
CloseArchive(handle);
} }
TEST(ziparchive, IterationWithBadPrefixAndSuffix) { TEST(ziparchive, IterationWithBadPrefixAndSuffix) {