transient_file_util_unittest.cc 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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. #include <memory>
  5. #include <string>
  6. #include "base/files/file_path.h"
  7. #include "base/files/file_util.h"
  8. #include "base/files/scoped_temp_dir.h"
  9. #include "base/run_loop.h"
  10. #include "base/test/task_environment.h"
  11. #include "storage/browser/blob/scoped_file.h"
  12. #include "storage/browser/file_system/file_system_context.h"
  13. #include "storage/browser/file_system/file_system_operation_context.h"
  14. #include "storage/browser/file_system/isolated_context.h"
  15. #include "storage/browser/file_system/transient_file_util.h"
  16. #include "storage/browser/quota/quota_manager_proxy.h"
  17. #include "storage/browser/test/test_file_system_context.h"
  18. #include "testing/gtest/include/gtest/gtest.h"
  19. #include "third_party/blink/public/common/storage_key/storage_key.h"
  20. #include "url/gurl.h"
  21. namespace storage {
  22. class TransientFileUtilTest : public testing::Test {
  23. public:
  24. TransientFileUtilTest() = default;
  25. TransientFileUtilTest(const TransientFileUtilTest&) = delete;
  26. TransientFileUtilTest& operator=(const TransientFileUtilTest&) = delete;
  27. ~TransientFileUtilTest() override = default;
  28. void SetUp() override {
  29. file_system_context_ = CreateFileSystemContextForTesting(
  30. /*quota_manager_proxy=*/nullptr,
  31. base::FilePath(FILE_PATH_LITERAL("dummy")));
  32. transient_file_util_ = std::make_unique<TransientFileUtil>();
  33. ASSERT_TRUE(data_dir_.CreateUniqueTempDir());
  34. }
  35. void TearDown() override {
  36. file_system_context_ = nullptr;
  37. base::RunLoop().RunUntilIdle();
  38. }
  39. void CreateAndRegisterTemporaryFile(
  40. FileSystemURL* file_url,
  41. base::FilePath* file_path,
  42. IsolatedContext::ScopedFSHandle* filesystem) {
  43. EXPECT_TRUE(base::CreateTemporaryFileInDir(data_dir_.GetPath(), file_path));
  44. IsolatedContext* isolated_context = IsolatedContext::GetInstance();
  45. std::string name = "tmp";
  46. *filesystem = isolated_context->RegisterFileSystemForPath(
  47. kFileSystemTypeForTransientFile, std::string(), *file_path, &name);
  48. ASSERT_TRUE(filesystem->is_valid());
  49. base::FilePath virtual_path =
  50. isolated_context->CreateVirtualRootPath(filesystem->id())
  51. .AppendASCII(name);
  52. *file_url = file_system_context_->CreateCrackedFileSystemURL(
  53. blink::StorageKey::CreateFromStringForTesting("http://foo"),
  54. kFileSystemTypeIsolated, virtual_path);
  55. }
  56. std::unique_ptr<FileSystemOperationContext> NewOperationContext() {
  57. return std::make_unique<FileSystemOperationContext>(
  58. file_system_context_.get());
  59. }
  60. FileSystemFileUtil* file_util() { return transient_file_util_.get(); }
  61. private:
  62. base::test::TaskEnvironment task_environment_;
  63. base::ScopedTempDir data_dir_;
  64. scoped_refptr<FileSystemContext> file_system_context_;
  65. std::unique_ptr<TransientFileUtil> transient_file_util_;
  66. };
  67. TEST_F(TransientFileUtilTest, TransientFile) {
  68. FileSystemURL temp_url;
  69. base::FilePath temp_path;
  70. IsolatedContext::ScopedFSHandle filesystem;
  71. CreateAndRegisterTemporaryFile(&temp_url, &temp_path, &filesystem);
  72. base::File::Error error;
  73. base::File::Info file_info;
  74. base::FilePath path;
  75. // Make sure the file is there.
  76. ASSERT_TRUE(temp_url.is_valid());
  77. ASSERT_TRUE(base::PathExists(temp_path));
  78. ASSERT_FALSE(base::DirectoryExists(temp_path));
  79. // Create a snapshot file.
  80. {
  81. ScopedFile scoped_file = file_util()->CreateSnapshotFile(
  82. NewOperationContext().get(), temp_url, &error, &file_info, &path);
  83. ASSERT_EQ(base::File::FILE_OK, error);
  84. ASSERT_EQ(temp_path, path);
  85. ASSERT_FALSE(file_info.is_directory);
  86. // The file should be still there.
  87. ASSERT_TRUE(base::PathExists(temp_path));
  88. ASSERT_EQ(base::File::FILE_OK,
  89. file_util()->GetFileInfo(NewOperationContext().get(), temp_url,
  90. &file_info, &path));
  91. ASSERT_EQ(temp_path, path);
  92. ASSERT_FALSE(file_info.is_directory);
  93. }
  94. // The file's now scoped out.
  95. base::RunLoop().RunUntilIdle();
  96. // Now the temporary file and the transient filesystem must be gone too.
  97. ASSERT_FALSE(base::PathExists(temp_path));
  98. ASSERT_EQ(base::File::FILE_ERROR_NOT_FOUND,
  99. file_util()->GetFileInfo(NewOperationContext().get(), temp_url,
  100. &file_info, &path));
  101. }
  102. } // namespace storage