Make castv2 performance test work.
[chromium-blink-merge.git] / components / variations / net / variations_http_header_provider_unittest.cc
blob2cb5c81565c2c19c0aab2c8d8146d258901e7393
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
3 // found in the LICENSE file.
5 #include "components/variations/net/variations_http_header_provider.h"
7 #include <string>
9 #include "base/base64.h"
10 #include "base/message_loop/message_loop.h"
11 #include "base/metrics/field_trial.h"
12 #include "base/run_loop.h"
13 #include "components/variations/entropy_provider.h"
14 #include "components/variations/proto/client_variations.pb.h"
15 #include "components/variations/variations_associated_data.h"
16 #include "net/http/http_request_headers.h"
17 #include "testing/gtest/include/gtest/gtest.h"
18 #include "url/gurl.h"
20 namespace variations {
22 namespace {
24 // Decodes the variations header and extracts the variation ids.
25 bool ExtractVariationIds(const std::string& variations,
26 std::set<VariationID>* variation_ids,
27 std::set<VariationID>* trigger_ids) {
28 std::string serialized_proto;
29 if (!base::Base64Decode(variations, &serialized_proto))
30 return false;
31 ClientVariations proto;
32 if (!proto.ParseFromString(serialized_proto)) return false;
33 for (int i = 0; i < proto.variation_id_size(); ++i)
34 variation_ids->insert(proto.variation_id(i));
35 for (int i = 0; i < proto.trigger_variation_id_size(); ++i)
36 trigger_ids->insert(proto.trigger_variation_id(i));
37 return true;
40 scoped_refptr<base::FieldTrial> CreateTrialAndAssociateId(
41 const std::string& trial_name,
42 const std::string& default_group_name,
43 IDCollectionKey key,
44 VariationID id) {
45 scoped_refptr<base::FieldTrial> trial(
46 base::FieldTrialList::CreateFieldTrial(trial_name, default_group_name));
48 AssociateGoogleVariationID(key, trial->trial_name(), trial->group_name(), id);
50 return trial;
53 } // namespace
55 class VariationsHttpHeaderProviderTest : public ::testing::Test {
56 public:
57 VariationsHttpHeaderProviderTest() {}
59 ~VariationsHttpHeaderProviderTest() override {}
61 void TearDown() override { testing::ClearAllVariationIDs(); }
64 TEST_F(VariationsHttpHeaderProviderTest, ShouldAppendHeaders) {
65 struct {
66 const char* url;
67 bool should_append_headers;
68 } cases[] = {
69 {"http://google.com", true},
70 {"http://www.google.com", true},
71 {"http://m.google.com", true},
72 {"http://google.ca", true},
73 {"https://google.ca", true},
74 {"http://google.co.uk", true},
75 {"http://google.co.uk:8080/", true},
76 {"http://www.google.co.uk:8080/", true},
77 {"http://google", false},
79 {"http://youtube.com", true},
80 {"http://www.youtube.com", true},
81 {"http://www.youtube.ca", true},
82 {"http://www.youtube.co.uk:8080/", true},
83 {"https://www.youtube.com", true},
84 {"http://youtube", false},
86 {"http://www.yahoo.com", false},
88 {"http://ad.doubleclick.net", true},
89 {"https://a.b.c.doubleclick.net", true},
90 {"https://a.b.c.doubleclick.net:8081", true},
91 {"http://www.doubleclick.com", true},
92 {"http://www.doubleclick.org", false},
93 {"http://www.doubleclick.net.com", false},
94 {"https://www.doubleclick.net.com", false},
96 {"http://ad.googlesyndication.com", true},
97 {"https://a.b.c.googlesyndication.com", true},
98 {"https://a.b.c.googlesyndication.com:8080", true},
99 {"http://www.doubleclick.edu", false},
100 {"http://www.googlesyndication.com.edu", false},
101 {"https://www.googlesyndication.com.com", false},
103 {"http://www.googleadservices.com", true},
104 {"http://www.googleadservices.com:8080", true},
105 {"https://www.googleadservices.com", true},
106 {"https://www.internal.googleadservices.com", true},
107 {"https://www2.googleadservices.com", true},
108 {"https://www.googleadservices.org", false},
109 {"https://www.googleadservices.com.co.uk", false},
111 {"http://WWW.ANDROID.COM", true},
112 {"http://www.android.com", true},
113 {"http://www.doubleclick.com", true},
114 {"http://www.doubleclick.net", true},
115 {"http://www.ggpht.com", true},
116 {"http://www.googleadservices.com", true},
117 {"http://www.googleapis.com", true},
118 {"http://www.googlesyndication.com", true},
119 {"http://www.googleusercontent.com", true},
120 {"http://www.googlevideo.com", true},
121 {"http://ssl.gstatic.com", true},
122 {"http://www.gstatic.com", true},
123 {"http://www.ytimg.com", true},
124 {"http://wwwytimg.com", false},
125 {"http://ytimg.com", false},
127 {"http://www.android.org", false},
128 {"http://www.doubleclick.org", false},
129 {"http://www.doubleclick.net", true},
130 {"http://www.ggpht.org", false},
131 {"http://www.googleadservices.org", false},
132 {"http://www.googleapis.org", false},
133 {"http://www.googlesyndication.org", false},
134 {"http://www.googleusercontent.org", false},
135 {"http://www.googlevideo.org", false},
136 {"http://ssl.gstatic.org", false},
137 {"http://www.gstatic.org", false},
138 {"http://www.ytimg.org", false},
140 {"http://a.b.android.com", true},
141 {"http://a.b.doubleclick.com", true},
142 {"http://a.b.doubleclick.net", true},
143 {"http://a.b.ggpht.com", true},
144 {"http://a.b.googleadservices.com", true},
145 {"http://a.b.googleapis.com", true},
146 {"http://a.b.googlesyndication.com", true},
147 {"http://a.b.googleusercontent.com", true},
148 {"http://a.b.googlevideo.com", true},
149 {"http://ssl.gstatic.com", true},
150 {"http://a.b.gstatic.com", true},
151 {"http://a.b.ytimg.com", true},
154 for (size_t i = 0; i < arraysize(cases); ++i) {
155 const GURL url(cases[i].url);
156 EXPECT_EQ(cases[i].should_append_headers,
157 VariationsHttpHeaderProvider::ShouldAppendHeaders(url))
158 << url;
162 TEST_F(VariationsHttpHeaderProviderTest, SetDefaultVariationIds_Valid) {
163 base::MessageLoop loop;
164 VariationsHttpHeaderProvider provider;
165 GURL url("http://www.google.com");
166 net::HttpRequestHeaders headers;
167 std::string variations;
169 // Valid experiment ids.
170 EXPECT_TRUE(provider.SetDefaultVariationIds("12,456,t789"));
171 provider.InitVariationIDsCacheIfNeeded();
172 provider.AppendHeaders(url, false, false, &headers);
173 EXPECT_TRUE(headers.HasHeader("X-Client-Data"));
174 headers.GetHeader("X-Client-Data", &variations);
175 std::set<VariationID> variation_ids;
176 std::set<VariationID> trigger_ids;
177 ASSERT_TRUE(ExtractVariationIds(variations, &variation_ids, &trigger_ids));
178 EXPECT_TRUE(variation_ids.find(12) != variation_ids.end());
179 EXPECT_TRUE(variation_ids.find(456) != variation_ids.end());
180 EXPECT_TRUE(trigger_ids.find(789) != trigger_ids.end());
181 EXPECT_FALSE(variation_ids.find(789) != variation_ids.end());
184 TEST_F(VariationsHttpHeaderProviderTest, SetDefaultVariationIds_Invalid) {
185 base::MessageLoop loop;
186 VariationsHttpHeaderProvider provider;
187 GURL url("http://www.google.com");
188 net::HttpRequestHeaders headers;
190 // Invalid experiment ids.
191 EXPECT_FALSE(provider.SetDefaultVariationIds("abcd12,456"));
192 provider.InitVariationIDsCacheIfNeeded();
193 provider.AppendHeaders(url, false, false, &headers);
194 EXPECT_FALSE(headers.HasHeader("X-Client-Data"));
196 // Invalid trigger experiment id
197 EXPECT_FALSE(provider.SetDefaultVariationIds("12,tabc456"));
198 provider.InitVariationIDsCacheIfNeeded();
199 provider.AppendHeaders(url, false, false, &headers);
200 EXPECT_FALSE(headers.HasHeader("X-Client-Data"));
203 TEST_F(VariationsHttpHeaderProviderTest, OnFieldTrialGroupFinalized) {
204 base::MessageLoop loop;
205 base::FieldTrialList field_trial_list(
206 new metrics::SHA1EntropyProvider("test"));
207 VariationsHttpHeaderProvider provider;
208 provider.InitVariationIDsCacheIfNeeded();
210 const std::string default_name = "default";
211 scoped_refptr<base::FieldTrial> trial_1(CreateTrialAndAssociateId(
212 "t1", default_name, GOOGLE_WEB_PROPERTIES, 123));
214 ASSERT_EQ(default_name, trial_1->group_name());
216 scoped_refptr<base::FieldTrial> trial_2(CreateTrialAndAssociateId(
217 "t2", default_name, GOOGLE_WEB_PROPERTIES_TRIGGER, 456));
219 ASSERT_EQ(default_name, trial_2->group_name());
221 // Run the message loop to make sure OnFieldTrialGroupFinalized is called for
222 // the two field trials.
223 base::RunLoop().RunUntilIdle();
225 GURL url("http://www.google.com");
226 net::HttpRequestHeaders headers;
227 provider.AppendHeaders(url, false, false, &headers);
228 std::string variations;
229 headers.GetHeader("X-Client-Data", &variations);
231 std::set<VariationID> variation_ids;
232 std::set<VariationID> trigger_ids;
233 ASSERT_TRUE(ExtractVariationIds(variations, &variation_ids, &trigger_ids));
234 EXPECT_TRUE(variation_ids.find(123) != variation_ids.end());
235 EXPECT_TRUE(trigger_ids.find(456) != trigger_ids.end());
238 } // namespace variations