OLD | NEW |
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_io.h" | 5 #include "ppapi/tests/test_file_io.h" |
6 | 6 |
7 #include <stdio.h> | 7 #include <stdio.h> |
8 #include <string.h> | 8 #include <string.h> |
9 | 9 |
10 #include "ppapi/c/pp_errors.h" | 10 #include "ppapi/c/pp_errors.h" |
(...skipping 11 matching lines...) Expand all Loading... |
22 | 22 |
23 namespace { | 23 namespace { |
24 | 24 |
25 std::string ReportMismatch(const std::string& method_name, | 25 std::string ReportMismatch(const std::string& method_name, |
26 const std::string& returned_result, | 26 const std::string& returned_result, |
27 const std::string& expected_result) { | 27 const std::string& expected_result) { |
28 return method_name + " returned '" + returned_result + "'; '" + | 28 return method_name + " returned '" + returned_result + "'; '" + |
29 expected_result + "' expected."; | 29 expected_result + "' expected."; |
30 } | 30 } |
31 | 31 |
| 32 std::string ReportOpenError(int32_t open_flags) { |
| 33 static const char* kFlagNames[] = { |
| 34 "PP_FILEOPENFLAG_READ", |
| 35 "PP_FILEOPENFLAG_WRITE", |
| 36 "PP_FILEOPENFLAG_CREATE", |
| 37 "PP_FILEOPENFLAG_TRUNCATE", |
| 38 "PP_FILEOPENFLAG_EXCLUSIVE" |
| 39 }; |
| 40 |
| 41 std::string result = "FileIO:Open had unexpected behavior with flags: "; |
| 42 bool first_flag = true; |
| 43 for (int32_t mask = 1, index = 0; mask <= PP_FILEOPENFLAG_EXCLUSIVE; |
| 44 mask <<= 1, ++index) { |
| 45 if (mask & open_flags) { |
| 46 if (first_flag) { |
| 47 first_flag = false; |
| 48 } else { |
| 49 result += " | "; |
| 50 } |
| 51 result += kFlagNames[index]; |
| 52 } |
| 53 } |
| 54 if (first_flag) |
| 55 result += "[None]"; |
| 56 |
| 57 return result; |
| 58 } |
| 59 |
32 int32_t ReadEntireFile(PP_Instance instance, | 60 int32_t ReadEntireFile(PP_Instance instance, |
33 pp::FileIO_Dev* file_io, | 61 pp::FileIO_Dev* file_io, |
34 int32_t offset, | 62 int32_t offset, |
35 std::string* data) { | 63 std::string* data) { |
36 TestCompletionCallback callback(instance); | 64 TestCompletionCallback callback(instance); |
37 char buf[256]; | 65 char buf[256]; |
38 int32_t read_offset = offset; | 66 int32_t read_offset = offset; |
39 | 67 |
40 for (;;) { | 68 for (;;) { |
41 int32_t rv = file_io->Read(read_offset, buf, sizeof(buf), callback); | 69 int32_t rv = file_io->Read(read_offset, buf, sizeof(buf), callback); |
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
96 TestCompletionCallback callback(instance_->pp_instance()); | 124 TestCompletionCallback callback(instance_->pp_instance()); |
97 | 125 |
98 pp::FileSystem_Dev file_system(instance_, PP_FILESYSTEMTYPE_LOCALTEMPORARY); | 126 pp::FileSystem_Dev file_system(instance_, PP_FILESYSTEMTYPE_LOCALTEMPORARY); |
99 pp::FileRef_Dev file_ref(file_system, "/file_open"); | 127 pp::FileRef_Dev file_ref(file_system, "/file_open"); |
100 int32_t rv = file_system.Open(1024, callback); | 128 int32_t rv = file_system.Open(1024, callback); |
101 if (rv == PP_OK_COMPLETIONPENDING) | 129 if (rv == PP_OK_COMPLETIONPENDING) |
102 rv = callback.WaitForResult(); | 130 rv = callback.WaitForResult(); |
103 if (rv != PP_OK) | 131 if (rv != PP_OK) |
104 return ReportError("FileSystem::Open", rv); | 132 return ReportError("FileSystem::Open", rv); |
105 | 133 |
106 pp::FileIO_Dev file_io(instance_); | 134 std::string result; |
107 rv = file_io.Open(file_ref, PP_FILEOPENFLAG_CREATE, callback); | 135 result = MatchOpenExpectation(&file_system, PP_FILEOPENFLAG_READ, |
108 if (rv == PP_OK_COMPLETIONPENDING) | 136 false, // invalid_combination |
109 rv = callback.WaitForResult(); | 137 false, // create_if_not_exist |
110 if (rv != PP_OK) | 138 true, // open_if_exist |
111 return ReportError("FileIO::Open", rv); | 139 false); // truncate_if_exist |
| 140 if (!result.empty()) |
| 141 return result; |
112 | 142 |
113 // Try opening a file that doesn't exist. | 143 // Test the behavior of the power set of |
114 pp::FileRef_Dev nonexistent_file_ref(file_system, "/nonexistent_file"); | 144 // { PP_FILEOPENFLAG_CREATE, |
115 pp::FileIO_Dev nonexistent_file_io(instance_); | 145 // PP_FILEOPENFLAG_TRUNCATE, |
116 rv = nonexistent_file_io.Open( | 146 // PP_FILEOPENFLAG_EXCLUSIVE }. |
117 nonexistent_file_ref, PP_FILEOPENFLAG_READ, callback); | 147 |
118 if (rv == PP_OK_COMPLETIONPENDING) | 148 // First of all, none of them are specificed. |
119 rv = callback.WaitForResult(); | 149 result = MatchOpenExpectation(&file_system, PP_FILEOPENFLAG_WRITE, |
120 if (rv != PP_ERROR_FILENOTFOUND) | 150 false, // invalid_combination |
121 return ReportError("FileIO::Open", rv); | 151 false, // create_if_not_exist |
| 152 true, // open_if_exist |
| 153 false); // truncate_if_exist |
| 154 if (!result.empty()) |
| 155 return result; |
| 156 |
| 157 result = MatchOpenExpectation(&file_system, |
| 158 PP_FILEOPENFLAG_WRITE | |
| 159 PP_FILEOPENFLAG_CREATE, |
| 160 false, // invalid_combination |
| 161 true, // create_if_not_exist |
| 162 true, // open_if_exist |
| 163 false); // truncate_if_exist |
| 164 if (!result.empty()) |
| 165 return result; |
| 166 |
| 167 result = MatchOpenExpectation(&file_system, |
| 168 PP_FILEOPENFLAG_WRITE | |
| 169 PP_FILEOPENFLAG_EXCLUSIVE, |
| 170 false, // invalid_combination |
| 171 false, // create_if_not_exist |
| 172 true, // open_if_exist |
| 173 false); // truncate_if_exist |
| 174 if (!result.empty()) |
| 175 return result; |
| 176 |
| 177 result = MatchOpenExpectation(&file_system, |
| 178 PP_FILEOPENFLAG_WRITE | |
| 179 PP_FILEOPENFLAG_TRUNCATE, |
| 180 false, // invalid_combination |
| 181 false, // create_if_not_exist |
| 182 true, // open_if_exist |
| 183 true); // truncate_if_exist |
| 184 if (!result.empty()) |
| 185 return result; |
| 186 |
| 187 result = MatchOpenExpectation(&file_system, |
| 188 PP_FILEOPENFLAG_WRITE | |
| 189 PP_FILEOPENFLAG_CREATE | |
| 190 PP_FILEOPENFLAG_EXCLUSIVE, |
| 191 false, // invalid_combination |
| 192 true, // create_if_not_exist |
| 193 false, // open_if_exist |
| 194 false); // truncate_if_exist |
| 195 if (!result.empty()) |
| 196 return result; |
| 197 |
| 198 result = MatchOpenExpectation(&file_system, |
| 199 PP_FILEOPENFLAG_WRITE | |
| 200 PP_FILEOPENFLAG_CREATE | |
| 201 PP_FILEOPENFLAG_TRUNCATE, |
| 202 false, // invalid_combination |
| 203 true, // create_if_not_exist |
| 204 true, // open_if_exist |
| 205 true); // truncate_if_exist |
| 206 if (!result.empty()) |
| 207 return result; |
| 208 |
| 209 result = MatchOpenExpectation(&file_system, |
| 210 PP_FILEOPENFLAG_WRITE | |
| 211 PP_FILEOPENFLAG_EXCLUSIVE | |
| 212 PP_FILEOPENFLAG_TRUNCATE, |
| 213 false, // invalid_combination |
| 214 false, // create_if_not_exist |
| 215 true, // open_if_exist |
| 216 true); // truncate_if_exist |
| 217 if (!result.empty()) |
| 218 return result; |
| 219 |
| 220 result = MatchOpenExpectation(&file_system, |
| 221 PP_FILEOPENFLAG_WRITE | |
| 222 PP_FILEOPENFLAG_CREATE | |
| 223 PP_FILEOPENFLAG_EXCLUSIVE | |
| 224 PP_FILEOPENFLAG_TRUNCATE, |
| 225 false, // invalid_combination |
| 226 true, // create_if_not_exist |
| 227 false, // open_if_exist |
| 228 false); // truncate_if_exist |
| 229 if (!result.empty()) |
| 230 return result; |
| 231 |
| 232 // Invalid combination: PP_FILEOPENFLAG_TRUNCATE without |
| 233 // PP_FILEOPENFLAG_WRITE. |
| 234 result = MatchOpenExpectation(&file_system, |
| 235 PP_FILEOPENFLAG_READ | |
| 236 PP_FILEOPENFLAG_TRUNCATE, |
| 237 true, // invalid_combination |
| 238 false, // create_if_not_exist |
| 239 false, // open_if_exist |
| 240 false); // truncate_if_exist |
| 241 if (!result.empty()) |
| 242 return result; |
122 | 243 |
123 PASS(); | 244 PASS(); |
124 } | 245 } |
125 | 246 |
126 std::string TestFileIO::TestReadWriteSetLength() { | 247 std::string TestFileIO::TestReadWriteSetLength() { |
127 TestCompletionCallback callback(instance_->pp_instance()); | 248 TestCompletionCallback callback(instance_->pp_instance()); |
128 | 249 |
129 pp::FileSystem_Dev file_system(instance_, PP_FILESYSTEMTYPE_LOCALTEMPORARY); | 250 pp::FileSystem_Dev file_system(instance_, PP_FILESYSTEMTYPE_LOCALTEMPORARY); |
130 pp::FileRef_Dev file_ref(file_system, "/file_read_write_setlength"); | 251 pp::FileRef_Dev file_ref(file_system, "/file_read_write_setlength"); |
131 int32_t rv = file_system.Open(1024, callback); | 252 int32_t rv = file_system.Open(1024, callback); |
132 if (rv == PP_OK_COMPLETIONPENDING) | 253 if (rv == PP_OK_COMPLETIONPENDING) |
133 rv = callback.WaitForResult(); | 254 rv = callback.WaitForResult(); |
134 if (rv != PP_OK) | 255 if (rv != PP_OK) |
135 return ReportError("FileSystem::Open", rv); | 256 return ReportError("FileSystem::Open", rv); |
136 | 257 |
137 pp::FileIO_Dev file_io(instance_); | 258 pp::FileIO_Dev file_io(instance_); |
138 rv = file_io.Open(file_ref, | 259 rv = file_io.Open(file_ref, |
139 PP_FILEOPENFLAG_CREATE | | 260 PP_FILEOPENFLAG_CREATE | |
| 261 PP_FILEOPENFLAG_TRUNCATE | |
140 PP_FILEOPENFLAG_READ | | 262 PP_FILEOPENFLAG_READ | |
141 PP_FILEOPENFLAG_WRITE, | 263 PP_FILEOPENFLAG_WRITE, |
142 callback); | 264 callback); |
143 if (rv == PP_OK_COMPLETIONPENDING) | 265 if (rv == PP_OK_COMPLETIONPENDING) |
144 rv = callback.WaitForResult(); | 266 rv = callback.WaitForResult(); |
145 if (rv != PP_OK) | 267 if (rv != PP_OK) |
146 return ReportError("FileIO::Open", rv); | 268 return ReportError("FileIO::Open", rv); |
147 | 269 |
148 // Write something to the file. | 270 // Write something to the file. |
149 rv = WriteEntireBuffer(instance_->pp_instance(), &file_io, 0, "test_test"); | 271 rv = WriteEntireBuffer(instance_->pp_instance(), &file_io, 0, "test_test"); |
(...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
243 pp::FileSystem_Dev file_system(instance_, PP_FILESYSTEMTYPE_LOCALTEMPORARY); | 365 pp::FileSystem_Dev file_system(instance_, PP_FILESYSTEMTYPE_LOCALTEMPORARY); |
244 int32_t rv = file_system.Open(1024, callback); | 366 int32_t rv = file_system.Open(1024, callback); |
245 if (rv == PP_OK_COMPLETIONPENDING) | 367 if (rv == PP_OK_COMPLETIONPENDING) |
246 rv = callback.WaitForResult(); | 368 rv = callback.WaitForResult(); |
247 if (rv != PP_OK) | 369 if (rv != PP_OK) |
248 return ReportError("FileSystem::Open", rv); | 370 return ReportError("FileSystem::Open", rv); |
249 | 371 |
250 pp::FileRef_Dev file_ref(file_system, "/file_touch"); | 372 pp::FileRef_Dev file_ref(file_system, "/file_touch"); |
251 pp::FileIO_Dev file_io(instance_); | 373 pp::FileIO_Dev file_io(instance_); |
252 rv = file_io.Open(file_ref, | 374 rv = file_io.Open(file_ref, |
253 PP_FILEOPENFLAG_CREATE | PP_FILEOPENFLAG_WRITE, | 375 PP_FILEOPENFLAG_CREATE | |
| 376 PP_FILEOPENFLAG_TRUNCATE | |
| 377 PP_FILEOPENFLAG_WRITE, |
254 callback); | 378 callback); |
255 if (rv == PP_OK_COMPLETIONPENDING) | 379 if (rv == PP_OK_COMPLETIONPENDING) |
256 rv = callback.WaitForResult(); | 380 rv = callback.WaitForResult(); |
257 if (rv != PP_OK) | 381 if (rv != PP_OK) |
258 return ReportError("FileIO::Open", rv); | 382 return ReportError("FileIO::Open", rv); |
259 | 383 |
260 // Write some data to have a non-zero file size. | 384 // Write some data to have a non-zero file size. |
261 rv = file_io.Write(0, "test", 4, callback); | 385 rv = file_io.Write(0, "test", 4, callback); |
262 if (rv == PP_OK_COMPLETIONPENDING) | 386 if (rv == PP_OK_COMPLETIONPENDING) |
263 rv = callback.WaitForResult(); | 387 rv = callback.WaitForResult(); |
(...skipping 225 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
489 return ReportError("FileIO::Flush", rv); | 613 return ReportError("FileIO::Flush", rv); |
490 } | 614 } |
491 } | 615 } |
492 | 616 |
493 // TODO(viettrungluu): Also test that Close() aborts callbacks. | 617 // TODO(viettrungluu): Also test that Close() aborts callbacks. |
494 // crbug.com/69457 | 618 // crbug.com/69457 |
495 | 619 |
496 PASS(); | 620 PASS(); |
497 } | 621 } |
498 | 622 |
| 623 std::string TestFileIO::MatchOpenExpectation(pp::FileSystem_Dev* file_system, |
| 624 size_t open_flags, |
| 625 bool invalid_combination, |
| 626 bool create_if_not_exist, |
| 627 bool open_if_exist, |
| 628 bool truncate_if_exist) { |
| 629 TestCompletionCallback callback(instance_->pp_instance()); |
| 630 |
| 631 pp::FileRef_Dev existent_file_ref( |
| 632 *file_system, "/match_open_expectation_existent_non_empty_file"); |
| 633 pp::FileRef_Dev nonexistent_file_ref( |
| 634 *file_system, "/match_open_expectation_nonexistent_file"); |
| 635 |
| 636 // Setup files for test. |
| 637 { |
| 638 int32_t rv = existent_file_ref.Delete(callback); |
| 639 if (rv == PP_OK_COMPLETIONPENDING) |
| 640 rv = callback.WaitForResult(); |
| 641 if (rv != PP_OK && rv != PP_ERROR_FILENOTFOUND) |
| 642 return ReportError("FileRef::Delete", rv); |
| 643 |
| 644 rv = nonexistent_file_ref.Delete(callback); |
| 645 if (rv == PP_OK_COMPLETIONPENDING) |
| 646 rv = callback.WaitForResult(); |
| 647 if (rv != PP_OK && rv != PP_ERROR_FILENOTFOUND) |
| 648 return ReportError("FileRef::Delete", rv); |
| 649 |
| 650 pp::FileIO_Dev existent_file_io(instance_); |
| 651 rv = existent_file_io.Open(existent_file_ref, |
| 652 PP_FILEOPENFLAG_CREATE | PP_FILEOPENFLAG_WRITE, |
| 653 callback); |
| 654 if (rv == PP_OK_COMPLETIONPENDING) |
| 655 rv = callback.WaitForResult(); |
| 656 if (rv != PP_OK) |
| 657 return ReportError("FileIO::Open", rv); |
| 658 |
| 659 rv = WriteEntireBuffer(instance_->pp_instance(), &existent_file_io, 0, |
| 660 "foobar"); |
| 661 if (rv != PP_OK) |
| 662 return ReportError("FileIO::Write", rv); |
| 663 } |
| 664 |
| 665 pp::FileIO_Dev existent_file_io(instance_); |
| 666 int32_t rv = existent_file_io.Open(existent_file_ref, open_flags, callback); |
| 667 if (rv == PP_OK_COMPLETIONPENDING) |
| 668 rv = callback.WaitForResult(); |
| 669 if ((invalid_combination && rv == PP_OK) || |
| 670 (!invalid_combination && ((rv == PP_OK) != open_if_exist))) { |
| 671 return ReportOpenError(open_flags); |
| 672 } |
| 673 |
| 674 if (!invalid_combination && open_if_exist) { |
| 675 PP_FileInfo_Dev info; |
| 676 rv = existent_file_io.Query(&info, callback); |
| 677 if (rv == PP_OK_COMPLETIONPENDING) |
| 678 rv = callback.WaitForResult(); |
| 679 if (rv != PP_OK) |
| 680 return ReportError("FileIO::Query", rv); |
| 681 |
| 682 if (truncate_if_exist != (info.size == 0)) |
| 683 return ReportOpenError(open_flags); |
| 684 } |
| 685 |
| 686 pp::FileIO_Dev nonexistent_file_io(instance_); |
| 687 rv = nonexistent_file_io.Open(nonexistent_file_ref, open_flags, callback); |
| 688 if (rv == PP_OK_COMPLETIONPENDING) |
| 689 rv = callback.WaitForResult(); |
| 690 if ((invalid_combination && rv == PP_OK) || |
| 691 (!invalid_combination && ((rv == PP_OK) != create_if_not_exist))) { |
| 692 return ReportOpenError(open_flags); |
| 693 } |
| 694 |
| 695 return std::string(); |
| 696 } |
| 697 |
499 // TODO(viettrungluu): Test Close(). crbug.com/69457 | 698 // TODO(viettrungluu): Test Close(). crbug.com/69457 |
OLD | NEW |