Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 <errno.h> | |
| 8 #include <fcntl.h> | |
| 7 #include <string.h> | 9 #include <string.h> |
| 10 #include <sys/stat.h> | |
| 11 #include <sys/types.h> | |
| 8 | 12 |
| 9 #include <vector> | 13 #include <vector> |
| 10 | 14 |
| 11 #include "ppapi/c/dev/ppb_testing_dev.h" | 15 #include "ppapi/c/dev/ppb_testing_dev.h" |
| 12 #include "ppapi/c/pp_errors.h" | 16 #include "ppapi/c/pp_errors.h" |
| 13 #include "ppapi/c/ppb_file_io.h" | 17 #include "ppapi/c/ppb_file_io.h" |
| 18 #include "ppapi/c/private/pp_file_handle.h" | |
| 14 #include "ppapi/c/trusted/ppb_file_io_trusted.h" | 19 #include "ppapi/c/trusted/ppb_file_io_trusted.h" |
| 15 #include "ppapi/cpp/file_io.h" | 20 #include "ppapi/cpp/file_io.h" |
| 16 #include "ppapi/cpp/file_ref.h" | 21 #include "ppapi/cpp/file_ref.h" |
| 17 #include "ppapi/cpp/file_system.h" | 22 #include "ppapi/cpp/file_system.h" |
| 18 #include "ppapi/cpp/instance.h" | 23 #include "ppapi/cpp/instance.h" |
| 19 #include "ppapi/cpp/module.h" | 24 #include "ppapi/cpp/module.h" |
| 25 #include "ppapi/cpp/private/file_io_private.h" | |
| 20 #include "ppapi/tests/test_utils.h" | 26 #include "ppapi/tests/test_utils.h" |
| 21 #include "ppapi/tests/testing_instance.h" | 27 #include "ppapi/tests/testing_instance.h" |
| 22 | 28 |
| 29 #if defined(PPAPI_OS_WIN) | |
| 30 # include <io.h> | |
| 31 # include <windows.h> | |
| 32 // TODO(hamaji): Use standard windows APIs instead of compatibility layer? | |
| 33 # define lseek _lseek | |
| 34 # define read _read | |
| 35 # define write _write | |
| 36 # define ssize_t int | |
| 37 #else | |
| 38 # include <sys/mman.h> | |
| 39 # include <unistd.h> | |
| 40 #endif | |
| 41 | |
| 23 REGISTER_TEST_CASE(FileIO); | 42 REGISTER_TEST_CASE(FileIO); |
| 24 | 43 |
| 25 namespace { | 44 namespace { |
| 26 | 45 |
| 27 std::string ReportMismatch(const std::string& method_name, | 46 std::string ReportMismatch(const std::string& method_name, |
| 28 const std::string& returned_result, | 47 const std::string& returned_result, |
| 29 const std::string& expected_result) { | 48 const std::string& expected_result) { |
| 30 return method_name + " returned '" + returned_result + "'; '" + | 49 return method_name + " returned '" + returned_result + "'; '" + |
| 31 expected_result + "' expected."; | 50 expected_result + "' expected."; |
| 32 } | 51 } |
| (...skipping 108 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 141 RUN_TEST_FORCEASYNC_AND_NOT(Open, filter); | 160 RUN_TEST_FORCEASYNC_AND_NOT(Open, filter); |
| 142 RUN_TEST_FORCEASYNC_AND_NOT(OpenDirectory, filter); | 161 RUN_TEST_FORCEASYNC_AND_NOT(OpenDirectory, filter); |
| 143 RUN_TEST_FORCEASYNC_AND_NOT(ReadWriteSetLength, filter); | 162 RUN_TEST_FORCEASYNC_AND_NOT(ReadWriteSetLength, filter); |
| 144 RUN_TEST_FORCEASYNC_AND_NOT(ReadToArrayWriteSetLength, filter); | 163 RUN_TEST_FORCEASYNC_AND_NOT(ReadToArrayWriteSetLength, filter); |
| 145 RUN_TEST_FORCEASYNC_AND_NOT(TouchQuery, filter); | 164 RUN_TEST_FORCEASYNC_AND_NOT(TouchQuery, filter); |
| 146 RUN_TEST_FORCEASYNC_AND_NOT(AbortCalls, filter); | 165 RUN_TEST_FORCEASYNC_AND_NOT(AbortCalls, filter); |
| 147 RUN_TEST_FORCEASYNC_AND_NOT(ParallelReads, filter); | 166 RUN_TEST_FORCEASYNC_AND_NOT(ParallelReads, filter); |
| 148 RUN_TEST_FORCEASYNC_AND_NOT(ParallelWrites, filter); | 167 RUN_TEST_FORCEASYNC_AND_NOT(ParallelWrites, filter); |
| 149 RUN_TEST_FORCEASYNC_AND_NOT(NotAllowMixedReadWrite, filter); | 168 RUN_TEST_FORCEASYNC_AND_NOT(NotAllowMixedReadWrite, filter); |
| 150 RUN_TEST_FORCEASYNC_AND_NOT(WillWriteWillSetLength, filter); | 169 RUN_TEST_FORCEASYNC_AND_NOT(WillWriteWillSetLength, filter); |
| 170 RUN_TEST_FORCEASYNC_AND_NOT(GetOSFileHandle, filter); | |
| 151 | 171 |
| 152 // TODO(viettrungluu): add tests: | 172 // TODO(viettrungluu): add tests: |
| 153 // - that PP_ERROR_PENDING is correctly returned | 173 // - that PP_ERROR_PENDING is correctly returned |
| 154 // - that operations respect the file open modes (flags) | 174 // - that operations respect the file open modes (flags) |
| 155 } | 175 } |
| 156 | 176 |
| 157 std::string TestFileIO::TestOpen() { | 177 std::string TestFileIO::TestOpen() { |
| 158 TestCompletionCallback callback(instance_->pp_instance(), force_async_); | 178 TestCompletionCallback callback(instance_->pp_instance(), force_async_); |
| 159 | 179 |
| 160 pp::FileSystem file_system(instance_, PP_FILESYSTEMTYPE_LOCALTEMPORARY); | 180 pp::FileSystem file_system(instance_, PP_FILESYSTEMTYPE_LOCALTEMPORARY); |
| (...skipping 1080 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1241 read_buffer.clear(); | 1261 read_buffer.clear(); |
| 1242 rv = ReadEntireFile(instance_->pp_instance(), &file_io, 0, &read_buffer); | 1262 rv = ReadEntireFile(instance_->pp_instance(), &file_io, 0, &read_buffer); |
| 1243 if (rv != PP_OK) | 1263 if (rv != PP_OK) |
| 1244 return ReportError("FileIO::Read", rv); | 1264 return ReportError("FileIO::Read", rv); |
| 1245 if (read_buffer != "test") | 1265 if (read_buffer != "test") |
| 1246 return ReportMismatch("FileIO::Read", read_buffer, "test"); | 1266 return ReportMismatch("FileIO::Read", read_buffer, "test"); |
| 1247 | 1267 |
| 1248 PASS(); | 1268 PASS(); |
| 1249 } | 1269 } |
| 1250 | 1270 |
| 1271 std::string TestFileIO::TestGetOSFileHandle() { | |
| 1272 TestCompletionCallback callback(instance_->pp_instance(), force_async_); | |
|
dmichael (off chromium)
2013/03/27 19:40:53
I know it would be inconsistent with the rest of t
hamaji
2013/03/28 00:17:29
Done.
| |
| 1273 | |
| 1274 pp::FileSystem file_system(instance_, PP_FILESYSTEMTYPE_LOCALTEMPORARY); | |
| 1275 pp::FileRef file_ref(file_system, "/file_os_fd"); | |
| 1276 | |
| 1277 int32_t rv = file_system.Open(1024, callback.GetCallback()); | |
| 1278 if (force_async_ && rv != PP_OK_COMPLETIONPENDING) | |
| 1279 return ReportError("FileSystem::Open force_async", rv); | |
| 1280 if (rv == PP_OK_COMPLETIONPENDING) | |
| 1281 rv = callback.WaitForResult(); | |
| 1282 if (rv != PP_OK) | |
| 1283 return ReportError("FileSystem::Open", rv); | |
| 1284 | |
| 1285 pp::FileIO file_io(instance_); | |
| 1286 rv = file_io.Open(file_ref, | |
| 1287 PP_FILEOPENFLAG_CREATE | | |
| 1288 PP_FILEOPENFLAG_TRUNCATE | | |
| 1289 PP_FILEOPENFLAG_READ | | |
| 1290 PP_FILEOPENFLAG_WRITE, | |
| 1291 callback.GetCallback()); | |
| 1292 if (force_async_ && rv != PP_OK_COMPLETIONPENDING) | |
| 1293 return ReportError("FileIO::Open force_async", rv); | |
| 1294 if (rv == PP_OK_COMPLETIONPENDING) | |
| 1295 rv = callback.WaitForResult(); | |
| 1296 if (rv != PP_OK) | |
| 1297 return ReportError("FileIO::Open", rv); | |
| 1298 | |
| 1299 pp::FileIO_Private file_io_private; | |
| 1300 PP_FileHandle handle; | |
| 1301 rv = file_io_private.GetOSFileHandle(file_io, | |
| 1302 &handle, | |
| 1303 callback.GetCallback()); | |
| 1304 if (force_async_ && rv != PP_OK_COMPLETIONPENDING) | |
| 1305 return ReportError("FileIO::GetOSFileHandle force_async", rv); | |
| 1306 if (rv == PP_OK_COMPLETIONPENDING) | |
| 1307 rv = callback.WaitForResult(); | |
| 1308 if (rv != PP_OK) | |
| 1309 return ReportError("FileIO::GetOSFileHandle", rv); | |
| 1310 | |
| 1311 if (handle == PP_kInvalidFileHandle) | |
| 1312 return "FileIO::GetOSFileHandle() returned a bad file handle."; | |
| 1313 #if defined(PPAPI_OS_WIN) | |
| 1314 int fd = _open_osfhandle(reinterpret_cast<intptr_t>(handle), | |
| 1315 _O_RDWR | _O_BINARY); | |
| 1316 #else | |
| 1317 int fd = handle; | |
| 1318 #endif | |
| 1319 if (fd < 0) | |
| 1320 return "FileIO::GetOSFileHandle() returned a bad file descriptor."; | |
| 1321 | |
| 1322 // Check write(2) for the native FD. | |
| 1323 const std::string msg = "foobar"; | |
| 1324 ssize_t cnt = write(fd, msg.data(), msg.size()); | |
| 1325 if (cnt < 0) | |
| 1326 return ReportError("write for native FD returned error", errno); | |
| 1327 if (cnt != static_cast<ssize_t>(msg.size())) | |
| 1328 return ReportError("write for native FD count mismatch", cnt); | |
| 1329 | |
| 1330 // Check lseek(2) for the native FD. | |
| 1331 off_t off = lseek(fd, 0, SEEK_CUR); | |
| 1332 if (off == static_cast<off_t>(-1)) | |
| 1333 return ReportError("lseek for native FD returned error", errno); | |
| 1334 if (off != static_cast<off_t>(msg.size())) | |
| 1335 return ReportError("lseek for native FD offset mismatch", off); | |
| 1336 | |
| 1337 off = lseek(fd, 0, SEEK_SET); | |
| 1338 if (off == static_cast<off_t>(-1)) | |
| 1339 return ReportError("lseek for native FD returned error", errno); | |
| 1340 if (off != 0) | |
| 1341 return ReportError("lseek for native FD offset mismatch", off); | |
| 1342 | |
| 1343 // Check read(2) for the native FD. | |
| 1344 std::string buf(msg.size(), '\0'); | |
| 1345 cnt = read(fd, &buf[0], msg.size()); | |
| 1346 if (cnt < 0) | |
| 1347 return ReportError("read for native FD returned error", errno); | |
| 1348 if (cnt != static_cast<ssize_t>(msg.size())) | |
| 1349 return ReportError("read for native FD count mismatch", cnt); | |
| 1350 if (msg != buf) | |
| 1351 return ReportMismatch("read for native FD", buf, msg); | |
| 1352 | |
| 1353 // TODO(hamaji): Test CreateFileMapping for windows. | |
| 1354 #if !defined(PPAPI_OS_WIN) | |
| 1355 // Check mmap(2) for read. | |
| 1356 char* mapped = reinterpret_cast<char*>( | |
| 1357 mmap(NULL, msg.size(), PROT_READ, MAP_PRIVATE, fd, 0)); | |
| 1358 if (mapped == MAP_FAILED) | |
| 1359 return ReportError("mmap(r) for native FD returned errno", errno); | |
| 1360 // Make sure the buffer is cleared. | |
| 1361 buf = std::string(msg.size(), '\0'); | |
| 1362 memcpy(&buf[0], mapped, msg.size()); | |
| 1363 if (msg != buf) | |
| 1364 return ReportMismatch("mmap(r) for native FD", buf, msg); | |
| 1365 int r = munmap(mapped, msg.size()); | |
| 1366 if (r < 0) | |
| 1367 return ReportError("munmap for native FD returned error", errno); | |
| 1368 | |
| 1369 // Check mmap(2) for write. | |
| 1370 mapped = reinterpret_cast<char*>( | |
| 1371 mmap(NULL, msg.size(), PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0)); | |
| 1372 if (mapped == MAP_FAILED) | |
| 1373 return ReportError("mmap(w) for native FD returned errno", errno); | |
| 1374 // s/foo/baz/ | |
| 1375 strcpy(mapped, "baz"); | |
| 1376 | |
| 1377 r = munmap(mapped, msg.size()); | |
| 1378 if (r < 0) | |
| 1379 return ReportError("munmap for native FD returned error", errno); | |
| 1380 #endif | |
| 1381 | |
| 1382 #if defined(PPAPI_OS_WIN) | |
| 1383 int r = _close(fd); | |
| 1384 #else | |
| 1385 r = close(handle); | |
| 1386 #endif | |
| 1387 if (r < 0) | |
| 1388 return ReportError("close for native FD returned error", errno); | |
| 1389 | |
| 1390 // TODO(hamaji): Check if the file is actually updated? | |
| 1391 | |
| 1392 PASS(); | |
| 1393 } | |
| 1394 | |
| 1251 std::string TestFileIO::MatchOpenExpectations(pp::FileSystem* file_system, | 1395 std::string TestFileIO::MatchOpenExpectations(pp::FileSystem* file_system, |
| 1252 size_t open_flags, | 1396 size_t open_flags, |
| 1253 size_t expectations) { | 1397 size_t expectations) { |
| 1254 std::string bad_argument = | 1398 std::string bad_argument = |
| 1255 "TestFileIO::MatchOpenExpectations has invalid input arguments."; | 1399 "TestFileIO::MatchOpenExpectations has invalid input arguments."; |
| 1256 bool invalid_combination = !!(expectations & INVALID_FLAG_COMBINATION); | 1400 bool invalid_combination = !!(expectations & INVALID_FLAG_COMBINATION); |
| 1257 if (invalid_combination) { | 1401 if (invalid_combination) { |
| 1258 if (expectations != INVALID_FLAG_COMBINATION) | 1402 if (expectations != INVALID_FLAG_COMBINATION) |
| 1259 return bad_argument; | 1403 return bad_argument; |
| 1260 } else { | 1404 } else { |
| (...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1346 rv = callback.WaitForResult(); | 1490 rv = callback.WaitForResult(); |
| 1347 if ((invalid_combination && rv == PP_OK) || | 1491 if ((invalid_combination && rv == PP_OK) || |
| 1348 (!invalid_combination && ((rv == PP_OK) != create_if_doesnt_exist))) { | 1492 (!invalid_combination && ((rv == PP_OK) != create_if_doesnt_exist))) { |
| 1349 return ReportOpenError(open_flags); | 1493 return ReportOpenError(open_flags); |
| 1350 } | 1494 } |
| 1351 | 1495 |
| 1352 return std::string(); | 1496 return std::string(); |
| 1353 } | 1497 } |
| 1354 | 1498 |
| 1355 // TODO(viettrungluu): Test Close(). crbug.com/69457 | 1499 // TODO(viettrungluu): Test Close(). crbug.com/69457 |
| OLD | NEW |