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

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: rebase 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
« no previous file with comments | « webkit/fileapi/copy_or_move_file_validator.h ('k') | webkit/fileapi/file_system_context.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 44 matching lines...) Expand 10 before | Expand all | Expand 10 after
195 base::PlatformFileError result_; 201 base::PlatformFileError result_;
196 202
197 DISALLOW_COPY_AND_ASSIGN(TestCopyOrMoveFileValidator); 203 DISALLOW_COPY_AND_ASSIGN(TestCopyOrMoveFileValidator);
198 }; 204 };
199 205
200 bool all_valid_; 206 bool all_valid_;
201 207
202 DISALLOW_COPY_AND_ASSIGN(TestCopyOrMoveFileValidatorFactory); 208 DISALLOW_COPY_AND_ASSIGN(TestCopyOrMoveFileValidatorFactory);
203 }; 209 };
204 210
211 } // namespace
212
205 TEST(CopyOrMoveFileValidatorTest, NoValidatorWithin6ameFSType) { 213 TEST(CopyOrMoveFileValidatorTest, NoValidatorWithin6ameFSType) {
206 // Within a file system type, validation is not expected, so it should 214 // Within a file system type, validation is not expected, so it should
207 // work for kFileSystemTypeNativeMedia without a validator set. 215 // work for kWithValidatorType without a validator set.
208 CopyOrMoveFileValidatorTestHelper helper(GURL("http://foo"), 216 CopyOrMoveFileValidatorTestHelper helper(GURL("http://foo"),
209 kFileSystemTypeNativeMedia, 217 kWithValidatorType,
210 kFileSystemTypeNativeMedia); 218 kWithValidatorType);
211 helper.SetUp(); 219 helper.SetUp();
212 helper.CopyTest(base::PLATFORM_FILE_OK); 220 helper.CopyTest(base::PLATFORM_FILE_OK);
213 helper.MoveTest(base::PLATFORM_FILE_OK); 221 helper.MoveTest(base::PLATFORM_FILE_OK);
214 } 222 }
215 223
216 TEST(CopyOrMoveFileValidatorTest, MissingValidator) { 224 TEST(CopyOrMoveFileValidatorTest, MissingValidator) {
217 // Copying or moving into a kFileSystemTypeNativeMedia requires a file 225 // Copying or moving into a kWithValidatorType requires a file
218 // validator. An error is expect if copy is attempted without a validator. 226 // validator. An error is expect if copy is attempted without a validator.
219 CopyOrMoveFileValidatorTestHelper helper(GURL("http://foo"), 227 CopyOrMoveFileValidatorTestHelper helper(GURL("http://foo"),
220 kFileSystemTypeTemporary, 228 kNoValidatorType,
221 kFileSystemTypeNativeMedia); 229 kWithValidatorType);
222 helper.SetUp(); 230 helper.SetUp();
223 helper.CopyTest(base::PLATFORM_FILE_ERROR_SECURITY); 231 helper.CopyTest(base::PLATFORM_FILE_ERROR_SECURITY);
224 helper.MoveTest(base::PLATFORM_FILE_ERROR_SECURITY); 232 helper.MoveTest(base::PLATFORM_FILE_ERROR_SECURITY);
225 } 233 }
226 234
227 TEST(CopyOrMoveFileValidatorTest, AcceptAll) { 235 TEST(CopyOrMoveFileValidatorTest, AcceptAll) {
228 CopyOrMoveFileValidatorTestHelper helper(GURL("http://foo"), 236 CopyOrMoveFileValidatorTestHelper helper(GURL("http://foo"),
229 kFileSystemTypeTemporary, 237 kNoValidatorType,
230 kFileSystemTypeNativeMedia); 238 kWithValidatorType);
231 helper.SetUp(); 239 helper.SetUp();
232 scoped_ptr<CopyOrMoveFileValidatorFactory> factory( 240 scoped_ptr<CopyOrMoveFileValidatorFactory> factory(
233 new TestCopyOrMoveFileValidatorFactory(true /*accept_all*/)); 241 new TestCopyOrMoveFileValidatorFactory(true /*accept_all*/));
234 helper.SetMediaCopyOrMoveFileValidatorFactory(factory.Pass()); 242 helper.SetMediaCopyOrMoveFileValidatorFactory(factory.Pass());
235 243
236 helper.CopyTest(base::PLATFORM_FILE_OK); 244 helper.CopyTest(base::PLATFORM_FILE_OK);
237 helper.MoveTest(base::PLATFORM_FILE_OK); 245 helper.MoveTest(base::PLATFORM_FILE_OK);
238 } 246 }
239 247
240 TEST(CopyOrMoveFileValidatorTest, AcceptNone) { 248 TEST(CopyOrMoveFileValidatorTest, AcceptNone) {
241 CopyOrMoveFileValidatorTestHelper helper(GURL("http://foo"), 249 CopyOrMoveFileValidatorTestHelper helper(GURL("http://foo"),
242 kFileSystemTypeTemporary, 250 kNoValidatorType,
243 kFileSystemTypeNativeMedia); 251 kWithValidatorType);
244 helper.SetUp(); 252 helper.SetUp();
245 scoped_ptr<CopyOrMoveFileValidatorFactory> factory( 253 scoped_ptr<CopyOrMoveFileValidatorFactory> factory(
246 new TestCopyOrMoveFileValidatorFactory(false /*accept_all*/)); 254 new TestCopyOrMoveFileValidatorFactory(false /*accept_all*/));
247 helper.SetMediaCopyOrMoveFileValidatorFactory(factory.Pass()); 255 helper.SetMediaCopyOrMoveFileValidatorFactory(factory.Pass());
248 256
249 helper.CopyTest(base::PLATFORM_FILE_ERROR_SECURITY); 257 helper.CopyTest(base::PLATFORM_FILE_ERROR_SECURITY);
250 helper.MoveTest(base::PLATFORM_FILE_ERROR_SECURITY); 258 helper.MoveTest(base::PLATFORM_FILE_ERROR_SECURITY);
251 } 259 }
252 260
253 TEST(CopyOrMoveFileValidatorTest, OverrideValidator) { 261 TEST(CopyOrMoveFileValidatorTest, OverrideValidator) {
254 // Once set, you can not override the validator. 262 // Once set, you can not override the validator.
255 CopyOrMoveFileValidatorTestHelper helper(GURL("http://foo"), 263 CopyOrMoveFileValidatorTestHelper helper(GURL("http://foo"),
256 kFileSystemTypeTemporary, 264 kNoValidatorType,
257 kFileSystemTypeNativeMedia); 265 kWithValidatorType);
258 helper.SetUp(); 266 helper.SetUp();
259 scoped_ptr<CopyOrMoveFileValidatorFactory> reject_factory( 267 scoped_ptr<CopyOrMoveFileValidatorFactory> reject_factory(
260 new TestCopyOrMoveFileValidatorFactory(false /*accept_all*/)); 268 new TestCopyOrMoveFileValidatorFactory(false /*accept_all*/));
261 helper.SetMediaCopyOrMoveFileValidatorFactory(reject_factory.Pass()); 269 helper.SetMediaCopyOrMoveFileValidatorFactory(reject_factory.Pass());
262 270
263 scoped_ptr<CopyOrMoveFileValidatorFactory> accept_factory( 271 scoped_ptr<CopyOrMoveFileValidatorFactory> accept_factory(
264 new TestCopyOrMoveFileValidatorFactory(true /*accept_all*/)); 272 new TestCopyOrMoveFileValidatorFactory(true /*accept_all*/));
265 helper.SetMediaCopyOrMoveFileValidatorFactory(accept_factory.Pass()); 273 helper.SetMediaCopyOrMoveFileValidatorFactory(accept_factory.Pass());
266 274
267 helper.CopyTest(base::PLATFORM_FILE_ERROR_SECURITY); 275 helper.CopyTest(base::PLATFORM_FILE_ERROR_SECURITY);
268 helper.MoveTest(base::PLATFORM_FILE_ERROR_SECURITY); 276 helper.MoveTest(base::PLATFORM_FILE_ERROR_SECURITY);
269 } 277 }
270 278
271 } // namespace fileapi 279 } // namespace fileapi
OLDNEW
« no previous file with comments | « webkit/fileapi/copy_or_move_file_validator.h ('k') | webkit/fileapi/file_system_context.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698