1 // Copyright (c) 2011 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.
5 #include "ppapi/tests/test_file_ref.h"
12 #include "ppapi/c/pp_errors.h"
13 #include "ppapi/c/ppb_file_io.h"
14 #include "ppapi/c/dev/ppb_testing_dev.h"
15 #include "ppapi/cpp/directory_entry.h"
16 #include "ppapi/cpp/file_io.h"
17 #include "ppapi/cpp/file_ref.h"
18 #include "ppapi/cpp/file_system.h"
19 #include "ppapi/cpp/instance.h"
20 #include "ppapi/cpp/module.h"
21 #include "ppapi/cpp/url_loader.h"
22 #include "ppapi/cpp/url_request_info.h"
23 #include "ppapi/cpp/url_response_info.h"
24 #include "ppapi/tests/test_utils.h"
25 #include "ppapi/tests/testing_instance.h"
27 REGISTER_TEST_CASE(FileRef
);
31 const char* kPersFileName
= "persistent";
32 const char* kTempFileName
= "temporary";
33 const char* kParentPath
= "/foo/bar";
34 const char* kPersFilePath
= "/foo/bar/persistent";
35 const char* kTempFilePath
= "/foo/bar/temporary";
36 const char* kTerribleName
= "!@#$%^&*()-_=+{}[] ;:'\"|`~\t\n\r\b?";
38 typedef std::vector
<pp::DirectoryEntry
> DirEntries
;
40 std::string
ReportMismatch(const std::string
& method_name
,
41 const std::string
& returned_result
,
42 const std::string
& expected_result
) {
43 return method_name
+ " returned '" + returned_result
+ "'; '" +
44 expected_result
+ "' expected.";
49 bool TestFileRef::Init() {
50 return CheckTestingInterface() && EnsureRunningOverHTTP();
53 std::string
TestFileRef::MakeExternalFileRef(pp::FileRef
* file_ref_ext
) {
54 pp::URLRequestInfo
request(instance_
);
55 request
.SetURL("test_url_loader_data/hello.txt");
56 request
.SetStreamToFile(true);
58 TestCompletionCallback
callback(instance_
->pp_instance(), callback_type());
60 pp::URLLoader
loader(instance_
);
61 callback
.WaitForResult(loader
.Open(request
, callback
.GetCallback()));
62 CHECK_CALLBACK_BEHAVIOR(callback
);
63 ASSERT_EQ(PP_OK
, callback
.result());
65 pp::URLResponseInfo
response_info(loader
.GetResponseInfo());
66 ASSERT_FALSE(response_info
.is_null());
67 ASSERT_EQ(200, response_info
.GetStatusCode());
69 *file_ref_ext
= pp::FileRef(response_info
.GetBodyAsFileRef());
70 ASSERT_EQ(PP_FILESYSTEMTYPE_EXTERNAL
, file_ref_ext
->GetFileSystemType());
74 int32_t TestFileRef::DeleteDirectoryRecursively(pp::FileRef
* dir
) {
76 return PP_ERROR_BADARGUMENT
;
78 TestCompletionCallback
callback(instance_
->pp_instance(), callback_type());
79 TestCompletionCallbackWithOutput
<DirEntries
> output_callback(
80 instance_
->pp_instance(), callback_type());
82 output_callback
.WaitForResult(
83 dir
->ReadDirectoryEntries(output_callback
.GetCallback()));
84 int32_t rv
= output_callback
.result();
85 if (rv
!= PP_OK
&& rv
!= PP_ERROR_FILENOTFOUND
)
88 DirEntries entries
= output_callback
.output();
89 for (DirEntries::const_iterator it
= entries
.begin();
92 pp::FileRef file_ref
= it
->file_ref();
93 if (it
->file_type() == PP_FILETYPE_DIRECTORY
) {
94 rv
= DeleteDirectoryRecursively(&file_ref
);
95 if (rv
!= PP_OK
&& rv
!= PP_ERROR_FILENOTFOUND
)
98 callback
.WaitForResult(file_ref
.Delete(callback
.GetCallback()));
99 rv
= callback
.result();
100 if (rv
!= PP_OK
&& rv
!= PP_ERROR_FILENOTFOUND
)
104 callback
.WaitForResult(dir
->Delete(callback
.GetCallback()));
105 return callback
.result();
108 void TestFileRef::RunTests(const std::string
& filter
) {
109 RUN_CALLBACK_TEST(TestFileRef
, Create
, filter
);
110 RUN_CALLBACK_TEST(TestFileRef
, GetFileSystemType
, filter
);
111 RUN_CALLBACK_TEST(TestFileRef
, GetName
, filter
);
112 RUN_CALLBACK_TEST(TestFileRef
, GetPath
, filter
);
113 RUN_CALLBACK_TEST(TestFileRef
, GetParent
, filter
);
114 RUN_CALLBACK_TEST(TestFileRef
, MakeDirectory
, filter
);
115 RUN_CALLBACK_TEST(TestFileRef
, QueryAndTouchFile
, filter
);
116 RUN_CALLBACK_TEST(TestFileRef
, DeleteFileAndDirectory
, filter
);
117 RUN_CALLBACK_TEST(TestFileRef
, RenameFileAndDirectory
, filter
);
118 RUN_CALLBACK_TEST(TestFileRef
, Query
, filter
);
119 RUN_CALLBACK_TEST(TestFileRef
, FileNameEscaping
, filter
);
120 RUN_CALLBACK_TEST(TestFileRef
, ReadDirectoryEntries
, filter
);
123 std::string
TestFileRef::TestCreate() {
124 std::vector
<std::string
> invalid_paths
;
125 invalid_paths
.push_back("invalid_path"); // no '/' at the first character
126 invalid_paths
.push_back(std::string()); // empty path
127 // The following are directory traversal checks
128 invalid_paths
.push_back("..");
129 invalid_paths
.push_back("/../invalid_path");
130 invalid_paths
.push_back("/../../invalid_path");
131 invalid_paths
.push_back("/invalid/../../path");
132 const size_t num_invalid_paths
= invalid_paths
.size();
134 pp::FileSystem
file_system_pers(
135 instance_
, PP_FILESYSTEMTYPE_LOCALPERSISTENT
);
136 pp::FileSystem
file_system_temp(
137 instance_
, PP_FILESYSTEMTYPE_LOCALTEMPORARY
);
138 for (size_t j
= 0; j
< num_invalid_paths
; ++j
) {
139 pp::FileRef
file_ref_pers(file_system_pers
, invalid_paths
[j
].c_str());
140 if (file_ref_pers
.pp_resource() != 0) {
141 return "file_ref_pers expected to be invalid for path: " +
144 pp::FileRef
file_ref_temp(file_system_temp
, invalid_paths
[j
].c_str());
145 if (file_ref_temp
.pp_resource() != 0) {
146 return "file_ref_temp expected to be invalid for path: " +
153 std::string
TestFileRef::TestGetFileSystemType() {
154 pp::FileSystem
file_system_pers(
155 instance_
, PP_FILESYSTEMTYPE_LOCALPERSISTENT
);
156 pp::FileSystem
file_system_temp(
157 instance_
, PP_FILESYSTEMTYPE_LOCALTEMPORARY
);
159 pp::FileRef
file_ref_pers(file_system_pers
, kPersFilePath
);
160 if (file_ref_pers
.GetFileSystemType() != PP_FILESYSTEMTYPE_LOCALPERSISTENT
)
161 return "file_ref_pers expected to be persistent.";
163 pp::FileRef
file_ref_temp(file_system_temp
, kTempFilePath
);
164 if (file_ref_temp
.GetFileSystemType() != PP_FILESYSTEMTYPE_LOCALTEMPORARY
)
165 return "file_ref_temp expected to be temporary.";
167 pp::FileRef file_ref_ext
;
168 std::string result
= MakeExternalFileRef(&file_ref_ext
);
174 std::string
TestFileRef::TestGetName() {
175 pp::FileSystem
file_system_pers(
176 instance_
, PP_FILESYSTEMTYPE_LOCALPERSISTENT
);
177 pp::FileSystem
file_system_temp(
178 instance_
, PP_FILESYSTEMTYPE_LOCALTEMPORARY
);
180 pp::FileRef
file_ref_pers(file_system_pers
, kPersFilePath
);
181 std::string name
= file_ref_pers
.GetName().AsString();
182 if (name
!= kPersFileName
)
183 return ReportMismatch("FileRef::GetName", name
, kPersFileName
);
185 pp::FileRef
file_ref_temp(file_system_temp
, kTempFilePath
);
186 name
= file_ref_temp
.GetName().AsString();
187 if (name
!= kTempFileName
)
188 return ReportMismatch("FileRef::GetName", name
, kTempFileName
);
190 // Test the "/" case.
191 pp::FileRef
file_ref_slash(file_system_temp
, "/");
192 name
= file_ref_slash
.GetName().AsString();
194 return ReportMismatch("FileRef::GetName", name
, "/");
196 pp::URLRequestInfo
request(instance_
);
197 request
.SetURL("test_url_loader_data/hello.txt");
198 request
.SetStreamToFile(true);
200 TestCompletionCallback
callback(instance_
->pp_instance(), callback_type());
202 pp::URLLoader
loader(instance_
);
203 callback
.WaitForResult(loader
.Open(request
, callback
.GetCallback()));
204 CHECK_CALLBACK_BEHAVIOR(callback
);
205 ASSERT_EQ(PP_OK
, callback
.result());
207 pp::URLResponseInfo
response_info(loader
.GetResponseInfo());
208 ASSERT_FALSE(response_info
.is_null());
209 ASSERT_EQ(200, response_info
.GetStatusCode());
211 pp::FileRef
file_ref_ext(response_info
.GetBodyAsFileRef());
212 name
= file_ref_ext
.GetName().AsString();
213 ASSERT_FALSE(name
.empty());
218 std::string
TestFileRef::TestGetPath() {
219 pp::FileSystem
file_system_pers(
220 instance_
, PP_FILESYSTEMTYPE_LOCALPERSISTENT
);
221 pp::FileSystem
file_system_temp(
222 instance_
, PP_FILESYSTEMTYPE_LOCALTEMPORARY
);
224 pp::FileRef
file_ref_pers(file_system_pers
, kPersFilePath
);
225 ASSERT_EQ(kPersFilePath
, file_ref_pers
.GetPath().AsString());
227 pp::FileRef
file_ref_temp(file_system_temp
, kTempFilePath
);
228 ASSERT_EQ(kTempFilePath
, file_ref_temp
.GetPath().AsString());
230 pp::URLRequestInfo
request(instance_
);
231 request
.SetURL("test_url_loader_data/hello.txt");
232 request
.SetStreamToFile(true);
234 TestCompletionCallback
callback(instance_
->pp_instance(), callback_type());
236 pp::URLLoader
loader(instance_
);
237 callback
.WaitForResult(loader
.Open(request
, callback
.GetCallback()));
238 CHECK_CALLBACK_BEHAVIOR(callback
);
239 ASSERT_EQ(PP_OK
, callback
.result());
241 pp::URLResponseInfo
response_info(loader
.GetResponseInfo());
242 ASSERT_FALSE(response_info
.is_null());
243 ASSERT_EQ(200, response_info
.GetStatusCode());
245 pp::FileRef
file_ref_ext(response_info
.GetBodyAsFileRef());
246 ASSERT_TRUE(file_ref_ext
.GetPath().is_undefined());
251 std::string
TestFileRef::TestGetParent() {
252 pp::FileSystem
file_system_pers(
253 instance_
, PP_FILESYSTEMTYPE_LOCALPERSISTENT
);
254 pp::FileSystem
file_system_temp(
255 instance_
, PP_FILESYSTEMTYPE_LOCALTEMPORARY
);
257 pp::FileRef
file_ref_pers(file_system_pers
, kPersFilePath
);
258 ASSERT_EQ(kParentPath
, file_ref_pers
.GetParent().GetPath().AsString());
260 pp::FileRef
file_ref_temp(file_system_temp
, kTempFilePath
);
261 ASSERT_EQ(kParentPath
, file_ref_temp
.GetParent().GetPath().AsString());
263 // Test the "/" case.
264 pp::FileRef
file_ref_slash(file_system_temp
, "/");
265 ASSERT_EQ("/", file_ref_slash
.GetParent().GetPath().AsString());
267 // Test the "/foo" case (the parent is "/").
268 pp::FileRef
file_ref_with_root_parent(file_system_temp
, "/foo");
269 ASSERT_EQ("/", file_ref_with_root_parent
.GetParent().GetPath().AsString());
271 pp::URLRequestInfo
request(instance_
);
272 request
.SetURL("test_url_loader_data/hello.txt");
273 request
.SetStreamToFile(true);
275 TestCompletionCallback
callback(instance_
->pp_instance(), callback_type());
277 pp::URLLoader
loader(instance_
);
278 callback
.WaitForResult(loader
.Open(request
, callback
.GetCallback()));
279 CHECK_CALLBACK_BEHAVIOR(callback
);
280 ASSERT_EQ(PP_OK
, callback
.result());
282 pp::URLResponseInfo
response_info(loader
.GetResponseInfo());
283 ASSERT_FALSE(response_info
.is_null());
284 ASSERT_EQ(200, response_info
.GetStatusCode());
286 pp::FileRef
file_ref_ext(response_info
.GetBodyAsFileRef());
287 ASSERT_TRUE(file_ref_ext
.GetParent().is_null());
292 std::string
TestFileRef::TestMakeDirectory() {
293 TestCompletionCallback
callback(instance_
->pp_instance(), callback_type());
296 pp::FileSystem
file_system(instance_
, PP_FILESYSTEMTYPE_LOCALTEMPORARY
);
297 callback
.WaitForResult(file_system
.Open(1024, callback
.GetCallback()));
298 CHECK_CALLBACK_BEHAVIOR(callback
);
299 ASSERT_EQ(PP_OK
, callback
.result());
302 pp::FileRef
dir_ref(file_system
, "/test_dir_make_directory");
303 callback
.WaitForResult(dir_ref
.MakeDirectory(callback
.GetCallback()));
304 CHECK_CALLBACK_BEHAVIOR(callback
);
305 ASSERT_EQ(PP_OK
, callback
.result());
307 // MakeDirectory aborted.
308 int32_t rv
= PP_ERROR_FAILED
;
310 rv
= pp::FileRef(file_system
, "/test_dir_make_abort")
311 .MakeDirectory(callback
.GetCallback());
313 callback
.WaitForAbortResult(rv
);
314 CHECK_CALLBACK_BEHAVIOR(callback
);
316 // MakeDirectoryIncludingAncestors.
317 dir_ref
= pp::FileRef(file_system
, "/dir_make_dir_1/dir_make_dir_2");
318 callback
.WaitForResult(
319 dir_ref
.MakeDirectoryIncludingAncestors(callback
.GetCallback()));
320 CHECK_CALLBACK_BEHAVIOR(callback
);
321 ASSERT_EQ(PP_OK
, callback
.result());
323 // MakeDirectoryIncludingAncestors aborted.
325 rv
= pp::FileRef(file_system
, "/dir_make_abort_1/dir_make_abort_2")
326 .MakeDirectoryIncludingAncestors(callback
.GetCallback());
328 callback
.WaitForAbortResult(rv
);
329 CHECK_CALLBACK_BEHAVIOR(callback
);
331 // MakeDirectory with nested path should fail.
332 dir_ref
= pp::FileRef(file_system
, "/dir_make_dir_3/dir_make_dir_4");
333 callback
.WaitForResult(dir_ref
.MakeDirectory(callback
.GetCallback()));
334 CHECK_CALLBACK_BEHAVIOR(callback
);
335 ASSERT_NE(PP_OK
, callback
.result());
340 std::string
TestFileRef::TestQueryAndTouchFile() {
341 TestCompletionCallback
callback(instance_
->pp_instance(), callback_type());
342 pp::FileSystem
file_system(instance_
, PP_FILESYSTEMTYPE_LOCALTEMPORARY
);
343 callback
.WaitForResult(file_system
.Open(1024, callback
.GetCallback()));
344 CHECK_CALLBACK_BEHAVIOR(callback
);
345 ASSERT_EQ(PP_OK
, callback
.result());
347 pp::FileRef
file_ref(file_system
, "/file_touch");
348 pp::FileIO
file_io(instance_
);
349 callback
.WaitForResult(
350 file_io
.Open(file_ref
,
351 PP_FILEOPENFLAG_CREATE
|
352 PP_FILEOPENFLAG_TRUNCATE
|
353 PP_FILEOPENFLAG_WRITE
,
354 callback
.GetCallback()));
355 CHECK_CALLBACK_BEHAVIOR(callback
);
356 ASSERT_EQ(PP_OK
, callback
.result());
358 // Write some data to have a non-zero file size.
359 callback
.WaitForResult(file_io
.Write(0, "test", 4, callback
.GetCallback()));
360 CHECK_CALLBACK_BEHAVIOR(callback
);
361 ASSERT_EQ(4, callback
.result());
364 const PP_Time last_access_time
= 123 * 24 * 3600.0;
365 // last_modified_time's granularity is 2 seconds
366 // See note in test_file_io.cc for why we use this time.
367 const PP_Time last_modified_time
= 100 * 24 * 3600.0;
368 callback
.WaitForResult(file_ref
.Touch(last_access_time
, last_modified_time
,
369 callback
.GetCallback()));
370 CHECK_CALLBACK_BEHAVIOR(callback
);
371 ASSERT_EQ(PP_OK
, callback
.result());
374 int32_t rv
= PP_ERROR_FAILED
;
376 rv
= pp::FileRef(file_system
, "/file_touch_abort")
377 .Touch(last_access_time
, last_modified_time
, callback
.GetCallback());
379 callback
.WaitForResult(rv
);
380 CHECK_CALLBACK_BEHAVIOR(callback
);
381 if (rv
== PP_OK_COMPLETIONPENDING
) {
382 // Touch tried to run asynchronously and should have been aborted.
383 ASSERT_EQ(PP_ERROR_ABORTED
, callback
.result());
385 // Touch ran synchronously and should have failed because the file does not
387 ASSERT_EQ(PP_ERROR_FILENOTFOUND
, callback
.result());
392 callback
.WaitForResult(file_io
.Query(&info
, callback
.GetCallback()));
393 CHECK_CALLBACK_BEHAVIOR(callback
);
394 ASSERT_EQ(PP_OK
, callback
.result());
395 ASSERT_EQ(4, info
.size
);
396 ASSERT_EQ(PP_FILETYPE_REGULAR
, info
.type
);
397 ASSERT_EQ(PP_FILESYSTEMTYPE_LOCALTEMPORARY
, info
.system_type
);
398 ASSERT_EQ(last_access_time
, info
.last_access_time
);
399 ASSERT_EQ(last_modified_time
, info
.last_modified_time
);
401 // Cancellation test.
402 // TODO(viettrungluu): this test causes a bunch of LOG(WARNING)s; investigate.
403 // TODO(viettrungluu): check |info| for late writes.
405 rv
= pp::FileRef(file_system
, "/file_touch").Touch(
406 last_access_time
, last_modified_time
, callback
.GetCallback());
408 callback
.WaitForAbortResult(rv
);
409 CHECK_CALLBACK_BEHAVIOR(callback
);
414 std::string
TestFileRef::TestDeleteFileAndDirectory() {
415 TestCompletionCallback
callback(instance_
->pp_instance(), callback_type());
416 pp::FileSystem
file_system(instance_
, PP_FILESYSTEMTYPE_LOCALTEMPORARY
);
417 callback
.WaitForResult(file_system
.Open(1024, callback
.GetCallback()));
418 CHECK_CALLBACK_BEHAVIOR(callback
);
419 ASSERT_EQ(PP_OK
, callback
.result());
421 pp::FileRef
file_ref(file_system
, "/file_delete");
422 pp::FileIO
file_io(instance_
);
423 callback
.WaitForResult(
424 file_io
.Open(file_ref
, PP_FILEOPENFLAG_CREATE
, callback
.GetCallback()));
425 CHECK_CALLBACK_BEHAVIOR(callback
);
426 ASSERT_EQ(PP_OK
, callback
.result());
428 callback
.WaitForResult(file_ref
.Delete(callback
.GetCallback()));
429 CHECK_CALLBACK_BEHAVIOR(callback
);
430 ASSERT_EQ(PP_OK
, callback
.result());
432 pp::FileRef
dir_ref(file_system
, "/dir_delete");
433 callback
.WaitForResult(dir_ref
.MakeDirectory(callback
.GetCallback()));
434 CHECK_CALLBACK_BEHAVIOR(callback
);
435 ASSERT_EQ(PP_OK
, callback
.result());
437 callback
.WaitForResult(dir_ref
.Delete(callback
.GetCallback()));
438 CHECK_CALLBACK_BEHAVIOR(callback
);
439 ASSERT_EQ(PP_OK
, callback
.result());
441 pp::FileRef
nested_dir_ref(file_system
, "/dir_delete_1/dir_delete_2");
442 callback
.WaitForResult(
443 nested_dir_ref
.MakeDirectoryIncludingAncestors(callback
.GetCallback()));
444 CHECK_CALLBACK_BEHAVIOR(callback
);
445 ASSERT_EQ(PP_OK
, callback
.result());
447 // Attempt to delete the parent directory (should fail; it's non-empty).
448 pp::FileRef parent_dir_ref
= nested_dir_ref
.GetParent();
449 callback
.WaitForResult(parent_dir_ref
.Delete(callback
.GetCallback()));
450 CHECK_CALLBACK_BEHAVIOR(callback
);
451 ASSERT_EQ(PP_ERROR_FAILED
, callback
.result());
453 pp::FileRef
nonexistent_file_ref(file_system
, "/nonexistent_file_delete");
454 callback
.WaitForResult(nonexistent_file_ref
.Delete(callback
.GetCallback()));
455 CHECK_CALLBACK_BEHAVIOR(callback
);
456 ASSERT_EQ(PP_ERROR_FILENOTFOUND
, callback
.result());
459 int32_t rv
= PP_ERROR_FAILED
;
461 pp::FileRef
file_ref_abort(file_system
, "/file_delete_abort");
462 pp::FileIO
file_io_abort(instance_
);
463 callback
.WaitForResult(
464 file_io_abort
.Open(file_ref_abort
, PP_FILEOPENFLAG_CREATE
,
465 callback
.GetCallback()));
466 CHECK_CALLBACK_BEHAVIOR(callback
);
467 ASSERT_EQ(PP_OK
, callback
.result());
468 rv
= file_ref_abort
.Delete(callback
.GetCallback());
470 callback
.WaitForAbortResult(rv
);
471 CHECK_CALLBACK_BEHAVIOR(callback
);
476 std::string
TestFileRef::TestRenameFileAndDirectory() {
477 TestCompletionCallback
callback(instance_
->pp_instance(), callback_type());
478 pp::FileSystem
file_system(instance_
, PP_FILESYSTEMTYPE_LOCALTEMPORARY
);
479 callback
.WaitForResult(file_system
.Open(1024, callback
.GetCallback()));
480 CHECK_CALLBACK_BEHAVIOR(callback
);
481 ASSERT_EQ(PP_OK
, callback
.result());
483 pp::FileRef
file_ref(file_system
, "/file_rename");
484 pp::FileIO
file_io(instance_
);
485 callback
.WaitForResult(
486 file_io
.Open(file_ref
, PP_FILEOPENFLAG_CREATE
, callback
.GetCallback()));
487 CHECK_CALLBACK_BEHAVIOR(callback
);
488 ASSERT_EQ(PP_OK
, callback
.result());
490 pp::FileRef
target_file_ref(file_system
, "/target_file_rename");
491 callback
.WaitForResult(
492 file_ref
.Rename(target_file_ref
, callback
.GetCallback()));
493 CHECK_CALLBACK_BEHAVIOR(callback
);
494 ASSERT_EQ(PP_OK
, callback
.result());
496 pp::FileRef
dir_ref(file_system
, "/dir_rename");
497 callback
.WaitForResult(dir_ref
.MakeDirectory(callback
.GetCallback()));
498 CHECK_CALLBACK_BEHAVIOR(callback
);
499 ASSERT_EQ(PP_OK
, callback
.result());
501 pp::FileRef
target_dir_ref(file_system
, "/target_dir_rename");
502 callback
.WaitForResult(
503 dir_ref
.Rename(target_dir_ref
, callback
.GetCallback()));
504 CHECK_CALLBACK_BEHAVIOR(callback
);
505 ASSERT_EQ(PP_OK
, callback
.result());
507 pp::FileRef
nested_dir_ref(file_system
, "/dir_rename_1/dir_rename_2");
508 callback
.WaitForResult(
509 nested_dir_ref
.MakeDirectoryIncludingAncestors(callback
.GetCallback()));
510 CHECK_CALLBACK_BEHAVIOR(callback
);
511 ASSERT_EQ(PP_OK
, callback
.result());
513 // Try to rename nested directory to the parent name. Should fail.
514 pp::FileRef
target_nested_dir_ref(file_system
, "/dir_rename_1");
515 callback
.WaitForResult(
516 nested_dir_ref
.Rename(target_nested_dir_ref
, callback
.GetCallback()));
517 CHECK_CALLBACK_BEHAVIOR(callback
);
518 ASSERT_EQ(PP_ERROR_FAILED
, callback
.result());
521 // TODO(viettrungluu): Figure out what we want to do if the target file
522 // resource is destroyed before completion.
523 int32_t rv
= PP_ERROR_FAILED
;
524 pp::FileRef
target_file_ref_abort(file_system
,
525 "/target_file_rename_abort");
527 pp::FileRef
file_ref_abort(file_system
, "/file_rename_abort");
528 pp::FileIO
file_io_abort(instance_
);
529 callback
.WaitForResult(
530 file_io_abort
.Open(file_ref_abort
, PP_FILEOPENFLAG_CREATE
,
531 callback
.GetCallback()));
532 CHECK_CALLBACK_BEHAVIOR(callback
);
533 ASSERT_EQ(PP_OK
, callback
.result());
535 rv
= file_ref_abort
.Rename(target_file_ref_abort
, callback
.GetCallback());
537 callback
.WaitForAbortResult(rv
);
538 CHECK_CALLBACK_BEHAVIOR(callback
);
543 std::string
TestFileRef::TestQuery() {
544 TestCompletionCallback
callback(instance_
->pp_instance(), callback_type());
546 pp::FileSystem
file_system(instance_
, PP_FILESYSTEMTYPE_LOCALTEMPORARY
);
547 callback
.WaitForResult(file_system
.Open(1024, callback
.GetCallback()));
548 CHECK_CALLBACK_BEHAVIOR(callback
);
549 ASSERT_EQ(PP_OK
, callback
.result());
551 pp::FileRef
file_ref(file_system
, "/file");
552 pp::FileIO
file_io(instance_
);
553 callback
.WaitForResult(file_io
.Open(file_ref
, PP_FILEOPENFLAG_CREATE
,
554 callback
.GetCallback()));
555 CHECK_CALLBACK_BEHAVIOR(callback
);
556 ASSERT_EQ(PP_OK
, callback
.result());
558 // We touch the file so we can easily check access and modified time.
559 callback
.WaitForResult(file_io
.Touch(0, 0, callback
.GetCallback()));
560 CHECK_CALLBACK_BEHAVIOR(callback
);
561 ASSERT_EQ(PP_OK
, callback
.result());
563 TestCompletionCallbackWithOutput
<PP_FileInfo
> out_callback(
564 instance_
->pp_instance(), callback_type());
565 out_callback
.WaitForResult(file_ref
.Query(out_callback
.GetCallback()));
566 CHECK_CALLBACK_BEHAVIOR(out_callback
);
567 ASSERT_EQ(PP_OK
, out_callback
.result());
569 PP_FileInfo info
= out_callback
.output();
570 ASSERT_EQ(0, info
.size
);
571 ASSERT_EQ(PP_FILETYPE_REGULAR
, info
.type
);
572 ASSERT_EQ(PP_FILESYSTEMTYPE_LOCALTEMPORARY
, info
.system_type
);
573 ASSERT_DOUBLE_EQ(0.0, info
.last_access_time
);
574 ASSERT_DOUBLE_EQ(0.0, info
.last_modified_time
);
576 // Query a file ref on an external filesystem.
577 pp::FileRef file_ref_ext
;
578 std::string result
= MakeExternalFileRef(&file_ref_ext
);
581 out_callback
.WaitForResult(file_ref_ext
.Query(out_callback
.GetCallback()));
582 CHECK_CALLBACK_BEHAVIOR(out_callback
);
583 if (out_callback
.result() != PP_OK
)
584 return ReportError("Query() result", out_callback
.result());
585 ASSERT_EQ(PP_OK
, out_callback
.result());
587 info
= out_callback
.output();
588 ASSERT_EQ(PP_FILETYPE_REGULAR
, info
.type
);
589 ASSERT_EQ(PP_FILESYSTEMTYPE_EXTERNAL
, info
.system_type
);
591 // We can't touch the file, so just sanity check the times.
592 ASSERT_TRUE(info
.creation_time
>= 0.0);
593 ASSERT_TRUE(info
.last_modified_time
>= 0.0);
594 ASSERT_TRUE(info
.last_access_time
>= 0.0);
596 // Query a file ref for a file that doesn't exist.
597 pp::FileRef
missing_file_ref(file_system
, "/missing_file");
598 out_callback
.WaitForResult(missing_file_ref
.Query(
599 out_callback
.GetCallback()));
600 CHECK_CALLBACK_BEHAVIOR(out_callback
);
601 ASSERT_EQ(PP_ERROR_FILENOTFOUND
, out_callback
.result());
606 std::string
TestFileRef::TestFileNameEscaping() {
607 TestCompletionCallback
callback(instance_
->pp_instance(), callback_type());
608 pp::FileSystem
file_system(instance_
, PP_FILESYSTEMTYPE_LOCALTEMPORARY
);
609 callback
.WaitForResult(file_system
.Open(1024, callback
.GetCallback()));
610 CHECK_CALLBACK_BEHAVIOR(callback
);
611 ASSERT_EQ(PP_OK
, callback
.result());
613 std::string test_dir_path
= "/dir_for_escaping_test";
614 // Create a directory in which to test.
615 pp::FileRef
test_dir_ref(file_system
, test_dir_path
.c_str());
616 callback
.WaitForResult(test_dir_ref
.MakeDirectory(callback
.GetCallback()));
617 CHECK_CALLBACK_BEHAVIOR(callback
);
618 ASSERT_EQ(PP_OK
, callback
.result());
620 // Create the file with the terrible name.
621 std::string full_file_path
= test_dir_path
+ "/" + kTerribleName
;
622 pp::FileRef
file_ref(file_system
, full_file_path
.c_str());
623 pp::FileIO
file_io(instance_
);
624 callback
.WaitForResult(
625 file_io
.Open(file_ref
, PP_FILEOPENFLAG_CREATE
, callback
.GetCallback()));
626 CHECK_CALLBACK_BEHAVIOR(callback
);
627 ASSERT_EQ(PP_OK
, callback
.result());
629 // FileRef::ReadDirectoryEntries only works out-of-process.
630 if (testing_interface_
->IsOutOfProcess()) {
631 TestCompletionCallbackWithOutput
<DirEntries
>
632 output_callback(instance_
->pp_instance(), callback_type());
634 output_callback
.WaitForResult(
635 test_dir_ref
.ReadDirectoryEntries(output_callback
.GetCallback()));
636 CHECK_CALLBACK_BEHAVIOR(output_callback
);
637 ASSERT_EQ(PP_OK
, output_callback
.result());
639 DirEntries entries
= output_callback
.output();
640 ASSERT_EQ(1, entries
.size());
641 ASSERT_EQ(kTerribleName
, entries
.front().file_ref().GetName().AsString());
647 std::string
TestFileRef::TestReadDirectoryEntries() {
648 TestCompletionCallback
callback(instance_
->pp_instance(), callback_type());
649 pp::FileSystem
file_system(
650 instance_
, PP_FILESYSTEMTYPE_LOCALTEMPORARY
);
651 callback
.WaitForResult(file_system
.Open(1024, callback
.GetCallback()));
652 CHECK_CALLBACK_BEHAVIOR(callback
);
653 ASSERT_EQ(PP_OK
, callback
.result());
655 // Setup testing directories and files.
656 const char* test_dir_name
= "/test_get_next_file";
657 const char* file_prefix
= "file_";
658 const char* dir_prefix
= "dir_";
660 pp::FileRef
test_dir(file_system
, test_dir_name
);
661 int32_t rv
= DeleteDirectoryRecursively(&test_dir
);
662 ASSERT_TRUE(rv
== PP_OK
|| rv
== PP_ERROR_FILENOTFOUND
);
664 callback
.WaitForResult(test_dir
.MakeDirectory(callback
.GetCallback()));
665 CHECK_CALLBACK_BEHAVIOR(callback
);
666 ASSERT_EQ(PP_OK
, callback
.result());
668 static const int kNumFiles
= 3;
669 std::set
<std::string
> expected_file_names
;
670 for (int i
= 1; i
<= kNumFiles
; ++i
) {
671 std::ostringstream buffer
;
672 buffer
<< test_dir_name
<< '/' << file_prefix
<< i
;
673 pp::FileRef
file_ref(file_system
, buffer
.str().c_str());
675 pp::FileIO
file_io(instance_
);
676 callback
.WaitForResult(
677 file_io
.Open(file_ref
, PP_FILEOPENFLAG_CREATE
, callback
.GetCallback()));
678 CHECK_CALLBACK_BEHAVIOR(callback
);
679 ASSERT_EQ(PP_OK
, callback
.result());
681 expected_file_names
.insert(buffer
.str());
684 static const int kNumDirectories
= 3;
685 std::set
<std::string
> expected_dir_names
;
686 for (int i
= 1; i
<= kNumDirectories
; ++i
) {
687 std::ostringstream buffer
;
688 buffer
<< test_dir_name
<< '/' << dir_prefix
<< i
;
689 pp::FileRef
file_ref(file_system
, buffer
.str().c_str());
691 callback
.WaitForResult(file_ref
.MakeDirectory(callback
.GetCallback()));
692 CHECK_CALLBACK_BEHAVIOR(callback
);
693 ASSERT_EQ(PP_OK
, callback
.result());
695 expected_dir_names
.insert(buffer
.str());
698 // Test that |ReadDirectoryEntries()| is able to fetch all
699 // directories and files that we created.
701 TestCompletionCallbackWithOutput
<DirEntries
> output_callback(
702 instance_
->pp_instance(), callback_type());
704 output_callback
.WaitForResult(
705 test_dir
.ReadDirectoryEntries(output_callback
.GetCallback()));
706 CHECK_CALLBACK_BEHAVIOR(output_callback
);
707 ASSERT_EQ(PP_OK
, output_callback
.result());
709 DirEntries entries
= output_callback
.output();
710 size_t sum
= expected_file_names
.size() + expected_dir_names
.size();
711 ASSERT_EQ(sum
, entries
.size());
713 for (DirEntries::const_iterator it
= entries
.begin();
714 it
!= entries
.end(); ++it
) {
715 pp::FileRef file_ref
= it
->file_ref();
716 std::string file_path
= file_ref
.GetPath().AsString();
717 std::set
<std::string
>::iterator found
=
718 expected_file_names
.find(file_path
);
719 if (found
!= expected_file_names
.end()) {
720 if (it
->file_type() != PP_FILETYPE_REGULAR
)
721 return file_path
+ " should have been a regular file.";
722 expected_file_names
.erase(found
);
724 found
= expected_dir_names
.find(file_path
);
725 if (found
== expected_dir_names
.end())
726 return "Unexpected file path: " + file_path
;
727 if (it
->file_type() != PP_FILETYPE_DIRECTORY
)
728 return file_path
+ " should have been a directory.";
729 expected_dir_names
.erase(found
);
732 ASSERT_TRUE(expected_file_names
.empty());
733 ASSERT_TRUE(expected_dir_names
.empty());
736 // Test cancellation of asynchronous |ReadDirectoryEntries()|.
737 TestCompletionCallbackWithOutput
<DirEntries
> output_callback(
738 instance_
->pp_instance(), callback_type());
740 rv
= pp::FileRef(file_system
, test_dir_name
)
741 .ReadDirectoryEntries(output_callback
.GetCallback());
743 output_callback
.WaitForAbortResult(rv
);
744 CHECK_CALLBACK_BEHAVIOR(output_callback
);