Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(516)

Side by Side Diff: components/variations/service/variations_service_unittest.cc

Issue 1404583004: Support gzip-compressed seed data from the variations server. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Fix signed-unsigned comparison issue Created 5 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 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/variations/service/variations_service.h" 5 #include "components/variations/service/variations_service.h"
6 6
7 #include <vector> 7 #include <vector>
8 8
9 #include "base/base64.h" 9 #include "base/base64.h"
10 #include "base/feature_list.h" 10 #include "base/feature_list.h"
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after
76 TestVariationsService( 76 TestVariationsService(
77 scoped_ptr<web_resource::TestRequestAllowedNotifier> test_notifier, 77 scoped_ptr<web_resource::TestRequestAllowedNotifier> test_notifier,
78 PrefService* local_state) 78 PrefService* local_state)
79 : VariationsService(make_scoped_ptr(new TestVariationsServiceClient()), 79 : VariationsService(make_scoped_ptr(new TestVariationsServiceClient()),
80 test_notifier.Pass(), 80 test_notifier.Pass(),
81 local_state, 81 local_state,
82 NULL, 82 NULL,
83 UIStringOverrider()), 83 UIStringOverrider()),
84 intercepts_fetch_(true), 84 intercepts_fetch_(true),
85 fetch_attempted_(false), 85 fetch_attempted_(false),
86 seed_stored_(false) { 86 seed_stored_(false),
87 delta_compressed_seed_(false),
88 gzip_compressed_seed_(false) {
87 // Set this so StartRepeatedVariationsSeedFetch can be called in tests. 89 // Set this so StartRepeatedVariationsSeedFetch can be called in tests.
88 SetCreateTrialsFromSeedCalledForTesting(true); 90 SetCreateTrialsFromSeedCalledForTesting(true);
89 } 91 }
90 92
91 ~TestVariationsService() override {} 93 ~TestVariationsService() override {}
92 94
93 void set_intercepts_fetch(bool value) { 95 void set_intercepts_fetch(bool value) {
94 intercepts_fetch_ = value; 96 intercepts_fetch_ = value;
95 } 97 }
96 98
97 bool fetch_attempted() const { return fetch_attempted_; } 99 bool fetch_attempted() const { return fetch_attempted_; }
98 bool seed_stored() const { return seed_stored_; } 100 bool seed_stored() const { return seed_stored_; }
99 const std::string& stored_country() const { return stored_country_; } 101 const std::string& stored_country() const { return stored_country_; }
102 bool delta_compressed_seed() const { return delta_compressed_seed_; }
103 bool gzip_compressed_seed() const { return gzip_compressed_seed_; }
100 104
101 void DoActualFetch() override { 105 void DoActualFetch() override {
102 if (intercepts_fetch_) { 106 if (intercepts_fetch_) {
103 fetch_attempted_ = true; 107 fetch_attempted_ = true;
104 return; 108 return;
105 } 109 }
106 110
107 VariationsService::DoActualFetch(); 111 VariationsService::DoActualFetch();
108 } 112 }
109 113
110 bool StoreSeed(const std::string& seed_data, 114 bool StoreSeed(const std::string& seed_data,
111 const std::string& seed_signature, 115 const std::string& seed_signature,
112 const std::string& country_code, 116 const std::string& country_code,
113 const base::Time& date_fetched, 117 const base::Time& date_fetched,
114 bool is_delta_compressed) override { 118 bool is_delta_compressed,
119 bool is_gzip_compressed) override {
115 seed_stored_ = true; 120 seed_stored_ = true;
116 stored_seed_data_ = seed_data; 121 stored_seed_data_ = seed_data;
117 stored_country_ = country_code; 122 stored_country_ = country_code;
123 delta_compressed_seed_ = is_delta_compressed;
124 gzip_compressed_seed_ = is_gzip_compressed;
118 return true; 125 return true;
119 } 126 }
120 127
121 private: 128 private:
122 bool LoadSeed(VariationsSeed* seed) override { 129 bool LoadSeed(VariationsSeed* seed) override {
123 if (!seed_stored_) 130 if (!seed_stored_)
124 return false; 131 return false;
125 return seed->ParseFromString(stored_seed_data_); 132 return seed->ParseFromString(stored_seed_data_);
126 } 133 }
127 134
128 bool intercepts_fetch_; 135 bool intercepts_fetch_;
129 bool fetch_attempted_; 136 bool fetch_attempted_;
130 bool seed_stored_; 137 bool seed_stored_;
131 std::string stored_seed_data_; 138 std::string stored_seed_data_;
132 std::string stored_country_; 139 std::string stored_country_;
140 bool delta_compressed_seed_;
141 bool gzip_compressed_seed_;
133 142
134 DISALLOW_COPY_AND_ASSIGN(TestVariationsService); 143 DISALLOW_COPY_AND_ASSIGN(TestVariationsService);
135 }; 144 };
136 145
137 class TestVariationsServiceObserver : public VariationsService::Observer { 146 class TestVariationsServiceObserver : public VariationsService::Observer {
138 public: 147 public:
139 TestVariationsServiceObserver() 148 TestVariationsServiceObserver()
140 : best_effort_changes_notified_(0), 149 : best_effort_changes_notified_(0),
141 crticial_changes_notified_(0) { 150 crticial_changes_notified_(0) {
142 } 151 }
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after
194 } 203 }
195 204
196 // Serializes |seed| to protobuf binary format. 205 // Serializes |seed| to protobuf binary format.
197 std::string SerializeSeed(const variations::VariationsSeed& seed) { 206 std::string SerializeSeed(const variations::VariationsSeed& seed) {
198 std::string serialized_seed; 207 std::string serialized_seed;
199 seed.SerializeToString(&serialized_seed); 208 seed.SerializeToString(&serialized_seed);
200 return serialized_seed; 209 return serialized_seed;
201 } 210 }
202 211
203 // Simulates a variations service response by setting a date header and the 212 // Simulates a variations service response by setting a date header and the
204 // specified HTTP |response_code| on |fetcher|. 213 // specified HTTP |response_code| on |fetcher|. Sets additional header |header|
214 // if it is not NULL.
205 scoped_refptr<net::HttpResponseHeaders> SimulateServerResponse( 215 scoped_refptr<net::HttpResponseHeaders> SimulateServerResponse(
206 int response_code, 216 int response_code,
207 net::TestURLFetcher* fetcher) { 217 net::TestURLFetcher* fetcher,
218 const std::string* header = NULL) {
Alexei Svitkine (slow) 2015/10/15 15:50:44 Default values for params are not allowed by our s
veranika 2015/10/15 20:02:57 But this doesn't apply for functions in unnamed na
Alexei Svitkine (slow) 2015/10/15 20:30:26 Looks like you're right, however we don't use it i
veranika 2015/10/15 22:10:37 Done.
208 EXPECT_TRUE(fetcher); 219 EXPECT_TRUE(fetcher);
209 scoped_refptr<net::HttpResponseHeaders> headers( 220 scoped_refptr<net::HttpResponseHeaders> headers(
210 new net::HttpResponseHeaders("date:Wed, 13 Feb 2013 00:25:24 GMT\0\0")); 221 new net::HttpResponseHeaders("date:Wed, 13 Feb 2013 00:25:24 GMT\0\0"));
222 if (header)
223 headers->AddHeader(*header);
211 fetcher->set_response_headers(headers); 224 fetcher->set_response_headers(headers);
212 fetcher->set_response_code(response_code); 225 fetcher->set_response_code(response_code);
213 return headers; 226 return headers;
214 } 227 }
215 228
216 // Converts |list_value| to a string, to make it easier for debugging. 229 // Converts |list_value| to a string, to make it easier for debugging.
217 std::string ListValueToString(const base::ListValue& list_value) { 230 std::string ListValueToString(const base::ListValue& list_value) {
218 std::string json; 231 std::string json;
219 JSONStringValueSerializer serializer(&json); 232 JSONStringValueSerializer serializer(&json);
220 serializer.set_pretty_print(true); 233 serializer.set_pretty_print(true);
(...skipping 25 matching lines...) Expand all
246 // this test. 259 // this test.
247 base::FieldTrialList field_trial_list(nullptr); 260 base::FieldTrialList field_trial_list(nullptr);
248 261
249 // Create a variations service. 262 // Create a variations service.
250 TestVariationsService service( 263 TestVariationsService service(
251 make_scoped_ptr(new web_resource::TestRequestAllowedNotifier(&prefs)), 264 make_scoped_ptr(new web_resource::TestRequestAllowedNotifier(&prefs)),
252 &prefs); 265 &prefs);
253 266
254 // Store a seed. 267 // Store a seed.
255 service.StoreSeed(SerializeSeed(CreateTestSeed()), std::string(), 268 service.StoreSeed(SerializeSeed(CreateTestSeed()), std::string(),
256 std::string(), base::Time::Now(), false); 269 std::string(), base::Time::Now(), false, false);
257 prefs.SetInt64(prefs::kVariationsLastFetchTime, 270 prefs.SetInt64(prefs::kVariationsLastFetchTime,
258 base::Time::Now().ToInternalValue()); 271 base::Time::Now().ToInternalValue());
259 272
260 // Check that field trials are created from the seed. Since the test study has 273 // Check that field trials are created from the seed. Since the test study has
261 // only 1 experiment with 100% probability weight, we must be part of it. 274 // only 1 experiment with 100% probability weight, we must be part of it.
262 EXPECT_TRUE(service.CreateTrialsFromSeed(base::FeatureList::GetInstance())); 275 EXPECT_TRUE(service.CreateTrialsFromSeed(base::FeatureList::GetInstance()));
263 EXPECT_EQ(base::FieldTrialList::FindFullName(kTestSeedStudyName), 276 EXPECT_EQ(base::FieldTrialList::FindFullName(kTestSeedStudyName),
264 kTestSeedExperimentName); 277 kTestSeedExperimentName);
265 } 278 }
266 279
(...skipping 10 matching lines...) Expand all
277 base::FieldTrialList field_trial_list(nullptr); 290 base::FieldTrialList field_trial_list(nullptr);
278 291
279 // Create a variations service 292 // Create a variations service
280 TestVariationsService service( 293 TestVariationsService service(
281 make_scoped_ptr(new web_resource::TestRequestAllowedNotifier(&prefs)), 294 make_scoped_ptr(new web_resource::TestRequestAllowedNotifier(&prefs)),
282 &prefs); 295 &prefs);
283 296
284 // Store a seed. To simulate a first run, |prefs::kVariationsLastFetchTime| 297 // Store a seed. To simulate a first run, |prefs::kVariationsLastFetchTime|
285 // is left empty. 298 // is left empty.
286 service.StoreSeed(SerializeSeed(CreateTestSeed()), std::string(), 299 service.StoreSeed(SerializeSeed(CreateTestSeed()), std::string(),
287 std::string(), base::Time::Now(), false); 300 std::string(), base::Time::Now(), false, false);
288 EXPECT_EQ(0, prefs.GetInt64(prefs::kVariationsLastFetchTime)); 301 EXPECT_EQ(0, prefs.GetInt64(prefs::kVariationsLastFetchTime));
289 302
290 // Check that field trials are created from the seed. Since the test study has 303 // Check that field trials are created from the seed. Since the test study has
291 // only 1 experiment with 100% probability weight, we must be part of it. 304 // only 1 experiment with 100% probability weight, we must be part of it.
292 EXPECT_TRUE(service.CreateTrialsFromSeed(base::FeatureList::GetInstance())); 305 EXPECT_TRUE(service.CreateTrialsFromSeed(base::FeatureList::GetInstance()));
293 EXPECT_EQ(base::FieldTrialList::FindFullName(kTestSeedStudyName), 306 EXPECT_EQ(base::FieldTrialList::FindFullName(kTestSeedStudyName),
294 kTestSeedExperimentName); 307 kTestSeedExperimentName);
295 } 308 }
296 309
297 TEST_F(VariationsServiceTest, CreateTrialsFromOutdatedSeed) { 310 TEST_F(VariationsServiceTest, CreateTrialsFromOutdatedSeed) {
(...skipping 10 matching lines...) Expand all
308 321
309 // Create a variations service. 322 // Create a variations service.
310 TestVariationsService service( 323 TestVariationsService service(
311 make_scoped_ptr(new web_resource::TestRequestAllowedNotifier(&prefs)), 324 make_scoped_ptr(new web_resource::TestRequestAllowedNotifier(&prefs)),
312 &prefs); 325 &prefs);
313 326
314 // Store a seed, with a fetch time 31 days in the past. 327 // Store a seed, with a fetch time 31 days in the past.
315 const base::Time seed_date = 328 const base::Time seed_date =
316 base::Time::Now() - base::TimeDelta::FromDays(31); 329 base::Time::Now() - base::TimeDelta::FromDays(31);
317 service.StoreSeed(SerializeSeed(CreateTestSeed()), std::string(), 330 service.StoreSeed(SerializeSeed(CreateTestSeed()), std::string(),
318 std::string(), seed_date, false); 331 std::string(), seed_date, false, false);
319 prefs.SetInt64(prefs::kVariationsLastFetchTime, seed_date.ToInternalValue()); 332 prefs.SetInt64(prefs::kVariationsLastFetchTime, seed_date.ToInternalValue());
320 333
321 // Check that field trials are not created from the seed. 334 // Check that field trials are not created from the seed.
322 EXPECT_FALSE(service.CreateTrialsFromSeed(base::FeatureList::GetInstance())); 335 EXPECT_FALSE(service.CreateTrialsFromSeed(base::FeatureList::GetInstance()));
323 EXPECT_TRUE(base::FieldTrialList::FindFullName(kTestSeedStudyName).empty()); 336 EXPECT_TRUE(base::FieldTrialList::FindFullName(kTestSeedStudyName).empty());
324 } 337 }
325 338
326 TEST_F(VariationsServiceTest, GetVariationsServerURL) { 339 TEST_F(VariationsServiceTest, GetVariationsServerURL) {
327 TestingPrefServiceSimple prefs; 340 TestingPrefServiceSimple prefs;
328 VariationsService::RegisterPrefs(prefs.registry()); 341 VariationsService::RegisterPrefs(prefs.registry());
(...skipping 133 matching lines...) Expand 10 before | Expand all | Expand 10 after
462 EXPECT_TRUE(prefs.FindPreference(prefs::kVariationsSeed)->IsDefaultValue()); 475 EXPECT_TRUE(prefs.FindPreference(prefs::kVariationsSeed)->IsDefaultValue());
463 476
464 net::TestURLFetcher* fetcher = factory.GetFetcherByID(0); 477 net::TestURLFetcher* fetcher = factory.GetFetcherByID(0);
465 SimulateServerResponse(non_ok_status_codes[i], fetcher); 478 SimulateServerResponse(non_ok_status_codes[i], fetcher);
466 service.OnURLFetchComplete(fetcher); 479 service.OnURLFetchComplete(fetcher);
467 480
468 EXPECT_TRUE(prefs.FindPreference(prefs::kVariationsSeed)->IsDefaultValue()); 481 EXPECT_TRUE(prefs.FindPreference(prefs::kVariationsSeed)->IsDefaultValue());
469 } 482 }
470 } 483 }
471 484
485 TEST_F(VariationsServiceTest, RequestGzipCompressedSeed) {
486 TestingPrefServiceSimple prefs;
487 VariationsService::RegisterPrefs(prefs.registry());
488 net::TestURLFetcherFactory factory;
489
490 TestVariationsService service(
491 make_scoped_ptr(new web_resource::TestRequestAllowedNotifier(&prefs)),
492 &prefs);
493 service.variations_server_url_ =
494 service.GetVariationsServerURL(&prefs, std::string());
Alexei Svitkine (slow) 2015/10/15 15:50:44 Could this line of code be moved into the TestVari
veranika 2015/10/15 20:02:57 VariationsService::variations_server_url_ is a pri
Alexei Svitkine (slow) 2015/10/15 20:30:26 I suggest providing a protected setter for it.
veranika 2015/10/15 22:10:37 Good idea, thanks.
495 service.set_intercepts_fetch(false);
496 service.DoActualFetch();
497
498 net::TestURLFetcher* fetcher = factory.GetFetcherByID(0);
499 net::HttpRequestHeaders headers;
500 fetcher->GetExtraRequestHeaders(&headers);
501 std::string field;
502 ASSERT_TRUE(headers.GetHeader("A-IM", &field));
503 EXPECT_EQ("gzip", field);
504 }
505
506 TEST_F(VariationsServiceTest, InstanceManipulations) {
507 struct {
508 std::string im;
509 bool delta_compressed;
510 bool gzip_compressed;
511 bool seed_stored;
512 } cases[] = {
513 {"", false, false, true},
514 {"IM:gzip", false, true, true},
515 {"IM:x-bm", true, false, true},
516 {"IM:x-bm,gzip", true, true, true},
517 {"IM: x-bm, gzip", true, true, true},
518 {"IM:gzip,x-bm", false, false, false},
519 {"IM:deflate,x-bm,gzip", false, false, false},
520 };
521
522 TestingPrefServiceSimple prefs;
523 VariationsService::RegisterPrefs(prefs.registry());
524 std::string serialized_seed = SerializeSeed(CreateTestSeed());
525 net::TestURLFetcherFactory factory;
526
527 for (size_t i = 0; i < arraysize(cases); ++i) {
528 TestVariationsService service(
529 make_scoped_ptr(new web_resource::TestRequestAllowedNotifier(&prefs)),
530 &prefs);
531 service.variations_server_url_ =
532 service.GetVariationsServerURL(&prefs, std::string());
533 service.set_intercepts_fetch(false);
534 service.DoActualFetch();
535 net::TestURLFetcher* fetcher = factory.GetFetcherByID(0);
536
537 if (cases[i].im.empty())
538 SimulateServerResponse(net::HTTP_OK, fetcher);
539 else
540 SimulateServerResponse(net::HTTP_OK, fetcher, &cases[i].im);
541 fetcher->SetResponseString(serialized_seed);
542 service.OnURLFetchComplete(fetcher);
543
544 EXPECT_EQ(cases[i].seed_stored, service.seed_stored());
545 EXPECT_EQ(cases[i].delta_compressed, service.delta_compressed_seed());
546 EXPECT_EQ(cases[i].gzip_compressed, service.gzip_compressed_seed());
547 }
548 }
549
472 TEST_F(VariationsServiceTest, CountryHeader) { 550 TEST_F(VariationsServiceTest, CountryHeader) {
473 TestingPrefServiceSimple prefs; 551 TestingPrefServiceSimple prefs;
474 VariationsService::RegisterPrefs(prefs.registry()); 552 VariationsService::RegisterPrefs(prefs.registry());
475 553
476 TestVariationsService service( 554 TestVariationsService service(
477 make_scoped_ptr(new web_resource::TestRequestAllowedNotifier(&prefs)), 555 make_scoped_ptr(new web_resource::TestRequestAllowedNotifier(&prefs)),
478 &prefs); 556 &prefs);
479 service.variations_server_url_ = 557 service.variations_server_url_ =
480 service.GetVariationsServerURL(&prefs, std::string()); 558 service.GetVariationsServerURL(&prefs, std::string());
481 service.set_intercepts_fetch(false); 559 service.set_intercepts_fetch(false);
(...skipping 156 matching lines...) Expand 10 before | Expand all | Expand 10 after
638 << test.pref_value_before << ", " << test.version << ", " 716 << test.pref_value_before << ", " << test.version << ", "
639 << test.latest_country_code; 717 << test.latest_country_code;
640 718
641 histogram_tester.ExpectUniqueSample( 719 histogram_tester.ExpectUniqueSample(
642 "Variations.LoadPermanentConsistencyCountryResult", 720 "Variations.LoadPermanentConsistencyCountryResult",
643 test.expected_result, 1); 721 test.expected_result, 1);
644 } 722 }
645 } 723 }
646 724
647 } // namespace variations 725 } // namespace variations
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698