This source file includes following definitions.
- platform_file
- current_progress_
- OnUnzipSuccess
- OnUnzipFailure
- OnUnzipProgress
- success_calls
- failure_calls
- progress_calls
- current_progress
- SetUp
- TearDown
- GetTestDataDirectory
- CompareFileAndMD5
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
#include "third_party/zlib/google/zip_reader.h"
#include <set>
#include <string>
#include "base/bind.h"
#include "base/file_util.h"
#include "base/files/file.h"
#include "base/files/scoped_temp_dir.h"
#include "base/logging.h"
#include "base/md5.h"
#include "base/path_service.h"
#include "base/run_loop.h"
#include "base/strings/utf_string_conversions.h"
#include "base/time/time.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "testing/platform_test.h"
#include "third_party/zlib/google/zip_internal.h"
namespace {
const static std::string kQuuxExpectedMD5 = "d1ae4ac8a17a0e09317113ab284b57a6";
class FileWrapper {
public:
typedef enum {
READ_ONLY,
READ_WRITE
} AccessMode;
FileWrapper(const base::FilePath& path, AccessMode mode) {
int flags = base::File::FLAG_READ;
if (mode == READ_ONLY)
flags |= base::File::FLAG_OPEN;
else
flags |= base::File::FLAG_WRITE | base::File::FLAG_CREATE_ALWAYS;
file_.Initialize(path, flags);
}
~FileWrapper() {}
base::PlatformFile platform_file() { return file_.GetPlatformFile(); }
private:
base::File file_;
};
class MockUnzipListener : public base::SupportsWeakPtr<MockUnzipListener> {
public:
MockUnzipListener()
: success_calls_(0),
failure_calls_(0),
progress_calls_(0),
current_progress_(0) {
}
void OnUnzipSuccess() {
success_calls_++;
}
void OnUnzipFailure() {
failure_calls_++;
}
void OnUnzipProgress(int64 progress) {
DCHECK(progress > current_progress_);
progress_calls_++;
current_progress_ = progress;
}
int success_calls() { return success_calls_; }
int failure_calls() { return failure_calls_; }
int progress_calls() { return progress_calls_; }
int current_progress() { return current_progress_; }
private:
int success_calls_;
int failure_calls_;
int progress_calls_;
int64 current_progress_;
};
}
namespace zip {
class ZipReaderTest : public PlatformTest {
protected:
virtual void SetUp() {
PlatformTest::SetUp();
ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
test_dir_ = temp_dir_.path();
ASSERT_TRUE(GetTestDataDirectory(&test_data_dir_));
test_zip_file_ = test_data_dir_.AppendASCII("test.zip");
evil_zip_file_ = test_data_dir_.AppendASCII("evil.zip");
evil_via_invalid_utf8_zip_file_ = test_data_dir_.AppendASCII(
"evil_via_invalid_utf8.zip");
evil_via_absolute_file_name_zip_file_ = test_data_dir_.AppendASCII(
"evil_via_absolute_file_name.zip");
test_zip_contents_.insert(base::FilePath(FILE_PATH_LITERAL("foo/")));
test_zip_contents_.insert(base::FilePath(FILE_PATH_LITERAL("foo/bar/")));
test_zip_contents_.insert(
base::FilePath(FILE_PATH_LITERAL("foo/bar/baz.txt")));
test_zip_contents_.insert(
base::FilePath(FILE_PATH_LITERAL("foo/bar/quux.txt")));
test_zip_contents_.insert(
base::FilePath(FILE_PATH_LITERAL("foo/bar.txt")));
test_zip_contents_.insert(base::FilePath(FILE_PATH_LITERAL("foo.txt")));
test_zip_contents_.insert(
base::FilePath(FILE_PATH_LITERAL("foo/bar/.hidden")));
}
virtual void TearDown() {
PlatformTest::TearDown();
}
bool GetTestDataDirectory(base::FilePath* path) {
bool success = PathService::Get(base::DIR_SOURCE_ROOT, path);
EXPECT_TRUE(success);
if (!success)
return false;
*path = path->AppendASCII("third_party");
*path = path->AppendASCII("zlib");
*path = path->AppendASCII("google");
*path = path->AppendASCII("test");
*path = path->AppendASCII("data");
return true;
}
bool CompareFileAndMD5(const base::FilePath& path,
const std::string expected_md5) {
std::string output;
if (!base::ReadFileToString(path, &output))
return false;
const std::string md5 = base::MD5String(output);
return expected_md5 == md5;
}
base::FilePath test_dir_;
base::FilePath test_data_dir_;
base::FilePath test_zip_file_;
base::FilePath evil_zip_file_;
base::FilePath evil_via_invalid_utf8_zip_file_;
base::FilePath evil_via_absolute_file_name_zip_file_;
std::set<base::FilePath> test_zip_contents_;
base::ScopedTempDir temp_dir_;
base::MessageLoop message_loop_;
};
TEST_F(ZipReaderTest, Open_ValidZipFile) {
ZipReader reader;
ASSERT_TRUE(reader.Open(test_zip_file_));
}
TEST_F(ZipReaderTest, Open_ValidZipPlatformFile) {
ZipReader reader;
FileWrapper zip_fd_wrapper(test_zip_file_, FileWrapper::READ_ONLY);
ASSERT_TRUE(reader.OpenFromPlatformFile(zip_fd_wrapper.platform_file()));
}
TEST_F(ZipReaderTest, Open_NonExistentFile) {
ZipReader reader;
ASSERT_FALSE(reader.Open(test_data_dir_.AppendASCII("nonexistent.zip")));
}
TEST_F(ZipReaderTest, Open_ExistentButNonZipFile) {
ZipReader reader;
ASSERT_FALSE(reader.Open(test_data_dir_.AppendASCII("create_test_zip.sh")));
}
TEST_F(ZipReaderTest, Iteration) {
std::set<base::FilePath> actual_contents;
ZipReader reader;
ASSERT_TRUE(reader.Open(test_zip_file_));
while (reader.HasMore()) {
ASSERT_TRUE(reader.OpenCurrentEntryInZip());
actual_contents.insert(reader.current_entry_info()->file_path());
ASSERT_TRUE(reader.AdvanceToNextEntry());
}
EXPECT_FALSE(reader.AdvanceToNextEntry());
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);
}
TEST_F(ZipReaderTest, PlatformFileIteration) {
std::set<base::FilePath> actual_contents;
ZipReader reader;
FileWrapper zip_fd_wrapper(test_zip_file_, FileWrapper::READ_ONLY);
ASSERT_TRUE(reader.OpenFromPlatformFile(zip_fd_wrapper.platform_file()));
while (reader.HasMore()) {
ASSERT_TRUE(reader.OpenCurrentEntryInZip());
actual_contents.insert(reader.current_entry_info()->file_path());
ASSERT_TRUE(reader.AdvanceToNextEntry());
}
EXPECT_FALSE(reader.AdvanceToNextEntry());
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);
}
TEST_F(ZipReaderTest, LocateAndOpenEntry_ValidFile) {
std::set<base::FilePath> actual_contents;
ZipReader reader;
ASSERT_TRUE(reader.Open(test_zip_file_));
base::FilePath target_path(FILE_PATH_LITERAL("foo/bar/quux.txt"));
ASSERT_TRUE(reader.LocateAndOpenEntry(target_path));
EXPECT_EQ(target_path, reader.current_entry_info()->file_path());
}
TEST_F(ZipReaderTest, LocateAndOpenEntry_NonExistentFile) {
std::set<base::FilePath> actual_contents;
ZipReader reader;
ASSERT_TRUE(reader.Open(test_zip_file_));
base::FilePath target_path(FILE_PATH_LITERAL("nonexistent.txt"));
ASSERT_FALSE(reader.LocateAndOpenEntry(target_path));
EXPECT_EQ(NULL, reader.current_entry_info());
}
TEST_F(ZipReaderTest, ExtractCurrentEntryToFilePath_RegularFile) {
ZipReader reader;
ASSERT_TRUE(reader.Open(test_zip_file_));
base::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")));
std::string output;
ASSERT_TRUE(base::ReadFileToString(test_dir_.AppendASCII("quux.txt"),
&output));
const std::string md5 = base::MD5String(output);
EXPECT_EQ(kQuuxExpectedMD5, md5);
EXPECT_LT(static_cast<size_t>(internal::kZipBufSize), output.size());
}
TEST_F(ZipReaderTest, PlatformFileExtractCurrentEntryToFilePath_RegularFile) {
ZipReader reader;
FileWrapper zip_fd_wrapper(test_zip_file_, FileWrapper::READ_ONLY);
ASSERT_TRUE(reader.OpenFromPlatformFile(zip_fd_wrapper.platform_file()));
base::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")));
std::string output;
ASSERT_TRUE(base::ReadFileToString(test_dir_.AppendASCII("quux.txt"),
&output));
const std::string md5 = base::MD5String(output);
EXPECT_EQ(kQuuxExpectedMD5, md5);
EXPECT_LT(static_cast<size_t>(internal::kZipBufSize), output.size());
}
#if defined(OS_POSIX)
TEST_F(ZipReaderTest, PlatformFileExtractCurrentEntryToFd_RegularFile) {
ZipReader reader;
FileWrapper zip_fd_wrapper(test_zip_file_, FileWrapper::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");
FileWrapper out_fd_w(out_path, FileWrapper::READ_WRITE);
ASSERT_TRUE(reader.LocateAndOpenEntry(target_path));
ASSERT_TRUE(reader.ExtractCurrentEntryToFd(out_fd_w.platform_file()));
std::string output;
ASSERT_TRUE(base::ReadFileToString(test_dir_.AppendASCII("quux.txt"),
&output));
const std::string md5 = base::MD5String(output);
EXPECT_EQ(kQuuxExpectedMD5, md5);
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_));
base::FilePath target_path(FILE_PATH_LITERAL("foo/"));
ASSERT_TRUE(reader.LocateAndOpenEntry(target_path));
ASSERT_TRUE(reader.ExtractCurrentEntryToFilePath(
test_dir_.AppendASCII("foo")));
ASSERT_TRUE(base::DirectoryExists(test_dir_.AppendASCII("foo")));
}
TEST_F(ZipReaderTest, ExtractCurrentEntryIntoDirectory_RegularFile) {
ZipReader reader;
ASSERT_TRUE(reader.Open(test_zip_file_));
base::FilePath target_path(FILE_PATH_LITERAL("foo/bar/quux.txt"));
ASSERT_TRUE(reader.LocateAndOpenEntry(target_path));
ASSERT_TRUE(reader.ExtractCurrentEntryIntoDirectory(test_dir_));
ASSERT_TRUE(base::DirectoryExists(test_dir_.AppendASCII("foo/bar")));
std::string output;
ASSERT_TRUE(base::ReadFileToString(
test_dir_.AppendASCII("foo/bar/quux.txt"), &output));
const std::string md5 = base::MD5String(output);
EXPECT_EQ(kQuuxExpectedMD5, md5);
}
TEST_F(ZipReaderTest, current_entry_info_RegularFile) {
ZipReader reader;
ASSERT_TRUE(reader.Open(test_zip_file_));
base::FilePath target_path(FILE_PATH_LITERAL("foo/bar/quux.txt"));
ASSERT_TRUE(reader.LocateAndOpenEntry(target_path));
ZipReader::EntryInfo* current_entry_info = reader.current_entry_info();
EXPECT_EQ(target_path, current_entry_info->file_path());
EXPECT_EQ(13527, current_entry_info->original_size());
base::Time::Exploded exploded = {};
current_entry_info->last_modified().LocalExplode(&exploded);
EXPECT_EQ(2009, exploded.year);
EXPECT_EQ(5, exploded.month);
EXPECT_EQ(29, exploded.day_of_month);
EXPECT_EQ(6, exploded.hour);
EXPECT_EQ(22, exploded.minute);
EXPECT_EQ(20, exploded.second);
EXPECT_EQ(0, exploded.millisecond);
EXPECT_FALSE(current_entry_info->is_unsafe());
EXPECT_FALSE(current_entry_info->is_directory());
}
TEST_F(ZipReaderTest, current_entry_info_DotDotFile) {
ZipReader reader;
ASSERT_TRUE(reader.Open(evil_zip_file_));
base::FilePath target_path(FILE_PATH_LITERAL(
"../levilevilevilevilevilevilevilevilevilevilevilevil"));
ASSERT_TRUE(reader.LocateAndOpenEntry(target_path));
ZipReader::EntryInfo* current_entry_info = reader.current_entry_info();
EXPECT_EQ(target_path, current_entry_info->file_path());
EXPECT_TRUE(current_entry_info->is_unsafe());
EXPECT_FALSE(current_entry_info->is_directory());
}
TEST_F(ZipReaderTest, current_entry_info_InvalidUTF8File) {
ZipReader reader;
ASSERT_TRUE(reader.Open(evil_via_invalid_utf8_zip_file_));
ASSERT_TRUE(reader.AdvanceToNextEntry());
ASSERT_TRUE(reader.OpenCurrentEntryInZip());
ZipReader::EntryInfo* current_entry_info = reader.current_entry_info();
EXPECT_TRUE(current_entry_info->is_unsafe());
EXPECT_FALSE(current_entry_info->is_directory());
}
TEST_F(ZipReaderTest, current_entry_info_AbsoluteFile) {
ZipReader reader;
ASSERT_TRUE(reader.Open(evil_via_absolute_file_name_zip_file_));
base::FilePath target_path(FILE_PATH_LITERAL("/evil.txt"));
ASSERT_TRUE(reader.LocateAndOpenEntry(target_path));
ZipReader::EntryInfo* current_entry_info = reader.current_entry_info();
EXPECT_EQ(target_path, current_entry_info->file_path());
EXPECT_TRUE(current_entry_info->is_unsafe());
EXPECT_FALSE(current_entry_info->is_directory());
}
TEST_F(ZipReaderTest, current_entry_info_Directory) {
ZipReader reader;
ASSERT_TRUE(reader.Open(test_zip_file_));
base::FilePath target_path(FILE_PATH_LITERAL("foo/bar/"));
ASSERT_TRUE(reader.LocateAndOpenEntry(target_path));
ZipReader::EntryInfo* current_entry_info = reader.current_entry_info();
EXPECT_EQ(base::FilePath(FILE_PATH_LITERAL("foo/bar/")),
current_entry_info->file_path());
EXPECT_EQ(0, current_entry_info->original_size());
base::Time::Exploded exploded = {};
current_entry_info->last_modified().LocalExplode(&exploded);
EXPECT_EQ(2009, exploded.year);
EXPECT_EQ(5, exploded.month);
EXPECT_EQ(31, exploded.day_of_month);
EXPECT_EQ(15, exploded.hour);
EXPECT_EQ(49, exploded.minute);
EXPECT_EQ(52, exploded.second);
EXPECT_EQ(0, exploded.millisecond);
EXPECT_FALSE(current_entry_info->is_unsafe());
EXPECT_TRUE(current_entry_info->is_directory());
}
TEST_F(ZipReaderTest, OpenFromString) {
const char kTestData[] =
"\x50\x4b\x03\x04\x0a\x00\x00\x00\x00\x00\xa4\x66\x24\x41\x13\xe8"
"\xcb\x27\x10\x00\x00\x00\x10\x00\x00\x00\x08\x00\x1c\x00\x74\x65"
"\x73\x74\x2e\x74\x78\x74\x55\x54\x09\x00\x03\x34\x89\x45\x50\x34"
"\x89\x45\x50\x75\x78\x0b\x00\x01\x04\x8e\xf0\x00\x00\x04\x88\x13"
"\x00\x00\x54\x68\x69\x73\x20\x69\x73\x20\x61\x20\x74\x65\x73\x74"
"\x2e\x0a\x50\x4b\x01\x02\x1e\x03\x0a\x00\x00\x00\x00\x00\xa4\x66"
"\x24\x41\x13\xe8\xcb\x27\x10\x00\x00\x00\x10\x00\x00\x00\x08\x00"
"\x18\x00\x00\x00\x00\x00\x01\x00\x00\x00\xa4\x81\x00\x00\x00\x00"
"\x74\x65\x73\x74\x2e\x74\x78\x74\x55\x54\x05\x00\x03\x34\x89\x45"
"\x50\x75\x78\x0b\x00\x01\x04\x8e\xf0\x00\x00\x04\x88\x13\x00\x00"
"\x50\x4b\x05\x06\x00\x00\x00\x00\x01\x00\x01\x00\x4e\x00\x00\x00"
"\x52\x00\x00\x00\x00\x00";
std::string data(kTestData, arraysize(kTestData));
ZipReader reader;
ASSERT_TRUE(reader.OpenFromString(data));
base::FilePath target_path(FILE_PATH_LITERAL("test.txt"));
ASSERT_TRUE(reader.LocateAndOpenEntry(target_path));
ASSERT_TRUE(reader.ExtractCurrentEntryToFilePath(
test_dir_.AppendASCII("test.txt")));
std::string actual;
ASSERT_TRUE(base::ReadFileToString(
test_dir_.AppendASCII("test.txt"), &actual));
EXPECT_EQ(std::string("This is a test.\n"), actual);
}
TEST_F(ZipReaderTest, ExtractToFileAsync_RegularFile) {
MockUnzipListener listener;
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,
base::Bind(&MockUnzipListener::OnUnzipSuccess,
listener.AsWeakPtr()),
base::Bind(&MockUnzipListener::OnUnzipFailure,
listener.AsWeakPtr()),
base::Bind(&MockUnzipListener::OnUnzipProgress,
listener.AsWeakPtr()));
EXPECT_EQ(0, listener.success_calls());
EXPECT_EQ(0, listener.failure_calls());
EXPECT_EQ(0, listener.progress_calls());
base::RunLoop().RunUntilIdle();
EXPECT_EQ(1, listener.success_calls());
EXPECT_EQ(0, listener.failure_calls());
EXPECT_LE(1, listener.progress_calls());
std::string output;
ASSERT_TRUE(base::ReadFileToString(test_dir_.AppendASCII("quux.txt"),
&output));
const std::string md5 = base::MD5String(output);
EXPECT_EQ(kQuuxExpectedMD5, md5);
int64 file_size = 0;
ASSERT_TRUE(base::GetFileSize(target_file, &file_size));
EXPECT_EQ(file_size, listener.current_progress());
}
TEST_F(ZipReaderTest, ExtractToFileAsync_Directory) {
MockUnzipListener listener;
ZipReader reader;
base::FilePath target_file = test_dir_.AppendASCII("foo");
base::FilePath target_path(FILE_PATH_LITERAL("foo/"));
ASSERT_TRUE(reader.Open(test_zip_file_));
ASSERT_TRUE(reader.LocateAndOpenEntry(target_path));
reader.ExtractCurrentEntryToFilePathAsync(
target_file,
base::Bind(&MockUnzipListener::OnUnzipSuccess,
listener.AsWeakPtr()),
base::Bind(&MockUnzipListener::OnUnzipFailure,
listener.AsWeakPtr()),
base::Bind(&MockUnzipListener::OnUnzipProgress,
listener.AsWeakPtr()));
EXPECT_EQ(0, listener.success_calls());
EXPECT_EQ(0, listener.failure_calls());
EXPECT_EQ(0, listener.progress_calls());
base::RunLoop().RunUntilIdle();
EXPECT_EQ(1, listener.success_calls());
EXPECT_EQ(0, listener.failure_calls());
EXPECT_GE(0, listener.progress_calls());
ASSERT_TRUE(base::DirectoryExists(target_file));
}
}