This source file includes following definitions.
- lie_about_file_existence_
- GetNetExportLogDirectory
- NetExportLogExists
- set_lie_about_net_export_log_directory
- set_lie_about_file_existence
- file_user_blocking_thread_
- SetUp
- TearDown
- GetStateString
- GetLogTypeString
- VerifyNetExportLog
- VerifyFilePathAndStateAfterEnsureInitFailure
- VerifyFilePathAndStateAfterEnsureInit
- VerifyFileAndStateAfterDoStart
- VerifyFileAndStateAfterDoStartStripPrivateData
- VerifyFileAndStateAfterDoStop
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
#include "chrome/browser/net/net_log_temp_file.h"
#include "base/basictypes.h"
#include "base/file_util.h"
#include "base/files/file_path.h"
#include "base/message_loop/message_loop.h"
#include "base/values.h"
#include "build/build_config.h"
#include "chrome/browser/net/chrome_net_log.h"
#include "content/public/test/test_browser_thread.h"
#include "testing/gtest/include/gtest/gtest.h"
using content::BrowserThread;
class TestNetLogTempFile : public NetLogTempFile {
public:
explicit TestNetLogTempFile(ChromeNetLog* chrome_net_log)
: NetLogTempFile(chrome_net_log),
lie_about_net_export_log_directory_(false),
lie_about_file_existence_(false) {
}
virtual bool GetNetExportLogDirectory(base::FilePath* path) OVERRIDE {
if (lie_about_net_export_log_directory_)
return false;
return NetLogTempFile::GetNetExportLogDirectory(path);
}
virtual bool NetExportLogExists() OVERRIDE {
if (lie_about_file_existence_)
return false;
return NetLogTempFile::NetExportLogExists();
}
void set_lie_about_net_export_log_directory(
bool lie_about_net_export_log_directory) {
lie_about_net_export_log_directory_ = lie_about_net_export_log_directory;
}
void set_lie_about_file_existence(bool lie_about_file_existence) {
lie_about_file_existence_ = lie_about_file_existence;
}
private:
bool lie_about_net_export_log_directory_;
bool lie_about_file_existence_;
};
class NetLogTempFileTest : public ::testing::Test {
public:
NetLogTempFileTest()
: net_log_(new ChromeNetLog),
net_log_temp_file_(new TestNetLogTempFile(net_log_.get())),
file_user_blocking_thread_(BrowserThread::FILE_USER_BLOCKING,
&message_loop_) {
}
virtual void SetUp() OVERRIDE {
base::FilePath net_log_dir;
ASSERT_TRUE(net_log_temp_file_->GetNetExportLogDirectory(&net_log_dir));
ASSERT_TRUE(base::CreateTemporaryFileInDir(net_log_dir, &net_export_log_));
net_log_temp_file_->log_filename_ = net_export_log_.BaseName().value();
ASSERT_TRUE(net_log_temp_file_->GetNetExportLog());
net_export_log_ = net_log_temp_file_->log_path_;
ASSERT_FALSE(net_export_log_.empty());
}
virtual void TearDown() OVERRIDE {
ASSERT_TRUE(base::DeleteFile(net_export_log_, false));
}
std::string GetStateString() const {
scoped_ptr<base::DictionaryValue> dict(net_log_temp_file_->GetState());
std::string state;
EXPECT_TRUE(dict->GetString("state", &state));
return state;
}
std::string GetLogTypeString() const {
scoped_ptr<base::DictionaryValue> dict(net_log_temp_file_->GetState());
std::string log_type;
EXPECT_TRUE(dict->GetString("logType", &log_type));
return log_type;
}
void VerifyNetExportLog() {
EXPECT_EQ(net_export_log_, net_log_temp_file_->log_path_);
EXPECT_TRUE(base::PathExists(net_export_log_));
int64 file_size;
EXPECT_TRUE(base::GetFileSize(net_export_log_, &file_size));
EXPECT_GT(file_size, 0);
}
void VerifyFilePathAndStateAfterEnsureInitFailure() {
EXPECT_EQ("UNINITIALIZED", GetStateString());
EXPECT_EQ(NetLogTempFile::STATE_UNINITIALIZED,
net_log_temp_file_->state());
base::FilePath net_export_file_path;
EXPECT_FALSE(net_log_temp_file_->GetFilePath(&net_export_file_path));
}
void VerifyFilePathAndStateAfterEnsureInit() {
EXPECT_EQ("NOT_LOGGING", GetStateString());
EXPECT_EQ(NetLogTempFile::STATE_NOT_LOGGING, net_log_temp_file_->state());
EXPECT_EQ("NONE", GetLogTypeString());
EXPECT_EQ(NetLogTempFile::LOG_TYPE_NONE, net_log_temp_file_->log_type());
base::FilePath net_export_file_path;
EXPECT_FALSE(net_log_temp_file_->GetFilePath(&net_export_file_path));
EXPECT_FALSE(net_log_temp_file_->NetExportLogExists());
}
void VerifyFileAndStateAfterDoStart() {
EXPECT_EQ("LOGGING", GetStateString());
EXPECT_EQ(NetLogTempFile::STATE_LOGGING, net_log_temp_file_->state());
EXPECT_EQ("NORMAL", GetLogTypeString());
EXPECT_EQ(NetLogTempFile::LOG_TYPE_NORMAL, net_log_temp_file_->log_type());
base::FilePath net_export_file_path;
EXPECT_FALSE(net_log_temp_file_->GetFilePath(&net_export_file_path));
VerifyNetExportLog();
EXPECT_EQ(net::NetLog::LOG_ALL_BUT_BYTES, net_log_->GetLogLevel());
}
void VerifyFileAndStateAfterDoStartStripPrivateData() {
EXPECT_EQ("LOGGING", GetStateString());
EXPECT_EQ(NetLogTempFile::STATE_LOGGING, net_log_temp_file_->state());
EXPECT_EQ("STRIP_PRIVATE_DATA", GetLogTypeString());
EXPECT_EQ(NetLogTempFile::LOG_TYPE_STRIP_PRIVATE_DATA,
net_log_temp_file_->log_type());
base::FilePath net_export_file_path;
EXPECT_FALSE(net_log_temp_file_->GetFilePath(&net_export_file_path));
VerifyNetExportLog();
EXPECT_EQ(net::NetLog::LOG_STRIP_PRIVATE_DATA, net_log_->GetLogLevel());
}
void VerifyFileAndStateAfterDoStop() {
EXPECT_EQ("NOT_LOGGING", GetStateString());
EXPECT_EQ(NetLogTempFile::STATE_NOT_LOGGING, net_log_temp_file_->state());
EXPECT_EQ("NORMAL", GetLogTypeString());
EXPECT_EQ(NetLogTempFile::LOG_TYPE_NORMAL, net_log_temp_file_->log_type());
base::FilePath net_export_file_path;
EXPECT_TRUE(net_log_temp_file_->GetFilePath(&net_export_file_path));
EXPECT_TRUE(base::PathExists(net_export_file_path));
EXPECT_EQ(net_export_log_, net_export_file_path);
VerifyNetExportLog();
}
scoped_ptr<ChromeNetLog> net_log_;
scoped_ptr<TestNetLogTempFile> net_log_temp_file_;
base::FilePath net_export_log_;
private:
base::MessageLoop message_loop_;
content::TestBrowserThread file_user_blocking_thread_;
};
TEST_F(NetLogTempFileTest, EnsureInitFailure) {
net_log_temp_file_->set_lie_about_net_export_log_directory(true);
EXPECT_FALSE(net_log_temp_file_->EnsureInit());
VerifyFilePathAndStateAfterEnsureInitFailure();
net_log_temp_file_->ProcessCommand(NetLogTempFile::DO_START);
VerifyFilePathAndStateAfterEnsureInitFailure();
}
TEST_F(NetLogTempFileTest, EnsureInitAllowStart) {
net_log_temp_file_->set_lie_about_file_existence(true);
EXPECT_TRUE(net_log_temp_file_->EnsureInit());
VerifyFilePathAndStateAfterEnsureInit();
EXPECT_TRUE(net_log_temp_file_->EnsureInit());
VerifyFilePathAndStateAfterEnsureInit();
}
TEST_F(NetLogTempFileTest, EnsureInitAllowStartOrSend) {
EXPECT_TRUE(net_log_temp_file_->EnsureInit());
EXPECT_EQ("NOT_LOGGING", GetStateString());
EXPECT_EQ(NetLogTempFile::STATE_NOT_LOGGING, net_log_temp_file_->state());
EXPECT_EQ("UNKNOWN", GetLogTypeString());
EXPECT_EQ(NetLogTempFile::LOG_TYPE_UNKNOWN, net_log_temp_file_->log_type());
EXPECT_EQ(net_export_log_, net_log_temp_file_->log_path_);
EXPECT_TRUE(base::PathExists(net_export_log_));
base::FilePath net_export_file_path;
EXPECT_TRUE(net_log_temp_file_->GetFilePath(&net_export_file_path));
EXPECT_TRUE(base::PathExists(net_export_file_path));
EXPECT_EQ(net_export_log_, net_export_file_path);
net_log_temp_file_->set_lie_about_file_existence(true);
EXPECT_FALSE(net_log_temp_file_->GetFilePath(&net_export_file_path));
}
TEST_F(NetLogTempFileTest, ProcessCommandDoStartAndStop) {
net_log_temp_file_->ProcessCommand(NetLogTempFile::DO_START);
VerifyFileAndStateAfterDoStart();
net_log_temp_file_->ProcessCommand(NetLogTempFile::DO_START);
VerifyFileAndStateAfterDoStart();
net_log_temp_file_->ProcessCommand(NetLogTempFile::DO_STOP);
VerifyFileAndStateAfterDoStop();
net_log_temp_file_->ProcessCommand(NetLogTempFile::DO_STOP);
VerifyFileAndStateAfterDoStop();
}
TEST_F(NetLogTempFileTest, DoStartClearsFile) {
net_log_temp_file_->ProcessCommand(NetLogTempFile::DO_START);
VerifyFileAndStateAfterDoStart();
int64 start_file_size;
EXPECT_TRUE(base::GetFileSize(net_export_log_, &start_file_size));
net_log_temp_file_->ProcessCommand(NetLogTempFile::DO_STOP);
VerifyFileAndStateAfterDoStop();
int64 stop_file_size;
EXPECT_TRUE(base::GetFileSize(net_export_log_, &stop_file_size));
EXPECT_GE(stop_file_size, start_file_size);
std::string junk_data("Hello");
EXPECT_GT(base::AppendToFile(
net_export_log_, junk_data.c_str(), junk_data.size()), 0);
int64 junk_file_size;
EXPECT_TRUE(base::GetFileSize(net_export_log_, &junk_file_size));
EXPECT_GT(junk_file_size, stop_file_size);
net_log_temp_file_->ProcessCommand(NetLogTempFile::DO_START);
VerifyFileAndStateAfterDoStart();
int64 new_start_file_size;
EXPECT_TRUE(base::GetFileSize(net_export_log_, &new_start_file_size));
EXPECT_EQ(new_start_file_size, start_file_size);
net_log_temp_file_->ProcessCommand(NetLogTempFile::DO_STOP);
VerifyFileAndStateAfterDoStop();
int64 new_stop_file_size;
EXPECT_TRUE(base::GetFileSize(net_export_log_, &new_stop_file_size));
EXPECT_EQ(new_stop_file_size, stop_file_size);
}
TEST_F(NetLogTempFileTest, CheckAddEvent) {
net_log_temp_file_->ProcessCommand(NetLogTempFile::DO_START);
VerifyFileAndStateAfterDoStart();
net_log_temp_file_->ProcessCommand(NetLogTempFile::DO_STOP);
VerifyFileAndStateAfterDoStop();
int64 stop_file_size;
EXPECT_TRUE(base::GetFileSize(net_export_log_, &stop_file_size));
net_log_temp_file_->ProcessCommand(NetLogTempFile::DO_START);
VerifyFileAndStateAfterDoStart();
net_log_->AddGlobalEntry(net::NetLog::TYPE_CANCELLED);
net_log_temp_file_->ProcessCommand(NetLogTempFile::DO_STOP);
VerifyFileAndStateAfterDoStop();
int64 new_stop_file_size;
EXPECT_TRUE(base::GetFileSize(net_export_log_, &new_stop_file_size));
EXPECT_GE(new_stop_file_size, stop_file_size);
}