Make USB permissions work in the new permission message system
[chromium-blink-merge.git] / content / browser / fileapi / plugin_private_file_system_backend_unittest.cc
blob8c5e45d0d37901d79a1a5ada32c0328c900021c9
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/files/file_util.h"
7 #include "base/files/scoped_temp_dir.h"
8 #include "base/memory/scoped_ptr.h"
9 #include "base/message_loop/message_loop.h"
10 #include "base/run_loop.h"
11 #include "content/public/test/async_file_test_helper.h"
12 #include "content/public/test/test_file_system_context.h"
13 #include "content/public/test/test_file_system_options.h"
14 #include "storage/browser/fileapi/file_system_context.h"
15 #include "storage/browser/fileapi/isolated_context.h"
16 #include "storage/browser/fileapi/obfuscated_file_util.h"
17 #include "storage/browser/fileapi/plugin_private_file_system_backend.h"
18 #include "storage/common/fileapi/file_system_util.h"
19 #include "testing/gtest/include/gtest/gtest.h"
21 using content::AsyncFileTestHelper;
22 using storage::FileSystemContext;
23 using storage::FileSystemURL;
24 using storage::IsolatedContext;
26 namespace content {
28 namespace {
30 const GURL kOrigin1("http://www.example.com");
31 const GURL kOrigin2("https://www.example.com");
32 const std::string kPlugin1("plugin1");
33 const std::string kPlugin2("plugin2");
34 const storage::FileSystemType kType = storage::kFileSystemTypePluginPrivate;
35 const std::string kRootName = "pluginprivate";
37 void DidOpenFileSystem(base::File::Error* error_out,
38 base::File::Error error) {
39 *error_out = error;
42 std::string RegisterFileSystem() {
43 return IsolatedContext::GetInstance()->RegisterFileSystemForVirtualPath(
44 kType, kRootName, base::FilePath());
47 } // namespace
49 class PluginPrivateFileSystemBackendTest : public testing::Test {
50 protected:
51 void SetUp() override {
52 ASSERT_TRUE(data_dir_.CreateUniqueTempDir());
53 context_ = CreateFileSystemContextForTesting(
54 NULL /* quota_manager_proxy */,
55 data_dir_.path());
58 FileSystemURL CreateURL(const GURL& root_url, const std::string& relative) {
59 FileSystemURL root = context_->CrackURL(root_url);
60 return context_->CreateCrackedFileSystemURL(
61 root.origin(),
62 root.mount_type(),
63 root.virtual_path().AppendASCII(relative));
66 storage::PluginPrivateFileSystemBackend* backend() const {
67 return context_->plugin_private_backend();
70 const base::FilePath& base_path() const { return backend()->base_path(); }
72 base::ScopedTempDir data_dir_;
73 base::MessageLoop message_loop_;
74 scoped_refptr<FileSystemContext> context_;
77 // TODO(kinuko,nhiroki): There are a lot of duplicate code in these tests. Write
78 // helper functions to simplify the tests.
80 TEST_F(PluginPrivateFileSystemBackendTest, OpenFileSystemBasic) {
81 const std::string filesystem_id1 = RegisterFileSystem();
82 base::File::Error error = base::File::FILE_ERROR_FAILED;
83 backend()->OpenPrivateFileSystem(
84 kOrigin1,
85 kType,
86 filesystem_id1,
87 kPlugin1,
88 storage::OPEN_FILE_SYSTEM_CREATE_IF_NONEXISTENT,
89 base::Bind(&DidOpenFileSystem, &error));
90 base::RunLoop().RunUntilIdle();
91 ASSERT_EQ(base::File::FILE_OK, error);
93 // Run this again with FAIL_IF_NONEXISTENT to see if it succeeds.
94 const std::string filesystem_id2 = RegisterFileSystem();
95 error = base::File::FILE_ERROR_FAILED;
96 backend()->OpenPrivateFileSystem(
97 kOrigin1,
98 kType,
99 filesystem_id2,
100 kPlugin1,
101 storage::OPEN_FILE_SYSTEM_FAIL_IF_NONEXISTENT,
102 base::Bind(&DidOpenFileSystem, &error));
103 base::RunLoop().RunUntilIdle();
104 ASSERT_EQ(base::File::FILE_OK, error);
106 const GURL root_url(storage::GetIsolatedFileSystemRootURIString(
107 kOrigin1, filesystem_id1, kRootName));
108 FileSystemURL file = CreateURL(root_url, "foo");
109 base::FilePath platform_path;
110 EXPECT_EQ(base::File::FILE_OK,
111 AsyncFileTestHelper::CreateFile(context_.get(), file));
112 EXPECT_EQ(base::File::FILE_OK,
113 AsyncFileTestHelper::GetPlatformPath(context_.get(), file,
114 &platform_path));
115 EXPECT_TRUE(base_path().AppendASCII("000").AppendASCII(kPlugin1).IsParent(
116 platform_path));
119 TEST_F(PluginPrivateFileSystemBackendTest, PluginIsolation) {
120 // Open filesystem for kPlugin1 and kPlugin2.
121 const std::string filesystem_id1 = RegisterFileSystem();
122 base::File::Error error = base::File::FILE_ERROR_FAILED;
123 backend()->OpenPrivateFileSystem(
124 kOrigin1,
125 kType,
126 filesystem_id1,
127 kPlugin1,
128 storage::OPEN_FILE_SYSTEM_CREATE_IF_NONEXISTENT,
129 base::Bind(&DidOpenFileSystem, &error));
130 base::RunLoop().RunUntilIdle();
131 ASSERT_EQ(base::File::FILE_OK, error);
133 const std::string filesystem_id2 = RegisterFileSystem();
134 error = base::File::FILE_ERROR_FAILED;
135 backend()->OpenPrivateFileSystem(
136 kOrigin1,
137 kType,
138 filesystem_id2,
139 kPlugin2,
140 storage::OPEN_FILE_SYSTEM_CREATE_IF_NONEXISTENT,
141 base::Bind(&DidOpenFileSystem, &error));
142 base::RunLoop().RunUntilIdle();
143 ASSERT_EQ(base::File::FILE_OK, error);
145 // Create 'foo' in kPlugin1.
146 const GURL root_url1(storage::GetIsolatedFileSystemRootURIString(
147 kOrigin1, filesystem_id1, kRootName));
148 FileSystemURL file1 = CreateURL(root_url1, "foo");
149 EXPECT_EQ(base::File::FILE_OK,
150 AsyncFileTestHelper::CreateFile(context_.get(), file1));
151 EXPECT_TRUE(AsyncFileTestHelper::FileExists(
152 context_.get(), file1, AsyncFileTestHelper::kDontCheckSize));
154 // See the same path is not available in kPlugin2.
155 const GURL root_url2(storage::GetIsolatedFileSystemRootURIString(
156 kOrigin1, filesystem_id2, kRootName));
157 FileSystemURL file2 = CreateURL(root_url2, "foo");
158 EXPECT_FALSE(AsyncFileTestHelper::FileExists(
159 context_.get(), file2, AsyncFileTestHelper::kDontCheckSize));
162 TEST_F(PluginPrivateFileSystemBackendTest, OriginIsolation) {
163 // Open filesystem for kOrigin1 and kOrigin2.
164 const std::string filesystem_id1 = RegisterFileSystem();
165 base::File::Error error = base::File::FILE_ERROR_FAILED;
166 backend()->OpenPrivateFileSystem(
167 kOrigin1,
168 kType,
169 filesystem_id1,
170 kPlugin1,
171 storage::OPEN_FILE_SYSTEM_CREATE_IF_NONEXISTENT,
172 base::Bind(&DidOpenFileSystem, &error));
173 base::RunLoop().RunUntilIdle();
174 ASSERT_EQ(base::File::FILE_OK, error);
176 const std::string filesystem_id2 = RegisterFileSystem();
177 error = base::File::FILE_ERROR_FAILED;
178 backend()->OpenPrivateFileSystem(
179 kOrigin2,
180 kType,
181 filesystem_id2,
182 kPlugin1,
183 storage::OPEN_FILE_SYSTEM_CREATE_IF_NONEXISTENT,
184 base::Bind(&DidOpenFileSystem, &error));
185 base::RunLoop().RunUntilIdle();
186 ASSERT_EQ(base::File::FILE_OK, error);
188 // Create 'foo' in kOrigin1.
189 const GURL root_url1(storage::GetIsolatedFileSystemRootURIString(
190 kOrigin1, filesystem_id1, kRootName));
191 FileSystemURL file1 = CreateURL(root_url1, "foo");
192 EXPECT_EQ(base::File::FILE_OK,
193 AsyncFileTestHelper::CreateFile(context_.get(), file1));
194 EXPECT_TRUE(AsyncFileTestHelper::FileExists(
195 context_.get(), file1, AsyncFileTestHelper::kDontCheckSize));
197 // See the same path is not available in kOrigin2.
198 const GURL root_url2(storage::GetIsolatedFileSystemRootURIString(
199 kOrigin2, filesystem_id2, kRootName));
200 FileSystemURL file2 = CreateURL(root_url2, "foo");
201 EXPECT_FALSE(AsyncFileTestHelper::FileExists(
202 context_.get(), file2, AsyncFileTestHelper::kDontCheckSize));
205 TEST_F(PluginPrivateFileSystemBackendTest, DeleteOriginDirectory) {
206 // Open filesystem for kOrigin1 and kOrigin2.
207 const std::string filesystem_id1 = RegisterFileSystem();
208 base::File::Error error = base::File::FILE_ERROR_FAILED;
209 backend()->OpenPrivateFileSystem(
210 kOrigin1,
211 kType,
212 filesystem_id1,
213 kPlugin1,
214 storage::OPEN_FILE_SYSTEM_CREATE_IF_NONEXISTENT,
215 base::Bind(&DidOpenFileSystem, &error));
216 base::RunLoop().RunUntilIdle();
217 ASSERT_EQ(base::File::FILE_OK, error);
219 const std::string filesystem_id2 = RegisterFileSystem();
220 error = base::File::FILE_ERROR_FAILED;
221 backend()->OpenPrivateFileSystem(
222 kOrigin2,
223 kType,
224 filesystem_id2,
225 kPlugin1,
226 storage::OPEN_FILE_SYSTEM_CREATE_IF_NONEXISTENT,
227 base::Bind(&DidOpenFileSystem, &error));
228 base::RunLoop().RunUntilIdle();
229 ASSERT_EQ(base::File::FILE_OK, error);
231 // Create 'foo' in kOrigin1.
232 const GURL root_url1(storage::GetIsolatedFileSystemRootURIString(
233 kOrigin1, filesystem_id1, kRootName));
234 FileSystemURL file1 = CreateURL(root_url1, "foo");
235 EXPECT_EQ(base::File::FILE_OK,
236 AsyncFileTestHelper::CreateFile(context_.get(), file1));
237 EXPECT_TRUE(AsyncFileTestHelper::FileExists(
238 context_.get(), file1, AsyncFileTestHelper::kDontCheckSize));
240 // Create 'foo' in kOrigin2.
241 const GURL root_url2(storage::GetIsolatedFileSystemRootURIString(
242 kOrigin2, filesystem_id2, kRootName));
243 FileSystemURL file2 = CreateURL(root_url2, "foo");
244 EXPECT_EQ(base::File::FILE_OK,
245 AsyncFileTestHelper::CreateFile(context_.get(), file2));
246 EXPECT_TRUE(AsyncFileTestHelper::FileExists(
247 context_.get(), file2, AsyncFileTestHelper::kDontCheckSize));
249 // Delete data for kOrigin1.
250 error = backend()->DeleteOriginDataOnFileTaskRunner(
251 context_.get(), NULL, kOrigin1, kType);
252 EXPECT_EQ(base::File::FILE_OK, error);
254 // Confirm 'foo' in kOrigin1 is deleted.
255 EXPECT_FALSE(AsyncFileTestHelper::FileExists(
256 context_.get(), file1, AsyncFileTestHelper::kDontCheckSize));
258 // Confirm 'foo' in kOrigin2 is NOT deleted.
259 EXPECT_TRUE(AsyncFileTestHelper::FileExists(
260 context_.get(), file2, AsyncFileTestHelper::kDontCheckSize));
262 // Re-open filesystem for kOrigin1.
263 const std::string filesystem_id3 = RegisterFileSystem();
264 error = base::File::FILE_ERROR_FAILED;
265 backend()->OpenPrivateFileSystem(
266 kOrigin1,
267 kType,
268 filesystem_id3,
269 kPlugin1,
270 storage::OPEN_FILE_SYSTEM_CREATE_IF_NONEXISTENT,
271 base::Bind(&DidOpenFileSystem, &error));
272 base::RunLoop().RunUntilIdle();
273 ASSERT_EQ(base::File::FILE_OK, error);
275 // Re-create 'foo' in kOrigin1.
276 const GURL root_url3(storage::GetIsolatedFileSystemRootURIString(
277 kOrigin1, filesystem_id3, kRootName));
278 FileSystemURL file3 = CreateURL(root_url3, "foo");
279 EXPECT_EQ(base::File::FILE_OK,
280 AsyncFileTestHelper::CreateFile(context_.get(), file3));
281 EXPECT_TRUE(AsyncFileTestHelper::FileExists(
282 context_.get(), file3, AsyncFileTestHelper::kDontCheckSize));
284 // Confirm 'foo' in kOrigin1 is re-created.
285 EXPECT_TRUE(AsyncFileTestHelper::FileExists(
286 context_.get(), file3, AsyncFileTestHelper::kDontCheckSize));
289 } // namespace content