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