OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "third_party/zlib/google/zip_writer.h" |
| 6 |
| 7 #include "base/files/file_path.h" |
| 8 #include "base/files/scoped_temp_dir.h" |
| 9 #include "base/memory/ref_counted_memory.h" |
| 10 #include "testing/gtest/include/gtest/gtest.h" |
| 11 #include "third_party/zlib/google/zip_reader.h" |
| 12 |
| 13 namespace zip { |
| 14 |
| 15 namespace { |
| 16 base::FilePath AsPath(const char* p) { |
| 17 return base::FilePath::FromUTF8Unsafe(p); |
| 18 } |
| 19 |
| 20 void TestZipFileHasWriterContents(const char *id, |
| 21 const base::FilePath& zip_file, |
| 22 const zip::ZipWriter& writer) { |
| 23 SCOPED_TRACE(std::string("TestZipFileHasWriterContents(") + id + ")"); |
| 24 |
| 25 zip::ZipReader reader; |
| 26 |
| 27 bool open_success = reader.Open(zip_file); |
| 28 EXPECT_TRUE(open_success); |
| 29 if (!open_success) |
| 30 // Else ZipReader will assert while using its API. |
| 31 return; |
| 32 |
| 33 int count = 0; |
| 34 for (zip::ZipWriter::const_iterator it = writer.begin(); |
| 35 it != writer.end(); |
| 36 ++it) { |
| 37 count++; |
| 38 bool found_entry = reader.LocateAndOpenEntry(it->first); |
| 39 EXPECT_TRUE(found_entry); |
| 40 if (!found_entry) |
| 41 continue; // Else entry_info will be NULL. |
| 42 |
| 43 ASSERT_TRUE(it->second.data()); |
| 44 |
| 45 const zip::ZipReader::EntryInfo* entry_info = |
| 46 reader.current_entry_info(); |
| 47 EXPECT_EQ(entry_info->file_path(), it->first); |
| 48 EXPECT_EQ(entry_info->is_directory(), it->first.EndsWithSeparator()); |
| 49 EXPECT_EQ(entry_info->original_size(), |
| 50 static_cast<int64>(it->second.size())); |
| 51 |
| 52 if (it->second.size() != 0) { |
| 53 std::string mem; |
| 54 ASSERT_TRUE(reader.ExtractCurrentEntryToString(1024, &mem)); |
| 55 EXPECT_EQ(it->second, mem); |
| 56 } |
| 57 } |
| 58 |
| 59 EXPECT_EQ(count, reader.num_entries()); |
| 60 } |
| 61 } // namespace |
| 62 |
| 63 TEST(ZipWriterTest, MainTest) { |
| 64 base::ScopedTempDir temp_dir; |
| 65 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
| 66 |
| 67 base::FilePath zip_file = temp_dir.path().AppendASCII("out.zip"); |
| 68 EXPECT_FALSE(base::PathExists(zip_file)); |
| 69 |
| 70 // Nothing to do. |
| 71 zip::ZipWriter writer; |
| 72 |
| 73 EXPECT_TRUE(writer.Commit(zip_file, zip::ZipWriter::OverWrite)); |
| 74 |
| 75 // Add some files. |
| 76 writer.AddFile(AsPath("a"), ""); |
| 77 std::string k_str = "k"; |
| 78 writer.AddFile(AsPath("b"), k_str); |
| 79 std::string y_str = "y"; |
| 80 scoped_refptr<base::RefCountedMemory> y_ptr(base::RefCountedString::TakeString
(&y_str)); |
| 81 writer.AddFile(AsPath("b"), y_ptr.get()); |
| 82 writer.AddFile(AsPath("c"), "x"); |
| 83 writer.AddFolder(AsPath("D/")); |
| 84 writer.AddFile(AsPath("E/a"), "a"); |
| 85 EXPECT_TRUE(writer.Commit(zip_file, zip::ZipWriter::Append)); |
| 86 |
| 87 TestZipFileHasWriterContents("1", zip_file, writer); |
| 88 |
| 89 // Add new file, and remove old one. |
| 90 zip::ZipWriter writer_2; |
| 91 writer_2.AddFile(AsPath("h"), "h"); |
| 92 writer_2.DeleteFile(AsPath("E/a")); |
| 93 EXPECT_TRUE(writer_2.Commit(zip_file, zip::ZipWriter::Append)); |
| 94 |
| 95 writer.AddFile(AsPath("h"), "h"); |
| 96 writer.DeleteFile(AsPath("E/a")); |
| 97 writer.DropModifications(AsPath("E/a")); |
| 98 TestZipFileHasWriterContents("2", zip_file, writer); |
| 99 |
| 100 // Modify, add and remove all in one set. |
| 101 writer.AddFile(AsPath("a"), "aaa"); |
| 102 writer.DropModifications(AsPath("b")); // Must be preserved. |
| 103 writer.AddFile(AsPath("c"), ""); |
| 104 writer.DeleteFolder(AsPath("D")); |
| 105 writer.DeleteFolder(AsPath("not-here/")); |
| 106 writer.DeleteFile(AsPath("not-here-2")); |
| 107 writer.AddFile(AsPath("E/a"), ""); |
| 108 writer.AddFile(AsPath("X/c/a"), "xx"); |
| 109 writer.AddFolder(AsPath("X/c")); |
| 110 EXPECT_TRUE(writer.Commit(zip_file, zip::ZipWriter::Append)); |
| 111 |
| 112 writer.DropModifications(AsPath("D/")); |
| 113 writer.DropModifications(AsPath("not-here")); |
| 114 writer.DropModifications(AsPath("not-here-2")); |
| 115 writer.AddFile(AsPath("b"), "y"); |
| 116 TestZipFileHasWriterContents("3", zip_file, writer); |
| 117 |
| 118 // Recreate file from scratch. |
| 119 EXPECT_TRUE(writer.Commit(zip_file, zip::ZipWriter::OverWrite)); |
| 120 TestZipFileHasWriterContents("4", zip_file, writer); |
| 121 } |
| 122 |
| 123 } // namespace zip |
OLD | NEW |