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

Side by Side Diff: webkit/appcache/appcache_database_unittest.cc

Issue 8396013: AppCache INTERCEPT namespace. (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/
Patch Set: '' Created 9 years 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 | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2011 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 "testing/gtest/include/gtest/gtest.h" 5 #include "testing/gtest/include/gtest/gtest.h"
6 6
7 #include "base/file_util.h" 7 #include "base/file_util.h"
8 #include "base/scoped_temp_dir.h" 8 #include "base/scoped_temp_dir.h"
9 #include "base/stringprintf.h"
9 #include "sql/connection.h" 10 #include "sql/connection.h"
11 #include "sql/meta_table.h"
12 #include "sql/statement.h"
13 #include "sql/transaction.h"
10 #include "webkit/appcache/appcache_database.h" 14 #include "webkit/appcache/appcache_database.h"
11 #include "webkit/appcache/appcache_entry.h" 15 #include "webkit/appcache/appcache_entry.h"
12 16
13 namespace { 17 namespace {
14 18
15 const base::Time kZeroTime; 19 const base::Time kZeroTime;
16 20
17 class TestErrorDelegate : public sql::ErrorDelegate { 21 class TestErrorDelegate : public sql::ErrorDelegate {
18 public: 22 public:
19 virtual ~TestErrorDelegate() { } 23 virtual ~TestErrorDelegate() { }
(...skipping 295 matching lines...) Expand 10 before | Expand all | Expand 10 after
315 cache_record.update_time = kZeroTime; 319 cache_record.update_time = kZeroTime;
316 EXPECT_TRUE(db.InsertCache(&cache_record)); 320 EXPECT_TRUE(db.InsertCache(&cache_record));
317 321
318 record = kZeroRecord; 322 record = kZeroRecord;
319 EXPECT_TRUE(db.FindGroupForCache(1, &record)); 323 EXPECT_TRUE(db.FindGroupForCache(1, &record));
320 EXPECT_EQ(1, record.group_id); 324 EXPECT_EQ(1, record.group_id);
321 EXPECT_EQ(kManifest2, record.manifest_url); 325 EXPECT_EQ(kManifest2, record.manifest_url);
322 EXPECT_EQ(kOrigin2, record.origin); 326 EXPECT_EQ(kOrigin2, record.origin);
323 } 327 }
324 328
325 TEST(AppCacheDatabaseTest, FallbackNameSpaceRecords) { 329 TEST(AppCacheDatabaseTest, NamespaceRecords) {
326 const FilePath kEmptyPath; 330 const FilePath kEmptyPath;
327 AppCacheDatabase db(kEmptyPath); 331 AppCacheDatabase db(kEmptyPath);
328 EXPECT_TRUE(db.LazyOpen(true)); 332 EXPECT_TRUE(db.LazyOpen(true));
329 333
330 scoped_refptr<TestErrorDelegate> error_delegate(new TestErrorDelegate); 334 scoped_refptr<TestErrorDelegate> error_delegate(new TestErrorDelegate);
331 db.db_->set_error_delegate(error_delegate); 335 db.db_->set_error_delegate(error_delegate);
332 336
333 const GURL kFooNameSpace1("http://foo/namespace1"); 337 const GURL kFooNameSpace1("http://foo/namespace1");
334 const GURL kFooNameSpace2("http://foo/namespace2"); 338 const GURL kFooNameSpace2("http://foo/namespace2");
335 const GURL kFooFallbackEntry("http://foo/entry"); 339 const GURL kFooFallbackEntry("http://foo/entry");
336 const GURL kFooOrigin(kFooNameSpace1.GetOrigin()); 340 const GURL kFooOrigin(kFooNameSpace1.GetOrigin());
337 const GURL kBarNameSpace1("http://bar/namespace1"); 341 const GURL kBarNameSpace1("http://bar/namespace1");
338 const GURL kBarNameSpace2("http://bar/namespace2"); 342 const GURL kBarNameSpace2("http://bar/namespace2");
339 const GURL kBarFallbackEntry("http://bar/entry"); 343 const GURL kBarFallbackEntry("http://bar/entry");
340 const GURL kBarOrigin(kBarNameSpace1.GetOrigin()); 344 const GURL kBarOrigin(kBarNameSpace1.GetOrigin());
341 345
342 const AppCacheDatabase::FallbackNameSpaceRecord kZeroRecord; 346 const AppCacheDatabase::NamespaceRecord kZeroRecord;
343 AppCacheDatabase::FallbackNameSpaceRecord record; 347 AppCacheDatabase::NamespaceRecord record;
344 std::vector<AppCacheDatabase::FallbackNameSpaceRecord> records; 348 std::vector<AppCacheDatabase::NamespaceRecord> intercepts;
349 std::vector<AppCacheDatabase::NamespaceRecord> fallbacks;
345 350
346 // Behavior with an empty table 351 // Behavior with an empty table
347 EXPECT_TRUE(db.FindFallbackNameSpacesForCache(1, &records)); 352 EXPECT_TRUE(db.FindNamespacesForCache(1, &intercepts, &fallbacks));
348 EXPECT_TRUE(records.empty()); 353 EXPECT_TRUE(fallbacks.empty());
349 EXPECT_TRUE(db.FindFallbackNameSpacesForOrigin(kFooOrigin, &records)); 354 EXPECT_TRUE(db.FindNamespacesForOrigin(kFooOrigin, &intercepts, &fallbacks));
350 EXPECT_TRUE(records.empty()); 355 EXPECT_TRUE(fallbacks.empty());
351 EXPECT_TRUE(db.DeleteFallbackNameSpacesForCache(1)); 356 EXPECT_TRUE(db.DeleteNamespacesForCache(1));
352 357
353 // Two records for two differenent caches in the Foo origin. 358 // Two records for two differenent caches in the Foo origin.
354 record.cache_id = 1; 359 record.cache_id = 1;
355 record.origin = kFooOrigin; 360 record.origin = kFooOrigin;
356 record.namespace_url = kFooNameSpace1; 361 record.namespace_url = kFooNameSpace1;
357 record.fallback_entry_url = kFooFallbackEntry; 362 record.target_url = kFooFallbackEntry;
358 EXPECT_TRUE(db.InsertFallbackNameSpace(&record)); 363 EXPECT_TRUE(db.InsertNamespace(&record));
359 EXPECT_FALSE(db.InsertFallbackNameSpace(&record)); 364 EXPECT_FALSE(db.InsertNamespace(&record));
360 365
361 record.cache_id = 2; 366 record.cache_id = 2;
362 record.origin = kFooOrigin; 367 record.origin = kFooOrigin;
363 record.namespace_url = kFooNameSpace2; 368 record.namespace_url = kFooNameSpace2;
364 record.fallback_entry_url = kFooFallbackEntry; 369 record.target_url = kFooFallbackEntry;
365 EXPECT_TRUE(db.InsertFallbackNameSpace(&record)); 370 EXPECT_TRUE(db.InsertNamespace(&record));
366 371
367 records.clear(); 372 fallbacks.clear();
368 EXPECT_TRUE(db.FindFallbackNameSpacesForCache(1, &records)); 373 EXPECT_TRUE(db.FindNamespacesForCache(1, &intercepts, &fallbacks));
369 EXPECT_EQ(1U, records.size()); 374 EXPECT_EQ(1U, fallbacks.size());
370 EXPECT_EQ(1, records[0].cache_id); 375 EXPECT_EQ(1, fallbacks[0].cache_id);
371 EXPECT_EQ(kFooOrigin, records[0].origin); 376 EXPECT_EQ(kFooOrigin, fallbacks[0].origin);
372 EXPECT_EQ(kFooNameSpace1, records[0].namespace_url); 377 EXPECT_EQ(kFooNameSpace1, fallbacks[0].namespace_url);
373 EXPECT_EQ(kFooFallbackEntry, records[0].fallback_entry_url); 378 EXPECT_EQ(kFooFallbackEntry, fallbacks[0].target_url);
374 379
375 records.clear(); 380 fallbacks.clear();
376 EXPECT_TRUE(db.FindFallbackNameSpacesForCache(2, &records)); 381 EXPECT_TRUE(db.FindNamespacesForCache(2, &intercepts, &fallbacks));
377 EXPECT_EQ(1U, records.size()); 382 EXPECT_EQ(1U, fallbacks.size());
378 EXPECT_EQ(2, records[0].cache_id); 383 EXPECT_EQ(2, fallbacks[0].cache_id);
379 EXPECT_EQ(kFooOrigin, records[0].origin); 384 EXPECT_EQ(kFooOrigin, fallbacks[0].origin);
380 EXPECT_EQ(kFooNameSpace2, records[0].namespace_url); 385 EXPECT_EQ(kFooNameSpace2, fallbacks[0].namespace_url);
381 EXPECT_EQ(kFooFallbackEntry, records[0].fallback_entry_url); 386 EXPECT_EQ(kFooFallbackEntry, fallbacks[0].target_url);
382 387
383 records.clear(); 388 fallbacks.clear();
384 EXPECT_TRUE(db.FindFallbackNameSpacesForOrigin(kFooOrigin, &records)); 389 EXPECT_TRUE(db.FindNamespacesForOrigin(kFooOrigin, &intercepts, &fallbacks));
385 EXPECT_EQ(2U, records.size()); 390 EXPECT_EQ(2U, fallbacks.size());
386 EXPECT_EQ(1, records[0].cache_id); 391 EXPECT_EQ(1, fallbacks[0].cache_id);
387 EXPECT_EQ(kFooOrigin, records[0].origin); 392 EXPECT_EQ(kFooOrigin, fallbacks[0].origin);
388 EXPECT_EQ(kFooNameSpace1, records[0].namespace_url); 393 EXPECT_EQ(kFooNameSpace1, fallbacks[0].namespace_url);
389 EXPECT_EQ(kFooFallbackEntry, records[0].fallback_entry_url); 394 EXPECT_EQ(kFooFallbackEntry, fallbacks[0].target_url);
390 EXPECT_EQ(2, records[1].cache_id); 395 EXPECT_EQ(2, fallbacks[1].cache_id);
391 EXPECT_EQ(kFooOrigin, records[1].origin); 396 EXPECT_EQ(kFooOrigin, fallbacks[1].origin);
392 EXPECT_EQ(kFooNameSpace2, records[1].namespace_url); 397 EXPECT_EQ(kFooNameSpace2, fallbacks[1].namespace_url);
393 EXPECT_EQ(kFooFallbackEntry, records[1].fallback_entry_url); 398 EXPECT_EQ(kFooFallbackEntry, fallbacks[1].target_url);
394 399
395 EXPECT_TRUE(db.DeleteFallbackNameSpacesForCache(1)); 400 EXPECT_TRUE(db.DeleteNamespacesForCache(1));
396 records.clear(); 401 fallbacks.clear();
397 EXPECT_TRUE(db.FindFallbackNameSpacesForOrigin(kFooOrigin, &records)); 402 EXPECT_TRUE(db.FindNamespacesForOrigin(kFooOrigin, &intercepts, &fallbacks));
398 EXPECT_EQ(1U, records.size()); 403 EXPECT_EQ(1U, fallbacks.size());
399 EXPECT_EQ(2, records[0].cache_id); 404 EXPECT_EQ(2, fallbacks[0].cache_id);
400 EXPECT_EQ(kFooOrigin, records[0].origin); 405 EXPECT_EQ(kFooOrigin, fallbacks[0].origin);
401 EXPECT_EQ(kFooNameSpace2, records[0].namespace_url); 406 EXPECT_EQ(kFooNameSpace2, fallbacks[0].namespace_url);
402 EXPECT_EQ(kFooFallbackEntry, records[0].fallback_entry_url); 407 EXPECT_EQ(kFooFallbackEntry, fallbacks[0].target_url);
403 408
404 // Two more records for the same cache in the Bar origin. 409 // Two more records for the same cache in the Bar origin.
405 record.cache_id = 3; 410 record.cache_id = 3;
406 record.origin = kBarOrigin; 411 record.origin = kBarOrigin;
407 record.namespace_url = kBarNameSpace1; 412 record.namespace_url = kBarNameSpace1;
408 record.fallback_entry_url = kBarFallbackEntry; 413 record.target_url = kBarFallbackEntry;
409 EXPECT_TRUE(db.InsertFallbackNameSpace(&record)); 414 EXPECT_TRUE(db.InsertNamespace(&record));
410 415
411 record.cache_id = 3; 416 record.cache_id = 3;
412 record.origin = kBarOrigin; 417 record.origin = kBarOrigin;
413 record.namespace_url = kBarNameSpace2; 418 record.namespace_url = kBarNameSpace2;
414 record.fallback_entry_url = kBarFallbackEntry; 419 record.target_url = kBarFallbackEntry;
415 EXPECT_TRUE(db.InsertFallbackNameSpace(&record)); 420 EXPECT_TRUE(db.InsertNamespace(&record));
416 421
417 records.clear(); 422 fallbacks.clear();
418 EXPECT_TRUE(db.FindFallbackNameSpacesForCache(3, &records)); 423 EXPECT_TRUE(db.FindNamespacesForCache(3, &intercepts, &fallbacks));
419 EXPECT_EQ(2U, records.size()); 424 EXPECT_EQ(2U, fallbacks.size());
420 records.clear(); 425 fallbacks.clear();
421 EXPECT_TRUE(db.FindFallbackNameSpacesForOrigin(kBarOrigin, &records)); 426 EXPECT_TRUE(db.FindNamespacesForOrigin(kBarOrigin, &intercepts, &fallbacks));
422 EXPECT_EQ(2U, records.size()); 427 EXPECT_EQ(2U, fallbacks.size());
423 } 428 }
424 429
425 TEST(AppCacheDatabaseTest, OnlineWhiteListRecords) { 430 TEST(AppCacheDatabaseTest, OnlineWhiteListRecords) {
426 const FilePath kEmptyPath; 431 const FilePath kEmptyPath;
427 AppCacheDatabase db(kEmptyPath); 432 AppCacheDatabase db(kEmptyPath);
428 EXPECT_TRUE(db.LazyOpen(true)); 433 EXPECT_TRUE(db.LazyOpen(true));
429 434
430 scoped_refptr<TestErrorDelegate> error_delegate(new TestErrorDelegate); 435 scoped_refptr<TestErrorDelegate> error_delegate(new TestErrorDelegate);
431 db.db_->set_error_delegate(error_delegate); 436 db.db_->set_error_delegate(error_delegate);
432 437
(...skipping 173 matching lines...) Expand 10 before | Expand all | Expand 10 after
606 EXPECT_TRUE(db.FindCachesForOrigin(kOtherOrigin, &cache_records)); 611 EXPECT_TRUE(db.FindCachesForOrigin(kOtherOrigin, &cache_records));
607 EXPECT_EQ(1U, cache_records.size()); 612 EXPECT_EQ(1U, cache_records.size());
608 613
609 std::map<GURL, int64> usage_map; 614 std::map<GURL, int64> usage_map;
610 EXPECT_TRUE(db.GetAllOriginUsage(&usage_map)); 615 EXPECT_TRUE(db.GetAllOriginUsage(&usage_map));
611 EXPECT_EQ(2U, usage_map.size()); 616 EXPECT_EQ(2U, usage_map.size());
612 EXPECT_EQ(1100, usage_map[kOrigin]); 617 EXPECT_EQ(1100, usage_map[kOrigin]);
613 EXPECT_EQ(5000, usage_map[kOtherOrigin]); 618 EXPECT_EQ(5000, usage_map[kOtherOrigin]);
614 } 619 }
615 620
621 TEST(AppCacheDatabaseTest, UpgradeSchema3to4) {
622 // Real file on disk for this test.
623 ScopedTempDir temp_dir;
624 ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
625 const FilePath kDbFile = temp_dir.path().AppendASCII("upgrade.db");
626
627 const GURL kMockOrigin("http://mockorigin/");
628 const char kNamespaceUrlFormat[] = "namespace%d";
629 const char kTargetUrlFormat[] = "target%d";
630 const int kNumNamespaces = 10;
631
632 // Create a v3 schema based database containing some fallback records.
633 {
634 const int kVersion3 = 3;
635 const char kGroupsTable[] = "Groups";
636 const char kCachesTable[] = "Caches";
637 const char kEntriesTable[] = "Entries";
638 const char kFallbackNameSpacesTable[] = "FallbackNameSpaces";
639 const char kOnlineWhiteListsTable[] = "OnlineWhiteLists";
640 const char kDeletableResponseIdsTable[] = "DeletableResponseIds";
641
642 const struct {
643 const char* table_name;
644 const char* columns;
645 } kTables3[] = {
646 { kGroupsTable,
647 "(group_id INTEGER PRIMARY KEY,"
648 " origin TEXT,"
649 " manifest_url TEXT,"
650 " creation_time INTEGER,"
651 " last_access_time INTEGER)" },
652
653 { kCachesTable,
654 "(cache_id INTEGER PRIMARY KEY,"
655 " group_id INTEGER,"
656 " online_wildcard INTEGER CHECK(online_wildcard IN (0, 1)),"
657 " update_time INTEGER,"
658 " cache_size INTEGER)" }, // intentionally not normalized
659
660 { kEntriesTable,
661 "(cache_id INTEGER,"
662 " url TEXT,"
663 " flags INTEGER,"
664 " response_id INTEGER,"
665 " response_size INTEGER)" },
666
667 { kFallbackNameSpacesTable,
668 "(cache_id INTEGER,"
669 " origin TEXT," // intentionally not normalized
670 " namespace_url TEXT,"
671 " fallback_entry_url TEXT)" },
672
673 { kOnlineWhiteListsTable,
674 "(cache_id INTEGER,"
675 " namespace_url TEXT)" },
676
677 { kDeletableResponseIdsTable,
678 "(response_id INTEGER NOT NULL)" },
679 };
680
681 const struct {
682 const char* index_name;
683 const char* table_name;
684 const char* columns;
685 bool unique;
686 } kIndexes3[] = {
687 { "GroupsOriginIndex",
688 kGroupsTable,
689 "(origin)",
690 false },
691
692 { "GroupsManifestIndex",
693 kGroupsTable,
694 "(manifest_url)",
695 true },
696
697 { "CachesGroupIndex",
698 kCachesTable,
699 "(group_id)",
700 false },
701
702 { "EntriesCacheIndex",
703 kEntriesTable,
704 "(cache_id)",
705 false },
706
707 { "EntriesCacheAndUrlIndex",
708 kEntriesTable,
709 "(cache_id, url)",
710 true },
711
712 { "EntriesResponseIdIndex",
713 kEntriesTable,
714 "(response_id)",
715 true },
716
717 { "FallbackNameSpacesCacheIndex",
718 kFallbackNameSpacesTable,
719 "(cache_id)",
720 false },
721
722 { "FallbackNameSpacesOriginIndex",
723 kFallbackNameSpacesTable,
724 "(origin)",
725 false },
726
727 { "FallbackNameSpacesCacheAndUrlIndex",
728 kFallbackNameSpacesTable,
729 "(cache_id, namespace_url)",
730 true },
731
732 { "OnlineWhiteListCacheIndex",
733 kOnlineWhiteListsTable,
734 "(cache_id)",
735 false },
736
737 { "DeletableResponsesIdIndex",
738 kDeletableResponseIdsTable,
739 "(response_id)",
740 true },
741 };
742
743 const int kTableCount3 = ARRAYSIZE_UNSAFE(kTables3);
744 const int kIndexCount3 = ARRAYSIZE_UNSAFE(kIndexes3);
745
746 sql::Connection connection;
747 EXPECT_TRUE(connection.Open(kDbFile));
748
749 sql::Transaction transaction(&connection);
750 EXPECT_TRUE(transaction.Begin());
751
752 sql::MetaTable meta_table;
753 EXPECT_TRUE(meta_table.Init(&connection, kVersion3, kVersion3));
754
755 for (int i = 0; i < kTableCount3; ++i) {
756 std::string sql("CREATE TABLE ");
757 sql += kTables3[i].table_name;
758 sql += kTables3[i].columns;
759 EXPECT_TRUE(connection.Execute(sql.c_str()));
760 }
761
762 for (int i = 0; i < kIndexCount3; ++i) {
763 std::string sql;
764 if (kIndexes3[i].unique)
765 sql += "CREATE UNIQUE INDEX ";
766 else
767 sql += "CREATE INDEX ";
768 sql += kIndexes3[i].index_name;
769 sql += " ON ";
770 sql += kIndexes3[i].table_name;
771 sql += kIndexes3[i].columns;
772 EXPECT_TRUE(connection.Execute(sql.c_str()));
773 }
774
775 const char* kSql =
776 "INSERT INTO FallbackNameSpaces"
777 " (cache_id, origin, namespace_url, fallback_entry_url)"
778 " VALUES (?, ?, ?, ?)";
779
780 sql::Statement statement;
781 statement.Assign(connection.GetUniqueStatement(kSql));
782 EXPECT_TRUE(statement.is_valid());
783 for (int i = 0; i < kNumNamespaces; ++i) {
784 GURL namespace_url(
785 kMockOrigin.Resolve(base::StringPrintf(kNamespaceUrlFormat, i)));
786 GURL target_url(
787 kMockOrigin.Resolve(base::StringPrintf(kTargetUrlFormat, i)));
788 statement.BindInt64(0, i);
789 statement.BindString(1, kMockOrigin.spec().c_str());
790 statement.BindString(2, namespace_url.spec().c_str());
791 statement.BindString(3, target_url.spec().c_str());
792 ASSERT_TRUE(statement.Run());
793 statement.Reset();
794 }
795
796 EXPECT_TRUE(transaction.Commit());
797 }
798
799 // Open that database and verify that it got updated.
800 AppCacheDatabase db(kDbFile);
801 EXPECT_TRUE(db.LazyOpen(true));
802
803 EXPECT_FALSE(db.db_->DoesTableExist("FallbackNameSpaces"));
804 EXPECT_FALSE(db.db_->DoesIndexExist("FallbackNamesSpacesCacheIndex"));
805 EXPECT_FALSE(db.db_->DoesIndexExist("FallbackNameSpacesOriginIndex"));
806 EXPECT_FALSE(db.db_->DoesIndexExist("FallbackNameSpacesCacheAndUrlIndex"));
807
808 EXPECT_TRUE(db.db_->DoesTableExist("Namespaces"));
809 EXPECT_TRUE(db.db_->DoesIndexExist("NamespacesCacheIndex"));
810 EXPECT_TRUE(db.db_->DoesIndexExist("NamespacesOriginIndex"));
811 EXPECT_TRUE(db.db_->DoesIndexExist("NamespacesCacheAndUrlIndex"));
812
813 EXPECT_EQ(4, db.meta_table_->GetVersionNumber());
814 EXPECT_EQ(4, db.meta_table_->GetCompatibleVersionNumber());
815
816 std::vector<AppCacheDatabase::NamespaceRecord> intercepts;
817 std::vector<AppCacheDatabase::NamespaceRecord> fallbacks;
818 EXPECT_TRUE(db.FindNamespacesForOrigin(kMockOrigin, &intercepts,
819 &fallbacks));
820 EXPECT_TRUE(intercepts.empty());
821 EXPECT_EQ(kNumNamespaces, static_cast<int>(fallbacks.size()));
822
823 for (int i = 0; i < kNumNamespaces; ++i) {
824 GURL expected_namespace_url(
825 kMockOrigin.Resolve(base::StringPrintf(kNamespaceUrlFormat, i)));
826 GURL expected_target_url(
827 kMockOrigin.Resolve(base::StringPrintf(kTargetUrlFormat, i)));
828
829 EXPECT_EQ(i, fallbacks[i].cache_id);
830 EXPECT_EQ(FALLBACK_NAMESPACE, fallbacks[i].type);
831 EXPECT_EQ(kMockOrigin, fallbacks[i].origin);
832 EXPECT_EQ(expected_namespace_url, fallbacks[i].namespace_url);
833 EXPECT_EQ(expected_target_url, fallbacks[i].target_url);
834 }
835 }
836
616 } // namespace appcache 837 } // namespace appcache
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698