OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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 "chrome/browser/metrics/variations/variations_http_header_provider.h" | 5 #include "components/variations/variations_http_header_provider.h" |
6 | 6 |
7 #include <string> | 7 #include <string> |
8 | 8 |
9 #include "base/base64.h" | 9 #include "base/base64.h" |
10 #include "base/message_loop/message_loop.h" | 10 #include "base/message_loop/message_loop.h" |
11 #include "base/metrics/field_trial.h" | 11 #include "base/metrics/field_trial.h" |
12 #include "base/run_loop.h" | 12 #include "base/run_loop.h" |
13 #include "chrome/browser/metrics/variations/proto/chrome_experiments.pb.h" | |
14 #include "components/variations/entropy_provider.h" | 13 #include "components/variations/entropy_provider.h" |
| 14 #include "components/variations/proto/chrome_experiments.pb.h" |
15 #include "components/variations/variations_associated_data.h" | 15 #include "components/variations/variations_associated_data.h" |
16 #include "net/http/http_request_headers.h" | 16 #include "net/http/http_request_headers.h" |
17 #include "testing/gtest/include/gtest/gtest.h" | 17 #include "testing/gtest/include/gtest/gtest.h" |
18 #include "url/gurl.h" | 18 #include "url/gurl.h" |
19 | 19 |
20 namespace chrome_variations { | 20 // TODO(mathp): Remove once everything is under namespace 'variations'. |
| 21 using chrome_variations::GOOGLE_WEB_PROPERTIES; |
| 22 using chrome_variations::GOOGLE_WEB_PROPERTIES_TRIGGER; |
| 23 using chrome_variations::IDCollectionKey; |
| 24 using chrome_variations::VariationID; |
| 25 |
| 26 namespace variations { |
21 | 27 |
22 namespace { | 28 namespace { |
23 | 29 |
24 // Decodes the variations header and extracts the variation ids. | 30 // Decodes the variations header and extracts the variation ids. |
25 bool ExtractVariationIds(const std::string& variations, | 31 bool ExtractVariationIds(const std::string& variations, |
26 std::set<VariationID>* variation_ids, | 32 std::set<VariationID>* variation_ids, |
27 std::set<VariationID>* trigger_ids) { | 33 std::set<VariationID>* trigger_ids) { |
28 std::string serialized_proto; | 34 std::string serialized_proto; |
29 if (!base::Base64Decode(variations, &serialized_proto)) | 35 if (!base::Base64Decode(variations, &serialized_proto)) return false; |
30 return false; | |
31 metrics::ChromeVariations proto; | 36 metrics::ChromeVariations proto; |
32 if (!proto.ParseFromString(serialized_proto)) | 37 if (!proto.ParseFromString(serialized_proto)) return false; |
33 return false; | |
34 for (int i = 0; i < proto.variation_id_size(); ++i) | 38 for (int i = 0; i < proto.variation_id_size(); ++i) |
35 variation_ids->insert(proto.variation_id(i)); | 39 variation_ids->insert(proto.variation_id(i)); |
36 for (int i = 0; i < proto.trigger_variation_id_size(); ++i) | 40 for (int i = 0; i < proto.trigger_variation_id_size(); ++i) |
37 trigger_ids->insert(proto.trigger_variation_id(i)); | 41 trigger_ids->insert(proto.trigger_variation_id(i)); |
38 return true; | 42 return true; |
39 } | 43 } |
40 | 44 |
41 scoped_refptr<base::FieldTrial> CreateTrialAndAssociateId( | 45 scoped_refptr<base::FieldTrial> CreateTrialAndAssociateId( |
42 const std::string& trial_name, | 46 const std::string& trial_name, const std::string& default_group_name, |
43 const std::string& default_group_name, | 47 IDCollectionKey key, VariationID id) { |
44 IDCollectionKey key, | |
45 VariationID id) { | |
46 scoped_refptr<base::FieldTrial> trial( | 48 scoped_refptr<base::FieldTrial> trial( |
47 base::FieldTrialList::CreateFieldTrial(trial_name, default_group_name)); | 49 base::FieldTrialList::CreateFieldTrial(trial_name, default_group_name)); |
48 | 50 |
49 chrome_variations::AssociateGoogleVariationID(key, trial->trial_name(), | 51 chrome_variations::AssociateGoogleVariationID(key, trial->trial_name(), |
50 trial->group_name(), id); | 52 trial->group_name(), id); |
51 | 53 |
52 return trial; | 54 return trial; |
53 } | 55 } |
54 | 56 |
55 } // namespace | 57 } // namespace |
56 | 58 |
57 class VariationsHttpHeaderProviderTest : public ::testing::Test { | 59 class VariationsHttpHeaderProviderTest : public ::testing::Test { |
58 public: | 60 public: |
59 VariationsHttpHeaderProviderTest() {} | 61 VariationsHttpHeaderProviderTest() {} |
60 | 62 |
61 virtual ~VariationsHttpHeaderProviderTest() {} | 63 virtual ~VariationsHttpHeaderProviderTest() {} |
62 | 64 |
63 virtual void TearDown() OVERRIDE { | 65 virtual void TearDown() OVERRIDE { |
64 testing::ClearAllVariationIDs(); | 66 chrome_variations::testing::ClearAllVariationIDs(); |
65 } | 67 } |
66 }; | 68 }; |
67 | 69 |
68 TEST_F(VariationsHttpHeaderProviderTest, ShouldAppendHeaders) { | 70 TEST_F(VariationsHttpHeaderProviderTest, ShouldAppendHeaders) { |
69 struct { | 71 struct { |
70 const char* url; | 72 const char* url; |
71 bool should_append_headers; | 73 bool should_append_headers; |
72 } cases[] = { | 74 } cases[] = { |
73 { "http://google.com", true }, | 75 {"http://google.com", true}, |
74 { "http://www.google.com", true }, | 76 {"http://www.google.com", true}, |
75 { "http://m.google.com", true }, | 77 {"http://m.google.com", true}, |
76 { "http://google.ca", true }, | 78 {"http://google.ca", true}, |
77 { "https://google.ca", true }, | 79 {"https://google.ca", true}, |
78 { "http://google.co.uk", true }, | 80 {"http://google.co.uk", true}, |
79 { "http://google.co.uk:8080/", true }, | 81 {"http://google.co.uk:8080/", true}, |
80 { "http://www.google.co.uk:8080/", true }, | 82 {"http://www.google.co.uk:8080/", true}, |
81 { "http://google", false }, | 83 {"http://google", false}, |
82 | 84 |
83 { "http://youtube.com", true }, | 85 {"http://youtube.com", true}, |
84 { "http://www.youtube.com", true }, | 86 {"http://www.youtube.com", true}, |
85 { "http://www.youtube.ca", true }, | 87 {"http://www.youtube.ca", true}, |
86 { "http://www.youtube.co.uk:8080/", true }, | 88 {"http://www.youtube.co.uk:8080/", true}, |
87 { "https://www.youtube.com", true }, | 89 {"https://www.youtube.com", true}, |
88 { "http://youtube", false }, | 90 {"http://youtube", false}, |
89 | 91 |
90 { "http://www.yahoo.com", false }, | 92 {"http://www.yahoo.com", false}, |
91 | 93 |
92 { "http://ad.doubleclick.net", true }, | 94 {"http://ad.doubleclick.net", true}, |
93 { "https://a.b.c.doubleclick.net", true }, | 95 {"https://a.b.c.doubleclick.net", true}, |
94 { "https://a.b.c.doubleclick.net:8081", true }, | 96 {"https://a.b.c.doubleclick.net:8081", true}, |
95 { "http://www.doubleclick.com", true }, | 97 {"http://www.doubleclick.com", true}, |
96 { "http://www.doubleclick.org", false }, | 98 {"http://www.doubleclick.org", false}, |
97 { "http://www.doubleclick.net.com", false }, | 99 {"http://www.doubleclick.net.com", false}, |
98 { "https://www.doubleclick.net.com", false }, | 100 {"https://www.doubleclick.net.com", false}, |
99 | 101 |
100 { "http://ad.googlesyndication.com", true }, | 102 {"http://ad.googlesyndication.com", true}, |
101 { "https://a.b.c.googlesyndication.com", true }, | 103 {"https://a.b.c.googlesyndication.com", true}, |
102 { "https://a.b.c.googlesyndication.com:8080", true }, | 104 {"https://a.b.c.googlesyndication.com:8080", true}, |
103 { "http://www.doubleclick.edu", false }, | 105 {"http://www.doubleclick.edu", false}, |
104 { "http://www.googlesyndication.com.edu", false }, | 106 {"http://www.googlesyndication.com.edu", false}, |
105 { "https://www.googlesyndication.com.com", false }, | 107 {"https://www.googlesyndication.com.com", false}, |
106 | 108 |
107 { "http://www.googleadservices.com", true }, | 109 {"http://www.googleadservices.com", true}, |
108 { "http://www.googleadservices.com:8080", true }, | 110 {"http://www.googleadservices.com:8080", true}, |
109 { "https://www.googleadservices.com", true }, | 111 {"https://www.googleadservices.com", true}, |
110 { "https://www.internal.googleadservices.com", true }, | 112 {"https://www.internal.googleadservices.com", true}, |
111 { "https://www2.googleadservices.com", true }, | 113 {"https://www2.googleadservices.com", true}, |
112 { "https://www.googleadservices.org", false }, | 114 {"https://www.googleadservices.org", false}, |
113 { "https://www.googleadservices.com.co.uk", false }, | 115 {"https://www.googleadservices.com.co.uk", false}, |
114 | 116 |
115 { "http://WWW.ANDROID.COM", true }, | 117 {"http://WWW.ANDROID.COM", true}, |
116 { "http://www.android.com", true }, | 118 {"http://www.android.com", true}, |
117 { "http://www.doubleclick.com", true }, | 119 {"http://www.doubleclick.com", true}, |
118 { "http://www.doubleclick.net", true }, | 120 {"http://www.doubleclick.net", true}, |
119 { "http://www.ggpht.com", true }, | 121 {"http://www.ggpht.com", true}, |
120 { "http://www.googleadservices.com", true }, | 122 {"http://www.googleadservices.com", true}, |
121 { "http://www.googleapis.com", true }, | 123 {"http://www.googleapis.com", true}, |
122 { "http://www.googlesyndication.com", true }, | 124 {"http://www.googlesyndication.com", true}, |
123 { "http://www.googleusercontent.com", true }, | 125 {"http://www.googleusercontent.com", true}, |
124 { "http://www.googlevideo.com", true }, | 126 {"http://www.googlevideo.com", true}, |
125 { "http://ssl.gstatic.com", true }, | 127 {"http://ssl.gstatic.com", true}, |
126 { "http://www.gstatic.com", true }, | 128 {"http://www.gstatic.com", true}, |
127 { "http://www.ytimg.com", true }, | 129 {"http://www.ytimg.com", true}, |
128 { "http://wwwytimg.com", false }, | 130 {"http://wwwytimg.com", false}, |
129 { "http://ytimg.com", false }, | 131 {"http://ytimg.com", false}, |
130 | 132 |
131 { "http://www.android.org", false }, | 133 {"http://www.android.org", false}, |
132 { "http://www.doubleclick.org", false }, | 134 {"http://www.doubleclick.org", false}, |
133 { "http://www.doubleclick.net", true }, | 135 {"http://www.doubleclick.net", true}, |
134 { "http://www.ggpht.org", false }, | 136 {"http://www.ggpht.org", false}, |
135 { "http://www.googleadservices.org", false }, | 137 {"http://www.googleadservices.org", false}, |
136 { "http://www.googleapis.org", false }, | 138 {"http://www.googleapis.org", false}, |
137 { "http://www.googlesyndication.org", false }, | 139 {"http://www.googlesyndication.org", false}, |
138 { "http://www.googleusercontent.org", false }, | 140 {"http://www.googleusercontent.org", false}, |
139 { "http://www.googlevideo.org", false }, | 141 {"http://www.googlevideo.org", false}, |
140 { "http://ssl.gstatic.org", false }, | 142 {"http://ssl.gstatic.org", false}, |
141 { "http://www.gstatic.org", false }, | 143 {"http://www.gstatic.org", false}, |
142 { "http://www.ytimg.org", false }, | 144 {"http://www.ytimg.org", false}, |
143 | 145 |
144 { "http://a.b.android.com", true }, | 146 {"http://a.b.android.com", true}, |
145 { "http://a.b.doubleclick.com", true }, | 147 {"http://a.b.doubleclick.com", true}, |
146 { "http://a.b.doubleclick.net", true }, | 148 {"http://a.b.doubleclick.net", true}, |
147 { "http://a.b.ggpht.com", true }, | 149 {"http://a.b.ggpht.com", true}, |
148 { "http://a.b.googleadservices.com", true }, | 150 {"http://a.b.googleadservices.com", true}, |
149 { "http://a.b.googleapis.com", true }, | 151 {"http://a.b.googleapis.com", true}, |
150 { "http://a.b.googlesyndication.com", true }, | 152 {"http://a.b.googlesyndication.com", true}, |
151 { "http://a.b.googleusercontent.com", true }, | 153 {"http://a.b.googleusercontent.com", true}, |
152 { "http://a.b.googlevideo.com", true }, | 154 {"http://a.b.googlevideo.com", true}, |
153 { "http://ssl.gstatic.com", true }, | 155 {"http://ssl.gstatic.com", true}, |
154 { "http://a.b.gstatic.com", true }, | 156 {"http://a.b.gstatic.com", true}, |
155 { "http://a.b.ytimg.com", true }, | 157 {"http://a.b.ytimg.com", true}, |
156 | 158 |
157 }; | 159 }; |
158 | 160 |
159 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(cases); ++i) { | 161 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(cases); ++i) { |
160 const GURL url(cases[i].url); | 162 const GURL url(cases[i].url); |
161 EXPECT_EQ(cases[i].should_append_headers, | 163 EXPECT_EQ(cases[i].should_append_headers, |
162 VariationsHttpHeaderProvider::ShouldAppendHeaders(url)) << url; | 164 VariationsHttpHeaderProvider::ShouldAppendHeaders(url)) |
| 165 << url; |
163 } | 166 } |
164 } | 167 } |
165 | 168 |
166 TEST_F(VariationsHttpHeaderProviderTest, SetDefaultVariationIds_Valid) { | 169 TEST_F(VariationsHttpHeaderProviderTest, SetDefaultVariationIds_Valid) { |
167 base::MessageLoop loop; | 170 base::MessageLoop loop; |
168 VariationsHttpHeaderProvider provider; | 171 VariationsHttpHeaderProvider provider; |
169 GURL url("http://www.google.com"); | 172 GURL url("http://www.google.com"); |
170 net::HttpRequestHeaders headers; | 173 net::HttpRequestHeaders headers; |
171 std::string variations; | 174 std::string variations; |
172 | 175 |
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
232 std::string variations; | 235 std::string variations; |
233 headers.GetHeader("X-Client-Data", &variations); | 236 headers.GetHeader("X-Client-Data", &variations); |
234 | 237 |
235 std::set<VariationID> variation_ids; | 238 std::set<VariationID> variation_ids; |
236 std::set<VariationID> trigger_ids; | 239 std::set<VariationID> trigger_ids; |
237 ASSERT_TRUE(ExtractVariationIds(variations, &variation_ids, &trigger_ids)); | 240 ASSERT_TRUE(ExtractVariationIds(variations, &variation_ids, &trigger_ids)); |
238 EXPECT_TRUE(variation_ids.find(123) != variation_ids.end()); | 241 EXPECT_TRUE(variation_ids.find(123) != variation_ids.end()); |
239 EXPECT_TRUE(trigger_ids.find(456) != trigger_ids.end()); | 242 EXPECT_TRUE(trigger_ids.find(456) != trigger_ids.end()); |
240 } | 243 } |
241 | 244 |
242 } // namespace chrome_variations | 245 } // namespace variations |
OLD | NEW |