PageRenderTime 792ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 1ms

/content/browser/fileapi/local_file_util_unittest.cc

https://gitlab.com/0072016/Facebook-SDK-
C++ | 381 lines | 304 code | 70 blank | 7 comment | 0 complexity | 7cc176cf67badbff8530e21cd4668b6e MD5 | raw file
  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 <stdint.h>
  5. #include <string>
  6. #include "base/files/file.h"
  7. #include "base/files/file_path.h"
  8. #include "base/files/file_util.h"
  9. #include "base/files/scoped_temp_dir.h"
  10. #include "base/macros.h"
  11. #include "base/memory/scoped_ptr.h"
  12. #include "base/run_loop.h"
  13. #include "base/strings/sys_string_conversions.h"
  14. #include "base/strings/utf_string_conversions.h"
  15. #include "build/build_config.h"
  16. #include "content/public/test/async_file_test_helper.h"
  17. #include "content/public/test/test_file_system_context.h"
  18. #include "storage/browser/fileapi/async_file_util_adapter.h"
  19. #include "storage/browser/fileapi/file_system_context.h"
  20. #include "storage/browser/fileapi/file_system_file_util.h"
  21. #include "storage/browser/fileapi/file_system_operation_context.h"
  22. #include "storage/browser/fileapi/local_file_util.h"
  23. #include "storage/browser/fileapi/native_file_util.h"
  24. #include "storage/common/fileapi/file_system_types.h"
  25. #include "testing/gtest/include/gtest/gtest.h"
  26. using content::AsyncFileTestHelper;
  27. using storage::AsyncFileUtilAdapter;
  28. using storage::FileSystemContext;
  29. using storage::FileSystemOperationContext;
  30. using storage::FileSystemURL;
  31. using storage::LocalFileUtil;
  32. namespace content {
  33. namespace {
  34. const GURL kOrigin("http://foo/");
  35. const storage::FileSystemType kFileSystemType = storage::kFileSystemTypeTest;
  36. } // namespace
  37. class LocalFileUtilTest : public testing::Test {
  38. public:
  39. LocalFileUtilTest() {}
  40. void SetUp() override {
  41. ASSERT_TRUE(data_dir_.CreateUniqueTempDir());
  42. file_system_context_ = CreateFileSystemContextForTesting(
  43. NULL, data_dir_.path());
  44. }
  45. void TearDown() override {
  46. file_system_context_ = NULL;
  47. base::RunLoop().RunUntilIdle();
  48. }
  49. protected:
  50. FileSystemOperationContext* NewContext() {
  51. FileSystemOperationContext* context =
  52. new FileSystemOperationContext(file_system_context_.get());
  53. context->set_update_observers(
  54. *file_system_context_->GetUpdateObservers(kFileSystemType));
  55. return context;
  56. }
  57. LocalFileUtil* file_util() {
  58. AsyncFileUtilAdapter* adapter = static_cast<AsyncFileUtilAdapter*>(
  59. file_system_context_->GetAsyncFileUtil(kFileSystemType));
  60. return static_cast<LocalFileUtil*>(adapter->sync_file_util());
  61. }
  62. FileSystemURL CreateURL(const std::string& file_name) {
  63. return file_system_context_->CreateCrackedFileSystemURL(
  64. kOrigin, kFileSystemType, base::FilePath().FromUTF8Unsafe(file_name));
  65. }
  66. base::FilePath LocalPath(const char *file_name) {
  67. base::FilePath path;
  68. scoped_ptr<FileSystemOperationContext> context(NewContext());
  69. file_util()->GetLocalFilePath(context.get(), CreateURL(file_name), &path);
  70. return path;
  71. }
  72. bool FileExists(const char *file_name) {
  73. return base::PathExists(LocalPath(file_name)) &&
  74. !base::DirectoryExists(LocalPath(file_name));
  75. }
  76. bool DirectoryExists(const char *file_name) {
  77. return base::DirectoryExists(LocalPath(file_name));
  78. }
  79. int64_t GetSize(const char* file_name) {
  80. base::File::Info info;
  81. base::GetFileInfo(LocalPath(file_name), &info);
  82. return info.size;
  83. }
  84. base::File CreateFile(const char* file_name) {
  85. int file_flags = base::File::FLAG_CREATE |
  86. base::File::FLAG_WRITE | base::File::FLAG_ASYNC;
  87. scoped_ptr<FileSystemOperationContext> context(NewContext());
  88. return file_util()->CreateOrOpen(context.get(), CreateURL(file_name),
  89. file_flags);
  90. }
  91. base::File::Error EnsureFileExists(const char* file_name,
  92. bool* created) {
  93. scoped_ptr<FileSystemOperationContext> context(NewContext());
  94. return file_util()->EnsureFileExists(context.get(),
  95. CreateURL(file_name), created);
  96. }
  97. FileSystemContext* file_system_context() {
  98. return file_system_context_.get();
  99. }
  100. private:
  101. base::MessageLoop message_loop_;
  102. scoped_refptr<FileSystemContext> file_system_context_;
  103. base::ScopedTempDir data_dir_;
  104. DISALLOW_COPY_AND_ASSIGN(LocalFileUtilTest);
  105. };
  106. TEST_F(LocalFileUtilTest, CreateAndClose) {
  107. const char *file_name = "test_file";
  108. base::File file = CreateFile(file_name);
  109. ASSERT_TRUE(file.IsValid());
  110. ASSERT_TRUE(file.created());
  111. EXPECT_TRUE(FileExists(file_name));
  112. EXPECT_EQ(0, GetSize(file_name));
  113. scoped_ptr<FileSystemOperationContext> context(NewContext());
  114. }
  115. // base::CreateSymbolicLink is only supported on POSIX.
  116. #if defined(OS_POSIX)
  117. TEST_F(LocalFileUtilTest, CreateFailForSymlink) {
  118. // Create symlink target file.
  119. const char *target_name = "symlink_target";
  120. base::File target_file = CreateFile(target_name);
  121. ASSERT_TRUE(target_file.IsValid());
  122. ASSERT_TRUE(target_file.created());
  123. base::FilePath target_path = LocalPath(target_name);
  124. // Create symlink where target must be real file.
  125. const char *symlink_name = "symlink_file";
  126. base::FilePath symlink_path = LocalPath(symlink_name);
  127. ASSERT_TRUE(base::CreateSymbolicLink(target_path, symlink_path));
  128. ASSERT_TRUE(FileExists(symlink_name));
  129. // Try to open the symlink file which should fail.
  130. scoped_ptr<FileSystemOperationContext> context(NewContext());
  131. FileSystemURL url = CreateURL(symlink_name);
  132. int file_flags = base::File::FLAG_OPEN | base::File::FLAG_READ;
  133. base::File file = file_util()->CreateOrOpen(context.get(), url, file_flags);
  134. ASSERT_FALSE(file.IsValid());
  135. EXPECT_EQ(base::File::FILE_ERROR_NOT_FOUND, file.error_details());
  136. }
  137. #endif
  138. TEST_F(LocalFileUtilTest, EnsureFileExists) {
  139. const char *file_name = "foobar";
  140. bool created;
  141. ASSERT_EQ(base::File::FILE_OK, EnsureFileExists(file_name, &created));
  142. ASSERT_TRUE(created);
  143. EXPECT_TRUE(FileExists(file_name));
  144. EXPECT_EQ(0, GetSize(file_name));
  145. ASSERT_EQ(base::File::FILE_OK, EnsureFileExists(file_name, &created));
  146. EXPECT_FALSE(created);
  147. }
  148. TEST_F(LocalFileUtilTest, TouchFile) {
  149. const char *file_name = "test_file";
  150. base::File file = CreateFile(file_name);
  151. ASSERT_TRUE(file.IsValid());
  152. ASSERT_TRUE(file.created());
  153. scoped_ptr<FileSystemOperationContext> context(NewContext());
  154. base::File::Info info;
  155. ASSERT_TRUE(base::GetFileInfo(LocalPath(file_name), &info));
  156. const base::Time new_accessed =
  157. info.last_accessed + base::TimeDelta::FromHours(10);
  158. const base::Time new_modified =
  159. info.last_modified + base::TimeDelta::FromHours(5);
  160. EXPECT_EQ(base::File::FILE_OK,
  161. file_util()->Touch(context.get(), CreateURL(file_name),
  162. new_accessed, new_modified));
  163. ASSERT_TRUE(base::GetFileInfo(LocalPath(file_name), &info));
  164. EXPECT_EQ(new_accessed, info.last_accessed);
  165. EXPECT_EQ(new_modified, info.last_modified);
  166. }
  167. TEST_F(LocalFileUtilTest, TouchDirectory) {
  168. const char *dir_name = "test_dir";
  169. scoped_ptr<FileSystemOperationContext> context(NewContext());
  170. ASSERT_EQ(base::File::FILE_OK,
  171. file_util()->CreateDirectory(context.get(),
  172. CreateURL(dir_name),
  173. false /* exclusive */,
  174. false /* recursive */));
  175. base::File::Info info;
  176. ASSERT_TRUE(base::GetFileInfo(LocalPath(dir_name), &info));
  177. const base::Time new_accessed =
  178. info.last_accessed + base::TimeDelta::FromHours(10);
  179. const base::Time new_modified =
  180. info.last_modified + base::TimeDelta::FromHours(5);
  181. EXPECT_EQ(base::File::FILE_OK,
  182. file_util()->Touch(context.get(), CreateURL(dir_name),
  183. new_accessed, new_modified));
  184. ASSERT_TRUE(base::GetFileInfo(LocalPath(dir_name), &info));
  185. EXPECT_EQ(new_accessed, info.last_accessed);
  186. EXPECT_EQ(new_modified, info.last_modified);
  187. }
  188. TEST_F(LocalFileUtilTest, Truncate) {
  189. const char *file_name = "truncated";
  190. bool created;
  191. ASSERT_EQ(base::File::FILE_OK, EnsureFileExists(file_name, &created));
  192. ASSERT_TRUE(created);
  193. scoped_ptr<FileSystemOperationContext> context;
  194. context.reset(NewContext());
  195. ASSERT_EQ(base::File::FILE_OK,
  196. file_util()->Truncate(context.get(), CreateURL(file_name), 1020));
  197. EXPECT_TRUE(FileExists(file_name));
  198. EXPECT_EQ(1020, GetSize(file_name));
  199. }
  200. TEST_F(LocalFileUtilTest, CopyFile) {
  201. const char *from_file = "fromfile";
  202. const char *to_file1 = "tofile1";
  203. const char *to_file2 = "tofile2";
  204. bool created;
  205. ASSERT_EQ(base::File::FILE_OK, EnsureFileExists(from_file, &created));
  206. ASSERT_TRUE(created);
  207. scoped_ptr<FileSystemOperationContext> context;
  208. context.reset(NewContext());
  209. ASSERT_EQ(base::File::FILE_OK,
  210. file_util()->Truncate(context.get(), CreateURL(from_file), 1020));
  211. EXPECT_TRUE(FileExists(from_file));
  212. EXPECT_EQ(1020, GetSize(from_file));
  213. ASSERT_EQ(base::File::FILE_OK,
  214. AsyncFileTestHelper::Copy(file_system_context(),
  215. CreateURL(from_file),
  216. CreateURL(to_file1)));
  217. context.reset(NewContext());
  218. ASSERT_EQ(base::File::FILE_OK,
  219. AsyncFileTestHelper::Copy(file_system_context(),
  220. CreateURL(from_file),
  221. CreateURL(to_file2)));
  222. EXPECT_TRUE(FileExists(from_file));
  223. EXPECT_EQ(1020, GetSize(from_file));
  224. EXPECT_TRUE(FileExists(to_file1));
  225. EXPECT_EQ(1020, GetSize(to_file1));
  226. EXPECT_TRUE(FileExists(to_file2));
  227. EXPECT_EQ(1020, GetSize(to_file2));
  228. }
  229. TEST_F(LocalFileUtilTest, CopyDirectory) {
  230. const char *from_dir = "fromdir";
  231. const char *from_file = "fromdir/fromfile";
  232. const char *to_dir = "todir";
  233. const char *to_file = "todir/fromfile";
  234. bool created;
  235. scoped_ptr<FileSystemOperationContext> context;
  236. context.reset(NewContext());
  237. ASSERT_EQ(base::File::FILE_OK,
  238. file_util()->CreateDirectory(context.get(), CreateURL(from_dir),
  239. false, false));
  240. ASSERT_EQ(base::File::FILE_OK, EnsureFileExists(from_file, &created));
  241. ASSERT_TRUE(created);
  242. context.reset(NewContext());
  243. ASSERT_EQ(base::File::FILE_OK,
  244. file_util()->Truncate(context.get(), CreateURL(from_file), 1020));
  245. EXPECT_TRUE(DirectoryExists(from_dir));
  246. EXPECT_TRUE(FileExists(from_file));
  247. EXPECT_EQ(1020, GetSize(from_file));
  248. EXPECT_FALSE(DirectoryExists(to_dir));
  249. context.reset(NewContext());
  250. ASSERT_EQ(base::File::FILE_OK,
  251. AsyncFileTestHelper::Copy(file_system_context(),
  252. CreateURL(from_dir), CreateURL(to_dir)));
  253. EXPECT_TRUE(DirectoryExists(from_dir));
  254. EXPECT_TRUE(FileExists(from_file));
  255. EXPECT_EQ(1020, GetSize(from_file));
  256. EXPECT_TRUE(DirectoryExists(to_dir));
  257. EXPECT_TRUE(FileExists(to_file));
  258. EXPECT_EQ(1020, GetSize(to_file));
  259. }
  260. TEST_F(LocalFileUtilTest, MoveFile) {
  261. const char *from_file = "fromfile";
  262. const char *to_file = "tofile";
  263. bool created;
  264. ASSERT_EQ(base::File::FILE_OK, EnsureFileExists(from_file, &created));
  265. ASSERT_TRUE(created);
  266. scoped_ptr<FileSystemOperationContext> context;
  267. context.reset(NewContext());
  268. ASSERT_EQ(base::File::FILE_OK,
  269. file_util()->Truncate(context.get(), CreateURL(from_file), 1020));
  270. EXPECT_TRUE(FileExists(from_file));
  271. EXPECT_EQ(1020, GetSize(from_file));
  272. context.reset(NewContext());
  273. ASSERT_EQ(base::File::FILE_OK,
  274. AsyncFileTestHelper::Move(file_system_context(),
  275. CreateURL(from_file),
  276. CreateURL(to_file)));
  277. EXPECT_FALSE(FileExists(from_file));
  278. EXPECT_TRUE(FileExists(to_file));
  279. EXPECT_EQ(1020, GetSize(to_file));
  280. }
  281. TEST_F(LocalFileUtilTest, MoveDirectory) {
  282. const char *from_dir = "fromdir";
  283. const char *from_file = "fromdir/fromfile";
  284. const char *to_dir = "todir";
  285. const char *to_file = "todir/fromfile";
  286. bool created;
  287. scoped_ptr<FileSystemOperationContext> context;
  288. context.reset(NewContext());
  289. ASSERT_EQ(base::File::FILE_OK,
  290. file_util()->CreateDirectory(context.get(), CreateURL(from_dir),
  291. false, false));
  292. ASSERT_EQ(base::File::FILE_OK, EnsureFileExists(from_file, &created));
  293. ASSERT_TRUE(created);
  294. context.reset(NewContext());
  295. ASSERT_EQ(base::File::FILE_OK,
  296. file_util()->Truncate(context.get(), CreateURL(from_file), 1020));
  297. EXPECT_TRUE(DirectoryExists(from_dir));
  298. EXPECT_TRUE(FileExists(from_file));
  299. EXPECT_EQ(1020, GetSize(from_file));
  300. EXPECT_FALSE(DirectoryExists(to_dir));
  301. context.reset(NewContext());
  302. ASSERT_EQ(base::File::FILE_OK,
  303. AsyncFileTestHelper::Move(file_system_context(),
  304. CreateURL(from_dir),
  305. CreateURL(to_dir)));
  306. EXPECT_FALSE(DirectoryExists(from_dir));
  307. EXPECT_TRUE(DirectoryExists(to_dir));
  308. EXPECT_TRUE(FileExists(to_file));
  309. EXPECT_EQ(1020, GetSize(to_file));
  310. }
  311. } // namespace content