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/variations_http_header_provider.h"
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"
20 namespace variations
{
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
))
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
));
40 scoped_refptr
<base::FieldTrial
> CreateTrialAndAssociateId(
41 const std::string
& trial_name
,
42 const std::string
& default_group_name
,
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
);
55 class VariationsHttpHeaderProviderTest
: public ::testing::Test
{
57 VariationsHttpHeaderProviderTest() {}
59 virtual ~VariationsHttpHeaderProviderTest() {}
61 virtual void TearDown() OVERRIDE
{
62 testing::ClearAllVariationIDs();
66 TEST_F(VariationsHttpHeaderProviderTest
, ShouldAppendHeaders
) {
69 bool should_append_headers
;
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},
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},
88 {"http://www.yahoo.com", false},
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},
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},
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},
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},
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},
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},
156 for (size_t i
= 0; i
< ARRAYSIZE_UNSAFE(cases
); ++i
) {
157 const GURL
url(cases
[i
].url
);
158 EXPECT_EQ(cases
[i
].should_append_headers
,
159 VariationsHttpHeaderProvider::ShouldAppendHeaders(url
))
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
;
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());
186 TEST_F(VariationsHttpHeaderProviderTest
, SetDefaultVariationIds_Invalid
) {
187 base::MessageLoop loop
;
188 VariationsHttpHeaderProvider provider
;
189 GURL
url("http://www.google.com");
190 net::HttpRequestHeaders headers
;
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"));
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"));
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();
212 const std::string default_name
= "default";
213 scoped_refptr
<base::FieldTrial
> trial_1(CreateTrialAndAssociateId(
214 "t1", default_name
, GOOGLE_WEB_PROPERTIES
, 123));
216 ASSERT_EQ(default_name
, trial_1
->group_name());
218 scoped_refptr
<base::FieldTrial
> trial_2(CreateTrialAndAssociateId(
219 "t2", default_name
, GOOGLE_WEB_PROPERTIES_TRIGGER
, 456));
221 ASSERT_EQ(default_name
, trial_2
->group_name());
223 // Run the message loop to make sure OnFieldTrialGroupFinalized is called for
224 // the two field trials.
225 base::RunLoop().RunUntilIdle();
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
);
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());
240 } // namespace variations