| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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 "net/log/bounded_file_net_log_observer.h" | 5 #include "net/log/bounded_file_net_log_observer.h" |
| 6 | 6 |
| 7 #include <math.h> | 7 #include <math.h> |
| 8 | 8 |
| 9 #include <memory> | 9 #include <memory> |
| 10 #include <string> | 10 #include <string> |
| 11 #include <utility> | 11 #include <utility> |
| 12 | 12 |
| 13 #include "base/files/file_path.h" | 13 #include "base/files/file_path.h" |
| 14 #include "base/files/file_util.h" | 14 #include "base/files/file_util.h" |
| 15 #include "base/files/scoped_file.h" | 15 #include "base/files/scoped_file.h" |
| 16 #include "base/files/scoped_temp_dir.h" | 16 #include "base/files/scoped_temp_dir.h" |
| 17 #include "base/json/json_reader.h" | 17 #include "base/json/json_reader.h" |
| 18 #include "base/json/json_writer.h" | 18 #include "base/json/json_writer.h" |
| 19 #include "base/strings/string_util.h" | 19 #include "base/strings/string_util.h" |
| 20 #include "base/strings/stringprintf.h" |
| 20 #include "base/values.h" | 21 #include "base/values.h" |
| 21 #include "net/base/test_completion_callback.h" | 22 #include "net/base/test_completion_callback.h" |
| 22 #include "net/log/net_log_entry.h" | 23 #include "net/log/net_log_entry.h" |
| 23 #include "net/log/net_log_event_type.h" | 24 #include "net/log/net_log_event_type.h" |
| 24 #include "net/log/net_log_parameters_callback.h" | 25 #include "net/log/net_log_parameters_callback.h" |
| 25 #include "net/log/net_log_source.h" | 26 #include "net/log/net_log_source.h" |
| 26 #include "net/log/net_log_source_type.h" | 27 #include "net/log/net_log_source_type.h" |
| 27 #include "net/log/net_log_util.h" | 28 #include "net/log/net_log_util.h" |
| 28 #include "net/url_request/url_request.h" | 29 #include "net/url_request/url_request.h" |
| 29 #include "net/url_request/url_request_context.h" | 30 #include "net/url_request/url_request_context.h" |
| (...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 132 message = std::string(entry_size - base_entry_size - id.size() + 1, 'x'); | 133 message = std::string(entry_size - base_entry_size - id.size() + 1, 'x'); |
| 133 callback = NetLog::StringCallback("message", &message); | 134 callback = NetLog::StringCallback("message", &message); |
| 134 NetLogEntryData entry_data(NetLogEventType::PAC_JAVASCRIPT_ERROR, source, | 135 NetLogEntryData entry_data(NetLogEventType::PAC_JAVASCRIPT_ERROR, source, |
| 135 NetLogEventPhase::BEGIN, | 136 NetLogEventPhase::BEGIN, |
| 136 base::TimeTicks::Now(), &callback); | 137 base::TimeTicks::Now(), &callback); |
| 137 NetLogEntry entry(&entry_data, NetLogCaptureMode::IncludeSocketBytes()); | 138 NetLogEntry entry(&entry_data, NetLogCaptureMode::IncludeSocketBytes()); |
| 138 logger_->OnAddEntry(entry); | 139 logger_->OnAddEntry(entry); |
| 139 } | 140 } |
| 140 } | 141 } |
| 141 | 142 |
| 143 // Reads the NetLog data that was written to disk to |root|. |events| |
| 144 // points at the "events" section. |
| 145 ::testing::AssertionResult ReadNetLogFromDisk( |
| 146 std::unique_ptr<base::Value>* root, |
| 147 base::ListValue** events) { |
| 148 std::string input; |
| 149 AddAllFiles(&input); |
| 150 if (input.empty()) { |
| 151 return ::testing::AssertionFailure() << "input is empty"; |
| 152 } |
| 153 |
| 154 base::JSONReader reader; |
| 155 *root = reader.ReadToValue(input); |
| 156 if (!*root) { |
| 157 return ::testing::AssertionFailure() << reader.GetErrorMessage(); |
| 158 } |
| 159 |
| 160 base::DictionaryValue* dict; |
| 161 if (!(*root)->GetAsDictionary(&dict)) { |
| 162 return ::testing::AssertionFailure() << "Not a dictionary"; |
| 163 } |
| 164 |
| 165 if (!dict->GetList("events", events)) { |
| 166 return ::testing::AssertionFailure() << "No events list"; |
| 167 } |
| 168 |
| 169 return ::testing::AssertionSuccess(); |
| 170 } |
| 171 |
| 142 protected: | 172 protected: |
| 143 base::FilePath log_path_; | 173 base::FilePath log_path_; |
| 144 NetLog net_log_; | 174 NetLog net_log_; |
| 145 std::unique_ptr<base::Thread> file_thread_; | 175 std::unique_ptr<base::Thread> file_thread_; |
| 146 std::unique_ptr<BoundedFileNetLogObserver> logger_; | 176 std::unique_ptr<BoundedFileNetLogObserver> logger_; |
| 147 | 177 |
| 148 private: | 178 private: |
| 149 base::ScopedTempDir temp_dir_; | 179 base::ScopedTempDir temp_dir_; |
| 150 }; | 180 }; |
| 151 | 181 |
| (...skipping 17 matching lines...) Expand all Loading... |
| 169 | 199 |
| 170 TEST_F(BoundedFileNetLogObserverTest, GeneratesValidJSONForNoEvents) { | 200 TEST_F(BoundedFileNetLogObserverTest, GeneratesValidJSONForNoEvents) { |
| 171 TestClosure closure; | 201 TestClosure closure; |
| 172 | 202 |
| 173 logger_->StartObserving(&net_log_, log_path_, nullptr, nullptr, | 203 logger_->StartObserving(&net_log_, log_path_, nullptr, nullptr, |
| 174 kLargeFileSize, kTotalNumFiles); | 204 kLargeFileSize, kTotalNumFiles); |
| 175 logger_->StopObserving(nullptr, closure.closure()); | 205 logger_->StopObserving(nullptr, closure.closure()); |
| 176 | 206 |
| 177 closure.WaitForResult(); | 207 closure.WaitForResult(); |
| 178 | 208 |
| 179 std::string input; | 209 std::unique_ptr<base::Value> root; |
| 180 AddAllFiles(&input); | 210 base::ListValue* events; |
| 181 ASSERT_FALSE(input.empty()); | 211 ASSERT_TRUE(ReadNetLogFromDisk(&root, &events)); |
| 182 | |
| 183 // Parse JSON | |
| 184 base::JSONReader reader; | |
| 185 std::unique_ptr<base::Value> root(reader.Read(input)); | |
| 186 ASSERT_TRUE(root) << reader.GetErrorMessage(); | |
| 187 | 212 |
| 188 // Check that there are no events | 213 // Check that there are no events |
| 189 base::DictionaryValue* dict; | |
| 190 ASSERT_TRUE(root->GetAsDictionary(&dict)); | |
| 191 base::ListValue* events; | |
| 192 ASSERT_TRUE(dict->GetList("events", &events)); | |
| 193 ASSERT_EQ(0u, events->GetSize()); | 214 ASSERT_EQ(0u, events->GetSize()); |
| 194 | 215 |
| 195 // Check that constants are printed | 216 // Check that constants are printed |
| 217 base::DictionaryValue* dict; |
| 218 ASSERT_TRUE(root->GetAsDictionary(&dict)); |
| 196 base::DictionaryValue* constants; | 219 base::DictionaryValue* constants; |
| 197 ASSERT_TRUE(dict->GetDictionary("constants", &constants)); | 220 ASSERT_TRUE(dict->GetDictionary("constants", &constants)); |
| 198 } | 221 } |
| 199 | 222 |
| 200 // Checks that capture_mode_ defaults correctly when set_capture_mode is not | 223 // Checks that capture_mode_ defaults correctly when set_capture_mode is not |
| 201 // called, and that |capture_mode_| is changed when set_capture_mode is called. | 224 // called, and that |capture_mode_| is changed when set_capture_mode is called. |
| 202 TEST_F(BoundedFileNetLogObserverTest, SetsCaptureMode) { | 225 TEST_F(BoundedFileNetLogObserverTest, SetsCaptureMode) { |
| 203 TestClosure default_closure; | 226 TestClosure default_closure; |
| 204 | 227 |
| 205 logger_->StartObserving(&net_log_, log_path_, nullptr, nullptr, | 228 logger_->StartObserving(&net_log_, log_path_, nullptr, nullptr, |
| (...skipping 27 matching lines...) Expand all Loading... |
| 233 logger_->StartObserving(&net_log_, log_path_, nullptr, nullptr, | 256 logger_->StartObserving(&net_log_, log_path_, nullptr, nullptr, |
| 234 kLargeFileSize, kTotalNumFiles); | 257 kLargeFileSize, kTotalNumFiles); |
| 235 | 258 |
| 236 // Send dummy event. | 259 // Send dummy event. |
| 237 AddEntries(1, kDummyEventSize); | 260 AddEntries(1, kDummyEventSize); |
| 238 | 261 |
| 239 logger_->StopObserving(nullptr, closure.closure()); | 262 logger_->StopObserving(nullptr, closure.closure()); |
| 240 | 263 |
| 241 closure.WaitForResult(); | 264 closure.WaitForResult(); |
| 242 | 265 |
| 243 std::string input; | 266 std::unique_ptr<base::Value> root; |
| 244 AddAllFiles(&input); | 267 base::ListValue* events; |
| 245 ASSERT_FALSE(input.empty()); | 268 ASSERT_TRUE(ReadNetLogFromDisk(&root, &events)); |
| 246 | |
| 247 // Parse input. | |
| 248 base::JSONReader reader; | |
| 249 std::unique_ptr<base::Value> root(reader.ReadToValue(input)); | |
| 250 | |
| 251 ASSERT_TRUE(root) << reader.GetErrorMessage(); | |
| 252 | 269 |
| 253 // Check that there is 1 event written. | 270 // Check that there is 1 event written. |
| 254 base::DictionaryValue* dict; | |
| 255 ASSERT_TRUE(root->GetAsDictionary(&dict)); | |
| 256 base::ListValue* events; | |
| 257 ASSERT_TRUE(dict->GetList("events", &events)); | |
| 258 ASSERT_EQ(1u, events->GetSize()); | 271 ASSERT_EQ(1u, events->GetSize()); |
| 259 } | 272 } |
| 260 | 273 |
| 261 TEST_F(BoundedFileNetLogObserverTest, GeneratesValidJSONWithMultipleEvents) { | 274 TEST_F(BoundedFileNetLogObserverTest, GeneratesValidJSONWithMultipleEvents) { |
| 262 const int kTotalFileSize = 250000; | 275 const int kTotalFileSize = 250000; |
| 263 TestClosure closure; | 276 TestClosure closure; |
| 264 | 277 |
| 265 logger_->StartObserving(&net_log_, log_path_, nullptr, nullptr, | 278 logger_->StartObserving(&net_log_, log_path_, nullptr, nullptr, |
| 266 kTotalFileSize, kTotalNumFiles); | 279 kTotalFileSize, kTotalNumFiles); |
| 267 | 280 |
| 268 AddEntries(2, kDummyEventSize); | 281 AddEntries(2, kDummyEventSize); |
| 269 | 282 |
| 270 logger_->StopObserving(nullptr, closure.closure()); | 283 logger_->StopObserving(nullptr, closure.closure()); |
| 271 | 284 |
| 272 closure.WaitForResult(); | 285 closure.WaitForResult(); |
| 273 | 286 |
| 274 std::string input; | 287 std::unique_ptr<base::Value> root; |
| 275 AddAllFiles(&input); | 288 base::ListValue* events; |
| 276 ASSERT_FALSE(input.empty()); | 289 ASSERT_TRUE(ReadNetLogFromDisk(&root, &events)); |
| 277 | |
| 278 base::JSONReader reader; | |
| 279 std::unique_ptr<base::Value> root(reader.ReadToValue(input)); | |
| 280 ASSERT_TRUE(root) << reader.GetErrorMessage(); | |
| 281 | 290 |
| 282 // Check that 2 events are written. | 291 // Check that 2 events are written. |
| 283 base::DictionaryValue* dict; | |
| 284 ASSERT_TRUE(root->GetAsDictionary(&dict)); | |
| 285 base::ListValue* events; | |
| 286 ASSERT_TRUE(dict->GetList("events", &events)); | |
| 287 ASSERT_EQ(2u, events->GetSize()); | 292 ASSERT_EQ(2u, events->GetSize()); |
| 288 } | 293 } |
| 289 | 294 |
| 290 // Sends enough events to the observer to completely fill one file, but not | 295 // Sends enough events to the observer to completely fill one file, but not |
| 291 // write any events to an additional file. Checks the file bounds. | 296 // write any events to an additional file. Checks the file bounds. |
| 292 TEST_F(BoundedFileNetLogObserverTest, EqualToOneFile) { | 297 TEST_F(BoundedFileNetLogObserverTest, EqualToOneFile) { |
| 293 // The total size of the events is equal to the size of one file. | 298 // The total size of the events is equal to the size of one file. |
| 294 // |kNumEvents| * |kEventSize| = |kTotalFileSize| / |kTotalNumEvents| | 299 // |kNumEvents| * |kEventSize| = |kTotalFileSize| / |kTotalNumEvents| |
| 295 const int kTotalFileSize = 5000; | 300 const int kTotalFileSize = 5000; |
| 296 const int kNumEvents = 2; | 301 const int kNumEvents = 2; |
| 297 const int kEventSize = 250; | 302 const int kEventSize = 250; |
| 298 TestClosure closure; | 303 TestClosure closure; |
| 299 | 304 |
| 300 logger_->StartObserving(&net_log_, log_path_, nullptr, nullptr, | 305 logger_->StartObserving(&net_log_, log_path_, nullptr, nullptr, |
| 301 kTotalFileSize, kTotalNumFiles); | 306 kTotalFileSize, kTotalNumFiles); |
| 302 | 307 |
| 303 AddEntries(kNumEvents, kEventSize); | 308 AddEntries(kNumEvents, kEventSize); |
| 304 logger_->StopObserving(nullptr, closure.closure()); | 309 logger_->StopObserving(nullptr, closure.closure()); |
| 305 | 310 |
| 306 closure.WaitForResult(); | 311 closure.WaitForResult(); |
| 307 | 312 |
| 308 std::string input; | 313 std::unique_ptr<base::Value> root; |
| 309 AddAllFiles(&input); | 314 base::ListValue* events; |
| 310 ASSERT_FALSE(input.empty()); | 315 ASSERT_TRUE(ReadNetLogFromDisk(&root, &events)); |
| 311 | |
| 312 base::JSONReader reader; | |
| 313 std::unique_ptr<base::Value> root(reader.ReadToValue(input)); | |
| 314 ASSERT_TRUE(root) << reader.GetErrorMessage(); | |
| 315 | 316 |
| 316 // Check that the correct number of events were written. | 317 // Check that the correct number of events were written. |
| 317 base::DictionaryValue* dict; | |
| 318 ASSERT_TRUE(root->GetAsDictionary(&dict)); | |
| 319 base::ListValue* events; | |
| 320 ASSERT_TRUE(dict->GetList("events", &events)); | |
| 321 ASSERT_EQ(static_cast<size_t>(kNumEvents), events->GetSize()); | 318 ASSERT_EQ(static_cast<size_t>(kNumEvents), events->GetSize()); |
| 322 | 319 |
| 323 // Check that the last event in array is the last event written. | 320 // Check that the last event in array is the last event written. |
| 324 base::Value* last_event = nullptr; | 321 base::Value* last_event = nullptr; |
| 325 ASSERT_TRUE(events->Get(events->GetSize() - 1, &last_event)); | 322 ASSERT_TRUE(events->Get(events->GetSize() - 1, &last_event)); |
| 323 base::DictionaryValue* dict; |
| 326 last_event->GetAsDictionary(&dict); | 324 last_event->GetAsDictionary(&dict); |
| 327 base::Value* id_value = nullptr; | 325 base::Value* id_value = nullptr; |
| 328 ASSERT_TRUE(dict->Get("source.id", &id_value)); | 326 ASSERT_TRUE(dict->Get("source.id", &id_value)); |
| 329 int id; | 327 int id; |
| 330 ASSERT_TRUE(id_value->GetAsInteger(&id)); | 328 ASSERT_TRUE(id_value->GetAsInteger(&id)); |
| 331 ASSERT_EQ(kNumEvents - 1, id); | 329 ASSERT_EQ(kNumEvents - 1, id); |
| 332 | 330 |
| 333 // Check that events have been written to the first file. | 331 // Check that events have been written to the first file. |
| 334 base::ScopedFILE first_file(base::OpenFile( | 332 base::ScopedFILE first_file(base::OpenFile( |
| 335 log_path_.AppendASCII("event_file_" + std::to_string(0) + ".json"), | 333 log_path_.AppendASCII("event_file_" + std::to_string(0) + ".json"), |
| (...skipping 24 matching lines...) Expand all Loading... |
| 360 | 358 |
| 361 logger_->StartObserving(&net_log_, log_path_, nullptr, nullptr, | 359 logger_->StartObserving(&net_log_, log_path_, nullptr, nullptr, |
| 362 kTotalFileSize, kTotalNumFiles); | 360 kTotalFileSize, kTotalNumFiles); |
| 363 | 361 |
| 364 AddEntries(kNumEvents, kEventSize); | 362 AddEntries(kNumEvents, kEventSize); |
| 365 | 363 |
| 366 logger_->StopObserving(nullptr, closure.closure()); | 364 logger_->StopObserving(nullptr, closure.closure()); |
| 367 | 365 |
| 368 closure.WaitForResult(); | 366 closure.WaitForResult(); |
| 369 | 367 |
| 370 std::string input; | 368 std::unique_ptr<base::Value> root; |
| 371 AddAllFiles(&input); | 369 base::ListValue* events; |
| 372 ASSERT_FALSE(input.empty()); | 370 ASSERT_TRUE(ReadNetLogFromDisk(&root, &events)); |
| 373 | |
| 374 base::JSONReader reader; | |
| 375 std::unique_ptr<base::Value> root(reader.ReadToValue(input)); | |
| 376 ASSERT_TRUE(root) << reader.GetErrorMessage(); | |
| 377 | 371 |
| 378 // Check that the correct number of events were written. | 372 // Check that the correct number of events were written. |
| 379 base::DictionaryValue* dict; | |
| 380 ASSERT_TRUE(root->GetAsDictionary(&dict)); | |
| 381 base::ListValue* events; | |
| 382 ASSERT_TRUE(dict->GetList("events", &events)); | |
| 383 ASSERT_EQ(static_cast<size_t>(kNumEvents), events->GetSize()); | 373 ASSERT_EQ(static_cast<size_t>(kNumEvents), events->GetSize()); |
| 384 | 374 |
| 385 // Check that the last event in array is the last event written. | 375 // Check that the last event in array is the last event written. |
| 386 base::Value* last_event = nullptr; | 376 base::Value* last_event = nullptr; |
| 387 ASSERT_TRUE(events->Get(events->GetSize() - 1, &last_event)); | 377 ASSERT_TRUE(events->Get(events->GetSize() - 1, &last_event)); |
| 378 base::DictionaryValue* dict; |
| 388 last_event->GetAsDictionary(&dict); | 379 last_event->GetAsDictionary(&dict); |
| 389 base::Value* id_value = nullptr; | 380 base::Value* id_value = nullptr; |
| 390 ASSERT_TRUE(dict->Get("source.id", &id_value)); | 381 ASSERT_TRUE(dict->Get("source.id", &id_value)); |
| 391 int id; | 382 int id; |
| 392 ASSERT_TRUE(id_value->GetAsInteger(&id)); | 383 ASSERT_TRUE(id_value->GetAsInteger(&id)); |
| 393 ASSERT_EQ(kNumEvents - 1, id); | 384 ASSERT_EQ(kNumEvents - 1, id); |
| 394 | 385 |
| 395 // Check that all event files except the first two do not exist. | 386 // Check that all event files except the first two do not exist. |
| 396 for (int i = 2; i < kTotalNumFiles; i++) { | 387 for (int i = 2; i < kTotalNumFiles; i++) { |
| 397 ASSERT_FALSE(base::PathExists( | 388 ASSERT_FALSE(base::PathExists( |
| (...skipping 12 matching lines...) Expand all Loading... |
| 410 | 401 |
| 411 logger_->StartObserving(&net_log_, log_path_, nullptr, nullptr, | 402 logger_->StartObserving(&net_log_, log_path_, nullptr, nullptr, |
| 412 kTotalFileSize, kTotalNumFiles); | 403 kTotalFileSize, kTotalNumFiles); |
| 413 | 404 |
| 414 AddEntries(kNumEvents, kEventSize); | 405 AddEntries(kNumEvents, kEventSize); |
| 415 | 406 |
| 416 logger_->StopObserving(nullptr, closure.closure()); | 407 logger_->StopObserving(nullptr, closure.closure()); |
| 417 | 408 |
| 418 closure.WaitForResult(); | 409 closure.WaitForResult(); |
| 419 | 410 |
| 420 std::string input; | 411 std::unique_ptr<base::Value> root; |
| 421 AddAllFiles(&input); | 412 base::ListValue* events; |
| 422 ASSERT_FALSE(input.empty()); | 413 ASSERT_TRUE(ReadNetLogFromDisk(&root, &events)); |
| 423 | |
| 424 base::JSONReader reader; | |
| 425 std::unique_ptr<base::Value> root(reader.ReadToValue(input)); | |
| 426 ASSERT_TRUE(root) << reader.GetErrorMessage(); | |
| 427 | 414 |
| 428 // Check that the correct number of events were written. | 415 // Check that the correct number of events were written. |
| 429 base::DictionaryValue* dict; | |
| 430 ASSERT_TRUE(root->GetAsDictionary(&dict)); | |
| 431 base::ListValue* events; | |
| 432 ASSERT_TRUE(dict->GetList("events", &events)); | |
| 433 ASSERT_EQ(static_cast<size_t>(kNumEvents), events->GetSize()); | 416 ASSERT_EQ(static_cast<size_t>(kNumEvents), events->GetSize()); |
| 434 | 417 |
| 435 // Check that the last event in array is the last event written. | 418 // Check that the last event in array is the last event written. |
| 436 base::Value* last_event = nullptr; | 419 base::Value* last_event = nullptr; |
| 437 ASSERT_TRUE(events->Get(events->GetSize() - 1, &last_event)); | 420 ASSERT_TRUE(events->Get(events->GetSize() - 1, &last_event)); |
| 421 base::DictionaryValue* dict; |
| 438 last_event->GetAsDictionary(&dict); | 422 last_event->GetAsDictionary(&dict); |
| 439 base::Value* id_value = nullptr; | 423 base::Value* id_value = nullptr; |
| 440 ASSERT_TRUE(dict->Get("source.id", &id_value)); | 424 ASSERT_TRUE(dict->Get("source.id", &id_value)); |
| 441 int id; | 425 int id; |
| 442 ASSERT_TRUE(id_value->GetAsInteger(&id)); | 426 ASSERT_TRUE(id_value->GetAsInteger(&id)); |
| 443 ASSERT_EQ(kNumEvents - 1, id); | 427 ASSERT_EQ(kNumEvents - 1, id); |
| 444 | 428 |
| 445 // Check that the first two event files are full. | 429 // Check that the first two event files are full. |
| 446 for (int i = 0; i < (kNumEvents * kEventSize) / | 430 for (int i = 0; i < (kNumEvents * kEventSize) / |
| 447 ((kTotalFileSize - 1) / kTotalNumFiles + 1); | 431 ((kTotalFileSize - 1) / kTotalNumFiles + 1); |
| (...skipping 27 matching lines...) Expand all Loading... |
| 475 | 459 |
| 476 logger_->StartObserving(&net_log_, log_path_, nullptr, nullptr, | 460 logger_->StartObserving(&net_log_, log_path_, nullptr, nullptr, |
| 477 kTotalFileSize, kTotalNumFiles); | 461 kTotalFileSize, kTotalNumFiles); |
| 478 | 462 |
| 479 AddEntries(kNumEvents, kEventSize); | 463 AddEntries(kNumEvents, kEventSize); |
| 480 | 464 |
| 481 logger_->StopObserving(nullptr, closure.closure()); | 465 logger_->StopObserving(nullptr, closure.closure()); |
| 482 | 466 |
| 483 closure.WaitForResult(); | 467 closure.WaitForResult(); |
| 484 | 468 |
| 485 std::string input; | 469 std::unique_ptr<base::Value> root; |
| 486 AddAllFiles(&input); | 470 base::ListValue* events; |
| 487 ASSERT_FALSE(input.empty()); | 471 ASSERT_TRUE(ReadNetLogFromDisk(&root, &events)); |
| 488 | |
| 489 base::JSONReader reader; | |
| 490 std::unique_ptr<base::Value> root(reader.ReadToValue(input)); | |
| 491 ASSERT_TRUE(root) << reader.GetErrorMessage(); | |
| 492 | 472 |
| 493 // Check that the correct number of events were written. | 473 // Check that the correct number of events were written. |
| 494 base::DictionaryValue* dict; | |
| 495 ASSERT_TRUE(root->GetAsDictionary(&dict)); | |
| 496 base::ListValue* events; | |
| 497 ASSERT_TRUE(dict->GetList("events", &events)); | |
| 498 ASSERT_EQ(static_cast<size_t>(kNumEvents), events->GetSize()); | 474 ASSERT_EQ(static_cast<size_t>(kNumEvents), events->GetSize()); |
| 499 | 475 |
| 500 // Check that the last event in array is the last event written. | 476 // Check that the last event in array is the last event written. |
| 501 base::Value* last_event = nullptr; | 477 base::Value* last_event = nullptr; |
| 502 ASSERT_TRUE(events->Get(events->GetSize() - 1, &last_event)); | 478 ASSERT_TRUE(events->Get(events->GetSize() - 1, &last_event)); |
| 479 base::DictionaryValue* dict; |
| 503 last_event->GetAsDictionary(&dict); | 480 last_event->GetAsDictionary(&dict); |
| 504 base::Value* id_value = nullptr; | 481 base::Value* id_value = nullptr; |
| 505 ASSERT_TRUE(dict->Get("source.id", &id_value)); | 482 ASSERT_TRUE(dict->Get("source.id", &id_value)); |
| 506 int id; | 483 int id; |
| 507 ASSERT_TRUE(id_value->GetAsInteger(&id)); | 484 ASSERT_TRUE(id_value->GetAsInteger(&id)); |
| 508 ASSERT_EQ(kNumEvents - 1, id); | 485 ASSERT_EQ(kNumEvents - 1, id); |
| 509 | 486 |
| 510 // Check that all the event files are full. | 487 // Check that all the event files are full. |
| 511 for (int i = 0; i < kTotalNumFiles; i++) { | 488 for (int i = 0; i < kTotalNumFiles; i++) { |
| 512 base::ScopedFILE file_to_test(base::OpenFile( | 489 base::ScopedFILE file_to_test(base::OpenFile( |
| (...skipping 20 matching lines...) Expand all Loading... |
| 533 | 510 |
| 534 logger_->StartObserving(&net_log_, log_path_, nullptr, nullptr, | 511 logger_->StartObserving(&net_log_, log_path_, nullptr, nullptr, |
| 535 kTotalFileSize, kTotalNumFiles); | 512 kTotalFileSize, kTotalNumFiles); |
| 536 | 513 |
| 537 AddEntries(kNumEvents, kEventSize); | 514 AddEntries(kNumEvents, kEventSize); |
| 538 | 515 |
| 539 logger_->StopObserving(nullptr, closure.closure()); | 516 logger_->StopObserving(nullptr, closure.closure()); |
| 540 | 517 |
| 541 closure.WaitForResult(); | 518 closure.WaitForResult(); |
| 542 | 519 |
| 543 std::string input; | 520 std::unique_ptr<base::Value> root; |
| 544 AddAllFiles(&input); | 521 base::ListValue* events; |
| 545 ASSERT_FALSE(input.empty()); | 522 ASSERT_TRUE(ReadNetLogFromDisk(&root, &events)); |
| 546 | |
| 547 base::JSONReader reader; | |
| 548 std::unique_ptr<base::Value> root(reader.ReadToValue(input)); | |
| 549 ASSERT_TRUE(root) << reader.GetErrorMessage(); | |
| 550 | 523 |
| 551 // Check that the correct number of events were written. | 524 // Check that the correct number of events were written. |
| 552 base::DictionaryValue* dict; | |
| 553 ASSERT_TRUE(root->GetAsDictionary(&dict)); | |
| 554 base::ListValue* events; | |
| 555 ASSERT_TRUE(dict->GetList("events", &events)); | |
| 556 ASSERT_EQ( | 525 ASSERT_EQ( |
| 557 static_cast<size_t>(kTotalNumFiles * ((kFileSize - 1) / kEventSize + 1)), | 526 static_cast<size_t>(kTotalNumFiles * ((kFileSize - 1) / kEventSize + 1)), |
| 558 events->GetSize()); | 527 events->GetSize()); |
| 559 | 528 |
| 560 // Check that the oldest event was dropped from the queue. | 529 // Check that the oldest event was dropped from the queue. |
| 561 base::Value* event_to_check = nullptr; | 530 base::Value* event_to_check = nullptr; |
| 562 ASSERT_TRUE(events->Get(0, &event_to_check)); | 531 ASSERT_TRUE(events->Get(0, &event_to_check)); |
| 532 base::DictionaryValue* dict; |
| 563 event_to_check->GetAsDictionary(&dict); | 533 event_to_check->GetAsDictionary(&dict); |
| 564 base::Value* id_value = nullptr; | 534 base::Value* id_value = nullptr; |
| 565 ASSERT_TRUE(dict->Get("source.id", &id_value)); | 535 ASSERT_TRUE(dict->Get("source.id", &id_value)); |
| 566 int id; | 536 int id; |
| 567 ASSERT_TRUE(id_value->GetAsInteger(&id)); | 537 ASSERT_TRUE(id_value->GetAsInteger(&id)); |
| 568 ASSERT_EQ(1, id); | 538 ASSERT_EQ(1, id); |
| 569 | 539 |
| 570 // Check that the last event was written last. | 540 // Check that the last event was written last. |
| 571 event_to_check = nullptr; | 541 event_to_check = nullptr; |
| 572 ASSERT_TRUE(events->Get(events->GetSize() - 1, &event_to_check)); | 542 ASSERT_TRUE(events->Get(events->GetSize() - 1, &event_to_check)); |
| (...skipping 19 matching lines...) Expand all Loading... |
| 592 | 562 |
| 593 logger_->StartObserving(&net_log_, log_path_, nullptr, nullptr, | 563 logger_->StartObserving(&net_log_, log_path_, nullptr, nullptr, |
| 594 kTotalFileSize, kTotalNumFiles); | 564 kTotalFileSize, kTotalNumFiles); |
| 595 | 565 |
| 596 AddEntries(kNumEvents, kEventSize); | 566 AddEntries(kNumEvents, kEventSize); |
| 597 | 567 |
| 598 logger_->StopObserving(nullptr, closure.closure()); | 568 logger_->StopObserving(nullptr, closure.closure()); |
| 599 | 569 |
| 600 closure.WaitForResult(); | 570 closure.WaitForResult(); |
| 601 | 571 |
| 602 std::string input; | 572 std::unique_ptr<base::Value> root; |
| 603 AddAllFiles(&input); | |
| 604 ASSERT_FALSE(input.empty()); | |
| 605 | |
| 606 base::JSONReader reader; | |
| 607 std::unique_ptr<base::Value> root(reader.ReadToValue(input)); | |
| 608 ASSERT_TRUE(root) << reader.GetErrorMessage(); | |
| 609 | |
| 610 // Check that the correct number of events were written. | |
| 611 base::DictionaryValue* dict; | |
| 612 ASSERT_TRUE(root->GetAsDictionary(&dict)); | |
| 613 base::ListValue* events; | 573 base::ListValue* events; |
| 614 ASSERT_TRUE(dict->GetList("events", &events)); | 574 ASSERT_TRUE(ReadNetLogFromDisk(&root, &events)); |
| 615 | 575 |
| 616 // Check that the minimum number of events that should fit in event files | 576 // Check that the minimum number of events that should fit in event files |
| 617 // have been written to all files. | 577 // have been written to all files. |
| 618 int events_per_file = (kFileSize - 1) / kEventSize + 1; | 578 int events_per_file = (kFileSize - 1) / kEventSize + 1; |
| 619 int events_in_last_file = (kNumEvents - 1) % events_per_file + 1; | 579 int events_in_last_file = (kNumEvents - 1) % events_per_file + 1; |
| 620 | 580 |
| 621 // Indicates the total number of events that should be written to all files. | 581 // Indicates the total number of events that should be written to all files. |
| 622 int num_events_in_files = | 582 int num_events_in_files = |
| 623 (kTotalNumFiles - 1) * events_per_file + events_in_last_file; | 583 (kTotalNumFiles - 1) * events_per_file + events_in_last_file; |
| 624 | 584 |
| 625 // Tracks whether each of the events that should be written to all files | 585 // Tracks whether each of the events that should be written to all files |
| 626 // actually appears in the |events| array. The bool at each index corresponds | 586 // actually appears in the |events| array. The bool at each index corresponds |
| 627 // to the event with id = index + |kNumEvents| - |num_events_in_files|. | 587 // to the event with id = index + |kNumEvents| - |num_events_in_files|. |
| 628 std::vector<bool> events_written(num_events_in_files, false); | 588 std::vector<bool> events_written(num_events_in_files, false); |
| 629 | 589 |
| 630 base::Value* event = nullptr; | 590 base::Value* event = nullptr; |
| 631 base::Value* id_value = nullptr; | 591 base::Value* id_value = nullptr; |
| 632 int id; | 592 int id; |
| 633 | 593 |
| 634 // Iterate through each event in |events| and if it is supposed to appear in | 594 // Iterate through each event in |events| and if it is supposed to appear in |
| 635 // file, mark the corresponding bool in |events_written| as true. | 595 // file, mark the corresponding bool in |events_written| as true. |
| 636 for (size_t i = 0; i < events->GetSize(); i++) { | 596 for (size_t i = 0; i < events->GetSize(); i++) { |
| 637 ASSERT_TRUE(events->Get(i, &event)); | 597 ASSERT_TRUE(events->Get(i, &event)); |
| 598 base::DictionaryValue* dict; |
| 638 event->GetAsDictionary(&dict); | 599 event->GetAsDictionary(&dict); |
| 639 ASSERT_TRUE(dict->Get("source.id", &id_value)); | 600 ASSERT_TRUE(dict->Get("source.id", &id_value)); |
| 640 ASSERT_TRUE(id_value->GetAsInteger(&id)); | 601 ASSERT_TRUE(id_value->GetAsInteger(&id)); |
| 641 ASSERT_LT(id, kNumEvents); | 602 ASSERT_LT(id, kNumEvents); |
| 642 if (id >= kNumEvents - num_events_in_files) { | 603 if (id >= kNumEvents - num_events_in_files) { |
| 643 events_written[id - (kNumEvents - num_events_in_files)] = true; | 604 events_written[id - (kNumEvents - num_events_in_files)] = true; |
| 644 } | 605 } |
| 645 } | 606 } |
| 646 | 607 |
| 647 // Check that all events that are supposed to be written to all files | 608 // Check that all events that are supposed to be written to all files |
| (...skipping 30 matching lines...) Expand all Loading... |
| 678 | 639 |
| 679 logger_->StartObserving(&net_log_, log_path_, nullptr, nullptr, | 640 logger_->StartObserving(&net_log_, log_path_, nullptr, nullptr, |
| 680 kTotalFileSize, kTotalNumFiles); | 641 kTotalFileSize, kTotalNumFiles); |
| 681 | 642 |
| 682 AddEntries(kNumEvents, kEventSize); | 643 AddEntries(kNumEvents, kEventSize); |
| 683 | 644 |
| 684 logger_->StopObserving(nullptr, closure.closure()); | 645 logger_->StopObserving(nullptr, closure.closure()); |
| 685 | 646 |
| 686 closure.WaitForResult(); | 647 closure.WaitForResult(); |
| 687 | 648 |
| 688 std::string input; | 649 std::unique_ptr<base::Value> root; |
| 689 AddAllFiles(&input); | |
| 690 ASSERT_FALSE(input.empty()); | |
| 691 | |
| 692 base::JSONReader reader; | |
| 693 std::unique_ptr<base::Value> root(reader.ReadToValue(input)); | |
| 694 ASSERT_TRUE(root) << reader.GetErrorMessage(); | |
| 695 | |
| 696 base::DictionaryValue* dict; | |
| 697 ASSERT_TRUE(root->GetAsDictionary(&dict)); | |
| 698 base::ListValue* events; | 650 base::ListValue* events; |
| 699 ASSERT_TRUE(dict->GetList("events", &events)); | 651 ASSERT_TRUE(ReadNetLogFromDisk(&root, &events)); |
| 700 | 652 |
| 701 // Check that the minimum number of events that should fit in event files | 653 // Check that the minimum number of events that should fit in event files |
| 702 // have been written to a file. | 654 // have been written to a file. |
| 703 int events_per_file = (kFileSize - 1) / kEventSize + 1; | 655 int events_per_file = (kFileSize - 1) / kEventSize + 1; |
| 704 int events_in_last_file = kNumEvents % events_per_file; | 656 int events_in_last_file = kNumEvents % events_per_file; |
| 705 if (!events_in_last_file) | 657 if (!events_in_last_file) |
| 706 events_in_last_file = events_per_file; | 658 events_in_last_file = events_per_file; |
| 707 int num_events_in_files = | 659 int num_events_in_files = |
| 708 (kTotalNumFiles - 1) * events_per_file + events_in_last_file; | 660 (kTotalNumFiles - 1) * events_per_file + events_in_last_file; |
| 709 std::vector<bool> events_written(num_events_in_files, false); | 661 std::vector<bool> events_written(num_events_in_files, false); |
| 710 base::Value* event = nullptr; | 662 base::Value* event = nullptr; |
| 711 base::Value* id_value = nullptr; | 663 base::Value* id_value = nullptr; |
| 712 int id; | 664 int id; |
| 713 for (size_t i = 0; i < events->GetSize(); i++) { | 665 for (size_t i = 0; i < events->GetSize(); i++) { |
| 714 ASSERT_TRUE(events->Get(i, &event)); | 666 ASSERT_TRUE(events->Get(i, &event)); |
| 667 base::DictionaryValue* dict; |
| 715 event->GetAsDictionary(&dict); | 668 event->GetAsDictionary(&dict); |
| 716 ASSERT_TRUE(dict->Get("source.id", &id_value)); | 669 ASSERT_TRUE(dict->Get("source.id", &id_value)); |
| 717 ASSERT_TRUE(id_value->GetAsInteger(&id)); | 670 ASSERT_TRUE(id_value->GetAsInteger(&id)); |
| 718 ASSERT_LT(id, kNumEvents); | 671 ASSERT_LT(id, kNumEvents); |
| 719 if (id >= kNumEvents - num_events_in_files) { | 672 if (id >= kNumEvents - num_events_in_files) { |
| 720 events_written[id - (kNumEvents - num_events_in_files)] = true; | 673 events_written[id - (kNumEvents - num_events_in_files)] = true; |
| 721 } | 674 } |
| 722 } | 675 } |
| 723 ASSERT_TRUE(std::all_of(std::begin(events_written), std::end(events_written), | 676 ASSERT_TRUE(std::all_of(std::begin(events_written), std::end(events_written), |
| 724 [](bool j) { return j; })); | 677 [](bool j) { return j; })); |
| (...skipping 16 matching lines...) Expand all Loading... |
| 741 std::unique_ptr<base::Value> constants( | 694 std::unique_ptr<base::Value> constants( |
| 742 new base::StringValue(kConstantString)); | 695 new base::StringValue(kConstantString)); |
| 743 | 696 |
| 744 logger_->StartObserving(&net_log_, log_path_, constants.get(), nullptr, | 697 logger_->StartObserving(&net_log_, log_path_, constants.get(), nullptr, |
| 745 kLargeFileSize, kTotalNumFiles); | 698 kLargeFileSize, kTotalNumFiles); |
| 746 | 699 |
| 747 logger_->StopObserving(nullptr, closure.closure()); | 700 logger_->StopObserving(nullptr, closure.closure()); |
| 748 | 701 |
| 749 closure.WaitForResult(); | 702 closure.WaitForResult(); |
| 750 | 703 |
| 751 std::string input; | 704 std::unique_ptr<base::Value> root; |
| 752 AddAllFiles(&input); | 705 base::ListValue* events; |
| 753 ASSERT_FALSE(input.empty()); | 706 ASSERT_TRUE(ReadNetLogFromDisk(&root, &events)); |
| 754 | |
| 755 base::JSONReader reader; | |
| 756 std::unique_ptr<base::Value> root(reader.ReadToValue(input)); | |
| 757 ASSERT_TRUE(root) << reader.GetErrorMessage(); | |
| 758 | 707 |
| 759 // Check that custom constant was correctly printed. | 708 // Check that custom constant was correctly printed. |
| 760 base::DictionaryValue* dict; | 709 base::DictionaryValue* dict; |
| 761 ASSERT_TRUE(root->GetAsDictionary(&dict)); | 710 ASSERT_TRUE(root->GetAsDictionary(&dict)); |
| 762 std::string constants_string; | 711 std::string constants_string; |
| 763 ASSERT_TRUE(dict->GetString("constants", &constants_string)); | 712 ASSERT_TRUE(dict->GetString("constants", &constants_string)); |
| 764 ASSERT_EQ(kConstantString, constants_string); | 713 ASSERT_EQ(kConstantString, constants_string); |
| 765 } | 714 } |
| 766 | 715 |
| 767 TEST_F(BoundedFileNetLogObserverTest, GeneratesValidJSONWithContext) { | 716 TEST_F(BoundedFileNetLogObserverTest, GeneratesValidJSONWithContext) { |
| 768 TestClosure closure; | 717 TestClosure closure; |
| 769 | 718 |
| 770 logger_->StartObserving(&net_log_, log_path_, nullptr, nullptr, | 719 logger_->StartObserving(&net_log_, log_path_, nullptr, nullptr, |
| 771 kLargeFileSize, kTotalNumFiles); | 720 kLargeFileSize, kTotalNumFiles); |
| 772 | 721 |
| 773 // Create unique context. | 722 // Create unique context. |
| 774 TestURLRequestContext context(true); | 723 TestURLRequestContext context(true); |
| 775 context.set_net_log(&net_log_); | 724 context.set_net_log(&net_log_); |
| 776 const int kDummyParam = 75; | 725 const int kDummyParam = 75; |
| 777 std::unique_ptr<HttpNetworkSession::Params> params( | 726 std::unique_ptr<HttpNetworkSession::Params> params( |
| 778 new HttpNetworkSession::Params); | 727 new HttpNetworkSession::Params); |
| 779 params->quic_idle_connection_timeout_seconds = kDummyParam; | 728 params->quic_idle_connection_timeout_seconds = kDummyParam; |
| 780 context.set_http_network_session_params(std::move(params)); | 729 context.set_http_network_session_params(std::move(params)); |
| 781 context.Init(); | 730 context.Init(); |
| 782 | 731 |
| 783 logger_->StopObserving(&context, closure.closure()); | 732 logger_->StopObserving(&context, closure.closure()); |
| 784 | 733 |
| 785 closure.WaitForResult(); | 734 closure.WaitForResult(); |
| 786 | 735 |
| 787 std::string input; | 736 std::unique_ptr<base::Value> root; |
| 788 AddAllFiles(&input); | 737 base::ListValue* events; |
| 789 ASSERT_FALSE(input.empty()); | 738 ASSERT_TRUE(ReadNetLogFromDisk(&root, &events)); |
| 790 | |
| 791 base::JSONReader reader; | |
| 792 std::unique_ptr<base::Value> root(reader.ReadToValue(input)); | |
| 793 ASSERT_TRUE(root) << reader.GetErrorMessage(); | |
| 794 | 739 |
| 795 // Check that no events were written. | 740 // Check that no events were written. |
| 796 base::DictionaryValue* dict; | |
| 797 ASSERT_TRUE(root->GetAsDictionary(&dict)); | |
| 798 base::ListValue* events; | |
| 799 ASSERT_TRUE(dict->GetList("events", &events)); | |
| 800 ASSERT_EQ(0u, events->GetSize()); | 741 ASSERT_EQ(0u, events->GetSize()); |
| 801 | 742 |
| 802 // Make sure additional information is present and validate it. | 743 // Make sure additional information is present and validate it. |
| 744 base::DictionaryValue* dict; |
| 745 ASSERT_TRUE(root->GetAsDictionary(&dict)); |
| 803 base::DictionaryValue* tab_info; | 746 base::DictionaryValue* tab_info; |
| 804 base::DictionaryValue* quic_info; | 747 base::DictionaryValue* quic_info; |
| 805 ASSERT_TRUE(dict->GetDictionary("tabInfo", &tab_info)); | 748 ASSERT_TRUE(dict->GetDictionary("tabInfo", &tab_info)); |
| 806 ASSERT_TRUE(tab_info->GetDictionary("quicInfo", &quic_info)); | 749 ASSERT_TRUE(tab_info->GetDictionary("quicInfo", &quic_info)); |
| 807 base::Value* timeout_value = nullptr; | 750 base::Value* timeout_value = nullptr; |
| 808 int timeout; | 751 int timeout; |
| 809 ASSERT_TRUE( | 752 ASSERT_TRUE( |
| 810 quic_info->Get("idle_connection_timeout_seconds", &timeout_value)); | 753 quic_info->Get("idle_connection_timeout_seconds", &timeout_value)); |
| 811 ASSERT_TRUE(timeout_value->GetAsInteger(&timeout)); | 754 ASSERT_TRUE(timeout_value->GetAsInteger(&timeout)); |
| 812 ASSERT_EQ(timeout, kDummyParam); | 755 ASSERT_EQ(timeout, kDummyParam); |
| (...skipping 15 matching lines...) Expand all Loading... |
| 828 context.CreateRequest(GURL("blah:blah"), IDLE, &delegate)); | 771 context.CreateRequest(GURL("blah:blah"), IDLE, &delegate)); |
| 829 request->Start(); | 772 request->Start(); |
| 830 | 773 |
| 831 logger_->StartObserving(&net_log_, log_path_, nullptr, &context, | 774 logger_->StartObserving(&net_log_, log_path_, nullptr, &context, |
| 832 kLargeFileSize, kTotalNumFiles); | 775 kLargeFileSize, kTotalNumFiles); |
| 833 | 776 |
| 834 logger_->StopObserving(&context, closure.closure()); | 777 logger_->StopObserving(&context, closure.closure()); |
| 835 | 778 |
| 836 closure.WaitForResult(); | 779 closure.WaitForResult(); |
| 837 | 780 |
| 838 std::string input; | 781 std::unique_ptr<base::Value> root; |
| 839 AddAllFiles(&input); | 782 base::ListValue* events; |
| 840 ASSERT_FALSE(input.empty()); | 783 ASSERT_TRUE(ReadNetLogFromDisk(&root, &events)); |
| 841 | |
| 842 base::JSONReader reader; | |
| 843 std::unique_ptr<base::Value> root(reader.ReadToValue(input)); | |
| 844 ASSERT_TRUE(root) << reader.GetErrorMessage(); | |
| 845 | 784 |
| 846 // Check that 1 event was written | 785 // Check that 1 event was written |
| 847 base::DictionaryValue* dict; | |
| 848 ASSERT_TRUE(root->GetAsDictionary(&dict)); | |
| 849 base::ListValue* events; | |
| 850 ASSERT_TRUE(dict->GetList("events", &events)); | |
| 851 ASSERT_EQ(1u, events->GetSize()); | 786 ASSERT_EQ(1u, events->GetSize()); |
| 852 | 787 |
| 853 // Make sure additional information is present, but don't validate it. | 788 // Make sure additional information is present, but don't validate it. |
| 789 base::DictionaryValue* dict; |
| 790 ASSERT_TRUE(root->GetAsDictionary(&dict)); |
| 854 base::DictionaryValue* tab_info; | 791 base::DictionaryValue* tab_info; |
| 855 ASSERT_TRUE(dict->GetDictionary("tabInfo", &tab_info)); | 792 ASSERT_TRUE(dict->GetDictionary("tabInfo", &tab_info)); |
| 856 } | 793 } |
| 857 | 794 |
| 858 } // namespace | 795 } // namespace |
| 859 | 796 |
| 860 } // namespace net | 797 } // namespace net |
| OLD | NEW |