| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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/domain_reliability/context.h" | 5 #include "components/domain_reliability/context.h" |
| 6 | 6 |
| 7 #include <stddef.h> | 7 #include <stddef.h> |
| 8 #include <map> | 8 #include <map> |
| 9 #include <string> | 9 #include <string> |
| 10 #include <utility> | 10 #include <utility> |
| (...skipping 15 matching lines...) Expand all Loading... |
| 26 | 26 |
| 27 using base::DictionaryValue; | 27 using base::DictionaryValue; |
| 28 using base::ListValue; | 28 using base::ListValue; |
| 29 using base::Value; | 29 using base::Value; |
| 30 | 30 |
| 31 typedef std::vector<const DomainReliabilityBeacon*> BeaconVector; | 31 typedef std::vector<const DomainReliabilityBeacon*> BeaconVector; |
| 32 | 32 |
| 33 scoped_ptr<DomainReliabilityBeacon> MakeCustomizedBeacon( | 33 scoped_ptr<DomainReliabilityBeacon> MakeCustomizedBeacon( |
| 34 MockableTime* time, | 34 MockableTime* time, |
| 35 std::string status, | 35 std::string status, |
| 36 std::string quic_error) { | 36 std::string quic_error, |
| 37 bool quic_port_migration_detected) { |
| 37 scoped_ptr<DomainReliabilityBeacon> beacon(new DomainReliabilityBeacon()); | 38 scoped_ptr<DomainReliabilityBeacon> beacon(new DomainReliabilityBeacon()); |
| 38 beacon->url = GURL("https://localhost/"); | 39 beacon->url = GURL("https://localhost/"); |
| 39 beacon->status = status; | 40 beacon->status = status; |
| 40 beacon->quic_error = quic_error; | 41 beacon->quic_error = quic_error; |
| 41 beacon->chrome_error = net::ERR_CONNECTION_RESET; | 42 beacon->chrome_error = net::ERR_CONNECTION_RESET; |
| 42 beacon->server_ip = "127.0.0.1"; | 43 beacon->server_ip = "127.0.0.1"; |
| 43 beacon->was_proxied = false; | 44 beacon->was_proxied = false; |
| 44 beacon->protocol = "HTTP"; | 45 beacon->protocol = "HTTP"; |
| 45 beacon->details.quic_broken = true; | 46 beacon->details.quic_broken = true; |
| 47 beacon->details.quic_port_migration_detected = quic_port_migration_detected; |
| 46 beacon->http_response_code = -1; | 48 beacon->http_response_code = -1; |
| 47 beacon->elapsed = base::TimeDelta::FromMilliseconds(250); | 49 beacon->elapsed = base::TimeDelta::FromMilliseconds(250); |
| 48 beacon->start_time = time->NowTicks() - beacon->elapsed; | 50 beacon->start_time = time->NowTicks() - beacon->elapsed; |
| 49 beacon->upload_depth = 0; | 51 beacon->upload_depth = 0; |
| 50 beacon->sample_rate = 1.0; | 52 beacon->sample_rate = 1.0; |
| 51 return beacon; | 53 return beacon; |
| 52 } | 54 } |
| 53 | 55 |
| 54 scoped_ptr<DomainReliabilityBeacon> MakeBeacon(MockableTime* time) { | 56 scoped_ptr<DomainReliabilityBeacon> MakeBeacon(MockableTime* time) { |
| 55 return MakeCustomizedBeacon(time, "tcp.connection_reset", ""); | 57 return MakeCustomizedBeacon(time, "tcp.connection_reset", "", false); |
| 56 } | 58 } |
| 57 | 59 |
| 58 template <typename ValueType, | 60 template <typename ValueType, |
| 59 bool (DictionaryValue::* GetValueType)(const std::string&, | 61 bool (DictionaryValue::* GetValueType)(const std::string&, |
| 60 ValueType*) const> | 62 ValueType*) const> |
| 61 struct HasValue { | 63 struct HasValue { |
| 62 bool operator()(const DictionaryValue& dict, | 64 bool operator()(const DictionaryValue& dict, |
| 63 const std::string& key, | 65 const std::string& key, |
| 64 ValueType expected_value) { | 66 ValueType expected_value) { |
| 65 ValueType actual_value; | 67 ValueType actual_value; |
| (...skipping 182 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 248 // Check that both beacons were uploaded. | 250 // Check that both beacons were uploaded. |
| 249 DomainReliabilityUploader::UploadResult result; | 251 DomainReliabilityUploader::UploadResult result; |
| 250 result.status = DomainReliabilityUploader::UploadResult::SUCCESS; | 252 result.status = DomainReliabilityUploader::UploadResult::SUCCESS; |
| 251 CallUploadCallback(result); | 253 CallUploadCallback(result); |
| 252 | 254 |
| 253 EXPECT_TRUE(CheckNoBeacons()); | 255 EXPECT_TRUE(CheckNoBeacons()); |
| 254 } | 256 } |
| 255 | 257 |
| 256 TEST_F(DomainReliabilityContextTest, ReportUpload) { | 258 TEST_F(DomainReliabilityContextTest, ReportUpload) { |
| 257 InitContext(MakeTestConfig()); | 259 InitContext(MakeTestConfig()); |
| 258 context_->OnBeacon(MakeBeacon(&time_)); | 260 context_->OnBeacon( |
| 261 MakeCustomizedBeacon(&time_, "tcp.connection_reset", "", true)); |
| 259 | 262 |
| 260 BeaconVector beacons; | 263 BeaconVector beacons; |
| 261 context_->GetQueuedBeaconsForTesting(&beacons); | 264 context_->GetQueuedBeaconsForTesting(&beacons); |
| 262 EXPECT_EQ(1u, beacons.size()); | 265 EXPECT_EQ(1u, beacons.size()); |
| 263 | 266 |
| 264 time_.Advance(max_delay()); | 267 time_.Advance(max_delay()); |
| 265 EXPECT_TRUE(upload_pending()); | 268 EXPECT_TRUE(upload_pending()); |
| 266 EXPECT_EQ(0, upload_max_depth()); | 269 EXPECT_EQ(0, upload_max_depth()); |
| 267 EXPECT_EQ(GURL("https://exampleuploader/upload"), upload_url()); | 270 EXPECT_EQ(GURL("https://exampleuploader/upload"), upload_url()); |
| 268 | 271 |
| 269 scoped_ptr<Value> value = base::JSONReader::Read(upload_report()); | 272 scoped_ptr<Value> value = base::JSONReader::Read(upload_report()); |
| 270 const DictionaryValue* entry; | 273 const DictionaryValue* entry; |
| 271 ASSERT_TRUE(GetEntryFromReport(value.get(), 0, &entry)); | 274 ASSERT_TRUE(GetEntryFromReport(value.get(), 0, &entry)); |
| 272 EXPECT_TRUE(HasStringValue(*entry, "failure_data.custom_error", | 275 EXPECT_TRUE(HasStringValue(*entry, "failure_data.custom_error", |
| 273 "net::ERR_CONNECTION_RESET")); | 276 "net::ERR_CONNECTION_RESET")); |
| 274 EXPECT_TRUE(HasBooleanValue(*entry, "network_changed", false)); | 277 EXPECT_TRUE(HasBooleanValue(*entry, "network_changed", false)); |
| 275 EXPECT_TRUE(HasStringValue(*entry, "protocol", "HTTP")); | 278 EXPECT_TRUE(HasStringValue(*entry, "protocol", "HTTP")); |
| 276 EXPECT_TRUE(HasBooleanValue(*entry, "quic_broken", true)); | 279 EXPECT_TRUE(HasBooleanValue(*entry, "quic_broken", true)); |
| 280 EXPECT_TRUE(HasBooleanValue(*entry, "quic_port_migration_detected", true)); |
| 277 // N.B.: Assumes max_delay is 5 minutes. | 281 // N.B.: Assumes max_delay is 5 minutes. |
| 278 EXPECT_TRUE(HasIntegerValue(*entry, "request_age_ms", 300250)); | 282 EXPECT_TRUE(HasIntegerValue(*entry, "request_age_ms", 300250)); |
| 279 EXPECT_TRUE(HasIntegerValue(*entry, "request_elapsed_ms", 250)); | 283 EXPECT_TRUE(HasIntegerValue(*entry, "request_elapsed_ms", 250)); |
| 280 EXPECT_TRUE(HasDoubleValue(*entry, "sample_rate", 1.0)); | 284 EXPECT_TRUE(HasDoubleValue(*entry, "sample_rate", 1.0)); |
| 281 EXPECT_TRUE(HasStringValue(*entry, "server_ip", "127.0.0.1")); | 285 EXPECT_TRUE(HasStringValue(*entry, "server_ip", "127.0.0.1")); |
| 282 EXPECT_TRUE(HasStringValue(*entry, "status", "tcp.connection_reset")); | 286 EXPECT_TRUE(HasStringValue(*entry, "status", "tcp.connection_reset")); |
| 283 EXPECT_TRUE(HasStringValue(*entry, "url", "https://localhost/")); | 287 EXPECT_TRUE(HasStringValue(*entry, "url", "https://localhost/")); |
| 284 EXPECT_TRUE(HasBooleanValue(*entry, "was_proxied", false)); | 288 EXPECT_TRUE(HasBooleanValue(*entry, "was_proxied", false)); |
| 285 | 289 |
| 286 DomainReliabilityUploader::UploadResult result; | 290 DomainReliabilityUploader::UploadResult result; |
| (...skipping 28 matching lines...) Expand all Loading... |
| 315 CallUploadCallback(result); | 319 CallUploadCallback(result); |
| 316 | 320 |
| 317 EXPECT_TRUE(CheckNoBeacons()); | 321 EXPECT_TRUE(CheckNoBeacons()); |
| 318 } | 322 } |
| 319 | 323 |
| 320 // Always expecting granular QUIC errors if status is quic.protocol error. | 324 // Always expecting granular QUIC errors if status is quic.protocol error. |
| 321 TEST_F(DomainReliabilityContextTest, | 325 TEST_F(DomainReliabilityContextTest, |
| 322 ReportUploadWithQuicProtocolErrorAndQuicError) { | 326 ReportUploadWithQuicProtocolErrorAndQuicError) { |
| 323 InitContext(MakeTestConfig()); | 327 InitContext(MakeTestConfig()); |
| 324 context_->OnBeacon(MakeCustomizedBeacon(&time_, "quic.protocol", | 328 context_->OnBeacon(MakeCustomizedBeacon(&time_, "quic.protocol", |
| 325 "quic.invalid.stream_data")); | 329 "quic.invalid.stream_data", true)); |
| 326 | 330 |
| 327 BeaconVector beacons; | 331 BeaconVector beacons; |
| 328 context_->GetQueuedBeaconsForTesting(&beacons); | 332 context_->GetQueuedBeaconsForTesting(&beacons); |
| 329 EXPECT_EQ(1u, beacons.size()); | 333 EXPECT_EQ(1u, beacons.size()); |
| 330 | 334 |
| 331 time_.Advance(max_delay()); | 335 time_.Advance(max_delay()); |
| 332 EXPECT_TRUE(upload_pending()); | 336 EXPECT_TRUE(upload_pending()); |
| 333 EXPECT_EQ(0, upload_max_depth()); | 337 EXPECT_EQ(0, upload_max_depth()); |
| 334 EXPECT_EQ(GURL("https://exampleuploader/upload"), upload_url()); | 338 EXPECT_EQ(GURL("https://exampleuploader/upload"), upload_url()); |
| 335 | 339 |
| 336 scoped_ptr<Value> value = base::JSONReader::Read(upload_report()); | 340 scoped_ptr<Value> value = base::JSONReader::Read(upload_report()); |
| 337 const DictionaryValue* entry; | 341 const DictionaryValue* entry; |
| 338 ASSERT_TRUE(GetEntryFromReport(value.get(), 0, &entry)); | 342 ASSERT_TRUE(GetEntryFromReport(value.get(), 0, &entry)); |
| 339 | 343 |
| 340 EXPECT_TRUE(HasBooleanValue(*entry, "quic_broken", true)); | 344 EXPECT_TRUE(HasBooleanValue(*entry, "quic_broken", true)); |
| 345 EXPECT_TRUE(HasBooleanValue(*entry, "quic_port_migration_detected", true)); |
| 341 EXPECT_TRUE(HasStringValue(*entry, "status", "quic.protocol")); | 346 EXPECT_TRUE(HasStringValue(*entry, "status", "quic.protocol")); |
| 342 EXPECT_TRUE(HasStringValue(*entry, "quic_error", "quic.invalid.stream_data")); | 347 EXPECT_TRUE(HasStringValue(*entry, "quic_error", "quic.invalid.stream_data")); |
| 343 | 348 |
| 344 DomainReliabilityUploader::UploadResult result; | 349 DomainReliabilityUploader::UploadResult result; |
| 345 result.status = DomainReliabilityUploader::UploadResult::SUCCESS; | 350 result.status = DomainReliabilityUploader::UploadResult::SUCCESS; |
| 346 CallUploadCallback(result); | 351 CallUploadCallback(result); |
| 347 | 352 |
| 348 EXPECT_TRUE(CheckNoBeacons()); | 353 EXPECT_TRUE(CheckNoBeacons()); |
| 349 } | 354 } |
| 350 | 355 |
| (...skipping 26 matching lines...) Expand all Loading... |
| 377 EXPECT_TRUE(CheckNoBeacons()); | 382 EXPECT_TRUE(CheckNoBeacons()); |
| 378 } | 383 } |
| 379 | 384 |
| 380 // Edge cases that a non-QUIC protocol error with granular QUIC error reported, | 385 // Edge cases that a non-QUIC protocol error with granular QUIC error reported, |
| 381 // probably indicating state machine in http_network_transaction is working | 386 // probably indicating state machine in http_network_transaction is working |
| 382 // in a different way. | 387 // in a different way. |
| 383 TEST_F(DomainReliabilityContextTest, | 388 TEST_F(DomainReliabilityContextTest, |
| 384 ReportUploadWithNonQuicProtocolErrorAndQuicError) { | 389 ReportUploadWithNonQuicProtocolErrorAndQuicError) { |
| 385 InitContext(MakeTestConfig()); | 390 InitContext(MakeTestConfig()); |
| 386 context_->OnBeacon(MakeCustomizedBeacon(&time_, "tcp.connection_reset", | 391 context_->OnBeacon(MakeCustomizedBeacon(&time_, "tcp.connection_reset", |
| 387 "quic.invalid.stream_data")); | 392 "quic.invalid.stream_data", false)); |
| 388 | 393 |
| 389 BeaconVector beacons; | 394 BeaconVector beacons; |
| 390 context_->GetQueuedBeaconsForTesting(&beacons); | 395 context_->GetQueuedBeaconsForTesting(&beacons); |
| 391 EXPECT_EQ(1u, beacons.size()); | 396 EXPECT_EQ(1u, beacons.size()); |
| 392 | 397 |
| 393 time_.Advance(max_delay()); | 398 time_.Advance(max_delay()); |
| 394 EXPECT_TRUE(upload_pending()); | 399 EXPECT_TRUE(upload_pending()); |
| 395 EXPECT_EQ(0, upload_max_depth()); | 400 EXPECT_EQ(0, upload_max_depth()); |
| 396 EXPECT_EQ(GURL("https://exampleuploader/upload"), upload_url()); | 401 EXPECT_EQ(GURL("https://exampleuploader/upload"), upload_url()); |
| 397 | 402 |
| (...skipping 129 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 527 beacon->chrome_error = net::OK; | 532 beacon->chrome_error = net::OK; |
| 528 context_->OnBeacon(std::move(beacon)); | 533 context_->OnBeacon(std::move(beacon)); |
| 529 context_->GetQueuedBeaconsForTesting(&beacons); | 534 context_->GetQueuedBeaconsForTesting(&beacons); |
| 530 EXPECT_EQ(0u, beacons.size()); | 535 EXPECT_EQ(0u, beacons.size()); |
| 531 } | 536 } |
| 532 | 537 |
| 533 // TODO(ttuttle): Add beacon_unittest.cc to test serialization. | 538 // TODO(ttuttle): Add beacon_unittest.cc to test serialization. |
| 534 | 539 |
| 535 } // namespace | 540 } // namespace |
| 536 } // namespace domain_reliability | 541 } // namespace domain_reliability |
| OLD | NEW |