Chromium Code Reviews| Index: chrome/common/zip_reader_unittest.cc |
| diff --git a/chrome/common/zip_reader_unittest.cc b/chrome/common/zip_reader_unittest.cc |
| index 82f0b14f727040feb3a435cbb028890b9990db33..610b801f97e6c81a920db347eb6f828875775da7 100644 |
| --- a/chrome/common/zip_reader_unittest.cc |
| +++ b/chrome/common/zip_reader_unittest.cc |
| @@ -4,6 +4,12 @@ |
| #include "chrome/common/zip_reader.h" |
| +#if defined(OS_POSIX) |
| +#include <fcntl.h> |
| +#include <sys/stat.h> |
| +#include <sys/types.h> |
| +#endif |
| + |
| #include <set> |
| #include <string> |
| @@ -52,6 +58,17 @@ class ZipReaderTest : public PlatformTest { |
| PlatformTest::TearDown(); |
| } |
| +#if defined(OS_POSIX) |
| + int OpenFileRdOnly(FilePath& file) { |
|
satorux1
2011/12/12 04:54:14
Please write a brief function comment.
http://www
Jorge Lucangeli Obes
2011/12/12 23:34:35
Done.
|
| + return open(file.value().c_str(), O_RDONLY); |
| + } |
|
satorux1
2011/12/12 04:54:14
We usually have a blank line between function defi
Jorge Lucangeli Obes
2011/12/12 23:34:35
Done.
|
| + int OpenFileRdWr(FilePath& file) { |
| + return open(file.value().c_str(), |
| + O_RDWR | O_CREAT, |
| + S_IRUSR | S_IWUSR); |
| + } |
| +#endif |
| + |
| // The path to temporary directory used to contain the test operations. |
| FilePath test_dir_; |
| // The path to the test data directory where test.zip etc. are located. |
| @@ -74,6 +91,14 @@ TEST_F(ZipReaderTest, Open_ValidZipFile) { |
| ASSERT_TRUE(reader.Open(test_zip_file_)); |
| } |
| +#if defined(OS_POSIX) |
| +TEST_F(ZipReaderTest, Open_ValidZipFd) { |
| + ZipReader reader; |
| + int zip_fd = OpenFileRdOnly(test_zip_file_); |
| + ASSERT_TRUE(reader.OpenFd(zip_fd)); |
| +} |
| +#endif |
| + |
| TEST_F(ZipReaderTest, Open_NonExistentFile) { |
| ZipReader reader; |
| ASSERT_FALSE(reader.Open(test_data_dir_.AppendASCII("nonexistent.zip"))); |
| @@ -102,6 +127,26 @@ TEST_F(ZipReaderTest, Iteration) { |
| EXPECT_EQ(test_zip_contents_, actual_contents); |
| } |
| +#if defined(OS_POSIX) |
| +// Open the test zip file from a file descriptor, iterate through its contents, |
| +// and compare that they match the expected contents. |
| +TEST_F(ZipReaderTest, FdIteration) { |
| + std::set<FilePath> actual_contents; |
| + ZipReader reader; |
| + int zip_fd = OpenFileRdOnly(test_zip_file_); |
| + ASSERT_TRUE(reader.OpenFd(zip_fd)); |
| + while (reader.HasMore()) { |
| + ASSERT_TRUE(reader.OpenCurrentEntryInZip()); |
| + actual_contents.insert(reader.current_entry_info()->file_path()); |
| + ASSERT_TRUE(reader.AdvanceToNextEntry()); |
| + } |
| + EXPECT_FALSE(reader.AdvanceToNextEntry()); // Shouldn't go further. |
| + EXPECT_EQ(test_zip_contents_.size(), |
| + static_cast<size_t>(reader.num_entries())); |
| + EXPECT_EQ(test_zip_contents_.size(), actual_contents.size()); |
| + EXPECT_EQ(test_zip_contents_, actual_contents); |
| +} |
| +#endif |
| TEST_F(ZipReaderTest, LocateAndOpenEntry_ValidFile) { |
| std::set<FilePath> actual_contents; |
| @@ -140,6 +185,49 @@ TEST_F(ZipReaderTest, ExtractCurrentEntryToFilePath_RegularFile) { |
| EXPECT_LT(static_cast<size_t>(internal::kZipBufSize), output.size()); |
| } |
| +#if defined(OS_POSIX) |
| +TEST_F(ZipReaderTest, FdExtractCurrentEntryToFilePath_RegularFile) { |
| + ZipReader reader; |
| + int zip_fd = OpenFileRdOnly(test_zip_file_); |
| + ASSERT_TRUE(reader.OpenFd(zip_fd)); |
| + FilePath target_path(FILE_PATH_LITERAL("foo/bar/quux.txt")); |
| + ASSERT_TRUE(reader.LocateAndOpenEntry(target_path)); |
| + ASSERT_TRUE(reader.ExtractCurrentEntryToFilePath( |
| + test_dir_.AppendASCII("quux.txt"))); |
| + // Read the output file and compute the MD5. |
| + std::string output; |
| + ASSERT_TRUE(file_util::ReadFileToString(test_dir_.AppendASCII("quux.txt"), |
| + &output)); |
| + const std::string md5 = base::MD5String(output); |
| + const std::string kExpectedMD5 = "d1ae4ac8a17a0e09317113ab284b57a6"; |
| + EXPECT_EQ(kExpectedMD5, md5); |
| + // quux.txt should be larger than kZipBufSize so that we can exercise |
| + // the loop in ExtractCurrentEntry(). |
| + EXPECT_LT(static_cast<size_t>(internal::kZipBufSize), output.size()); |
| +} |
| + |
| +TEST_F(ZipReaderTest, FdExtractCurrentEntryToFd_RegularFile) { |
| + ZipReader reader; |
| + int zip_fd = OpenFileRdOnly(test_zip_file_); |
| + ASSERT_TRUE(reader.OpenFd(zip_fd)); |
| + FilePath target_path(FILE_PATH_LITERAL("foo/bar/quux.txt")); |
| + FilePath out_path = test_dir_.AppendASCII("quux.txt"); |
| + int out_fd = OpenFileRdWr(out_path); |
| + ASSERT_TRUE(reader.LocateAndOpenEntry(target_path)); |
| + ASSERT_TRUE(reader.ExtractCurrentEntryToFd(out_fd)); |
| + // Read the output file and compute the MD5. |
| + std::string output; |
| + ASSERT_TRUE(file_util::ReadFileToString(test_dir_.AppendASCII("quux.txt"), |
| + &output)); |
| + const std::string md5 = base::MD5String(output); |
| + const std::string kExpectedMD5 = "d1ae4ac8a17a0e09317113ab284b57a6"; |
| + EXPECT_EQ(kExpectedMD5, md5); |
| + // quux.txt should be larger than kZipBufSize so that we can exercise |
| + // the loop in ExtractCurrentEntry(). |
| + EXPECT_LT(static_cast<size_t>(internal::kZipBufSize), output.size()); |
| +} |
| +#endif |
| + |
| TEST_F(ZipReaderTest, ExtractCurrentEntryToFilePath_Directory) { |
| ZipReader reader; |
| ASSERT_TRUE(reader.Open(test_zip_file_)); |