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

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

Issue 2254433003: When network time is unavailable, record the reason in UMA (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: holte comments Created 4 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
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 <map> 7 #include <map>
8 #include <memory> 8 #include <memory>
9 #include <string> 9 #include <string>
10 #include <utility> 10 #include <utility>
(...skipping 15 matching lines...) Expand all
26 #include "net/http/http_response_headers.h" 26 #include "net/http/http_response_headers.h"
27 #include "net/test/embedded_test_server/embedded_test_server.h" 27 #include "net/test/embedded_test_server/embedded_test_server.h"
28 #include "net/test/embedded_test_server/http_response.h" 28 #include "net/test/embedded_test_server/http_response.h"
29 #include "net/url_request/url_fetcher.h" 29 #include "net/url_request/url_fetcher.h"
30 #include "net/url_request/url_request_test_util.h" 30 #include "net/url_request/url_request_test_util.h"
31 #include "testing/gtest/include/gtest/gtest.h" 31 #include "testing/gtest/include/gtest/gtest.h"
32 32
33 namespace network_time { 33 namespace network_time {
34 34
35 namespace { 35 namespace {
36 const uint32_t kOneDayInSeconds = 86400;
36 const char kFetchFailedHistogram[] = "NetworkTimeTracker.UpdateTimeFetchFailed"; 37 const char kFetchFailedHistogram[] = "NetworkTimeTracker.UpdateTimeFetchFailed";
37 const char kFetchValidHistogram[] = "NetworkTimeTracker.UpdateTimeFetchValid"; 38 const char kFetchValidHistogram[] = "NetworkTimeTracker.UpdateTimeFetchValid";
39 const char kClockDivergencePositiveHistogram[] =
40 "NetworkTimeTracker.ClockDivergence.Positive";
41 const char kClockDivergenceNegativeHistogram[] =
42 "NetworkTimeTracker.ClockDivergence.Negative";
43 const char kWallClockBackwardsHistogram[] =
44 "NetworkTimeTracker.WallClockRanBackwards";
38 } // namespace 45 } // namespace
39 46
40 class NetworkTimeTrackerTest : public testing::Test { 47 class NetworkTimeTrackerTest : public testing::Test {
41 public: 48 public:
42 ~NetworkTimeTrackerTest() override {} 49 ~NetworkTimeTrackerTest() override {}
43 50
44 NetworkTimeTrackerTest() 51 NetworkTimeTrackerTest()
45 : io_thread_("IO thread"), 52 : io_thread_("IO thread"),
46 clock_(new base::SimpleTestClock), 53 clock_(new base::SimpleTestClock),
47 tick_clock_(new base::SimpleTestTickClock), 54 tick_clock_(new base::SimpleTestTickClock),
(...skipping 177 matching lines...) Expand 10 before | Expand all | Expand 10 after
225 TestingPrefServiceSimple pref_service_; 232 TestingPrefServiceSimple pref_service_;
226 std::unique_ptr<base::FieldTrialList> field_trial_list_; 233 std::unique_ptr<base::FieldTrialList> field_trial_list_;
227 std::unique_ptr<NetworkTimeTracker> tracker_; 234 std::unique_ptr<NetworkTimeTracker> tracker_;
228 std::unique_ptr<net::EmbeddedTestServer> test_server_; 235 std::unique_ptr<net::EmbeddedTestServer> test_server_;
229 std::unique_ptr<base::test::ScopedFeatureList> scoped_feature_list_; 236 std::unique_ptr<base::test::ScopedFeatureList> scoped_feature_list_;
230 }; 237 };
231 238
232 TEST_F(NetworkTimeTrackerTest, Uninitialized) { 239 TEST_F(NetworkTimeTrackerTest, Uninitialized) {
233 base::Time network_time; 240 base::Time network_time;
234 base::TimeDelta uncertainty; 241 base::TimeDelta uncertainty;
235 EXPECT_FALSE(tracker_->GetNetworkTime(&network_time, &uncertainty)); 242 EXPECT_EQ(NetworkTimeTracker::NETWORK_TIME_NO_SYNC,
243 tracker_->GetNetworkTime(&network_time, &uncertainty));
236 } 244 }
237 245
238 TEST_F(NetworkTimeTrackerTest, LongPostingDelay) { 246 TEST_F(NetworkTimeTrackerTest, LongPostingDelay) {
239 // The request arrives at the server, which records the time. Advance the 247 // The request arrives at the server, which records the time. Advance the
240 // clock to simulate the latency of sending the reply, which we'll say for 248 // clock to simulate the latency of sending the reply, which we'll say for
241 // convenience is half the total latency. 249 // convenience is half the total latency.
242 base::Time in_network_time = clock_->Now(); 250 base::Time in_network_time = clock_->Now();
243 AdvanceBoth(latency_ / 2); 251 AdvanceBoth(latency_ / 2);
244 252
245 // Record the tick counter at the time the reply is received. At this point, 253 // Record the tick counter at the time the reply is received. At this point,
246 // we would post UpdateNetworkTime to be run on the browser thread. 254 // we would post UpdateNetworkTime to be run on the browser thread.
247 base::TimeTicks posting_time = tick_clock_->NowTicks(); 255 base::TimeTicks posting_time = tick_clock_->NowTicks();
248 256
249 // Simulate that it look a long time (1888us) for the browser thread to get 257 // Simulate that it look a long time (1888us) for the browser thread to get
250 // around to executing the update. 258 // around to executing the update.
251 AdvanceBoth(base::TimeDelta::FromMicroseconds(1888)); 259 AdvanceBoth(base::TimeDelta::FromMicroseconds(1888));
252 UpdateNetworkTime(in_network_time, resolution_, latency_, posting_time); 260 UpdateNetworkTime(in_network_time, resolution_, latency_, posting_time);
253 261
254 base::Time out_network_time; 262 base::Time out_network_time;
255 base::TimeDelta uncertainty; 263 base::TimeDelta uncertainty;
256 EXPECT_TRUE(tracker_->GetNetworkTime(&out_network_time, &uncertainty)); 264 EXPECT_EQ(NetworkTimeTracker::NETWORK_TIME_AVAILABLE,
265 tracker_->GetNetworkTime(&out_network_time, &uncertainty));
257 EXPECT_EQ(resolution_ + latency_ + adjustment_, uncertainty); 266 EXPECT_EQ(resolution_ + latency_ + adjustment_, uncertainty);
258 EXPECT_EQ(clock_->Now(), out_network_time); 267 EXPECT_EQ(clock_->Now(), out_network_time);
259 } 268 }
260 269
261 TEST_F(NetworkTimeTrackerTest, LopsidedLatency) { 270 TEST_F(NetworkTimeTrackerTest, LopsidedLatency) {
262 // Simulate that the server received the request instantaneously, and that all 271 // Simulate that the server received the request instantaneously, and that all
263 // of the latency was in sending the reply. (This contradicts the assumption 272 // of the latency was in sending the reply. (This contradicts the assumption
264 // in the code.) 273 // in the code.)
265 base::Time in_network_time = clock_->Now(); 274 base::Time in_network_time = clock_->Now();
266 AdvanceBoth(latency_); 275 AdvanceBoth(latency_);
267 UpdateNetworkTime(in_network_time, resolution_, latency_, 276 UpdateNetworkTime(in_network_time, resolution_, latency_,
268 tick_clock_->NowTicks()); 277 tick_clock_->NowTicks());
269 278
270 // But, the answer is still within the uncertainty bounds! 279 // But, the answer is still within the uncertainty bounds!
271 base::Time out_network_time; 280 base::Time out_network_time;
272 base::TimeDelta uncertainty; 281 base::TimeDelta uncertainty;
273 EXPECT_TRUE(tracker_->GetNetworkTime(&out_network_time, &uncertainty)); 282 EXPECT_EQ(NetworkTimeTracker::NETWORK_TIME_AVAILABLE,
283 tracker_->GetNetworkTime(&out_network_time, &uncertainty));
274 EXPECT_LT(out_network_time - uncertainty / 2, clock_->Now()); 284 EXPECT_LT(out_network_time - uncertainty / 2, clock_->Now());
275 EXPECT_GT(out_network_time + uncertainty / 2, clock_->Now()); 285 EXPECT_GT(out_network_time + uncertainty / 2, clock_->Now());
276 } 286 }
277 287
278 TEST_F(NetworkTimeTrackerTest, ClockIsWack) { 288 TEST_F(NetworkTimeTrackerTest, ClockIsWack) {
279 // Now let's assume the system clock is completely wrong. 289 // Now let's assume the system clock is completely wrong.
280 base::Time in_network_time = clock_->Now() - base::TimeDelta::FromDays(90); 290 base::Time in_network_time = clock_->Now() - base::TimeDelta::FromDays(90);
281 UpdateNetworkTime(in_network_time - latency_ / 2, resolution_, latency_, 291 UpdateNetworkTime(in_network_time - latency_ / 2, resolution_, latency_,
282 tick_clock_->NowTicks()); 292 tick_clock_->NowTicks());
283 293
284 base::Time out_network_time; 294 base::Time out_network_time;
285 EXPECT_TRUE(tracker_->GetNetworkTime(&out_network_time, nullptr)); 295 EXPECT_EQ(NetworkTimeTracker::NETWORK_TIME_AVAILABLE,
296 tracker_->GetNetworkTime(&out_network_time, nullptr));
286 EXPECT_EQ(in_network_time, out_network_time); 297 EXPECT_EQ(in_network_time, out_network_time);
287 } 298 }
288 299
289 TEST_F(NetworkTimeTrackerTest, ClocksDivergeSlightly) { 300 TEST_F(NetworkTimeTrackerTest, ClocksDivergeSlightly) {
290 // The two clocks are allowed to diverge a little bit. 301 // The two clocks are allowed to diverge a little bit.
302 base::HistogramTester histograms;
303 histograms.ExpectTotalCount(kClockDivergencePositiveHistogram, 0);
304 histograms.ExpectTotalCount(kClockDivergenceNegativeHistogram, 0);
305 histograms.ExpectTotalCount(kWallClockBackwardsHistogram, 0);
291 base::Time in_network_time = clock_->Now(); 306 base::Time in_network_time = clock_->Now();
292 UpdateNetworkTime(in_network_time - latency_ / 2, resolution_, latency_, 307 UpdateNetworkTime(in_network_time - latency_ / 2, resolution_, latency_,
293 tick_clock_->NowTicks()); 308 tick_clock_->NowTicks());
294 309
295 base::TimeDelta small = base::TimeDelta::FromSeconds(30); 310 base::TimeDelta small = base::TimeDelta::FromSeconds(30);
296 tick_clock_->Advance(small); 311 tick_clock_->Advance(small);
297 base::Time out_network_time; 312 base::Time out_network_time;
298 base::TimeDelta out_uncertainty; 313 base::TimeDelta out_uncertainty;
299 EXPECT_TRUE(tracker_->GetNetworkTime(&out_network_time, &out_uncertainty)); 314 EXPECT_EQ(NetworkTimeTracker::NETWORK_TIME_AVAILABLE,
315 tracker_->GetNetworkTime(&out_network_time, &out_uncertainty));
300 EXPECT_EQ(in_network_time + small, out_network_time); 316 EXPECT_EQ(in_network_time + small, out_network_time);
301 // The clock divergence should show up in the uncertainty. 317 // The clock divergence should show up in the uncertainty.
302 EXPECT_EQ(resolution_ + latency_ + adjustment_ + small, out_uncertainty); 318 EXPECT_EQ(resolution_ + latency_ + adjustment_ + small, out_uncertainty);
319 histograms.ExpectTotalCount(kClockDivergencePositiveHistogram, 0);
320 histograms.ExpectTotalCount(kClockDivergenceNegativeHistogram, 0);
321 histograms.ExpectTotalCount(kWallClockBackwardsHistogram, 0);
303 } 322 }
304 323
305 TEST_F(NetworkTimeTrackerTest, NetworkTimeUpdates) { 324 TEST_F(NetworkTimeTrackerTest, NetworkTimeUpdates) {
306 // Verify that the the tracker receives and properly handles updates to the 325 // Verify that the the tracker receives and properly handles updates to the
307 // network time. 326 // network time.
308 base::Time out_network_time; 327 base::Time out_network_time;
309 base::TimeDelta uncertainty; 328 base::TimeDelta uncertainty;
310 329
311 UpdateNetworkTime(clock_->Now() - latency_ / 2, resolution_, latency_, 330 UpdateNetworkTime(clock_->Now() - latency_ / 2, resolution_, latency_,
312 tick_clock_->NowTicks()); 331 tick_clock_->NowTicks());
313 EXPECT_TRUE(tracker_->GetNetworkTime(&out_network_time, &uncertainty)); 332 EXPECT_EQ(NetworkTimeTracker::NETWORK_TIME_AVAILABLE,
333 tracker_->GetNetworkTime(&out_network_time, &uncertainty));
314 EXPECT_EQ(clock_->Now(), out_network_time); 334 EXPECT_EQ(clock_->Now(), out_network_time);
315 EXPECT_EQ(resolution_ + latency_ + adjustment_, uncertainty); 335 EXPECT_EQ(resolution_ + latency_ + adjustment_, uncertainty);
316 336
317 // Fake a wait to make sure we keep tracking. 337 // Fake a wait to make sure we keep tracking.
318 AdvanceBoth(base::TimeDelta::FromSeconds(1)); 338 AdvanceBoth(base::TimeDelta::FromSeconds(1));
319 EXPECT_TRUE(tracker_->GetNetworkTime(&out_network_time, &uncertainty)); 339 EXPECT_EQ(NetworkTimeTracker::NETWORK_TIME_AVAILABLE,
340 tracker_->GetNetworkTime(&out_network_time, &uncertainty));
320 EXPECT_EQ(clock_->Now(), out_network_time); 341 EXPECT_EQ(clock_->Now(), out_network_time);
321 EXPECT_EQ(resolution_ + latency_ + adjustment_, uncertainty); 342 EXPECT_EQ(resolution_ + latency_ + adjustment_, uncertainty);
322 343
323 // And one more time. 344 // And one more time.
324 UpdateNetworkTime(clock_->Now() - latency_ / 2, resolution_, latency_, 345 UpdateNetworkTime(clock_->Now() - latency_ / 2, resolution_, latency_,
325 tick_clock_->NowTicks()); 346 tick_clock_->NowTicks());
326 AdvanceBoth(base::TimeDelta::FromSeconds(1)); 347 AdvanceBoth(base::TimeDelta::FromSeconds(1));
327 EXPECT_TRUE(tracker_->GetNetworkTime(&out_network_time, &uncertainty)); 348 EXPECT_EQ(NetworkTimeTracker::NETWORK_TIME_AVAILABLE,
349 tracker_->GetNetworkTime(&out_network_time, &uncertainty));
328 EXPECT_EQ(clock_->Now(), out_network_time); 350 EXPECT_EQ(clock_->Now(), out_network_time);
329 EXPECT_EQ(resolution_ + latency_ + adjustment_, uncertainty); 351 EXPECT_EQ(resolution_ + latency_ + adjustment_, uncertainty);
330 } 352 }
331 353
332 TEST_F(NetworkTimeTrackerTest, SpringForward) { 354 TEST_F(NetworkTimeTrackerTest, SpringForward) {
355 base::HistogramTester histograms;
356 histograms.ExpectTotalCount(kClockDivergencePositiveHistogram, 0);
357 histograms.ExpectTotalCount(kClockDivergenceNegativeHistogram, 0);
358 histograms.ExpectTotalCount(kWallClockBackwardsHistogram, 0);
333 // Simulate the wall clock advancing faster than the tick clock. 359 // Simulate the wall clock advancing faster than the tick clock.
334 UpdateNetworkTime(clock_->Now(), resolution_, latency_, 360 UpdateNetworkTime(clock_->Now(), resolution_, latency_,
335 tick_clock_->NowTicks()); 361 tick_clock_->NowTicks());
336 tick_clock_->Advance(base::TimeDelta::FromSeconds(1)); 362 tick_clock_->Advance(base::TimeDelta::FromSeconds(1));
337 clock_->Advance(base::TimeDelta::FromDays(1)); 363 clock_->Advance(base::TimeDelta::FromDays(1));
338 base::Time out_network_time; 364 base::Time out_network_time;
339 EXPECT_FALSE(tracker_->GetNetworkTime(&out_network_time, nullptr)); 365 EXPECT_EQ(NetworkTimeTracker::NETWORK_TIME_SYNC_LOST,
366 tracker_->GetNetworkTime(&out_network_time, nullptr));
367 histograms.ExpectTotalCount(kClockDivergencePositiveHistogram, 0);
368 histograms.ExpectTotalCount(kClockDivergenceNegativeHistogram, 1);
369 histograms.ExpectTotalCount(kWallClockBackwardsHistogram, 0);
370 // The recorded clock divergence should be 1 second - 1 day in seconds.
371 histograms.ExpectBucketCount(kClockDivergenceNegativeHistogram,
372 kOneDayInSeconds - 1, 1);
373 }
374
375 TEST_F(NetworkTimeTrackerTest, TickClockSpringsForward) {
376 base::HistogramTester histograms;
377 histograms.ExpectTotalCount(kClockDivergencePositiveHistogram, 0);
378 histograms.ExpectTotalCount(kClockDivergenceNegativeHistogram, 0);
379 histograms.ExpectTotalCount(kWallClockBackwardsHistogram, 0);
380 // Simulate the tick clock advancing faster than the wall clock.
381 UpdateNetworkTime(clock_->Now(), resolution_, latency_,
382 tick_clock_->NowTicks());
383 tick_clock_->Advance(base::TimeDelta::FromDays(1));
384 clock_->Advance(base::TimeDelta::FromSeconds(1));
385 base::Time out_network_time;
386 EXPECT_EQ(NetworkTimeTracker::NETWORK_TIME_SYNC_LOST,
387 tracker_->GetNetworkTime(&out_network_time, nullptr));
388 histograms.ExpectTotalCount(kClockDivergencePositiveHistogram, 1);
389 histograms.ExpectTotalCount(kClockDivergenceNegativeHistogram, 0);
390 histograms.ExpectTotalCount(kWallClockBackwardsHistogram, 0);
391 // The recorded clock divergence should be 1 day - 1 second.
392 histograms.ExpectBucketCount(kClockDivergencePositiveHistogram,
393 kOneDayInSeconds - 1, 1);
340 } 394 }
341 395
342 TEST_F(NetworkTimeTrackerTest, FallBack) { 396 TEST_F(NetworkTimeTrackerTest, FallBack) {
397 base::HistogramTester histograms;
398 histograms.ExpectTotalCount(kClockDivergencePositiveHistogram, 0);
399 histograms.ExpectTotalCount(kClockDivergenceNegativeHistogram, 0);
400 histograms.ExpectTotalCount(kWallClockBackwardsHistogram, 0);
343 // Simulate the wall clock running backward. 401 // Simulate the wall clock running backward.
344 UpdateNetworkTime(clock_->Now(), resolution_, latency_, 402 UpdateNetworkTime(clock_->Now(), resolution_, latency_,
345 tick_clock_->NowTicks()); 403 tick_clock_->NowTicks());
346 tick_clock_->Advance(base::TimeDelta::FromSeconds(1)); 404 tick_clock_->Advance(base::TimeDelta::FromSeconds(1));
347 clock_->Advance(base::TimeDelta::FromDays(-1)); 405 clock_->Advance(base::TimeDelta::FromDays(-1));
348 base::Time out_network_time; 406 base::Time out_network_time;
349 EXPECT_FALSE(tracker_->GetNetworkTime(&out_network_time, nullptr)); 407 EXPECT_EQ(NetworkTimeTracker::NETWORK_TIME_SYNC_LOST,
408 tracker_->GetNetworkTime(&out_network_time, nullptr));
409 histograms.ExpectTotalCount(kClockDivergencePositiveHistogram, 0);
410 histograms.ExpectTotalCount(kClockDivergenceNegativeHistogram, 0);
411 histograms.ExpectTotalCount(kWallClockBackwardsHistogram, 1);
412 histograms.ExpectBucketCount(kWallClockBackwardsHistogram,
413 kOneDayInSeconds - 1, 1);
350 } 414 }
351 415
352 TEST_F(NetworkTimeTrackerTest, SuspendAndResume) { 416 TEST_F(NetworkTimeTrackerTest, SuspendAndResume) {
353 // Simulate the wall clock advancing while the tick clock stands still, as 417 // Simulate the wall clock advancing while the tick clock stands still, as
354 // would happen in a suspend+resume cycle. 418 // would happen in a suspend+resume cycle.
355 UpdateNetworkTime(clock_->Now(), resolution_, latency_, 419 UpdateNetworkTime(clock_->Now(), resolution_, latency_,
356 tick_clock_->NowTicks()); 420 tick_clock_->NowTicks());
357 clock_->Advance(base::TimeDelta::FromHours(1)); 421 clock_->Advance(base::TimeDelta::FromHours(1));
358 base::Time out_network_time; 422 base::Time out_network_time;
359 EXPECT_FALSE(tracker_->GetNetworkTime(&out_network_time, nullptr)); 423 EXPECT_EQ(NetworkTimeTracker::NETWORK_TIME_SYNC_LOST,
424 tracker_->GetNetworkTime(&out_network_time, nullptr));
360 } 425 }
361 426
362 TEST_F(NetworkTimeTrackerTest, Serialize) { 427 TEST_F(NetworkTimeTrackerTest, Serialize) {
363 // Test that we can serialize and deserialize state and get consistent 428 // Test that we can serialize and deserialize state and get consistent
364 // results. 429 // results.
365 base::Time in_network_time = clock_->Now() - base::TimeDelta::FromDays(90); 430 base::Time in_network_time = clock_->Now() - base::TimeDelta::FromDays(90);
366 UpdateNetworkTime(in_network_time - latency_ / 2, resolution_, latency_, 431 UpdateNetworkTime(in_network_time - latency_ / 2, resolution_, latency_,
367 tick_clock_->NowTicks()); 432 tick_clock_->NowTicks());
368 base::Time out_network_time; 433 base::Time out_network_time;
369 base::TimeDelta out_uncertainty; 434 base::TimeDelta out_uncertainty;
370 EXPECT_TRUE(tracker_->GetNetworkTime(&out_network_time, &out_uncertainty)); 435 EXPECT_EQ(NetworkTimeTracker::NETWORK_TIME_AVAILABLE,
436 tracker_->GetNetworkTime(&out_network_time, &out_uncertainty));
371 EXPECT_EQ(in_network_time, out_network_time); 437 EXPECT_EQ(in_network_time, out_network_time);
372 EXPECT_EQ(resolution_ + latency_ + adjustment_, out_uncertainty); 438 EXPECT_EQ(resolution_ + latency_ + adjustment_, out_uncertainty);
373 439
374 // 6 days is just under the threshold for discarding data. 440 // 6 days is just under the threshold for discarding data.
375 base::TimeDelta delta = base::TimeDelta::FromDays(6); 441 base::TimeDelta delta = base::TimeDelta::FromDays(6);
376 AdvanceBoth(delta); 442 AdvanceBoth(delta);
377 Reset(); 443 Reset();
378 EXPECT_TRUE(tracker_->GetNetworkTime(&out_network_time, &out_uncertainty)); 444 EXPECT_EQ(NetworkTimeTracker::NETWORK_TIME_AVAILABLE,
445 tracker_->GetNetworkTime(&out_network_time, &out_uncertainty));
379 EXPECT_EQ(in_network_time + delta, out_network_time); 446 EXPECT_EQ(in_network_time + delta, out_network_time);
380 EXPECT_EQ(resolution_ + latency_ + adjustment_, out_uncertainty); 447 EXPECT_EQ(resolution_ + latency_ + adjustment_, out_uncertainty);
381 } 448 }
382 449
383 TEST_F(NetworkTimeTrackerTest, DeserializeOldFormat) { 450 TEST_F(NetworkTimeTrackerTest, DeserializeOldFormat) {
384 // Test that deserializing old data (which do not record the uncertainty and 451 // Test that deserializing old data (which do not record the uncertainty and
385 // tick clock) causes the serialized data to be ignored. 452 // tick clock) causes the serialized data to be ignored.
386 base::Time in_network_time = clock_->Now() - base::TimeDelta::FromDays(90); 453 base::Time in_network_time = clock_->Now() - base::TimeDelta::FromDays(90);
387 UpdateNetworkTime(in_network_time - latency_ / 2, resolution_, latency_, 454 UpdateNetworkTime(in_network_time - latency_ / 2, resolution_, latency_,
388 tick_clock_->NowTicks()); 455 tick_clock_->NowTicks());
389 456
390 base::Time out_network_time; 457 base::Time out_network_time;
391 EXPECT_TRUE(tracker_->GetNetworkTime(&out_network_time, nullptr)); 458 EXPECT_EQ(NetworkTimeTracker::NETWORK_TIME_AVAILABLE,
459 tracker_->GetNetworkTime(&out_network_time, nullptr));
392 double local, network; 460 double local, network;
393 const base::DictionaryValue* saved_prefs = 461 const base::DictionaryValue* saved_prefs =
394 pref_service_.GetDictionary(prefs::kNetworkTimeMapping); 462 pref_service_.GetDictionary(prefs::kNetworkTimeMapping);
395 saved_prefs->GetDouble("local", &local); 463 saved_prefs->GetDouble("local", &local);
396 saved_prefs->GetDouble("network", &network); 464 saved_prefs->GetDouble("network", &network);
397 base::DictionaryValue prefs; 465 base::DictionaryValue prefs;
398 prefs.SetDouble("local", local); 466 prefs.SetDouble("local", local);
399 prefs.SetDouble("network", network); 467 prefs.SetDouble("network", network);
400 pref_service_.Set(prefs::kNetworkTimeMapping, prefs); 468 pref_service_.Set(prefs::kNetworkTimeMapping, prefs);
401 Reset(); 469 Reset();
402 EXPECT_FALSE(tracker_->GetNetworkTime(&out_network_time, nullptr)); 470 EXPECT_EQ(NetworkTimeTracker::NETWORK_TIME_NO_SYNC,
471 tracker_->GetNetworkTime(&out_network_time, nullptr));
403 } 472 }
404 473
405 TEST_F(NetworkTimeTrackerTest, SerializeWithLongDelay) { 474 TEST_F(NetworkTimeTrackerTest, SerializeWithLongDelay) {
406 // Test that if the serialized data are more than a week old, they are 475 // Test that if the serialized data are more than a week old, they are
407 // discarded. 476 // discarded.
408 base::Time in_network_time = clock_->Now() - base::TimeDelta::FromDays(90); 477 base::Time in_network_time = clock_->Now() - base::TimeDelta::FromDays(90);
409 UpdateNetworkTime(in_network_time - latency_ / 2, resolution_, latency_, 478 UpdateNetworkTime(in_network_time - latency_ / 2, resolution_, latency_,
410 tick_clock_->NowTicks()); 479 tick_clock_->NowTicks());
411 base::Time out_network_time; 480 base::Time out_network_time;
412 EXPECT_TRUE(tracker_->GetNetworkTime(&out_network_time, nullptr)); 481 EXPECT_EQ(NetworkTimeTracker::NETWORK_TIME_AVAILABLE,
482 tracker_->GetNetworkTime(&out_network_time, nullptr));
413 AdvanceBoth(base::TimeDelta::FromDays(8)); 483 AdvanceBoth(base::TimeDelta::FromDays(8));
414 Reset(); 484 Reset();
415 EXPECT_FALSE(tracker_->GetNetworkTime(&out_network_time, nullptr)); 485 EXPECT_EQ(NetworkTimeTracker::NETWORK_TIME_NO_SYNC,
486 tracker_->GetNetworkTime(&out_network_time, nullptr));
416 } 487 }
417 488
418 TEST_F(NetworkTimeTrackerTest, SerializeWithTickClockAdvance) { 489 TEST_F(NetworkTimeTrackerTest, SerializeWithTickClockAdvance) {
419 // Test that serialized data are discarded if the wall clock and tick clock 490 // Test that serialized data are discarded if the wall clock and tick clock
420 // have not advanced consistently since data were serialized. 491 // have not advanced consistently since data were serialized.
421 base::Time in_network_time = clock_->Now() - base::TimeDelta::FromDays(90); 492 base::Time in_network_time = clock_->Now() - base::TimeDelta::FromDays(90);
422 UpdateNetworkTime(in_network_time - latency_ / 2, resolution_, latency_, 493 UpdateNetworkTime(in_network_time - latency_ / 2, resolution_, latency_,
423 tick_clock_->NowTicks()); 494 tick_clock_->NowTicks());
424 base::Time out_network_time; 495 base::Time out_network_time;
425 EXPECT_TRUE(tracker_->GetNetworkTime(&out_network_time, nullptr)); 496 EXPECT_EQ(NetworkTimeTracker::NETWORK_TIME_AVAILABLE,
497 tracker_->GetNetworkTime(&out_network_time, nullptr));
426 tick_clock_->Advance(base::TimeDelta::FromDays(1)); 498 tick_clock_->Advance(base::TimeDelta::FromDays(1));
427 Reset(); 499 Reset();
428 EXPECT_FALSE(tracker_->GetNetworkTime(&out_network_time, nullptr)); 500 EXPECT_EQ(NetworkTimeTracker::NETWORK_TIME_SYNC_LOST,
501 tracker_->GetNetworkTime(&out_network_time, nullptr));
429 } 502 }
430 503
431 TEST_F(NetworkTimeTrackerTest, SerializeWithWallClockAdvance) { 504 TEST_F(NetworkTimeTrackerTest, SerializeWithWallClockAdvance) {
432 // Test that serialized data are discarded if the wall clock and tick clock 505 // Test that serialized data are discarded if the wall clock and tick clock
433 // have not advanced consistently since data were serialized. 506 // have not advanced consistently since data were serialized.
434 base::Time in_network_time = clock_->Now() - base::TimeDelta::FromDays(90); 507 base::Time in_network_time = clock_->Now() - base::TimeDelta::FromDays(90);
435 UpdateNetworkTime(in_network_time - latency_ / 2, resolution_, latency_, 508 UpdateNetworkTime(in_network_time - latency_ / 2, resolution_, latency_,
436 tick_clock_->NowTicks()); 509 tick_clock_->NowTicks());
437 510
438 base::Time out_network_time; 511 base::Time out_network_time;
439 EXPECT_TRUE(tracker_->GetNetworkTime(&out_network_time, nullptr)); 512 EXPECT_EQ(NetworkTimeTracker::NETWORK_TIME_AVAILABLE,
513 tracker_->GetNetworkTime(&out_network_time, nullptr));
440 clock_->Advance(base::TimeDelta::FromDays(1)); 514 clock_->Advance(base::TimeDelta::FromDays(1));
441 Reset(); 515 Reset();
442 EXPECT_FALSE(tracker_->GetNetworkTime(&out_network_time, nullptr)); 516 EXPECT_EQ(NetworkTimeTracker::NETWORK_TIME_SYNC_LOST,
517 tracker_->GetNetworkTime(&out_network_time, nullptr));
443 } 518 }
444 519
445 TEST_F(NetworkTimeTrackerTest, UpdateFromNetwork) { 520 TEST_F(NetworkTimeTrackerTest, UpdateFromNetwork) {
446 base::HistogramTester histograms; 521 base::HistogramTester histograms;
447 histograms.ExpectTotalCount(kFetchFailedHistogram, 0); 522 histograms.ExpectTotalCount(kFetchFailedHistogram, 0);
448 histograms.ExpectTotalCount(kFetchValidHistogram, 0); 523 histograms.ExpectTotalCount(kFetchValidHistogram, 0);
449 524
450 base::Time out_network_time; 525 base::Time out_network_time;
451 EXPECT_FALSE(tracker_->GetNetworkTime(&out_network_time, nullptr)); 526 EXPECT_EQ(NetworkTimeTracker::NETWORK_TIME_NO_SYNC,
527 tracker_->GetNetworkTime(&out_network_time, nullptr));
452 // First query should happen soon. 528 // First query should happen soon.
453 EXPECT_EQ(base::TimeDelta::FromMinutes(0), 529 EXPECT_EQ(base::TimeDelta::FromMinutes(0),
454 tracker_->GetTimerDelayForTesting()); 530 tracker_->GetTimerDelayForTesting());
455 531
456 test_server_->RegisterRequestHandler( 532 test_server_->RegisterRequestHandler(
457 base::Bind(&NetworkTimeTrackerTest::GoodTimeResponseHandler)); 533 base::Bind(&NetworkTimeTrackerTest::GoodTimeResponseHandler));
458 EXPECT_TRUE(test_server_->Start()); 534 EXPECT_TRUE(test_server_->Start());
459 tracker_->SetTimeServerURLForTesting(test_server_->GetURL("/")); 535 tracker_->SetTimeServerURLForTesting(test_server_->GetURL("/"));
460 EXPECT_TRUE(tracker_->QueryTimeServiceForTesting()); 536 EXPECT_TRUE(tracker_->QueryTimeServiceForTesting());
461 tracker_->WaitForFetchForTesting(123123123); 537 tracker_->WaitForFetchForTesting(123123123);
462 538
463 EXPECT_TRUE(tracker_->GetNetworkTime(&out_network_time, nullptr)); 539 EXPECT_EQ(NetworkTimeTracker::NETWORK_TIME_AVAILABLE,
540 tracker_->GetNetworkTime(&out_network_time, nullptr));
464 EXPECT_EQ(base::Time::UnixEpoch() + 541 EXPECT_EQ(base::Time::UnixEpoch() +
465 base::TimeDelta::FromMilliseconds(1461621971825), 542 base::TimeDelta::FromMilliseconds(1461621971825),
466 out_network_time); 543 out_network_time);
467 // Should see no backoff in the success case. 544 // Should see no backoff in the success case.
468 EXPECT_EQ(base::TimeDelta::FromMinutes(60), 545 EXPECT_EQ(base::TimeDelta::FromMinutes(60),
469 tracker_->GetTimerDelayForTesting()); 546 tracker_->GetTimerDelayForTesting());
470 547
471 histograms.ExpectTotalCount(kFetchFailedHistogram, 0); 548 histograms.ExpectTotalCount(kFetchFailedHistogram, 0);
472 histograms.ExpectTotalCount(kFetchValidHistogram, 1); 549 histograms.ExpectTotalCount(kFetchValidHistogram, 1);
473 histograms.ExpectBucketCount(kFetchValidHistogram, true, 1); 550 histograms.ExpectBucketCount(kFetchValidHistogram, true, 1);
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after
516 histograms.ExpectTotalCount(kFetchValidHistogram, 0); 593 histograms.ExpectTotalCount(kFetchValidHistogram, 0);
517 594
518 test_server_->RegisterRequestHandler( 595 test_server_->RegisterRequestHandler(
519 base::Bind(&NetworkTimeTrackerTest::BadSignatureResponseHandler)); 596 base::Bind(&NetworkTimeTrackerTest::BadSignatureResponseHandler));
520 EXPECT_TRUE(test_server_->Start()); 597 EXPECT_TRUE(test_server_->Start());
521 tracker_->SetTimeServerURLForTesting(test_server_->GetURL("/")); 598 tracker_->SetTimeServerURLForTesting(test_server_->GetURL("/"));
522 EXPECT_TRUE(tracker_->QueryTimeServiceForTesting()); 599 EXPECT_TRUE(tracker_->QueryTimeServiceForTesting());
523 tracker_->WaitForFetchForTesting(123123123); 600 tracker_->WaitForFetchForTesting(123123123);
524 601
525 base::Time out_network_time; 602 base::Time out_network_time;
526 EXPECT_FALSE(tracker_->GetNetworkTime(&out_network_time, nullptr)); 603 EXPECT_EQ(NetworkTimeTracker::NETWORK_TIME_NO_SYNC,
604 tracker_->GetNetworkTime(&out_network_time, nullptr));
527 EXPECT_EQ(base::TimeDelta::FromMinutes(120), 605 EXPECT_EQ(base::TimeDelta::FromMinutes(120),
528 tracker_->GetTimerDelayForTesting()); 606 tracker_->GetTimerDelayForTesting());
529 607
530 histograms.ExpectTotalCount(kFetchFailedHistogram, 0); 608 histograms.ExpectTotalCount(kFetchFailedHistogram, 0);
531 histograms.ExpectTotalCount(kFetchValidHistogram, 1); 609 histograms.ExpectTotalCount(kFetchValidHistogram, 1);
532 histograms.ExpectBucketCount(kFetchValidHistogram, false, 1); 610 histograms.ExpectBucketCount(kFetchValidHistogram, false, 1);
533 } 611 }
534 612
535 static const uint8_t kDevKeyPubBytes[] = { 613 static const uint8_t kDevKeyPubBytes[] = {
536 0x30, 0x59, 0x30, 0x13, 0x06, 0x07, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x02, 614 0x30, 0x59, 0x30, 0x13, 0x06, 0x07, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x02,
(...skipping 13 matching lines...) Expand all
550 test_server_->RegisterRequestHandler( 628 test_server_->RegisterRequestHandler(
551 base::Bind(&NetworkTimeTrackerTest::BadDataResponseHandler)); 629 base::Bind(&NetworkTimeTrackerTest::BadDataResponseHandler));
552 EXPECT_TRUE(test_server_->Start()); 630 EXPECT_TRUE(test_server_->Start());
553 base::StringPiece key = {reinterpret_cast<const char*>(kDevKeyPubBytes), 631 base::StringPiece key = {reinterpret_cast<const char*>(kDevKeyPubBytes),
554 sizeof(kDevKeyPubBytes)}; 632 sizeof(kDevKeyPubBytes)};
555 tracker_->SetPublicKeyForTesting(key); 633 tracker_->SetPublicKeyForTesting(key);
556 tracker_->SetTimeServerURLForTesting(test_server_->GetURL("/")); 634 tracker_->SetTimeServerURLForTesting(test_server_->GetURL("/"));
557 EXPECT_TRUE(tracker_->QueryTimeServiceForTesting()); 635 EXPECT_TRUE(tracker_->QueryTimeServiceForTesting());
558 tracker_->WaitForFetchForTesting(123123123); 636 tracker_->WaitForFetchForTesting(123123123);
559 base::Time out_network_time; 637 base::Time out_network_time;
560 EXPECT_FALSE(tracker_->GetNetworkTime(&out_network_time, nullptr)); 638 EXPECT_EQ(NetworkTimeTracker::NETWORK_TIME_NO_SYNC,
639 tracker_->GetNetworkTime(&out_network_time, nullptr));
561 EXPECT_EQ(base::TimeDelta::FromMinutes(120), 640 EXPECT_EQ(base::TimeDelta::FromMinutes(120),
562 tracker_->GetTimerDelayForTesting()); 641 tracker_->GetTimerDelayForTesting());
563 642
564 histograms.ExpectTotalCount(kFetchFailedHistogram, 0); 643 histograms.ExpectTotalCount(kFetchFailedHistogram, 0);
565 histograms.ExpectTotalCount(kFetchValidHistogram, 1); 644 histograms.ExpectTotalCount(kFetchValidHistogram, 1);
566 histograms.ExpectBucketCount(kFetchValidHistogram, false, 1); 645 histograms.ExpectBucketCount(kFetchValidHistogram, false, 1);
567 } 646 }
568 647
569 TEST_F(NetworkTimeTrackerTest, UpdateFromNetworkServerError) { 648 TEST_F(NetworkTimeTrackerTest, UpdateFromNetworkServerError) {
570 base::HistogramTester histograms; 649 base::HistogramTester histograms;
571 histograms.ExpectTotalCount(kFetchFailedHistogram, 0); 650 histograms.ExpectTotalCount(kFetchFailedHistogram, 0);
572 histograms.ExpectTotalCount(kFetchValidHistogram, 0); 651 histograms.ExpectTotalCount(kFetchValidHistogram, 0);
573 652
574 test_server_->RegisterRequestHandler( 653 test_server_->RegisterRequestHandler(
575 base::Bind(&NetworkTimeTrackerTest::ServerErrorResponseHandler)); 654 base::Bind(&NetworkTimeTrackerTest::ServerErrorResponseHandler));
576 EXPECT_TRUE(test_server_->Start()); 655 EXPECT_TRUE(test_server_->Start());
577 tracker_->SetTimeServerURLForTesting(test_server_->GetURL("/")); 656 tracker_->SetTimeServerURLForTesting(test_server_->GetURL("/"));
578 EXPECT_TRUE(tracker_->QueryTimeServiceForTesting()); 657 EXPECT_TRUE(tracker_->QueryTimeServiceForTesting());
579 tracker_->WaitForFetchForTesting(123123123); 658 tracker_->WaitForFetchForTesting(123123123);
580 659
581 base::Time out_network_time; 660 base::Time out_network_time;
582 EXPECT_FALSE(tracker_->GetNetworkTime(&out_network_time, nullptr)); 661 EXPECT_EQ(NetworkTimeTracker::NETWORK_TIME_NO_SYNC,
662 tracker_->GetNetworkTime(&out_network_time, nullptr));
583 // Should see backoff in the error case. 663 // Should see backoff in the error case.
584 EXPECT_EQ(base::TimeDelta::FromMinutes(120), 664 EXPECT_EQ(base::TimeDelta::FromMinutes(120),
585 tracker_->GetTimerDelayForTesting()); 665 tracker_->GetTimerDelayForTesting());
586 666
587 histograms.ExpectTotalCount(kFetchFailedHistogram, 1); 667 histograms.ExpectTotalCount(kFetchFailedHistogram, 1);
588 // There was no network error, so the histogram is recorded as 668 // There was no network error, so the histogram is recorded as
589 // net::OK, indicating that the connection succeeded but there was a 669 // net::OK, indicating that the connection succeeded but there was a
590 // non-200 HTTP status code. 670 // non-200 HTTP status code.
591 histograms.ExpectBucketCount(kFetchFailedHistogram, net::OK, 1); 671 histograms.ExpectBucketCount(kFetchFailedHistogram, net::OK, 1);
592 histograms.ExpectTotalCount(kFetchValidHistogram, 0); 672 histograms.ExpectTotalCount(kFetchValidHistogram, 0);
593 } 673 }
594 674
595 TEST_F(NetworkTimeTrackerTest, UpdateFromNetworkNetworkError) { 675 TEST_F(NetworkTimeTrackerTest, UpdateFromNetworkNetworkError) {
596 base::HistogramTester histograms; 676 base::HistogramTester histograms;
597 histograms.ExpectTotalCount(kFetchFailedHistogram, 0); 677 histograms.ExpectTotalCount(kFetchFailedHistogram, 0);
598 histograms.ExpectTotalCount(kFetchValidHistogram, 0); 678 histograms.ExpectTotalCount(kFetchValidHistogram, 0);
599 679
600 test_server_->RegisterRequestHandler( 680 test_server_->RegisterRequestHandler(
601 base::Bind(&NetworkTimeTrackerTest::NetworkErrorResponseHandler)); 681 base::Bind(&NetworkTimeTrackerTest::NetworkErrorResponseHandler));
602 EXPECT_TRUE(test_server_->Start()); 682 EXPECT_TRUE(test_server_->Start());
603 tracker_->SetTimeServerURLForTesting(test_server_->GetURL("/")); 683 tracker_->SetTimeServerURLForTesting(test_server_->GetURL("/"));
604 EXPECT_TRUE(tracker_->QueryTimeServiceForTesting()); 684 EXPECT_TRUE(tracker_->QueryTimeServiceForTesting());
605 tracker_->WaitForFetchForTesting(123123123); 685 tracker_->WaitForFetchForTesting(123123123);
606 686
607 base::Time out_network_time; 687 base::Time out_network_time;
608 EXPECT_FALSE(tracker_->GetNetworkTime(&out_network_time, nullptr)); 688 EXPECT_EQ(NetworkTimeTracker::NETWORK_TIME_NO_SYNC,
689 tracker_->GetNetworkTime(&out_network_time, nullptr));
609 // Should see backoff in the error case. 690 // Should see backoff in the error case.
610 EXPECT_EQ(base::TimeDelta::FromMinutes(120), 691 EXPECT_EQ(base::TimeDelta::FromMinutes(120),
611 tracker_->GetTimerDelayForTesting()); 692 tracker_->GetTimerDelayForTesting());
612 693
613 histograms.ExpectTotalCount(kFetchFailedHistogram, 1); 694 histograms.ExpectTotalCount(kFetchFailedHistogram, 1);
614 histograms.ExpectBucketCount(kFetchFailedHistogram, -net::ERR_EMPTY_RESPONSE, 695 histograms.ExpectBucketCount(kFetchFailedHistogram, -net::ERR_EMPTY_RESPONSE,
615 1); 696 1);
616 histograms.ExpectTotalCount(kFetchValidHistogram, 0); 697 histograms.ExpectTotalCount(kFetchValidHistogram, 0);
617 } 698 }
618 699
619 TEST_F(NetworkTimeTrackerTest, UpdateFromNetworkLargeResponse) { 700 TEST_F(NetworkTimeTrackerTest, UpdateFromNetworkLargeResponse) {
620 base::HistogramTester histograms; 701 base::HistogramTester histograms;
621 histograms.ExpectTotalCount(kFetchFailedHistogram, 0); 702 histograms.ExpectTotalCount(kFetchFailedHistogram, 0);
622 histograms.ExpectTotalCount(kFetchValidHistogram, 0); 703 histograms.ExpectTotalCount(kFetchValidHistogram, 0);
623 704
624 test_server_->RegisterRequestHandler( 705 test_server_->RegisterRequestHandler(
625 base::Bind(&NetworkTimeTrackerTest::GoodTimeResponseHandler)); 706 base::Bind(&NetworkTimeTrackerTest::GoodTimeResponseHandler));
626 EXPECT_TRUE(test_server_->Start()); 707 EXPECT_TRUE(test_server_->Start());
627 tracker_->SetTimeServerURLForTesting(test_server_->GetURL("/")); 708 tracker_->SetTimeServerURLForTesting(test_server_->GetURL("/"));
628 709
629 base::Time out_network_time; 710 base::Time out_network_time;
630 711
631 tracker_->SetMaxResponseSizeForTesting(3); 712 tracker_->SetMaxResponseSizeForTesting(3);
632 EXPECT_TRUE(tracker_->QueryTimeServiceForTesting()); 713 EXPECT_TRUE(tracker_->QueryTimeServiceForTesting());
633 tracker_->WaitForFetchForTesting(123123123); 714 tracker_->WaitForFetchForTesting(123123123);
634 EXPECT_FALSE(tracker_->GetNetworkTime(&out_network_time, nullptr)); 715 EXPECT_EQ(NetworkTimeTracker::NETWORK_TIME_NO_SYNC,
716 tracker_->GetNetworkTime(&out_network_time, nullptr));
635 717
636 histograms.ExpectTotalCount(kFetchFailedHistogram, 1); 718 histograms.ExpectTotalCount(kFetchFailedHistogram, 1);
637 histograms.ExpectTotalCount(kFetchValidHistogram, 0); 719 histograms.ExpectTotalCount(kFetchValidHistogram, 0);
638 720
639 tracker_->SetMaxResponseSizeForTesting(1024); 721 tracker_->SetMaxResponseSizeForTesting(1024);
640 EXPECT_TRUE(tracker_->QueryTimeServiceForTesting()); 722 EXPECT_TRUE(tracker_->QueryTimeServiceForTesting());
641 tracker_->WaitForFetchForTesting(123123123); 723 tracker_->WaitForFetchForTesting(123123123);
642 EXPECT_TRUE(tracker_->GetNetworkTime(&out_network_time, nullptr)); 724 EXPECT_EQ(NetworkTimeTracker::NETWORK_TIME_AVAILABLE,
725 tracker_->GetNetworkTime(&out_network_time, nullptr));
643 726
644 histograms.ExpectTotalCount(kFetchFailedHistogram, 1); 727 histograms.ExpectTotalCount(kFetchFailedHistogram, 1);
645 histograms.ExpectTotalCount(kFetchValidHistogram, 1); 728 histograms.ExpectTotalCount(kFetchValidHistogram, 1);
646 histograms.ExpectBucketCount(kFetchValidHistogram, true, 1); 729 histograms.ExpectBucketCount(kFetchValidHistogram, true, 1);
647 } 730 }
648 731
649 } // namespace network_time 732 } // namespace network_time
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698