Index: third_party/zlib/google/zip_writer_unittest.cc |
diff --git a/third_party/zlib/google/zip_writer_unittest.cc b/third_party/zlib/google/zip_writer_unittest.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..ee31a7c9230f782890c7e0dcf9e1f52038dcfc81 |
--- /dev/null |
+++ b/third_party/zlib/google/zip_writer_unittest.cc |
@@ -0,0 +1,123 @@ |
+// Copyright (c) 2011 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#include "third_party/zlib/google/zip_writer.h" |
+ |
+#include "base/files/file_path.h" |
+#include "base/files/scoped_temp_dir.h" |
+#include "base/memory/ref_counted_memory.h" |
+#include "testing/gtest/include/gtest/gtest.h" |
+#include "third_party/zlib/google/zip_reader.h" |
+ |
+namespace zip { |
+ |
+namespace { |
+base::FilePath AsPath(const char* p) { |
+ return base::FilePath::FromUTF8Unsafe(p); |
+} |
+ |
+void TestZipFileHasWriterContents(const char *id, |
+ const base::FilePath& zip_file, |
+ const zip::ZipWriter& writer) { |
+ SCOPED_TRACE(std::string("TestZipFileHasWriterContents(") + id + ")"); |
+ |
+ zip::ZipReader reader; |
+ |
+ bool open_success = reader.Open(zip_file); |
+ EXPECT_TRUE(open_success); |
+ if (!open_success) |
+ // Else ZipReader will assert while using its API. |
+ return; |
+ |
+ int count = 0; |
+ for (zip::ZipWriter::const_iterator it = writer.begin(); |
+ it != writer.end(); |
+ ++it) { |
+ count++; |
+ bool found_entry = reader.LocateAndOpenEntry(it->first); |
+ EXPECT_TRUE(found_entry); |
+ if (!found_entry) |
+ continue; // Else entry_info will be NULL. |
+ |
+ ASSERT_TRUE(it->second.data()); |
+ |
+ const zip::ZipReader::EntryInfo* entry_info = |
+ reader.current_entry_info(); |
+ EXPECT_EQ(entry_info->file_path(), it->first); |
+ EXPECT_EQ(entry_info->is_directory(), it->first.EndsWithSeparator()); |
+ EXPECT_EQ(entry_info->original_size(), |
+ static_cast<int64>(it->second.size())); |
+ |
+ if (it->second.size() != 0) { |
+ std::string mem; |
+ ASSERT_TRUE(reader.ExtractCurrentEntryToString(1024, &mem)); |
+ EXPECT_EQ(it->second, mem); |
+ } |
+ } |
+ |
+ EXPECT_EQ(count, reader.num_entries()); |
+} |
+} // namespace |
+ |
+TEST(ZipWriterTest, MainTest) { |
+ base::ScopedTempDir temp_dir; |
+ ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
+ |
+ base::FilePath zip_file = temp_dir.path().AppendASCII("out.zip"); |
+ EXPECT_FALSE(base::PathExists(zip_file)); |
+ |
+ // Nothing to do. |
+ zip::ZipWriter writer; |
+ |
+ EXPECT_TRUE(writer.Commit(zip_file, zip::ZipWriter::OverWrite)); |
+ |
+ // Add some files. |
+ writer.AddFile(AsPath("a"), ""); |
+ std::string k_str = "k"; |
+ writer.AddFile(AsPath("b"), k_str); |
+ std::string y_str = "y"; |
+ scoped_refptr<base::RefCountedMemory> y_ptr(base::RefCountedString::TakeString(&y_str)); |
+ writer.AddFile(AsPath("b"), y_ptr.get()); |
+ writer.AddFile(AsPath("c"), "x"); |
+ writer.AddFolder(AsPath("D/")); |
+ writer.AddFile(AsPath("E/a"), "a"); |
+ EXPECT_TRUE(writer.Commit(zip_file, zip::ZipWriter::Append)); |
+ |
+ TestZipFileHasWriterContents("1", zip_file, writer); |
+ |
+ // Add new file, and remove old one. |
+ zip::ZipWriter writer_2; |
+ writer_2.AddFile(AsPath("h"), "h"); |
+ writer_2.DeleteFile(AsPath("E/a")); |
+ EXPECT_TRUE(writer_2.Commit(zip_file, zip::ZipWriter::Append)); |
+ |
+ writer.AddFile(AsPath("h"), "h"); |
+ writer.DeleteFile(AsPath("E/a")); |
+ writer.DropModifications(AsPath("E/a")); |
+ TestZipFileHasWriterContents("2", zip_file, writer); |
+ |
+ // Modify, add and remove all in one set. |
+ writer.AddFile(AsPath("a"), "aaa"); |
+ writer.DropModifications(AsPath("b")); // Must be preserved. |
+ writer.AddFile(AsPath("c"), ""); |
+ writer.DeleteFolder(AsPath("D")); |
+ writer.DeleteFolder(AsPath("not-here/")); |
+ writer.DeleteFile(AsPath("not-here-2")); |
+ writer.AddFile(AsPath("E/a"), ""); |
+ writer.AddFile(AsPath("X/c/a"), "xx"); |
+ writer.AddFolder(AsPath("X/c")); |
+ EXPECT_TRUE(writer.Commit(zip_file, zip::ZipWriter::Append)); |
+ |
+ writer.DropModifications(AsPath("D/")); |
+ writer.DropModifications(AsPath("not-here")); |
+ writer.DropModifications(AsPath("not-here-2")); |
+ writer.AddFile(AsPath("b"), "y"); |
+ TestZipFileHasWriterContents("3", zip_file, writer); |
+ |
+ // Recreate file from scratch. |
+ EXPECT_TRUE(writer.Commit(zip_file, zip::ZipWriter::OverWrite)); |
+ TestZipFileHasWriterContents("4", zip_file, writer); |
+} |
+ |
+} // namespace zip |