| OLD | NEW |
| 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
| 3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
| 4 // | 4 // |
| 5 // Dart test program for testing error handling in file I/O. | 5 // Dart test program for testing error handling in file I/O. |
| 6 | 6 |
| 7 #import("dart:io"); | 7 #import("dart:io"); |
| 8 #import("dart:isolate"); | 8 #import("dart:isolate"); |
| 9 | 9 |
| 10 Directory tempDir() { | 10 Directory tempDir() { |
| (...skipping 284 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 295 }; | 295 }; |
| 296 } | 296 } |
| 297 | 297 |
| 298 bool checkWriteReadOnlyFileException(e) { | 298 bool checkWriteReadOnlyFileException(e) { |
| 299 Expect.isTrue(e is FileIOException); | 299 Expect.isTrue(e is FileIOException); |
| 300 Expect.isTrue(e.osError != null); | 300 Expect.isTrue(e.osError != null); |
| 301 Expect.isTrue(e.osError.errorCode != OSError.noErrorCode); | 301 Expect.isTrue(e.osError.errorCode != OSError.noErrorCode); |
| 302 return true; | 302 return true; |
| 303 } | 303 } |
| 304 | 304 |
| 305 testWriteByteToReadOnlyFile() { | 305 |
| 306 // Create a test file in a temporary directory. Setup a port to signal |
| 307 // when the temporary directory should be deleted. Pass the file and |
| 308 // the port to the callback argument. |
| 309 createTestFile(callback) { |
| 306 Directory temp = tempDir(); | 310 Directory temp = tempDir(); |
| 307 ReceivePort p = new ReceivePort(); | 311 ReceivePort p = new ReceivePort(); |
| 308 p.receive((x, y) { | 312 p.receive((x, y) { |
| 309 p.close(); | 313 p.close(); |
| 310 temp.deleteRecursivelySync(); | 314 temp.deleteRecursivelySync(); |
| 311 }); | 315 }); |
| 312 | 316 |
| 313 var file = new File("${temp.path}/test_file"); | 317 var file = new File("${temp.path}/test_file"); |
| 314 file.createSync(); | 318 file.createSync(); |
| 315 var openedFile = file.openSync(FileMode.READ); | 319 callback(file, p.toSendPort()); |
| 320 } |
| 316 | 321 |
| 317 // Writing to read only file should throw an exception. | |
| 318 Expect.throws(() => openedFile.writeByteSync(0), | |
| 319 (e) => checkWriteReadOnlyFileException(e)); | |
| 320 | 322 |
| 321 openedFile.writeByte(0); | 323 testWriteByteToReadOnlyFile() { |
| 322 openedFile.onError = (e) { | 324 createTestFile((file, port) { |
| 323 checkWriteReadOnlyFileException(e); | 325 var openedFile = file.openSync(FileMode.READ); |
| 324 openedFile.close(() => p.toSendPort().send(null)); | 326 |
| 325 }; | 327 // Writing to read only file should throw an exception. |
| 328 Expect.throws(() => openedFile.writeByteSync(0), |
| 329 (e) => checkWriteReadOnlyFileException(e)); |
| 330 |
| 331 openedFile.writeByte(0); |
| 332 openedFile.onError = (e) { |
| 333 checkWriteReadOnlyFileException(e); |
| 334 openedFile.close(() => port.send(null)); |
| 335 }; |
| 336 }); |
| 326 } | 337 } |
| 327 | 338 |
| 328 testWriteListToReadOnlyFile() { | 339 testWriteListToReadOnlyFile() { |
| 329 Directory temp = tempDir(); | 340 createTestFile((file, port) { |
| 330 ReceivePort p = new ReceivePort(); | 341 var openedFile = file.openSync(FileMode.READ); |
| 331 p.receive((x, y) { | 342 |
| 332 p.close(); | 343 List data = [0, 1, 2, 3]; |
| 333 temp.deleteRecursivelySync(); | 344 // Writing to read only file should throw an exception. |
| 345 Expect.throws(() => openedFile.writeListSync(data, 0, data.length), |
| 346 (e) => checkWriteReadOnlyFileException(e)); |
| 347 |
| 348 openedFile.writeList(data, 0, data.length); |
| 349 openedFile.onError = (e) { |
| 350 checkWriteReadOnlyFileException(e); |
| 351 openedFile.close(() => port.send(null)); |
| 352 }; |
| 334 }); | 353 }); |
| 335 | |
| 336 var file = new File("${temp.path}/test_file"); | |
| 337 file.createSync(); | |
| 338 var openedFile = file.openSync(FileMode.READ); | |
| 339 | |
| 340 List data = [0, 1, 2, 3]; | |
| 341 // Writing to read only file should throw an exception. | |
| 342 Expect.throws(() => openedFile.writeListSync(data, 0, data.length), | |
| 343 (e) => checkWriteReadOnlyFileException(e)); | |
| 344 | |
| 345 openedFile.writeList(data, 0, data.length); | |
| 346 openedFile.onError = (e) { | |
| 347 checkWriteReadOnlyFileException(e); | |
| 348 openedFile.close(() => p.toSendPort().send(null)); | |
| 349 }; | |
| 350 } | 354 } |
| 351 | 355 |
| 352 testTruncateReadOnlyFile() { | 356 testTruncateReadOnlyFile() { |
| 353 Directory temp = tempDir(); | 357 createTestFile((file, port) { |
| 354 ReceivePort p = new ReceivePort(); | 358 var openedFile = file.openSync(FileMode.WRITE); |
| 355 p.receive((x, y) { | 359 openedFile.writeByteSync(0); |
| 356 p.close(); | 360 openedFile.closeSync(); |
| 357 temp.deleteRecursivelySync(); | 361 openedFile = file.openSync(FileMode.READ); |
| 362 |
| 363 // Truncating read only file should throw an exception. |
| 364 Expect.throws(() => openedFile.truncateSync(0), |
| 365 (e) => checkWriteReadOnlyFileException(e)); |
| 366 |
| 367 openedFile.truncate(0, () => Expect.fail("Unreachable code")); |
| 368 openedFile.onError = (e) { |
| 369 checkWriteReadOnlyFileException(e); |
| 370 openedFile.close(() => port.send(null)); |
| 371 }; |
| 358 }); | 372 }); |
| 359 | |
| 360 var file = new File("${temp.path}/test_file"); | |
| 361 file.createSync(); | |
| 362 var openedFile = file.openSync(FileMode.WRITE); | |
| 363 openedFile.writeByteSync(0); | |
| 364 openedFile.closeSync(); | |
| 365 openedFile = file.openSync(FileMode.READ); | |
| 366 | |
| 367 // Truncating read only file should throw an exception. | |
| 368 Expect.throws(() => openedFile.truncateSync(0), | |
| 369 (e) => checkWriteReadOnlyFileException(e)); | |
| 370 | |
| 371 openedFile.truncate(0, () => Expect.fail("Unreachable code")); | |
| 372 openedFile.onError = (e) { | |
| 373 checkWriteReadOnlyFileException(e); | |
| 374 openedFile.close(() => p.toSendPort().send(null)); | |
| 375 }; | |
| 376 } | 373 } |
| 377 | 374 |
| 378 bool checkFileClosedException(e) { | 375 bool checkFileClosedException(e) { |
| 379 Expect.isTrue(e is FileIOException); | 376 Expect.isTrue(e is FileIOException); |
| 380 Expect.isTrue(e.toString().indexOf("File closed") != -1); | 377 Expect.isTrue(e.toString().indexOf("File closed") != -1); |
| 381 Expect.isTrue(e.osError == null); | 378 Expect.isTrue(e.osError == null); |
| 382 return true; | 379 return true; |
| 383 } | 380 } |
| 384 | 381 |
| 385 testOperateOnClosedFile() { | 382 testOperateOnClosedFile() { |
| 386 Directory temp = tempDir(); | 383 createTestFile((file, port) { |
| 387 ReceivePort p = new ReceivePort(); | 384 var openedFile = file.openSync(FileMode.READ); |
| 388 p.receive((x, y) { | 385 openedFile.closeSync(); |
| 389 p.close(); | 386 |
| 390 temp.deleteRecursivelySync(); | 387 List data = [0, 1, 2, 3]; |
| 388 Expect.throws(() => openedFile.readByteSync(), |
| 389 (e) => checkFileClosedException(e)); |
| 390 Expect.throws(() => openedFile.writeByteSync(0), |
| 391 (e) => checkFileClosedException(e)); |
| 392 Expect.throws(() => openedFile.writeListSync(data, 0, data.length), |
| 393 (e) => checkFileClosedException(e)); |
| 394 Expect.throws(() => openedFile.readListSync(data, 0, data.length), |
| 395 (e) => checkFileClosedException(e)); |
| 396 Expect.throws(() => openedFile.writeStringSync("Hello"), |
| 397 (e) => checkFileClosedException(e)); |
| 398 Expect.throws(() => openedFile.positionSync(), |
| 399 (e) => checkFileClosedException(e)); |
| 400 Expect.throws(() => openedFile.setPositionSync(0), |
| 401 (e) => checkFileClosedException(e)); |
| 402 Expect.throws(() => openedFile.truncateSync(0), |
| 403 (e) => checkFileClosedException(e)); |
| 404 Expect.throws(() => openedFile.lengthSync(), |
| 405 (e) => checkFileClosedException(e)); |
| 406 Expect.throws(() => openedFile.flushSync(), |
| 407 (e) => checkFileClosedException(e)); |
| 408 |
| 409 var errorCount = 0; |
| 410 openedFile.readByte((byte) => Expect.fail("Unreachable code")); |
| 411 errorCount++; |
| 412 openedFile.writeByte(0); |
| 413 errorCount++; |
| 414 openedFile.readList( |
| 415 data, 0, data.length, (bytesRead) => Expect.fail("Unreachable code")); |
| 416 errorCount++; |
| 417 openedFile.writeList(data, 0, data.length); |
| 418 errorCount++; |
| 419 openedFile.writeString("Hello"); |
| 420 errorCount++; |
| 421 openedFile.position((position) => Expect.fail("Unreachable code")); |
| 422 errorCount++; |
| 423 openedFile.setPosition(0, () => Expect.fail("Unreachable code")); |
| 424 errorCount++; |
| 425 openedFile.truncate(0, () => Expect.fail("Unreachable code")); |
| 426 errorCount++; |
| 427 openedFile.length((length) => Expect.fail("Unreachable code")); |
| 428 errorCount++; |
| 429 openedFile.flush(() => Expect.fail("Unreachable code")); |
| 430 errorCount++; |
| 431 |
| 432 openedFile.onError = (e) { |
| 433 checkFileClosedException(e); |
| 434 if (--errorCount == 0) { |
| 435 port.send(null); |
| 436 } |
| 437 }; |
| 391 }); | 438 }); |
| 439 } |
| 392 | 440 |
| 393 var file = new File("${temp.path}/test_file"); | 441 testRepeatedlyCloseFile() { |
| 394 file.createSync(); | 442 createTestFile((file, port) { |
| 395 var openedFile = file.openSync(FileMode.READ); | 443 var openedFile = file.openSync(); |
| 396 openedFile.closeSync(); | 444 openedFile.close(() { |
| 397 | 445 openedFile.onError = (e) { |
| 398 List data = [0, 1, 2, 3]; | 446 Expect.isTrue(e is FileIOException); |
| 399 Expect.throws(() => openedFile.readByteSync(), | 447 port.send(null); |
| 400 (e) => checkFileClosedException(e)); | 448 }; |
| 401 Expect.throws(() => openedFile.writeByteSync(0), | 449 openedFile.close(() => null); |
| 402 (e) => checkFileClosedException(e)); | 450 }); |
| 403 Expect.throws(() => openedFile.writeListSync(data, 0, data.length), | 451 }); |
| 404 (e) => checkFileClosedException(e)); | |
| 405 Expect.throws(() => openedFile.readListSync(data, 0, data.length), | |
| 406 (e) => checkFileClosedException(e)); | |
| 407 Expect.throws(() => openedFile.writeStringSync("Hello"), | |
| 408 (e) => checkFileClosedException(e)); | |
| 409 Expect.throws(() => openedFile.positionSync(), | |
| 410 (e) => checkFileClosedException(e)); | |
| 411 Expect.throws(() => openedFile.setPositionSync(0), | |
| 412 (e) => checkFileClosedException(e)); | |
| 413 Expect.throws(() => openedFile.truncateSync(0), | |
| 414 (e) => checkFileClosedException(e)); | |
| 415 Expect.throws(() => openedFile.lengthSync(), | |
| 416 (e) => checkFileClosedException(e)); | |
| 417 Expect.throws(() => openedFile.flushSync(), | |
| 418 (e) => checkFileClosedException(e)); | |
| 419 | |
| 420 var errorCount = 0; | |
| 421 openedFile.readByte((byte) => Expect.fail("Unreachable code")); | |
| 422 errorCount++; | |
| 423 openedFile.writeByte(0); | |
| 424 errorCount++; | |
| 425 openedFile.readList( | |
| 426 data, 0, data.length, (bytesRead) => Expect.fail("Unreachable code")); | |
| 427 errorCount++; | |
| 428 openedFile.writeList(data, 0, data.length); | |
| 429 errorCount++; | |
| 430 openedFile.writeString("Hello"); | |
| 431 errorCount++; | |
| 432 openedFile.position((position) => Expect.fail("Unreachable code")); | |
| 433 errorCount++; | |
| 434 openedFile.setPosition(0, () => Expect.fail("Unreachable code")); | |
| 435 errorCount++; | |
| 436 openedFile.truncate(0, () => Expect.fail("Unreachable code")); | |
| 437 errorCount++; | |
| 438 openedFile.length((length) => Expect.fail("Unreachable code")); | |
| 439 errorCount++; | |
| 440 openedFile.flush(() => Expect.fail("Unreachable code")); | |
| 441 errorCount++; | |
| 442 | |
| 443 openedFile.onError = (e) { | |
| 444 checkFileClosedException(e); | |
| 445 if (--errorCount == 0) { | |
| 446 p.toSendPort().send(null); | |
| 447 } | |
| 448 }; | |
| 449 } | 452 } |
| 450 | 453 |
| 451 main() { | 454 main() { |
| 452 testOpenNonExistent(); | 455 testOpenNonExistent(); |
| 453 testDeleteNonExistent(); | 456 testDeleteNonExistent(); |
| 454 testLengthNonExistent(); | 457 testLengthNonExistent(); |
| 455 testCreateInNonExistentDirectory(); | 458 testCreateInNonExistentDirectory(); |
| 456 testFullPathOnNonExistentDirectory(); | 459 testFullPathOnNonExistentDirectory(); |
| 457 testDirectoryInNonExistentDirectory(); | 460 testDirectoryInNonExistentDirectory(); |
| 458 testReadAsBytesNonExistent(); | 461 testReadAsBytesNonExistent(); |
| 459 testReadAsTextNonExistent(); | 462 testReadAsTextNonExistent(); |
| 460 testReadAsLinesNonExistent(); | 463 testReadAsLinesNonExistent(); |
| 461 testWriteByteToReadOnlyFile(); | 464 testWriteByteToReadOnlyFile(); |
| 462 testWriteListToReadOnlyFile(); | 465 testWriteListToReadOnlyFile(); |
| 463 testTruncateReadOnlyFile(); | 466 testTruncateReadOnlyFile(); |
| 464 testOperateOnClosedFile(); | 467 testOperateOnClosedFile(); |
| 468 testRepeatedlyCloseFile(); |
| 465 } | 469 } |
| OLD | NEW |