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 <map> | |
8 #include <memory> | 7 #include <memory> |
9 #include <string> | 8 #include <string> |
10 #include <utility> | 9 #include <utility> |
11 | 10 |
12 #include "base/compiler_specific.h" | 11 #include "base/compiler_specific.h" |
13 #include "base/feature_list.h" | |
14 #include "base/memory/ptr_util.h" | 12 #include "base/memory/ptr_util.h" |
15 #include "base/metrics/field_trial.h" | |
16 #include "base/strings/string_number_conversions.h" | |
17 #include "base/strings/stringprintf.h" | 13 #include "base/strings/stringprintf.h" |
18 #include "base/test/histogram_tester.h" | 14 #include "base/test/histogram_tester.h" |
19 #include "base/test/mock_entropy_provider.h" | |
20 #include "base/test/scoped_feature_list.h" | |
21 #include "base/test/simple_test_clock.h" | 15 #include "base/test/simple_test_clock.h" |
22 #include "base/test/simple_test_tick_clock.h" | 16 #include "base/test/simple_test_tick_clock.h" |
23 #include "components/client_update_protocol/ecdsa.h" | 17 #include "components/client_update_protocol/ecdsa.h" |
24 #include "components/network_time/network_time_pref_names.h" | 18 #include "components/network_time/network_time_pref_names.h" |
| 19 #include "components/network_time/network_time_test_utils.h" |
25 #include "components/prefs/testing_pref_service.h" | 20 #include "components/prefs/testing_pref_service.h" |
26 #include "components/variations/variations_associated_data.h" | |
27 #include "net/http/http_response_headers.h" | 21 #include "net/http/http_response_headers.h" |
28 #include "net/test/embedded_test_server/embedded_test_server.h" | 22 #include "net/test/embedded_test_server/embedded_test_server.h" |
29 #include "net/test/embedded_test_server/http_response.h" | 23 #include "net/test/embedded_test_server/http_response.h" |
30 #include "net/url_request/url_fetcher.h" | 24 #include "net/url_request/url_fetcher.h" |
31 #include "net/url_request/url_request_test_util.h" | 25 #include "net/url_request/url_request_test_util.h" |
32 #include "testing/gtest/include/gtest/gtest.h" | 26 #include "testing/gtest/include/gtest/gtest.h" |
33 | 27 |
34 namespace network_time { | 28 namespace network_time { |
35 | 29 |
36 namespace { | 30 namespace { |
37 const uint32_t kOneDayInSeconds = 86400; | 31 const uint32_t kOneDayInSeconds = 86400; |
38 const char kFetchFailedHistogram[] = "NetworkTimeTracker.UpdateTimeFetchFailed"; | 32 const char kFetchFailedHistogram[] = "NetworkTimeTracker.UpdateTimeFetchFailed"; |
39 const char kFetchValidHistogram[] = "NetworkTimeTracker.UpdateTimeFetchValid"; | 33 const char kFetchValidHistogram[] = "NetworkTimeTracker.UpdateTimeFetchValid"; |
40 const char kClockDivergencePositiveHistogram[] = | 34 const char kClockDivergencePositiveHistogram[] = |
41 "NetworkTimeTracker.ClockDivergence.Positive"; | 35 "NetworkTimeTracker.ClockDivergence.Positive"; |
42 const char kClockDivergenceNegativeHistogram[] = | 36 const char kClockDivergenceNegativeHistogram[] = |
43 "NetworkTimeTracker.ClockDivergence.Negative"; | 37 "NetworkTimeTracker.ClockDivergence.Negative"; |
44 const char kWallClockBackwardsHistogram[] = | 38 const char kWallClockBackwardsHistogram[] = |
45 "NetworkTimeTracker.WallClockRanBackwards"; | 39 "NetworkTimeTracker.WallClockRanBackwards"; |
46 } // namespace | 40 } // namespace |
47 | 41 |
48 class NetworkTimeTrackerTest : public testing::Test { | 42 class NetworkTimeTrackerTest : public FieldTrialTest { |
49 public: | 43 public: |
50 ~NetworkTimeTrackerTest() override {} | 44 ~NetworkTimeTrackerTest() override {} |
51 | 45 |
52 NetworkTimeTrackerTest() | 46 NetworkTimeTrackerTest() |
53 : io_thread_("IO thread"), | 47 : io_thread_("IO thread"), |
54 clock_(new base::SimpleTestClock), | 48 clock_(new base::SimpleTestClock), |
55 tick_clock_(new base::SimpleTestTickClock), | 49 tick_clock_(new base::SimpleTestTickClock), |
56 test_server_(new net::EmbeddedTestServer) { | 50 test_server_(new net::EmbeddedTestServer) { |
57 base::Thread::Options thread_options; | 51 base::Thread::Options thread_options; |
58 thread_options.message_loop_type = base::MessageLoop::TYPE_IO; | 52 thread_options.message_loop_type = base::MessageLoop::TYPE_IO; |
(...skipping 114 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
173 } | 167 } |
174 | 168 |
175 // Advances both the system clock and the tick clock. This should be used for | 169 // Advances both the system clock and the tick clock. This should be used for |
176 // the normal passage of time, i.e. when neither clock is doing anything odd. | 170 // the normal passage of time, i.e. when neither clock is doing anything odd. |
177 void AdvanceBoth(const base::TimeDelta& delta) { | 171 void AdvanceBoth(const base::TimeDelta& delta) { |
178 tick_clock_->Advance(delta); | 172 tick_clock_->Advance(delta); |
179 clock_->Advance(delta); | 173 clock_->Advance(delta); |
180 } | 174 } |
181 | 175 |
182 protected: | 176 protected: |
183 void SetNetworkQueriesWithVariationsService(bool enable, | |
184 float query_probability) { | |
185 const std::string kTrialName = "Trial"; | |
186 const std::string kGroupName = "group"; | |
187 const base::Feature kFeature{"NetworkTimeServiceQuerying", | |
188 base::FEATURE_DISABLED_BY_DEFAULT}; | |
189 | |
190 // Clear all the things. | |
191 variations::testing::ClearAllVariationParams(); | |
192 | |
193 std::map<std::string, std::string> params; | |
194 params["RandomQueryProbability"] = base::DoubleToString(query_probability); | |
195 params["CheckTimeIntervalSeconds"] = base::Int64ToString(360); | |
196 | |
197 // There are 3 things here: a FieldTrial, a FieldTrialList, and a | |
198 // FeatureList. Don't get confused! The FieldTrial is reference-counted, | |
199 // and a reference is held by the FieldTrialList. The FieldTrialList and | |
200 // FeatureList are both singletons. The authorized way to reset the former | |
201 // for testing is to destruct it (above). The latter, by contrast, should | |
202 // should already start in a clean state and can be manipulated via the | |
203 // ScopedFeatureList helper class. If this comment was useful to you | |
204 // please send me a postcard. | |
205 | |
206 field_trial_list_.reset(); // Averts a CHECK fail in constructor below. | |
207 field_trial_list_.reset( | |
208 new base::FieldTrialList( | |
209 base::MakeUnique<base::MockEntropyProvider>())); | |
210 // refcounted, and reference held by field_trial_list_. | |
211 base::FieldTrial* trial = base::FieldTrialList::FactoryGetFieldTrial( | |
212 kTrialName, 100, kGroupName, 1971, 1, 1, | |
213 base::FieldTrial::SESSION_RANDOMIZED, | |
214 nullptr /* default_group_number */); | |
215 ASSERT_TRUE( | |
216 variations::AssociateVariationParams(kTrialName, kGroupName, params)); | |
217 | |
218 std::unique_ptr<base::FeatureList> feature_list(new base::FeatureList); | |
219 feature_list->RegisterFieldTrialOverride( | |
220 kFeature.name, enable ? base::FeatureList::OVERRIDE_ENABLE_FEATURE | |
221 : base::FeatureList::OVERRIDE_DISABLE_FEATURE, | |
222 trial); | |
223 scoped_feature_list_.reset(new base::test::ScopedFeatureList); | |
224 scoped_feature_list_->InitWithFeatureList(std::move(feature_list)); | |
225 } | |
226 | |
227 base::Thread io_thread_; | 177 base::Thread io_thread_; |
228 base::MessageLoop message_loop_; | 178 base::MessageLoop message_loop_; |
229 base::TimeDelta resolution_; | 179 base::TimeDelta resolution_; |
230 base::TimeDelta latency_; | 180 base::TimeDelta latency_; |
231 base::TimeDelta adjustment_; | 181 base::TimeDelta adjustment_; |
232 base::SimpleTestClock* clock_; | 182 base::SimpleTestClock* clock_; |
233 base::SimpleTestTickClock* tick_clock_; | 183 base::SimpleTestTickClock* tick_clock_; |
234 TestingPrefServiceSimple pref_service_; | 184 TestingPrefServiceSimple pref_service_; |
235 std::unique_ptr<base::FieldTrialList> field_trial_list_; | |
236 std::unique_ptr<NetworkTimeTracker> tracker_; | 185 std::unique_ptr<NetworkTimeTracker> tracker_; |
237 std::unique_ptr<net::EmbeddedTestServer> test_server_; | 186 std::unique_ptr<net::EmbeddedTestServer> test_server_; |
238 std::unique_ptr<base::test::ScopedFeatureList> scoped_feature_list_; | |
239 }; | 187 }; |
240 | 188 |
241 TEST_F(NetworkTimeTrackerTest, Uninitialized) { | 189 TEST_F(NetworkTimeTrackerTest, Uninitialized) { |
242 base::Time network_time; | 190 base::Time network_time; |
243 base::TimeDelta uncertainty; | 191 base::TimeDelta uncertainty; |
244 EXPECT_EQ(NetworkTimeTracker::NETWORK_TIME_NO_SYNC_ATTEMPT, | 192 EXPECT_EQ(NetworkTimeTracker::NETWORK_TIME_NO_SYNC_ATTEMPT, |
245 tracker_->GetNetworkTime(&network_time, &uncertainty)); | 193 tracker_->GetNetworkTime(&network_time, &uncertainty)); |
246 } | 194 } |
247 | 195 |
248 TEST_F(NetworkTimeTrackerTest, LongPostingDelay) { | 196 TEST_F(NetworkTimeTrackerTest, LongPostingDelay) { |
(...skipping 535 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
784 EXPECT_EQ(NetworkTimeTracker::NETWORK_TIME_SUBSEQUENT_SYNC_PENDING, | 732 EXPECT_EQ(NetworkTimeTracker::NETWORK_TIME_SUBSEQUENT_SYNC_PENDING, |
785 tracker_->GetNetworkTime(&out_network_time, nullptr)); | 733 tracker_->GetNetworkTime(&out_network_time, nullptr)); |
786 histograms.ExpectTotalCount(kFetchFailedHistogram, 0); | 734 histograms.ExpectTotalCount(kFetchFailedHistogram, 0); |
787 histograms.ExpectTotalCount(kFetchValidHistogram, 1); | 735 histograms.ExpectTotalCount(kFetchValidHistogram, 1); |
788 histograms.ExpectBucketCount(kFetchValidHistogram, false, 1); | 736 histograms.ExpectBucketCount(kFetchValidHistogram, false, 1); |
789 | 737 |
790 tracker_->WaitForFetchForTesting(123123123); | 738 tracker_->WaitForFetchForTesting(123123123); |
791 } | 739 } |
792 | 740 |
793 } // namespace network_time | 741 } // namespace network_time |
OLD | NEW |