Chromium Code Reviews| Index: third_party/zlib/google/zip_reader_unittest.cc |
| diff --git a/third_party/zlib/google/zip_reader_unittest.cc b/third_party/zlib/google/zip_reader_unittest.cc |
| index 09efd9a7a669ec0707853111f8394ed9998a61d9..88ed4d3d912f71caf74a16501bbce53ade6e979d 100644 |
| --- a/third_party/zlib/google/zip_reader_unittest.cc |
| +++ b/third_party/zlib/google/zip_reader_unittest.cc |
| @@ -11,16 +11,24 @@ |
| #include "base/files/scoped_temp_dir.h" |
| #include "base/logging.h" |
| #include "base/md5.h" |
| +#include "base/message_loop/message_loop.h" |
| #include "base/path_service.h" |
| #include "base/platform_file.h" |
| +#include "base/run_loop.h" |
| #include "base/strings/utf_string_conversions.h" |
| #include "base/time/time.h" |
| +#include "testing/gmock/include/gmock/gmock.h" |
|
satorux1
2013/12/05 04:39:37
gmock should usually be avoided in Chrome code.
Drew Haven
2013/12/09 23:33:12
I personally like gmock for mocking out interfaces
|
| #include "testing/gtest/include/gtest/gtest.h" |
| #include "testing/platform_test.h" |
| #include "third_party/zlib/google/zip_internal.h" |
| namespace { |
| +using testing::_; |
| +using testing::AtLeast; |
| + |
| +const static std::string kQuuxExpectedMD5 = "d1ae4ac8a17a0e09317113ab284b57a6"; |
| + |
| // Wrap PlatformFiles in a class so that we don't leak them in tests. |
| class PlatformFileWrapper { |
| public: |
| @@ -61,6 +69,28 @@ class PlatformFileWrapper { |
| base::PlatformFile file_; |
| }; |
| +class MockListener : public zip::ZipReader::Listener { |
| + public: |
| + MockListener() {} |
| + MOCK_METHOD1(OnUnzipProgress, void(int progress)); |
| + MOCK_METHOD0(OnUnzipSuccess, void()); |
| + MOCK_METHOD0(OnUnzipFailed, void()); |
| + |
| + void ExpectSuccess() { |
| + EXPECT_CALL(*this, OnUnzipSuccess()); |
| + EXPECT_CALL(*this, OnUnzipFailed()).Times(0); |
| + EXPECT_CALL(*this, OnUnzipProgress(_)).Times(AtLeast(0)); |
| + } |
| + |
| + void ExpectSuccessWithProgress() { |
| + EXPECT_CALL(*this, OnUnzipSuccess()); |
| + EXPECT_CALL(*this, OnUnzipFailed()).Times(0); |
| + EXPECT_CALL(*this, OnUnzipProgress(_)).Times(AtLeast(1)); |
|
satorux1
2013/12/05 04:39:37
Instead of checking with GMock, can you do somethi
Drew Haven
2013/12/09 23:33:12
I was originally reporting the percentage because
|
| + } |
| + protected: |
| + virtual ~MockListener() { } |
| +}; |
| + |
| } // namespace |
| namespace zip { |
| @@ -94,6 +124,9 @@ class ZipReaderTest : public PlatformTest { |
| test_zip_contents_.insert(base::FilePath(FILE_PATH_LITERAL("foo.txt"))); |
| test_zip_contents_.insert( |
| base::FilePath(FILE_PATH_LITERAL("foo/bar/.hidden"))); |
| + |
| + mock_listener_ = new MockListener; |
| + message_loop_proxy_ = base::MessageLoop::current()->message_loop_proxy(); |
| } |
| virtual void TearDown() { |
| @@ -113,6 +146,15 @@ class ZipReaderTest : public PlatformTest { |
| return true; |
| } |
| + void CompareFileAndMD5(const base::FilePath& path, |
| + const std::string expected_md5) { |
| + // Read the output file and compute the MD5. |
| + std::string output; |
| + ASSERT_TRUE(base::ReadFileToString(path, &output)); |
| + const std::string md5 = base::MD5String(output); |
| + EXPECT_EQ(expected_md5, md5); |
| + } |
| + |
| // The path to temporary directory used to contain the test operations. |
| base::FilePath test_dir_; |
| // The path to the test data directory where test.zip etc. are located. |
| @@ -128,6 +170,10 @@ class ZipReaderTest : public PlatformTest { |
| std::set<base::FilePath> test_zip_contents_; |
| base::ScopedTempDir temp_dir_; |
| + |
| + scoped_refptr<MockListener> mock_listener_; |
| + base::MessageLoop message_loop_; |
| + scoped_refptr<base::MessageLoopProxy> message_loop_proxy_; |
| }; |
| TEST_F(ZipReaderTest, Open_ValidZipFile) { |
| @@ -220,8 +266,7 @@ TEST_F(ZipReaderTest, ExtractCurrentEntryToFilePath_RegularFile) { |
| ASSERT_TRUE(base::ReadFileToString(test_dir_.AppendASCII("quux.txt"), |
| &output)); |
| const std::string md5 = base::MD5String(output); |
| - const std::string kExpectedMD5 = "d1ae4ac8a17a0e09317113ab284b57a6"; |
| - EXPECT_EQ(kExpectedMD5, md5); |
| + EXPECT_EQ(kQuuxExpectedMD5, 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()); |
| @@ -241,8 +286,7 @@ TEST_F(ZipReaderTest, PlatformFileExtractCurrentEntryToFilePath_RegularFile) { |
| ASSERT_TRUE(base::ReadFileToString(test_dir_.AppendASCII("quux.txt"), |
| &output)); |
| const std::string md5 = base::MD5String(output); |
| - const std::string kExpectedMD5 = "d1ae4ac8a17a0e09317113ab284b57a6"; |
| - EXPECT_EQ(kExpectedMD5, md5); |
| + EXPECT_EQ(kQuuxExpectedMD5, 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()); |
| @@ -264,8 +308,7 @@ TEST_F(ZipReaderTest, PlatformFileExtractCurrentEntryToFd_RegularFile) { |
| ASSERT_TRUE(base::ReadFileToString(test_dir_.AppendASCII("quux.txt"), |
| &output)); |
| const std::string md5 = base::MD5String(output); |
| - const std::string kExpectedMD5 = "d1ae4ac8a17a0e09317113ab284b57a6"; |
| - EXPECT_EQ(kExpectedMD5, md5); |
| + EXPECT_EQ(kQuuxExpectedMD5, 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()); |
| @@ -296,8 +339,7 @@ TEST_F(ZipReaderTest, ExtractCurrentEntryIntoDirectory_RegularFile) { |
| ASSERT_TRUE(base::ReadFileToString( |
| test_dir_.AppendASCII("foo/bar/quux.txt"), &output)); |
| const std::string md5 = base::MD5String(output); |
| - const std::string kExpectedMD5 = "d1ae4ac8a17a0e09317113ab284b57a6"; |
| - EXPECT_EQ(kExpectedMD5, md5); |
| + EXPECT_EQ(kQuuxExpectedMD5, md5); |
| } |
| TEST_F(ZipReaderTest, current_entry_info_RegularFile) { |
| @@ -428,4 +470,102 @@ TEST_F(ZipReaderTest, OpenFromString) { |
| EXPECT_EQ(std::string("This is a test.\n"), actual); |
| } |
| +// Verifies that the asynchronous extraction to a file works. |
| +TEST_F(ZipReaderTest, ExtractToFileAsync_RegularFile) { |
| + mock_listener_->ExpectSuccessWithProgress(); |
| + |
| + ZipReader reader; |
| + base::FilePath target_file = test_dir_.AppendASCII("quux.txt"); |
| + base::FilePath target_path(FILE_PATH_LITERAL("foo/bar/quux.txt")); |
| + ASSERT_TRUE(reader.Open(test_zip_file_)); |
| + ASSERT_TRUE(reader.LocateAndOpenEntry(target_path)); |
| + reader.ExtractCurrentEntryToFilePathAsync(target_file, |
| + message_loop_proxy_, |
| + mock_listener_); |
| + |
| + base::RunLoop().RunUntilIdle(); |
| + |
| + CompareFileAndMD5(target_file, kQuuxExpectedMD5); |
| +} |
| + |
| +// Verifies that the asynchronous extraction to a file works. |
| +TEST_F(ZipReaderTest, ExtractToFileAsync_Directory) { |
| + mock_listener_->ExpectSuccess(); |
| + |
| + ZipReader reader; |
| + base::FilePath target_path(FILE_PATH_LITERAL("foo/")); |
| + ASSERT_TRUE(reader.Open(test_zip_file_)); |
| + ASSERT_TRUE(reader.LocateAndOpenEntry(target_path)); |
| + reader.ExtractCurrentEntryToFilePathAsync(target_path, |
| + message_loop_proxy_, |
| + mock_listener_); |
| + |
| + base::RunLoop().RunUntilIdle(); |
| + |
| + ASSERT_TRUE(base::DirectoryExists(target_path)); |
| +} |
| + |
| +// Verifies that the asynchronous extraction to a directory works. |
| +TEST_F(ZipReaderTest, ExtractIntoDirectoryAsync_RegularFile) { |
| + mock_listener_->ExpectSuccessWithProgress(); |
| + |
| + ZipReader reader; |
| + base::FilePath target_path(FILE_PATH_LITERAL("foo/bar/quux.txt")); |
| + base::FilePath target_file = test_dir_.AppendASCII("foo/bar/quux.txt"); |
| + ASSERT_TRUE(reader.Open(test_zip_file_)); |
| + ASSERT_TRUE(reader.LocateAndOpenEntry(target_path)); |
| + reader.ExtractCurrentEntryIntoDirectoryAsync(test_dir_, |
| + message_loop_proxy_, |
| + mock_listener_); |
| + |
| + base::RunLoop().RunUntilIdle(); |
| + |
| + ASSERT_TRUE(base::DirectoryExists(test_dir_.AppendASCII("foo/bar"))); |
| + CompareFileAndMD5(target_file, kQuuxExpectedMD5); |
| +} |
| + |
| +#if defined(OS_POSIX) |
| +// Verifies that the asynchronous extraction to a file descriptor works. |
| +TEST_F(ZipReaderTest, ExtractToFdAsync) { |
| + mock_listener_->ExpectSuccessWithProgress(); |
| + |
| + ZipReader reader; |
| + PlatformFileWrapper zip_fd_wrapper(test_zip_file_, |
| + PlatformFileWrapper::READ_ONLY); |
| + ASSERT_TRUE(reader.OpenFromPlatformFile(zip_fd_wrapper.platform_file())); |
| + base::FilePath target_path(FILE_PATH_LITERAL("foo/bar/quux.txt")); |
| + base::FilePath out_path = test_dir_.AppendASCII("quux.txt"); |
| + PlatformFileWrapper out_fd_w(out_path, PlatformFileWrapper::READ_WRITE); |
| + ASSERT_TRUE(reader.LocateAndOpenEntry(target_path)); |
| + reader.ExtractCurrentEntryToFdAsync(out_fd_w.platform_file(), |
| + message_loop_proxy_, |
| + mock_listener_); |
| + |
| + base::RunLoop().RunUntilIdle(); |
| + |
| + CompareFileAndMD5(out_path, kQuuxExpectedMD5); |
| +} |
| +#endif |
| + |
| +// Verifies that the asynchronous extraction to a platform file works. |
| +TEST_F(ZipReaderTest, ExtractToPlatformFileAsync) { |
| + mock_listener_->ExpectSuccessWithProgress(); |
| + |
| + ZipReader reader; |
| + PlatformFileWrapper zip_fd_wrapper(test_zip_file_, |
| + PlatformFileWrapper::READ_ONLY); |
| + ASSERT_TRUE(reader.OpenFromPlatformFile(zip_fd_wrapper.platform_file())); |
| + base::FilePath target_path(FILE_PATH_LITERAL("foo/bar/quux.txt")); |
| + base::FilePath out_path = test_dir_.AppendASCII("quux.txt"); |
| + PlatformFileWrapper out_fd_w(out_path, PlatformFileWrapper::READ_WRITE); |
| + ASSERT_TRUE(reader.LocateAndOpenEntry(target_path)); |
| + reader.ExtractCurrentEntryToPlatformFileAsync(out_fd_w.platform_file(), |
| + message_loop_proxy_, |
| + mock_listener_); |
| + |
| + base::RunLoop().RunUntilIdle(); |
| + |
| + CompareFileAndMD5(out_path, kQuuxExpectedMD5); |
| +} |
| + |
| } // namespace zip |