Linux: Depend on liberation-fonts package for RPMs.
[chromium-blink-merge.git] / components / net_log / net_log_temp_file_unittest.cc
blob618c8e4fb40a7049f1432ebd0ce150233f9233fb
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 "components/net_log/net_log_temp_file.h"
7 #include "base/basictypes.h"
8 #include "base/command_line.h"
9 #include "base/files/file_path.h"
10 #include "base/files/file_util.h"
11 #include "base/files/scoped_file.h"
12 #include "base/files/scoped_temp_dir.h"
13 #include "base/json/json_reader.h"
14 #include "base/memory/scoped_ptr.h"
15 #include "base/message_loop/message_loop.h"
16 #include "base/values.h"
17 #include "build/build_config.h"
18 #include "components/net_log/chrome_net_log.h"
19 #include "net/log/net_log_capture_mode.h"
20 #include "net/log/write_to_file_net_log_observer.h"
21 #include "testing/gtest/include/gtest/gtest.h"
23 namespace {
25 const char kChannelString[] = "SomeChannel";
27 } // namespace
29 namespace net_log {
31 class TestNetLogTempFile : public NetLogTempFile {
32 public:
33 explicit TestNetLogTempFile(ChromeNetLog* chrome_net_log)
34 : NetLogTempFile(
35 chrome_net_log,
36 base::CommandLine::ForCurrentProcess()->GetCommandLineString(),
37 kChannelString),
38 lie_about_net_export_log_directory_(false) {
39 EXPECT_TRUE(net_log_temp_dir_.CreateUniqueTempDir());
42 ~TestNetLogTempFile() override { EXPECT_TRUE(net_log_temp_dir_.Delete()); }
44 // NetLogTempFile implementation:
45 bool GetNetExportLogBaseDirectory(base::FilePath* path) const override {
46 if (lie_about_net_export_log_directory_)
47 return false;
48 *path = net_log_temp_dir_.path();
49 return true;
52 void set_lie_about_net_export_log_directory(
53 bool lie_about_net_export_log_directory) {
54 lie_about_net_export_log_directory_ = lie_about_net_export_log_directory;
57 private:
58 bool lie_about_net_export_log_directory_;
60 base::ScopedTempDir net_log_temp_dir_;
63 class NetLogTempFileTest : public ::testing::Test {
64 public:
65 NetLogTempFileTest()
66 : net_log_(new ChromeNetLog(
67 base::FilePath(),
68 net::NetLogCaptureMode::Default(),
69 base::CommandLine::ForCurrentProcess()->GetCommandLineString(),
70 kChannelString)),
71 net_log_temp_file_(new TestNetLogTempFile(net_log_.get())) {
72 EXPECT_TRUE(net_log_temp_file_->SetUpNetExportLogPath());
73 net_export_log_ = net_log_temp_file_->log_path_;
76 std::string GetStateString() const {
77 scoped_ptr<base::DictionaryValue> dict(net_log_temp_file_->GetState());
78 std::string state;
79 EXPECT_TRUE(dict->GetString("state", &state));
80 return state;
83 std::string GetLogTypeString() const {
84 scoped_ptr<base::DictionaryValue> dict(net_log_temp_file_->GetState());
85 std::string log_type;
86 EXPECT_TRUE(dict->GetString("logType", &log_type));
87 return log_type;
90 // Make sure the export file has been created and is non-empty, as net
91 // constants will always be written to it on creation.
92 void VerifyNetExportLogExists() const {
93 EXPECT_EQ(net_export_log_, net_log_temp_file_->log_path_);
94 ASSERT_TRUE(base::PathExists(net_export_log_));
96 int64 file_size;
97 // base::GetFileSize returns proper file size on open handles.
98 ASSERT_TRUE(base::GetFileSize(net_export_log_, &file_size));
99 EXPECT_GT(file_size, 0);
102 // Make sure the export file has been created and a valid JSON file. This
103 // should always be the case once logging has been stopped.
104 void VerifyNetExportLogComplete() const {
105 VerifyNetExportLogExists();
107 std::string log;
108 ASSERT_TRUE(ReadFileToString(net_export_log_, &log));
109 base::JSONReader reader;
110 scoped_ptr<base::Value> json = base::JSONReader::Read(log);
111 EXPECT_TRUE(json);
114 // Verify state and GetFilePath return correct values if EnsureInit() fails.
115 void VerifyFilePathAndStateAfterEnsureInitFailure() {
116 EXPECT_EQ("UNINITIALIZED", GetStateString());
117 EXPECT_EQ(NetLogTempFile::STATE_UNINITIALIZED, net_log_temp_file_->state());
119 base::FilePath net_export_file_path;
120 EXPECT_FALSE(net_log_temp_file_->GetFilePath(&net_export_file_path));
123 // When we lie in NetExportLogExists, make sure state and GetFilePath return
124 // correct values.
125 void VerifyFilePathAndStateAfterEnsureInit() {
126 EXPECT_EQ("NOT_LOGGING", GetStateString());
127 EXPECT_EQ(NetLogTempFile::STATE_NOT_LOGGING, net_log_temp_file_->state());
128 EXPECT_EQ("NONE", GetLogTypeString());
129 EXPECT_EQ(NetLogTempFile::LOG_TYPE_NONE, net_log_temp_file_->log_type());
131 base::FilePath net_export_file_path;
132 EXPECT_FALSE(net_log_temp_file_->GetFilePath(&net_export_file_path));
133 EXPECT_FALSE(net_log_temp_file_->NetExportLogExists());
136 // The following methods make sure the export file has been successfully
137 // initialized by a DO_START command of the given type.
139 void VerifyFileAndStateAfterDoStart() {
140 VerifyFileAndStateAfterStart(
141 NetLogTempFile::LOG_TYPE_NORMAL, "NORMAL",
142 net::NetLogCaptureMode::IncludeCookiesAndCredentials());
145 void VerifyFileAndStateAfterDoStartStripPrivateData() const {
146 VerifyFileAndStateAfterStart(NetLogTempFile::LOG_TYPE_STRIP_PRIVATE_DATA,
147 "STRIP_PRIVATE_DATA",
148 net::NetLogCaptureMode::Default());
151 void VerifyFileAndStateAfterDoStartLogBytes() const {
152 VerifyFileAndStateAfterStart(NetLogTempFile::LOG_TYPE_LOG_BYTES,
153 "LOG_BYTES",
154 net::NetLogCaptureMode::IncludeSocketBytes());
157 // Make sure the export file has been successfully initialized after DO_STOP
158 // command following a DO_START command of the given type.
160 void VerifyFileAndStateAfterDoStop() const {
161 VerifyFileAndStateAfterDoStop(NetLogTempFile::LOG_TYPE_NORMAL, "NORMAL");
164 void VerifyFileAndStateAfterDoStopWithStripPrivateData() const {
165 VerifyFileAndStateAfterDoStop(NetLogTempFile::LOG_TYPE_STRIP_PRIVATE_DATA,
166 "STRIP_PRIVATE_DATA");
169 void VerifyFileAndStateAfterDoStopWithLogBytes() const {
170 VerifyFileAndStateAfterDoStop(NetLogTempFile::LOG_TYPE_LOG_BYTES,
171 "LOG_BYTES");
174 scoped_ptr<ChromeNetLog> net_log_;
175 // |net_log_temp_file_| is initialized after |net_log_| so that it can stop
176 // obvserving on destruction.
177 scoped_ptr<TestNetLogTempFile> net_log_temp_file_;
178 base::FilePath net_export_log_;
180 private:
181 // Checks state after one of the DO_START* commands.
182 void VerifyFileAndStateAfterStart(
183 NetLogTempFile::LogType expected_log_type,
184 const std::string& expected_log_type_string,
185 net::NetLogCaptureMode expected_capture_mode) const {
186 EXPECT_EQ(NetLogTempFile::STATE_LOGGING, net_log_temp_file_->state());
187 EXPECT_EQ("LOGGING", GetStateString());
188 EXPECT_EQ(expected_log_type, net_log_temp_file_->log_type());
189 EXPECT_EQ(expected_log_type_string, GetLogTypeString());
190 EXPECT_EQ(expected_capture_mode,
191 net_log_temp_file_->write_to_file_observer_->capture_mode());
193 // Check GetFilePath returns false when still writing to the file.
194 base::FilePath net_export_file_path;
195 EXPECT_FALSE(net_log_temp_file_->GetFilePath(&net_export_file_path));
197 VerifyNetExportLogExists();
200 void VerifyFileAndStateAfterDoStop(
201 NetLogTempFile::LogType expected_log_type,
202 const std::string& expected_log_type_string) const {
203 EXPECT_EQ(NetLogTempFile::STATE_NOT_LOGGING, net_log_temp_file_->state());
204 EXPECT_EQ("NOT_LOGGING", GetStateString());
205 EXPECT_EQ(expected_log_type, net_log_temp_file_->log_type());
206 EXPECT_EQ(expected_log_type_string, GetLogTypeString());
208 base::FilePath net_export_file_path;
209 EXPECT_TRUE(net_log_temp_file_->GetFilePath(&net_export_file_path));
210 EXPECT_EQ(net_export_log_, net_export_file_path);
212 VerifyNetExportLogComplete();
215 base::MessageLoop message_loop_;
218 TEST_F(NetLogTempFileTest, EnsureInitFailure) {
219 net_log_temp_file_->set_lie_about_net_export_log_directory(true);
221 EXPECT_FALSE(net_log_temp_file_->EnsureInit());
222 VerifyFilePathAndStateAfterEnsureInitFailure();
224 net_log_temp_file_->ProcessCommand(NetLogTempFile::DO_START);
225 VerifyFilePathAndStateAfterEnsureInitFailure();
228 TEST_F(NetLogTempFileTest, EnsureInitAllowStart) {
229 EXPECT_TRUE(net_log_temp_file_->EnsureInit());
230 VerifyFilePathAndStateAfterEnsureInit();
232 // Calling EnsureInit() second time should be a no-op.
233 EXPECT_TRUE(net_log_temp_file_->EnsureInit());
234 VerifyFilePathAndStateAfterEnsureInit();
236 // GetFilePath() should failed when there's no temp file.
237 base::FilePath net_export_file_path;
238 EXPECT_FALSE(net_log_temp_file_->GetFilePath(&net_export_file_path));
241 TEST_F(NetLogTempFileTest, EnsureInitAllowStartOrSend) {
242 // Create and close an empty log file, to simulate an old log file already
243 // existing.
244 base::ScopedFILE created_file(base::OpenFile(net_export_log_, "w"));
245 ASSERT_TRUE(created_file.get());
246 created_file.reset();
248 EXPECT_TRUE(net_log_temp_file_->EnsureInit());
250 EXPECT_EQ("NOT_LOGGING", GetStateString());
251 EXPECT_EQ(NetLogTempFile::STATE_NOT_LOGGING, net_log_temp_file_->state());
252 EXPECT_EQ("UNKNOWN", GetLogTypeString());
253 EXPECT_EQ(NetLogTempFile::LOG_TYPE_UNKNOWN, net_log_temp_file_->log_type());
254 EXPECT_EQ(net_export_log_, net_log_temp_file_->log_path_);
255 EXPECT_TRUE(base::PathExists(net_export_log_));
257 base::FilePath net_export_file_path;
258 EXPECT_TRUE(net_log_temp_file_->GetFilePath(&net_export_file_path));
259 EXPECT_TRUE(base::PathExists(net_export_file_path));
260 EXPECT_EQ(net_export_log_, net_export_file_path);
263 TEST_F(NetLogTempFileTest, ProcessCommandDoStartAndStop) {
264 net_log_temp_file_->ProcessCommand(NetLogTempFile::DO_START);
265 VerifyFileAndStateAfterDoStart();
267 // Calling a second time should be a no-op.
268 net_log_temp_file_->ProcessCommand(NetLogTempFile::DO_START);
269 VerifyFileAndStateAfterDoStart();
271 // starting with other log levels should also be no-ops.
272 net_log_temp_file_->ProcessCommand(
273 NetLogTempFile::DO_START_STRIP_PRIVATE_DATA);
274 VerifyFileAndStateAfterDoStart();
275 net_log_temp_file_->ProcessCommand(NetLogTempFile::DO_START_LOG_BYTES);
276 VerifyFileAndStateAfterDoStart();
278 net_log_temp_file_->ProcessCommand(NetLogTempFile::DO_STOP);
279 VerifyFileAndStateAfterDoStop();
281 // Calling DO_STOP second time should be a no-op.
282 net_log_temp_file_->ProcessCommand(NetLogTempFile::DO_STOP);
283 VerifyFileAndStateAfterDoStop();
286 TEST_F(NetLogTempFileTest,
287 ProcessCommandDoStartAndStopWithPrivateDataStripping) {
288 net_log_temp_file_->ProcessCommand(
289 NetLogTempFile::DO_START_STRIP_PRIVATE_DATA);
290 VerifyFileAndStateAfterDoStartStripPrivateData();
292 // Calling a second time should be a no-op.
293 net_log_temp_file_->ProcessCommand(
294 NetLogTempFile::DO_START_STRIP_PRIVATE_DATA);
295 VerifyFileAndStateAfterDoStartStripPrivateData();
297 net_log_temp_file_->ProcessCommand(NetLogTempFile::DO_STOP);
298 VerifyFileAndStateAfterDoStopWithStripPrivateData();
300 // Calling DO_STOP second time should be a no-op.
301 net_log_temp_file_->ProcessCommand(NetLogTempFile::DO_STOP);
302 VerifyFileAndStateAfterDoStopWithStripPrivateData();
305 TEST_F(NetLogTempFileTest, ProcessCommandDoStartAndStopWithByteLogging) {
306 net_log_temp_file_->ProcessCommand(NetLogTempFile::DO_START_LOG_BYTES);
307 VerifyFileAndStateAfterDoStartLogBytes();
309 // Calling a second time should be a no-op.
310 net_log_temp_file_->ProcessCommand(NetLogTempFile::DO_START_LOG_BYTES);
311 VerifyFileAndStateAfterDoStartLogBytes();
313 net_log_temp_file_->ProcessCommand(NetLogTempFile::DO_STOP);
314 VerifyFileAndStateAfterDoStopWithLogBytes();
316 // Calling DO_STOP second time should be a no-op.
317 net_log_temp_file_->ProcessCommand(NetLogTempFile::DO_STOP);
318 VerifyFileAndStateAfterDoStopWithLogBytes();
321 TEST_F(NetLogTempFileTest, DoStartClearsFile) {
322 // Verify file sizes after two consecutive starts/stops are the same (even if
323 // we add some junk data in between).
324 net_log_temp_file_->ProcessCommand(NetLogTempFile::DO_START);
325 VerifyFileAndStateAfterDoStart();
327 int64 start_file_size;
328 EXPECT_TRUE(base::GetFileSize(net_export_log_, &start_file_size));
330 net_log_temp_file_->ProcessCommand(NetLogTempFile::DO_STOP);
331 VerifyFileAndStateAfterDoStop();
333 int64 stop_file_size;
334 EXPECT_TRUE(base::GetFileSize(net_export_log_, &stop_file_size));
335 EXPECT_GE(stop_file_size, start_file_size);
337 // Add some junk at the end of the file.
338 std::string junk_data("Hello");
339 EXPECT_TRUE(
340 base::AppendToFile(net_export_log_, junk_data.c_str(), junk_data.size()));
342 int64 junk_file_size;
343 EXPECT_TRUE(base::GetFileSize(net_export_log_, &junk_file_size));
344 EXPECT_GT(junk_file_size, stop_file_size);
346 // Execute DO_START/DO_STOP commands and make sure the file is back to the
347 // size before addition of junk data.
348 net_log_temp_file_->ProcessCommand(NetLogTempFile::DO_START);
349 VerifyFileAndStateAfterDoStart();
351 int64 new_start_file_size;
352 EXPECT_TRUE(base::GetFileSize(net_export_log_, &new_start_file_size));
353 EXPECT_EQ(new_start_file_size, start_file_size);
355 net_log_temp_file_->ProcessCommand(NetLogTempFile::DO_STOP);
356 VerifyFileAndStateAfterDoStop();
358 int64 new_stop_file_size;
359 EXPECT_TRUE(base::GetFileSize(net_export_log_, &new_stop_file_size));
360 EXPECT_EQ(new_stop_file_size, stop_file_size);
363 TEST_F(NetLogTempFileTest, CheckAddEvent) {
364 // Add an event to |net_log_| and then test to make sure that, after we stop
365 // logging, the file is larger than the file created without that event.
366 net_log_temp_file_->ProcessCommand(NetLogTempFile::DO_START);
367 VerifyFileAndStateAfterDoStart();
369 // Get file size without the event.
370 net_log_temp_file_->ProcessCommand(NetLogTempFile::DO_STOP);
371 VerifyFileAndStateAfterDoStop();
373 int64 stop_file_size;
374 EXPECT_TRUE(base::GetFileSize(net_export_log_, &stop_file_size));
376 // Perform DO_START and add an Event and then DO_STOP and then compare
377 // file sizes.
378 net_log_temp_file_->ProcessCommand(NetLogTempFile::DO_START);
379 VerifyFileAndStateAfterDoStart();
381 // Log an event.
382 net_log_->AddGlobalEntry(net::NetLog::TYPE_CANCELLED);
384 net_log_temp_file_->ProcessCommand(NetLogTempFile::DO_STOP);
385 VerifyFileAndStateAfterDoStop();
387 int64 new_stop_file_size;
388 EXPECT_TRUE(base::GetFileSize(net_export_log_, &new_stop_file_size));
389 EXPECT_GE(new_stop_file_size, stop_file_size);
392 } // namespace net_log