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