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
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
|
// Copyright 2014 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_mapping.h"
#include <string.h>
#include <limits>
#include <string>
#include "ppapi/c/pp_errors.h"
#include "ppapi/c/ppb_file_io.h"
#include "ppapi/c/ppb_file_mapping.h"
#include "ppapi/cpp/file_io.h"
#include "ppapi/cpp/file_ref.h"
#include "ppapi/cpp/file_system.h"
#include "ppapi/cpp/instance.h"
#include "ppapi/cpp/module.h"
#include "ppapi/tests/test_utils.h"
REGISTER_TEST_CASE(FileMapping);
namespace {
// TODO(dmichael): Move these to test_utils so we can share them?
int32_t ReadEntireFile(PP_Instance instance,
pp::FileIO* file_io,
int32_t offset,
std::string* data,
CallbackType callback_type) {
TestCompletionCallback callback(instance, callback_type);
char buf[256];
int32_t read_offset = offset;
for (;;) {
callback.WaitForResult(
file_io->Read(read_offset, buf, sizeof(buf), callback.GetCallback()));
if (callback.result() < 0)
return callback.result();
if (callback.result() == 0)
break;
read_offset += callback.result();
data->append(buf, callback.result());
}
return PP_OK;
}
int32_t WriteEntireBuffer(PP_Instance instance,
pp::FileIO* file_io,
int32_t offset,
const std::string& data,
CallbackType callback_type) {
TestCompletionCallback callback(instance, callback_type);
int32_t write_offset = offset;
const char* buf = data.c_str();
int32_t size = data.size();
while (write_offset < offset + size) {
callback.WaitForResult(file_io->Write(write_offset,
&buf[write_offset - offset],
size - write_offset + offset,
callback.GetCallback()));
if (callback.result() < 0)
return callback.result();
if (callback.result() == 0)
return PP_ERROR_FAILED;
write_offset += callback.result();
}
callback.WaitForResult(file_io->Flush(callback.GetCallback()));
return callback.result();
}
} // namespace
std::string TestFileMapping::MapAndCheckResults(uint32_t prot,
uint32_t flags) {
TestCompletionCallback callback(instance_->pp_instance(), callback_type());
pp::FileSystem file_system(instance_, PP_FILESYSTEMTYPE_LOCALTEMPORARY);
pp::FileRef file_ref(file_system, "/mapped_file");
callback.WaitForResult(file_system.Open(1024, callback.GetCallback()));
ASSERT_EQ(PP_OK, callback.result());
const int64_t page_size =
file_mapping_if_->GetMapPageSize(instance_->pp_instance());
const int64_t kNumPages = 4;
// Make a string that's big enough that it spans all of the first |n-1| pages,
// plus a little bit of the |nth| page.
std::string file_contents((page_size * (kNumPages - 1)) + 128, 'a');
pp::FileIO file_io(instance_);
callback.WaitForResult(file_io.Open(file_ref,
PP_FILEOPENFLAG_CREATE |
PP_FILEOPENFLAG_TRUNCATE |
PP_FILEOPENFLAG_READ |
PP_FILEOPENFLAG_WRITE,
callback.GetCallback()));
ASSERT_EQ(PP_OK, callback.result());
ASSERT_EQ(PP_OK, WriteEntireBuffer(instance_->pp_instance(),
&file_io,
0,
file_contents,
callback_type()));
// TODO(dmichael): Use C++ interface.
void* address = NULL;
callback.WaitForResult(
file_mapping_if_->Map(
instance_->pp_instance(),
file_io.pp_resource(),
kNumPages * page_size,
prot,
flags,
0,
&address,
callback.GetCallback().pp_completion_callback()));
CHECK_CALLBACK_BEHAVIOR(callback);
ASSERT_EQ(PP_OK, callback.result());
ASSERT_NE(NULL, address);
if (prot & PP_FILEMAPPROTECTION_READ) {
// Make sure we can read.
std::string mapped_data(static_cast<char*>(address), file_contents.size());
// The initial data should match.
ASSERT_EQ(file_contents, mapped_data);
// Now write some data and flush it.
const std::string file_contents2(file_contents.size(), 'x');
ASSERT_EQ(PP_OK, WriteEntireBuffer(instance_->pp_instance(),
&file_io,
0,
file_contents2,
callback_type()));
// If the region was mapped SHARED, it should get updated.
std::string mapped_data2(static_cast<char*>(address), file_contents.size());
if (flags & PP_FILEMAPFLAG_SHARED)
ASSERT_EQ(file_contents2, mapped_data2);
// In POSIX, it is unspecified in the PRIVATE case whether changes to the
// file are visible to the mapped region. So we can't really test anything
// here in that case.
// TODO(dmichael): Make sure our Pepper documentation reflects this.
}
if (prot & PP_FILEMAPPROTECTION_WRITE) {
std::string old_file_contents;
ASSERT_EQ(PP_OK, ReadEntireFile(instance_->pp_instance(),
&file_io,
0,
&old_file_contents,
callback_type()));
// Write something else to the mapped region, then unmap, and see if it
// gets written to the file. (Note we have to Unmap to make sure that the
// write is committed).
memset(address, 'y', file_contents.size());
// Note, we might not have read access to the mapped region here, so we
// make a string with the same contents without actually reading.
std::string mapped_data3(file_contents.size(), 'y');
callback.WaitForResult(
file_mapping_if_->Unmap(
instance_->pp_instance(), address, file_contents.size(),
callback.GetCallback().pp_completion_callback()));
CHECK_CALLBACK_BEHAVIOR(callback);
ASSERT_EQ(PP_OK, callback.result());
std::string new_file_contents;
ASSERT_EQ(PP_OK, ReadEntireFile(instance_->pp_instance(),
&file_io,
0,
&new_file_contents,
callback_type()));
// Sanity-check that the data we wrote isn't the same as what was already
// there, otherwise our test is invalid.
ASSERT_NE(mapped_data3, old_file_contents);
// If it's SHARED, the file should match what we wrote to the mapped region.
// Otherwise, it should not have changed.
if (flags & PP_FILEMAPFLAG_SHARED)
ASSERT_EQ(mapped_data3, new_file_contents);
else
ASSERT_EQ(old_file_contents, new_file_contents);
} else {
// We didn't do the "WRITE" test, but we still want to Unmap.
callback.WaitForResult(
file_mapping_if_->Unmap(
instance_->pp_instance(), address, file_contents.size(),
callback.GetCallback().pp_completion_callback()));
CHECK_CALLBACK_BEHAVIOR(callback);
ASSERT_EQ(PP_OK, callback.result());
}
PASS();
}
bool TestFileMapping::Init() {
// TODO(dmichael): Use unversioned string when this goes to stable?
file_mapping_if_ = static_cast<const PPB_FileMapping_0_1*>(
pp::Module::Get()->GetBrowserInterface(PPB_FILEMAPPING_INTERFACE_0_1));
return !!file_mapping_if_ && CheckTestingInterface() &&
EnsureRunningOverHTTP();
}
void TestFileMapping::RunTests(const std::string& filter) {
RUN_CALLBACK_TEST(TestFileMapping, BadParameters, filter);
RUN_CALLBACK_TEST(TestFileMapping, Map, filter);
RUN_CALLBACK_TEST(TestFileMapping, PartialRegions, filter);
}
std::string TestFileMapping::TestBadParameters() {
TestCompletionCallback callback(instance_->pp_instance(), callback_type());
pp::FileSystem file_system(instance_, PP_FILESYSTEMTYPE_LOCALTEMPORARY);
pp::FileRef file_ref(file_system, "/mapped_file");
callback.WaitForResult(file_system.Open(1024, callback.GetCallback()));
ASSERT_EQ(PP_OK, callback.result());
const int64_t page_size =
file_mapping_if_->GetMapPageSize(instance_->pp_instance());
// const int64_t kNumPages = 4;
// Make a string that's big enough that it spans 3 pages, plus a little extra.
//std::string file_contents((page_size * (kNumPages - 1)) + 128, 'a');
std::string file_contents(page_size, 'a');
pp::FileIO file_io(instance_);
callback.WaitForResult(file_io.Open(file_ref,
PP_FILEOPENFLAG_CREATE |
PP_FILEOPENFLAG_TRUNCATE |
PP_FILEOPENFLAG_READ |
PP_FILEOPENFLAG_WRITE,
callback.GetCallback()));
ASSERT_EQ(PP_OK, callback.result());
ASSERT_EQ(PP_OK, WriteEntireBuffer(instance_->pp_instance(),
&file_io,
0,
file_contents,
callback_type()));
// Bad instance.
void* address = NULL;
callback.WaitForResult(
file_mapping_if_->Map(
PP_Instance(0xbadbad),
file_io.pp_resource(),
page_size,
PP_FILEMAPPROTECTION_READ,
PP_FILEMAPFLAG_PRIVATE,
0,
&address,
callback.GetCallback().pp_completion_callback()));
CHECK_CALLBACK_BEHAVIOR(callback);
ASSERT_EQ(PP_ERROR_BADARGUMENT, callback.result());
ASSERT_EQ(NULL, address);
// Bad resource.
callback.WaitForResult(
file_mapping_if_->Map(
instance_->pp_instance(),
PP_Resource(0xbadbad),
page_size,
PP_FILEMAPPROTECTION_READ,
PP_FILEMAPFLAG_PRIVATE,
0,
&address,
callback.GetCallback().pp_completion_callback()));
CHECK_CALLBACK_BEHAVIOR(callback);
ASSERT_EQ(PP_ERROR_BADARGUMENT, callback.result());
ASSERT_EQ(NULL, address);
// Length too big.
callback.WaitForResult(
file_mapping_if_->Map(
instance_->pp_instance(),
file_io.pp_resource(),
std::numeric_limits<int64_t>::max(),
PP_FILEMAPPROTECTION_READ,
PP_FILEMAPFLAG_PRIVATE,
0,
&address,
callback.GetCallback().pp_completion_callback()));
CHECK_CALLBACK_BEHAVIOR(callback);
ASSERT_EQ(PP_ERROR_NOMEMORY, callback.result());
ASSERT_EQ(NULL, address);
// Length too small.
callback.WaitForResult(
file_mapping_if_->Map(
instance_->pp_instance(),
file_io.pp_resource(),
-1,
PP_FILEMAPPROTECTION_READ,
PP_FILEMAPFLAG_PRIVATE,
0,
&address,
callback.GetCallback().pp_completion_callback()));
CHECK_CALLBACK_BEHAVIOR(callback);
ASSERT_EQ(PP_ERROR_BADARGUMENT, callback.result());
ASSERT_EQ(NULL, address);
// TODO(dmichael): Check & test length that is not a multiple of page size???
// Bad protection.
callback.WaitForResult(
file_mapping_if_->Map(
instance_->pp_instance(),
file_io.pp_resource(),
page_size,
~PP_FILEMAPPROTECTION_READ,
PP_FILEMAPFLAG_PRIVATE,
0,
&address,
callback.GetCallback().pp_completion_callback()));
CHECK_CALLBACK_BEHAVIOR(callback);
ASSERT_EQ(PP_ERROR_BADARGUMENT, callback.result());
ASSERT_EQ(NULL, address);
// No flags.
callback.WaitForResult(
file_mapping_if_->Map(
instance_->pp_instance(),
file_io.pp_resource(),
page_size,
PP_FILEMAPPROTECTION_READ,
0,
0,
&address,
callback.GetCallback().pp_completion_callback()));
CHECK_CALLBACK_BEHAVIOR(callback);
ASSERT_EQ(PP_ERROR_BADARGUMENT, callback.result());
ASSERT_EQ(NULL, address);
// Both flags.
callback.WaitForResult(
file_mapping_if_->Map(
instance_->pp_instance(),
file_io.pp_resource(),
page_size,
PP_FILEMAPPROTECTION_READ,
PP_FILEMAPFLAG_SHARED | PP_FILEMAPFLAG_PRIVATE,
0,
&address,
callback.GetCallback().pp_completion_callback()));
CHECK_CALLBACK_BEHAVIOR(callback);
ASSERT_EQ(PP_ERROR_BADARGUMENT, callback.result());
ASSERT_EQ(NULL, address);
// Bad flags.
callback.WaitForResult(
file_mapping_if_->Map(
instance_->pp_instance(),
file_io.pp_resource(),
page_size,
PP_FILEMAPPROTECTION_READ,
~PP_FILEMAPFLAG_SHARED,
0,
&address,
callback.GetCallback().pp_completion_callback()));
CHECK_CALLBACK_BEHAVIOR(callback);
ASSERT_EQ(PP_ERROR_BADARGUMENT, callback.result());
ASSERT_EQ(NULL, address);
// Bad offset; not a multiple of page size.
callback.WaitForResult(
file_mapping_if_->Map(
instance_->pp_instance(),
file_io.pp_resource(),
page_size,
PP_FILEMAPPROTECTION_READ,
~PP_FILEMAPFLAG_SHARED,
1,
&address,
callback.GetCallback().pp_completion_callback()));
CHECK_CALLBACK_BEHAVIOR(callback);
ASSERT_EQ(PP_ERROR_BADARGUMENT, callback.result());
ASSERT_EQ(NULL, address);
// Unmap NULL.
callback.WaitForResult(
file_mapping_if_->Unmap(
instance_->pp_instance(),
NULL,
page_size,
callback.GetCallback().pp_completion_callback()));
CHECK_CALLBACK_BEHAVIOR(callback);
ASSERT_EQ(PP_ERROR_BADARGUMENT, callback.result());
// Unmap bad address.
callback.WaitForResult(
file_mapping_if_->Unmap(
instance_->pp_instance(),
reinterpret_cast<const void*>(0xdeadbeef),
page_size,
callback.GetCallback().pp_completion_callback()));
CHECK_CALLBACK_BEHAVIOR(callback);
ASSERT_EQ(PP_ERROR_BADARGUMENT, callback.result());
PASS();
}
std::string TestFileMapping::TestMap() {
ASSERT_SUBTEST_SUCCESS(MapAndCheckResults(PP_FILEMAPPROTECTION_READ,
PP_FILEMAPFLAG_SHARED));
ASSERT_SUBTEST_SUCCESS(MapAndCheckResults(PP_FILEMAPPROTECTION_WRITE,
PP_FILEMAPFLAG_SHARED));
ASSERT_SUBTEST_SUCCESS(
MapAndCheckResults(PP_FILEMAPPROTECTION_WRITE | PP_FILEMAPPROTECTION_READ,
PP_FILEMAPFLAG_SHARED));
ASSERT_SUBTEST_SUCCESS(MapAndCheckResults(PP_FILEMAPPROTECTION_READ,
PP_FILEMAPFLAG_PRIVATE));
ASSERT_SUBTEST_SUCCESS(MapAndCheckResults(PP_FILEMAPPROTECTION_WRITE,
PP_FILEMAPFLAG_PRIVATE));
ASSERT_SUBTEST_SUCCESS(
MapAndCheckResults(PP_FILEMAPPROTECTION_WRITE | PP_FILEMAPPROTECTION_READ,
PP_FILEMAPFLAG_PRIVATE));
PASS();
}
std::string TestFileMapping::TestPartialRegions() {
TestCompletionCallback callback(instance_->pp_instance(), callback_type());
pp::FileSystem file_system(instance_, PP_FILESYSTEMTYPE_LOCALTEMPORARY);
pp::FileRef file_ref1(file_system, "/mapped_file1");
pp::FileRef file_ref2(file_system, "/mapped_file2");
callback.WaitForResult(file_system.Open(1024, callback.GetCallback()));
ASSERT_EQ(PP_OK, callback.result());
const int64_t page_size =
file_mapping_if_->GetMapPageSize(instance_->pp_instance());
const int64_t kNumPages = 3;
std::string file_contents1(kNumPages * page_size, 'a');
pp::FileIO file_io1(instance_);
callback.WaitForResult(file_io1.Open(file_ref1,
PP_FILEOPENFLAG_CREATE |
PP_FILEOPENFLAG_TRUNCATE |
PP_FILEOPENFLAG_READ |
PP_FILEOPENFLAG_WRITE,
callback.GetCallback()));
ASSERT_EQ(PP_OK, callback.result());
ASSERT_EQ(PP_OK, WriteEntireBuffer(instance_->pp_instance(),
&file_io1,
0,
file_contents1,
callback_type()));
// TODO(dmichael): Use C++ interface.
void* address = NULL;
callback.WaitForResult(
file_mapping_if_->Map(
instance_->pp_instance(),
file_io1.pp_resource(),
kNumPages * page_size,
PP_FILEMAPPROTECTION_WRITE | PP_FILEMAPPROTECTION_READ,
PP_FILEMAPFLAG_SHARED,
0,
&address,
callback.GetCallback().pp_completion_callback()));
CHECK_CALLBACK_BEHAVIOR(callback);
ASSERT_EQ(PP_OK, callback.result());
ASSERT_NE(NULL, address);
// Unmap only the middle page.
void* address_of_middle_page =
reinterpret_cast<void*>(reinterpret_cast<uintptr_t>(address) + page_size);
callback.WaitForResult(
file_mapping_if_->Unmap(
instance_->pp_instance(),
address_of_middle_page,
page_size,
callback.GetCallback().pp_completion_callback()));
CHECK_CALLBACK_BEHAVIOR(callback);
ASSERT_EQ(PP_OK, callback.result());
// Write another file, map it in to the middle hole that was left above.
pp::FileIO file_io2(instance_);
callback.WaitForResult(file_io2.Open(file_ref2,
PP_FILEOPENFLAG_CREATE |
PP_FILEOPENFLAG_TRUNCATE |
PP_FILEOPENFLAG_READ |
PP_FILEOPENFLAG_WRITE,
callback.GetCallback()));
ASSERT_EQ(PP_OK, callback.result());
// This second file will have 1 page worth of data.
std::string file_contents2(page_size, 'b');
ASSERT_EQ(PP_OK, WriteEntireBuffer(instance_->pp_instance(),
&file_io2,
0,
file_contents2,
callback_type()));
callback.WaitForResult(
file_mapping_if_->Map(
instance_->pp_instance(),
file_io2.pp_resource(),
page_size,
PP_FILEMAPPROTECTION_WRITE | PP_FILEMAPPROTECTION_READ,
PP_FILEMAPFLAG_SHARED | PP_FILEMAPFLAG_FIXED,
0,
&address_of_middle_page,
callback.GetCallback().pp_completion_callback()));
CHECK_CALLBACK_BEHAVIOR(callback);
ASSERT_EQ(PP_OK, callback.result());
PASS();
// Write something else to the mapped region, then unmap, and see if it
// gets written to both files. (Note we have to Unmap to make sure that the
// write is committed).
memset(address, 'c', kNumPages * page_size);
callback.WaitForResult(
file_mapping_if_->Unmap(
instance_->pp_instance(), address, kNumPages * page_size,
callback.GetCallback().pp_completion_callback()));
CHECK_CALLBACK_BEHAVIOR(callback);
ASSERT_EQ(PP_OK, callback.result());
// The first and third page should have been written with 'c', but the
// second page should be untouched.
std::string expected_file_contents1 = std::string(page_size, 'c') +
std::string(page_size, 'a') +
std::string(page_size, 'c');
std::string new_file_contents1;
ASSERT_EQ(PP_OK, ReadEntireFile(instance_->pp_instance(),
&file_io1,
0,
&new_file_contents1,
callback_type()));
ASSERT_EQ(expected_file_contents1, new_file_contents1);
// The second file should have been entirely over-written.
std::string expected_file_contents2 = std::string(page_size, 'c');
std::string new_file_contents2;
ASSERT_EQ(PP_OK, ReadEntireFile(instance_->pp_instance(),
&file_io2,
0,
&new_file_contents2,
callback_type()));
ASSERT_EQ(expected_file_contents2, new_file_contents2);
// TODO(dmichael): Test non-zero offset
PASS();
}
|