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/memory/scoped_ptr.h"
9 #include "base/message_loop/message_loop.h"
10 #include "base/message_loop/message_loop_proxy.h"
11 #include "base/run_loop.h"
12 #include "content/public/test/async_file_test_helper.h"
13 #include "content/public/test/test_file_system_context.h"
14 #include "content/public/test/test_file_system_options.h"
15 #include "testing/gtest/include/gtest/gtest.h"
16 #include "webkit/browser/fileapi/file_system_context.h"
17 #include "webkit/browser/fileapi/isolated_context.h"
18 #include "webkit/browser/fileapi/obfuscated_file_util.h"
19 #include "webkit/browser/fileapi/plugin_private_file_system_backend.h"
20 #include "webkit/common/fileapi/file_system_util.h"
22 using content::AsyncFileTestHelper
;
23 using storage::FileSystemContext
;
24 using storage::FileSystemURL
;
25 using storage::IsolatedContext
;
31 const GURL
kOrigin("http://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
) {
42 std::string
RegisterFileSystem() {
43 return IsolatedContext::GetInstance()->RegisterFileSystemForVirtualPath(
44 kType
, kRootName
, base::FilePath());
49 class PluginPrivateFileSystemBackendTest
: public testing::Test
{
51 virtual void SetUp() OVERRIDE
{
52 ASSERT_TRUE(data_dir_
.CreateUniqueTempDir());
53 context_
= CreateFileSystemContextForTesting(
54 NULL
/* quota_manager_proxy */,
58 FileSystemURL
CreateURL(const GURL
& root_url
, const std::string
& relative
) {
59 FileSystemURL root
= context_
->CrackURL(root_url
);
60 return context_
->CreateCrackedFileSystemURL(
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_
;
75 std::string filesystem_id_
;
78 TEST_F(PluginPrivateFileSystemBackendTest
, OpenFileSystemBasic
) {
79 const std::string filesystem_id1
= RegisterFileSystem();
80 base::File::Error error
= base::File::FILE_ERROR_FAILED
;
81 backend()->OpenPrivateFileSystem(
86 storage::OPEN_FILE_SYSTEM_CREATE_IF_NONEXISTENT
,
87 base::Bind(&DidOpenFileSystem
, &error
));
88 base::RunLoop().RunUntilIdle();
89 ASSERT_EQ(base::File::FILE_OK
, error
);
91 // Run this again with FAIL_IF_NONEXISTENT to see if it succeeds.
92 const std::string filesystem_id2
= RegisterFileSystem();
93 error
= base::File::FILE_ERROR_FAILED
;
94 backend()->OpenPrivateFileSystem(
99 storage::OPEN_FILE_SYSTEM_FAIL_IF_NONEXISTENT
,
100 base::Bind(&DidOpenFileSystem
, &error
));
101 base::RunLoop().RunUntilIdle();
102 ASSERT_EQ(base::File::FILE_OK
, error
);
104 const GURL
root_url(storage::GetIsolatedFileSystemRootURIString(
105 kOrigin
, filesystem_id1
, kRootName
));
106 FileSystemURL file
= CreateURL(root_url
, "foo");
107 base::FilePath platform_path
;
108 EXPECT_EQ(base::File::FILE_OK
,
109 AsyncFileTestHelper::CreateFile(context_
.get(), file
));
110 EXPECT_EQ(base::File::FILE_OK
,
111 AsyncFileTestHelper::GetPlatformPath(context_
.get(), file
,
113 EXPECT_TRUE(base_path().AppendASCII("000").AppendASCII(kPlugin1
).IsParent(
117 TEST_F(PluginPrivateFileSystemBackendTest
, PluginIsolation
) {
118 // Open filesystem for kPlugin1 and kPlugin2.
119 const std::string filesystem_id1
= RegisterFileSystem();
120 base::File::Error error
= base::File::FILE_ERROR_FAILED
;
121 backend()->OpenPrivateFileSystem(
126 storage::OPEN_FILE_SYSTEM_CREATE_IF_NONEXISTENT
,
127 base::Bind(&DidOpenFileSystem
, &error
));
128 base::RunLoop().RunUntilIdle();
129 ASSERT_EQ(base::File::FILE_OK
, error
);
131 const std::string filesystem_id2
= RegisterFileSystem();
132 error
= base::File::FILE_ERROR_FAILED
;
133 backend()->OpenPrivateFileSystem(
138 storage::OPEN_FILE_SYSTEM_CREATE_IF_NONEXISTENT
,
139 base::Bind(&DidOpenFileSystem
, &error
));
140 base::RunLoop().RunUntilIdle();
141 ASSERT_EQ(base::File::FILE_OK
, error
);
143 // Create 'foo' in kPlugin1.
144 const GURL
root_url1(storage::GetIsolatedFileSystemRootURIString(
145 kOrigin
, filesystem_id1
, kRootName
));
146 FileSystemURL file1
= CreateURL(root_url1
, "foo");
147 base::FilePath platform_path
;
148 EXPECT_EQ(base::File::FILE_OK
,
149 AsyncFileTestHelper::CreateFile(context_
.get(), file1
));
150 EXPECT_TRUE(AsyncFileTestHelper::FileExists(
151 context_
.get(), file1
, AsyncFileTestHelper::kDontCheckSize
));
153 // See the same path is not available in kPlugin2.
154 const GURL
root_url2(storage::GetIsolatedFileSystemRootURIString(
155 kOrigin
, filesystem_id2
, kRootName
));
156 FileSystemURL file2
= CreateURL(root_url2
, "foo");
157 EXPECT_FALSE(AsyncFileTestHelper::FileExists(
158 context_
.get(), file2
, AsyncFileTestHelper::kDontCheckSize
));
161 // TODO(kinuko,nhiroki): also test if DeleteOriginDataOnFileThread
162 // works fine when there's multiple plugin partitions.
164 } // namespace content