Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 47ede546 authored by Narayan Kamath's avatar Narayan Kamath Committed by android-build-merger
Browse files

Merge "zip_archive: Fix tests broken by 1f93d710." am: 492de535

am: fede183c

Change-Id: Ib1e16a1b0799ffe88b822b04d3231c4d30f77917
parents f8227734 fede183c
Loading
Loading
Loading
Loading
+27 −104
Original line number Original line Diff line number Diff line
@@ -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;
  ZipString name;
  std::vector<std::string> names;


  // b/c.txt
  ZipString name;
  ASSERT_EQ(0, Next(iteration_cookie, &data, &name));
  for (size_t i = 0; i < expected_names_sorted.size(); ++i) {
  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));
    ASSERT_EQ(0, Next(iteration_cookie, &data, &name));
  AssertNameEquals("b/", name);
    names.push_back(std::string(reinterpret_cast<const char*>(name.name), name.name_length));
  }


  // 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);
}

TEST(ziparchive, IterationWithPrefix) {
  ZipArchiveHandle handle;
  ASSERT_EQ(0, OpenArchiveWrapper(kValidZip, &handle));

  void* iteration_cookie;
  ZipString prefix("b/");
  ASSERT_EQ(0, StartIteration(handle, &iteration_cookie, &prefix, nullptr));


  ZipEntry data;
  // Assert that the names are as expected.
  ZipString name;
  std::sort(names.begin(), names.end());

  ASSERT_EQ(expected_names_sorted, names);
  // b/c.txt
}
  ASSERT_EQ(0, Next(iteration_cookie, &data, &name));
  AssertNameEquals("b/c.txt", name);


  // b/d.txt
TEST(ziparchive, Iteration) {
  ASSERT_EQ(0, Next(iteration_cookie, &data, &name));
  static const std::vector<std::string> kExpectedMatchesSorted = {"a.txt", "b.txt", "b/", "b/c.txt",
  AssertNameEquals("b/d.txt", name);
                                                                  "b/d.txt"};


  // b/
  AssertIterationOrder(nullptr, nullptr, kExpectedMatchesSorted);
  ASSERT_EQ(0, Next(iteration_cookie, &data, &name));
}
  AssertNameEquals("b/", name);


  // End of iteration.
TEST(ziparchive, IterationWithPrefix) {
  ASSERT_EQ(-1, Next(iteration_cookie, &data, &name));
  ZipString prefix("b/");
  static const std::vector<std::string> kExpectedMatchesSorted = {"b/", "b/c.txt", "b/d.txt"};


  CloseArchive(handle);
  AssertIterationOrder(&prefix, nullptr, kExpectedMatchesSorted);
}
}


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;
  ZipString name;


  // b/c.txt
  AssertIterationOrder(nullptr, &suffix, kExpectedMatchesSorted);
  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;
  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.
  AssertIterationOrder(&prefix, &suffix, kExpectedMatchesSorted);
  ASSERT_EQ(-1, Next(iteration_cookie, &data, &name));

  CloseArchive(handle);
}
}


TEST(ziparchive, IterationWithBadPrefixAndSuffix) {
TEST(ziparchive, IterationWithBadPrefixAndSuffix) {