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

Side by Side Diff: ppapi/tests/test_file_ref.cc

Issue 14784002: Move DirectoryReader::ReadEntries to FileRef::ReadDirectoryEntries (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: address dmichael's comments Created 7 years, 7 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
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "ppapi/tests/test_file_ref.h" 5 #include "ppapi/tests/test_file_ref.h"
6 6
7 #include <stdio.h> 7 #include <stdio.h>
8 #include <string.h>
9
8 #include <vector> 10 #include <vector>
9 11
10 #include "ppapi/c/pp_errors.h" 12 #include "ppapi/c/pp_errors.h"
11 #include "ppapi/c/ppb_file_io.h" 13 #include "ppapi/c/ppb_file_io.h"
12 #include "ppapi/c/dev/ppb_testing_dev.h" 14 #include "ppapi/c/dev/ppb_testing_dev.h"
13 #include "ppapi/cpp/dev/directory_entry_dev.h" 15 #include "ppapi/cpp/directory_entry.h"
14 #include "ppapi/cpp/dev/directory_reader_dev.h"
15 #include "ppapi/cpp/file_io.h" 16 #include "ppapi/cpp/file_io.h"
16 #include "ppapi/cpp/file_ref.h" 17 #include "ppapi/cpp/file_ref.h"
17 #include "ppapi/cpp/file_system.h" 18 #include "ppapi/cpp/file_system.h"
18 #include "ppapi/cpp/instance.h" 19 #include "ppapi/cpp/instance.h"
19 #include "ppapi/cpp/module.h" 20 #include "ppapi/cpp/module.h"
20 #include "ppapi/cpp/url_loader.h" 21 #include "ppapi/cpp/url_loader.h"
21 #include "ppapi/cpp/url_request_info.h" 22 #include "ppapi/cpp/url_request_info.h"
22 #include "ppapi/cpp/url_response_info.h" 23 #include "ppapi/cpp/url_response_info.h"
23 #include "ppapi/tests/test_utils.h" 24 #include "ppapi/tests/test_utils.h"
24 #include "ppapi/tests/testing_instance.h" 25 #include "ppapi/tests/testing_instance.h"
25 26
26 REGISTER_TEST_CASE(FileRef); 27 REGISTER_TEST_CASE(FileRef);
27 28
28 namespace { 29 namespace {
29 30
30 const char* kPersFileName = "persistent"; 31 const char* kPersFileName = "persistent";
31 const char* kTempFileName = "temporary"; 32 const char* kTempFileName = "temporary";
32 const char* kParentPath = "/foo/bar"; 33 const char* kParentPath = "/foo/bar";
33 const char* kPersFilePath = "/foo/bar/persistent"; 34 const char* kPersFilePath = "/foo/bar/persistent";
34 const char* kTempFilePath = "/foo/bar/temporary"; 35 const char* kTempFilePath = "/foo/bar/temporary";
35 const char* kTerribleName = "!@#$%^&*()-_=+{}[] ;:'\"|`~\t\n\r\b?"; 36 const char* kTerribleName = "!@#$%^&*()-_=+{}[] ;:'\"|`~\t\n\r\b?";
36 37
38 typedef std::vector<pp::DirectoryEntry> DirEntries;
39
37 std::string ReportMismatch(const std::string& method_name, 40 std::string ReportMismatch(const std::string& method_name,
38 const std::string& returned_result, 41 const std::string& returned_result,
39 const std::string& expected_result) { 42 const std::string& expected_result) {
40 return method_name + " returned '" + returned_result + "'; '" + 43 return method_name + " returned '" + returned_result + "'; '" +
41 expected_result + "' expected."; 44 expected_result + "' expected.";
42 } 45 }
43 46
44 } // namespace 47 } // namespace
45 48
46 bool TestFileRef::Init() { 49 bool TestFileRef::Init() {
(...skipping 14 matching lines...) Expand all
61 64
62 pp::URLResponseInfo response_info(loader.GetResponseInfo()); 65 pp::URLResponseInfo response_info(loader.GetResponseInfo());
63 ASSERT_FALSE(response_info.is_null()); 66 ASSERT_FALSE(response_info.is_null());
64 ASSERT_EQ(200, response_info.GetStatusCode()); 67 ASSERT_EQ(200, response_info.GetStatusCode());
65 68
66 *file_ref_ext = pp::FileRef(response_info.GetBodyAsFileRef()); 69 *file_ref_ext = pp::FileRef(response_info.GetBodyAsFileRef());
67 ASSERT_EQ(PP_FILESYSTEMTYPE_EXTERNAL, file_ref_ext->GetFileSystemType()); 70 ASSERT_EQ(PP_FILESYSTEMTYPE_EXTERNAL, file_ref_ext->GetFileSystemType());
68 PASS(); 71 PASS();
69 } 72 }
70 73
74 int32_t TestFileRef::DeleteDirectoryRecursively(pp::FileRef* dir) {
75 if (!dir)
76 return PP_ERROR_BADARGUMENT;
77
78 TestCompletionCallback callback(instance_->pp_instance(), callback_type());
79 TestCompletionCallbackWithOutput<DirEntries> output_callback(
80 instance_->pp_instance(), callback_type());
81
82 output_callback.WaitForResult(
83 dir->ReadEntries(output_callback.GetCallback()));
84 int32_t rv = output_callback.result();
85 if (rv != PP_OK && rv != PP_ERROR_FILENOTFOUND)
86 return rv;
87
88 DirEntries entries = output_callback.output();
89 for (DirEntries::const_iterator it = entries.begin();
90 it != entries.end();
91 ++it) {
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)
96 return rv;
97 } else {
98 callback.WaitForResult(file_ref.Delete(callback.GetCallback()));
99 rv = callback.result();
100 if (rv != PP_OK && rv != PP_ERROR_FILENOTFOUND)
101 return rv;
102 }
103 }
104 callback.WaitForResult(dir->Delete(callback.GetCallback()));
105 return callback.result();
106 }
107
71 void TestFileRef::RunTests(const std::string& filter) { 108 void TestFileRef::RunTests(const std::string& filter) {
72 RUN_CALLBACK_TEST(TestFileRef, Create, filter); 109 RUN_CALLBACK_TEST(TestFileRef, Create, filter);
73 RUN_CALLBACK_TEST(TestFileRef, GetFileSystemType, filter); 110 RUN_CALLBACK_TEST(TestFileRef, GetFileSystemType, filter);
74 RUN_CALLBACK_TEST(TestFileRef, GetName, filter); 111 RUN_CALLBACK_TEST(TestFileRef, GetName, filter);
75 RUN_CALLBACK_TEST(TestFileRef, GetPath, filter); 112 RUN_CALLBACK_TEST(TestFileRef, GetPath, filter);
76 RUN_CALLBACK_TEST(TestFileRef, GetParent, filter); 113 RUN_CALLBACK_TEST(TestFileRef, GetParent, filter);
77 RUN_CALLBACK_TEST(TestFileRef, MakeDirectory, filter); 114 RUN_CALLBACK_TEST(TestFileRef, MakeDirectory, filter);
78 RUN_CALLBACK_TEST(TestFileRef, QueryAndTouchFile, filter); 115 RUN_CALLBACK_TEST(TestFileRef, QueryAndTouchFile, filter);
79 RUN_CALLBACK_TEST(TestFileRef, DeleteFileAndDirectory, filter); 116 RUN_CALLBACK_TEST(TestFileRef, DeleteFileAndDirectory, filter);
80 RUN_CALLBACK_TEST(TestFileRef, RenameFileAndDirectory, filter); 117 RUN_CALLBACK_TEST(TestFileRef, RenameFileAndDirectory, filter);
81 RUN_CALLBACK_TEST(TestFileRef, Query, filter); 118 RUN_CALLBACK_TEST(TestFileRef, Query, filter);
82 RUN_CALLBACK_TEST(TestFileRef, FileNameEscaping, filter); 119 RUN_CALLBACK_TEST(TestFileRef, FileNameEscaping, filter);
120 // FileRef::ReadEntries is in-process only.
raymes 2013/05/02 21:45:21 I think you mean out-of-process only?
hamaji 2013/05/02 22:45:14 Oops, done!
121 if (testing_interface_->IsOutOfProcess())
122 RUN_CALLBACK_TEST(TestFileRef, ReadEntries, filter);
83 } 123 }
84 124
85 std::string TestFileRef::TestCreate() { 125 std::string TestFileRef::TestCreate() {
86 std::vector<std::string> invalid_paths; 126 std::vector<std::string> invalid_paths;
87 invalid_paths.push_back("invalid_path"); // no '/' at the first character 127 invalid_paths.push_back("invalid_path"); // no '/' at the first character
88 invalid_paths.push_back(std::string()); // empty path 128 invalid_paths.push_back(std::string()); // empty path
89 // The following are directory traversal checks 129 // The following are directory traversal checks
90 invalid_paths.push_back(".."); 130 invalid_paths.push_back("..");
91 invalid_paths.push_back("/../invalid_path"); 131 invalid_paths.push_back("/../invalid_path");
92 invalid_paths.push_back("/../../invalid_path"); 132 invalid_paths.push_back("/../../invalid_path");
(...skipping 488 matching lines...) Expand 10 before | Expand all | Expand 10 after
581 621
582 // Create the file with the terrible name. 622 // Create the file with the terrible name.
583 std::string full_file_path = test_dir_path + "/" + kTerribleName; 623 std::string full_file_path = test_dir_path + "/" + kTerribleName;
584 pp::FileRef file_ref(file_system, full_file_path.c_str()); 624 pp::FileRef file_ref(file_system, full_file_path.c_str());
585 pp::FileIO file_io(instance_); 625 pp::FileIO file_io(instance_);
586 callback.WaitForResult( 626 callback.WaitForResult(
587 file_io.Open(file_ref, PP_FILEOPENFLAG_CREATE, callback.GetCallback())); 627 file_io.Open(file_ref, PP_FILEOPENFLAG_CREATE, callback.GetCallback()));
588 CHECK_CALLBACK_BEHAVIOR(callback); 628 CHECK_CALLBACK_BEHAVIOR(callback);
589 ASSERT_EQ(PP_OK, callback.result()); 629 ASSERT_EQ(PP_OK, callback.result());
590 630
591 // DirectoryReader only works out-of-process. 631 // FileRef::ReadEntries only works out-of-process.
592 if (testing_interface_->IsOutOfProcess()) { 632 if (testing_interface_->IsOutOfProcess()) {
593 TestCompletionCallbackWithOutput< std::vector<pp::DirectoryEntry_Dev> > 633 TestCompletionCallbackWithOutput<DirEntries>
594 output_callback(instance_->pp_instance(), callback_type()); 634 output_callback(instance_->pp_instance(), callback_type());
595 pp::DirectoryReader_Dev directory_reader(test_dir_ref);
596 635
597 output_callback.WaitForResult( 636 output_callback.WaitForResult(
598 directory_reader.ReadEntries(output_callback.GetCallback())); 637 test_dir_ref.ReadEntries(output_callback.GetCallback()));
599 CHECK_CALLBACK_BEHAVIOR(output_callback); 638 CHECK_CALLBACK_BEHAVIOR(output_callback);
600 ASSERT_EQ(PP_OK, output_callback.result()); 639 ASSERT_EQ(PP_OK, output_callback.result());
601 640
602 std::vector<pp::DirectoryEntry_Dev> entries = output_callback.output(); 641 DirEntries entries = output_callback.output();
603 ASSERT_EQ(1, entries.size()); 642 ASSERT_EQ(1, entries.size());
604 ASSERT_EQ(kTerribleName, entries.front().file_ref().GetName().AsString()); 643 ASSERT_EQ(kTerribleName, entries.front().file_ref().GetName().AsString());
605 } 644 }
606 645
607 PASS(); 646 PASS();
608 } 647 }
648
649 std::string TestFileRef::TestReadEntries() {
650 TestCompletionCallback callback(instance_->pp_instance(), callback_type());
651 pp::FileSystem file_system(
652 instance_, PP_FILESYSTEMTYPE_LOCALTEMPORARY);
653 callback.WaitForResult(file_system.Open(1024, callback.GetCallback()));
654 CHECK_CALLBACK_BEHAVIOR(callback);
655 ASSERT_EQ(PP_OK, callback.result());
656
657 // Setup testing directories and files.
658 const char* test_dir_name = "/test_get_next_file";
659 const char* file_prefix = "file_";
660 const char* dir_prefix = "dir_";
661
662 pp::FileRef test_dir(file_system, test_dir_name);
663 int32_t rv = DeleteDirectoryRecursively(&test_dir);
664 ASSERT_TRUE(rv == PP_OK || rv == PP_ERROR_FILENOTFOUND);
665
666 callback.WaitForResult(test_dir.MakeDirectory(callback.GetCallback()));
667 CHECK_CALLBACK_BEHAVIOR(callback);
668 ASSERT_EQ(PP_OK, callback.result());
669
670 static const int kNumFiles = 3;
671 std::set<std::string> expected_file_names;
672 for (int i = 1; i <= kNumFiles; ++i) {
673 char buffer[40];
674 memset(buffer, 0, sizeof(buffer));
675 snprintf(buffer, sizeof(buffer) - 1,
676 "%s/%s%d", test_dir_name, file_prefix, i);
677 pp::FileRef file_ref(file_system, buffer);
678
679 pp::FileIO file_io(instance_);
680 callback.WaitForResult(
681 file_io.Open(file_ref, PP_FILEOPENFLAG_CREATE, callback.GetCallback()));
682 CHECK_CALLBACK_BEHAVIOR(callback);
683 ASSERT_EQ(PP_OK, callback.result());
684
685 expected_file_names.insert(buffer);
686 }
687
688 static const int kNumDirectories = 3;
689 std::set<std::string> expected_dir_names;
690 for (int i = 1; i <= kNumDirectories; ++i) {
691 char buffer[40];
692 memset(buffer, 0, sizeof(buffer));
693 snprintf(buffer, sizeof(buffer) - 1,
694 "%s/%s%d", test_dir_name, dir_prefix, i);
695 pp::FileRef file_ref(file_system, buffer);
696
697 callback.WaitForResult(file_ref.MakeDirectory(callback.GetCallback()));
698 CHECK_CALLBACK_BEHAVIOR(callback);
699 ASSERT_EQ(PP_OK, callback.result());
700
701 expected_dir_names.insert(buffer);
702 }
703
704 // Test that |ReadEntries()| is able to fetch all directories and files that
705 // we created.
706 {
707 TestCompletionCallbackWithOutput<DirEntries> output_callback(
708 instance_->pp_instance(), callback_type());
709
710 output_callback.WaitForResult(
711 test_dir.ReadEntries(output_callback.GetCallback()));
712 CHECK_CALLBACK_BEHAVIOR(output_callback);
713 ASSERT_EQ(PP_OK, output_callback.result());
714
715 DirEntries entries = output_callback.output();
716 size_t sum = expected_file_names.size() + expected_dir_names.size();
717 ASSERT_EQ(sum, entries.size());
718
719 for (DirEntries::const_iterator it = entries.begin();
720 it != entries.end(); ++it) {
721 pp::FileRef file_ref = it->file_ref();
722 std::string file_path = file_ref.GetPath().AsString();
723 std::set<std::string>::iterator found =
724 expected_file_names.find(file_path);
725 if (found != expected_file_names.end()) {
726 if (it->file_type() != PP_FILETYPE_REGULAR)
727 return file_path + " should have been a regular file.";
728 expected_file_names.erase(found);
729 } else {
730 found = expected_dir_names.find(file_path);
731 if (found == expected_dir_names.end())
732 return "Unexpected file path: " + file_path;
733 if (it->file_type() != PP_FILETYPE_DIRECTORY)
734 return file_path + " should have been a directory.";
735 expected_dir_names.erase(found);
736 }
737 }
738 ASSERT_TRUE(expected_file_names.empty());
739 ASSERT_TRUE(expected_dir_names.empty());
740 }
741
742 // Test cancellation of asynchronous |ReadEntries()|.
743 TestCompletionCallbackWithOutput<DirEntries> output_callback(
744 instance_->pp_instance(), callback_type());
745 {
746 rv = pp::FileRef(file_system, test_dir_name)
747 .ReadEntries(output_callback.GetCallback());
748 }
749 output_callback.WaitForAbortResult(rv);
750 CHECK_CALLBACK_BEHAVIOR(output_callback);
751
752
753 PASS();
754 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698