Index: chrome/browser/net/net_log_temp_file_unittest.cc |
=================================================================== |
--- chrome/browser/net/net_log_temp_file_unittest.cc (revision 0) |
+++ chrome/browser/net/net_log_temp_file_unittest.cc (revision 0) |
@@ -0,0 +1,208 @@ |
+// Copyright (c) 2013 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#include "chrome/browser/net/net_log_temp_file.h" |
+ |
+#include "base/basictypes.h" |
+#include "base/file_path.h" |
+#include "base/file_util.h" |
+#include "base/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_file_existence_(false) { |
+ } |
+ |
mmenke
2013/01/24 03:55:34
nit: "NetLogTempFile implementation:"
ramant (doing other things)
2013/01/24 21:56:49
Done.
|
+ bool NetExportLogExists() { |
mmenke
2013/01/24 03:55:34
Add virtual and OVERRIDE
ramant (doing other things)
2013/01/24 21:56:49
Done.
|
+ if (lie_about_file_existence_) |
+ return false; |
+ return NetLogTempFile::NetExportLogExists(); |
+ } |
+ |
+ void set_lie_about_file_existence(bool lie_about_file_existence) { |
+ lie_about_file_existence_ = lie_about_file_existence; |
+ } |
+ |
+ private: |
+ bool lie_about_file_existence_; |
+}; |
+ |
+class NetLogTempFileTest : public ::testing::Test { |
+ protected: |
+ NetLogTempFileTest() |
+ : file_user_blocking_thread_(BrowserThread::FILE_USER_BLOCKING, |
+ &message_loop_) { |
+ net_log_.reset(new ChromeNetLog); |
+ net_log_temp_file_ = new TestNetLogTempFile(net_log_.get()); |
+ } |
+ |
mmenke
2013/01/24 03:55:34
nit: ::testing::Test implementation:
ramant (doing other things)
2013/01/24 21:56:49
Done.
|
+ virtual void SetUp() OVERRIDE { |
+ // Get a temporary file name for unit tests. |
+ ASSERT_TRUE(file_util::CreateTemporaryFile(&net_export_log_)); |
mmenke
2013/01/24 03:55:34
Are we guaranteed this will be in the file_util::G
ramant (doing other things)
2013/01/24 21:56:49
Good point.
Added GetNetExportLogDirectory (to ge
|
+ |
+ net_log_temp_file_->log_filename_ = net_export_log_.BaseName().value(); |
+ } |
+ |
+ virtual void TearDown() OVERRIDE { |
+ // Delete the temporary file we have created. |
+ ASSERT_TRUE(file_util::Delete(net_export_log_, false)); |
+ } |
+ |
+ scoped_ptr<ChromeNetLog> net_log_; |
+ TestNetLogTempFile* net_log_temp_file_; |
+ FilePath net_export_log_; |
+ |
+ private: |
+ MessageLoop message_loop_; |
+ content::TestBrowserThread file_user_blocking_thread_; |
+}; |
+ |
+TEST_F(NetLogTempFileTest, InitAllowStart) { |
+ net_log_temp_file_->set_lie_about_file_existence(true); |
+ net_log_temp_file_->Init(); |
+ |
+ base::DictionaryValue* dict = net_log_temp_file_->GetState(); |
+ std::string state; |
+ EXPECT_TRUE(dict->GetString("state", &state)); |
+ EXPECT_EQ("ALLOW_START", state); |
+ EXPECT_EQ(NetLogTempFile::STATE_ALLOW_START, net_log_temp_file_->state()); |
+ |
+ FilePath net_export_file_path; |
+ EXPECT_FALSE(net_log_temp_file_->GetFilePath(&net_export_file_path)); |
+ EXPECT_FALSE(net_log_temp_file_->NetExportLogExists()); |
+} |
+ |
+TEST_F(NetLogTempFileTest, InitAllowStartOrSend) { |
+ net_log_temp_file_->Init(); |
+ |
+ base::DictionaryValue* dict = net_log_temp_file_->GetState(); |
+ std::string state; |
+ EXPECT_TRUE(dict->GetString("state", &state)); |
+ EXPECT_EQ("ALLOW_START_SEND", state); |
+ EXPECT_EQ(NetLogTempFile::STATE_ALLOW_START_SEND, |
+ net_log_temp_file_->state()); |
+ EXPECT_TRUE(file_util::PathExists(net_export_log_)); |
mmenke
2013/01/24 03:55:34
So we can't rely on path equality, as a string, on
ramant (doing other things)
2013/01/24 21:56:49
Correct.
|
+ |
+ FilePath net_export_file_path; |
+ EXPECT_TRUE(net_log_temp_file_->GetFilePath(&net_export_file_path)); |
+ EXPECT_TRUE(file_util::PathExists(net_export_file_path)); |
+} |
+ |
+TEST_F(NetLogTempFileTest, ProcessCommandDoStartAndStop) { |
+ // Execute DO_START command via ProcessCommand and verfiy that we transition |
+ // to ALLOW_STOP state. |
+ net_log_temp_file_->ProcessCommand(NetLogTempFile::DO_START); |
+ base::DictionaryValue* dict = net_log_temp_file_->GetState(); |
mmenke
2013/01/24 03:55:34
These are leaks. Should store these in scoped_ptr
ramant (doing other things)
2013/01/24 21:56:49
Many thanks for catching this.
Done.
|
+ std::string state; |
+ EXPECT_TRUE(dict->GetString("state", &state)); |
+ EXPECT_EQ("ALLOW_STOP", state); |
+ EXPECT_EQ(NetLogTempFile::STATE_ALLOW_STOP, net_log_temp_file_->state()); |
+ EXPECT_TRUE(file_util::PathExists(net_export_log_)); |
+ int64 file_size; |
+ EXPECT_TRUE(file_util::GetFileSize(net_export_log_, &file_size)); |
+ EXPECT_GT(file_size, 0); |
+ |
+ FilePath net_export_file_path; |
+ EXPECT_FALSE(net_log_temp_file_->GetFilePath(&net_export_file_path)); |
+ |
+ // Execute DO_STOP command via ProcessCommand and verfiy that we have |
+ // transitioned to STATE_ALLOW_START_SEND state. |
+ net_log_temp_file_->ProcessCommand(NetLogTempFile::DO_STOP); |
+ dict = net_log_temp_file_->GetState(); |
+ EXPECT_TRUE(dict->GetString("state", &state)); |
+ EXPECT_EQ("ALLOW_START_SEND", state); |
+ EXPECT_EQ(NetLogTempFile::STATE_ALLOW_START_SEND, |
+ net_log_temp_file_->state()); |
+ EXPECT_TRUE(file_util::PathExists(net_export_log_)); |
+ |
+ EXPECT_TRUE(net_log_temp_file_->GetFilePath(&net_export_file_path)); |
+ EXPECT_TRUE(file_util::PathExists(net_export_file_path)); |
+ |
+ // TODO(rtenneti): Add check for the contents of the file, by adding a random |
+ // NetLog event and verify it is there. |
+} |
+ |
+TEST_F(NetLogTempFileTest, DoStartClearsFile) { |
+ // Execute DO_START command via ProcessCommand and verfiy that we transition |
+ // to ALLOW_STOP state. |
+ net_log_temp_file_->ProcessCommand(NetLogTempFile::DO_START); |
+ base::DictionaryValue* dict = net_log_temp_file_->GetState(); |
+ std::string state; |
+ EXPECT_TRUE(dict->GetString("state", &state)); |
+ EXPECT_EQ("ALLOW_STOP", state); |
+ int64 start_file_size; |
+ EXPECT_TRUE(file_util::GetFileSize(net_export_log_, &start_file_size)); |
+ |
+ // Execute DO_STOP command via ProcessCommand and verfiy that we have |
+ // transitioned to STATE_ALLOW_START_SEND state. |
+ net_log_temp_file_->ProcessCommand(NetLogTempFile::DO_STOP); |
+ dict = net_log_temp_file_->GetState(); |
+ EXPECT_TRUE(dict->GetString("state", &state)); |
+ EXPECT_EQ("ALLOW_START_SEND", state); |
+ EXPECT_EQ(NetLogTempFile::STATE_ALLOW_START_SEND, |
+ net_log_temp_file_->state()); |
+ int64 stop_file_size; |
+ EXPECT_TRUE(file_util::GetFileSize(net_export_log_, &stop_file_size)); |
+ EXPECT_GE(stop_file_size, start_file_size); |
+ |
+ // Add some junk at the end of the file. |
+ std::string junk_data("Hello"); |
+ EXPECT_GT(file_util::AppendToFile( |
+ net_export_log_, junk_data.c_str(), junk_data.size()), 0); |
+ int64 junk_file_size; |
+ EXPECT_TRUE(file_util::GetFileSize(net_export_log_, &junk_file_size)); |
+ EXPECT_GT(junk_file_size, start_file_size); |
+ |
+ // Execute DO_START command and make sure the file is back to the size before |
+ // addition of junk data. |
+ net_log_temp_file_->ProcessCommand(NetLogTempFile::DO_START); |
+ dict = net_log_temp_file_->GetState(); |
+ EXPECT_TRUE(dict->GetString("state", &state)); |
+ EXPECT_EQ("ALLOW_STOP", state); |
+ EXPECT_EQ(NetLogTempFile::STATE_ALLOW_STOP, net_log_temp_file_->state()); |
+ int64 new_file_size; |
+ EXPECT_TRUE(file_util::GetFileSize(net_export_log_, &new_file_size)); |
+ EXPECT_EQ(new_file_size, start_file_size); |
+ |
+ // Execute DO_STOP so that temporary file could be deleted. |
+ net_log_temp_file_->ProcessCommand(NetLogTempFile::DO_STOP); |
+} |
+ |
+TEST_F(NetLogTempFileTest, CheckAddEvent) { |
+ // Add an event to |net_log_| and then test to make sure that, after we stop |
+ // logging, the file is larger than the file created without that event. |
+ |
+ // Perform DO_START/DO_STOP and get the file size. |
+ net_log_temp_file_->ProcessCommand(NetLogTempFile::DO_START); |
+ base::DictionaryValue* dict = net_log_temp_file_->GetState(); |
+ std::string state; |
+ EXPECT_TRUE(dict->GetString("state", &state)); |
+ EXPECT_EQ("ALLOW_STOP", state); |
+ |
+ // Get file size without the event. |
+ net_log_temp_file_->ProcessCommand(NetLogTempFile::DO_STOP); |
+ int64 stop_file_size; |
+ EXPECT_TRUE(file_util::GetFileSize(net_export_log_, &stop_file_size)); |
+ |
+ // Perform DO_START and add an Event and then DO_STOP and then compare |
+ // file sizes. |
+ net_log_temp_file_->ProcessCommand(NetLogTempFile::DO_START); |
+ |
+ // Log an event. |
+ net_log_->AddGlobalEntry(net::NetLog::TYPE_CANCELLED); |
+ |
+ net_log_temp_file_->ProcessCommand(NetLogTempFile::DO_STOP); |
+ int64 new_stop_file_size; |
+ EXPECT_TRUE(file_util::GetFileSize(net_export_log_, &new_stop_file_size)); |
mmenke
2013/01/24 03:55:34
Are we guaranteed that file_util will see the larg
ramant (doing other things)
2013/01/24 21:56:49
We delete net_log_logger in DoStop, which closes t
|
+ EXPECT_GE(new_stop_file_size, stop_file_size); |
+} |