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

Side by Side Diff: webkit/fileapi/copy_or_move_file_validator_unittest.cc

Issue 14352004: Split Media-related code from IsolatedMountPointProvider (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 7 years, 8 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
1 // Copyright 2013 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "base/basictypes.h" 5 #include "base/basictypes.h"
6 #include "base/bind.h" 6 #include "base/bind.h"
7 #include "base/files/file_path.h" 7 #include "base/files/file_path.h"
8 #include "base/files/scoped_temp_dir.h" 8 #include "base/files/scoped_temp_dir.h"
9 #include "base/message_loop.h" 9 #include "base/message_loop.h"
10 #include "testing/gtest/include/gtest/gtest.h" 10 #include "testing/gtest/include/gtest/gtest.h"
11 #include "webkit/fileapi/async_file_test_helper.h" 11 #include "webkit/fileapi/async_file_test_helper.h"
12 #include "webkit/fileapi/copy_or_move_file_validator.h" 12 #include "webkit/fileapi/copy_or_move_file_validator.h"
13 #include "webkit/fileapi/external_mount_points.h" 13 #include "webkit/fileapi/external_mount_points.h"
14 #include "webkit/fileapi/file_system_context.h" 14 #include "webkit/fileapi/file_system_context.h"
15 #include "webkit/fileapi/file_system_mount_point_provider.h" 15 #include "webkit/fileapi/file_system_mount_point_provider.h"
16 #include "webkit/fileapi/file_system_url.h" 16 #include "webkit/fileapi/file_system_url.h"
17 #include "webkit/fileapi/file_system_util.h" 17 #include "webkit/fileapi/file_system_util.h"
18 #include "webkit/fileapi/isolated_context.h" 18 #include "webkit/fileapi/isolated_context.h"
19 #include "webkit/fileapi/mock_file_system_context.h" 19 #include "webkit/fileapi/mock_file_system_context.h"
20 #include "webkit/fileapi/test_mount_point_provider.h"
20 #include "webkit/quota/mock_special_storage_policy.h" 21 #include "webkit/quota/mock_special_storage_policy.h"
21 22
22 namespace fileapi { 23 namespace fileapi {
23 24
25 namespace {
26
27 const FileSystemType kNoValidatorType = kFileSystemTypeTemporary;
28 const FileSystemType kWithValidatorType = kFileSystemTypeTest;
29
24 class CopyOrMoveFileValidatorTestHelper { 30 class CopyOrMoveFileValidatorTestHelper {
25 public: 31 public:
26 CopyOrMoveFileValidatorTestHelper( 32 CopyOrMoveFileValidatorTestHelper(
27 const GURL& origin, 33 const GURL& origin,
28 FileSystemType src_type, 34 FileSystemType src_type,
29 FileSystemType dest_type) 35 FileSystemType dest_type)
30 : origin_(origin), 36 : origin_(origin),
31 src_type_(src_type), 37 src_type_(src_type),
32 dest_type_(dest_type) {} 38 dest_type_(dest_type) {}
33 39
34 ~CopyOrMoveFileValidatorTestHelper() { 40 ~CopyOrMoveFileValidatorTestHelper() {
35 file_system_context_ = NULL; 41 file_system_context_ = NULL;
36 MessageLoop::current()->RunUntilIdle(); 42 MessageLoop::current()->RunUntilIdle();
37 } 43 }
38 44
39 void SetUp() { 45 void SetUp() {
40 ASSERT_TRUE(base_.CreateUniqueTempDir()); 46 ASSERT_TRUE(base_.CreateUniqueTempDir());
41 base::FilePath base_dir = base_.path(); 47 base::FilePath base_dir = base_.path();
48
42 file_system_context_ = CreateFileSystemContextForTesting(NULL, base_dir); 49 file_system_context_ = CreateFileSystemContextForTesting(NULL, base_dir);
43 50
44 // Prepare the origin's root directory. 51 // Set up TestMountPointProvider to require CopyOrMoveFileValidator.
45 if (src_type_ == kFileSystemTypeNativeMedia) { 52 FileSystemMountPointProvider* test_mount_point_provider =
46 base::FilePath src_path = base_dir.Append(FILE_PATH_LITERAL("src_media")); 53 file_system_context_->GetMountPointProvider(kWithValidatorType);
47 file_util::CreateDirectory(src_path); 54 static_cast<TestMountPointProvider*>(test_mount_point_provider)->
48 src_fsid_ = IsolatedContext::GetInstance()->RegisterFileSystemForPath( 55 set_require_copy_or_move_validator(true);
49 kFileSystemTypeNativeMedia, src_path, NULL); 56
50 } else { 57 // Sets up source.
51 FileSystemMountPointProvider* mount_point_provider = 58 FileSystemMountPointProvider* src_mount_point_provider =
52 file_system_context_->GetMountPointProvider(src_type_); 59 file_system_context_->GetMountPointProvider(src_type_);
53 mount_point_provider->GetFileSystemRootPathOnFileThread( 60 src_mount_point_provider->GetFileSystemRootPathOnFileThread(
54 SourceURL(std::string()), true /* create */); 61 SourceURL(std::string()), true /* create */);
55 } 62 ASSERT_EQ(base::PLATFORM_FILE_OK, CreateDirectory(SourceURL("")));
56 DCHECK_EQ(kFileSystemTypeNativeMedia, dest_type_); 63
57 base::FilePath dest_path = base_dir.Append(FILE_PATH_LITERAL("dest_media")); 64 // Sets up dest.
58 file_util::CreateDirectory(dest_path); 65 DCHECK_EQ(kWithValidatorType, dest_type_);
59 dest_fsid_ = IsolatedContext::GetInstance()->RegisterFileSystemForPath( 66 ASSERT_EQ(base::PLATFORM_FILE_OK, CreateDirectory(DestURL("")));
60 kFileSystemTypeNativeMedia, dest_path, NULL);
61 67
62 copy_src_ = SourceURL("copy_src.jpg"); 68 copy_src_ = SourceURL("copy_src.jpg");
63 move_src_ = SourceURL("move_src.jpg"); 69 move_src_ = SourceURL("move_src.jpg");
64 copy_dest_ = DestURL("copy_dest.jpg"); 70 copy_dest_ = DestURL("copy_dest.jpg");
65 move_dest_ = DestURL("move_dest.jpg"); 71 move_dest_ = DestURL("move_dest.jpg");
66 72
67 ASSERT_EQ(base::PLATFORM_FILE_OK, CreateFile(copy_src_, 10)); 73 ASSERT_EQ(base::PLATFORM_FILE_OK, CreateFile(copy_src_, 10));
68 ASSERT_EQ(base::PLATFORM_FILE_OK, CreateFile(move_src_, 10)); 74 ASSERT_EQ(base::PLATFORM_FILE_OK, CreateFile(move_src_, 10));
69 75
70 ASSERT_TRUE(FileExists(copy_src_, 10)); 76 ASSERT_TRUE(FileExists(copy_src_, 10));
71 ASSERT_TRUE(FileExists(move_src_, 10)); 77 ASSERT_TRUE(FileExists(move_src_, 10));
72 ASSERT_FALSE(FileExists(copy_dest_, 10)); 78 ASSERT_FALSE(FileExists(copy_dest_, 10));
73 ASSERT_FALSE(FileExists(move_dest_, 10)); 79 ASSERT_FALSE(FileExists(move_dest_, 10));
74 } 80 }
75 81
76 void SetMediaCopyOrMoveFileValidatorFactory( 82 void SetMediaCopyOrMoveFileValidatorFactory(
77 scoped_ptr<CopyOrMoveFileValidatorFactory> factory) { 83 scoped_ptr<CopyOrMoveFileValidatorFactory> factory) {
78 FileSystemMountPointProvider* mount_point_provider = 84 FileSystemMountPointProvider* mount_point_provider =
79 file_system_context_->GetMountPointProvider(kFileSystemTypeNativeMedia); 85 file_system_context_->GetMountPointProvider(kWithValidatorType);
80 mount_point_provider->InitializeCopyOrMoveFileValidatorFactory( 86 mount_point_provider->InitializeCopyOrMoveFileValidatorFactory(
81 kFileSystemTypeNativeMedia, factory.Pass()); 87 kWithValidatorType, factory.Pass());
82 } 88 }
83 89
84 void CopyTest(base::PlatformFileError expected) { 90 void CopyTest(base::PlatformFileError expected) {
85 ASSERT_TRUE(FileExists(copy_src_, 10)); 91 ASSERT_TRUE(FileExists(copy_src_, 10));
86 ASSERT_FALSE(FileExists(copy_dest_, 10)); 92 ASSERT_FALSE(FileExists(copy_dest_, 10));
87 93
88 EXPECT_EQ(expected, 94 EXPECT_EQ(expected,
89 AsyncFileTestHelper::Copy(file_system_context_, copy_src_, 95 AsyncFileTestHelper::Copy(file_system_context_, copy_src_,
90 copy_dest_)); 96 copy_dest_));
91 97
(...skipping 16 matching lines...) Expand all
108 EXPECT_FALSE(FileExists(move_src_, 10)); 114 EXPECT_FALSE(FileExists(move_src_, 10));
109 EXPECT_TRUE(FileExists(move_dest_, 10)); 115 EXPECT_TRUE(FileExists(move_dest_, 10));
110 } else { 116 } else {
111 EXPECT_TRUE(FileExists(move_src_, 10)); 117 EXPECT_TRUE(FileExists(move_src_, 10));
112 EXPECT_FALSE(FileExists(move_dest_, 10)); 118 EXPECT_FALSE(FileExists(move_dest_, 10));
113 } 119 }
114 }; 120 };
115 121
116 private: 122 private:
117 FileSystemURL SourceURL(const std::string& path) { 123 FileSystemURL SourceURL(const std::string& path) {
118 if (src_type_ == kFileSystemTypeNativeMedia) {
119 std::string root_fs_url = GetIsolatedFileSystemRootURIString(
120 origin_, src_fsid_, "src_media/");
121 return file_system_context_->CrackURL(GURL(root_fs_url + path));
122 }
123 return file_system_context_->CreateCrackedFileSystemURL( 124 return file_system_context_->CreateCrackedFileSystemURL(
124 origin_, src_type_, base::FilePath::FromUTF8Unsafe(path)); 125 origin_, src_type_,
126 base::FilePath().AppendASCII("src").AppendASCII(path));
125 } 127 }
126 128
127 FileSystemURL DestURL(const std::string& path) { 129 FileSystemURL DestURL(const std::string& path) {
128 std::string root_fs_url = GetIsolatedFileSystemRootURIString( 130 return file_system_context_->CreateCrackedFileSystemURL(
129 origin_, dest_fsid_, "dest_media/"); 131 origin_, dest_type_,
130 return file_system_context_->CrackURL(GURL(root_fs_url + path)); 132 base::FilePath().AppendASCII("dest").AppendASCII(path));
131 } 133 }
132 134
133 base::PlatformFileError CreateFile(const FileSystemURL& url, size_t size) { 135 base::PlatformFileError CreateFile(const FileSystemURL& url, size_t size) {
134 base::PlatformFileError result = 136 base::PlatformFileError result =
135 AsyncFileTestHelper::CreateFile(file_system_context_, url); 137 AsyncFileTestHelper::CreateFile(file_system_context_, url);
136 if (result != base::PLATFORM_FILE_OK) 138 if (result != base::PLATFORM_FILE_OK)
137 return result; 139 return result;
138 return AsyncFileTestHelper::TruncateFile(file_system_context_, url, size); 140 return AsyncFileTestHelper::TruncateFile(file_system_context_, url, size);
139 } 141 }
140 142
143 base::PlatformFileError CreateDirectory(const FileSystemURL& url) {
144 return AsyncFileTestHelper::CreateDirectory(file_system_context_, url);
145 }
146
141 bool FileExists(const FileSystemURL& url, int64 expected_size) { 147 bool FileExists(const FileSystemURL& url, int64 expected_size) {
142 return AsyncFileTestHelper::FileExists( 148 return AsyncFileTestHelper::FileExists(
143 file_system_context_, url, expected_size); 149 file_system_context_, url, expected_size);
144 } 150 }
145 151
146 base::ScopedTempDir base_; 152 base::ScopedTempDir base_;
147 153
148 const GURL origin_; 154 const GURL origin_;
149 155
150 const FileSystemType src_type_; 156 const FileSystemType src_type_;
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after
194 base::PlatformFileError result_; 200 base::PlatformFileError result_;
195 201
196 DISALLOW_COPY_AND_ASSIGN(TestCopyOrMoveFileValidator); 202 DISALLOW_COPY_AND_ASSIGN(TestCopyOrMoveFileValidator);
197 }; 203 };
198 204
199 bool all_valid_; 205 bool all_valid_;
200 206
201 DISALLOW_COPY_AND_ASSIGN(TestCopyOrMoveFileValidatorFactory); 207 DISALLOW_COPY_AND_ASSIGN(TestCopyOrMoveFileValidatorFactory);
202 }; 208 };
203 209
210 } // namespace
211
204 TEST(CopyOrMoveFileValidatorTest, NoValidatorWithin6ameFSType) { 212 TEST(CopyOrMoveFileValidatorTest, NoValidatorWithin6ameFSType) {
205 // Within a file system type, validation is not expected, so it should 213 // Within a file system type, validation is not expected, so it should
206 // work for kFileSystemTypeNativeMedia without a validator set. 214 // work for kWithValidatorType without a validator set.
207 CopyOrMoveFileValidatorTestHelper helper(GURL("http://foo"), 215 CopyOrMoveFileValidatorTestHelper helper(GURL("http://foo"),
208 kFileSystemTypeNativeMedia, 216 kWithValidatorType,
209 kFileSystemTypeNativeMedia); 217 kWithValidatorType);
210 helper.SetUp(); 218 helper.SetUp();
211 helper.CopyTest(base::PLATFORM_FILE_OK); 219 helper.CopyTest(base::PLATFORM_FILE_OK);
212 helper.MoveTest(base::PLATFORM_FILE_OK); 220 helper.MoveTest(base::PLATFORM_FILE_OK);
213 } 221 }
214 222
215 TEST(CopyOrMoveFileValidatorTest, MissingValidator) { 223 TEST(CopyOrMoveFileValidatorTest, MissingValidator) {
216 // Copying or moving into a kFileSystemTypeNativeMedia requires a file 224 // Copying or moving into a kWithValidatorType requires a file
217 // validator. An error is expect if copy is attempted without a validator. 225 // validator. An error is expect if copy is attempted without a validator.
218 CopyOrMoveFileValidatorTestHelper helper(GURL("http://foo"), 226 CopyOrMoveFileValidatorTestHelper helper(GURL("http://foo"),
219 kFileSystemTypeTemporary, 227 kNoValidatorType,
220 kFileSystemTypeNativeMedia); 228 kWithValidatorType);
221 helper.SetUp(); 229 helper.SetUp();
222 helper.CopyTest(base::PLATFORM_FILE_ERROR_SECURITY); 230 helper.CopyTest(base::PLATFORM_FILE_ERROR_SECURITY);
223 helper.MoveTest(base::PLATFORM_FILE_ERROR_SECURITY); 231 helper.MoveTest(base::PLATFORM_FILE_ERROR_SECURITY);
224 } 232 }
225 233
226 TEST(CopyOrMoveFileValidatorTest, AcceptAll) { 234 TEST(CopyOrMoveFileValidatorTest, AcceptAll) {
227 CopyOrMoveFileValidatorTestHelper helper(GURL("http://foo"), 235 CopyOrMoveFileValidatorTestHelper helper(GURL("http://foo"),
228 kFileSystemTypeTemporary, 236 kNoValidatorType,
229 kFileSystemTypeNativeMedia); 237 kWithValidatorType);
230 helper.SetUp(); 238 helper.SetUp();
231 scoped_ptr<CopyOrMoveFileValidatorFactory> factory( 239 scoped_ptr<CopyOrMoveFileValidatorFactory> factory(
232 new TestCopyOrMoveFileValidatorFactory(true /*accept_all*/)); 240 new TestCopyOrMoveFileValidatorFactory(true /*accept_all*/));
233 helper.SetMediaCopyOrMoveFileValidatorFactory(factory.Pass()); 241 helper.SetMediaCopyOrMoveFileValidatorFactory(factory.Pass());
234 242
235 helper.CopyTest(base::PLATFORM_FILE_OK); 243 helper.CopyTest(base::PLATFORM_FILE_OK);
236 helper.MoveTest(base::PLATFORM_FILE_OK); 244 helper.MoveTest(base::PLATFORM_FILE_OK);
237 } 245 }
238 246
239 TEST(CopyOrMoveFileValidatorTest, AcceptNone) { 247 TEST(CopyOrMoveFileValidatorTest, AcceptNone) {
240 CopyOrMoveFileValidatorTestHelper helper(GURL("http://foo"), 248 CopyOrMoveFileValidatorTestHelper helper(GURL("http://foo"),
241 kFileSystemTypeTemporary, 249 kNoValidatorType,
242 kFileSystemTypeNativeMedia); 250 kWithValidatorType);
243 helper.SetUp(); 251 helper.SetUp();
244 scoped_ptr<CopyOrMoveFileValidatorFactory> factory( 252 scoped_ptr<CopyOrMoveFileValidatorFactory> factory(
245 new TestCopyOrMoveFileValidatorFactory(false /*accept_all*/)); 253 new TestCopyOrMoveFileValidatorFactory(false /*accept_all*/));
246 helper.SetMediaCopyOrMoveFileValidatorFactory(factory.Pass()); 254 helper.SetMediaCopyOrMoveFileValidatorFactory(factory.Pass());
247 255
248 helper.CopyTest(base::PLATFORM_FILE_ERROR_SECURITY); 256 helper.CopyTest(base::PLATFORM_FILE_ERROR_SECURITY);
249 helper.MoveTest(base::PLATFORM_FILE_ERROR_SECURITY); 257 helper.MoveTest(base::PLATFORM_FILE_ERROR_SECURITY);
250 } 258 }
251 259
252 TEST(CopyOrMoveFileValidatorTest, OverrideValidator) { 260 TEST(CopyOrMoveFileValidatorTest, OverrideValidator) {
253 // Once set, you can not override the validator. 261 // Once set, you can not override the validator.
254 CopyOrMoveFileValidatorTestHelper helper(GURL("http://foo"), 262 CopyOrMoveFileValidatorTestHelper helper(GURL("http://foo"),
255 kFileSystemTypeTemporary, 263 kNoValidatorType,
256 kFileSystemTypeNativeMedia); 264 kWithValidatorType);
257 helper.SetUp(); 265 helper.SetUp();
258 scoped_ptr<CopyOrMoveFileValidatorFactory> reject_factory( 266 scoped_ptr<CopyOrMoveFileValidatorFactory> reject_factory(
259 new TestCopyOrMoveFileValidatorFactory(false /*accept_all*/)); 267 new TestCopyOrMoveFileValidatorFactory(false /*accept_all*/));
260 helper.SetMediaCopyOrMoveFileValidatorFactory(reject_factory.Pass()); 268 helper.SetMediaCopyOrMoveFileValidatorFactory(reject_factory.Pass());
261 269
262 scoped_ptr<CopyOrMoveFileValidatorFactory> accept_factory( 270 scoped_ptr<CopyOrMoveFileValidatorFactory> accept_factory(
263 new TestCopyOrMoveFileValidatorFactory(true /*accept_all*/)); 271 new TestCopyOrMoveFileValidatorFactory(true /*accept_all*/));
264 helper.SetMediaCopyOrMoveFileValidatorFactory(accept_factory.Pass()); 272 helper.SetMediaCopyOrMoveFileValidatorFactory(accept_factory.Pass());
265 273
266 helper.CopyTest(base::PLATFORM_FILE_ERROR_SECURITY); 274 helper.CopyTest(base::PLATFORM_FILE_ERROR_SECURITY);
267 helper.MoveTest(base::PLATFORM_FILE_ERROR_SECURITY); 275 helper.MoveTest(base::PLATFORM_FILE_ERROR_SECURITY);
268 } 276 }
269 277
270 } // namespace fileapi 278 } // namespace fileapi
OLDNEW
« no previous file with comments | « no previous file | webkit/fileapi/file_system_context.h » ('j') | webkit/fileapi/file_system_context.cc » ('J')

Powered by Google App Engine
This is Rietveld 408576698