OLD | NEW |
(Empty) | |
| 1 // Copyright 2014 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 <string> |
| 6 #include <vector> |
| 7 |
| 8 #include "base/files/file.h" |
| 9 #include "base/files/file_path.h" |
| 10 #include "base/files/scoped_temp_dir.h" |
| 11 #include "base/memory/scoped_ptr.h" |
| 12 #include "base/memory/weak_ptr.h" |
| 13 #include "base/platform_file.h" |
| 14 #include "chrome/browser/chromeos/file_system_provider/fileapi/provider_async_fi
le_util.h" |
| 15 #include "chrome/browser/chromeos/file_system_provider/mount_path_util.h" |
| 16 #include "chrome/test/base/testing_profile.h" |
| 17 #include "content/public/test/test_browser_thread_bundle.h" |
| 18 #include "content/public/test/test_file_system_context.h" |
| 19 #include "testing/gtest/include/gtest/gtest.h" |
| 20 #include "webkit/browser/fileapi/async_file_util.h" |
| 21 #include "webkit/browser/fileapi/external_mount_points.h" |
| 22 #include "webkit/browser/fileapi/file_system_context.h" |
| 23 #include "webkit/browser/fileapi/file_system_url.h" |
| 24 #include "webkit/common/blob/shareable_file_reference.h" |
| 25 |
| 26 namespace chromeos { |
| 27 namespace file_system_provider { |
| 28 namespace { |
| 29 |
| 30 const char kExtensionId[] = "mbflcebpggnecokmikipoihdbecnjfoj"; |
| 31 const int kFileSystemId = 1; |
| 32 |
| 33 // Logs callbacks invocations on the tested operations. |
| 34 // TODO(mtomasz): Store and verify more arguments, once the operations return |
| 35 // anything else than just an error. |
| 36 class EventLogger { |
| 37 public: |
| 38 EventLogger() : weak_ptr_factory_(this) {} |
| 39 virtual ~EventLogger() {} |
| 40 |
| 41 void OnStatus(base::File::Error error) { |
| 42 error_.reset(new base::File::Error(error)); |
| 43 } |
| 44 |
| 45 void OnCreateOrOpen(base::File::Error error, |
| 46 base::PassPlatformFile platform_file, |
| 47 const base::Closure& on_close_callback) { |
| 48 error_.reset(new base::File::Error(error)); |
| 49 } |
| 50 |
| 51 void OnEnsureFileExists(base::File::Error error, bool created) { |
| 52 error_.reset(new base::File::Error(error)); |
| 53 } |
| 54 |
| 55 void OnGetFileInfo(base::File::Error error, |
| 56 const base::File::Info& file_info) { |
| 57 error_.reset(new base::File::Error(error)); |
| 58 } |
| 59 |
| 60 void OnReadDirectory(base::File::Error error, |
| 61 const fileapi::AsyncFileUtil::EntryList& file_list, |
| 62 bool has_more) { |
| 63 error_.reset(new base::File::Error(error)); |
| 64 } |
| 65 |
| 66 void OnCreateSnapshotFile( |
| 67 base::File::Error error, |
| 68 const base::File::Info& file_info, |
| 69 const base::FilePath& platform_path, |
| 70 const scoped_refptr<webkit_blob::ShareableFileReference>& file_ref) { |
| 71 error_.reset(new base::File::Error(error)); |
| 72 } |
| 73 |
| 74 void OnCopyFileProgress(int64 size) {} |
| 75 |
| 76 base::WeakPtr<EventLogger> GetWeakPtr() { |
| 77 return weak_ptr_factory_.GetWeakPtr(); |
| 78 } |
| 79 |
| 80 base::File::Error* error() { return error_.get(); } |
| 81 |
| 82 private: |
| 83 scoped_ptr<base::File::Error> error_; |
| 84 base::WeakPtrFactory<EventLogger> weak_ptr_factory_; |
| 85 |
| 86 DISALLOW_COPY_AND_ASSIGN(EventLogger); |
| 87 }; |
| 88 |
| 89 // Registers an external mount point, and removes it once the object gets out |
| 90 // of scope. To ensure that creating the mount point succeeded, call is_valid(). |
| 91 class ScopedExternalMountPoint { |
| 92 public: |
| 93 ScopedExternalMountPoint(const std::string& mount_point_name, |
| 94 const base::FilePath& mount_path, |
| 95 fileapi::FileSystemType type) |
| 96 : mount_point_name_(mount_point_name) { |
| 97 fileapi::ExternalMountPoints* const mount_points = |
| 98 fileapi::ExternalMountPoints::GetSystemInstance(); |
| 99 DCHECK(mount_points); |
| 100 is_valid_ = |
| 101 mount_points->RegisterFileSystem(mount_point_name, |
| 102 fileapi::kFileSystemTypeProvided, |
| 103 fileapi::FileSystemMountOption(), |
| 104 mount_path); |
| 105 } |
| 106 |
| 107 virtual ~ScopedExternalMountPoint() { |
| 108 if (!is_valid_) |
| 109 return; |
| 110 |
| 111 // If successfully registered in the constructor, then unregister. |
| 112 fileapi::ExternalMountPoints* const mount_points = |
| 113 fileapi::ExternalMountPoints::GetSystemInstance(); |
| 114 DCHECK(mount_points); |
| 115 mount_points->RevokeFileSystem(mount_point_name_); |
| 116 } |
| 117 |
| 118 bool is_valid() { return is_valid_; } |
| 119 |
| 120 private: |
| 121 const std::string mount_point_name_; |
| 122 bool is_valid_; |
| 123 }; |
| 124 |
| 125 // Creates a cracked FileSystemURL for tests. |
| 126 fileapi::FileSystemURL CreateFileSystemURL(const std::string& mount_point_name, |
| 127 const base::FilePath& file_path) { |
| 128 const std::string origin = std::string("chrome-extension://") + kExtensionId; |
| 129 const fileapi::ExternalMountPoints* const mount_points = |
| 130 fileapi::ExternalMountPoints::GetSystemInstance(); |
| 131 return mount_points->CreateCrackedFileSystemURL( |
| 132 GURL(origin), |
| 133 fileapi::kFileSystemTypeExternal, |
| 134 base::FilePath::FromUTF8Unsafe(mount_point_name).Append(file_path)); |
| 135 } |
| 136 |
| 137 } // namespace |
| 138 |
| 139 // Tests in this file are very lightweight and just test integration between |
| 140 // AsyncFileUtil and ProvideFileSystemInterface. Currently it tests if not |
| 141 // implemented operations return a correct error code. For not allowed |
| 142 // operations it is FILE_ERROR_SECURITY, and for not implemented the error is |
| 143 // FILE_ERROR_NOT_FOUND. |
| 144 class FileSystemProviderProviderAsyncFileUtilTest : public testing::Test { |
| 145 protected: |
| 146 FileSystemProviderProviderAsyncFileUtilTest() {} |
| 147 virtual ~FileSystemProviderProviderAsyncFileUtilTest() {} |
| 148 |
| 149 virtual void SetUp() OVERRIDE { |
| 150 ASSERT_TRUE(data_dir_.CreateUniqueTempDir()); |
| 151 profile_.reset(new TestingProfile); |
| 152 async_file_util_.reset(new internal::ProviderAsyncFileUtil); |
| 153 const base::FilePath mount_path = |
| 154 util::GetMountPointPath(profile_.get(), kExtensionId, kFileSystemId); |
| 155 file_system_context_ = |
| 156 content::CreateFileSystemContextForTesting(NULL, data_dir_.path()); |
| 157 |
| 158 const std::string mount_point_name = mount_path.BaseName().AsUTF8Unsafe(); |
| 159 mount_point_.reset(new ScopedExternalMountPoint( |
| 160 mount_point_name, mount_path, fileapi::kFileSystemTypeProvided)); |
| 161 ASSERT_TRUE(mount_point_->is_valid()); |
| 162 |
| 163 file_url_ = CreateFileSystemURL( |
| 164 mount_point_name, base::FilePath::FromUTF8Unsafe("hello/world.txt")); |
| 165 ASSERT_TRUE(file_url_.is_valid()); |
| 166 directory_url_ = CreateFileSystemURL( |
| 167 mount_point_name, base::FilePath::FromUTF8Unsafe("hello")); |
| 168 ASSERT_TRUE(directory_url_.is_valid()); |
| 169 root_url_ = CreateFileSystemURL(mount_point_name, base::FilePath()); |
| 170 ASSERT_TRUE(root_url_.is_valid()); |
| 171 } |
| 172 |
| 173 scoped_ptr<fileapi::FileSystemOperationContext> CreateOperationContext() { |
| 174 return make_scoped_ptr( |
| 175 new fileapi::FileSystemOperationContext(file_system_context_.get())); |
| 176 } |
| 177 |
| 178 content::TestBrowserThreadBundle thread_bundle_; |
| 179 base::ScopedTempDir data_dir_; |
| 180 scoped_ptr<TestingProfile> profile_; |
| 181 scoped_ptr<fileapi::AsyncFileUtil> async_file_util_; |
| 182 scoped_refptr<fileapi::FileSystemContext> file_system_context_; |
| 183 scoped_ptr<ScopedExternalMountPoint> mount_point_; |
| 184 fileapi::FileSystemURL file_url_; |
| 185 fileapi::FileSystemURL directory_url_; |
| 186 fileapi::FileSystemURL root_url_; |
| 187 }; |
| 188 |
| 189 TEST_F(FileSystemProviderProviderAsyncFileUtilTest, CreateOrOpen_Create) { |
| 190 EventLogger logger; |
| 191 |
| 192 async_file_util_->CreateOrOpen( |
| 193 CreateOperationContext(), |
| 194 file_url_, |
| 195 base::PLATFORM_FILE_CREATE, |
| 196 base::Bind(&EventLogger::OnCreateOrOpen, logger.GetWeakPtr())); |
| 197 |
| 198 ASSERT_TRUE(logger.error()); |
| 199 EXPECT_EQ(base::File::FILE_ERROR_SECURITY, *logger.error()); |
| 200 } |
| 201 |
| 202 TEST_F(FileSystemProviderProviderAsyncFileUtilTest, CreateOrOpen_CreateAlways) { |
| 203 EventLogger logger; |
| 204 |
| 205 async_file_util_->CreateOrOpen( |
| 206 CreateOperationContext(), |
| 207 file_url_, |
| 208 base::PLATFORM_FILE_CREATE_ALWAYS, |
| 209 base::Bind(&EventLogger::OnCreateOrOpen, logger.GetWeakPtr())); |
| 210 |
| 211 ASSERT_TRUE(logger.error()); |
| 212 EXPECT_EQ(base::File::FILE_ERROR_SECURITY, *logger.error()); |
| 213 } |
| 214 |
| 215 TEST_F(FileSystemProviderProviderAsyncFileUtilTest, CreateOrOpen_OpenAlways) { |
| 216 EventLogger logger; |
| 217 |
| 218 async_file_util_->CreateOrOpen( |
| 219 CreateOperationContext(), |
| 220 file_url_, |
| 221 base::PLATFORM_FILE_OPEN_ALWAYS, |
| 222 base::Bind(&EventLogger::OnCreateOrOpen, logger.GetWeakPtr())); |
| 223 |
| 224 ASSERT_TRUE(logger.error()); |
| 225 EXPECT_EQ(base::File::FILE_ERROR_SECURITY, *logger.error()); |
| 226 } |
| 227 |
| 228 TEST_F(FileSystemProviderProviderAsyncFileUtilTest, |
| 229 CreateOrOpen_OpenTruncated) { |
| 230 EventLogger logger; |
| 231 |
| 232 async_file_util_->CreateOrOpen( |
| 233 CreateOperationContext(), |
| 234 file_url_, |
| 235 base::PLATFORM_FILE_OPEN_TRUNCATED, |
| 236 base::Bind(&EventLogger::OnCreateOrOpen, logger.GetWeakPtr())); |
| 237 |
| 238 ASSERT_TRUE(logger.error()); |
| 239 EXPECT_EQ(base::File::FILE_ERROR_SECURITY, *logger.error()); |
| 240 } |
| 241 |
| 242 TEST_F(FileSystemProviderProviderAsyncFileUtilTest, CreateOrOpen_Open) { |
| 243 EventLogger logger; |
| 244 |
| 245 async_file_util_->CreateOrOpen( |
| 246 CreateOperationContext(), |
| 247 file_url_, |
| 248 base::PLATFORM_FILE_OPEN, |
| 249 base::Bind(&EventLogger::OnCreateOrOpen, logger.GetWeakPtr())); |
| 250 |
| 251 ASSERT_TRUE(logger.error()); |
| 252 EXPECT_EQ(base::File::FILE_ERROR_NOT_FOUND, *logger.error()); |
| 253 } |
| 254 |
| 255 TEST_F(FileSystemProviderProviderAsyncFileUtilTest, EnsureFileExists) { |
| 256 EventLogger logger; |
| 257 |
| 258 async_file_util_->EnsureFileExists( |
| 259 CreateOperationContext(), |
| 260 file_url_, |
| 261 base::Bind(&EventLogger::OnEnsureFileExists, logger.GetWeakPtr())); |
| 262 |
| 263 ASSERT_TRUE(logger.error()); |
| 264 EXPECT_EQ(base::File::FILE_ERROR_SECURITY, *logger.error()); |
| 265 } |
| 266 |
| 267 TEST_F(FileSystemProviderProviderAsyncFileUtilTest, CreateDirectory) { |
| 268 EventLogger logger; |
| 269 |
| 270 async_file_util_->CreateDirectory( |
| 271 CreateOperationContext(), |
| 272 directory_url_, |
| 273 false, // exclusive |
| 274 false, // recursive |
| 275 base::Bind(&EventLogger::OnStatus, logger.GetWeakPtr())); |
| 276 |
| 277 ASSERT_TRUE(logger.error()); |
| 278 EXPECT_EQ(base::File::FILE_ERROR_SECURITY, *logger.error()); |
| 279 } |
| 280 |
| 281 TEST_F(FileSystemProviderProviderAsyncFileUtilTest, GetFileInfo) { |
| 282 EventLogger logger; |
| 283 |
| 284 async_file_util_->GetFileInfo( |
| 285 CreateOperationContext(), |
| 286 file_url_, |
| 287 base::Bind(&EventLogger::OnGetFileInfo, logger.GetWeakPtr())); |
| 288 |
| 289 ASSERT_TRUE(logger.error()); |
| 290 EXPECT_EQ(base::File::FILE_ERROR_NOT_FOUND, *logger.error()); |
| 291 } |
| 292 |
| 293 TEST_F(FileSystemProviderProviderAsyncFileUtilTest, ReadDirectory) { |
| 294 EventLogger logger; |
| 295 |
| 296 async_file_util_->ReadDirectory( |
| 297 CreateOperationContext(), |
| 298 root_url_, |
| 299 base::Bind(&EventLogger::OnReadDirectory, logger.GetWeakPtr())); |
| 300 |
| 301 ASSERT_TRUE(logger.error()); |
| 302 EXPECT_EQ(base::File::FILE_ERROR_NOT_FOUND, *logger.error()); |
| 303 } |
| 304 |
| 305 TEST_F(FileSystemProviderProviderAsyncFileUtilTest, Touch) { |
| 306 EventLogger logger; |
| 307 |
| 308 async_file_util_->CreateDirectory( |
| 309 CreateOperationContext(), |
| 310 file_url_, |
| 311 false, // exclusive |
| 312 false, // recursive |
| 313 base::Bind(&EventLogger::OnStatus, logger.GetWeakPtr())); |
| 314 |
| 315 ASSERT_TRUE(logger.error()); |
| 316 EXPECT_EQ(base::File::FILE_ERROR_SECURITY, *logger.error()); |
| 317 } |
| 318 |
| 319 TEST_F(FileSystemProviderProviderAsyncFileUtilTest, Truncate) { |
| 320 EventLogger logger; |
| 321 |
| 322 async_file_util_->Truncate( |
| 323 CreateOperationContext(), |
| 324 file_url_, |
| 325 0, // length |
| 326 base::Bind(&EventLogger::OnStatus, logger.GetWeakPtr())); |
| 327 |
| 328 ASSERT_TRUE(logger.error()); |
| 329 EXPECT_EQ(base::File::FILE_ERROR_SECURITY, *logger.error()); |
| 330 } |
| 331 |
| 332 TEST_F(FileSystemProviderProviderAsyncFileUtilTest, CopyFileLocal) { |
| 333 EventLogger logger; |
| 334 |
| 335 async_file_util_->CopyFileLocal( |
| 336 CreateOperationContext(), |
| 337 file_url_, // src_url |
| 338 file_url_, // dst_url |
| 339 fileapi::FileSystemOperation::OPTION_NONE, |
| 340 base::Bind(&EventLogger::OnCopyFileProgress, logger.GetWeakPtr()), |
| 341 base::Bind(&EventLogger::OnStatus, logger.GetWeakPtr())); |
| 342 |
| 343 ASSERT_TRUE(logger.error()); |
| 344 EXPECT_EQ(base::File::FILE_ERROR_SECURITY, *logger.error()); |
| 345 } |
| 346 |
| 347 TEST_F(FileSystemProviderProviderAsyncFileUtilTest, MoveFileLocal) { |
| 348 EventLogger logger; |
| 349 |
| 350 async_file_util_->MoveFileLocal( |
| 351 CreateOperationContext(), |
| 352 file_url_, // src_url |
| 353 file_url_, // dst_url |
| 354 fileapi::FileSystemOperation::OPTION_NONE, |
| 355 base::Bind(&EventLogger::OnStatus, logger.GetWeakPtr())); |
| 356 |
| 357 ASSERT_TRUE(logger.error()); |
| 358 EXPECT_EQ(base::File::FILE_ERROR_SECURITY, *logger.error()); |
| 359 } |
| 360 |
| 361 TEST_F(FileSystemProviderProviderAsyncFileUtilTest, CopyInForeignFile) { |
| 362 EventLogger logger; |
| 363 |
| 364 async_file_util_->CopyInForeignFile( |
| 365 CreateOperationContext(), |
| 366 base::FilePath(), // src_file_path |
| 367 file_url_, // dst_url |
| 368 base::Bind(&EventLogger::OnStatus, logger.GetWeakPtr())); |
| 369 |
| 370 ASSERT_TRUE(logger.error()); |
| 371 EXPECT_EQ(base::File::FILE_ERROR_SECURITY, *logger.error()); |
| 372 } |
| 373 |
| 374 TEST_F(FileSystemProviderProviderAsyncFileUtilTest, DeleteFile) { |
| 375 EventLogger logger; |
| 376 |
| 377 async_file_util_->DeleteFile( |
| 378 CreateOperationContext(), |
| 379 file_url_, |
| 380 base::Bind(&EventLogger::OnStatus, logger.GetWeakPtr())); |
| 381 |
| 382 ASSERT_TRUE(logger.error()); |
| 383 EXPECT_EQ(base::File::FILE_ERROR_SECURITY, *logger.error()); |
| 384 } |
| 385 |
| 386 TEST_F(FileSystemProviderProviderAsyncFileUtilTest, DeleteDirectory) { |
| 387 EventLogger logger; |
| 388 |
| 389 async_file_util_->DeleteDirectory( |
| 390 CreateOperationContext(), |
| 391 directory_url_, |
| 392 base::Bind(&EventLogger::OnStatus, logger.GetWeakPtr())); |
| 393 |
| 394 ASSERT_TRUE(logger.error()); |
| 395 EXPECT_EQ(base::File::FILE_ERROR_SECURITY, *logger.error()); |
| 396 } |
| 397 |
| 398 TEST_F(FileSystemProviderProviderAsyncFileUtilTest, DeleteRecursively) { |
| 399 EventLogger logger; |
| 400 |
| 401 async_file_util_->DeleteRecursively( |
| 402 CreateOperationContext(), |
| 403 directory_url_, |
| 404 base::Bind(&EventLogger::OnStatus, logger.GetWeakPtr())); |
| 405 |
| 406 ASSERT_TRUE(logger.error()); |
| 407 EXPECT_EQ(base::File::FILE_ERROR_SECURITY, *logger.error()); |
| 408 } |
| 409 |
| 410 TEST_F(FileSystemProviderProviderAsyncFileUtilTest, CreateSnapshotFile) { |
| 411 EventLogger logger; |
| 412 |
| 413 async_file_util_->CreateSnapshotFile( |
| 414 CreateOperationContext(), |
| 415 file_url_, |
| 416 base::Bind(&EventLogger::OnCreateSnapshotFile, logger.GetWeakPtr())); |
| 417 |
| 418 ASSERT_TRUE(logger.error()); |
| 419 EXPECT_EQ(base::File::FILE_ERROR_NOT_FOUND, *logger.error()); |
| 420 } |
| 421 |
| 422 } // namespace file_system_provider |
| 423 } // namespace chromeos |
OLD | NEW |