OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 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 <stddef.h> | 5 #include <stddef.h> |
6 #include <stdint.h> | 6 #include <stdint.h> |
7 | 7 |
| 8 #include "base/test/scoped_task_environment.h" |
8 #include "content/browser/appcache/appcache.h" | 9 #include "content/browser/appcache/appcache.h" |
9 #include "content/browser/appcache/appcache_host.h" | 10 #include "content/browser/appcache/appcache_host.h" |
10 #include "content/browser/appcache/mock_appcache_service.h" | 11 #include "content/browser/appcache/mock_appcache_service.h" |
11 #include "testing/gtest/include/gtest/gtest.h" | 12 #include "testing/gtest/include/gtest/gtest.h" |
12 | 13 |
13 namespace content { | 14 namespace content { |
14 | 15 |
15 namespace { | 16 namespace { |
16 | 17 |
17 class MockAppCacheFrontend : public AppCacheFrontend { | 18 class MockAppCacheFrontend : public AppCacheFrontend { |
(...skipping 11 matching lines...) Expand all Loading... |
29 const AppCacheErrorDetails& details) override {} | 30 const AppCacheErrorDetails& details) override {} |
30 void OnLogMessage(int host_id, | 31 void OnLogMessage(int host_id, |
31 AppCacheLogLevel log_level, | 32 AppCacheLogLevel log_level, |
32 const std::string& message) override {} | 33 const std::string& message) override {} |
33 void OnContentBlocked(int host_id, const GURL& manifest_url) override {} | 34 void OnContentBlocked(int host_id, const GURL& manifest_url) override {} |
34 }; | 35 }; |
35 | 36 |
36 } // namespace | 37 } // namespace |
37 | 38 |
38 class AppCacheTest : public testing::Test { | 39 class AppCacheTest : public testing::Test { |
| 40 base::test::ScopedTaskEnvironment scoped_task_environment_; |
39 }; | 41 }; |
40 | 42 |
41 TEST(AppCacheTest, CleanupUnusedCache) { | 43 TEST_F(AppCacheTest, CleanupUnusedCache) { |
42 MockAppCacheService service; | 44 MockAppCacheService service; |
43 MockAppCacheFrontend frontend; | 45 MockAppCacheFrontend frontend; |
44 scoped_refptr<AppCache> cache(new AppCache(service.storage(), 111)); | 46 scoped_refptr<AppCache> cache(new AppCache(service.storage(), 111)); |
45 cache->set_complete(true); | 47 cache->set_complete(true); |
46 scoped_refptr<AppCacheGroup> group( | 48 scoped_refptr<AppCacheGroup> group( |
47 new AppCacheGroup(service.storage(), GURL("http://blah/manifest"), 111)); | 49 new AppCacheGroup(service.storage(), GURL("http://blah/manifest"), 111)); |
48 group->AddCache(cache.get()); | 50 group->AddCache(cache.get()); |
49 | 51 |
50 AppCacheHost host1(1, &frontend, &service); | 52 AppCacheHost host1(1, &frontend, &service); |
51 AppCacheHost host2(2, &frontend, &service); | 53 AppCacheHost host2(2, &frontend, &service); |
52 | 54 |
53 host1.AssociateCompleteCache(cache.get()); | 55 host1.AssociateCompleteCache(cache.get()); |
54 host2.AssociateCompleteCache(cache.get()); | 56 host2.AssociateCompleteCache(cache.get()); |
55 | 57 |
56 host1.AssociateNoCache(GURL()); | 58 host1.AssociateNoCache(GURL()); |
57 host2.AssociateNoCache(GURL()); | 59 host2.AssociateNoCache(GURL()); |
58 } | 60 } |
59 | 61 |
60 TEST(AppCacheTest, AddModifyRemoveEntry) { | 62 TEST_F(AppCacheTest, AddModifyRemoveEntry) { |
61 MockAppCacheService service; | 63 MockAppCacheService service; |
62 scoped_refptr<AppCache> cache(new AppCache(service.storage(), 111)); | 64 scoped_refptr<AppCache> cache(new AppCache(service.storage(), 111)); |
63 | 65 |
64 EXPECT_TRUE(cache->entries().empty()); | 66 EXPECT_TRUE(cache->entries().empty()); |
65 EXPECT_EQ(0L, cache->cache_size()); | 67 EXPECT_EQ(0L, cache->cache_size()); |
66 | 68 |
67 const GURL kFooUrl("http://foo.com"); | 69 const GURL kFooUrl("http://foo.com"); |
68 const int64_t kFooResponseId = 1; | 70 const int64_t kFooResponseId = 1; |
69 const int64_t kFooSize = 100; | 71 const int64_t kFooSize = 100; |
70 AppCacheEntry entry1(AppCacheEntry::MASTER, kFooResponseId, kFooSize); | 72 AppCacheEntry entry1(AppCacheEntry::MASTER, kFooResponseId, kFooSize); |
(...skipping 23 matching lines...) Expand all Loading... |
94 | 96 |
95 EXPECT_EQ(entry2.types(), cache->GetEntry(kBarUrl)->types()); // unchanged | 97 EXPECT_EQ(entry2.types(), cache->GetEntry(kBarUrl)->types()); // unchanged |
96 | 98 |
97 cache->RemoveEntry(kBarUrl); | 99 cache->RemoveEntry(kBarUrl); |
98 EXPECT_EQ(kFooSize, cache->cache_size()); | 100 EXPECT_EQ(kFooSize, cache->cache_size()); |
99 cache->RemoveEntry(kFooUrl); | 101 cache->RemoveEntry(kFooUrl); |
100 EXPECT_EQ(0L, cache->cache_size()); | 102 EXPECT_EQ(0L, cache->cache_size()); |
101 EXPECT_TRUE(cache->entries().empty()); | 103 EXPECT_TRUE(cache->entries().empty()); |
102 } | 104 } |
103 | 105 |
104 TEST(AppCacheTest, InitializeWithManifest) { | 106 TEST_F(AppCacheTest, InitializeWithManifest) { |
105 MockAppCacheService service; | 107 MockAppCacheService service; |
106 | 108 |
107 scoped_refptr<AppCache> cache(new AppCache(service.storage(), 1234)); | 109 scoped_refptr<AppCache> cache(new AppCache(service.storage(), 1234)); |
108 EXPECT_TRUE(cache->fallback_namespaces_.empty()); | 110 EXPECT_TRUE(cache->fallback_namespaces_.empty()); |
109 EXPECT_TRUE(cache->online_whitelist_namespaces_.empty()); | 111 EXPECT_TRUE(cache->online_whitelist_namespaces_.empty()); |
110 EXPECT_FALSE(cache->online_whitelist_all_); | 112 EXPECT_FALSE(cache->online_whitelist_all_); |
111 | 113 |
112 AppCacheManifest manifest; | 114 AppCacheManifest manifest; |
113 manifest.explicit_urls.insert("http://one.com"); | 115 manifest.explicit_urls.insert("http://one.com"); |
114 manifest.explicit_urls.insert("http://two.com"); | 116 manifest.explicit_urls.insert("http://two.com"); |
(...skipping 23 matching lines...) Expand all Loading... |
138 EXPECT_EQ(GURL("http://w1.com"), whitelist[0].namespace_url); | 140 EXPECT_EQ(GURL("http://w1.com"), whitelist[0].namespace_url); |
139 EXPECT_EQ(GURL("http://w2.com"), whitelist[1].namespace_url); | 141 EXPECT_EQ(GURL("http://w2.com"), whitelist[1].namespace_url); |
140 EXPECT_TRUE(cache->online_whitelist_all_); | 142 EXPECT_TRUE(cache->online_whitelist_all_); |
141 | 143 |
142 // Ensure collections in manifest were taken over by the cache rather than | 144 // Ensure collections in manifest were taken over by the cache rather than |
143 // copied. | 145 // copied. |
144 EXPECT_TRUE(manifest.fallback_namespaces.empty()); | 146 EXPECT_TRUE(manifest.fallback_namespaces.empty()); |
145 EXPECT_TRUE(manifest.online_whitelist_namespaces.empty()); | 147 EXPECT_TRUE(manifest.online_whitelist_namespaces.empty()); |
146 } | 148 } |
147 | 149 |
148 TEST(AppCacheTest, FindResponseForRequest) { | 150 TEST_F(AppCacheTest, FindResponseForRequest) { |
149 MockAppCacheService service; | 151 MockAppCacheService service; |
150 | 152 |
151 const GURL kOnlineNamespaceUrl("http://blah/online_namespace"); | 153 const GURL kOnlineNamespaceUrl("http://blah/online_namespace"); |
152 const GURL kFallbackEntryUrl1("http://blah/fallback_entry1"); | 154 const GURL kFallbackEntryUrl1("http://blah/fallback_entry1"); |
153 const GURL kFallbackNamespaceUrl1("http://blah/fallback_namespace/"); | 155 const GURL kFallbackNamespaceUrl1("http://blah/fallback_namespace/"); |
154 const GURL kFallbackEntryUrl2("http://blah/fallback_entry2"); | 156 const GURL kFallbackEntryUrl2("http://blah/fallback_entry2"); |
155 const GURL kFallbackNamespaceUrl2("http://blah/fallback_namespace/longer"); | 157 const GURL kFallbackNamespaceUrl2("http://blah/fallback_namespace/longer"); |
156 const GURL kManifestUrl("http://blah/manifest"); | 158 const GURL kManifestUrl("http://blah/manifest"); |
157 const GURL kForeignExplicitEntryUrl("http://blah/foreign"); | 159 const GURL kForeignExplicitEntryUrl("http://blah/foreign"); |
158 const GURL kInOnlineNamespaceUrl( | 160 const GURL kInOnlineNamespaceUrl( |
(...skipping 191 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
350 &network_namespace); | 352 &network_namespace); |
351 EXPECT_TRUE(found); | 353 EXPECT_TRUE(found); |
352 EXPECT_EQ(kInterceptResponseId, entry.response_id()); | 354 EXPECT_EQ(kInterceptResponseId, entry.response_id()); |
353 EXPECT_EQ(kInterceptNamespaceEntry, | 355 EXPECT_EQ(kInterceptNamespaceEntry, |
354 cache->GetInterceptEntryUrl(intercept_namespace)); | 356 cache->GetInterceptEntryUrl(intercept_namespace)); |
355 EXPECT_FALSE(fallback_entry.has_response_id()); | 357 EXPECT_FALSE(fallback_entry.has_response_id()); |
356 EXPECT_TRUE(fallback_namespace.is_empty()); | 358 EXPECT_TRUE(fallback_namespace.is_empty()); |
357 EXPECT_FALSE(network_namespace); | 359 EXPECT_FALSE(network_namespace); |
358 } | 360 } |
359 | 361 |
360 TEST(AppCacheTest, FindInterceptPatternResponseForRequest) { | 362 TEST_F(AppCacheTest, FindInterceptPatternResponseForRequest) { |
361 MockAppCacheService service; | 363 MockAppCacheService service; |
362 | 364 |
363 // Setup an appcache with an intercept namespace that uses pattern matching. | 365 // Setup an appcache with an intercept namespace that uses pattern matching. |
364 const GURL kInterceptNamespaceBase("http://blah/intercept_namespace/"); | 366 const GURL kInterceptNamespaceBase("http://blah/intercept_namespace/"); |
365 const GURL kInterceptPatternNamespace( | 367 const GURL kInterceptPatternNamespace( |
366 kInterceptNamespaceBase.Resolve("*.hit*")); | 368 kInterceptNamespaceBase.Resolve("*.hit*")); |
367 const GURL kInterceptNamespaceEntry("http://blah/intercept_resource"); | 369 const GURL kInterceptNamespaceEntry("http://blah/intercept_resource"); |
368 const int64_t kInterceptResponseId = 1; | 370 const int64_t kInterceptResponseId = 1; |
369 AppCacheManifest manifest; | 371 AppCacheManifest manifest; |
370 manifest.intercept_namespaces.push_back( | 372 manifest.intercept_namespaces.push_back( |
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
421 &network_namespace); | 423 &network_namespace); |
422 EXPECT_TRUE(found); | 424 EXPECT_TRUE(found); |
423 EXPECT_EQ(kInterceptResponseId, entry.response_id()); | 425 EXPECT_EQ(kInterceptResponseId, entry.response_id()); |
424 EXPECT_EQ(kInterceptNamespaceEntry, | 426 EXPECT_EQ(kInterceptNamespaceEntry, |
425 cache->GetInterceptEntryUrl(intercept_namespace)); | 427 cache->GetInterceptEntryUrl(intercept_namespace)); |
426 EXPECT_FALSE(fallback_entry.has_response_id()); | 428 EXPECT_FALSE(fallback_entry.has_response_id()); |
427 EXPECT_TRUE(fallback_namespace.is_empty()); | 429 EXPECT_TRUE(fallback_namespace.is_empty()); |
428 EXPECT_FALSE(network_namespace); | 430 EXPECT_FALSE(network_namespace); |
429 } | 431 } |
430 | 432 |
431 TEST(AppCacheTest, FindFallbackPatternResponseForRequest) { | 433 TEST_F(AppCacheTest, FindFallbackPatternResponseForRequest) { |
432 MockAppCacheService service; | 434 MockAppCacheService service; |
433 | 435 |
434 // Setup an appcache with a fallback namespace that uses pattern matching. | 436 // Setup an appcache with a fallback namespace that uses pattern matching. |
435 const GURL kFallbackNamespaceBase("http://blah/fallback_namespace/"); | 437 const GURL kFallbackNamespaceBase("http://blah/fallback_namespace/"); |
436 const GURL kFallbackPatternNamespace( | 438 const GURL kFallbackPatternNamespace( |
437 kFallbackNamespaceBase.Resolve("*.hit*")); | 439 kFallbackNamespaceBase.Resolve("*.hit*")); |
438 const GURL kFallbackNamespaceEntry("http://blah/fallback_resource"); | 440 const GURL kFallbackNamespaceEntry("http://blah/fallback_resource"); |
439 const int64_t kFallbackResponseId = 1; | 441 const int64_t kFallbackResponseId = 1; |
440 AppCacheManifest manifest; | 442 AppCacheManifest manifest; |
441 manifest.fallback_namespaces.push_back( | 443 manifest.fallback_namespaces.push_back( |
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
492 &network_namespace); | 494 &network_namespace); |
493 EXPECT_TRUE(found); | 495 EXPECT_TRUE(found); |
494 EXPECT_FALSE(entry.has_response_id()); | 496 EXPECT_FALSE(entry.has_response_id()); |
495 EXPECT_EQ(kFallbackResponseId, fallback_entry.response_id()); | 497 EXPECT_EQ(kFallbackResponseId, fallback_entry.response_id()); |
496 EXPECT_EQ(kFallbackNamespaceEntry, | 498 EXPECT_EQ(kFallbackNamespaceEntry, |
497 cache->GetFallbackEntryUrl(fallback_namespace)); | 499 cache->GetFallbackEntryUrl(fallback_namespace)); |
498 EXPECT_TRUE(intercept_namespace.is_empty()); | 500 EXPECT_TRUE(intercept_namespace.is_empty()); |
499 EXPECT_FALSE(network_namespace); | 501 EXPECT_FALSE(network_namespace); |
500 } | 502 } |
501 | 503 |
502 | 504 TEST_F(AppCacheTest, FindNetworkNamespacePatternResponseForRequest) { |
503 TEST(AppCacheTest, FindNetworkNamespacePatternResponseForRequest) { | |
504 MockAppCacheService service; | 505 MockAppCacheService service; |
505 | 506 |
506 // Setup an appcache with a network namespace that uses pattern matching. | 507 // Setup an appcache with a network namespace that uses pattern matching. |
507 const GURL kNetworkNamespaceBase("http://blah/network_namespace/"); | 508 const GURL kNetworkNamespaceBase("http://blah/network_namespace/"); |
508 const GURL kNetworkPatternNamespace( | 509 const GURL kNetworkPatternNamespace( |
509 kNetworkNamespaceBase.Resolve("*.hit*")); | 510 kNetworkNamespaceBase.Resolve("*.hit*")); |
510 AppCacheManifest manifest; | 511 AppCacheManifest manifest; |
511 manifest.online_whitelist_namespaces.push_back( | 512 manifest.online_whitelist_namespaces.push_back( |
512 AppCacheNamespace(APPCACHE_NETWORK_NAMESPACE, kNetworkPatternNamespace, | 513 AppCacheNamespace(APPCACHE_NETWORK_NAMESPACE, kNetworkPatternNamespace, |
513 GURL(), true)); | 514 GURL(), true)); |
(...skipping 21 matching lines...) Expand all Loading... |
535 GURL("http://blah/network_namespace/path.hit"), | 536 GURL("http://blah/network_namespace/path.hit"), |
536 &entry, &intercept_namespace, | 537 &entry, &intercept_namespace, |
537 &fallback_entry, &fallback_namespace, | 538 &fallback_entry, &fallback_namespace, |
538 &network_namespace); | 539 &network_namespace); |
539 EXPECT_TRUE(found); | 540 EXPECT_TRUE(found); |
540 EXPECT_TRUE(network_namespace); | 541 EXPECT_TRUE(network_namespace); |
541 EXPECT_FALSE(entry.has_response_id()); | 542 EXPECT_FALSE(entry.has_response_id()); |
542 EXPECT_FALSE(fallback_entry.has_response_id()); | 543 EXPECT_FALSE(fallback_entry.has_response_id()); |
543 } | 544 } |
544 | 545 |
545 TEST(AppCacheTest, ToFromDatabaseRecords) { | 546 TEST_F(AppCacheTest, ToFromDatabaseRecords) { |
546 // Setup a cache with some entries. | 547 // Setup a cache with some entries. |
547 const int64_t kCacheId = 1234; | 548 const int64_t kCacheId = 1234; |
548 const int64_t kGroupId = 4321; | 549 const int64_t kGroupId = 4321; |
549 const GURL kManifestUrl("http://foo.com/manifest"); | 550 const GURL kManifestUrl("http://foo.com/manifest"); |
550 const GURL kInterceptUrl("http://foo.com/intercept.html"); | 551 const GURL kInterceptUrl("http://foo.com/intercept.html"); |
551 const GURL kFallbackUrl("http://foo.com/fallback.html"); | 552 const GURL kFallbackUrl("http://foo.com/fallback.html"); |
552 const GURL kWhitelistUrl("http://foo.com/whitelist*"); | 553 const GURL kWhitelistUrl("http://foo.com/whitelist*"); |
553 const std::string kData( | 554 const std::string kData( |
554 "CACHE MANIFEST\r" | 555 "CACHE MANIFEST\r" |
555 "CHROMIUM-INTERCEPT:\r" | 556 "CHROMIUM-INTERCEPT:\r" |
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
620 EXPECT_EQ(kFallbackUrl, | 621 EXPECT_EQ(kFallbackUrl, |
621 cache->GetFallbackEntryUrl(GURL("http://foo.com/"))); | 622 cache->GetFallbackEntryUrl(GURL("http://foo.com/"))); |
622 EXPECT_EQ(1 + 2 + 3, cache->cache_size()); | 623 EXPECT_EQ(1 + 2 + 3, cache->cache_size()); |
623 EXPECT_EQ(APPCACHE_NETWORK_NAMESPACE, | 624 EXPECT_EQ(APPCACHE_NETWORK_NAMESPACE, |
624 cache->online_whitelist_namespaces_[0].type); | 625 cache->online_whitelist_namespaces_[0].type); |
625 EXPECT_TRUE(cache->online_whitelist_namespaces_[0].is_pattern); | 626 EXPECT_TRUE(cache->online_whitelist_namespaces_[0].is_pattern); |
626 EXPECT_EQ(kWhitelistUrl, | 627 EXPECT_EQ(kWhitelistUrl, |
627 cache->online_whitelist_namespaces_[0].namespace_url); | 628 cache->online_whitelist_namespaces_[0].namespace_url); |
628 } | 629 } |
629 | 630 |
630 TEST(AppCacheTest, IsNamespaceMatch) { | 631 TEST_F(AppCacheTest, IsNamespaceMatch) { |
631 AppCacheNamespace prefix; | 632 AppCacheNamespace prefix; |
632 prefix.namespace_url = GURL("http://foo.com/prefix"); | 633 prefix.namespace_url = GURL("http://foo.com/prefix"); |
633 prefix.is_pattern = false; | 634 prefix.is_pattern = false; |
634 EXPECT_TRUE(prefix.IsMatch( | 635 EXPECT_TRUE(prefix.IsMatch( |
635 GURL("http://foo.com/prefix_and_anothing_goes"))); | 636 GURL("http://foo.com/prefix_and_anothing_goes"))); |
636 EXPECT_FALSE(prefix.IsMatch( | 637 EXPECT_FALSE(prefix.IsMatch( |
637 GURL("http://foo.com/nope"))); | 638 GURL("http://foo.com/nope"))); |
638 | 639 |
639 AppCacheNamespace bar_no_star; | 640 AppCacheNamespace bar_no_star; |
640 bar_no_star.namespace_url = GURL("http://foo.com/bar"); | 641 bar_no_star.namespace_url = GURL("http://foo.com/bar"); |
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
694 EXPECT_TRUE(star_greediness.IsMatch( | 695 EXPECT_TRUE(star_greediness.IsMatch( |
695 GURL("http://foo.com/a/b/01234567890abcdef/b"))); | 696 GURL("http://foo.com/a/b/01234567890abcdef/b"))); |
696 EXPECT_TRUE(star_greediness.IsMatch( | 697 EXPECT_TRUE(star_greediness.IsMatch( |
697 GURL("http://foo.com/a/b/01234567890abcdef/b01234567890abcdef/b"))); | 698 GURL("http://foo.com/a/b/01234567890abcdef/b01234567890abcdef/b"))); |
698 EXPECT_TRUE(star_greediness.IsMatch( | 699 EXPECT_TRUE(star_greediness.IsMatch( |
699 GURL("http://foo.com/a/b/01234567890abcdef_eat_some_more_characters_" | 700 GURL("http://foo.com/a/b/01234567890abcdef_eat_some_more_characters_" |
700 "/and_even_more_for_the_heck_of_it/01234567890abcdef/b"))); | 701 "/and_even_more_for_the_heck_of_it/01234567890abcdef/b"))); |
701 } | 702 } |
702 | 703 |
703 } // namespace content | 704 } // namespace content |
OLD | NEW |