Chromium Code Reviews| 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/network_time/network_time_tracker.h" | 5 #include "components/network_time/network_time_tracker.h" |
| 6 | 6 |
| 7 #include <memory> | 7 #include <memory> |
| 8 | 8 |
| 9 #include "base/compiler_specific.h" | 9 #include "base/compiler_specific.h" |
| 10 #include "base/message_loop/message_loop.h" | |
| 11 #include "base/strings/stringprintf.h" | |
| 10 #include "base/test/simple_test_clock.h" | 12 #include "base/test/simple_test_clock.h" |
| 11 #include "base/test/simple_test_tick_clock.h" | 13 #include "base/test/simple_test_tick_clock.h" |
| 14 #include "components/client_update_protocol/ecdsa.h" | |
| 12 #include "components/network_time/network_time_pref_names.h" | 15 #include "components/network_time/network_time_pref_names.h" |
| 13 #include "components/prefs/testing_pref_service.h" | 16 #include "components/prefs/testing_pref_service.h" |
| 17 #include "net/http/http_response_headers.h" | |
| 18 #include "net/url_request/test_url_fetcher_factory.h" | |
| 19 #include "net/url_request/url_request_test_util.h" | |
| 14 #include "testing/gtest/include/gtest/gtest.h" | 20 #include "testing/gtest/include/gtest/gtest.h" |
| 15 | 21 |
| 16 namespace network_time { | 22 namespace network_time { |
| 17 | 23 |
| 18 class NetworkTimeTrackerTest : public testing::Test { | 24 class NetworkTimeTrackerTest : public testing::Test { |
| 19 public: | 25 public: |
| 20 ~NetworkTimeTrackerTest() override {} | 26 ~NetworkTimeTrackerTest() override {} |
| 21 | 27 |
| 22 void SetUp() override { | 28 void SetUp() override { |
|
Ryan Sleevi
2016/04/28 01:08:34
Please see https://github.com/google/googletest/bl
mab
2016/04/28 02:29:04
Done.
| |
| 29 url_fetcher_factory_.reset(new net::TestURLFetcherFactory()); | |
| 23 NetworkTimeTracker::RegisterPrefs(pref_service_.registry()); | 30 NetworkTimeTracker::RegisterPrefs(pref_service_.registry()); |
| 24 | 31 |
| 25 clock_ = new base::SimpleTestClock(); | 32 clock_ = new base::SimpleTestClock(); |
| 26 tick_clock_ = new base::SimpleTestTickClock(); | 33 tick_clock_ = new base::SimpleTestTickClock(); |
| 27 // Do this to be sure that |is_null| returns false. | 34 // Do this to be sure that |is_null| returns false. |
| 28 clock_->Advance(base::TimeDelta::FromDays(111)); | 35 clock_->Advance(base::TimeDelta::FromDays(111)); |
| 29 tick_clock_->Advance(base::TimeDelta::FromDays(222)); | 36 tick_clock_->Advance(base::TimeDelta::FromDays(222)); |
| 30 | 37 |
| 31 tracker_.reset(new NetworkTimeTracker( | 38 tracker_.reset(new NetworkTimeTracker( |
| 32 std::unique_ptr<base::Clock>(clock_), | 39 std::unique_ptr<base::Clock>(clock_), |
| 33 std::unique_ptr<base::TickClock>(tick_clock_), &pref_service_)); | 40 std::unique_ptr<base::TickClock>(tick_clock_), &pref_service_, |
| 41 new net::TestURLRequestContextGetter(message_loop_.task_runner()))); | |
|
Ryan Sleevi
2016/04/28 01:08:34
This means the TestURLRequestContextGetter is leak
mab
2016/04/28 02:29:04
I guess leaks are not detected automatically by th
| |
| 34 | 42 |
| 35 // Can not be smaller than 15, it's the NowFromSystemTime() resolution. | 43 // Can not be smaller than 15, it's the NowFromSystemTime() resolution. |
| 36 resolution_ = base::TimeDelta::FromMilliseconds(17); | 44 resolution_ = base::TimeDelta::FromMilliseconds(17); |
| 37 latency_ = base::TimeDelta::FromMilliseconds(50); | 45 latency_ = base::TimeDelta::FromMilliseconds(50); |
| 38 adjustment_ = 7 * base::TimeDelta::FromMilliseconds(kTicksResolutionMs); | 46 adjustment_ = 7 * base::TimeDelta::FromMilliseconds(kTicksResolutionMs); |
| 39 } | 47 } |
| 40 | 48 |
| 41 // Replaces |tracker_| with a new object, while preserving the | 49 // Replaces |tracker_| with a new object, while preserving the |
| 42 // testing clocks. | 50 // testing clocks. |
| 43 void Reset() { | 51 void Reset() { |
| 44 base::SimpleTestClock* new_clock = new base::SimpleTestClock(); | 52 base::SimpleTestClock* new_clock = new base::SimpleTestClock(); |
| 45 new_clock->SetNow(clock_->Now()); | 53 new_clock->SetNow(clock_->Now()); |
| 46 base::SimpleTestTickClock* new_tick_clock = new base::SimpleTestTickClock(); | 54 base::SimpleTestTickClock* new_tick_clock = new base::SimpleTestTickClock(); |
| 47 new_tick_clock->SetNowTicks(tick_clock_->NowTicks()); | 55 new_tick_clock->SetNowTicks(tick_clock_->NowTicks()); |
| 48 clock_ = new_clock; | 56 clock_ = new_clock; |
| 49 tick_clock_= new_tick_clock; | 57 tick_clock_= new_tick_clock; |
| 50 tracker_.reset(new NetworkTimeTracker( | 58 tracker_.reset(new NetworkTimeTracker( |
| 51 std::unique_ptr<base::Clock>(clock_), | 59 std::unique_ptr<base::Clock>(clock_), |
| 52 std::unique_ptr<base::TickClock>(tick_clock_), &pref_service_)); | 60 std::unique_ptr<base::TickClock>(tick_clock_), &pref_service_, |
| 61 new net::TestURLRequestContextGetter(message_loop_.task_runner()))); | |
| 62 } | |
| 63 | |
| 64 // Helper for verifying signatures over time server responses. Sets the | |
| 65 // client's internal nonce, which is normally randomly generated, to a | |
| 66 // predictable value. | |
| 67 void OverrideNonce(uint32_t nonce) { | |
| 68 tracker_->query_signer_->request_query_cup2key_ = | |
| 69 base::StringPrintf("%d:%u", 1 /* key version */, nonce); | |
| 53 } | 70 } |
| 54 | 71 |
| 55 // Updates the notifier's time with the specified parameters. | 72 // Updates the notifier's time with the specified parameters. |
| 56 void UpdateNetworkTime(const base::Time& network_time, | 73 void UpdateNetworkTime(const base::Time& network_time, |
| 57 const base::TimeDelta& resolution, | 74 const base::TimeDelta& resolution, |
| 58 const base::TimeDelta& latency, | 75 const base::TimeDelta& latency, |
| 59 const base::TimeTicks& post_time) { | 76 const base::TimeTicks& post_time) { |
| 60 tracker_->UpdateNetworkTime( | 77 tracker_->UpdateNetworkTime( |
| 61 network_time, resolution, latency, post_time); | 78 network_time, resolution, latency, post_time); |
| 62 } | 79 } |
| 63 | 80 |
| 64 // Advances both the system clock and the tick clock. This should be used for | 81 // Advances both the system clock and the tick clock. This should be used for |
| 65 // the normal passage of time, i.e. when neither clock is doing anything odd. | 82 // the normal passage of time, i.e. when neither clock is doing anything odd. |
| 66 void AdvanceBoth(const base::TimeDelta& delta) { | 83 void AdvanceBoth(const base::TimeDelta& delta) { |
| 67 tick_clock_->Advance(delta); | 84 tick_clock_->Advance(delta); |
| 68 clock_->Advance(delta); | 85 clock_->Advance(delta); |
| 69 } | 86 } |
| 70 | 87 |
| 71 protected: | 88 protected: |
| 89 base::MessageLoop message_loop_; | |
| 72 base::TimeDelta resolution_; | 90 base::TimeDelta resolution_; |
| 73 base::TimeDelta latency_; | 91 base::TimeDelta latency_; |
| 74 base::TimeDelta adjustment_; | 92 base::TimeDelta adjustment_; |
| 75 base::SimpleTestClock* clock_; | 93 base::SimpleTestClock* clock_; |
| 76 base::SimpleTestTickClock* tick_clock_; | 94 base::SimpleTestTickClock* tick_clock_; |
| 77 TestingPrefServiceSimple pref_service_; | 95 TestingPrefServiceSimple pref_service_; |
| 78 std::unique_ptr<NetworkTimeTracker> tracker_; | 96 std::unique_ptr<NetworkTimeTracker> tracker_; |
| 97 std::unique_ptr<net::TestURLFetcherFactory> url_fetcher_factory_; | |
| 79 }; | 98 }; |
| 80 | 99 |
| 81 TEST_F(NetworkTimeTrackerTest, Uninitialized) { | 100 TEST_F(NetworkTimeTrackerTest, Uninitialized) { |
| 82 base::Time network_time; | 101 base::Time network_time; |
| 83 base::TimeDelta uncertainty; | 102 base::TimeDelta uncertainty; |
| 84 EXPECT_FALSE(tracker_->GetNetworkTime(&network_time, &uncertainty)); | 103 EXPECT_FALSE(tracker_->GetNetworkTime(&network_time, &uncertainty)); |
| 85 } | 104 } |
| 86 | 105 |
| 87 TEST_F(NetworkTimeTrackerTest, LongPostingDelay) { | 106 TEST_F(NetworkTimeTrackerTest, LongPostingDelay) { |
| 88 // The request arrives at the server, which records the time. Advance the | 107 // The request arrives at the server, which records the time. Advance the |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 124 EXPECT_GT(out_network_time + uncertainty / 2, clock_->Now()); | 143 EXPECT_GT(out_network_time + uncertainty / 2, clock_->Now()); |
| 125 } | 144 } |
| 126 | 145 |
| 127 TEST_F(NetworkTimeTrackerTest, ClockIsWack) { | 146 TEST_F(NetworkTimeTrackerTest, ClockIsWack) { |
| 128 // Now let's assume the system clock is completely wrong. | 147 // Now let's assume the system clock is completely wrong. |
| 129 base::Time in_network_time = clock_->Now() - base::TimeDelta::FromDays(90); | 148 base::Time in_network_time = clock_->Now() - base::TimeDelta::FromDays(90); |
| 130 UpdateNetworkTime(in_network_time - latency_ / 2, resolution_, latency_, | 149 UpdateNetworkTime(in_network_time - latency_ / 2, resolution_, latency_, |
| 131 tick_clock_->NowTicks()); | 150 tick_clock_->NowTicks()); |
| 132 | 151 |
| 133 base::Time out_network_time; | 152 base::Time out_network_time; |
| 134 EXPECT_TRUE(tracker_->GetNetworkTime(&out_network_time, NULL)); | 153 EXPECT_TRUE(tracker_->GetNetworkTime(&out_network_time, nullptr)); |
| 135 EXPECT_EQ(in_network_time, out_network_time); | 154 EXPECT_EQ(in_network_time, out_network_time); |
| 136 } | 155 } |
| 137 | 156 |
| 138 TEST_F(NetworkTimeTrackerTest, ClocksDivergeSlightly) { | 157 TEST_F(NetworkTimeTrackerTest, ClocksDivergeSlightly) { |
| 139 // The two clocks are allowed to diverge a little bit. | 158 // The two clocks are allowed to diverge a little bit. |
| 140 base::Time in_network_time = clock_->Now(); | 159 base::Time in_network_time = clock_->Now(); |
| 141 UpdateNetworkTime(in_network_time - latency_ / 2, resolution_, latency_, | 160 UpdateNetworkTime(in_network_time - latency_ / 2, resolution_, latency_, |
| 142 tick_clock_->NowTicks()); | 161 tick_clock_->NowTicks()); |
| 143 | 162 |
| 144 base::TimeDelta small = base::TimeDelta::FromSeconds(30); | 163 base::TimeDelta small = base::TimeDelta::FromSeconds(30); |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 178 EXPECT_EQ(resolution_ + latency_ + adjustment_, uncertainty); | 197 EXPECT_EQ(resolution_ + latency_ + adjustment_, uncertainty); |
| 179 } | 198 } |
| 180 | 199 |
| 181 TEST_F(NetworkTimeTrackerTest, SpringForward) { | 200 TEST_F(NetworkTimeTrackerTest, SpringForward) { |
| 182 // Simulate the wall clock advancing faster than the tick clock. | 201 // Simulate the wall clock advancing faster than the tick clock. |
| 183 UpdateNetworkTime(clock_->Now(), resolution_, latency_, | 202 UpdateNetworkTime(clock_->Now(), resolution_, latency_, |
| 184 tick_clock_->NowTicks()); | 203 tick_clock_->NowTicks()); |
| 185 tick_clock_->Advance(base::TimeDelta::FromSeconds(1)); | 204 tick_clock_->Advance(base::TimeDelta::FromSeconds(1)); |
| 186 clock_->Advance(base::TimeDelta::FromDays(1)); | 205 clock_->Advance(base::TimeDelta::FromDays(1)); |
| 187 base::Time out_network_time; | 206 base::Time out_network_time; |
| 188 EXPECT_FALSE(tracker_->GetNetworkTime(&out_network_time, NULL)); | 207 EXPECT_FALSE(tracker_->GetNetworkTime(&out_network_time, nullptr)); |
| 189 } | 208 } |
| 190 | 209 |
| 191 TEST_F(NetworkTimeTrackerTest, FallBack) { | 210 TEST_F(NetworkTimeTrackerTest, FallBack) { |
| 192 // Simulate the wall clock running backward. | 211 // Simulate the wall clock running backward. |
| 193 UpdateNetworkTime(clock_->Now(), resolution_, latency_, | 212 UpdateNetworkTime(clock_->Now(), resolution_, latency_, |
| 194 tick_clock_->NowTicks()); | 213 tick_clock_->NowTicks()); |
| 195 tick_clock_->Advance(base::TimeDelta::FromSeconds(1)); | 214 tick_clock_->Advance(base::TimeDelta::FromSeconds(1)); |
| 196 clock_->Advance(base::TimeDelta::FromDays(-1)); | 215 clock_->Advance(base::TimeDelta::FromDays(-1)); |
| 197 base::Time out_network_time; | 216 base::Time out_network_time; |
| 198 EXPECT_FALSE(tracker_->GetNetworkTime(&out_network_time, NULL)); | 217 EXPECT_FALSE(tracker_->GetNetworkTime(&out_network_time, nullptr)); |
| 199 } | 218 } |
| 200 | 219 |
| 201 TEST_F(NetworkTimeTrackerTest, SuspendAndResume) { | 220 TEST_F(NetworkTimeTrackerTest, SuspendAndResume) { |
| 202 // Simulate the wall clock advancing while the tick clock stands still, as | 221 // Simulate the wall clock advancing while the tick clock stands still, as |
| 203 // would happen in a suspend+resume cycle. | 222 // would happen in a suspend+resume cycle. |
| 204 UpdateNetworkTime(clock_->Now(), resolution_, latency_, | 223 UpdateNetworkTime(clock_->Now(), resolution_, latency_, |
| 205 tick_clock_->NowTicks()); | 224 tick_clock_->NowTicks()); |
| 206 clock_->Advance(base::TimeDelta::FromHours(1)); | 225 clock_->Advance(base::TimeDelta::FromHours(1)); |
| 207 base::Time out_network_time; | 226 base::Time out_network_time; |
| 208 EXPECT_FALSE(tracker_->GetNetworkTime(&out_network_time, NULL)); | 227 EXPECT_FALSE(tracker_->GetNetworkTime(&out_network_time, nullptr)); |
| 209 } | 228 } |
| 210 | 229 |
| 211 TEST_F(NetworkTimeTrackerTest, Serialize) { | 230 TEST_F(NetworkTimeTrackerTest, Serialize) { |
| 212 // Test that we can serialize and deserialize state and get consistent | 231 // Test that we can serialize and deserialize state and get consistent |
| 213 // results. | 232 // results. |
| 214 base::Time in_network_time = clock_->Now() - base::TimeDelta::FromDays(90); | 233 base::Time in_network_time = clock_->Now() - base::TimeDelta::FromDays(90); |
| 215 UpdateNetworkTime(in_network_time - latency_ / 2, resolution_, latency_, | 234 UpdateNetworkTime(in_network_time - latency_ / 2, resolution_, latency_, |
| 216 tick_clock_->NowTicks()); | 235 tick_clock_->NowTicks()); |
| 217 base::Time out_network_time; | 236 base::Time out_network_time; |
| 218 base::TimeDelta out_uncertainty; | 237 base::TimeDelta out_uncertainty; |
| (...skipping 11 matching lines...) Expand all Loading... | |
| 230 } | 249 } |
| 231 | 250 |
| 232 TEST_F(NetworkTimeTrackerTest, DeserializeOldFormat) { | 251 TEST_F(NetworkTimeTrackerTest, DeserializeOldFormat) { |
| 233 // Test that deserializing old data (which do not record the uncertainty and | 252 // Test that deserializing old data (which do not record the uncertainty and |
| 234 // tick clock) causes the serialized data to be ignored. | 253 // tick clock) causes the serialized data to be ignored. |
| 235 base::Time in_network_time = clock_->Now() - base::TimeDelta::FromDays(90); | 254 base::Time in_network_time = clock_->Now() - base::TimeDelta::FromDays(90); |
| 236 UpdateNetworkTime(in_network_time - latency_ / 2, resolution_, latency_, | 255 UpdateNetworkTime(in_network_time - latency_ / 2, resolution_, latency_, |
| 237 tick_clock_->NowTicks()); | 256 tick_clock_->NowTicks()); |
| 238 | 257 |
| 239 base::Time out_network_time; | 258 base::Time out_network_time; |
| 240 EXPECT_TRUE(tracker_->GetNetworkTime(&out_network_time, NULL)); | 259 EXPECT_TRUE(tracker_->GetNetworkTime(&out_network_time, nullptr)); |
| 241 double local, network; | 260 double local, network; |
| 242 const base::DictionaryValue* saved_prefs = | 261 const base::DictionaryValue* saved_prefs = |
| 243 pref_service_.GetDictionary(prefs::kNetworkTimeMapping); | 262 pref_service_.GetDictionary(prefs::kNetworkTimeMapping); |
| 244 saved_prefs->GetDouble("local", &local); | 263 saved_prefs->GetDouble("local", &local); |
| 245 saved_prefs->GetDouble("network", &network); | 264 saved_prefs->GetDouble("network", &network); |
| 246 base::DictionaryValue prefs; | 265 base::DictionaryValue prefs; |
| 247 prefs.SetDouble("local", local); | 266 prefs.SetDouble("local", local); |
| 248 prefs.SetDouble("network", network); | 267 prefs.SetDouble("network", network); |
| 249 pref_service_.Set(prefs::kNetworkTimeMapping, prefs); | 268 pref_service_.Set(prefs::kNetworkTimeMapping, prefs); |
| 250 Reset(); | 269 Reset(); |
| 251 EXPECT_FALSE(tracker_->GetNetworkTime(&out_network_time, NULL)); | 270 EXPECT_FALSE(tracker_->GetNetworkTime(&out_network_time, nullptr)); |
| 252 } | 271 } |
| 253 | 272 |
| 254 TEST_F(NetworkTimeTrackerTest, SerializeWithLongDelay) { | 273 TEST_F(NetworkTimeTrackerTest, SerializeWithLongDelay) { |
| 255 // Test that if the serialized data are more than a week old, they are | 274 // Test that if the serialized data are more than a week old, they are |
| 256 // discarded. | 275 // discarded. |
| 257 base::Time in_network_time = clock_->Now() - base::TimeDelta::FromDays(90); | 276 base::Time in_network_time = clock_->Now() - base::TimeDelta::FromDays(90); |
| 258 UpdateNetworkTime(in_network_time - latency_ / 2, resolution_, latency_, | 277 UpdateNetworkTime(in_network_time - latency_ / 2, resolution_, latency_, |
| 259 tick_clock_->NowTicks()); | 278 tick_clock_->NowTicks()); |
| 260 base::Time out_network_time; | 279 base::Time out_network_time; |
| 261 EXPECT_TRUE(tracker_->GetNetworkTime(&out_network_time, NULL)); | 280 EXPECT_TRUE(tracker_->GetNetworkTime(&out_network_time, nullptr)); |
| 262 AdvanceBoth(base::TimeDelta::FromDays(8)); | 281 AdvanceBoth(base::TimeDelta::FromDays(8)); |
| 263 Reset(); | 282 Reset(); |
| 264 EXPECT_FALSE(tracker_->GetNetworkTime(&out_network_time, NULL)); | 283 EXPECT_FALSE(tracker_->GetNetworkTime(&out_network_time, nullptr)); |
| 265 } | 284 } |
| 266 | 285 |
| 267 TEST_F(NetworkTimeTrackerTest, SerializeWithTickClockAdvance) { | 286 TEST_F(NetworkTimeTrackerTest, SerializeWithTickClockAdvance) { |
| 268 // Test that serialized data are discarded if the wall clock and tick clock | 287 // Test that serialized data are discarded if the wall clock and tick clock |
| 269 // have not advanced consistently since data were serialized. | 288 // have not advanced consistently since data were serialized. |
| 270 base::Time in_network_time = clock_->Now() - base::TimeDelta::FromDays(90); | 289 base::Time in_network_time = clock_->Now() - base::TimeDelta::FromDays(90); |
| 271 UpdateNetworkTime(in_network_time - latency_ / 2, resolution_, latency_, | 290 UpdateNetworkTime(in_network_time - latency_ / 2, resolution_, latency_, |
| 272 tick_clock_->NowTicks()); | 291 tick_clock_->NowTicks()); |
| 273 base::Time out_network_time; | 292 base::Time out_network_time; |
| 274 EXPECT_TRUE(tracker_->GetNetworkTime(&out_network_time, NULL)); | 293 EXPECT_TRUE(tracker_->GetNetworkTime(&out_network_time, nullptr)); |
| 275 tick_clock_->Advance(base::TimeDelta::FromDays(1)); | 294 tick_clock_->Advance(base::TimeDelta::FromDays(1)); |
| 276 Reset(); | 295 Reset(); |
| 277 EXPECT_FALSE(tracker_->GetNetworkTime(&out_network_time, NULL)); | 296 EXPECT_FALSE(tracker_->GetNetworkTime(&out_network_time, nullptr)); |
| 278 } | 297 } |
| 279 | 298 |
| 280 TEST_F(NetworkTimeTrackerTest, SerializeWithWallClockAdvance) { | 299 TEST_F(NetworkTimeTrackerTest, SerializeWithWallClockAdvance) { |
| 281 // Test that serialized data are discarded if the wall clock and tick clock | 300 // Test that serialized data are discarded if the wall clock and tick clock |
| 282 // have not advanced consistently since data were serialized. | 301 // have not advanced consistently since data were serialized. |
| 283 base::Time in_network_time = clock_->Now() - base::TimeDelta::FromDays(90); | 302 base::Time in_network_time = clock_->Now() - base::TimeDelta::FromDays(90); |
| 284 UpdateNetworkTime(in_network_time - latency_ / 2, resolution_, latency_, | 303 UpdateNetworkTime(in_network_time - latency_ / 2, resolution_, latency_, |
| 285 tick_clock_->NowTicks()); | 304 tick_clock_->NowTicks()); |
| 286 | 305 |
| 287 base::Time out_network_time; | 306 base::Time out_network_time; |
| 288 EXPECT_TRUE(tracker_->GetNetworkTime(&out_network_time, NULL)); | 307 EXPECT_TRUE(tracker_->GetNetworkTime(&out_network_time, nullptr)); |
| 289 clock_->Advance(base::TimeDelta::FromDays(1)); | 308 clock_->Advance(base::TimeDelta::FromDays(1)); |
| 290 Reset(); | 309 Reset(); |
| 291 EXPECT_FALSE(tracker_->GetNetworkTime(&out_network_time, NULL)); | 310 EXPECT_FALSE(tracker_->GetNetworkTime(&out_network_time, nullptr)); |
| 311 } | |
| 312 | |
| 313 TEST_F(NetworkTimeTrackerTest, UpdateFromNetwork) { | |
| 314 base::Time out_network_time; | |
| 315 EXPECT_EQ(nullptr, url_fetcher_factory_->GetFetcherByID(0)); | |
| 316 EXPECT_FALSE(tracker_->GetNetworkTime(&out_network_time, nullptr)); | |
| 317 | |
| 318 tracker_->QueryTimeService(); | |
| 319 OverrideNonce(123123123); | |
| 320 net::TestURLFetcher* fetcher = url_fetcher_factory_->GetFetcherByID(0); | |
| 321 ASSERT_NE(nullptr, fetcher); | |
| 322 EXPECT_EQ("", fetcher->upload_data()); | |
| 323 | |
| 324 fetcher->set_response_code(200); | |
| 325 fetcher->SetResponseString( | |
| 326 ")]}'\n" | |
| 327 "{\"current_time_millis\":1461621971825,\"server_nonce\":-6." | |
| 328 "006853099049523E85}"); | |
| 329 | |
| 330 net::HttpResponseHeaders* headers = new net::HttpResponseHeaders(""); | |
|
Ryan Sleevi
2016/04/28 01:08:34
pedantry: For empty strings, std::string() avoids
mab
2016/04/28 02:29:04
Done.
| |
| 331 headers->AddHeader( | |
| 332 "x-cup-server-proof: " | |
| 333 "304402202e0f24db1ea69f1bbe81da4108f381fcf7a2781c53cf7663cb47083cb5fe8efd" | |
| 334 "022009d2b67c0deceaaf849f7c529be96701ed5f15d5efcaf401a94e0801accc9832:" | |
| 335 "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855"); | |
|
Ryan Sleevi
2016/04/28 01:08:34
This proof is tied to the key used, right? Is it a
mab
2016/04/28 02:29:04
It is tied to the server's keypair, to the (overri
| |
| 336 fetcher->set_response_headers(headers); | |
| 337 fetcher->delegate()->OnURLFetchComplete(fetcher); | |
| 338 | |
| 339 EXPECT_TRUE(tracker_->GetNetworkTime(&out_network_time, nullptr)); | |
| 340 EXPECT_EQ(base::Time::UnixEpoch() + | |
| 341 base::TimeDelta::FromMilliseconds(1461621971825), | |
| 342 out_network_time); | |
| 343 EXPECT_TRUE(tracker_->query_timer_.IsRunning()); | |
| 344 } | |
| 345 | |
| 346 TEST_F(NetworkTimeTrackerTest, NoNetworkQueryWhileSynced) { | |
| 347 base::Time in_network_time = clock_->Now(); | |
| 348 UpdateNetworkTime(in_network_time, resolution_, latency_, | |
| 349 tick_clock_->NowTicks()); | |
| 350 | |
| 351 tracker_->QueryTimeService(); | |
| 352 net::TestURLFetcher* fetcher = url_fetcher_factory_->GetFetcherByID(0); | |
| 353 EXPECT_EQ(nullptr, fetcher); // No query should be started. | |
| 354 } | |
| 355 | |
| 356 TEST_F(NetworkTimeTrackerTest, UpdateFromNetworkBadSignature) { | |
| 357 base::Time out_network_time; | |
| 358 EXPECT_EQ(nullptr, url_fetcher_factory_->GetFetcherByID(0)); | |
| 359 EXPECT_FALSE(tracker_->GetNetworkTime(&out_network_time, nullptr)); | |
| 360 | |
| 361 tracker_->QueryTimeService(); | |
| 362 OverrideNonce(123123123); | |
| 363 net::TestURLFetcher* fetcher = url_fetcher_factory_->GetFetcherByID(0); | |
| 364 ASSERT_NE(nullptr, fetcher); | |
| 365 EXPECT_EQ("", fetcher->upload_data()); | |
| 366 | |
| 367 fetcher->set_response_code(200); | |
| 368 fetcher->SetResponseString( | |
| 369 ")]}'\n" | |
| 370 "{\"current_time_millis\":1461621971825,\"server_nonce\":-6." | |
| 371 "006853099049523E85}"); | |
| 372 | |
| 373 net::HttpResponseHeaders* headers = new net::HttpResponseHeaders(""); | |
| 374 headers->AddHeader("x-cup-server-proof: deadbeef:deadbeef"); | |
| 375 fetcher->set_response_headers(headers); | |
| 376 fetcher->delegate()->OnURLFetchComplete(fetcher); | |
| 377 | |
| 378 EXPECT_FALSE(tracker_->GetNetworkTime(&out_network_time, nullptr)); | |
| 379 EXPECT_TRUE(tracker_->query_timer_.IsRunning()); | |
| 292 } | 380 } |
| 293 | 381 |
| 294 } // namespace network_time | 382 } // namespace network_time |
| OLD | NEW |