Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(68)

Side by Side Diff: chrome/browser/net/net_log_temp_file_unittest.cc

Issue 1347043002: Move ChromeNetLog to //components (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Rebase Created 5 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « chrome/browser/net/net_log_temp_file.cc ('k') | chrome/browser/net/predictor_browsertest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
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.
4
5 #include "chrome/browser/net/net_log_temp_file.h"
6
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"
21
22 using content::BrowserThread;
23
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());
30 }
31
32 ~TestNetLogTempFile() override {
33 EXPECT_TRUE(net_log_temp_dir_.Delete());
34 }
35
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;
42 }
43
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;
47 }
48
49 private:
50 bool lie_about_net_export_log_directory_;
51
52 base::ScopedTempDir net_log_temp_dir_;
53 };
54
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_;
64 }
65
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;
71 }
72
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;
78 }
79
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_));
85
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);
90 }
91
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();
96
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);
102 }
103
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());
109
110 base::FilePath net_export_file_path;
111 EXPECT_FALSE(net_log_temp_file_->GetFilePath(&net_export_file_path));
112 }
113
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());
121
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());
125 }
126
127 // The following methods make sure the export file has been successfully
128 // initialized by a DO_START command of the given type.
129
130 void VerifyFileAndStateAfterDoStart() {
131 VerifyFileAndStateAfterStart(
132 NetLogTempFile::LOG_TYPE_NORMAL, "NORMAL",
133 net::NetLogCaptureMode::IncludeCookiesAndCredentials());
134 }
135
136 void VerifyFileAndStateAfterDoStartStripPrivateData() const {
137 VerifyFileAndStateAfterStart(NetLogTempFile::LOG_TYPE_STRIP_PRIVATE_DATA,
138 "STRIP_PRIVATE_DATA",
139 net::NetLogCaptureMode::Default());
140 }
141
142 void VerifyFileAndStateAfterDoStartLogBytes() const {
143 VerifyFileAndStateAfterStart(NetLogTempFile::LOG_TYPE_LOG_BYTES,
144 "LOG_BYTES",
145 net::NetLogCaptureMode::IncludeSocketBytes());
146 }
147
148 // Make sure the export file has been successfully initialized after DO_STOP
149 // command following a DO_START command of the given type.
150
151 void VerifyFileAndStateAfterDoStop() const {
152 VerifyFileAndStateAfterDoStop(NetLogTempFile::LOG_TYPE_NORMAL, "NORMAL");
153 }
154
155 void VerifyFileAndStateAfterDoStopWithStripPrivateData() const {
156 VerifyFileAndStateAfterDoStop(NetLogTempFile::LOG_TYPE_STRIP_PRIVATE_DATA,
157 "STRIP_PRIVATE_DATA");
158 }
159
160 void VerifyFileAndStateAfterDoStopWithLogBytes() const {
161 VerifyFileAndStateAfterDoStop(NetLogTempFile::LOG_TYPE_LOG_BYTES,
162 "LOG_BYTES");
163 }
164
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_;
170
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());
183
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));
187
188 VerifyNetExportLogExists();
189 }
190
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());
198
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);
202
203 VerifyNetExportLogComplete();
204 }
205
206 base::MessageLoop message_loop_;
207 content::TestBrowserThread file_user_blocking_thread_;
208 };
209
210 TEST_F(NetLogTempFileTest, EnsureInitFailure) {
211 net_log_temp_file_->set_lie_about_net_export_log_directory(true);
212
213 EXPECT_FALSE(net_log_temp_file_->EnsureInit());
214 VerifyFilePathAndStateAfterEnsureInitFailure();
215
216 net_log_temp_file_->ProcessCommand(NetLogTempFile::DO_START);
217 VerifyFilePathAndStateAfterEnsureInitFailure();
218 }
219
220 TEST_F(NetLogTempFileTest, EnsureInitAllowStart) {
221 EXPECT_TRUE(net_log_temp_file_->EnsureInit());
222 VerifyFilePathAndStateAfterEnsureInit();
223
224 // Calling EnsureInit() second time should be a no-op.
225 EXPECT_TRUE(net_log_temp_file_->EnsureInit());
226 VerifyFilePathAndStateAfterEnsureInit();
227
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));
231 }
232
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();
239
240 EXPECT_TRUE(net_log_temp_file_->EnsureInit());
241
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_));
248
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);
253 }
254
255 TEST_F(NetLogTempFileTest, ProcessCommandDoStartAndStop) {
256 net_log_temp_file_->ProcessCommand(NetLogTempFile::DO_START);
257 VerifyFileAndStateAfterDoStart();
258
259 // Calling a second time should be a no-op.
260 net_log_temp_file_->ProcessCommand(NetLogTempFile::DO_START);
261 VerifyFileAndStateAfterDoStart();
262
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();
270
271 net_log_temp_file_->ProcessCommand(NetLogTempFile::DO_STOP);
272 VerifyFileAndStateAfterDoStop();
273
274 // Calling DO_STOP second time should be a no-op.
275 net_log_temp_file_->ProcessCommand(NetLogTempFile::DO_STOP);
276 VerifyFileAndStateAfterDoStop();
277 }
278
279 TEST_F(NetLogTempFileTest,
280 ProcessCommandDoStartAndStopWithPrivateDataStripping) {
281 net_log_temp_file_->ProcessCommand(
282 NetLogTempFile::DO_START_STRIP_PRIVATE_DATA);
283 VerifyFileAndStateAfterDoStartStripPrivateData();
284
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();
289
290 net_log_temp_file_->ProcessCommand(NetLogTempFile::DO_STOP);
291 VerifyFileAndStateAfterDoStopWithStripPrivateData();
292
293 // Calling DO_STOP second time should be a no-op.
294 net_log_temp_file_->ProcessCommand(NetLogTempFile::DO_STOP);
295 VerifyFileAndStateAfterDoStopWithStripPrivateData();
296 }
297
298 TEST_F(NetLogTempFileTest,
299 ProcessCommandDoStartAndStopWithByteLogging) {
300 net_log_temp_file_->ProcessCommand(NetLogTempFile::DO_START_LOG_BYTES);
301 VerifyFileAndStateAfterDoStartLogBytes();
302
303 // Calling a second time should be a no-op.
304 net_log_temp_file_->ProcessCommand(NetLogTempFile::DO_START_LOG_BYTES);
305 VerifyFileAndStateAfterDoStartLogBytes();
306
307 net_log_temp_file_->ProcessCommand(NetLogTempFile::DO_STOP);
308 VerifyFileAndStateAfterDoStopWithLogBytes();
309
310 // Calling DO_STOP second time should be a no-op.
311 net_log_temp_file_->ProcessCommand(NetLogTempFile::DO_STOP);
312 VerifyFileAndStateAfterDoStopWithLogBytes();
313 }
314
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();
320
321 int64 start_file_size;
322 EXPECT_TRUE(base::GetFileSize(net_export_log_, &start_file_size));
323
324 net_log_temp_file_->ProcessCommand(NetLogTempFile::DO_STOP);
325 VerifyFileAndStateAfterDoStop();
326
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);
330
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()));
335
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);
339
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();
344
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);
348
349 net_log_temp_file_->ProcessCommand(NetLogTempFile::DO_STOP);
350 VerifyFileAndStateAfterDoStop();
351
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);
355 }
356
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();
362
363 // Get file size without the event.
364 net_log_temp_file_->ProcessCommand(NetLogTempFile::DO_STOP);
365 VerifyFileAndStateAfterDoStop();
366
367 int64 stop_file_size;
368 EXPECT_TRUE(base::GetFileSize(net_export_log_, &stop_file_size));
369
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();
374
375 // Log an event.
376 net_log_->AddGlobalEntry(net::NetLog::TYPE_CANCELLED);
377
378 net_log_temp_file_->ProcessCommand(NetLogTempFile::DO_STOP);
379 VerifyFileAndStateAfterDoStop();
380
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);
384 }
OLDNEW
« no previous file with comments | « chrome/browser/net/net_log_temp_file.cc ('k') | chrome/browser/net/predictor_browsertest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698