OLD | NEW |
| (Empty) |
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. | |
4 | |
5 #include "components/variations/variations_http_header_provider.h" | |
6 | |
7 #include <string> | |
8 | |
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" | |
19 | |
20 namespace variations { | |
21 | |
22 namespace { | |
23 | |
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; | |
38 } | |
39 | |
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)); | |
47 | |
48 AssociateGoogleVariationID(key, trial->trial_name(), trial->group_name(), id); | |
49 | |
50 return trial; | |
51 } | |
52 | |
53 } // namespace | |
54 | |
55 class VariationsHttpHeaderProviderTest : public ::testing::Test { | |
56 public: | |
57 VariationsHttpHeaderProviderTest() {} | |
58 | |
59 virtual ~VariationsHttpHeaderProviderTest() {} | |
60 | |
61 virtual void TearDown() override { | |
62 testing::ClearAllVariationIDs(); | |
63 } | |
64 }; | |
65 | |
66 TEST_F(VariationsHttpHeaderProviderTest, ShouldAppendHeaders) { | |
67 struct { | |
68 const char* url; | |
69 bool should_append_headers; | |
70 } cases[] = { | |
71 {"http://google.com", true}, | |
72 {"http://www.google.com", true}, | |
73 {"http://m.google.com", true}, | |
74 {"http://google.ca", true}, | |
75 {"https://google.ca", true}, | |
76 {"http://google.co.uk", true}, | |
77 {"http://google.co.uk:8080/", true}, | |
78 {"http://www.google.co.uk:8080/", true}, | |
79 {"http://google", false}, | |
80 | |
81 {"http://youtube.com", true}, | |
82 {"http://www.youtube.com", true}, | |
83 {"http://www.youtube.ca", true}, | |
84 {"http://www.youtube.co.uk:8080/", true}, | |
85 {"https://www.youtube.com", true}, | |
86 {"http://youtube", false}, | |
87 | |
88 {"http://www.yahoo.com", false}, | |
89 | |
90 {"http://ad.doubleclick.net", true}, | |
91 {"https://a.b.c.doubleclick.net", true}, | |
92 {"https://a.b.c.doubleclick.net:8081", true}, | |
93 {"http://www.doubleclick.com", true}, | |
94 {"http://www.doubleclick.org", false}, | |
95 {"http://www.doubleclick.net.com", false}, | |
96 {"https://www.doubleclick.net.com", false}, | |
97 | |
98 {"http://ad.googlesyndication.com", true}, | |
99 {"https://a.b.c.googlesyndication.com", true}, | |
100 {"https://a.b.c.googlesyndication.com:8080", true}, | |
101 {"http://www.doubleclick.edu", false}, | |
102 {"http://www.googlesyndication.com.edu", false}, | |
103 {"https://www.googlesyndication.com.com", false}, | |
104 | |
105 {"http://www.googleadservices.com", true}, | |
106 {"http://www.googleadservices.com:8080", true}, | |
107 {"https://www.googleadservices.com", true}, | |
108 {"https://www.internal.googleadservices.com", true}, | |
109 {"https://www2.googleadservices.com", true}, | |
110 {"https://www.googleadservices.org", false}, | |
111 {"https://www.googleadservices.com.co.uk", false}, | |
112 | |
113 {"http://WWW.ANDROID.COM", true}, | |
114 {"http://www.android.com", true}, | |
115 {"http://www.doubleclick.com", true}, | |
116 {"http://www.doubleclick.net", true}, | |
117 {"http://www.ggpht.com", true}, | |
118 {"http://www.googleadservices.com", true}, | |
119 {"http://www.googleapis.com", true}, | |
120 {"http://www.googlesyndication.com", true}, | |
121 {"http://www.googleusercontent.com", true}, | |
122 {"http://www.googlevideo.com", true}, | |
123 {"http://ssl.gstatic.com", true}, | |
124 {"http://www.gstatic.com", true}, | |
125 {"http://www.ytimg.com", true}, | |
126 {"http://wwwytimg.com", false}, | |
127 {"http://ytimg.com", false}, | |
128 | |
129 {"http://www.android.org", false}, | |
130 {"http://www.doubleclick.org", false}, | |
131 {"http://www.doubleclick.net", true}, | |
132 {"http://www.ggpht.org", false}, | |
133 {"http://www.googleadservices.org", false}, | |
134 {"http://www.googleapis.org", false}, | |
135 {"http://www.googlesyndication.org", false}, | |
136 {"http://www.googleusercontent.org", false}, | |
137 {"http://www.googlevideo.org", false}, | |
138 {"http://ssl.gstatic.org", false}, | |
139 {"http://www.gstatic.org", false}, | |
140 {"http://www.ytimg.org", false}, | |
141 | |
142 {"http://a.b.android.com", true}, | |
143 {"http://a.b.doubleclick.com", true}, | |
144 {"http://a.b.doubleclick.net", true}, | |
145 {"http://a.b.ggpht.com", true}, | |
146 {"http://a.b.googleadservices.com", true}, | |
147 {"http://a.b.googleapis.com", true}, | |
148 {"http://a.b.googlesyndication.com", true}, | |
149 {"http://a.b.googleusercontent.com", true}, | |
150 {"http://a.b.googlevideo.com", true}, | |
151 {"http://ssl.gstatic.com", true}, | |
152 {"http://a.b.gstatic.com", true}, | |
153 {"http://a.b.ytimg.com", true}, | |
154 }; | |
155 | |
156 for (size_t i = 0; i < arraysize(cases); ++i) { | |
157 const GURL url(cases[i].url); | |
158 EXPECT_EQ(cases[i].should_append_headers, | |
159 VariationsHttpHeaderProvider::ShouldAppendHeaders(url)) | |
160 << url; | |
161 } | |
162 } | |
163 | |
164 TEST_F(VariationsHttpHeaderProviderTest, SetDefaultVariationIds_Valid) { | |
165 base::MessageLoop loop; | |
166 VariationsHttpHeaderProvider provider; | |
167 GURL url("http://www.google.com"); | |
168 net::HttpRequestHeaders headers; | |
169 std::string variations; | |
170 | |
171 // Valid experiment ids. | |
172 EXPECT_TRUE(provider.SetDefaultVariationIds("12,456,t789")); | |
173 provider.InitVariationIDsCacheIfNeeded(); | |
174 provider.AppendHeaders(url, false, false, &headers); | |
175 EXPECT_TRUE(headers.HasHeader("X-Client-Data")); | |
176 headers.GetHeader("X-Client-Data", &variations); | |
177 std::set<VariationID> variation_ids; | |
178 std::set<VariationID> trigger_ids; | |
179 ASSERT_TRUE(ExtractVariationIds(variations, &variation_ids, &trigger_ids)); | |
180 EXPECT_TRUE(variation_ids.find(12) != variation_ids.end()); | |
181 EXPECT_TRUE(variation_ids.find(456) != variation_ids.end()); | |
182 EXPECT_TRUE(trigger_ids.find(789) != trigger_ids.end()); | |
183 EXPECT_FALSE(variation_ids.find(789) != variation_ids.end()); | |
184 } | |
185 | |
186 TEST_F(VariationsHttpHeaderProviderTest, SetDefaultVariationIds_Invalid) { | |
187 base::MessageLoop loop; | |
188 VariationsHttpHeaderProvider provider; | |
189 GURL url("http://www.google.com"); | |
190 net::HttpRequestHeaders headers; | |
191 | |
192 // Invalid experiment ids. | |
193 EXPECT_FALSE(provider.SetDefaultVariationIds("abcd12,456")); | |
194 provider.InitVariationIDsCacheIfNeeded(); | |
195 provider.AppendHeaders(url, false, false, &headers); | |
196 EXPECT_FALSE(headers.HasHeader("X-Client-Data")); | |
197 | |
198 // Invalid trigger experiment id | |
199 EXPECT_FALSE(provider.SetDefaultVariationIds("12,tabc456")); | |
200 provider.InitVariationIDsCacheIfNeeded(); | |
201 provider.AppendHeaders(url, false, false, &headers); | |
202 EXPECT_FALSE(headers.HasHeader("X-Client-Data")); | |
203 } | |
204 | |
205 TEST_F(VariationsHttpHeaderProviderTest, OnFieldTrialGroupFinalized) { | |
206 base::MessageLoop loop; | |
207 base::FieldTrialList field_trial_list( | |
208 new metrics::SHA1EntropyProvider("test")); | |
209 VariationsHttpHeaderProvider provider; | |
210 provider.InitVariationIDsCacheIfNeeded(); | |
211 | |
212 const std::string default_name = "default"; | |
213 scoped_refptr<base::FieldTrial> trial_1(CreateTrialAndAssociateId( | |
214 "t1", default_name, GOOGLE_WEB_PROPERTIES, 123)); | |
215 | |
216 ASSERT_EQ(default_name, trial_1->group_name()); | |
217 | |
218 scoped_refptr<base::FieldTrial> trial_2(CreateTrialAndAssociateId( | |
219 "t2", default_name, GOOGLE_WEB_PROPERTIES_TRIGGER, 456)); | |
220 | |
221 ASSERT_EQ(default_name, trial_2->group_name()); | |
222 | |
223 // Run the message loop to make sure OnFieldTrialGroupFinalized is called for | |
224 // the two field trials. | |
225 base::RunLoop().RunUntilIdle(); | |
226 | |
227 GURL url("http://www.google.com"); | |
228 net::HttpRequestHeaders headers; | |
229 provider.AppendHeaders(url, false, false, &headers); | |
230 std::string variations; | |
231 headers.GetHeader("X-Client-Data", &variations); | |
232 | |
233 std::set<VariationID> variation_ids; | |
234 std::set<VariationID> trigger_ids; | |
235 ASSERT_TRUE(ExtractVariationIds(variations, &variation_ids, &trigger_ids)); | |
236 EXPECT_TRUE(variation_ids.find(123) != variation_ids.end()); | |
237 EXPECT_TRUE(trigger_ids.find(456) != trigger_ids.end()); | |
238 } | |
239 | |
240 } // namespace variations | |
OLD | NEW |