NaCl: Update revision in DEPS, r12770 -> r12773
[chromium-blink-merge.git] / chrome / browser / net / net_log_temp_file_unittest.cc
blob78f55d1dd7ccc58f743dbaa6b257bfcb032fad69
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "chrome/browser/net/net_log_temp_file.h"
7 #include "base/basictypes.h"
8 #include "base/file_util.h"
9 #include "base/files/file_path.h"
10 #include "base/message_loop/message_loop.h"
11 #include "base/values.h"
12 #include "build/build_config.h"
13 #include "chrome/browser/net/chrome_net_log.h"
14 #include "content/public/test/test_browser_thread.h"
15 #include "testing/gtest/include/gtest/gtest.h"
17 using content::BrowserThread;
19 class TestNetLogTempFile : public NetLogTempFile {
20 public:
21 explicit TestNetLogTempFile(ChromeNetLog* chrome_net_log)
22 : NetLogTempFile(chrome_net_log),
23 lie_about_net_export_log_directory_(false),
24 lie_about_file_existence_(false) {
27 // NetLogTempFile implementation:
28 virtual bool GetNetExportLogDirectory(base::FilePath* path) OVERRIDE {
29 if (lie_about_net_export_log_directory_)
30 return false;
31 return NetLogTempFile::GetNetExportLogDirectory(path);
34 virtual bool NetExportLogExists() OVERRIDE {
35 if (lie_about_file_existence_)
36 return false;
37 return NetLogTempFile::NetExportLogExists();
40 void set_lie_about_net_export_log_directory(
41 bool lie_about_net_export_log_directory) {
42 lie_about_net_export_log_directory_ = lie_about_net_export_log_directory;
45 void set_lie_about_file_existence(bool lie_about_file_existence) {
46 lie_about_file_existence_ = lie_about_file_existence;
49 private:
50 bool lie_about_net_export_log_directory_;
51 bool lie_about_file_existence_;
54 class NetLogTempFileTest : public ::testing::Test {
55 public:
56 NetLogTempFileTest()
57 : net_log_(new ChromeNetLog),
58 net_log_temp_file_(new TestNetLogTempFile(net_log_.get())),
59 file_user_blocking_thread_(BrowserThread::FILE_USER_BLOCKING,
60 &message_loop_) {
63 // ::testing::Test implementation:
64 virtual void SetUp() OVERRIDE {
65 // Get a temporary file name for unit tests.
66 base::FilePath net_log_dir;
67 ASSERT_TRUE(net_log_temp_file_->GetNetExportLogDirectory(&net_log_dir));
68 ASSERT_TRUE(base::CreateTemporaryFileInDir(net_log_dir, &net_export_log_));
70 net_log_temp_file_->log_filename_ = net_export_log_.BaseName().value();
72 // CreateTemporaryFileInDir may return a legacy 8.3 file name on windows.
73 // Need to use the original directory name for string comparisons.
74 ASSERT_TRUE(net_log_temp_file_->GetNetExportLog());
75 net_export_log_ = net_log_temp_file_->log_path_;
76 ASSERT_FALSE(net_export_log_.empty());
79 virtual void TearDown() OVERRIDE {
80 // Delete the temporary file we have created.
81 ASSERT_TRUE(base::DeleteFile(net_export_log_, false));
84 std::string GetStateString() const {
85 scoped_ptr<base::DictionaryValue> dict(net_log_temp_file_->GetState());
86 std::string state;
87 EXPECT_TRUE(dict->GetString("state", &state));
88 return state;
91 // Make sure the export file has been created and is non-empty, as net
92 // constants will always be written to it on creation.
93 void VerifyNetExportLog() {
94 EXPECT_EQ(net_export_log_, net_log_temp_file_->log_path_);
95 EXPECT_TRUE(base::PathExists(net_export_log_));
97 int64 file_size;
98 // base::GetFileSize returns proper file size on open handles.
99 EXPECT_TRUE(base::GetFileSize(net_export_log_, &file_size));
100 EXPECT_GT(file_size, 0);
103 // Verify state and GetFilePath return correct values if EnsureInit() fails.
104 void VerifyFilePathAndStateAfterEnsureInitFailure() {
105 EXPECT_EQ("UNINITIALIZED", GetStateString());
106 EXPECT_EQ(NetLogTempFile::STATE_UNINITIALIZED,
107 net_log_temp_file_->state());
109 base::FilePath net_export_file_path;
110 EXPECT_FALSE(net_log_temp_file_->GetFilePath(&net_export_file_path));
113 // When we lie in NetExportLogExists, make sure state and GetFilePath return
114 // correct values.
115 void VerifyFilePathAndStateAfterEnsureInit() {
116 EXPECT_EQ("ALLOW_START", GetStateString());
117 EXPECT_EQ(NetLogTempFile::STATE_ALLOW_START, net_log_temp_file_->state());
119 base::FilePath net_export_file_path;
120 EXPECT_FALSE(net_log_temp_file_->GetFilePath(&net_export_file_path));
121 EXPECT_FALSE(net_log_temp_file_->NetExportLogExists());
124 // Make sure the export file has been successfully initialized.
125 void VerifyFileAndStateAfterDoStart() {
126 EXPECT_EQ("ALLOW_STOP", GetStateString());
127 EXPECT_EQ(NetLogTempFile::STATE_ALLOW_STOP, net_log_temp_file_->state());
129 // Check GetFilePath returns false, if we are still writing to file.
130 base::FilePath net_export_file_path;
131 EXPECT_FALSE(net_log_temp_file_->GetFilePath(&net_export_file_path));
133 VerifyNetExportLog();
136 // Make sure the export file has been successfully initialized.
137 void VerifyFileAndStateAfterDoStop() {
138 EXPECT_EQ("ALLOW_START_SEND", GetStateString());
139 EXPECT_EQ(NetLogTempFile::STATE_ALLOW_START_SEND,
140 net_log_temp_file_->state());
142 base::FilePath net_export_file_path;
143 EXPECT_TRUE(net_log_temp_file_->GetFilePath(&net_export_file_path));
144 EXPECT_TRUE(base::PathExists(net_export_file_path));
145 EXPECT_EQ(net_export_log_, net_export_file_path);
147 VerifyNetExportLog();
150 scoped_ptr<ChromeNetLog> net_log_;
151 // |net_log_temp_file_| is initialized after |net_log_| so that it can stop
152 // obvserving on destruction.
153 scoped_ptr<TestNetLogTempFile> net_log_temp_file_;
154 base::FilePath net_export_log_;
156 private:
157 base::MessageLoop message_loop_;
158 content::TestBrowserThread file_user_blocking_thread_;
161 TEST_F(NetLogTempFileTest, EnsureInitFailure) {
162 net_log_temp_file_->set_lie_about_net_export_log_directory(true);
164 EXPECT_FALSE(net_log_temp_file_->EnsureInit());
165 VerifyFilePathAndStateAfterEnsureInitFailure();
167 net_log_temp_file_->ProcessCommand(NetLogTempFile::DO_START);
168 VerifyFilePathAndStateAfterEnsureInitFailure();
171 TEST_F(NetLogTempFileTest, EnsureInitAllowStart) {
172 net_log_temp_file_->set_lie_about_file_existence(true);
174 EXPECT_TRUE(net_log_temp_file_->EnsureInit());
175 VerifyFilePathAndStateAfterEnsureInit();
177 // Calling EnsureInit() second time should be a no-op.
178 EXPECT_TRUE(net_log_temp_file_->EnsureInit());
179 VerifyFilePathAndStateAfterEnsureInit();
182 TEST_F(NetLogTempFileTest, EnsureInitAllowStartOrSend) {
183 EXPECT_TRUE(net_log_temp_file_->EnsureInit());
185 EXPECT_EQ("ALLOW_START_SEND", GetStateString());
186 EXPECT_EQ(NetLogTempFile::STATE_ALLOW_START_SEND,
187 net_log_temp_file_->state());
188 EXPECT_EQ(net_export_log_, net_log_temp_file_->log_path_);
189 EXPECT_TRUE(base::PathExists(net_export_log_));
191 base::FilePath net_export_file_path;
192 EXPECT_TRUE(net_log_temp_file_->GetFilePath(&net_export_file_path));
193 EXPECT_TRUE(base::PathExists(net_export_file_path));
194 EXPECT_EQ(net_export_log_, net_export_file_path);
196 // GetFilePath should return false if NetExportLogExists() fails.
197 net_log_temp_file_->set_lie_about_file_existence(true);
198 EXPECT_FALSE(net_log_temp_file_->GetFilePath(&net_export_file_path));
201 TEST_F(NetLogTempFileTest, ProcessCommandDoStartAndStop) {
202 net_log_temp_file_->ProcessCommand(NetLogTempFile::DO_START);
203 VerifyFileAndStateAfterDoStart();
205 // Calling DO_START second time should be a no-op.
206 net_log_temp_file_->ProcessCommand(NetLogTempFile::DO_START);
207 VerifyFileAndStateAfterDoStart();
209 net_log_temp_file_->ProcessCommand(NetLogTempFile::DO_STOP);
210 VerifyFileAndStateAfterDoStop();
212 // Calling DO_STOP second time should be a no-op.
213 net_log_temp_file_->ProcessCommand(NetLogTempFile::DO_STOP);
214 VerifyFileAndStateAfterDoStop();
217 TEST_F(NetLogTempFileTest, DoStartClearsFile) {
218 // Verify file sizes after two consecutives start/stop are the same (even if
219 // we add some junk data in between).
220 net_log_temp_file_->ProcessCommand(NetLogTempFile::DO_START);
221 VerifyFileAndStateAfterDoStart();
223 int64 start_file_size;
224 EXPECT_TRUE(base::GetFileSize(net_export_log_, &start_file_size));
226 net_log_temp_file_->ProcessCommand(NetLogTempFile::DO_STOP);
227 VerifyFileAndStateAfterDoStop();
229 int64 stop_file_size;
230 EXPECT_TRUE(base::GetFileSize(net_export_log_, &stop_file_size));
231 EXPECT_GE(stop_file_size, start_file_size);
233 // Add some junk at the end of the file.
234 std::string junk_data("Hello");
235 EXPECT_GT(file_util::AppendToFile(
236 net_export_log_, junk_data.c_str(), junk_data.size()), 0);
238 int64 junk_file_size;
239 EXPECT_TRUE(base::GetFileSize(net_export_log_, &junk_file_size));
240 EXPECT_GT(junk_file_size, stop_file_size);
242 // Execute DO_START/DO_STOP commands and make sure the file is back to the
243 // size before addition of junk data.
244 net_log_temp_file_->ProcessCommand(NetLogTempFile::DO_START);
245 VerifyFileAndStateAfterDoStart();
247 int64 new_start_file_size;
248 EXPECT_TRUE(base::GetFileSize(net_export_log_, &new_start_file_size));
249 EXPECT_EQ(new_start_file_size, start_file_size);
251 net_log_temp_file_->ProcessCommand(NetLogTempFile::DO_STOP);
252 VerifyFileAndStateAfterDoStop();
254 int64 new_stop_file_size;
255 EXPECT_TRUE(base::GetFileSize(net_export_log_, &new_stop_file_size));
256 EXPECT_EQ(new_stop_file_size, stop_file_size);
259 TEST_F(NetLogTempFileTest, CheckAddEvent) {
260 // Add an event to |net_log_| and then test to make sure that, after we stop
261 // logging, the file is larger than the file created without that event.
262 net_log_temp_file_->ProcessCommand(NetLogTempFile::DO_START);
263 VerifyFileAndStateAfterDoStart();
265 // Get file size without the event.
266 net_log_temp_file_->ProcessCommand(NetLogTempFile::DO_STOP);
267 VerifyFileAndStateAfterDoStop();
269 int64 stop_file_size;
270 EXPECT_TRUE(base::GetFileSize(net_export_log_, &stop_file_size));
272 // Perform DO_START and add an Event and then DO_STOP and then compare
273 // file sizes.
274 net_log_temp_file_->ProcessCommand(NetLogTempFile::DO_START);
275 VerifyFileAndStateAfterDoStart();
277 // Log an event.
278 net_log_->AddGlobalEntry(net::NetLog::TYPE_CANCELLED);
280 net_log_temp_file_->ProcessCommand(NetLogTempFile::DO_STOP);
281 VerifyFileAndStateAfterDoStop();
283 int64 new_stop_file_size;
284 EXPECT_TRUE(base::GetFileSize(net_export_log_, &new_stop_file_size));
285 EXPECT_GE(new_stop_file_size, stop_file_size);