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. 4 5 #include "base/basictypes.h" 6 #include "base/file_util.h" 7 #include "base/files/file_path.h" 8 #include "base/files/scoped_temp_dir.h" 9 #include "base/memory/scoped_ptr.h" 10 #include "base/platform_file.h" 11 #include "base/run_loop.h" 12 #include "content/public/test/test_file_system_context.h" 13 #include "testing/gtest/include/gtest/gtest.h" 14 #include "webkit/browser/fileapi/file_system_context.h" 15 #include "webkit/browser/fileapi/file_system_operation_context.h" 16 #include "webkit/browser/fileapi/isolated_context.h" 17 #include "webkit/browser/fileapi/transient_file_util.h" 18 #include "webkit/common/blob/scoped_file.h" 19 20 namespace fileapi { 21 22 class TransientFileUtilTest : public testing::Test { 23 public: 24 TransientFileUtilTest() {} 25 virtual ~TransientFileUtilTest() {} 26 27 virtual void SetUp() OVERRIDE { 28 file_system_context_ = CreateFileSystemContextForTesting( 29 NULL, base::FilePath(FILE_PATH_LITERAL("dummy"))); 30 transient_file_util_.reset(new TransientFileUtil); 31 32 ASSERT_TRUE(data_dir_.CreateUniqueTempDir()); 33 } 34 35 virtual void TearDown() OVERRIDE { 36 file_system_context_ = NULL; 37 base::RunLoop().RunUntilIdle(); 38 } 39 40 void CreateAndRegisterTemporaryFile( 41 FileSystemURL* file_url, 42 base::FilePath* file_path) { 43 EXPECT_TRUE(base::CreateTemporaryFileInDir(data_dir_.path(), file_path)); 44 IsolatedContext* isolated_context = IsolatedContext::GetInstance(); 45 std::string name = "tmp"; 46 std::string fsid = isolated_context->RegisterFileSystemForPath( 47 kFileSystemTypeForTransientFile, 48 *file_path, 49 &name); 50 ASSERT_TRUE(!fsid.empty()); 51 base::FilePath virtual_path = isolated_context->CreateVirtualRootPath( 52 fsid).AppendASCII(name); 53 *file_url = file_system_context_->CreateCrackedFileSystemURL( 54 GURL("http://foo"), 55 kFileSystemTypeIsolated, 56 virtual_path); 57 } 58 59 scoped_ptr<FileSystemOperationContext> NewOperationContext() { 60 return make_scoped_ptr( 61 new FileSystemOperationContext(file_system_context_.get())); 62 } 63 64 FileSystemFileUtil* file_util() { return transient_file_util_.get(); } 65 66 private: 67 base::MessageLoop message_loop_; 68 base::ScopedTempDir data_dir_; 69 scoped_refptr<FileSystemContext> file_system_context_; 70 scoped_ptr<TransientFileUtil> transient_file_util_; 71 72 DISALLOW_COPY_AND_ASSIGN(TransientFileUtilTest); 73 }; 74 75 TEST_F(TransientFileUtilTest, TransientFile) { 76 FileSystemURL temp_url; 77 base::FilePath temp_path; 78 79 CreateAndRegisterTemporaryFile(&temp_url, &temp_path); 80 81 base::PlatformFileError error; 82 base::PlatformFileInfo file_info; 83 base::FilePath path; 84 85 // Make sure the file is there. 86 ASSERT_TRUE(temp_url.is_valid()); 87 ASSERT_TRUE(base::PathExists(temp_path)); 88 ASSERT_FALSE(base::DirectoryExists(temp_path)); 89 90 // Create a snapshot file. 91 { 92 webkit_blob::ScopedFile scoped_file = 93 file_util()->CreateSnapshotFile(NewOperationContext().get(), 94 temp_url, 95 &error, 96 &file_info, 97 &path); 98 ASSERT_EQ(base::PLATFORM_FILE_OK, error); 99 ASSERT_EQ(temp_path, path); 100 ASSERT_FALSE(file_info.is_directory); 101 102 // The file should be still there. 103 ASSERT_TRUE(base::PathExists(temp_path)); 104 ASSERT_EQ(base::PLATFORM_FILE_OK, 105 file_util()->GetFileInfo(NewOperationContext().get(), 106 temp_url, &file_info, &path)); 107 ASSERT_EQ(temp_path, path); 108 ASSERT_FALSE(file_info.is_directory); 109 } 110 111 // The file's now scoped out. 112 base::RunLoop().RunUntilIdle(); 113 114 // Now the temporary file and the transient filesystem must be gone too. 115 ASSERT_FALSE(base::PathExists(temp_path)); 116 ASSERT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND, 117 file_util()->GetFileInfo(NewOperationContext().get(), 118 temp_url, &file_info, &path)); 119 } 120 121 } // namespace fileapi 122