Supervised user import: Listen for profile creation/deletion
[chromium-blink-merge.git] / content / browser / fileapi / sandbox_file_system_backend_unittest.cc
blobefa7259fd4441a8cd6f48e7f58483696a6c9bf70
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 "storage/browser/fileapi/sandbox_file_system_backend.h"
7 #include <set>
9 #include "base/basictypes.h"
10 #include "base/files/file_util.h"
11 #include "base/files/scoped_temp_dir.h"
12 #include "base/memory/scoped_ptr.h"
13 #include "base/message_loop/message_loop_proxy.h"
14 #include "base/run_loop.h"
15 #include "content/public/test/test_file_system_options.h"
16 #include "storage/browser/fileapi/file_system_backend.h"
17 #include "storage/browser/fileapi/file_system_url.h"
18 #include "storage/browser/fileapi/sandbox_file_system_backend_delegate.h"
19 #include "storage/common/fileapi/file_system_util.h"
20 #include "testing/gtest/include/gtest/gtest.h"
21 #include "url/gurl.h"
23 using storage::FileSystemURL;
24 using storage::SandboxFileSystemBackend;
25 using storage::SandboxFileSystemBackendDelegate;
27 // PS stands for path separator.
28 #if defined(FILE_PATH_USES_WIN_SEPARATORS)
29 #define PS "\\"
30 #else
31 #define PS "/"
32 #endif
34 namespace content {
36 namespace {
38 const struct RootPathTest {
39 storage::FileSystemType type;
40 const char* origin_url;
41 const char* expected_path;
42 } kRootPathTestCases[] = {
43 {storage::kFileSystemTypeTemporary, "http://foo:1/", "000" PS "t"},
44 {storage::kFileSystemTypePersistent, "http://foo:1/", "000" PS "p"},
45 {storage::kFileSystemTypeTemporary, "http://bar.com/", "001" PS "t"},
46 {storage::kFileSystemTypePersistent, "http://bar.com/", "001" PS "p"},
47 {storage::kFileSystemTypeTemporary, "https://foo:2/", "002" PS "t"},
48 {storage::kFileSystemTypePersistent, "https://foo:2/", "002" PS "p"},
49 {storage::kFileSystemTypeTemporary, "https://bar.com/", "003" PS "t"},
50 {storage::kFileSystemTypePersistent, "https://bar.com/", "003" PS "p"},
53 const struct RootPathFileURITest {
54 storage::FileSystemType type;
55 const char* origin_url;
56 const char* expected_path;
57 const char* virtual_path;
58 } kRootPathFileURITestCases[] = {
59 {storage::kFileSystemTypeTemporary, "file:///", "000" PS "t", NULL},
60 {storage::kFileSystemTypePersistent, "file:///", "000" PS "p", NULL},
63 void DidOpenFileSystem(base::File::Error* error_out,
64 const GURL& origin_url,
65 const std::string& name,
66 base::File::Error error) {
67 *error_out = error;
70 } // namespace
72 class SandboxFileSystemBackendTest : public testing::Test {
73 protected:
74 void SetUp() override {
75 ASSERT_TRUE(data_dir_.CreateUniqueTempDir());
76 SetUpNewDelegate(CreateAllowFileAccessOptions());
79 void SetUpNewDelegate(const storage::FileSystemOptions& options) {
80 delegate_.reset(new SandboxFileSystemBackendDelegate(
81 NULL /* quota_manager_proxy */,
82 base::MessageLoopProxy::current().get(),
83 data_dir_.path(),
84 NULL /* special_storage_policy */,
85 options));
88 void SetUpNewBackend(const storage::FileSystemOptions& options) {
89 SetUpNewDelegate(options);
90 backend_.reset(new SandboxFileSystemBackend(delegate_.get()));
93 storage::SandboxFileSystemBackendDelegate::OriginEnumerator*
94 CreateOriginEnumerator() const {
95 return backend_->CreateOriginEnumerator();
98 void CreateOriginTypeDirectory(const GURL& origin,
99 storage::FileSystemType type) {
100 base::FilePath target = delegate_->
101 GetBaseDirectoryForOriginAndType(origin, type, true);
102 ASSERT_TRUE(!target.empty());
103 ASSERT_TRUE(base::DirectoryExists(target));
106 bool GetRootPath(const GURL& origin_url,
107 storage::FileSystemType type,
108 storage::OpenFileSystemMode mode,
109 base::FilePath* root_path) {
110 base::File::Error error = base::File::FILE_OK;
111 backend_->ResolveURL(
112 FileSystemURL::CreateForTest(origin_url, type, base::FilePath()),
113 mode,
114 base::Bind(&DidOpenFileSystem, &error));
115 base::RunLoop().RunUntilIdle();
116 if (error != base::File::FILE_OK)
117 return false;
118 base::FilePath returned_root_path =
119 delegate_->GetBaseDirectoryForOriginAndType(
120 origin_url, type, false /* create */);
121 if (root_path)
122 *root_path = returned_root_path;
123 return !returned_root_path.empty();
126 base::FilePath file_system_path() const {
127 return data_dir_.path().Append(
128 SandboxFileSystemBackendDelegate::kFileSystemDirectory);
131 base::ScopedTempDir data_dir_;
132 base::MessageLoop message_loop_;
133 scoped_ptr<storage::SandboxFileSystemBackendDelegate> delegate_;
134 scoped_ptr<storage::SandboxFileSystemBackend> backend_;
137 TEST_F(SandboxFileSystemBackendTest, Empty) {
138 SetUpNewBackend(CreateAllowFileAccessOptions());
139 scoped_ptr<SandboxFileSystemBackendDelegate::OriginEnumerator> enumerator(
140 CreateOriginEnumerator());
141 ASSERT_TRUE(enumerator->Next().is_empty());
144 TEST_F(SandboxFileSystemBackendTest, EnumerateOrigins) {
145 SetUpNewBackend(CreateAllowFileAccessOptions());
146 const char* temporary_origins[] = {
147 "http://www.bar.com/",
148 "http://www.foo.com/",
149 "http://www.foo.com:1/",
150 "http://www.example.com:8080/",
151 "http://www.google.com:80/",
153 const char* persistent_origins[] = {
154 "http://www.bar.com/",
155 "http://www.foo.com:8080/",
156 "http://www.foo.com:80/",
158 size_t temporary_size = arraysize(temporary_origins);
159 size_t persistent_size = arraysize(persistent_origins);
160 std::set<GURL> temporary_set, persistent_set;
161 for (size_t i = 0; i < temporary_size; ++i) {
162 CreateOriginTypeDirectory(GURL(temporary_origins[i]),
163 storage::kFileSystemTypeTemporary);
164 temporary_set.insert(GURL(temporary_origins[i]));
166 for (size_t i = 0; i < persistent_size; ++i) {
167 CreateOriginTypeDirectory(GURL(persistent_origins[i]),
168 storage::kFileSystemTypePersistent);
169 persistent_set.insert(GURL(persistent_origins[i]));
172 scoped_ptr<SandboxFileSystemBackendDelegate::OriginEnumerator> enumerator(
173 CreateOriginEnumerator());
174 size_t temporary_actual_size = 0;
175 size_t persistent_actual_size = 0;
176 GURL current;
177 while (!(current = enumerator->Next()).is_empty()) {
178 SCOPED_TRACE(testing::Message() << "EnumerateOrigin " << current.spec());
179 if (enumerator->HasFileSystemType(storage::kFileSystemTypeTemporary)) {
180 ASSERT_TRUE(temporary_set.find(current) != temporary_set.end());
181 ++temporary_actual_size;
183 if (enumerator->HasFileSystemType(storage::kFileSystemTypePersistent)) {
184 ASSERT_TRUE(persistent_set.find(current) != persistent_set.end());
185 ++persistent_actual_size;
189 EXPECT_EQ(temporary_size, temporary_actual_size);
190 EXPECT_EQ(persistent_size, persistent_actual_size);
193 TEST_F(SandboxFileSystemBackendTest, GetRootPathCreateAndExamine) {
194 std::vector<base::FilePath> returned_root_path(arraysize(kRootPathTestCases));
195 SetUpNewBackend(CreateAllowFileAccessOptions());
197 // Create a new root directory.
198 for (size_t i = 0; i < arraysize(kRootPathTestCases); ++i) {
199 SCOPED_TRACE(testing::Message() << "RootPath (create) #" << i << " "
200 << kRootPathTestCases[i].expected_path);
202 base::FilePath root_path;
203 EXPECT_TRUE(GetRootPath(GURL(kRootPathTestCases[i].origin_url),
204 kRootPathTestCases[i].type,
205 storage::OPEN_FILE_SYSTEM_CREATE_IF_NONEXISTENT,
206 &root_path));
208 base::FilePath expected = file_system_path().AppendASCII(
209 kRootPathTestCases[i].expected_path);
210 EXPECT_EQ(expected.value(), root_path.value());
211 EXPECT_TRUE(base::DirectoryExists(root_path));
212 ASSERT_TRUE(returned_root_path.size() > i);
213 returned_root_path[i] = root_path;
216 // Get the root directory with create=false and see if we get the
217 // same directory.
218 for (size_t i = 0; i < arraysize(kRootPathTestCases); ++i) {
219 SCOPED_TRACE(testing::Message() << "RootPath (get) #" << i << " "
220 << kRootPathTestCases[i].expected_path);
222 base::FilePath root_path;
223 EXPECT_TRUE(GetRootPath(GURL(kRootPathTestCases[i].origin_url),
224 kRootPathTestCases[i].type,
225 storage::OPEN_FILE_SYSTEM_FAIL_IF_NONEXISTENT,
226 &root_path));
227 ASSERT_TRUE(returned_root_path.size() > i);
228 EXPECT_EQ(returned_root_path[i].value(), root_path.value());
232 TEST_F(SandboxFileSystemBackendTest,
233 GetRootPathCreateAndExamineWithNewBackend) {
234 std::vector<base::FilePath> returned_root_path(arraysize(kRootPathTestCases));
235 SetUpNewBackend(CreateAllowFileAccessOptions());
237 GURL origin_url("http://foo.com:1/");
239 base::FilePath root_path1;
240 EXPECT_TRUE(GetRootPath(origin_url,
241 storage::kFileSystemTypeTemporary,
242 storage::OPEN_FILE_SYSTEM_CREATE_IF_NONEXISTENT,
243 &root_path1));
245 SetUpNewBackend(CreateDisallowFileAccessOptions());
246 base::FilePath root_path2;
247 EXPECT_TRUE(GetRootPath(origin_url,
248 storage::kFileSystemTypeTemporary,
249 storage::OPEN_FILE_SYSTEM_FAIL_IF_NONEXISTENT,
250 &root_path2));
252 EXPECT_EQ(root_path1.value(), root_path2.value());
255 TEST_F(SandboxFileSystemBackendTest, GetRootPathGetWithoutCreate) {
256 SetUpNewBackend(CreateDisallowFileAccessOptions());
258 // Try to get a root directory without creating.
259 for (size_t i = 0; i < arraysize(kRootPathTestCases); ++i) {
260 SCOPED_TRACE(testing::Message() << "RootPath (create=false) #" << i << " "
261 << kRootPathTestCases[i].expected_path);
262 EXPECT_FALSE(GetRootPath(GURL(kRootPathTestCases[i].origin_url),
263 kRootPathTestCases[i].type,
264 storage::OPEN_FILE_SYSTEM_FAIL_IF_NONEXISTENT,
265 NULL));
269 TEST_F(SandboxFileSystemBackendTest, GetRootPathInIncognito) {
270 SetUpNewBackend(CreateIncognitoFileSystemOptions());
272 // Try to get a root directory.
273 for (size_t i = 0; i < arraysize(kRootPathTestCases); ++i) {
274 SCOPED_TRACE(testing::Message() << "RootPath (incognito) #" << i << " "
275 << kRootPathTestCases[i].expected_path);
276 EXPECT_FALSE(GetRootPath(GURL(kRootPathTestCases[i].origin_url),
277 kRootPathTestCases[i].type,
278 storage::OPEN_FILE_SYSTEM_CREATE_IF_NONEXISTENT,
279 NULL));
283 TEST_F(SandboxFileSystemBackendTest, GetRootPathFileURI) {
284 SetUpNewBackend(CreateDisallowFileAccessOptions());
285 for (size_t i = 0; i < arraysize(kRootPathFileURITestCases); ++i) {
286 SCOPED_TRACE(testing::Message() << "RootPathFileURI (disallow) #"
287 << i << " " << kRootPathFileURITestCases[i].expected_path);
288 EXPECT_FALSE(GetRootPath(GURL(kRootPathFileURITestCases[i].origin_url),
289 kRootPathFileURITestCases[i].type,
290 storage::OPEN_FILE_SYSTEM_CREATE_IF_NONEXISTENT,
291 NULL));
295 TEST_F(SandboxFileSystemBackendTest, GetRootPathFileURIWithAllowFlag) {
296 SetUpNewBackend(CreateAllowFileAccessOptions());
297 for (size_t i = 0; i < arraysize(kRootPathFileURITestCases); ++i) {
298 SCOPED_TRACE(testing::Message() << "RootPathFileURI (allow) #"
299 << i << " " << kRootPathFileURITestCases[i].expected_path);
300 base::FilePath root_path;
301 EXPECT_TRUE(GetRootPath(GURL(kRootPathFileURITestCases[i].origin_url),
302 kRootPathFileURITestCases[i].type,
303 storage::OPEN_FILE_SYSTEM_CREATE_IF_NONEXISTENT,
304 &root_path));
305 base::FilePath expected = file_system_path().AppendASCII(
306 kRootPathFileURITestCases[i].expected_path);
307 EXPECT_EQ(expected.value(), root_path.value());
308 EXPECT_TRUE(base::DirectoryExists(root_path));
312 } // namespace content