1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
|
// Copyright (c) 2011 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "ppapi/tests/test_file_io.h"
#include <stdio.h>
#include <string.h>
#include "ppapi/c/pp_errors.h"
#include "ppapi/c/dev/ppb_file_io_dev.h"
#include "ppapi/c/dev/ppb_testing_dev.h"
#include "ppapi/cpp/dev/file_io_dev.h"
#include "ppapi/cpp/dev/file_ref_dev.h"
#include "ppapi/cpp/dev/file_system_dev.h"
#include "ppapi/cpp/instance.h"
#include "ppapi/cpp/module.h"
#include "ppapi/tests/test_utils.h"
#include "ppapi/tests/testing_instance.h"
REGISTER_TEST_CASE(FileIO);
namespace {
std::string ReportMismatch(const std::string& method_name,
const std::string& returned_result,
const std::string& expected_result) {
return method_name + " returned '" + returned_result + "'; '" +
expected_result + "' expected.";
}
int32_t ReadEntireFile(PP_Instance instance,
pp::FileIO_Dev* file_io,
int32_t offset,
std::string* data) {
TestCompletionCallback callback(instance);
char buf[256];
int32_t read_offset = offset;
for (;;) {
int32_t rv = file_io->Read(read_offset, buf, sizeof(buf), callback);
if (rv == PP_OK_COMPLETIONPENDING)
rv = callback.WaitForResult();
if (rv < 0)
return rv;
if (rv == 0)
break;
read_offset += rv;
data->append(buf, rv);
}
return PP_OK;
}
int32_t WriteEntireBuffer(PP_Instance instance,
pp::FileIO_Dev* file_io,
int32_t offset,
const std::string& data) {
TestCompletionCallback callback(instance);
int32_t write_offset = offset;
const char* buf = data.c_str();
int32_t size = data.size();
while (write_offset < offset + size) {
int32_t rv = file_io->Write(write_offset, &buf[write_offset - offset],
size - write_offset + offset, callback);
if (rv == PP_OK_COMPLETIONPENDING)
rv = callback.WaitForResult();
if (rv < 0)
return rv;
if (rv == 0)
return PP_ERROR_FAILED;
write_offset += rv;
}
return PP_OK;
}
} // namespace
bool TestFileIO::Init() {
return InitTestingInterface() && EnsureRunningOverHTTP();
}
void TestFileIO::RunTest() {
RUN_TEST(Open);
RUN_TEST(ReadWriteSetLength);
RUN_TEST(TouchQuery);
RUN_TEST(AbortCalls);
// TODO(viettrungluu): add tests:
// - that PP_ERROR_PENDING is correctly returned
// - that operations respect the file open modes (flags)
}
std::string TestFileIO::TestOpen() {
TestCompletionCallback callback(instance_->pp_instance());
pp::FileSystem_Dev file_system(instance_, PP_FILESYSTEMTYPE_LOCALTEMPORARY);
pp::FileRef_Dev file_ref(file_system, "/file_open");
int32_t rv = file_system.Open(1024, callback);
if (rv == PP_OK_COMPLETIONPENDING)
rv = callback.WaitForResult();
if (rv != PP_OK)
return ReportError("FileSystem::Open", rv);
pp::FileIO_Dev file_io(instance_);
rv = file_io.Open(file_ref, PP_FILEOPENFLAG_CREATE, callback);
if (rv == PP_OK_COMPLETIONPENDING)
rv = callback.WaitForResult();
if (rv != PP_OK)
return ReportError("FileIO::Open", rv);
// Try opening a file that doesn't exist.
pp::FileRef_Dev nonexistent_file_ref(file_system, "/nonexistent_file");
pp::FileIO_Dev nonexistent_file_io(instance_);
rv = nonexistent_file_io.Open(
nonexistent_file_ref, PP_FILEOPENFLAG_READ, callback);
if (rv == PP_OK_COMPLETIONPENDING)
rv = callback.WaitForResult();
if (rv != PP_ERROR_FILENOTFOUND)
return ReportError("FileIO::Open", rv);
PASS();
}
std::string TestFileIO::TestReadWriteSetLength() {
TestCompletionCallback callback(instance_->pp_instance());
pp::FileSystem_Dev file_system(instance_, PP_FILESYSTEMTYPE_LOCALTEMPORARY);
pp::FileRef_Dev file_ref(file_system, "/file_read_write_setlength");
int32_t rv = file_system.Open(1024, callback);
if (rv == PP_OK_COMPLETIONPENDING)
rv = callback.WaitForResult();
if (rv != PP_OK)
return ReportError("FileSystem::Open", rv);
pp::FileIO_Dev file_io(instance_);
rv = file_io.Open(file_ref,
PP_FILEOPENFLAG_CREATE |
PP_FILEOPENFLAG_READ |
PP_FILEOPENFLAG_WRITE,
callback);
if (rv == PP_OK_COMPLETIONPENDING)
rv = callback.WaitForResult();
if (rv != PP_OK)
return ReportError("FileIO::Open", rv);
// Write something to the file.
rv = WriteEntireBuffer(instance_->pp_instance(), &file_io, 0, "test_test");
if (rv != PP_OK)
return ReportError("FileIO::Write", rv);
// Read the entire file.
std::string read_buffer;
rv = ReadEntireFile(instance_->pp_instance(), &file_io, 0, &read_buffer);
if (rv != PP_OK)
return ReportError("FileIO::Read", rv);
if (read_buffer != "test_test")
return ReportMismatch("FileIO::Read", read_buffer, "test_test");
// Truncate the file.
rv = file_io.SetLength(4, callback);
if (rv == PP_OK_COMPLETIONPENDING)
rv = callback.WaitForResult();
if (rv != PP_OK)
return ReportError("FileIO::SetLength", rv);
// Check the file contents.
read_buffer.clear();
rv = ReadEntireFile(instance_->pp_instance(), &file_io, 0, &read_buffer);
if (rv != PP_OK)
return ReportError("FileIO::Read", rv);
if (read_buffer != "test")
return ReportMismatch("FileIO::Read", read_buffer, "test");
// Try to read past the end of the file.
read_buffer.clear();
rv = ReadEntireFile(instance_->pp_instance(), &file_io, 100, &read_buffer);
if (rv != PP_OK)
return ReportError("FileIO::Read", rv);
if (!read_buffer.empty())
return ReportMismatch("FileIO::Read", read_buffer, "<empty string>");
// Write past the end of the file. The file should be zero-padded.
rv = WriteEntireBuffer(instance_->pp_instance(), &file_io, 8, "test");
if (rv != PP_OK)
return ReportError("FileIO::Write", rv);
// Check the contents of the file.
read_buffer.clear();
rv = ReadEntireFile(instance_->pp_instance(), &file_io, 0, &read_buffer);
if (rv != PP_OK)
return ReportError("FileIO::Read", rv);
if (read_buffer != std::string("test\0\0\0\0test", 12))
return ReportMismatch("FileIO::Read", read_buffer,
std::string("test\0\0\0\0test", 12));
// Extend the file.
rv = file_io.SetLength(16, callback);
if (rv == PP_OK_COMPLETIONPENDING)
rv = callback.WaitForResult();
if (rv != PP_OK)
return ReportError("FileIO::SetLength", rv);
// Check the contents of the file.
read_buffer.clear();
rv = ReadEntireFile(instance_->pp_instance(), &file_io, 0, &read_buffer);
if (rv != PP_OK)
return ReportError("FileIO::Read", rv);
if (read_buffer != std::string("test\0\0\0\0test\0\0\0\0", 16))
return ReportMismatch("FileIO::Read", read_buffer,
std::string("test\0\0\0\0test\0\0\0\0", 16));
// Write in the middle of the file.
rv = WriteEntireBuffer(instance_->pp_instance(), &file_io, 4, "test");
if (rv != PP_OK)
return ReportError("FileIO::Write", rv);
// Check the contents of the file.
read_buffer.clear();
rv = ReadEntireFile(instance_->pp_instance(), &file_io, 0, &read_buffer);
if (rv != PP_OK)
return ReportError("FileIO::Read", rv);
if (read_buffer != std::string("testtesttest\0\0\0\0", 16))
return ReportMismatch("FileIO::Read", read_buffer,
std::string("testtesttest\0\0\0\0", 16));
// Read from the middle of the file.
read_buffer.clear();
rv = ReadEntireFile(instance_->pp_instance(), &file_io, 4, &read_buffer);
if (rv != PP_OK)
return ReportError("FileIO::Read", rv);
if (read_buffer != std::string("testtest\0\0\0\0", 12))
return ReportMismatch("FileIO::Read", read_buffer,
std::string("testtest\0\0\0\0", 12));
PASS();
}
std::string TestFileIO::TestTouchQuery() {
TestCompletionCallback callback(instance_->pp_instance());
pp::FileSystem_Dev file_system(instance_, PP_FILESYSTEMTYPE_LOCALTEMPORARY);
int32_t rv = file_system.Open(1024, callback);
if (rv == PP_OK_COMPLETIONPENDING)
rv = callback.WaitForResult();
if (rv != PP_OK)
return ReportError("FileSystem::Open", rv);
pp::FileRef_Dev file_ref(file_system, "/file_touch");
pp::FileIO_Dev file_io(instance_);
rv = file_io.Open(file_ref,
PP_FILEOPENFLAG_CREATE | PP_FILEOPENFLAG_WRITE,
callback);
if (rv == PP_OK_COMPLETIONPENDING)
rv = callback.WaitForResult();
if (rv != PP_OK)
return ReportError("FileIO::Open", rv);
// Write some data to have a non-zero file size.
rv = file_io.Write(0, "test", 4, callback);
if (rv == PP_OK_COMPLETIONPENDING)
rv = callback.WaitForResult();
if (rv != 4)
return ReportError("FileIO::Write", rv);
// last_access_time's granularity is 1 day
// last_modified_time's granularity is 2 seconds
const PP_Time last_access_time = 123 * 24 * 3600.0;
const PP_Time last_modified_time = 246.0;
rv = file_io.Touch(last_access_time, last_modified_time, callback);
if (rv == PP_OK_COMPLETIONPENDING)
rv = callback.WaitForResult();
if (rv != PP_OK)
return ReportError("FileSystem::Touch", rv);
PP_FileInfo_Dev info;
rv = file_io.Query(&info, callback);
if (rv == PP_OK_COMPLETIONPENDING)
rv = callback.WaitForResult();
if (rv != PP_OK)
return ReportError("FileSystem::Query", rv);
if ((info.size != 4) ||
(info.type != PP_FILETYPE_REGULAR) ||
(info.system_type != PP_FILESYSTEMTYPE_LOCALTEMPORARY) ||
(info.last_access_time != last_access_time) ||
(info.last_modified_time != last_modified_time))
return "FileSystem::Query() has returned bad data.";
// Call |Query()| again, to make sure it works a second time.
rv = file_io.Query(&info, callback);
if (rv == PP_OK_COMPLETIONPENDING)
rv = callback.WaitForResult();
if (rv != PP_OK)
return ReportError("FileSystem::Query", rv);
PASS();
}
std::string TestFileIO::TestAbortCalls() {
TestCompletionCallback callback(instance_->pp_instance());
pp::FileSystem_Dev file_system(instance_, PP_FILESYSTEMTYPE_LOCALTEMPORARY);
pp::FileRef_Dev file_ref(file_system, "/file_abort_calls");
int32_t rv = file_system.Open(1024, callback);
if (rv == PP_OK_COMPLETIONPENDING)
rv = callback.WaitForResult();
if (rv != PP_OK)
return ReportError("FileSystem::Open", rv);
// First, create a file which to do ops on.
{
pp::FileIO_Dev file_io(instance_);
rv = file_io.Open(file_ref,
PP_FILEOPENFLAG_CREATE | PP_FILEOPENFLAG_WRITE,
callback);
if (rv == PP_OK_COMPLETIONPENDING)
rv = callback.WaitForResult();
if (rv != PP_OK)
return ReportError("FileIO::Open", rv);
// N.B.: Should write at least 3 bytes.
rv = WriteEntireBuffer(instance_->pp_instance(),
&file_io,
0,
"foobarbazquux");
if (rv != PP_OK)
return ReportError("FileIO::Write", rv);
}
// Abort |Open()|.
{
callback.reset_run_count();
rv = pp::FileIO_Dev(instance_)
.Open(file_ref, PP_FILEOPENFLAG_READ,callback);
if (callback.run_count() > 0)
return "FileIO::Open ran callback synchronously.";
if (rv == PP_OK_COMPLETIONPENDING) {
rv = callback.WaitForResult();
if (rv != PP_ERROR_ABORTED)
return "FileIO::Open not aborted.";
} else if (rv != PP_OK) {
return ReportError("FileIO::Open", rv);
}
}
// Abort |Query()|.
{
PP_FileInfo_Dev info = { 0 };
{
pp::FileIO_Dev file_io(instance_);
rv = file_io.Open(file_ref, PP_FILEOPENFLAG_READ, callback);
if (rv == PP_OK_COMPLETIONPENDING)
rv = callback.WaitForResult();
if (rv != PP_OK)
return ReportError("FileIO::Open", rv);
callback.reset_run_count();
rv = file_io.Query(&info, callback);
} // Destroy |file_io|.
if (rv == PP_OK_COMPLETIONPENDING) {
// Save a copy and make sure |info| doesn't get written to.
PP_FileInfo_Dev info_copy;
memcpy(&info_copy, &info, sizeof(info));
rv = callback.WaitForResult();
if (rv != PP_ERROR_ABORTED)
return "FileIO::Query not aborted.";
if (memcmp(&info_copy, &info, sizeof(info)) != 0)
return "FileIO::Query wrote data after resource destruction.";
} else if (rv != PP_OK) {
return ReportError("FileIO::Query", rv);
}
}
// Abort |Touch()|.
{
{
pp::FileIO_Dev file_io(instance_);
rv = file_io.Open(file_ref, PP_FILEOPENFLAG_WRITE, callback);
if (rv == PP_OK_COMPLETIONPENDING)
rv = callback.WaitForResult();
if (rv != PP_OK)
return ReportError("FileIO::Open", rv);
callback.reset_run_count();
rv = file_io.Touch(0, 0, callback);
} // Destroy |file_io|.
if (rv == PP_OK_COMPLETIONPENDING) {
rv = callback.WaitForResult();
if (rv != PP_ERROR_ABORTED)
return "FileIO::Touch not aborted.";
} else if (rv != PP_OK) {
return ReportError("FileIO::Touch", rv);
}
}
// Abort |Read()|.
{
char buf[3] = { 0 };
{
pp::FileIO_Dev file_io(instance_);
rv = file_io.Open(file_ref, PP_FILEOPENFLAG_READ, callback);
if (rv == PP_OK_COMPLETIONPENDING)
rv = callback.WaitForResult();
if (rv != PP_OK)
return ReportError("FileIO::Open", rv);
callback.reset_run_count();
rv = file_io.Read(0, buf, sizeof(buf), callback);
} // Destroy |file_io|.
if (rv == PP_OK_COMPLETIONPENDING) {
// Save a copy and make sure |buf| doesn't get written to.
char buf_copy[3];
memcpy(&buf_copy, &buf, sizeof(buf));
rv = callback.WaitForResult();
if (rv != PP_ERROR_ABORTED)
return "FileIO::Read not aborted.";
if (memcmp(&buf_copy, &buf, sizeof(buf)) != 0)
return "FileIO::Read wrote data after resource destruction.";
} else if (rv != PP_OK) {
return ReportError("FileIO::Read", rv);
}
}
// Abort |Write()|.
{
char buf[3] = { 0 };
{
pp::FileIO_Dev file_io(instance_);
rv = file_io.Open(file_ref, PP_FILEOPENFLAG_READ, callback);
if (rv == PP_OK_COMPLETIONPENDING)
rv = callback.WaitForResult();
if (rv != PP_OK)
return ReportError("FileIO::Open", rv);
callback.reset_run_count();
rv = file_io.Write(0, buf, sizeof(buf), callback);
} // Destroy |file_io|.
if (rv == PP_OK_COMPLETIONPENDING) {
rv = callback.WaitForResult();
if (rv != PP_ERROR_ABORTED)
return "FileIO::Write not aborted.";
} else if (rv != PP_OK) {
return ReportError("FileIO::Write", rv);
}
}
// Abort |SetLength()|.
{
{
pp::FileIO_Dev file_io(instance_);
rv = file_io.Open(file_ref, PP_FILEOPENFLAG_READ, callback);
if (rv == PP_OK_COMPLETIONPENDING)
rv = callback.WaitForResult();
if (rv != PP_OK)
return ReportError("FileIO::Open", rv);
callback.reset_run_count();
rv = file_io.SetLength(3, callback);
} // Destroy |file_io|.
if (rv == PP_OK_COMPLETIONPENDING) {
rv = callback.WaitForResult();
if (rv != PP_ERROR_ABORTED)
return "FileIO::SetLength not aborted.";
} else if (rv != PP_OK) {
return ReportError("FileIO::SetLength", rv);
}
}
// Abort |Flush()|.
{
{
pp::FileIO_Dev file_io(instance_);
rv = file_io.Open(file_ref, PP_FILEOPENFLAG_READ, callback);
if (rv == PP_OK_COMPLETIONPENDING)
rv = callback.WaitForResult();
if (rv != PP_OK)
return ReportError("FileIO::Open", rv);
callback.reset_run_count();
rv = file_io.Flush(callback);
} // Destroy |file_io|.
if (rv == PP_OK_COMPLETIONPENDING) {
rv = callback.WaitForResult();
if (rv != PP_ERROR_ABORTED)
return "FileIO::Flush not aborted.";
} else if (rv != PP_OK) {
return ReportError("FileIO::Flush", rv);
}
}
// TODO(viettrungluu): Also test that Close() aborts callbacks.
// crbug.com/69457
PASS();
}
// TODO(viettrungluu): Test Close(). crbug.com/69457
|