Add new certificateProvider extension API.
[chromium-blink-merge.git] / chrome / browser / net / net_log_temp_file_unittest.cc
blob42f03870b965d5eeb066f84e70f1cdcdf8edf6f1
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/files/file_path.h"
9 #include "base/files/file_util.h"
10 #include "base/files/scoped_file.h"
11 #include "base/files/scoped_temp_dir.h"
12 #include "base/json/json_reader.h"
13 #include "base/memory/scoped_ptr.h"
14 #include "base/message_loop/message_loop.h"
15 #include "base/values.h"
16 #include "build/build_config.h"
17 #include "chrome/browser/net/chrome_net_log.h"
18 #include "content/public/test/test_browser_thread.h"
19 #include "net/log/write_to_file_net_log_observer.h"
20 #include "testing/gtest/include/gtest/gtest.h"
22 using content::BrowserThread;
24 class TestNetLogTempFile : public NetLogTempFile {
25 public:
26 explicit TestNetLogTempFile(ChromeNetLog* chrome_net_log)
27 : NetLogTempFile(chrome_net_log),
28 lie_about_net_export_log_directory_(false) {
29 EXPECT_TRUE(net_log_temp_dir_.CreateUniqueTempDir());
32 ~TestNetLogTempFile() override {
33 EXPECT_TRUE(net_log_temp_dir_.Delete());
36 // NetLogTempFile implementation:
37 bool GetNetExportLogBaseDirectory(base::FilePath* path) const override {
38 if (lie_about_net_export_log_directory_)
39 return false;
40 *path = net_log_temp_dir_.path();
41 return true;
44 void set_lie_about_net_export_log_directory(
45 bool lie_about_net_export_log_directory) {
46 lie_about_net_export_log_directory_ = lie_about_net_export_log_directory;
49 private:
50 bool lie_about_net_export_log_directory_;
52 base::ScopedTempDir net_log_temp_dir_;
55 class NetLogTempFileTest : public ::testing::Test {
56 public:
57 NetLogTempFileTest()
58 : net_log_(new ChromeNetLog),
59 net_log_temp_file_(new TestNetLogTempFile(net_log_.get())),
60 file_user_blocking_thread_(BrowserThread::FILE_USER_BLOCKING,
61 &message_loop_) {
62 EXPECT_TRUE(net_log_temp_file_->SetUpNetExportLogPath());
63 net_export_log_ = net_log_temp_file_->log_path_;
66 std::string GetStateString() const {
67 scoped_ptr<base::DictionaryValue> dict(net_log_temp_file_->GetState());
68 std::string state;
69 EXPECT_TRUE(dict->GetString("state", &state));
70 return state;
73 std::string GetLogTypeString() const {
74 scoped_ptr<base::DictionaryValue> dict(net_log_temp_file_->GetState());
75 std::string log_type;
76 EXPECT_TRUE(dict->GetString("logType", &log_type));
77 return log_type;
80 // Make sure the export file has been created and is non-empty, as net
81 // constants will always be written to it on creation.
82 void VerifyNetExportLogExists() const {
83 EXPECT_EQ(net_export_log_, net_log_temp_file_->log_path_);
84 ASSERT_TRUE(base::PathExists(net_export_log_));
86 int64 file_size;
87 // base::GetFileSize returns proper file size on open handles.
88 ASSERT_TRUE(base::GetFileSize(net_export_log_, &file_size));
89 EXPECT_GT(file_size, 0);
92 // Make sure the export file has been created and a valid JSON file. This
93 // should always be the case once logging has been stopped.
94 void VerifyNetExportLogComplete() const {
95 VerifyNetExportLogExists();
97 std::string log;
98 ASSERT_TRUE(ReadFileToString(net_export_log_, &log));
99 base::JSONReader reader;
100 scoped_ptr<base::Value> json = base::JSONReader::Read(log);
101 EXPECT_TRUE(json);
104 // Verify state and GetFilePath return correct values if EnsureInit() fails.
105 void VerifyFilePathAndStateAfterEnsureInitFailure() {
106 EXPECT_EQ("UNINITIALIZED", GetStateString());
107 EXPECT_EQ(NetLogTempFile::STATE_UNINITIALIZED,
108 net_log_temp_file_->state());
110 base::FilePath net_export_file_path;
111 EXPECT_FALSE(net_log_temp_file_->GetFilePath(&net_export_file_path));
114 // When we lie in NetExportLogExists, make sure state and GetFilePath return
115 // correct values.
116 void VerifyFilePathAndStateAfterEnsureInit() {
117 EXPECT_EQ("NOT_LOGGING", GetStateString());
118 EXPECT_EQ(NetLogTempFile::STATE_NOT_LOGGING, net_log_temp_file_->state());
119 EXPECT_EQ("NONE", GetLogTypeString());
120 EXPECT_EQ(NetLogTempFile::LOG_TYPE_NONE, net_log_temp_file_->log_type());
122 base::FilePath net_export_file_path;
123 EXPECT_FALSE(net_log_temp_file_->GetFilePath(&net_export_file_path));
124 EXPECT_FALSE(net_log_temp_file_->NetExportLogExists());
127 // The following methods make sure the export file has been successfully
128 // initialized by a DO_START command of the given type.
130 void VerifyFileAndStateAfterDoStart() {
131 VerifyFileAndStateAfterStart(
132 NetLogTempFile::LOG_TYPE_NORMAL, "NORMAL",
133 net::NetLogCaptureMode::IncludeCookiesAndCredentials());
136 void VerifyFileAndStateAfterDoStartStripPrivateData() const {
137 VerifyFileAndStateAfterStart(NetLogTempFile::LOG_TYPE_STRIP_PRIVATE_DATA,
138 "STRIP_PRIVATE_DATA",
139 net::NetLogCaptureMode::Default());
142 void VerifyFileAndStateAfterDoStartLogBytes() const {
143 VerifyFileAndStateAfterStart(NetLogTempFile::LOG_TYPE_LOG_BYTES,
144 "LOG_BYTES",
145 net::NetLogCaptureMode::IncludeSocketBytes());
148 // Make sure the export file has been successfully initialized after DO_STOP
149 // command following a DO_START command of the given type.
151 void VerifyFileAndStateAfterDoStop() const {
152 VerifyFileAndStateAfterDoStop(NetLogTempFile::LOG_TYPE_NORMAL, "NORMAL");
155 void VerifyFileAndStateAfterDoStopWithStripPrivateData() const {
156 VerifyFileAndStateAfterDoStop(NetLogTempFile::LOG_TYPE_STRIP_PRIVATE_DATA,
157 "STRIP_PRIVATE_DATA");
160 void VerifyFileAndStateAfterDoStopWithLogBytes() const {
161 VerifyFileAndStateAfterDoStop(NetLogTempFile::LOG_TYPE_LOG_BYTES,
162 "LOG_BYTES");
165 scoped_ptr<ChromeNetLog> net_log_;
166 // |net_log_temp_file_| is initialized after |net_log_| so that it can stop
167 // obvserving on destruction.
168 scoped_ptr<TestNetLogTempFile> net_log_temp_file_;
169 base::FilePath net_export_log_;
171 private:
172 // Checks state after one of the DO_START* commands.
173 void VerifyFileAndStateAfterStart(
174 NetLogTempFile::LogType expected_log_type,
175 const std::string& expected_log_type_string,
176 net::NetLogCaptureMode expected_capture_mode) const {
177 EXPECT_EQ(NetLogTempFile::STATE_LOGGING, net_log_temp_file_->state());
178 EXPECT_EQ("LOGGING", GetStateString());
179 EXPECT_EQ(expected_log_type, net_log_temp_file_->log_type());
180 EXPECT_EQ(expected_log_type_string, GetLogTypeString());
181 EXPECT_EQ(expected_capture_mode,
182 net_log_temp_file_->write_to_file_observer_->capture_mode());
184 // Check GetFilePath returns false when still writing to the file.
185 base::FilePath net_export_file_path;
186 EXPECT_FALSE(net_log_temp_file_->GetFilePath(&net_export_file_path));
188 VerifyNetExportLogExists();
191 void VerifyFileAndStateAfterDoStop(
192 NetLogTempFile::LogType expected_log_type,
193 const std::string& expected_log_type_string) const {
194 EXPECT_EQ(NetLogTempFile::STATE_NOT_LOGGING, net_log_temp_file_->state());
195 EXPECT_EQ("NOT_LOGGING", GetStateString());
196 EXPECT_EQ(expected_log_type, net_log_temp_file_->log_type());
197 EXPECT_EQ(expected_log_type_string, GetLogTypeString());
199 base::FilePath net_export_file_path;
200 EXPECT_TRUE(net_log_temp_file_->GetFilePath(&net_export_file_path));
201 EXPECT_EQ(net_export_log_, net_export_file_path);
203 VerifyNetExportLogComplete();
206 base::MessageLoop message_loop_;
207 content::TestBrowserThread file_user_blocking_thread_;
210 TEST_F(NetLogTempFileTest, EnsureInitFailure) {
211 net_log_temp_file_->set_lie_about_net_export_log_directory(true);
213 EXPECT_FALSE(net_log_temp_file_->EnsureInit());
214 VerifyFilePathAndStateAfterEnsureInitFailure();
216 net_log_temp_file_->ProcessCommand(NetLogTempFile::DO_START);
217 VerifyFilePathAndStateAfterEnsureInitFailure();
220 TEST_F(NetLogTempFileTest, EnsureInitAllowStart) {
221 EXPECT_TRUE(net_log_temp_file_->EnsureInit());
222 VerifyFilePathAndStateAfterEnsureInit();
224 // Calling EnsureInit() second time should be a no-op.
225 EXPECT_TRUE(net_log_temp_file_->EnsureInit());
226 VerifyFilePathAndStateAfterEnsureInit();
228 // GetFilePath() should failed when there's no temp file.
229 base::FilePath net_export_file_path;
230 EXPECT_FALSE(net_log_temp_file_->GetFilePath(&net_export_file_path));
233 TEST_F(NetLogTempFileTest, EnsureInitAllowStartOrSend) {
234 // Create and close an empty log file, to simulate an old log file already
235 // existing.
236 base::ScopedFILE created_file(base::OpenFile(net_export_log_, "w"));
237 ASSERT_TRUE(created_file.get());
238 created_file.reset();
240 EXPECT_TRUE(net_log_temp_file_->EnsureInit());
242 EXPECT_EQ("NOT_LOGGING", GetStateString());
243 EXPECT_EQ(NetLogTempFile::STATE_NOT_LOGGING, net_log_temp_file_->state());
244 EXPECT_EQ("UNKNOWN", GetLogTypeString());
245 EXPECT_EQ(NetLogTempFile::LOG_TYPE_UNKNOWN, net_log_temp_file_->log_type());
246 EXPECT_EQ(net_export_log_, net_log_temp_file_->log_path_);
247 EXPECT_TRUE(base::PathExists(net_export_log_));
249 base::FilePath net_export_file_path;
250 EXPECT_TRUE(net_log_temp_file_->GetFilePath(&net_export_file_path));
251 EXPECT_TRUE(base::PathExists(net_export_file_path));
252 EXPECT_EQ(net_export_log_, net_export_file_path);
255 TEST_F(NetLogTempFileTest, ProcessCommandDoStartAndStop) {
256 net_log_temp_file_->ProcessCommand(NetLogTempFile::DO_START);
257 VerifyFileAndStateAfterDoStart();
259 // Calling a second time should be a no-op.
260 net_log_temp_file_->ProcessCommand(NetLogTempFile::DO_START);
261 VerifyFileAndStateAfterDoStart();
263 // starting with other log levels should also be no-ops.
264 net_log_temp_file_->ProcessCommand(
265 NetLogTempFile::DO_START_STRIP_PRIVATE_DATA);
266 VerifyFileAndStateAfterDoStart();
267 net_log_temp_file_->ProcessCommand(
268 NetLogTempFile::DO_START_LOG_BYTES);
269 VerifyFileAndStateAfterDoStart();
271 net_log_temp_file_->ProcessCommand(NetLogTempFile::DO_STOP);
272 VerifyFileAndStateAfterDoStop();
274 // Calling DO_STOP second time should be a no-op.
275 net_log_temp_file_->ProcessCommand(NetLogTempFile::DO_STOP);
276 VerifyFileAndStateAfterDoStop();
279 TEST_F(NetLogTempFileTest,
280 ProcessCommandDoStartAndStopWithPrivateDataStripping) {
281 net_log_temp_file_->ProcessCommand(
282 NetLogTempFile::DO_START_STRIP_PRIVATE_DATA);
283 VerifyFileAndStateAfterDoStartStripPrivateData();
285 // Calling a second time should be a no-op.
286 net_log_temp_file_->ProcessCommand(
287 NetLogTempFile::DO_START_STRIP_PRIVATE_DATA);
288 VerifyFileAndStateAfterDoStartStripPrivateData();
290 net_log_temp_file_->ProcessCommand(NetLogTempFile::DO_STOP);
291 VerifyFileAndStateAfterDoStopWithStripPrivateData();
293 // Calling DO_STOP second time should be a no-op.
294 net_log_temp_file_->ProcessCommand(NetLogTempFile::DO_STOP);
295 VerifyFileAndStateAfterDoStopWithStripPrivateData();
298 TEST_F(NetLogTempFileTest,
299 ProcessCommandDoStartAndStopWithByteLogging) {
300 net_log_temp_file_->ProcessCommand(NetLogTempFile::DO_START_LOG_BYTES);
301 VerifyFileAndStateAfterDoStartLogBytes();
303 // Calling a second time should be a no-op.
304 net_log_temp_file_->ProcessCommand(NetLogTempFile::DO_START_LOG_BYTES);
305 VerifyFileAndStateAfterDoStartLogBytes();
307 net_log_temp_file_->ProcessCommand(NetLogTempFile::DO_STOP);
308 VerifyFileAndStateAfterDoStopWithLogBytes();
310 // Calling DO_STOP second time should be a no-op.
311 net_log_temp_file_->ProcessCommand(NetLogTempFile::DO_STOP);
312 VerifyFileAndStateAfterDoStopWithLogBytes();
315 TEST_F(NetLogTempFileTest, DoStartClearsFile) {
316 // Verify file sizes after two consecutive starts/stops are the same (even if
317 // we add some junk data in between).
318 net_log_temp_file_->ProcessCommand(NetLogTempFile::DO_START);
319 VerifyFileAndStateAfterDoStart();
321 int64 start_file_size;
322 EXPECT_TRUE(base::GetFileSize(net_export_log_, &start_file_size));
324 net_log_temp_file_->ProcessCommand(NetLogTempFile::DO_STOP);
325 VerifyFileAndStateAfterDoStop();
327 int64 stop_file_size;
328 EXPECT_TRUE(base::GetFileSize(net_export_log_, &stop_file_size));
329 EXPECT_GE(stop_file_size, start_file_size);
331 // Add some junk at the end of the file.
332 std::string junk_data("Hello");
333 EXPECT_TRUE(base::AppendToFile(net_export_log_, junk_data.c_str(),
334 junk_data.size()));
336 int64 junk_file_size;
337 EXPECT_TRUE(base::GetFileSize(net_export_log_, &junk_file_size));
338 EXPECT_GT(junk_file_size, stop_file_size);
340 // Execute DO_START/DO_STOP commands and make sure the file is back to the
341 // size before addition of junk data.
342 net_log_temp_file_->ProcessCommand(NetLogTempFile::DO_START);
343 VerifyFileAndStateAfterDoStart();
345 int64 new_start_file_size;
346 EXPECT_TRUE(base::GetFileSize(net_export_log_, &new_start_file_size));
347 EXPECT_EQ(new_start_file_size, start_file_size);
349 net_log_temp_file_->ProcessCommand(NetLogTempFile::DO_STOP);
350 VerifyFileAndStateAfterDoStop();
352 int64 new_stop_file_size;
353 EXPECT_TRUE(base::GetFileSize(net_export_log_, &new_stop_file_size));
354 EXPECT_EQ(new_stop_file_size, stop_file_size);
357 TEST_F(NetLogTempFileTest, CheckAddEvent) {
358 // Add an event to |net_log_| and then test to make sure that, after we stop
359 // logging, the file is larger than the file created without that event.
360 net_log_temp_file_->ProcessCommand(NetLogTempFile::DO_START);
361 VerifyFileAndStateAfterDoStart();
363 // Get file size without the event.
364 net_log_temp_file_->ProcessCommand(NetLogTempFile::DO_STOP);
365 VerifyFileAndStateAfterDoStop();
367 int64 stop_file_size;
368 EXPECT_TRUE(base::GetFileSize(net_export_log_, &stop_file_size));
370 // Perform DO_START and add an Event and then DO_STOP and then compare
371 // file sizes.
372 net_log_temp_file_->ProcessCommand(NetLogTempFile::DO_START);
373 VerifyFileAndStateAfterDoStart();
375 // Log an event.
376 net_log_->AddGlobalEntry(net::NetLog::TYPE_CANCELLED);
378 net_log_temp_file_->ProcessCommand(NetLogTempFile::DO_STOP);
379 VerifyFileAndStateAfterDoStop();
381 int64 new_stop_file_size;
382 EXPECT_TRUE(base::GetFileSize(net_export_log_, &new_stop_file_size));
383 EXPECT_GE(new_stop_file_size, stop_file_size);