1 // Copyright 2013 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. 4 5 #include "base/basictypes.h" 6 #include "base/file_util.h" 7 #include "base/files/scoped_temp_dir.h" 8 #include "testing/gtest/include/gtest/gtest.h" 9 #include "webkit/browser/fileapi/sandbox_isolated_origin_database.h" 10 #include "webkit/browser/fileapi/sandbox_origin_database.h" 11 12 namespace fileapi { 13 14 TEST(SandboxIsolatedOriginDatabaseTest, BasicTest) { 15 base::ScopedTempDir dir; 16 ASSERT_TRUE(dir.CreateUniqueTempDir()); 17 18 std::string kOrigin("origin"); 19 SandboxIsolatedOriginDatabase database(kOrigin, dir.path()); 20 21 EXPECT_TRUE(database.HasOriginPath(kOrigin)); 22 23 base::FilePath path1, path2; 24 25 EXPECT_FALSE(database.GetPathForOrigin(std::string(), &path1)); 26 EXPECT_FALSE(database.GetPathForOrigin("foo", &path1)); 27 28 EXPECT_TRUE(database.HasOriginPath(kOrigin)); 29 EXPECT_TRUE(database.GetPathForOrigin(kOrigin, &path1)); 30 EXPECT_TRUE(database.GetPathForOrigin(kOrigin, &path2)); 31 EXPECT_FALSE(path1.empty()); 32 EXPECT_FALSE(path2.empty()); 33 EXPECT_EQ(path1, path2); 34 } 35 36 TEST(SandboxIsolatedOriginDatabaseTest, MigrationTest) { 37 base::ScopedTempDir dir; 38 ASSERT_TRUE(dir.CreateUniqueTempDir()); 39 40 std::string kOrigin("origin"); 41 std::string kFakeDirectoryData("0123456789"); 42 base::FilePath path; 43 base::FilePath old_db_path; 44 45 // Initialize the directory with one origin using the regular 46 // SandboxOriginDatabase. 47 { 48 SandboxOriginDatabase database_old(dir.path()); 49 old_db_path = database_old.GetDatabasePath(); 50 EXPECT_FALSE(base::PathExists(old_db_path)); 51 EXPECT_TRUE(database_old.GetPathForOrigin(kOrigin, &path)); 52 EXPECT_FALSE(path.empty()); 53 EXPECT_TRUE(base::DirectoryExists(old_db_path)); 54 55 // Populate the origin directory with some fake data. 56 base::FilePath directory_db_path = dir.path().Append(path); 57 ASSERT_TRUE(file_util::CreateDirectory(directory_db_path)); 58 EXPECT_EQ(static_cast<int>(kFakeDirectoryData.size()), 59 file_util::WriteFile(directory_db_path.AppendASCII("dummy"), 60 kFakeDirectoryData.data(), 61 kFakeDirectoryData.size())); 62 } 63 64 // Re-open the directory using sandboxIsolatedOriginDatabase. 65 SandboxIsolatedOriginDatabase database(kOrigin, dir.path()); 66 67 // The database is migrated from the old one, so we should still 68 // see the same origin. 69 EXPECT_TRUE(database.HasOriginPath(kOrigin)); 70 EXPECT_TRUE(database.GetPathForOrigin(kOrigin, &path)); 71 EXPECT_FALSE(path.empty()); 72 73 // The directory content must be kept (or migrated if necessary), 74 // so we should see the same fake data. 75 std::string origin_db_data; 76 base::FilePath directory_db_path = dir.path().Append(path); 77 EXPECT_TRUE(base::DirectoryExists(directory_db_path)); 78 EXPECT_TRUE(base::PathExists(directory_db_path.AppendASCII("dummy"))); 79 EXPECT_TRUE(file_util::ReadFileToString( 80 directory_db_path.AppendASCII("dummy"), &origin_db_data)); 81 EXPECT_EQ(kFakeDirectoryData, origin_db_data); 82 83 // After the migration the database must be gone. 84 EXPECT_FALSE(base::PathExists(old_db_path)); 85 } 86 87 } // namespace fileapi 88