OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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_test_utils.h" | 5 #include "components/network_time/network_time_test_utils.h" |
6 | 6 |
7 #include "base/feature_list.h" | 7 #include "base/feature_list.h" |
8 #include "base/memory/ptr_util.h" | 8 #include "base/memory/ptr_util.h" |
9 #include "base/metrics/field_trial.h" | 9 #include "base/metrics/field_trial.h" |
10 #include "base/strings/string_number_conversions.h" | 10 #include "base/strings/string_number_conversions.h" |
11 #include "base/test/mock_entropy_provider.h" | 11 #include "base/test/mock_entropy_provider.h" |
12 #include "base/test/scoped_feature_list.h" | 12 #include "base/test/scoped_feature_list.h" |
13 #include "components/variations/variations_associated_data.h" | 13 #include "components/variations/variations_associated_data.h" |
14 #include "net/http/http_response_headers.h" | 14 #include "net/http/http_response_headers.h" |
15 #include "net/test/embedded_test_server/http_response.h" | 15 #include "net/test/embedded_test_server/http_response.h" |
16 #include "testing/gtest/include/gtest/gtest.h" | 16 #include "testing/gtest/include/gtest/gtest.h" |
17 | 17 |
18 namespace network_time { | 18 namespace network_time { |
19 | 19 |
| 20 const double kGoodTimeResponseHandlerTime = 1461621971825; |
| 21 |
20 // Returns a valid time response. Update as follows: | 22 // Returns a valid time response. Update as follows: |
21 // | 23 // |
22 // curl http://clients2.google.com/time/1/current?cup2key=1:123123123 | 24 // curl http://clients2.google.com/time/1/current?cup2key=1:123123123 |
23 // | 25 // |
24 // where 1 is the key version and 123123123 is the nonce. Copy the nonce, the | 26 // where 1 is the key version and 123123123 is the nonce. Copy the nonce, the |
25 // response, and the x-cup-server-proof header into the test. | 27 // response, and the x-cup-server-proof header into the test. |
26 std::unique_ptr<net::test_server::HttpResponse> GoodTimeResponseHandler( | 28 std::unique_ptr<net::test_server::HttpResponse> GoodTimeResponseHandler( |
27 const net::test_server::HttpRequest& request) { | 29 const net::test_server::HttpRequest& request) { |
28 net::test_server::BasicHttpResponse* response = | 30 net::test_server::BasicHttpResponse* response = |
29 new net::test_server::BasicHttpResponse(); | 31 new net::test_server::BasicHttpResponse(); |
30 response->set_code(net::HTTP_OK); | 32 response->set_code(net::HTTP_OK); |
31 response->set_content( | 33 response->set_content( |
32 ")]}'\n" | 34 ")]}'\n" |
33 "{\"current_time_millis\":1461621971825,\"server_nonce\":-6." | 35 "{\"current_time_millis\":" + |
| 36 base::DoubleToString(kGoodTimeResponseHandlerTime) + |
| 37 ",\"server_nonce\":-6." |
34 "006853099049523E85}"); | 38 "006853099049523E85}"); |
35 response->AddCustomHeader( | 39 response->AddCustomHeader( |
36 "x-cup-server-proof", | 40 "x-cup-server-proof", |
37 "304402202e0f24db1ea69f1bbe81da4108f381fcf7a2781c53cf7663cb47083cb5fe8e" | 41 "304402202e0f24db1ea69f1bbe81da4108f381fcf7a2781c53cf7663cb47083cb5fe8e" |
38 "fd" | 42 "fd" |
39 "022009d2b67c0deceaaf849f7c529be96701ed5f15d5efcaf401a94e0801accc9832:" | 43 "022009d2b67c0deceaaf849f7c529be96701ed5f15d5efcaf401a94e0801accc9832:" |
40 "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855"); | 44 "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855"); |
41 return std::unique_ptr<net::test_server::HttpResponse>(response); | 45 return std::unique_ptr<net::test_server::HttpResponse>(response); |
42 } | 46 } |
43 | 47 |
44 FieldTrialTest::FieldTrialTest() {} | 48 FieldTrialTest::FieldTrialTest() {} |
45 FieldTrialTest::~FieldTrialTest() {} | 49 FieldTrialTest::~FieldTrialTest() {} |
46 | 50 |
| 51 FieldTrialTest* FieldTrialTest::CreateForBrowserTest() { |
| 52 FieldTrialTest* test = new FieldTrialTest(); |
| 53 test->create_field_trial_list_ = false; |
| 54 return test; |
| 55 } |
| 56 |
47 void FieldTrialTest::SetNetworkQueriesWithVariationsService( | 57 void FieldTrialTest::SetNetworkQueriesWithVariationsService( |
48 bool enable, | 58 bool enable, |
49 float query_probability, | 59 float query_probability, |
50 FetchesOnDemandStatus fetches_on_demand) { | 60 FetchBehavior fetch_behavior) { |
51 const std::string kTrialName = "Trial"; | 61 const std::string kTrialName = "Trial"; |
52 const std::string kGroupName = "group"; | 62 const std::string kGroupName = "group"; |
53 const base::Feature kFeature{"NetworkTimeServiceQuerying", | 63 const base::Feature kFeature{"NetworkTimeServiceQuerying", |
54 base::FEATURE_DISABLED_BY_DEFAULT}; | 64 base::FEATURE_DISABLED_BY_DEFAULT}; |
55 | 65 |
56 // Clear all the things. | 66 // Clear all the things. |
57 variations::testing::ClearAllVariationParams(); | 67 variations::testing::ClearAllVariationParams(); |
58 | 68 |
59 std::map<std::string, std::string> params; | 69 std::map<std::string, std::string> params; |
60 params["RandomQueryProbability"] = base::DoubleToString(query_probability); | 70 params["RandomQueryProbability"] = base::DoubleToString(query_probability); |
61 params["CheckTimeIntervalSeconds"] = base::Int64ToString(360); | 71 params["CheckTimeIntervalSeconds"] = base::Int64ToString(360); |
62 params["EnableFetchesOnDemand"] = | 72 std::string fetch_behavior_param; |
63 fetches_on_demand == ENABLE_FETCHES_ON_DEMAND ? "true" : ""; | 73 switch (fetch_behavior) { |
| 74 case FETCHES_IN_BACKGROUND_ONLY: |
| 75 fetch_behavior_param = "background-only"; |
| 76 break; |
| 77 case FETCHES_ON_DEMAND_ONLY: |
| 78 fetch_behavior_param = "on-demand-only"; |
| 79 break; |
| 80 case FETCHES_IN_BACKGROUND_AND_ON_DEMAND: |
| 81 fetch_behavior_param = "background-and-on-demand"; |
| 82 break; |
| 83 } |
| 84 params["FetchBehavior"] = fetch_behavior_param; |
64 | 85 |
65 // There are 3 things here: a FieldTrial, a FieldTrialList, and a | 86 // There are 3 things here: a FieldTrial, a FieldTrialList, and a |
66 // FeatureList. Don't get confused! The FieldTrial is reference-counted, | 87 // FeatureList. Don't get confused! The FieldTrial is reference-counted, |
67 // and a reference is held by the FieldTrialList. The FieldTrialList and | 88 // and a reference is held by the FieldTrialList. The FieldTrialList and |
68 // FeatureList are both singletons. The authorized way to reset the former | 89 // FeatureList are both singletons. The authorized way to reset the former |
69 // for testing is to destruct it (above). The latter, by contrast, should | 90 // for testing is to destruct it (above). The latter, by contrast, should |
70 // should already start in a clean state and can be manipulated via the | 91 // should already start in a clean state and can be manipulated via the |
71 // ScopedFeatureList helper class. If this comment was useful to you | 92 // ScopedFeatureList helper class. If this comment was useful to you |
72 // please send me a postcard. | 93 // please send me a postcard. |
73 | 94 |
74 field_trial_list_.reset(); // Averts a CHECK fail in constructor below. | 95 if (create_field_trial_list_) { |
75 field_trial_list_.reset( | 96 field_trial_list_.reset(); // Averts a CHECK fail in constructor below. |
76 new base::FieldTrialList(base::MakeUnique<base::MockEntropyProvider>())); | 97 field_trial_list_.reset(new base::FieldTrialList( |
77 // refcounted, and reference held by field_trial_list_. | 98 base::MakeUnique<base::MockEntropyProvider>())); |
| 99 } |
| 100 // refcounted, and reference held by the singleton FieldTrialList. |
78 base::FieldTrial* trial = base::FieldTrialList::FactoryGetFieldTrial( | 101 base::FieldTrial* trial = base::FieldTrialList::FactoryGetFieldTrial( |
79 kTrialName, 100, kGroupName, 1971, 1, 1, | 102 kTrialName, 100, kGroupName, 1971, 1, 1, |
80 base::FieldTrial::SESSION_RANDOMIZED, nullptr /* default_group_number */); | 103 base::FieldTrial::SESSION_RANDOMIZED, nullptr /* default_group_number */); |
81 ASSERT_TRUE( | 104 ASSERT_TRUE( |
82 variations::AssociateVariationParams(kTrialName, kGroupName, params)); | 105 variations::AssociateVariationParams(kTrialName, kGroupName, params)); |
83 | 106 |
84 std::unique_ptr<base::FeatureList> feature_list(new base::FeatureList); | 107 std::unique_ptr<base::FeatureList> feature_list(new base::FeatureList); |
85 feature_list->RegisterFieldTrialOverride( | 108 feature_list->RegisterFieldTrialOverride( |
86 kFeature.name, enable ? base::FeatureList::OVERRIDE_ENABLE_FEATURE | 109 kFeature.name, enable ? base::FeatureList::OVERRIDE_ENABLE_FEATURE |
87 : base::FeatureList::OVERRIDE_DISABLE_FEATURE, | 110 : base::FeatureList::OVERRIDE_DISABLE_FEATURE, |
88 trial); | 111 trial); |
89 scoped_feature_list_.reset(new base::test::ScopedFeatureList); | 112 scoped_feature_list_.reset(new base::test::ScopedFeatureList); |
90 scoped_feature_list_->InitWithFeatureList(std::move(feature_list)); | 113 scoped_feature_list_->InitWithFeatureList(std::move(feature_list)); |
91 } | 114 } |
92 | 115 |
93 } // namespace network_time | 116 } // namespace network_time |
OLD | NEW |