OLD | NEW |
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "components/net_log/net_log_temp_file.h" | 5 #include "components/net_log/net_log_temp_file.h" |
6 | 6 |
7 #include "base/basictypes.h" | 7 #include <stdint.h> |
| 8 |
8 #include "base/command_line.h" | 9 #include "base/command_line.h" |
9 #include "base/files/file_path.h" | 10 #include "base/files/file_path.h" |
10 #include "base/files/file_util.h" | 11 #include "base/files/file_util.h" |
11 #include "base/files/scoped_file.h" | 12 #include "base/files/scoped_file.h" |
12 #include "base/files/scoped_temp_dir.h" | 13 #include "base/files/scoped_temp_dir.h" |
13 #include "base/json/json_reader.h" | 14 #include "base/json/json_reader.h" |
14 #include "base/memory/scoped_ptr.h" | 15 #include "base/memory/scoped_ptr.h" |
15 #include "base/message_loop/message_loop.h" | 16 #include "base/message_loop/message_loop.h" |
16 #include "base/values.h" | 17 #include "base/values.h" |
17 #include "build/build_config.h" | 18 #include "build/build_config.h" |
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
86 EXPECT_TRUE(dict->GetString("logType", &log_type)); | 87 EXPECT_TRUE(dict->GetString("logType", &log_type)); |
87 return log_type; | 88 return log_type; |
88 } | 89 } |
89 | 90 |
90 // Make sure the export file has been created and is non-empty, as net | 91 // 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 // constants will always be written to it on creation. |
92 void VerifyNetExportLogExists() const { | 93 void VerifyNetExportLogExists() const { |
93 EXPECT_EQ(net_export_log_, net_log_temp_file_->log_path_); | 94 EXPECT_EQ(net_export_log_, net_log_temp_file_->log_path_); |
94 ASSERT_TRUE(base::PathExists(net_export_log_)); | 95 ASSERT_TRUE(base::PathExists(net_export_log_)); |
95 | 96 |
96 int64 file_size; | 97 int64_t file_size; |
97 // base::GetFileSize returns proper file size on open handles. | 98 // base::GetFileSize returns proper file size on open handles. |
98 ASSERT_TRUE(base::GetFileSize(net_export_log_, &file_size)); | 99 ASSERT_TRUE(base::GetFileSize(net_export_log_, &file_size)); |
99 EXPECT_GT(file_size, 0); | 100 EXPECT_GT(file_size, 0); |
100 } | 101 } |
101 | 102 |
102 // Make sure the export file has been created and a valid JSON file. This | 103 // 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 // should always be the case once logging has been stopped. |
104 void VerifyNetExportLogComplete() const { | 105 void VerifyNetExportLogComplete() const { |
105 VerifyNetExportLogExists(); | 106 VerifyNetExportLogExists(); |
106 | 107 |
(...skipping 210 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
317 net_log_temp_file_->ProcessCommand(NetLogTempFile::DO_STOP); | 318 net_log_temp_file_->ProcessCommand(NetLogTempFile::DO_STOP); |
318 VerifyFileAndStateAfterDoStopWithLogBytes(); | 319 VerifyFileAndStateAfterDoStopWithLogBytes(); |
319 } | 320 } |
320 | 321 |
321 TEST_F(NetLogTempFileTest, DoStartClearsFile) { | 322 TEST_F(NetLogTempFileTest, DoStartClearsFile) { |
322 // Verify file sizes after two consecutive starts/stops are the same (even if | 323 // Verify file sizes after two consecutive starts/stops are the same (even if |
323 // we add some junk data in between). | 324 // we add some junk data in between). |
324 net_log_temp_file_->ProcessCommand(NetLogTempFile::DO_START); | 325 net_log_temp_file_->ProcessCommand(NetLogTempFile::DO_START); |
325 VerifyFileAndStateAfterDoStart(); | 326 VerifyFileAndStateAfterDoStart(); |
326 | 327 |
327 int64 start_file_size; | 328 int64_t start_file_size; |
328 EXPECT_TRUE(base::GetFileSize(net_export_log_, &start_file_size)); | 329 EXPECT_TRUE(base::GetFileSize(net_export_log_, &start_file_size)); |
329 | 330 |
330 net_log_temp_file_->ProcessCommand(NetLogTempFile::DO_STOP); | 331 net_log_temp_file_->ProcessCommand(NetLogTempFile::DO_STOP); |
331 VerifyFileAndStateAfterDoStop(); | 332 VerifyFileAndStateAfterDoStop(); |
332 | 333 |
333 int64 stop_file_size; | 334 int64_t stop_file_size; |
334 EXPECT_TRUE(base::GetFileSize(net_export_log_, &stop_file_size)); | 335 EXPECT_TRUE(base::GetFileSize(net_export_log_, &stop_file_size)); |
335 EXPECT_GE(stop_file_size, start_file_size); | 336 EXPECT_GE(stop_file_size, start_file_size); |
336 | 337 |
337 // Add some junk at the end of the file. | 338 // Add some junk at the end of the file. |
338 std::string junk_data("Hello"); | 339 std::string junk_data("Hello"); |
339 EXPECT_TRUE( | 340 EXPECT_TRUE( |
340 base::AppendToFile(net_export_log_, junk_data.c_str(), junk_data.size())); | 341 base::AppendToFile(net_export_log_, junk_data.c_str(), junk_data.size())); |
341 | 342 |
342 int64 junk_file_size; | 343 int64_t junk_file_size; |
343 EXPECT_TRUE(base::GetFileSize(net_export_log_, &junk_file_size)); | 344 EXPECT_TRUE(base::GetFileSize(net_export_log_, &junk_file_size)); |
344 EXPECT_GT(junk_file_size, stop_file_size); | 345 EXPECT_GT(junk_file_size, stop_file_size); |
345 | 346 |
346 // Execute DO_START/DO_STOP commands and make sure the file is back to the | 347 // Execute DO_START/DO_STOP commands and make sure the file is back to the |
347 // size before addition of junk data. | 348 // size before addition of junk data. |
348 net_log_temp_file_->ProcessCommand(NetLogTempFile::DO_START); | 349 net_log_temp_file_->ProcessCommand(NetLogTempFile::DO_START); |
349 VerifyFileAndStateAfterDoStart(); | 350 VerifyFileAndStateAfterDoStart(); |
350 | 351 |
351 int64 new_start_file_size; | 352 int64_t new_start_file_size; |
352 EXPECT_TRUE(base::GetFileSize(net_export_log_, &new_start_file_size)); | 353 EXPECT_TRUE(base::GetFileSize(net_export_log_, &new_start_file_size)); |
353 EXPECT_EQ(new_start_file_size, start_file_size); | 354 EXPECT_EQ(new_start_file_size, start_file_size); |
354 | 355 |
355 net_log_temp_file_->ProcessCommand(NetLogTempFile::DO_STOP); | 356 net_log_temp_file_->ProcessCommand(NetLogTempFile::DO_STOP); |
356 VerifyFileAndStateAfterDoStop(); | 357 VerifyFileAndStateAfterDoStop(); |
357 | 358 |
358 int64 new_stop_file_size; | 359 int64_t new_stop_file_size; |
359 EXPECT_TRUE(base::GetFileSize(net_export_log_, &new_stop_file_size)); | 360 EXPECT_TRUE(base::GetFileSize(net_export_log_, &new_stop_file_size)); |
360 EXPECT_EQ(new_stop_file_size, stop_file_size); | 361 EXPECT_EQ(new_stop_file_size, stop_file_size); |
361 } | 362 } |
362 | 363 |
363 TEST_F(NetLogTempFileTest, CheckAddEvent) { | 364 TEST_F(NetLogTempFileTest, CheckAddEvent) { |
364 // Add an event to |net_log_| and then test to make sure that, after we stop | 365 // 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 // logging, the file is larger than the file created without that event. |
366 net_log_temp_file_->ProcessCommand(NetLogTempFile::DO_START); | 367 net_log_temp_file_->ProcessCommand(NetLogTempFile::DO_START); |
367 VerifyFileAndStateAfterDoStart(); | 368 VerifyFileAndStateAfterDoStart(); |
368 | 369 |
369 // Get file size without the event. | 370 // Get file size without the event. |
370 net_log_temp_file_->ProcessCommand(NetLogTempFile::DO_STOP); | 371 net_log_temp_file_->ProcessCommand(NetLogTempFile::DO_STOP); |
371 VerifyFileAndStateAfterDoStop(); | 372 VerifyFileAndStateAfterDoStop(); |
372 | 373 |
373 int64 stop_file_size; | 374 int64_t stop_file_size; |
374 EXPECT_TRUE(base::GetFileSize(net_export_log_, &stop_file_size)); | 375 EXPECT_TRUE(base::GetFileSize(net_export_log_, &stop_file_size)); |
375 | 376 |
376 // Perform DO_START and add an Event and then DO_STOP and then compare | 377 // Perform DO_START and add an Event and then DO_STOP and then compare |
377 // file sizes. | 378 // file sizes. |
378 net_log_temp_file_->ProcessCommand(NetLogTempFile::DO_START); | 379 net_log_temp_file_->ProcessCommand(NetLogTempFile::DO_START); |
379 VerifyFileAndStateAfterDoStart(); | 380 VerifyFileAndStateAfterDoStart(); |
380 | 381 |
381 // Log an event. | 382 // Log an event. |
382 net_log_->AddGlobalEntry(net::NetLog::TYPE_CANCELLED); | 383 net_log_->AddGlobalEntry(net::NetLog::TYPE_CANCELLED); |
383 | 384 |
384 net_log_temp_file_->ProcessCommand(NetLogTempFile::DO_STOP); | 385 net_log_temp_file_->ProcessCommand(NetLogTempFile::DO_STOP); |
385 VerifyFileAndStateAfterDoStop(); | 386 VerifyFileAndStateAfterDoStop(); |
386 | 387 |
387 int64 new_stop_file_size; | 388 int64_t new_stop_file_size; |
388 EXPECT_TRUE(base::GetFileSize(net_export_log_, &new_stop_file_size)); | 389 EXPECT_TRUE(base::GetFileSize(net_export_log_, &new_stop_file_size)); |
389 EXPECT_GE(new_stop_file_size, stop_file_size); | 390 EXPECT_GE(new_stop_file_size, stop_file_size); |
390 } | 391 } |
391 | 392 |
392 } // namespace net_log | 393 } // namespace net_log |
OLD | NEW |