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
|
// Copyright (c) 2010 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 "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::FileIO_Dev* file_io,
int32_t offset,
std::string* data) {
TestCompletionCallback callback;
char buf[256];
int32_t read_offset = offset;
for (;;) {
int32_t rv = file_io->Read(read_offset, buf, sizeof(buf), callback);
if (rv == PP_ERROR_WOULDBLOCK)
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::FileIO_Dev* file_io,
int32_t offset,
const std::string& data) {
TestCompletionCallback callback;
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_ERROR_WOULDBLOCK)
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);
}
std::string TestFileIO::TestOpen() {
TestCompletionCallback callback;
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_ERROR_WOULDBLOCK)
rv = callback.WaitForResult();
if (rv != PP_OK)
return ReportError("FileSystem::Open", rv);
pp::FileIO_Dev file_io;
rv = file_io.Open(file_ref, PP_FILEOPENFLAG_CREATE, callback);
if (rv == PP_ERROR_WOULDBLOCK)
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;
rv = nonexistent_file_io.Open(
nonexistent_file_ref, PP_FILEOPENFLAG_READ, callback);
if (rv == PP_ERROR_WOULDBLOCK)
rv = callback.WaitForResult();
if (rv != PP_ERROR_FILENOTFOUND)
return ReportError("FileIO::Open", rv);
return "";
}
std::string TestFileIO::TestReadWriteSetLength() {
TestCompletionCallback callback;
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_ERROR_WOULDBLOCK)
rv = callback.WaitForResult();
if (rv != PP_OK)
return ReportError("FileSystem::Open", rv);
pp::FileIO_Dev file_io;
rv = file_io.Open(file_ref,
PP_FILEOPENFLAG_CREATE |
PP_FILEOPENFLAG_READ |
PP_FILEOPENFLAG_WRITE,
callback);
if (rv == PP_ERROR_WOULDBLOCK)
rv = callback.WaitForResult();
if (rv != PP_OK)
return ReportError("FileIO::Open", rv);
// Write something to the file.
rv = WriteEntireBuffer(&file_io, 0, "test_test");
if (rv != PP_OK)
return ReportError("FileIO::Write", rv);
// Read the entire file.
std::string read_buffer;
rv = ReadEntireFile(&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_ERROR_WOULDBLOCK)
rv = callback.WaitForResult();
if (rv != PP_OK)
return ReportError("FileIO::SetLength", rv);
// Check the file contents.
read_buffer.clear();
rv = ReadEntireFile(&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(&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(&file_io, 8, "test");
if (rv != PP_OK)
return ReportError("FileIO::Write", rv);
// Check the contents of the file.
read_buffer.clear();
rv = ReadEntireFile(&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_ERROR_WOULDBLOCK)
rv = callback.WaitForResult();
if (rv != PP_OK)
return ReportError("FileIO::SetLength", rv);
// Check the contents of the file.
read_buffer.clear();
rv = ReadEntireFile(&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(&file_io, 4, "test");
if (rv != PP_OK)
return ReportError("FileIO::Write", rv);
// Check the contents of the file.
read_buffer.clear();
rv = ReadEntireFile(&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(&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));
return "";
}
std::string TestFileIO::TestTouchQuery() {
TestCompletionCallback callback;
pp::FileSystem_Dev file_system(instance_, PP_FILESYSTEMTYPE_LOCALTEMPORARY);
int32_t rv = file_system.Open(1024, callback);
if (rv == PP_ERROR_WOULDBLOCK)
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;
rv = file_io.Open(file_ref,
PP_FILEOPENFLAG_CREATE | PP_FILEOPENFLAG_WRITE,
callback);
if (rv == PP_ERROR_WOULDBLOCK)
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_ERROR_WOULDBLOCK)
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_ERROR_WOULDBLOCK)
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_ERROR_WOULDBLOCK)
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.";
return "";
}
|