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 "base/file_util.h" | 5 #include "base/file_util.h" |
6 #include "base/files/file.h" | 6 #include "base/files/file.h" |
7 #include "base/files/file_path.h" | 7 #include "base/files/file_path.h" |
8 #include "base/files/scoped_temp_dir.h" | 8 #include "base/files/scoped_temp_dir.h" |
9 #include "base/memory/scoped_ptr.h" | 9 #include "base/memory/scoped_ptr.h" |
10 #include "base/message_loop/message_loop.h" | 10 #include "base/message_loop/message_loop.h" |
11 #include "base/message_loop/message_loop_proxy.h" | 11 #include "base/message_loop/message_loop_proxy.h" |
12 #include "base/strings/utf_string_conversions.h" | 12 #include "base/strings/utf_string_conversions.h" |
13 #include "base/time/time.h" | 13 #include "base/time/time.h" |
14 #include "content/public/test/mock_special_storage_policy.h" | |
14 #include "net/base/net_errors.h" | 15 #include "net/base/net_errors.h" |
15 #include "net/base/test_completion_callback.h" | 16 #include "net/base/test_completion_callback.h" |
16 #include "testing/gtest/include/gtest/gtest.h" | 17 #include "testing/gtest/include/gtest/gtest.h" |
17 #include "third_party/sqlite/sqlite3.h" | 18 #include "third_party/sqlite/sqlite3.h" |
18 #include "webkit/browser/database/database_tracker.h" | 19 #include "webkit/browser/database/database_tracker.h" |
19 #include "webkit/browser/quota/mock_special_storage_policy.h" | |
20 #include "webkit/browser/quota/quota_manager_proxy.h" | 20 #include "webkit/browser/quota/quota_manager_proxy.h" |
21 #include "webkit/common/database/database_identifier.h" | 21 #include "webkit/common/database/database_identifier.h" |
22 | 22 |
23 using base::ASCIIToUTF16; | 23 using base::ASCIIToUTF16; |
24 using webkit_database::DatabaseConnections; | |
25 using webkit_database::DatabaseTracker; | |
26 using webkit_database::OriginInfo; | |
24 | 27 |
25 namespace { | 28 namespace { |
26 | 29 |
27 const char kOrigin1Url[] = "http://origin1"; | 30 const char kOrigin1Url[] = "http://origin1"; |
28 const char kOrigin2Url[] = "http://protected_origin2"; | 31 const char kOrigin2Url[] = "http://protected_origin2"; |
29 | 32 |
30 class TestObserver : public webkit_database::DatabaseTracker::Observer { | 33 class TestObserver : public webkit_database::DatabaseTracker::Observer { |
31 public: | 34 public: |
32 TestObserver() | 35 TestObserver() |
33 : new_notification_received_(false), | 36 : new_notification_received_(false), |
(...skipping 141 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
175 bool EnsureFileOfSize(const base::FilePath& file_path, int64 length) { | 178 bool EnsureFileOfSize(const base::FilePath& file_path, int64 length) { |
176 base::File file(file_path, | 179 base::File file(file_path, |
177 base::File::FLAG_OPEN_ALWAYS | base::File::FLAG_WRITE); | 180 base::File::FLAG_OPEN_ALWAYS | base::File::FLAG_WRITE); |
178 if (!file.IsValid()) | 181 if (!file.IsValid()) |
179 return false; | 182 return false; |
180 return file.SetLength(length); | 183 return file.SetLength(length); |
181 } | 184 } |
182 | 185 |
183 } // namespace | 186 } // namespace |
184 | 187 |
185 namespace webkit_database { | 188 namespace content { |
186 | 189 |
187 // We declare a helper class, and make it a friend of DatabaseTracker using | 190 // We declare a helper class, and make it a friend of DatabaseTracker using |
188 // the FRIEND_TEST() macro, and we implement all tests we want to run as | 191 // the F R I E N D_TEST() macro, and we implement all tests we want to run as |
jam
2014/04/29 00:54:05
?
| |
189 // static methods of this class. Then we make our TEST() targets call these | 192 // static methods of this class. Then we make our TEST() targets call these |
190 // static functions. This allows us to run each test in normal mode and | 193 // static functions. This allows us to run each test in normal mode and |
191 // incognito mode without writing the same code twice. | 194 // incognito mode without writing the same code twice. |
192 class DatabaseTracker_TestHelper_Test { | 195 class DatabaseTracker_TestHelper_Test { |
193 public: | 196 public: |
194 static void TestDeleteOpenDatabase(bool incognito_mode) { | 197 static void TestDeleteOpenDatabase(bool incognito_mode) { |
195 // Initialize the tracker database. | 198 // Initialize the tracker database. |
196 base::ScopedTempDir temp_dir; | 199 base::ScopedTempDir temp_dir; |
197 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); | 200 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
198 scoped_refptr<quota::MockSpecialStoragePolicy> special_storage_policy = | 201 scoped_refptr<content::MockSpecialStoragePolicy> special_storage_policy = |
jam
2014/04/29 00:54:05
nit: here and below, get rid of "content::"
| |
199 new quota::MockSpecialStoragePolicy; | 202 new content::MockSpecialStoragePolicy; |
200 special_storage_policy->AddProtected(GURL(kOrigin2Url)); | 203 special_storage_policy->AddProtected(GURL(kOrigin2Url)); |
201 scoped_refptr<DatabaseTracker> tracker( | 204 scoped_refptr<DatabaseTracker> tracker( |
202 new DatabaseTracker(temp_dir.path(), | 205 new DatabaseTracker(temp_dir.path(), |
203 incognito_mode, | 206 incognito_mode, |
204 special_storage_policy.get(), | 207 special_storage_policy.get(), |
205 NULL, | 208 NULL, |
206 NULL)); | 209 NULL)); |
207 | 210 |
208 // Create and open three databases. | 211 // Create and open three databases. |
209 int64 database_size = 0; | 212 int64 database_size = 0; |
(...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
296 base::PathExists(tracker->GetFullDBFilePath(kOrigin2, kDB3))); | 299 base::PathExists(tracker->GetFullDBFilePath(kOrigin2, kDB3))); |
297 | 300 |
298 tracker->DatabaseClosed(kOrigin2, kDB3); | 301 tracker->DatabaseClosed(kOrigin2, kDB3); |
299 tracker->RemoveObserver(&observer); | 302 tracker->RemoveObserver(&observer); |
300 } | 303 } |
301 | 304 |
302 static void TestDatabaseTracker(bool incognito_mode) { | 305 static void TestDatabaseTracker(bool incognito_mode) { |
303 // Initialize the tracker database. | 306 // Initialize the tracker database. |
304 base::ScopedTempDir temp_dir; | 307 base::ScopedTempDir temp_dir; |
305 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); | 308 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
306 scoped_refptr<quota::MockSpecialStoragePolicy> special_storage_policy = | 309 scoped_refptr<content::MockSpecialStoragePolicy> special_storage_policy = |
307 new quota::MockSpecialStoragePolicy; | 310 new content::MockSpecialStoragePolicy; |
308 special_storage_policy->AddProtected(GURL(kOrigin2Url)); | 311 special_storage_policy->AddProtected(GURL(kOrigin2Url)); |
309 scoped_refptr<DatabaseTracker> tracker( | 312 scoped_refptr<DatabaseTracker> tracker( |
310 new DatabaseTracker(temp_dir.path(), | 313 new DatabaseTracker(temp_dir.path(), |
311 incognito_mode, | 314 incognito_mode, |
312 special_storage_policy.get(), | 315 special_storage_policy.get(), |
313 NULL, | 316 NULL, |
314 NULL)); | 317 NULL)); |
315 | 318 |
316 // Add two observers. | 319 // Add two observers. |
317 TestObserver observer1; | 320 TestObserver observer1; |
(...skipping 237 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
555 const base::string16 kDB2 = ASCIIToUTF16("db2"); | 558 const base::string16 kDB2 = ASCIIToUTF16("db2"); |
556 const base::string16 kDescription = ASCIIToUTF16("database_description"); | 559 const base::string16 kDescription = ASCIIToUTF16("database_description"); |
557 | 560 |
558 // Initialize the tracker database. | 561 // Initialize the tracker database. |
559 base::MessageLoop message_loop; | 562 base::MessageLoop message_loop; |
560 base::ScopedTempDir temp_dir; | 563 base::ScopedTempDir temp_dir; |
561 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); | 564 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
562 base::FilePath origin1_db_dir; | 565 base::FilePath origin1_db_dir; |
563 base::FilePath origin2_db_dir; | 566 base::FilePath origin2_db_dir; |
564 { | 567 { |
565 scoped_refptr<quota::MockSpecialStoragePolicy> special_storage_policy = | 568 scoped_refptr<content::MockSpecialStoragePolicy> special_storage_policy = |
566 new quota::MockSpecialStoragePolicy; | 569 new content::MockSpecialStoragePolicy; |
567 special_storage_policy->AddSessionOnly(GURL(kOrigin2Url)); | 570 special_storage_policy->AddSessionOnly(GURL(kOrigin2Url)); |
568 scoped_refptr<DatabaseTracker> tracker( | 571 scoped_refptr<DatabaseTracker> tracker( |
569 new DatabaseTracker(temp_dir.path(), | 572 new DatabaseTracker(temp_dir.path(), |
570 false, | 573 false, |
571 special_storage_policy.get(), | 574 special_storage_policy.get(), |
572 NULL, | 575 NULL, |
573 base::MessageLoopProxy::current().get())); | 576 base::MessageLoopProxy::current().get())); |
574 | 577 |
575 // Open two new databases. | 578 // Open two new databases. |
576 tracker->DatabaseOpened(kOrigin1, kDB1, kDescription, 0, | 579 tracker->DatabaseOpened(kOrigin1, kDB1, kDescription, 0, |
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
635 const base::string16 kDB2 = ASCIIToUTF16("db2"); | 638 const base::string16 kDB2 = ASCIIToUTF16("db2"); |
636 const base::string16 kDescription = ASCIIToUTF16("database_description"); | 639 const base::string16 kDescription = ASCIIToUTF16("database_description"); |
637 | 640 |
638 // Initialize the tracker database. | 641 // Initialize the tracker database. |
639 base::MessageLoop message_loop; | 642 base::MessageLoop message_loop; |
640 base::ScopedTempDir temp_dir; | 643 base::ScopedTempDir temp_dir; |
641 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); | 644 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
642 base::FilePath origin1_db_dir; | 645 base::FilePath origin1_db_dir; |
643 base::FilePath origin2_db_dir; | 646 base::FilePath origin2_db_dir; |
644 { | 647 { |
645 scoped_refptr<quota::MockSpecialStoragePolicy> special_storage_policy = | 648 scoped_refptr<content::MockSpecialStoragePolicy> special_storage_policy = |
646 new quota::MockSpecialStoragePolicy; | 649 new content::MockSpecialStoragePolicy; |
647 special_storage_policy->AddSessionOnly(GURL(kOrigin2Url)); | 650 special_storage_policy->AddSessionOnly(GURL(kOrigin2Url)); |
648 scoped_refptr<DatabaseTracker> tracker( | 651 scoped_refptr<DatabaseTracker> tracker( |
649 new DatabaseTracker(temp_dir.path(), | 652 new DatabaseTracker(temp_dir.path(), |
650 false, | 653 false, |
651 special_storage_policy.get(), | 654 special_storage_policy.get(), |
652 NULL, | 655 NULL, |
653 base::MessageLoopProxy::current().get())); | 656 base::MessageLoopProxy::current().get())); |
654 tracker->SetForceKeepSessionState(); | 657 tracker->SetForceKeepSessionState(); |
655 | 658 |
656 // Open two new databases. | 659 // Open two new databases. |
(...skipping 146 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
803 // tracker db is removed. | 806 // tracker db is removed. |
804 tracker->DatabaseClosed(kOriginId, kName); | 807 tracker->DatabaseClosed(kOriginId, kName); |
805 EXPECT_FALSE(base::PathExists(spoof_db_file)); | 808 EXPECT_FALSE(base::PathExists(spoof_db_file)); |
806 EXPECT_TRUE(tracker->GetFullDBFilePath(kOriginId, kName).empty()); | 809 EXPECT_TRUE(tracker->GetFullDBFilePath(kOriginId, kName).empty()); |
807 | 810 |
808 // -------------------------------------------------------- | 811 // -------------------------------------------------------- |
809 // Create another record of a database in the tracker db and create | 812 // Create another record of a database in the tracker db and create |
810 // a spoof_db_file on disk in the expected location. | 813 // a spoof_db_file on disk in the expected location. |
811 tracker->DatabaseOpened(kOriginId, kName, kDescription, 0, | 814 tracker->DatabaseOpened(kOriginId, kName, kDescription, 0, |
812 &database_size); | 815 &database_size); |
813 base::FilePath spoof_db_file2 = tracker->GetFullDBFilePath(kOriginId, kName) ; | 816 base::FilePath spoof_db_file2 = tracker->GetFullDBFilePath(kOriginId, |
817 kName); | |
jam
2014/04/29 00:54:05
nit: per style guide, the first parameter on each
| |
814 EXPECT_FALSE(tracker->GetFullDBFilePath(kOriginId, kName).empty()); | 818 EXPECT_FALSE(tracker->GetFullDBFilePath(kOriginId, kName).empty()); |
815 EXPECT_NE(spoof_db_file, spoof_db_file2); | 819 EXPECT_NE(spoof_db_file, spoof_db_file2); |
816 EXPECT_TRUE(base::CreateDirectory(spoof_db_file2.DirName())); | 820 EXPECT_TRUE(base::CreateDirectory(spoof_db_file2.DirName())); |
817 EXPECT_TRUE(EnsureFileOfSize(spoof_db_file2, 1)); | 821 EXPECT_TRUE(EnsureFileOfSize(spoof_db_file2, 1)); |
818 | 822 |
819 // Verify that with no connection open, the db is deleted immediately. | 823 // Verify that with no connection open, the db is deleted immediately. |
820 tracker->DatabaseClosed(kOriginId, kName); | 824 tracker->DatabaseClosed(kOriginId, kName); |
821 tracker->HandleSqliteError(kOriginId, kName, SQLITE_CORRUPT); | 825 tracker->HandleSqliteError(kOriginId, kName, SQLITE_CORRUPT); |
822 EXPECT_FALSE(tracker->IsDatabaseScheduledForDeletion(kOriginId, kName)); | 826 EXPECT_FALSE(tracker->IsDatabaseScheduledForDeletion(kOriginId, kName)); |
823 EXPECT_FALSE(observer.DidReceiveNewNotification()); | 827 EXPECT_FALSE(observer.DidReceiveNewNotification()); |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
861 } | 865 } |
862 | 866 |
863 TEST(DatabaseTrackerTest, EmptyDatabaseNameIsValid) { | 867 TEST(DatabaseTrackerTest, EmptyDatabaseNameIsValid) { |
864 DatabaseTracker_TestHelper_Test::EmptyDatabaseNameIsValid(); | 868 DatabaseTracker_TestHelper_Test::EmptyDatabaseNameIsValid(); |
865 } | 869 } |
866 | 870 |
867 TEST(DatabaseTrackerTest, HandleSqliteError) { | 871 TEST(DatabaseTrackerTest, HandleSqliteError) { |
868 DatabaseTracker_TestHelper_Test::HandleSqliteError(); | 872 DatabaseTracker_TestHelper_Test::HandleSqliteError(); |
869 } | 873 } |
870 | 874 |
871 } // namespace webkit_database | 875 } // namespace content |
OLD | NEW |