Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(320)

Side by Side Diff: chrome/browser/extensions/extension_local_filesystem_apitest.cc

Issue 9808023: Grant file access permissions for cached file paths to file browsers/handlers. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: . Created 8 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
1 // Copyright (c) 2012 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/file_util.h"
6 #include "base/platform_file.h"
7 #include "base/stringprintf.h"
8 #include "chrome/browser/extensions/extension_apitest.h"
9 #include "chrome/browser/profiles/profile.h"
10 #include "chrome/browser/ui/browser.h"
11 #include "testing/gmock/include/gmock/gmock.h"
12 #include "webkit/fileapi/file_system_context.h"
13 #include "webkit/fileapi/file_system_mount_point_provider.h"
14 #if defined(OS_CHROMEOS)
15 #include "chrome/browser/chromeos/gdata/gdata_util.h"
16 #include "webkit/chromeos/fileapi/remote_file_system_proxy.h"
17 #endif
18
19 using ::testing::_;
20 using content::BrowserContext;
21
22 #if defined(OS_CHROMEOS)
23
24 // These should match the counterparts in remote.js.
25 const char kTestFileName[] = "hello.txt";
26 const char kTestFileContents[] = "hello, world";
27
28 namespace {
29
30 // The ID of the file browser extension.
31 const char kFileBrowserExtensionId[] = "ddammdhioacbehjngdmkjcjbnfginlla";
32
33 // Returns the expected URL for the given path.
34 GURL GetExpectedURL(const std::string& path) {
35 return GURL(
36 base::StringPrintf(
37 "filesystem:chrome-extension://%s/external/%s",
38 kFileBrowserExtensionId,
39 path.c_str()));
40 }
41
42 // Action used to set mock expectations for CreateDirectory().
43 ACTION_P(MockCreateDirectory, status) {
44 arg3.Run(status);
45 }
46
47 // Action used to set mock expectations for GetFileInfo().
48 ACTION_P3(MockGetFileInfo, status, file_info, path) {
49 arg1.Run(status, file_info, path);
50 }
51
52 // Action used to set mock expectations for CreateSnapshotFile().
53 ACTION_P4(MockCreateSnapshotFile, status, file_info, path, file_ref) {
54 arg1.Run(status, file_info, path, file_ref);
55 }
56
57 // The mock is used to add a remote mount point, and write tests for it.
58 class MockRemoteFileSystemProxy :
59 public fileapi::RemoteFileSystemProxyInterface {
60 public:
61 MockRemoteFileSystemProxy() {}
62 virtual ~MockRemoteFileSystemProxy() {}
63
64 MOCK_METHOD2(
65 GetFileInfo,
66 void(const GURL& path,
67 const fileapi::FileSystemOperationInterface::GetMetadataCallback&
68 callback));
69 MOCK_METHOD3(
70 Copy,
71 void(const GURL& src_path,
72 const GURL& dest_path,
73 const fileapi::FileSystemOperationInterface::StatusCallback&
74 callback));
75 MOCK_METHOD3(
76 Move,
77 void(const GURL& src_path,
78 const GURL& dest_path,
79 const fileapi::FileSystemOperationInterface::StatusCallback&
80 callback));
81 MOCK_METHOD2(
82 ReadDirectory,
83 void(const GURL& path,
84 const fileapi::FileSystemOperationInterface::ReadDirectoryCallback&
85 callback));
86 MOCK_METHOD3(
87 Remove,
88 void(const GURL& path,
89 bool recursive,
90 const fileapi::FileSystemOperationInterface::StatusCallback&
91 callback));
92 MOCK_METHOD4(
93 CreateDirectory,
94 void(const GURL& file_url,
95 bool exclusive,
96 bool recursive,
97 const fileapi::FileSystemOperationInterface::StatusCallback&
98 callback));
99 MOCK_METHOD2(
100 CreateSnapshotFile,
101 void(const GURL& path,
102 const fileapi::FileSystemOperationInterface::SnapshotFileCallback&
103 callback));
104 };
105
106 const char kExpectedWriteError[] =
107 "Got unexpected error: File handler error: SECURITY_ERR";
108 }
109
110 class FileSystemExtensionApiTest : public ExtensionApiTest {
111 public:
112 FileSystemExtensionApiTest() : test_mount_point_("/tmp") {
113 }
114
115 virtual ~FileSystemExtensionApiTest() {}
116
117 // Adds a local mount point at at mount point /tmp.
118 void AddTmpMountPoint() {
119 fileapi::ExternalFileSystemMountPointProvider* provider =
120 BrowserContext::GetFileSystemContext(browser()->profile())->
121 external_provider();
122 provider->AddLocalMountPoint(test_mount_point_);
123 }
124
125 private:
126 FilePath test_mount_point_;
127 };
128
129 class RemoteFileSystemExtensionApiTest : public ExtensionApiTest {
130 public:
131 RemoteFileSystemExtensionApiTest()
132 : test_mount_point_("/tmp"),
133 mock_remote_file_system_proxy_(NULL) {
134 }
135
136 virtual ~RemoteFileSystemExtensionApiTest() {}
137
138 virtual void SetUp() OVERRIDE {
139 file_util::CreateTemporaryFile(&test_file_path_);
140 file_util::WriteFile(test_file_path_,
141 kTestFileContents,
142 sizeof(kTestFileContents) - 1);
143 file_util::GetFileInfo(test_file_path_, &test_file_info_);
144
145 // ExtensionApiTest::SetUp() should be called at the end. For some
146 // reason, ExtensionApiTest::SetUp() starts running tests, so any
147 // setup has to be done before calling this.
148 ExtensionApiTest::SetUp();
149 }
150
151 virtual void TearDown() OVERRIDE {
152 file_util::Delete(test_file_path_, false);
153
154 ExtensionApiTest::TearDown();
155 }
156
157 // Adds a remote mount point at at mount point /tmp.
158 void AddTmpMountPoint() {
159 fileapi::ExternalFileSystemMountPointProvider* provider =
160 BrowserContext::GetFileSystemContext(browser()->profile())->
161 external_provider();
162 mock_remote_file_system_proxy_ = new MockRemoteFileSystemProxy;
163 // Take the ownership of mock_remote_file_system_proxy_.
164 provider->AddRemoteMountPoint(test_mount_point_,
165 mock_remote_file_system_proxy_);
166 }
167
168 protected:
169 base::PlatformFileInfo test_file_info_;
170 FilePath test_file_path_;
171 FilePath test_mount_point_;
172 MockRemoteFileSystemProxy* mock_remote_file_system_proxy_;
173 };
174
175 IN_PROC_BROWSER_TEST_F(FileSystemExtensionApiTest, LocalFileSystem) {
176 AddTmpMountPoint();
177 ASSERT_TRUE(RunComponentExtensionTest("local_filesystem")) << message_;
178 }
179
180 IN_PROC_BROWSER_TEST_F(FileSystemExtensionApiTest, FileBrowserTest) {
181 AddTmpMountPoint();
182 ASSERT_TRUE(RunExtensionTest("filesystem_handler")) << message_;
183 ASSERT_TRUE(RunComponentExtensionSubtest("filebrowser_component",
184 "read.html")) << message_;
185 }
186
187 IN_PROC_BROWSER_TEST_F(FileSystemExtensionApiTest, FileBrowserTestWrite) {
188 AddTmpMountPoint();
189 ASSERT_TRUE(RunExtensionTest("filesystem_handler_write")) << message_;
190 ASSERT_TRUE(RunComponentExtensionSubtest("filebrowser_component",
191 "write.html")) << message_;
192 }
193
194 IN_PROC_BROWSER_TEST_F(FileSystemExtensionApiTest,
195 FileBrowserTestWriteComponent) {
196 AddTmpMountPoint();
197 ASSERT_TRUE(RunComponentExtensionTest("filesystem_handler_write"))
198 << message_;
199 ASSERT_TRUE(RunComponentExtensionSubtest("filebrowser_component",
200 "write.html")) << message_;
201 }
202
203 IN_PROC_BROWSER_TEST_F(RemoteFileSystemExtensionApiTest, RemoteMountPoint) {
204 AddTmpMountPoint();
205
206 // The test directory is created first.
207 const GURL test_dir_url = GetExpectedURL("tmp/test_dir");
208 EXPECT_CALL(*mock_remote_file_system_proxy_,
209 CreateDirectory(test_dir_url, false, false, _))
210 .WillOnce(MockCreateDirectory(base::PLATFORM_FILE_OK));
211
212 // Then GetFileInfo() is called over "tmp/test_dir/hello.txt".
213 const std::string expected_path =
214 std::string("tmp/test_dir/") + kTestFileName;
215 GURL expected_url = GetExpectedURL(expected_path);
216 EXPECT_CALL(*mock_remote_file_system_proxy_,
217 GetFileInfo(expected_url, _))
218 .WillOnce(MockGetFileInfo(
219 base::PLATFORM_FILE_OK,
220 test_file_info_,
221 FilePath::FromUTF8Unsafe(expected_path)));
222
223 // Then CreateSnapshotFile() is called over "tmp/test_dir/hello.txt".
224 EXPECT_CALL(*mock_remote_file_system_proxy_,
225 CreateSnapshotFile(expected_url, _))
226 .WillOnce(MockCreateSnapshotFile(
227 base::PLATFORM_FILE_OK,
228 test_file_info_,
229 // Returns the path to the temporary file on the local drive.
230 test_file_path_,
231 scoped_refptr<webkit_blob::ShareableFileReference>(NULL)));
232
233 ASSERT_TRUE(RunComponentExtensionSubtest("filebrowser_component",
234 "remote.html")) << message_;
235 }
236
237 #endif
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698