Supervised user import: Listen for profile creation/deletion
[chromium-blink-merge.git] / content / browser / appcache / appcache_unittest.cc
blob4062498e95efaac123f9d363b0154a4d2bb78864
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 "content/browser/appcache/appcache.h"
6 #include "content/browser/appcache/appcache_host.h"
7 #include "content/browser/appcache/mock_appcache_service.h"
8 #include "testing/gtest/include/gtest/gtest.h"
10 namespace content {
12 namespace {
14 class MockAppCacheFrontend : public AppCacheFrontend {
15 public:
16 void OnCacheSelected(int host_id, const AppCacheInfo& info) override {}
17 void OnStatusChanged(const std::vector<int>& host_ids,
18 AppCacheStatus status) override {}
19 void OnEventRaised(const std::vector<int>& host_ids,
20 AppCacheEventID event_id) override {}
21 void OnProgressEventRaised(const std::vector<int>& host_ids,
22 const GURL& url,
23 int num_total,
24 int num_complete) override {}
25 void OnErrorEventRaised(const std::vector<int>& host_ids,
26 const AppCacheErrorDetails& details) override {}
27 void OnLogMessage(int host_id,
28 AppCacheLogLevel log_level,
29 const std::string& message) override {}
30 void OnContentBlocked(int host_id, const GURL& manifest_url) override {}
33 } // namespace
35 class AppCacheTest : public testing::Test {
38 TEST(AppCacheTest, CleanupUnusedCache) {
39 MockAppCacheService service;
40 MockAppCacheFrontend frontend;
41 scoped_refptr<AppCache> cache(new AppCache(service.storage(), 111));
42 cache->set_complete(true);
43 scoped_refptr<AppCacheGroup> group(
44 new AppCacheGroup(service.storage(), GURL("http://blah/manifest"), 111));
45 group->AddCache(cache.get());
47 AppCacheHost host1(1, &frontend, &service);
48 AppCacheHost host2(2, &frontend, &service);
50 host1.AssociateCompleteCache(cache.get());
51 host2.AssociateCompleteCache(cache.get());
53 host1.AssociateNoCache(GURL());
54 host2.AssociateNoCache(GURL());
57 TEST(AppCacheTest, AddModifyRemoveEntry) {
58 MockAppCacheService service;
59 scoped_refptr<AppCache> cache(new AppCache(service.storage(), 111));
61 EXPECT_TRUE(cache->entries().empty());
62 EXPECT_EQ(0L, cache->cache_size());
64 const GURL kFooUrl("http://foo.com");
65 const int64 kFooResponseId = 1;
66 const int64 kFooSize = 100;
67 AppCacheEntry entry1(AppCacheEntry::MASTER, kFooResponseId, kFooSize);
68 cache->AddEntry(kFooUrl, entry1);
69 EXPECT_EQ(entry1.types(), cache->GetEntry(kFooUrl)->types());
70 EXPECT_EQ(1UL, cache->entries().size());
71 EXPECT_EQ(kFooSize, cache->cache_size());
73 const GURL kBarUrl("http://bar.com");
74 const int64 kBarResponseId = 2;
75 const int64 kBarSize = 200;
76 AppCacheEntry entry2(AppCacheEntry::FALLBACK, kBarResponseId, kBarSize);
77 EXPECT_TRUE(cache->AddOrModifyEntry(kBarUrl, entry2));
78 EXPECT_EQ(entry2.types(), cache->GetEntry(kBarUrl)->types());
79 EXPECT_EQ(2UL, cache->entries().size());
80 EXPECT_EQ(kFooSize + kBarSize, cache->cache_size());
82 // Expected to return false when an existing entry is modified.
83 AppCacheEntry entry3(AppCacheEntry::EXPLICIT);
84 EXPECT_FALSE(cache->AddOrModifyEntry(kFooUrl, entry3));
85 EXPECT_EQ((AppCacheEntry::MASTER | AppCacheEntry::EXPLICIT),
86 cache->GetEntry(kFooUrl)->types());
87 // Only the type should be modified.
88 EXPECT_EQ(kFooResponseId, cache->GetEntry(kFooUrl)->response_id());
89 EXPECT_EQ(kFooSize, cache->GetEntry(kFooUrl)->response_size());
90 EXPECT_EQ(kFooSize + kBarSize, cache->cache_size());
92 EXPECT_EQ(entry2.types(), cache->GetEntry(kBarUrl)->types()); // unchanged
94 cache->RemoveEntry(kBarUrl);
95 EXPECT_EQ(kFooSize, cache->cache_size());
96 cache->RemoveEntry(kFooUrl);
97 EXPECT_EQ(0L, cache->cache_size());
98 EXPECT_TRUE(cache->entries().empty());
101 TEST(AppCacheTest, InitializeWithManifest) {
102 MockAppCacheService service;
104 scoped_refptr<AppCache> cache(new AppCache(service.storage(), 1234));
105 EXPECT_TRUE(cache->fallback_namespaces_.empty());
106 EXPECT_TRUE(cache->online_whitelist_namespaces_.empty());
107 EXPECT_FALSE(cache->online_whitelist_all_);
109 AppCacheManifest manifest;
110 manifest.explicit_urls.insert("http://one.com");
111 manifest.explicit_urls.insert("http://two.com");
112 manifest.fallback_namespaces.push_back(
113 AppCacheNamespace(APPCACHE_FALLBACK_NAMESPACE, GURL("http://fb1.com"),
114 GURL("http://fbone.com"), true));
115 manifest.online_whitelist_namespaces.push_back(
116 AppCacheNamespace(APPCACHE_NETWORK_NAMESPACE, GURL("http://w1.com"),
117 GURL(), false));
118 manifest.online_whitelist_namespaces.push_back(
119 AppCacheNamespace(APPCACHE_NETWORK_NAMESPACE, GURL("http://w2.com"),
120 GURL(), false));
121 manifest.online_whitelist_all = true;
123 cache->InitializeWithManifest(&manifest);
124 const std::vector<AppCacheNamespace>& fallbacks =
125 cache->fallback_namespaces_;
126 size_t expected = 1;
127 EXPECT_EQ(expected, fallbacks.size());
128 EXPECT_EQ(GURL("http://fb1.com"), fallbacks[0].namespace_url);
129 EXPECT_EQ(GURL("http://fbone.com"), fallbacks[0].target_url);
130 EXPECT_TRUE(fallbacks[0].is_pattern);
131 const AppCacheNamespaceVector& whitelist =
132 cache->online_whitelist_namespaces_;
133 expected = 2;
134 EXPECT_EQ(expected, whitelist.size());
135 EXPECT_EQ(GURL("http://w1.com"), whitelist[0].namespace_url);
136 EXPECT_EQ(GURL("http://w2.com"), whitelist[1].namespace_url);
137 EXPECT_TRUE(cache->online_whitelist_all_);
139 // Ensure collections in manifest were taken over by the cache rather than
140 // copied.
141 EXPECT_TRUE(manifest.fallback_namespaces.empty());
142 EXPECT_TRUE(manifest.online_whitelist_namespaces.empty());
145 TEST(AppCacheTest, FindResponseForRequest) {
146 MockAppCacheService service;
148 const GURL kOnlineNamespaceUrl("http://blah/online_namespace");
149 const GURL kFallbackEntryUrl1("http://blah/fallback_entry1");
150 const GURL kFallbackNamespaceUrl1("http://blah/fallback_namespace/");
151 const GURL kFallbackEntryUrl2("http://blah/fallback_entry2");
152 const GURL kFallbackNamespaceUrl2("http://blah/fallback_namespace/longer");
153 const GURL kManifestUrl("http://blah/manifest");
154 const GURL kForeignExplicitEntryUrl("http://blah/foreign");
155 const GURL kInOnlineNamespaceUrl(
156 "http://blah/online_namespace/network");
157 const GURL kExplicitInOnlineNamespaceUrl(
158 "http://blah/online_namespace/explicit");
159 const GURL kFallbackTestUrl1("http://blah/fallback_namespace/1");
160 const GURL kFallbackTestUrl2("http://blah/fallback_namespace/longer2");
161 const GURL kInterceptNamespace("http://blah/intercept_namespace/");
162 const GURL kInterceptNamespaceWithinFallback(
163 "http://blah/fallback_namespace/intercept_namespace/");
164 const GURL kInterceptNamespaceEntry("http://blah/intercept_entry");
165 const GURL kOnlineNamespaceWithinOtherNamespaces(
166 "http://blah/fallback_namespace/intercept_namespace/1/online");
168 const int64 kFallbackResponseId1 = 1;
169 const int64 kFallbackResponseId2 = 2;
170 const int64 kManifestResponseId = 3;
171 const int64 kForeignExplicitResponseId = 4;
172 const int64 kExplicitInOnlineNamespaceResponseId = 5;
173 const int64 kInterceptResponseId = 6;
175 AppCacheManifest manifest;
176 manifest.online_whitelist_namespaces.push_back(
177 AppCacheNamespace(APPCACHE_NETWORK_NAMESPACE, kOnlineNamespaceUrl,
178 GURL(), false));
179 manifest.online_whitelist_namespaces.push_back(
180 AppCacheNamespace(APPCACHE_NETWORK_NAMESPACE,
181 kOnlineNamespaceWithinOtherNamespaces, GURL(), false));
182 manifest.fallback_namespaces.push_back(
183 AppCacheNamespace(APPCACHE_FALLBACK_NAMESPACE, kFallbackNamespaceUrl1,
184 kFallbackEntryUrl1, false));
185 manifest.fallback_namespaces.push_back(
186 AppCacheNamespace(APPCACHE_FALLBACK_NAMESPACE, kFallbackNamespaceUrl2,
187 kFallbackEntryUrl2, false));
188 manifest.intercept_namespaces.push_back(
189 AppCacheNamespace(APPCACHE_INTERCEPT_NAMESPACE, kInterceptNamespace,
190 kInterceptNamespaceEntry, false));
191 manifest.intercept_namespaces.push_back(
192 AppCacheNamespace(APPCACHE_INTERCEPT_NAMESPACE,
193 kInterceptNamespaceWithinFallback, kInterceptNamespaceEntry, false));
195 // Create a cache with some namespaces and entries.
196 scoped_refptr<AppCache> cache(new AppCache(service.storage(), 1234));
197 cache->InitializeWithManifest(&manifest);
198 cache->AddEntry(
199 kFallbackEntryUrl1,
200 AppCacheEntry(AppCacheEntry::FALLBACK, kFallbackResponseId1));
201 cache->AddEntry(
202 kFallbackEntryUrl2,
203 AppCacheEntry(AppCacheEntry::FALLBACK, kFallbackResponseId2));
204 cache->AddEntry(
205 kManifestUrl,
206 AppCacheEntry(AppCacheEntry::MANIFEST, kManifestResponseId));
207 cache->AddEntry(
208 kForeignExplicitEntryUrl,
209 AppCacheEntry(AppCacheEntry::EXPLICIT | AppCacheEntry::FOREIGN,
210 kForeignExplicitResponseId));
211 cache->AddEntry(
212 kExplicitInOnlineNamespaceUrl,
213 AppCacheEntry(AppCacheEntry::EXPLICIT,
214 kExplicitInOnlineNamespaceResponseId));
215 cache->AddEntry(
216 kInterceptNamespaceEntry,
217 AppCacheEntry(AppCacheEntry::INTERCEPT, kInterceptResponseId));
218 cache->set_complete(true);
220 // See that we get expected results from FindResponseForRequest
222 bool found = false;
223 AppCacheEntry entry;
224 AppCacheEntry fallback_entry;
225 GURL intercept_namespace;
226 GURL fallback_namespace;
227 bool network_namespace = false;
229 found = cache->FindResponseForRequest(GURL("http://blah/miss"),
230 &entry, &intercept_namespace,
231 &fallback_entry, &fallback_namespace,
232 &network_namespace);
233 EXPECT_FALSE(found);
235 found = cache->FindResponseForRequest(kForeignExplicitEntryUrl,
236 &entry, &intercept_namespace,
237 &fallback_entry, &fallback_namespace,
238 &network_namespace);
239 EXPECT_TRUE(found);
240 EXPECT_EQ(kForeignExplicitResponseId, entry.response_id());
241 EXPECT_FALSE(fallback_entry.has_response_id());
242 EXPECT_FALSE(network_namespace);
244 entry = AppCacheEntry(); // reset
246 found = cache->FindResponseForRequest(kManifestUrl,
247 &entry, &intercept_namespace,
248 &fallback_entry, &fallback_namespace,
249 &network_namespace);
250 EXPECT_TRUE(found);
251 EXPECT_EQ(kManifestResponseId, entry.response_id());
252 EXPECT_FALSE(fallback_entry.has_response_id());
253 EXPECT_FALSE(network_namespace);
255 entry = AppCacheEntry(); // reset
257 found = cache->FindResponseForRequest(kInOnlineNamespaceUrl,
258 &entry, &intercept_namespace,
259 &fallback_entry, &fallback_namespace,
260 &network_namespace);
261 EXPECT_TRUE(found);
262 EXPECT_FALSE(entry.has_response_id());
263 EXPECT_FALSE(fallback_entry.has_response_id());
264 EXPECT_TRUE(network_namespace);
266 network_namespace = false; // reset
268 found = cache->FindResponseForRequest(kExplicitInOnlineNamespaceUrl,
269 &entry, &intercept_namespace,
270 &fallback_entry, &fallback_namespace,
271 &network_namespace);
272 EXPECT_TRUE(found);
273 EXPECT_EQ(kExplicitInOnlineNamespaceResponseId, entry.response_id());
274 EXPECT_FALSE(fallback_entry.has_response_id());
275 EXPECT_FALSE(network_namespace);
277 entry = AppCacheEntry(); // reset
279 found = cache->FindResponseForRequest(kFallbackTestUrl1,
280 &entry, &intercept_namespace,
281 &fallback_entry, &fallback_namespace,
282 &network_namespace);
283 EXPECT_TRUE(found);
284 EXPECT_FALSE(entry.has_response_id());
285 EXPECT_EQ(kFallbackResponseId1, fallback_entry.response_id());
286 EXPECT_EQ(kFallbackEntryUrl1,
287 cache->GetFallbackEntryUrl(fallback_namespace));
288 EXPECT_FALSE(network_namespace);
290 fallback_entry = AppCacheEntry(); // reset
292 found = cache->FindResponseForRequest(kFallbackTestUrl2,
293 &entry, &intercept_namespace,
294 &fallback_entry, &fallback_namespace,
295 &network_namespace);
296 EXPECT_TRUE(found);
297 EXPECT_FALSE(entry.has_response_id());
298 EXPECT_EQ(kFallbackResponseId2, fallback_entry.response_id());
299 EXPECT_EQ(kFallbackEntryUrl2,
300 cache->GetFallbackEntryUrl(fallback_namespace));
301 EXPECT_FALSE(network_namespace);
303 fallback_entry = AppCacheEntry(); // reset
305 found = cache->FindResponseForRequest(kOnlineNamespaceWithinOtherNamespaces,
306 &entry, &intercept_namespace,
307 &fallback_entry, &fallback_namespace,
308 &network_namespace);
309 EXPECT_TRUE(found);
310 EXPECT_FALSE(entry.has_response_id());
311 EXPECT_FALSE(fallback_entry.has_response_id());
312 EXPECT_TRUE(network_namespace);
314 fallback_entry = AppCacheEntry(); // reset
316 found = cache->FindResponseForRequest(
317 kOnlineNamespaceWithinOtherNamespaces.Resolve("online_resource"),
318 &entry, &intercept_namespace,
319 &fallback_entry, &fallback_namespace,
320 &network_namespace);
321 EXPECT_TRUE(found);
322 EXPECT_FALSE(entry.has_response_id());
323 EXPECT_FALSE(fallback_entry.has_response_id());
324 EXPECT_TRUE(network_namespace);
326 fallback_namespace = GURL();
328 found = cache->FindResponseForRequest(
329 kInterceptNamespace.Resolve("intercept_me"),
330 &entry, &intercept_namespace,
331 &fallback_entry, &fallback_namespace,
332 &network_namespace);
333 EXPECT_TRUE(found);
334 EXPECT_EQ(kInterceptResponseId, entry.response_id());
335 EXPECT_EQ(kInterceptNamespaceEntry,
336 cache->GetInterceptEntryUrl(intercept_namespace));
337 EXPECT_FALSE(fallback_entry.has_response_id());
338 EXPECT_TRUE(fallback_namespace.is_empty());
339 EXPECT_FALSE(network_namespace);
341 entry = AppCacheEntry(); // reset
343 found = cache->FindResponseForRequest(
344 kInterceptNamespaceWithinFallback.Resolve("intercept_me"),
345 &entry, &intercept_namespace,
346 &fallback_entry, &fallback_namespace,
347 &network_namespace);
348 EXPECT_TRUE(found);
349 EXPECT_EQ(kInterceptResponseId, entry.response_id());
350 EXPECT_EQ(kInterceptNamespaceEntry,
351 cache->GetInterceptEntryUrl(intercept_namespace));
352 EXPECT_FALSE(fallback_entry.has_response_id());
353 EXPECT_TRUE(fallback_namespace.is_empty());
354 EXPECT_FALSE(network_namespace);
357 TEST(AppCacheTest, FindInterceptPatternResponseForRequest) {
358 MockAppCacheService service;
360 // Setup an appcache with an intercept namespace that uses pattern matching.
361 const GURL kInterceptNamespaceBase("http://blah/intercept_namespace/");
362 const GURL kInterceptPatternNamespace(
363 kInterceptNamespaceBase.Resolve("*.hit*"));
364 const GURL kInterceptNamespaceEntry("http://blah/intercept_resource");
365 const int64 kInterceptResponseId = 1;
366 AppCacheManifest manifest;
367 manifest.intercept_namespaces.push_back(
368 AppCacheNamespace(APPCACHE_INTERCEPT_NAMESPACE,
369 kInterceptPatternNamespace, kInterceptNamespaceEntry, true));
370 scoped_refptr<AppCache> cache(new AppCache(service.storage(), 1234));
371 cache->InitializeWithManifest(&manifest);
372 cache->AddEntry(
373 kInterceptNamespaceEntry,
374 AppCacheEntry(AppCacheEntry::INTERCEPT, kInterceptResponseId));
375 cache->set_complete(true);
377 // See that the pattern match works.
378 bool found = false;
379 AppCacheEntry entry;
380 AppCacheEntry fallback_entry;
381 GURL intercept_namespace;
382 GURL fallback_namespace;
383 bool network_namespace = false;
385 found = cache->FindResponseForRequest(
386 GURL("http://blah/miss"),
387 &entry, &intercept_namespace,
388 &fallback_entry, &fallback_namespace,
389 &network_namespace);
390 EXPECT_FALSE(found);
392 found = cache->FindResponseForRequest(
393 GURL("http://blah/intercept_namespace/another_miss"),
394 &entry, &intercept_namespace,
395 &fallback_entry, &fallback_namespace,
396 &network_namespace);
397 EXPECT_FALSE(found);
399 found = cache->FindResponseForRequest(
400 GURL("http://blah/intercept_namespace/path.hit"),
401 &entry, &intercept_namespace,
402 &fallback_entry, &fallback_namespace,
403 &network_namespace);
404 EXPECT_TRUE(found);
405 EXPECT_EQ(kInterceptResponseId, entry.response_id());
406 EXPECT_EQ(kInterceptNamespaceEntry,
407 cache->GetInterceptEntryUrl(intercept_namespace));
408 EXPECT_FALSE(fallback_entry.has_response_id());
409 EXPECT_TRUE(fallback_namespace.is_empty());
410 EXPECT_FALSE(network_namespace);
412 entry = AppCacheEntry(); // reset
414 found = cache->FindResponseForRequest(
415 GURL("http://blah/intercept_namespace/longer/path.hit?arg=ok"),
416 &entry, &intercept_namespace,
417 &fallback_entry, &fallback_namespace,
418 &network_namespace);
419 EXPECT_TRUE(found);
420 EXPECT_EQ(kInterceptResponseId, entry.response_id());
421 EXPECT_EQ(kInterceptNamespaceEntry,
422 cache->GetInterceptEntryUrl(intercept_namespace));
423 EXPECT_FALSE(fallback_entry.has_response_id());
424 EXPECT_TRUE(fallback_namespace.is_empty());
425 EXPECT_FALSE(network_namespace);
428 TEST(AppCacheTest, FindFallbackPatternResponseForRequest) {
429 MockAppCacheService service;
431 // Setup an appcache with a fallback namespace that uses pattern matching.
432 const GURL kFallbackNamespaceBase("http://blah/fallback_namespace/");
433 const GURL kFallbackPatternNamespace(
434 kFallbackNamespaceBase.Resolve("*.hit*"));
435 const GURL kFallbackNamespaceEntry("http://blah/fallback_resource");
436 const int64 kFallbackResponseId = 1;
437 AppCacheManifest manifest;
438 manifest.fallback_namespaces.push_back(
439 AppCacheNamespace(APPCACHE_FALLBACK_NAMESPACE, kFallbackPatternNamespace,
440 kFallbackNamespaceEntry, true));
441 scoped_refptr<AppCache> cache(new AppCache(service.storage(), 1234));
442 cache->InitializeWithManifest(&manifest);
443 cache->AddEntry(
444 kFallbackNamespaceEntry,
445 AppCacheEntry(AppCacheEntry::FALLBACK, kFallbackResponseId));
446 cache->set_complete(true);
448 // See that the pattern match works.
449 bool found = false;
450 AppCacheEntry entry;
451 AppCacheEntry fallback_entry;
452 GURL intercept_namespace;
453 GURL fallback_namespace;
454 bool network_namespace = false;
456 found = cache->FindResponseForRequest(
457 GURL("http://blah/miss"),
458 &entry, &intercept_namespace,
459 &fallback_entry, &fallback_namespace,
460 &network_namespace);
461 EXPECT_FALSE(found);
463 found = cache->FindResponseForRequest(
464 GURL("http://blah/fallback_namespace/another_miss"),
465 &entry, &intercept_namespace,
466 &fallback_entry, &fallback_namespace,
467 &network_namespace);
468 EXPECT_FALSE(found);
470 found = cache->FindResponseForRequest(
471 GURL("http://blah/fallback_namespace/path.hit"),
472 &entry, &intercept_namespace,
473 &fallback_entry, &fallback_namespace,
474 &network_namespace);
475 EXPECT_TRUE(found);
476 EXPECT_FALSE(entry.has_response_id());
477 EXPECT_EQ(kFallbackResponseId, fallback_entry.response_id());
478 EXPECT_EQ(kFallbackNamespaceEntry,
479 cache->GetFallbackEntryUrl(fallback_namespace));
480 EXPECT_FALSE(network_namespace);
482 fallback_entry = AppCacheEntry();
483 fallback_namespace = GURL();
485 found = cache->FindResponseForRequest(
486 GURL("http://blah/fallback_namespace/longer/path.hit?arg=ok"),
487 &entry, &intercept_namespace,
488 &fallback_entry, &fallback_namespace,
489 &network_namespace);
490 EXPECT_TRUE(found);
491 EXPECT_FALSE(entry.has_response_id());
492 EXPECT_EQ(kFallbackResponseId, fallback_entry.response_id());
493 EXPECT_EQ(kFallbackNamespaceEntry,
494 cache->GetFallbackEntryUrl(fallback_namespace));
495 EXPECT_TRUE(intercept_namespace.is_empty());
496 EXPECT_FALSE(network_namespace);
500 TEST(AppCacheTest, FindNetworkNamespacePatternResponseForRequest) {
501 MockAppCacheService service;
503 // Setup an appcache with a network namespace that uses pattern matching.
504 const GURL kNetworkNamespaceBase("http://blah/network_namespace/");
505 const GURL kNetworkPatternNamespace(
506 kNetworkNamespaceBase.Resolve("*.hit*"));
507 AppCacheManifest manifest;
508 manifest.online_whitelist_namespaces.push_back(
509 AppCacheNamespace(APPCACHE_NETWORK_NAMESPACE, kNetworkPatternNamespace,
510 GURL(), true));
511 manifest.online_whitelist_all = false;
512 scoped_refptr<AppCache> cache(new AppCache(service.storage(), 1234));
513 cache->InitializeWithManifest(&manifest);
514 cache->set_complete(true);
516 // See that the pattern match works.
517 bool found = false;
518 AppCacheEntry entry;
519 AppCacheEntry fallback_entry;
520 GURL intercept_namespace;
521 GURL fallback_namespace;
522 bool network_namespace = false;
524 found = cache->FindResponseForRequest(
525 GURL("http://blah/miss"),
526 &entry, &intercept_namespace,
527 &fallback_entry, &fallback_namespace,
528 &network_namespace);
529 EXPECT_FALSE(found);
531 found = cache->FindResponseForRequest(
532 GURL("http://blah/network_namespace/path.hit"),
533 &entry, &intercept_namespace,
534 &fallback_entry, &fallback_namespace,
535 &network_namespace);
536 EXPECT_TRUE(found);
537 EXPECT_TRUE(network_namespace);
538 EXPECT_FALSE(entry.has_response_id());
539 EXPECT_FALSE(fallback_entry.has_response_id());
542 TEST(AppCacheTest, ToFromDatabaseRecords) {
543 // Setup a cache with some entries.
544 const int64 kCacheId = 1234;
545 const int64 kGroupId = 4321;
546 const GURL kManifestUrl("http://foo.com/manifest");
547 const GURL kInterceptUrl("http://foo.com/intercept.html");
548 const GURL kFallbackUrl("http://foo.com/fallback.html");
549 const GURL kWhitelistUrl("http://foo.com/whitelist*");
550 const std::string kData(
551 "CACHE MANIFEST\r"
552 "CHROMIUM-INTERCEPT:\r"
553 "/intercept return /intercept.html\r"
554 "FALLBACK:\r"
555 "/ /fallback.html\r"
556 "NETWORK:\r"
557 "/whitelist* isPattern\r"
558 "*\r");
559 MockAppCacheService service;
560 scoped_refptr<AppCacheGroup> group =
561 new AppCacheGroup(service.storage(), kManifestUrl, kGroupId);
562 scoped_refptr<AppCache> cache(new AppCache(service.storage(), kCacheId));
563 AppCacheManifest manifest;
564 EXPECT_TRUE(ParseManifest(kManifestUrl, kData.c_str(), kData.length(),
565 PARSE_MANIFEST_ALLOWING_INTERCEPTS, manifest));
566 cache->InitializeWithManifest(&manifest);
567 EXPECT_EQ(APPCACHE_NETWORK_NAMESPACE,
568 cache->online_whitelist_namespaces_[0].type);
569 EXPECT_TRUE(cache->online_whitelist_namespaces_[0].is_pattern);
570 EXPECT_EQ(kWhitelistUrl,
571 cache->online_whitelist_namespaces_[0].namespace_url);
572 cache->AddEntry(
573 kManifestUrl,
574 AppCacheEntry(AppCacheEntry::MANIFEST, 1, 1));
575 cache->AddEntry(
576 kInterceptUrl,
577 AppCacheEntry(AppCacheEntry::INTERCEPT, 3, 3));
578 cache->AddEntry(
579 kFallbackUrl,
580 AppCacheEntry(AppCacheEntry::FALLBACK, 2, 2));
582 // Get it to produce database records and verify them.
583 AppCacheDatabase::CacheRecord cache_record;
584 std::vector<AppCacheDatabase::EntryRecord> entries;
585 std::vector<AppCacheDatabase::NamespaceRecord> intercepts;
586 std::vector<AppCacheDatabase::NamespaceRecord> fallbacks;
587 std::vector<AppCacheDatabase::OnlineWhiteListRecord> whitelists;
588 cache->ToDatabaseRecords(group.get(),
589 &cache_record,
590 &entries,
591 &intercepts,
592 &fallbacks,
593 &whitelists);
594 EXPECT_EQ(kCacheId, cache_record.cache_id);
595 EXPECT_EQ(kGroupId, cache_record.group_id);
596 EXPECT_TRUE(cache_record.online_wildcard);
597 EXPECT_EQ(1 + 2 + 3, cache_record.cache_size);
598 EXPECT_EQ(3u, entries.size());
599 EXPECT_EQ(1u, intercepts.size());
600 EXPECT_EQ(1u, fallbacks.size());
601 EXPECT_EQ(1u, whitelists.size());
602 cache = NULL;
604 // Create a new AppCache and populate it with those records and verify.
605 cache = new AppCache(service.storage(), kCacheId);
606 cache->InitializeWithDatabaseRecords(
607 cache_record, entries, intercepts,
608 fallbacks, whitelists);
609 EXPECT_TRUE(cache->online_whitelist_all_);
610 EXPECT_EQ(3u, cache->entries().size());
611 EXPECT_TRUE(cache->GetEntry(kManifestUrl));
612 EXPECT_TRUE(cache->GetEntry(kInterceptUrl));
613 EXPECT_TRUE(cache->GetEntry(kFallbackUrl));
614 EXPECT_EQ(kInterceptUrl,
615 cache->GetInterceptEntryUrl(GURL("http://foo.com/intercept")));
616 EXPECT_EQ(kFallbackUrl,
617 cache->GetFallbackEntryUrl(GURL("http://foo.com/")));
618 EXPECT_EQ(1 + 2 + 3, cache->cache_size());
619 EXPECT_EQ(APPCACHE_NETWORK_NAMESPACE,
620 cache->online_whitelist_namespaces_[0].type);
621 EXPECT_TRUE(cache->online_whitelist_namespaces_[0].is_pattern);
622 EXPECT_EQ(kWhitelistUrl,
623 cache->online_whitelist_namespaces_[0].namespace_url);
626 TEST(AppCacheTest, IsNamespaceMatch) {
627 AppCacheNamespace prefix;
628 prefix.namespace_url = GURL("http://foo.com/prefix");
629 prefix.is_pattern = false;
630 EXPECT_TRUE(prefix.IsMatch(
631 GURL("http://foo.com/prefix_and_anothing_goes")));
632 EXPECT_FALSE(prefix.IsMatch(
633 GURL("http://foo.com/nope")));
635 AppCacheNamespace bar_no_star;
636 bar_no_star.namespace_url = GURL("http://foo.com/bar");
637 bar_no_star.is_pattern = true;
638 EXPECT_TRUE(bar_no_star.IsMatch(
639 GURL("http://foo.com/bar")));
640 EXPECT_FALSE(bar_no_star.IsMatch(
641 GURL("http://foo.com/bar/nope")));
643 AppCacheNamespace bar_star;
644 bar_star.namespace_url = GURL("http://foo.com/bar/*");
645 bar_star.is_pattern = true;
646 EXPECT_TRUE(bar_star.IsMatch(
647 GURL("http://foo.com/bar/")));
648 EXPECT_TRUE(bar_star.IsMatch(
649 GURL("http://foo.com/bar/should_match")));
650 EXPECT_FALSE(bar_star.IsMatch(
651 GURL("http://foo.com/not_bar/should_not_match")));
653 AppCacheNamespace star_bar_star;
654 star_bar_star.namespace_url = GURL("http://foo.com/*/bar/*");
655 star_bar_star.is_pattern = true;
656 EXPECT_TRUE(star_bar_star.IsMatch(
657 GURL("http://foo.com/any/bar/should_match")));
658 EXPECT_TRUE(star_bar_star.IsMatch(
659 GURL("http://foo.com/any/bar/")));
660 EXPECT_FALSE(star_bar_star.IsMatch(
661 GURL("http://foo.com/any/not_bar/no_match")));
663 AppCacheNamespace query_star_edit;
664 query_star_edit.namespace_url = GURL("http://foo.com/query?id=*&verb=edit*");
665 query_star_edit.is_pattern = true;
666 EXPECT_TRUE(query_star_edit.IsMatch(
667 GURL("http://foo.com/query?id=1234&verb=edit&option=blue")));
668 EXPECT_TRUE(query_star_edit.IsMatch(
669 GURL("http://foo.com/query?id=12345&option=blue&verb=edit")));
670 EXPECT_FALSE(query_star_edit.IsMatch(
671 GURL("http://foo.com/query?id=12345&option=blue&verb=print")));
672 EXPECT_TRUE(query_star_edit.IsMatch(
673 GURL("http://foo.com/query?id=123&verb=print&verb=edit")));
675 AppCacheNamespace star_greediness;
676 star_greediness.namespace_url = GURL("http://foo.com/*/b");
677 star_greediness.is_pattern = true;
678 EXPECT_TRUE(star_greediness.IsMatch(
679 GURL("http://foo.com/a/b")));
680 EXPECT_TRUE(star_greediness.IsMatch(
681 GURL("http://foo.com/a/wxy/z/b")));
682 EXPECT_TRUE(star_greediness.IsMatch(
683 GURL("http://foo.com/a/b/b")));
684 EXPECT_TRUE(star_greediness.IsMatch(
685 GURL("http://foo.com/b/b")));
686 EXPECT_TRUE(star_greediness.IsMatch(
687 GURL("http://foo.com/a/b/b/b/b/b")));
688 EXPECT_TRUE(star_greediness.IsMatch(
689 GURL("http://foo.com/a/b/b/b/a/b")));
690 EXPECT_TRUE(star_greediness.IsMatch(
691 GURL("http://foo.com/a/b/01234567890abcdef/b")));
692 EXPECT_TRUE(star_greediness.IsMatch(
693 GURL("http://foo.com/a/b/01234567890abcdef/b01234567890abcdef/b")));
694 EXPECT_TRUE(star_greediness.IsMatch(
695 GURL("http://foo.com/a/b/01234567890abcdef_eat_some_more_characters_"
696 "/and_even_more_for_the_heck_of_it/01234567890abcdef/b")));
699 } // namespace content