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

Side by Side Diff: content/browser/fileapi/local_file_util_unittest.cc

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

Powered by Google App Engine
This is Rietveld 408576698