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

Side by Side Diff: components/network_time/network_time_tracker_unittest.cc

Issue 2447183002: Refactor network_time_test_utils (Closed)
Patch Set: another BUILD.gn fix Created 4 years, 1 month 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
OLDNEW
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/network_time/network_time_tracker.h" 5 #include "components/network_time/network_time_tracker.h"
6 6
7 #include <memory> 7 #include <memory>
8 #include <string> 8 #include <string>
9 #include <utility> 9 #include <utility>
10 10
(...skipping 21 matching lines...) Expand all
32 const char kFetchFailedHistogram[] = "NetworkTimeTracker.UpdateTimeFetchFailed"; 32 const char kFetchFailedHistogram[] = "NetworkTimeTracker.UpdateTimeFetchFailed";
33 const char kFetchValidHistogram[] = "NetworkTimeTracker.UpdateTimeFetchValid"; 33 const char kFetchValidHistogram[] = "NetworkTimeTracker.UpdateTimeFetchValid";
34 const char kClockDivergencePositiveHistogram[] = 34 const char kClockDivergencePositiveHistogram[] =
35 "NetworkTimeTracker.ClockDivergence.Positive"; 35 "NetworkTimeTracker.ClockDivergence.Positive";
36 const char kClockDivergenceNegativeHistogram[] = 36 const char kClockDivergenceNegativeHistogram[] =
37 "NetworkTimeTracker.ClockDivergence.Negative"; 37 "NetworkTimeTracker.ClockDivergence.Negative";
38 const char kWallClockBackwardsHistogram[] = 38 const char kWallClockBackwardsHistogram[] =
39 "NetworkTimeTracker.WallClockRanBackwards"; 39 "NetworkTimeTracker.WallClockRanBackwards";
40 } // namespace 40 } // namespace
41 41
42 class NetworkTimeTrackerTest : public FieldTrialTest { 42 class NetworkTimeTrackerTest : public ::testing::Test {
43 public: 43 public:
44 ~NetworkTimeTrackerTest() override {} 44 ~NetworkTimeTrackerTest() override {}
45 45
46 NetworkTimeTrackerTest() 46 NetworkTimeTrackerTest()
47 : io_thread_("IO thread"), 47 : io_thread_("IO thread"),
48 field_trial_test_(new FieldTrialTest()),
48 clock_(new base::SimpleTestClock), 49 clock_(new base::SimpleTestClock),
49 tick_clock_(new base::SimpleTestTickClock), 50 tick_clock_(new base::SimpleTestTickClock),
50 test_server_(new net::EmbeddedTestServer) { 51 test_server_(new net::EmbeddedTestServer) {
51 base::Thread::Options thread_options; 52 base::Thread::Options thread_options;
52 thread_options.message_loop_type = base::MessageLoop::TYPE_IO; 53 thread_options.message_loop_type = base::MessageLoop::TYPE_IO;
53 EXPECT_TRUE(io_thread_.StartWithOptions(thread_options)); 54 EXPECT_TRUE(io_thread_.StartWithOptions(thread_options));
54 NetworkTimeTracker::RegisterPrefs(pref_service_.registry()); 55 NetworkTimeTracker::RegisterPrefs(pref_service_.registry());
55 56
56 SetNetworkQueriesWithVariationsService(true, 0.0 /* query probability */); 57 field_trial_test_->SetNetworkQueriesWithVariationsService(
58 true, 0.0 /* query probability */,
59 FieldTrialTest::ENABLE_FETCHES_ON_DEMAND);
57 60
58 tracker_.reset(new NetworkTimeTracker( 61 tracker_.reset(new NetworkTimeTracker(
59 std::unique_ptr<base::Clock>(clock_), 62 std::unique_ptr<base::Clock>(clock_),
60 std::unique_ptr<base::TickClock>(tick_clock_), &pref_service_, 63 std::unique_ptr<base::TickClock>(tick_clock_), &pref_service_,
61 new net::TestURLRequestContextGetter(io_thread_.task_runner()))); 64 new net::TestURLRequestContextGetter(io_thread_.task_runner())));
62 65
63 // Do this to be sure that |is_null| returns false. 66 // Do this to be sure that |is_null| returns false.
64 clock_->Advance(base::TimeDelta::FromDays(111)); 67 clock_->Advance(base::TimeDelta::FromDays(111));
65 tick_clock_->Advance(base::TimeDelta::FromDays(222)); 68 tick_clock_->Advance(base::TimeDelta::FromDays(222));
66 69
(...skipping 13 matching lines...) Expand all
80 base::SimpleTestTickClock* new_tick_clock = new base::SimpleTestTickClock(); 83 base::SimpleTestTickClock* new_tick_clock = new base::SimpleTestTickClock();
81 new_tick_clock->SetNowTicks(tick_clock_->NowTicks()); 84 new_tick_clock->SetNowTicks(tick_clock_->NowTicks());
82 clock_ = new_clock; 85 clock_ = new_clock;
83 tick_clock_ = new_tick_clock; 86 tick_clock_ = new_tick_clock;
84 tracker_.reset(new NetworkTimeTracker( 87 tracker_.reset(new NetworkTimeTracker(
85 std::unique_ptr<base::Clock>(clock_), 88 std::unique_ptr<base::Clock>(clock_),
86 std::unique_ptr<base::TickClock>(tick_clock_), &pref_service_, 89 std::unique_ptr<base::TickClock>(tick_clock_), &pref_service_,
87 new net::TestURLRequestContextGetter(io_thread_.task_runner()))); 90 new net::TestURLRequestContextGetter(io_thread_.task_runner())));
88 } 91 }
89 92
90 // Returns a valid time response. Update as follows:
91 //
92 // curl http://clients2.google.com/time/1/current?cup2key=1:123123123
93 //
94 // where 1 is the key version and 123123123 is the nonce. Copy the nonce, the
95 // response, and the x-cup-server-proof header into the test.
96 static std::unique_ptr<net::test_server::HttpResponse>
97 GoodTimeResponseHandler(const net::test_server::HttpRequest& request) {
98 net::test_server::BasicHttpResponse* response =
99 new net::test_server::BasicHttpResponse();
100 response->set_code(net::HTTP_OK);
101 response->set_content(
102 ")]}'\n"
103 "{\"current_time_millis\":1461621971825,\"server_nonce\":-6."
104 "006853099049523E85}");
105 response->AddCustomHeader(
106 "x-cup-server-proof",
107 "304402202e0f24db1ea69f1bbe81da4108f381fcf7a2781c53cf7663cb47083cb5fe8e"
108 "fd"
109 "022009d2b67c0deceaaf849f7c529be96701ed5f15d5efcaf401a94e0801accc9832:"
110 "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855");
111 return std::unique_ptr<net::test_server::HttpResponse>(response);
112 }
113
114 // Good signature over invalid data, though made with a non-production key. 93 // Good signature over invalid data, though made with a non-production key.
115 static std::unique_ptr<net::test_server::HttpResponse> BadDataResponseHandler( 94 static std::unique_ptr<net::test_server::HttpResponse> BadDataResponseHandler(
116 const net::test_server::HttpRequest& request) { 95 const net::test_server::HttpRequest& request) {
117 net::test_server::BasicHttpResponse* response = 96 net::test_server::BasicHttpResponse* response =
118 new net::test_server::BasicHttpResponse(); 97 new net::test_server::BasicHttpResponse();
119 response->set_code(net::HTTP_OK); 98 response->set_code(net::HTTP_OK);
120 response->set_content( 99 response->set_content(
121 ")]}'\n" 100 ")]}'\n"
122 "{\"current_time_millis\":NaN,\"server_nonce\":9.420921002039447E182}"); 101 "{\"current_time_millis\":NaN,\"server_nonce\":9.420921002039447E182}");
123 response->AddCustomHeader( 102 response->AddCustomHeader(
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after
168 147
169 // Advances both the system clock and the tick clock. This should be used for 148 // Advances both the system clock and the tick clock. This should be used for
170 // the normal passage of time, i.e. when neither clock is doing anything odd. 149 // the normal passage of time, i.e. when neither clock is doing anything odd.
171 void AdvanceBoth(const base::TimeDelta& delta) { 150 void AdvanceBoth(const base::TimeDelta& delta) {
172 tick_clock_->Advance(delta); 151 tick_clock_->Advance(delta);
173 clock_->Advance(delta); 152 clock_->Advance(delta);
174 } 153 }
175 154
176 protected: 155 protected:
177 base::Thread io_thread_; 156 base::Thread io_thread_;
157 std::unique_ptr<FieldTrialTest> field_trial_test_;
178 base::MessageLoop message_loop_; 158 base::MessageLoop message_loop_;
179 base::TimeDelta resolution_; 159 base::TimeDelta resolution_;
180 base::TimeDelta latency_; 160 base::TimeDelta latency_;
181 base::TimeDelta adjustment_; 161 base::TimeDelta adjustment_;
182 base::SimpleTestClock* clock_; 162 base::SimpleTestClock* clock_;
183 base::SimpleTestTickClock* tick_clock_; 163 base::SimpleTestTickClock* tick_clock_;
184 TestingPrefServiceSimple pref_service_; 164 TestingPrefServiceSimple pref_service_;
185 std::unique_ptr<NetworkTimeTracker> tracker_; 165 std::unique_ptr<NetworkTimeTracker> tracker_;
186 std::unique_ptr<net::EmbeddedTestServer> test_server_; 166 std::unique_ptr<net::EmbeddedTestServer> test_server_;
187 }; 167 };
(...skipping 287 matching lines...) Expand 10 before | Expand all | Expand 10 after
475 histograms.ExpectTotalCount(kFetchFailedHistogram, 0); 455 histograms.ExpectTotalCount(kFetchFailedHistogram, 0);
476 histograms.ExpectTotalCount(kFetchValidHistogram, 0); 456 histograms.ExpectTotalCount(kFetchValidHistogram, 0);
477 457
478 base::Time out_network_time; 458 base::Time out_network_time;
479 EXPECT_EQ(NetworkTimeTracker::NETWORK_TIME_NO_SYNC_ATTEMPT, 459 EXPECT_EQ(NetworkTimeTracker::NETWORK_TIME_NO_SYNC_ATTEMPT,
480 tracker_->GetNetworkTime(&out_network_time, nullptr)); 460 tracker_->GetNetworkTime(&out_network_time, nullptr));
481 // First query should happen soon. 461 // First query should happen soon.
482 EXPECT_EQ(base::TimeDelta::FromMinutes(0), 462 EXPECT_EQ(base::TimeDelta::FromMinutes(0),
483 tracker_->GetTimerDelayForTesting()); 463 tracker_->GetTimerDelayForTesting());
484 464
485 test_server_->RegisterRequestHandler( 465 test_server_->RegisterRequestHandler(base::Bind(&GoodTimeResponseHandler));
486 base::Bind(&NetworkTimeTrackerTest::GoodTimeResponseHandler));
487 EXPECT_TRUE(test_server_->Start()); 466 EXPECT_TRUE(test_server_->Start());
488 tracker_->SetTimeServerURLForTesting(test_server_->GetURL("/")); 467 tracker_->SetTimeServerURLForTesting(test_server_->GetURL("/"));
489 EXPECT_TRUE(tracker_->QueryTimeServiceForTesting()); 468 EXPECT_TRUE(tracker_->QueryTimeServiceForTesting());
490 tracker_->WaitForFetchForTesting(123123123); 469 tracker_->WaitForFetchForTesting(123123123);
491 470
492 EXPECT_EQ(NetworkTimeTracker::NETWORK_TIME_AVAILABLE, 471 EXPECT_EQ(NetworkTimeTracker::NETWORK_TIME_AVAILABLE,
493 tracker_->GetNetworkTime(&out_network_time, nullptr)); 472 tracker_->GetNetworkTime(&out_network_time, nullptr));
494 EXPECT_EQ(base::Time::UnixEpoch() + 473 EXPECT_EQ(base::Time::UnixEpoch() +
495 base::TimeDelta::FromMilliseconds(1461621971825), 474 base::TimeDelta::FromMilliseconds(1461621971825),
496 out_network_time); 475 out_network_time);
497 // Should see no backoff in the success case. 476 // Should see no backoff in the success case.
498 EXPECT_EQ(base::TimeDelta::FromMinutes(60), 477 EXPECT_EQ(base::TimeDelta::FromMinutes(60),
499 tracker_->GetTimerDelayForTesting()); 478 tracker_->GetTimerDelayForTesting());
500 479
501 histograms.ExpectTotalCount(kFetchFailedHistogram, 0); 480 histograms.ExpectTotalCount(kFetchFailedHistogram, 0);
502 histograms.ExpectTotalCount(kFetchValidHistogram, 1); 481 histograms.ExpectTotalCount(kFetchValidHistogram, 1);
503 histograms.ExpectBucketCount(kFetchValidHistogram, true, 1); 482 histograms.ExpectBucketCount(kFetchValidHistogram, true, 1);
504 } 483 }
505 484
506 TEST_F(NetworkTimeTrackerTest, NoNetworkQueryWhileSynced) { 485 TEST_F(NetworkTimeTrackerTest, NoNetworkQueryWhileSynced) {
507 test_server_->RegisterRequestHandler( 486 test_server_->RegisterRequestHandler(base::Bind(&GoodTimeResponseHandler));
508 base::Bind(&NetworkTimeTrackerTest::GoodTimeResponseHandler));
509 EXPECT_TRUE(test_server_->Start()); 487 EXPECT_TRUE(test_server_->Start());
510 tracker_->SetTimeServerURLForTesting(test_server_->GetURL("/")); 488 tracker_->SetTimeServerURLForTesting(test_server_->GetURL("/"));
511 489
512 SetNetworkQueriesWithVariationsService(true, 0.0); 490 field_trial_test_->SetNetworkQueriesWithVariationsService(
491 true, 0.0, FieldTrialTest::ENABLE_FETCHES_ON_DEMAND);
513 base::Time in_network_time = clock_->Now(); 492 base::Time in_network_time = clock_->Now();
514 UpdateNetworkTime(in_network_time, resolution_, latency_, 493 UpdateNetworkTime(in_network_time, resolution_, latency_,
515 tick_clock_->NowTicks()); 494 tick_clock_->NowTicks());
516 495
517 // No query should be started so long as NetworkTimeTracker is synced, but the 496 // No query should be started so long as NetworkTimeTracker is synced, but the
518 // next check should happen soon. 497 // next check should happen soon.
519 EXPECT_FALSE(tracker_->QueryTimeServiceForTesting()); 498 EXPECT_FALSE(tracker_->QueryTimeServiceForTesting());
520 EXPECT_EQ(base::TimeDelta::FromMinutes(6), 499 EXPECT_EQ(base::TimeDelta::FromMinutes(6),
521 tracker_->GetTimerDelayForTesting()); 500 tracker_->GetTimerDelayForTesting());
522 501
523 SetNetworkQueriesWithVariationsService(true, 1.0); 502 field_trial_test_->SetNetworkQueriesWithVariationsService(
503 true, 1.0, FieldTrialTest::ENABLE_FETCHES_ON_DEMAND);
524 EXPECT_TRUE(tracker_->QueryTimeServiceForTesting()); 504 EXPECT_TRUE(tracker_->QueryTimeServiceForTesting());
525 tracker_->WaitForFetchForTesting(123123123); 505 tracker_->WaitForFetchForTesting(123123123);
526 EXPECT_EQ(base::TimeDelta::FromMinutes(60), 506 EXPECT_EQ(base::TimeDelta::FromMinutes(60),
527 tracker_->GetTimerDelayForTesting()); 507 tracker_->GetTimerDelayForTesting());
528 } 508 }
529 509
530 TEST_F(NetworkTimeTrackerTest, NoNetworkQueryWhileFeatureDisabled) { 510 TEST_F(NetworkTimeTrackerTest, NoNetworkQueryWhileFeatureDisabled) {
531 // Disable network time queries and check that a query is not sent. 511 // Disable network time queries and check that a query is not sent.
532 SetNetworkQueriesWithVariationsService(false, 0.0); 512 field_trial_test_->SetNetworkQueriesWithVariationsService(
513 false, 0.0, FieldTrialTest::ENABLE_FETCHES_ON_DEMAND);
533 EXPECT_FALSE(tracker_->QueryTimeServiceForTesting()); 514 EXPECT_FALSE(tracker_->QueryTimeServiceForTesting());
534 EXPECT_EQ(base::TimeDelta::FromMinutes(6), 515 EXPECT_EQ(base::TimeDelta::FromMinutes(6),
535 tracker_->GetTimerDelayForTesting()); 516 tracker_->GetTimerDelayForTesting());
536 517
537 // Enable time queries and check that a query is sent. 518 // Enable time queries and check that a query is sent.
538 SetNetworkQueriesWithVariationsService(true, 0.0); 519 field_trial_test_->SetNetworkQueriesWithVariationsService(
520 true, 0.0, FieldTrialTest::ENABLE_FETCHES_ON_DEMAND);
539 EXPECT_TRUE(tracker_->QueryTimeServiceForTesting()); 521 EXPECT_TRUE(tracker_->QueryTimeServiceForTesting());
540 tracker_->WaitForFetchForTesting(123123123); 522 tracker_->WaitForFetchForTesting(123123123);
541 } 523 }
542 524
543 TEST_F(NetworkTimeTrackerTest, UpdateFromNetworkBadSignature) { 525 TEST_F(NetworkTimeTrackerTest, UpdateFromNetworkBadSignature) {
544 base::HistogramTester histograms; 526 base::HistogramTester histograms;
545 histograms.ExpectTotalCount(kFetchFailedHistogram, 0); 527 histograms.ExpectTotalCount(kFetchFailedHistogram, 0);
546 histograms.ExpectTotalCount(kFetchValidHistogram, 0); 528 histograms.ExpectTotalCount(kFetchValidHistogram, 0);
547 529
548 test_server_->RegisterRequestHandler( 530 test_server_->RegisterRequestHandler(
(...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after
648 histograms.ExpectBucketCount(kFetchFailedHistogram, -net::ERR_EMPTY_RESPONSE, 630 histograms.ExpectBucketCount(kFetchFailedHistogram, -net::ERR_EMPTY_RESPONSE,
649 1); 631 1);
650 histograms.ExpectTotalCount(kFetchValidHistogram, 0); 632 histograms.ExpectTotalCount(kFetchValidHistogram, 0);
651 } 633 }
652 634
653 TEST_F(NetworkTimeTrackerTest, UpdateFromNetworkLargeResponse) { 635 TEST_F(NetworkTimeTrackerTest, UpdateFromNetworkLargeResponse) {
654 base::HistogramTester histograms; 636 base::HistogramTester histograms;
655 histograms.ExpectTotalCount(kFetchFailedHistogram, 0); 637 histograms.ExpectTotalCount(kFetchFailedHistogram, 0);
656 histograms.ExpectTotalCount(kFetchValidHistogram, 0); 638 histograms.ExpectTotalCount(kFetchValidHistogram, 0);
657 639
658 test_server_->RegisterRequestHandler( 640 test_server_->RegisterRequestHandler(base::Bind(&GoodTimeResponseHandler));
659 base::Bind(&NetworkTimeTrackerTest::GoodTimeResponseHandler));
660 EXPECT_TRUE(test_server_->Start()); 641 EXPECT_TRUE(test_server_->Start());
661 tracker_->SetTimeServerURLForTesting(test_server_->GetURL("/")); 642 tracker_->SetTimeServerURLForTesting(test_server_->GetURL("/"));
662 643
663 base::Time out_network_time; 644 base::Time out_network_time;
664 645
665 tracker_->SetMaxResponseSizeForTesting(3); 646 tracker_->SetMaxResponseSizeForTesting(3);
666 EXPECT_TRUE(tracker_->QueryTimeServiceForTesting()); 647 EXPECT_TRUE(tracker_->QueryTimeServiceForTesting());
667 tracker_->WaitForFetchForTesting(123123123); 648 tracker_->WaitForFetchForTesting(123123123);
668 EXPECT_EQ(NetworkTimeTracker::NETWORK_TIME_NO_SUCCESSFUL_SYNC, 649 EXPECT_EQ(NetworkTimeTracker::NETWORK_TIME_NO_SUCCESSFUL_SYNC,
669 tracker_->GetNetworkTime(&out_network_time, nullptr)); 650 tracker_->GetNetworkTime(&out_network_time, nullptr));
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after
732 EXPECT_EQ(NetworkTimeTracker::NETWORK_TIME_SUBSEQUENT_SYNC_PENDING, 713 EXPECT_EQ(NetworkTimeTracker::NETWORK_TIME_SUBSEQUENT_SYNC_PENDING,
733 tracker_->GetNetworkTime(&out_network_time, nullptr)); 714 tracker_->GetNetworkTime(&out_network_time, nullptr));
734 histograms.ExpectTotalCount(kFetchFailedHistogram, 0); 715 histograms.ExpectTotalCount(kFetchFailedHistogram, 0);
735 histograms.ExpectTotalCount(kFetchValidHistogram, 1); 716 histograms.ExpectTotalCount(kFetchValidHistogram, 1);
736 histograms.ExpectBucketCount(kFetchValidHistogram, false, 1); 717 histograms.ExpectBucketCount(kFetchValidHistogram, false, 1);
737 718
738 tracker_->WaitForFetchForTesting(123123123); 719 tracker_->WaitForFetchForTesting(123123123);
739 } 720 }
740 721
741 } // namespace network_time 722 } // namespace network_time
OLDNEW
« no previous file with comments | « components/network_time/network_time_test_utils.cc ('k') | components/ssl_errors/error_classification_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698