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