OLD | NEW |
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 <time.h> | 5 #include <time.h> |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 #include <sstream> | 8 #include <sstream> |
9 #include <string> | 9 #include <string> |
10 | 10 |
(...skipping 246 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
257 | 257 |
258 testing_master.Shutdown(); | 258 testing_master.Shutdown(); |
259 } | 259 } |
260 | 260 |
261 //------------------------------------------------------------------------------ | 261 //------------------------------------------------------------------------------ |
262 // Functions to help synthesize and test serializations of subresource referrer | 262 // Functions to help synthesize and test serializations of subresource referrer |
263 // lists. | 263 // lists. |
264 | 264 |
265 // Return a motivation_list if we can find one for the given motivating_host (or | 265 // Return a motivation_list if we can find one for the given motivating_host (or |
266 // NULL if a match is not found). | 266 // NULL if a match is not found). |
267 static const ListValue* FindSerializationMotivation( | 267 static const base::ListValue* FindSerializationMotivation( |
268 const GURL& motivation, | 268 const GURL& motivation, |
269 const ListValue* referral_list) { | 269 const base::ListValue* referral_list) { |
270 CHECK_LT(0u, referral_list->GetSize()); // Room for version. | 270 CHECK_LT(0u, referral_list->GetSize()); // Room for version. |
271 int format_version = -1; | 271 int format_version = -1; |
272 CHECK(referral_list->GetInteger(0, &format_version)); | 272 CHECK(referral_list->GetInteger(0, &format_version)); |
273 CHECK_EQ(Predictor::kPredictorReferrerVersion, format_version); | 273 CHECK_EQ(Predictor::kPredictorReferrerVersion, format_version); |
274 const ListValue* motivation_list(NULL); | 274 const base::ListValue* motivation_list(NULL); |
275 for (size_t i = 1; i < referral_list->GetSize(); ++i) { | 275 for (size_t i = 1; i < referral_list->GetSize(); ++i) { |
276 referral_list->GetList(i, &motivation_list); | 276 referral_list->GetList(i, &motivation_list); |
277 std::string existing_spec; | 277 std::string existing_spec; |
278 EXPECT_TRUE(motivation_list->GetString(0, &existing_spec)); | 278 EXPECT_TRUE(motivation_list->GetString(0, &existing_spec)); |
279 if (motivation == GURL(existing_spec)) | 279 if (motivation == GURL(existing_spec)) |
280 return motivation_list; | 280 return motivation_list; |
281 } | 281 } |
282 return NULL; | 282 return NULL; |
283 } | 283 } |
284 | 284 |
285 static ListValue* FindSerializationMotivation(const GURL& motivation, | 285 static base::ListValue* FindSerializationMotivation( |
286 ListValue* referral_list) { | 286 const GURL& motivation, |
287 return const_cast<ListValue*>(FindSerializationMotivation( | 287 base::ListValue* referral_list) { |
288 motivation, static_cast<const ListValue*>(referral_list))); | 288 return const_cast<base::ListValue*>(FindSerializationMotivation( |
| 289 motivation, static_cast<const base::ListValue*>(referral_list))); |
289 } | 290 } |
290 | 291 |
291 // Create a new empty serialization list. | 292 // Create a new empty serialization list. |
292 static ListValue* NewEmptySerializationList() { | 293 static base::ListValue* NewEmptySerializationList() { |
293 base::ListValue* list = new base::ListValue; | 294 base::ListValue* list = new base::ListValue; |
294 list->Append( | 295 list->Append( |
295 new base::FundamentalValue(Predictor::kPredictorReferrerVersion)); | 296 new base::FundamentalValue(Predictor::kPredictorReferrerVersion)); |
296 return list; | 297 return list; |
297 } | 298 } |
298 | 299 |
299 // Add a motivating_url and a subresource_url to a serialized list, using | 300 // Add a motivating_url and a subresource_url to a serialized list, using |
300 // this given latency. This is a helper function for quickly building these | 301 // this given latency. This is a helper function for quickly building these |
301 // lists. | 302 // lists. |
302 static void AddToSerializedList(const GURL& motivation, | 303 static void AddToSerializedList(const GURL& motivation, |
303 const GURL& subresource, | 304 const GURL& subresource, |
304 double use_rate, | 305 double use_rate, |
305 ListValue* referral_list ) { | 306 base::ListValue* referral_list) { |
306 // Find the motivation if it is already used. | 307 // Find the motivation if it is already used. |
307 ListValue* motivation_list = FindSerializationMotivation(motivation, | 308 base::ListValue* motivation_list = FindSerializationMotivation(motivation, |
308 referral_list); | 309 referral_list); |
309 if (!motivation_list) { | 310 if (!motivation_list) { |
310 // This is the first mention of this motivation, so build a list. | 311 // This is the first mention of this motivation, so build a list. |
311 motivation_list = new ListValue; | 312 motivation_list = new base::ListValue; |
312 motivation_list->Append(new StringValue(motivation.spec())); | 313 motivation_list->Append(new base::StringValue(motivation.spec())); |
313 // Provide empty subresource list. | 314 // Provide empty subresource list. |
314 motivation_list->Append(new ListValue()); | 315 motivation_list->Append(new base::ListValue()); |
315 | 316 |
316 // ...and make it part of the serialized referral_list. | 317 // ...and make it part of the serialized referral_list. |
317 referral_list->Append(motivation_list); | 318 referral_list->Append(motivation_list); |
318 } | 319 } |
319 | 320 |
320 ListValue* subresource_list(NULL); | 321 base::ListValue* subresource_list(NULL); |
321 // 0 == url; 1 == subresource_list. | 322 // 0 == url; 1 == subresource_list. |
322 EXPECT_TRUE(motivation_list->GetList(1, &subresource_list)); | 323 EXPECT_TRUE(motivation_list->GetList(1, &subresource_list)); |
323 | 324 |
324 // We won't bother to check for the subresource being there already. Worst | 325 // We won't bother to check for the subresource being there already. Worst |
325 // case, during deserialization, the latency value we supply plus the | 326 // case, during deserialization, the latency value we supply plus the |
326 // existing value(s) will be added to the referrer. | 327 // existing value(s) will be added to the referrer. |
327 | 328 |
328 subresource_list->Append(new base::StringValue(subresource.spec())); | 329 subresource_list->Append(new base::StringValue(subresource.spec())); |
329 subresource_list->Append(new base::FundamentalValue(use_rate)); | 330 subresource_list->Append(new base::FundamentalValue(use_rate)); |
330 } | 331 } |
331 | 332 |
332 // For a given motivation, and subresource, find what latency is currently | 333 // For a given motivation, and subresource, find what latency is currently |
333 // listed. This assume a well formed serialization, which has at most one such | 334 // listed. This assume a well formed serialization, which has at most one such |
334 // entry for any pair of names. If no such pair is found, then return false. | 335 // entry for any pair of names. If no such pair is found, then return false. |
335 // Data is written into use_rate arguments. | 336 // Data is written into use_rate arguments. |
336 static bool GetDataFromSerialization(const GURL& motivation, | 337 static bool GetDataFromSerialization(const GURL& motivation, |
337 const GURL& subresource, | 338 const GURL& subresource, |
338 const ListValue& referral_list, | 339 const base::ListValue& referral_list, |
339 double* use_rate) { | 340 double* use_rate) { |
340 const ListValue* motivation_list = | 341 const base::ListValue* motivation_list = |
341 FindSerializationMotivation(motivation, &referral_list); | 342 FindSerializationMotivation(motivation, &referral_list); |
342 if (!motivation_list) | 343 if (!motivation_list) |
343 return false; | 344 return false; |
344 const ListValue* subresource_list; | 345 const base::ListValue* subresource_list; |
345 EXPECT_TRUE(motivation_list->GetList(1, &subresource_list)); | 346 EXPECT_TRUE(motivation_list->GetList(1, &subresource_list)); |
346 for (size_t i = 0; i < subresource_list->GetSize();) { | 347 for (size_t i = 0; i < subresource_list->GetSize();) { |
347 std::string url_spec; | 348 std::string url_spec; |
348 EXPECT_TRUE(subresource_list->GetString(i++, &url_spec)); | 349 EXPECT_TRUE(subresource_list->GetString(i++, &url_spec)); |
349 EXPECT_TRUE(subresource_list->GetDouble(i++, use_rate)); | 350 EXPECT_TRUE(subresource_list->GetDouble(i++, use_rate)); |
350 if (subresource == GURL(url_spec)) { | 351 if (subresource == GURL(url_spec)) { |
351 return true; | 352 return true; |
352 } | 353 } |
353 } | 354 } |
354 return false; | 355 return false; |
355 } | 356 } |
356 | 357 |
357 //------------------------------------------------------------------------------ | 358 //------------------------------------------------------------------------------ |
358 | 359 |
359 // Make sure nil referral lists really have no entries, and no latency listed. | 360 // Make sure nil referral lists really have no entries, and no latency listed. |
360 TEST_F(PredictorTest, ReferrerSerializationNilTest) { | 361 TEST_F(PredictorTest, ReferrerSerializationNilTest) { |
361 Predictor predictor(true); | 362 Predictor predictor(true); |
362 predictor.SetHostResolver(host_resolver_.get()); | 363 predictor.SetHostResolver(host_resolver_.get()); |
363 | 364 |
364 scoped_ptr<ListValue> referral_list(NewEmptySerializationList()); | 365 scoped_ptr<base::ListValue> referral_list(NewEmptySerializationList()); |
365 predictor.SerializeReferrers(referral_list.get()); | 366 predictor.SerializeReferrers(referral_list.get()); |
366 EXPECT_EQ(1U, referral_list->GetSize()); | 367 EXPECT_EQ(1U, referral_list->GetSize()); |
367 EXPECT_FALSE(GetDataFromSerialization( | 368 EXPECT_FALSE(GetDataFromSerialization( |
368 GURL("http://a.com:79"), GURL("http://b.com:78"), | 369 GURL("http://a.com:79"), GURL("http://b.com:78"), |
369 *referral_list.get(), NULL)); | 370 *referral_list.get(), NULL)); |
370 | 371 |
371 predictor.Shutdown(); | 372 predictor.Shutdown(); |
372 } | 373 } |
373 | 374 |
374 // Make sure that when a serialization list includes a value, that it can be | 375 // Make sure that when a serialization list includes a value, that it can be |
375 // deserialized into the database, and can be extracted back out via | 376 // deserialized into the database, and can be extracted back out via |
376 // serialization without being changed. | 377 // serialization without being changed. |
377 TEST_F(PredictorTest, ReferrerSerializationSingleReferrerTest) { | 378 TEST_F(PredictorTest, ReferrerSerializationSingleReferrerTest) { |
378 Predictor predictor(true); | 379 Predictor predictor(true); |
379 predictor.SetHostResolver(host_resolver_.get()); | 380 predictor.SetHostResolver(host_resolver_.get()); |
380 const GURL motivation_url("http://www.google.com:91"); | 381 const GURL motivation_url("http://www.google.com:91"); |
381 const GURL subresource_url("http://icons.google.com:90"); | 382 const GURL subresource_url("http://icons.google.com:90"); |
382 const double kUseRate = 23.4; | 383 const double kUseRate = 23.4; |
383 scoped_ptr<ListValue> referral_list(NewEmptySerializationList()); | 384 scoped_ptr<base::ListValue> referral_list(NewEmptySerializationList()); |
384 | 385 |
385 AddToSerializedList(motivation_url, subresource_url, | 386 AddToSerializedList(motivation_url, subresource_url, |
386 kUseRate, referral_list.get()); | 387 kUseRate, referral_list.get()); |
387 | 388 |
388 predictor.DeserializeReferrers(*referral_list.get()); | 389 predictor.DeserializeReferrers(*referral_list.get()); |
389 | 390 |
390 ListValue recovered_referral_list; | 391 base::ListValue recovered_referral_list; |
391 predictor.SerializeReferrers(&recovered_referral_list); | 392 predictor.SerializeReferrers(&recovered_referral_list); |
392 EXPECT_EQ(2U, recovered_referral_list.GetSize()); | 393 EXPECT_EQ(2U, recovered_referral_list.GetSize()); |
393 double rate; | 394 double rate; |
394 EXPECT_TRUE(GetDataFromSerialization( | 395 EXPECT_TRUE(GetDataFromSerialization( |
395 motivation_url, subresource_url, recovered_referral_list, &rate)); | 396 motivation_url, subresource_url, recovered_referral_list, &rate)); |
396 EXPECT_EQ(rate, kUseRate); | 397 EXPECT_EQ(rate, kUseRate); |
397 | 398 |
398 predictor.Shutdown(); | 399 predictor.Shutdown(); |
399 } | 400 } |
400 | 401 |
401 // Check that GetHtmlReferrerLists() doesn't crash when given duplicated | 402 // Check that GetHtmlReferrerLists() doesn't crash when given duplicated |
402 // domains for referring URL, and that it sorts the results in the | 403 // domains for referring URL, and that it sorts the results in the |
403 // correct order. | 404 // correct order. |
404 TEST_F(PredictorTest, GetHtmlReferrerLists) { | 405 TEST_F(PredictorTest, GetHtmlReferrerLists) { |
405 Predictor predictor(true); | 406 Predictor predictor(true); |
406 predictor.SetHostResolver(host_resolver_.get()); | 407 predictor.SetHostResolver(host_resolver_.get()); |
407 const double kUseRate = 23.4; | 408 const double kUseRate = 23.4; |
408 scoped_ptr<ListValue> referral_list(NewEmptySerializationList()); | 409 scoped_ptr<base::ListValue> referral_list(NewEmptySerializationList()); |
409 | 410 |
410 AddToSerializedList( | 411 AddToSerializedList( |
411 GURL("http://d.google.com/x1"), | 412 GURL("http://d.google.com/x1"), |
412 GURL("http://foo.com/"), | 413 GURL("http://foo.com/"), |
413 kUseRate, referral_list.get()); | 414 kUseRate, referral_list.get()); |
414 | 415 |
415 // Duplicated hostname (d.google.com). This should not cause any crashes | 416 // Duplicated hostname (d.google.com). This should not cause any crashes |
416 // (i.e. crbug.com/116345) | 417 // (i.e. crbug.com/116345) |
417 AddToSerializedList( | 418 AddToSerializedList( |
418 GURL("http://d.google.com/x2"), | 419 GURL("http://d.google.com/x2"), |
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
489 TEST_F(PredictorTest, ReferrerSerializationTrimTest) { | 490 TEST_F(PredictorTest, ReferrerSerializationTrimTest) { |
490 Predictor predictor(true); | 491 Predictor predictor(true); |
491 predictor.SetHostResolver(host_resolver_.get()); | 492 predictor.SetHostResolver(host_resolver_.get()); |
492 GURL motivation_url("http://www.google.com:110"); | 493 GURL motivation_url("http://www.google.com:110"); |
493 | 494 |
494 GURL icon_subresource_url("http://icons.google.com:111"); | 495 GURL icon_subresource_url("http://icons.google.com:111"); |
495 const double kRateIcon = 16.0 * Predictor::kDiscardableExpectedValue; | 496 const double kRateIcon = 16.0 * Predictor::kDiscardableExpectedValue; |
496 GURL img_subresource_url("http://img.google.com:118"); | 497 GURL img_subresource_url("http://img.google.com:118"); |
497 const double kRateImg = 8.0 * Predictor::kDiscardableExpectedValue; | 498 const double kRateImg = 8.0 * Predictor::kDiscardableExpectedValue; |
498 | 499 |
499 scoped_ptr<ListValue> referral_list(NewEmptySerializationList()); | 500 scoped_ptr<base::ListValue> referral_list(NewEmptySerializationList()); |
500 AddToSerializedList( | 501 AddToSerializedList( |
501 motivation_url, icon_subresource_url, kRateIcon, referral_list.get()); | 502 motivation_url, icon_subresource_url, kRateIcon, referral_list.get()); |
502 AddToSerializedList( | 503 AddToSerializedList( |
503 motivation_url, img_subresource_url, kRateImg, referral_list.get()); | 504 motivation_url, img_subresource_url, kRateImg, referral_list.get()); |
504 | 505 |
505 predictor.DeserializeReferrers(*referral_list.get()); | 506 predictor.DeserializeReferrers(*referral_list.get()); |
506 | 507 |
507 ListValue recovered_referral_list; | 508 base::ListValue recovered_referral_list; |
508 predictor.SerializeReferrers(&recovered_referral_list); | 509 predictor.SerializeReferrers(&recovered_referral_list); |
509 EXPECT_EQ(2U, recovered_referral_list.GetSize()); | 510 EXPECT_EQ(2U, recovered_referral_list.GetSize()); |
510 double rate; | 511 double rate; |
511 EXPECT_TRUE(GetDataFromSerialization( | 512 EXPECT_TRUE(GetDataFromSerialization( |
512 motivation_url, icon_subresource_url, recovered_referral_list, | 513 motivation_url, icon_subresource_url, recovered_referral_list, |
513 &rate)); | 514 &rate)); |
514 EXPECT_SIMILAR(rate, kRateIcon); | 515 EXPECT_SIMILAR(rate, kRateIcon); |
515 | 516 |
516 EXPECT_TRUE(GetDataFromSerialization( | 517 EXPECT_TRUE(GetDataFromSerialization( |
517 motivation_url, img_subresource_url, recovered_referral_list, &rate)); | 518 motivation_url, img_subresource_url, recovered_referral_list, &rate)); |
(...skipping 153 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
671 | 672 |
672 // Https works fine. | 673 // Https works fine. |
673 GURL long_https("https://host:999/path?query=value"); | 674 GURL long_https("https://host:999/path?query=value"); |
674 EXPECT_EQ(Predictor::CanonicalizeUrl(long_https), | 675 EXPECT_EQ(Predictor::CanonicalizeUrl(long_https), |
675 long_https.GetWithEmptyPath()); | 676 long_https.GetWithEmptyPath()); |
676 } | 677 } |
677 | 678 |
678 TEST_F(PredictorTest, DiscardPredictorResults) { | 679 TEST_F(PredictorTest, DiscardPredictorResults) { |
679 Predictor predictor(true); | 680 Predictor predictor(true); |
680 predictor.SetHostResolver(host_resolver_.get()); | 681 predictor.SetHostResolver(host_resolver_.get()); |
681 ListValue referral_list; | 682 base::ListValue referral_list; |
682 predictor.SerializeReferrers(&referral_list); | 683 predictor.SerializeReferrers(&referral_list); |
683 EXPECT_EQ(1U, referral_list.GetSize()); | 684 EXPECT_EQ(1U, referral_list.GetSize()); |
684 | 685 |
685 GURL host_1("http://test_1"); | 686 GURL host_1("http://test_1"); |
686 GURL host_2("http://test_2"); | 687 GURL host_2("http://test_2"); |
687 predictor.LearnFromNavigation(host_1, host_2); | 688 predictor.LearnFromNavigation(host_1, host_2); |
688 | 689 |
689 predictor.SerializeReferrers(&referral_list); | 690 predictor.SerializeReferrers(&referral_list); |
690 EXPECT_EQ(2U, referral_list.GetSize()); | 691 EXPECT_EQ(2U, referral_list.GetSize()); |
691 | 692 |
(...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
791 | 792 |
792 EXPECT_EQ(advisor->would_proxy_count_, 0); | 793 EXPECT_EQ(advisor->would_proxy_count_, 0); |
793 EXPECT_EQ(advisor->advise_count_, 1); | 794 EXPECT_EQ(advisor->advise_count_, 1); |
794 | 795 |
795 testing_master.Shutdown(); | 796 testing_master.Shutdown(); |
796 } | 797 } |
797 | 798 |
798 #endif // defined(OS_ANDROID) || defined(OS_IOS) | 799 #endif // defined(OS_ANDROID) || defined(OS_IOS) |
799 | 800 |
800 } // namespace chrome_browser_net | 801 } // namespace chrome_browser_net |
OLD | NEW |