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.
5 #include "base/basictypes.h"
6 #include "base/file_util.h"
7 #include "base/files/scoped_temp_dir.h"
8 #include "base/platform_file.h"
9 #include "testing/gtest/include/gtest/gtest.h"
10 #include "webkit/browser/fileapi/sandbox_origin_database.h"
11 #include "webkit/browser/fileapi/sandbox_prioritized_origin_database.h"
15 TEST(SandboxPrioritizedOriginDatabaseTest, BasicTest) {
16 base::ScopedTempDir dir;
18 ASSERT_TRUE(dir.CreateUniqueTempDir());
20 const std::string kOrigin1("origin1");
21 const std::string kOrigin2("origin2");
23 SandboxPrioritizedOriginDatabase database(dir.path());
25 // Set the kOrigin1 as a parimary origin.
26 EXPECT_TRUE(database.InitializePrimaryOrigin(kOrigin1));
29 EXPECT_TRUE(database.GetPathForOrigin(kOrigin1, &path));
30 EXPECT_TRUE(database.GetPathForOrigin(kOrigin2, &path));
33 EXPECT_TRUE(database.HasOriginPath(kOrigin1));
34 EXPECT_TRUE(database.GetPathForOrigin(kOrigin1, &path));
35 EXPECT_FALSE(path.empty());
36 EXPECT_TRUE(database.HasOriginPath(kOrigin2));
37 EXPECT_TRUE(database.GetPathForOrigin(kOrigin2, &path));
38 EXPECT_FALSE(path.empty());
40 std::vector<SandboxOriginDatabaseInterface::OriginRecord> origins;
41 database.ListAllOrigins(&origins);
42 ASSERT_EQ(2U, origins.size());
43 EXPECT_TRUE(origins[0].origin == kOrigin1 ||
44 origins[1].origin == kOrigin1);
45 EXPECT_TRUE(origins[0].origin == kOrigin2 ||
46 origins[1].origin == kOrigin2);
47 EXPECT_NE(origins[0].path, origins[1].path);
49 // Try remove path for kOrigin1.
50 database.RemovePathForOrigin(kOrigin1);
52 // Verify the removal.
53 EXPECT_FALSE(database.HasOriginPath(kOrigin1));
54 EXPECT_TRUE(database.HasOriginPath(kOrigin2));
55 database.ListAllOrigins(&origins);
56 ASSERT_EQ(1U, origins.size());
57 EXPECT_EQ(kOrigin2, origins[0].origin);
59 // Try remove path for kOrigin2.
60 database.RemovePathForOrigin(kOrigin2);
62 // Verify the removal.
63 EXPECT_FALSE(database.HasOriginPath(kOrigin1));
64 EXPECT_FALSE(database.HasOriginPath(kOrigin2));
65 database.ListAllOrigins(&origins);
66 EXPECT_TRUE(origins.empty());
69 TEST(SandboxPrioritizedOriginDatabaseTest, SetPrimaryLaterTest) {
70 base::ScopedTempDir dir;
72 ASSERT_TRUE(dir.CreateUniqueTempDir());
74 const std::string kOrigin1("origin1");
75 const std::string kOrigin2("origin2");
77 SandboxPrioritizedOriginDatabase database(dir.path());
79 EXPECT_TRUE(database.GetPrimaryOrigin().empty());
81 EXPECT_TRUE(database.GetPathForOrigin(kOrigin1, &path));
82 EXPECT_TRUE(database.GetPathForOrigin(kOrigin2, &path));
84 // Set the kOrigin1 as a parimary origin.
85 EXPECT_TRUE(database.InitializePrimaryOrigin(kOrigin1));
86 EXPECT_EQ(kOrigin1, database.GetPrimaryOrigin());
88 // Regardless of whether it is initialized as primary or not
89 // they should just work.
90 EXPECT_TRUE(database.HasOriginPath(kOrigin1));
91 EXPECT_TRUE(database.GetPathForOrigin(kOrigin1, &path));
92 EXPECT_FALSE(path.empty());
93 EXPECT_TRUE(database.HasOriginPath(kOrigin2));
94 EXPECT_TRUE(database.GetPathForOrigin(kOrigin2, &path));
95 EXPECT_FALSE(path.empty());
98 TEST(SandboxPrioritizedOriginDatabaseTest, LostPrimaryOriginFileTest) {
99 base::ScopedTempDir dir;
101 ASSERT_TRUE(dir.CreateUniqueTempDir());
103 const std::string kOrigin1("origin1");
104 const std::string kData("foo");
106 SandboxPrioritizedOriginDatabase database(dir.path());
108 EXPECT_TRUE(database.GetPrimaryOrigin().empty());
110 // Set the kOrigin1 as a parimary origin.
111 EXPECT_TRUE(database.InitializePrimaryOrigin(kOrigin1));
112 EXPECT_EQ(kOrigin1, database.GetPrimaryOrigin());
114 // Make sure it works.
115 EXPECT_TRUE(database.HasOriginPath(kOrigin1));
116 EXPECT_TRUE(database.GetPathForOrigin(kOrigin1, &path));
118 // Reset the database.
119 database.DropDatabase();
121 // kOrigin1 should still be marked as primary.
122 EXPECT_TRUE(database.HasOriginPath(kOrigin1));
123 EXPECT_TRUE(database.GetPathForOrigin(kOrigin1, &path));
125 // Corrupt the primary origin file.
126 file_util::WriteFile(
127 database.primary_origin_file(), kData.data(), kData.size());
129 // Reset the database.
130 database.DropDatabase();
132 // kOrigin1 is no longer marked as primary, and unfortunately we fail
133 // to find the data for the origin.
134 EXPECT_FALSE(database.HasOriginPath(kOrigin1));
137 TEST(SandboxPrioritizedOriginDatabaseTest, MigrationTest) {
138 base::ScopedTempDir dir;
139 ASSERT_TRUE(dir.CreateUniqueTempDir());
141 const std::string kOrigin1("origin1");
142 const std::string kOrigin2("origin2");
143 const std::string kFakeDirectoryData1("0123456789");
144 const std::string kFakeDirectoryData2("abcde");
145 base::FilePath old_dir_db_path1, old_dir_db_path2;
146 base::FilePath path1, path2;
148 // Initialize the directory with two origins using the regular
149 // SandboxOriginDatabase.
151 SandboxOriginDatabase database_old(dir.path());
152 base::FilePath old_db_path = database_old.GetDatabasePath();
153 EXPECT_FALSE(base::PathExists(old_db_path));
155 // Initialize paths for kOrigin1 and kOrigin2.
156 EXPECT_TRUE(database_old.GetPathForOrigin(kOrigin1, &path1));
157 EXPECT_FALSE(path1.empty());
158 EXPECT_TRUE(database_old.GetPathForOrigin(kOrigin2, &path2));
159 EXPECT_FALSE(path2.empty());
161 EXPECT_TRUE(base::DirectoryExists(old_db_path));
163 // Populate the origin directory with some fake data.
164 old_dir_db_path1 = dir.path().Append(path1);
165 ASSERT_TRUE(base::CreateDirectory(old_dir_db_path1));
166 EXPECT_EQ(static_cast<int>(kFakeDirectoryData1.size()),
167 file_util::WriteFile(old_dir_db_path1.AppendASCII("dummy"),
168 kFakeDirectoryData1.data(),
169 kFakeDirectoryData1.size()));
170 old_dir_db_path2 = dir.path().Append(path2);
171 ASSERT_TRUE(base::CreateDirectory(old_dir_db_path2));
172 EXPECT_EQ(static_cast<int>(kFakeDirectoryData2.size()),
173 file_util::WriteFile(old_dir_db_path2.AppendASCII("dummy"),
174 kFakeDirectoryData2.data(),
175 kFakeDirectoryData2.size()));
178 // Re-open the directory using sandboxPrioritizedOriginDatabase.
179 SandboxPrioritizedOriginDatabase database(dir.path());
181 // Set the kOrigin1 as a parimary origin.
182 // (Trying to initialize another origin should fail).
183 EXPECT_TRUE(database.InitializePrimaryOrigin(kOrigin1));
184 EXPECT_FALSE(database.InitializePrimaryOrigin(kOrigin2));
186 EXPECT_EQ(kOrigin1, database.GetPrimaryOrigin());
188 // Regardless of whether the origin is registered as primary or not
189 // it should just work.
190 EXPECT_TRUE(database.HasOriginPath(kOrigin1));
191 EXPECT_TRUE(database.GetPathForOrigin(kOrigin1, &path1));
192 EXPECT_TRUE(database.HasOriginPath(kOrigin2));
193 EXPECT_TRUE(database.GetPathForOrigin(kOrigin2, &path2));
195 // The directory content must be kept (or migrated if necessary) as well.
196 std::string origin_db_data;
197 base::FilePath dir_db_path = dir.path().Append(path1);
198 EXPECT_TRUE(base::PathExists(dir_db_path.AppendASCII("dummy")));
199 EXPECT_TRUE(base::ReadFileToString(
200 dir_db_path.AppendASCII("dummy"), &origin_db_data));
201 EXPECT_EQ(kFakeDirectoryData1, origin_db_data);
203 origin_db_data.clear();
204 dir_db_path = dir.path().Append(path2);
205 EXPECT_TRUE(base::PathExists(dir_db_path.AppendASCII("dummy")));
206 EXPECT_TRUE(base::ReadFileToString(
207 dir_db_path.AppendASCII("dummy"), &origin_db_data));
208 EXPECT_EQ(kFakeDirectoryData2, origin_db_data);
210 // After the migration the kOrigin1 directory database path must be gone.
211 EXPECT_FALSE(base::PathExists(old_dir_db_path1));
212 EXPECT_TRUE(base::PathExists(old_dir_db_path2));
215 } // namespace fileapi