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..4599b6447682ec254b4633dc23610b98d0879101 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> |
| @@ -18,6 +24,42 @@ |
| #include "testing/gtest/include/gtest/gtest.h" |
| #include "testing/platform_test.h" |
| +namespace { |
| + |
| +#if defined(OS_POSIX) |
| +// Wrap file descriptors in a class so that we don't leak them in tests. |
| +class FdWrapper { |
| + public: |
| + static const int READ_ONLY = 0; |
| + static const int READ_WRITE = 1; |
|
satorux1
2011/12/15 00:05:36
We use enum for this.
enum {
READ_ONLY,
Jorge Lucangeli Obes
2011/12/15 00:19:00
Done.
|
| + |
| + FdWrapper(const FilePath& file, int mode) : fd_(-1) { |
| + switch (mode) { |
| + case READ_ONLY: |
| + fd_ = open(file.value().c_str(), O_RDONLY); |
| + break; |
| + case READ_WRITE: |
| + fd_ = open(file.value().c_str(), |
| + O_RDWR | O_CREAT, |
| + S_IRUSR | S_IWUSR); |
| + break; |
|
satorux1
2011/12/15 00:05:36
we usually have default: in switch.
default:
NO
Jorge Lucangeli Obes
2011/12/15 00:19:00
Done.
|
| + } |
| + return; |
| + } |
| + |
| + ~FdWrapper() { |
| + close(fd_); |
| + } |
| + |
| + int fd() { return fd_; } |
| + |
| + private: |
| + int fd_; |
| +}; |
| +#endif |
| + |
| +} // namespace |
| + |
| namespace zip { |
| // Make the test a PlatformTest to setup autorelease pools properly on Mac. |
| @@ -74,6 +116,14 @@ TEST_F(ZipReaderTest, Open_ValidZipFile) { |
| ASSERT_TRUE(reader.Open(test_zip_file_)); |
| } |
| +#if defined(OS_POSIX) |
| +TEST_F(ZipReaderTest, Open_ValidZipFd) { |
| + ZipReader reader; |
| + FdWrapper zip_fd_w(test_zip_file_, FdWrapper::READ_ONLY); |
|
satorux1
2011/12/15 00:05:36
zip_fd_wrapper in favor of less abbreviation?
Jorge Lucangeli Obes
2011/12/15 00:19:00
Done.
|
| + ASSERT_TRUE(reader.OpenFromFd(zip_fd_w.fd())); |
| +} |
| +#endif |
| + |
| TEST_F(ZipReaderTest, Open_NonExistentFile) { |
| ZipReader reader; |
| ASSERT_FALSE(reader.Open(test_data_dir_.AppendASCII("nonexistent.zip"))); |
| @@ -102,6 +152,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; |
| + FdWrapper zip_fd_w(test_zip_file_, FdWrapper::READ_ONLY); |
| + ASSERT_TRUE(reader.OpenFromFd(zip_fd_w.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 +210,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; |
| + FdWrapper zip_fd_w(test_zip_file_, FdWrapper::READ_ONLY); |
| + ASSERT_TRUE(reader.OpenFromFd(zip_fd_w.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; |
| + FdWrapper zip_fd_w(test_zip_file_, FdWrapper::READ_ONLY); |
| + ASSERT_TRUE(reader.OpenFromFd(zip_fd_w.fd())); |
| + FilePath target_path(FILE_PATH_LITERAL("foo/bar/quux.txt")); |
| + FilePath out_path = test_dir_.AppendASCII("quux.txt"); |
| + FdWrapper out_fd_w(out_path, FdWrapper::READ_WRITE); |
| + ASSERT_TRUE(reader.LocateAndOpenEntry(target_path)); |
| + ASSERT_TRUE(reader.ExtractCurrentEntryToFd(out_fd_w.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_)); |