| OLD | NEW |
| 1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2011, 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 file I/O. | 5 // Dart test program for testing file I/O. |
| 6 | 6 |
| 7 | 7 |
| 8 class FileTest { | 8 class FileTest { |
| 9 static Directory tempDirectory; |
| 10 static int numLiveAsyncTests = 0; |
| 11 |
| 12 static void asyncTestStarted() { ++numLiveAsyncTests; } |
| 13 static void asyncTestDone() { |
| 14 --numLiveAsyncTests; |
| 15 if (numLiveAsyncTests == 0) { |
| 16 deleteTempDirectory(); |
| 17 } |
| 18 } |
| 19 |
| 20 static void createTempDirectory(Function doNext) { |
| 21 tempDirectory = new Directory(''); |
| 22 tempDirectory.createTempHandler = doNext; |
| 23 tempDirectory.createTemp(); |
| 24 } |
| 25 |
| 26 static void deleteTempDirectory() { |
| 27 tempDirectory.deleteSync(); |
| 28 } |
| 29 |
| 9 // Test for file read functionality. | 30 // Test for file read functionality. |
| 10 static int testReadStream() { | 31 static int testReadStream() { |
| 11 // Read a file and check part of it's contents. | 32 // Read a file and check part of it's contents. |
| 12 String filename = getFilename("bin/file_test.cc"); | 33 String filename = getFilename("bin/file_test.cc"); |
| 13 File file = new File(filename); | 34 File file = new File(filename); |
| 14 FileInputStream input = file.openInputStream(); | 35 FileInputStream input = file.openInputStream(); |
| 15 List<int> buffer = new List<int>(42); | 36 List<int> buffer = new List<int>(42); |
| 16 int bytesRead = input.readInto(buffer, 0, 12); | 37 int bytesRead = input.readInto(buffer, 0, 12); |
| 17 Expect.equals(12, bytesRead); | 38 Expect.equals(12, bytesRead); |
| 18 bytesRead = input.readInto(buffer, 12, 30); | 39 bytesRead = input.readInto(buffer, 12, 30); |
| (...skipping 18 matching lines...) Expand all Loading... |
| 37 static int testReadWriteStream() { | 58 static int testReadWriteStream() { |
| 38 // Read a file. | 59 // Read a file. |
| 39 String inFilename = getFilename("tests/vm/data/fixed_length_file"); | 60 String inFilename = getFilename("tests/vm/data/fixed_length_file"); |
| 40 File file = new File(inFilename); | 61 File file = new File(inFilename); |
| 41 FileInputStream input = file.openInputStream(); | 62 FileInputStream input = file.openInputStream(); |
| 42 List<int> buffer1 = new List<int>(42); | 63 List<int> buffer1 = new List<int>(42); |
| 43 int bytesRead = input.readInto(buffer1, 0, 42); | 64 int bytesRead = input.readInto(buffer1, 0, 42); |
| 44 Expect.equals(42, bytesRead); | 65 Expect.equals(42, bytesRead); |
| 45 input.close(); | 66 input.close(); |
| 46 // Write the contents of the file just read into another file. | 67 // Write the contents of the file just read into another file. |
| 47 String outFilenameBase = getFilename("tests/vm/data/fixed_length_file"); | 68 String outFilename = tempDirectory.path + "/out_read_write_stream"; |
| 48 String outFilename = outFilenameBase + "_out_read_write_stream"; | |
| 49 file = new File(outFilename); | 69 file = new File(outFilename); |
| 50 OutputStream output = file.openOutputStream(); | 70 OutputStream output = file.openOutputStream(); |
| 51 bool writeDone = output.writeFrom(buffer1, 0, 42); | 71 bool writeDone = output.writeFrom(buffer1, 0, 42); |
| 52 Expect.equals(true, writeDone); | 72 Expect.equals(true, writeDone); |
| 53 output.close(); | 73 output.close(); |
| 54 // Now read the contents of the file just written. | 74 // Now read the contents of the file just written. |
| 55 List<int> buffer2 = new List<int>(42); | 75 List<int> buffer2 = new List<int>(42); |
| 56 file = new File(outFilename); | 76 file = new File(outFilename); |
| 57 input = file.openInputStream(); | 77 input = file.openInputStream(); |
| 58 bytesRead = input.readInto(buffer2, 0, 42); | 78 bytesRead = input.readInto(buffer2, 0, 42); |
| (...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 134 File file = new File(inFilename); | 154 File file = new File(inFilename); |
| 135 file.errorHandler = (s) { | 155 file.errorHandler = (s) { |
| 136 Expect.fail("No errors expected"); | 156 Expect.fail("No errors expected"); |
| 137 }; | 157 }; |
| 138 file.openHandler = () { | 158 file.openHandler = () { |
| 139 List<int> buffer1 = new List<int>(42); | 159 List<int> buffer1 = new List<int>(42); |
| 140 file.readListHandler = (bytes_read) { | 160 file.readListHandler = (bytes_read) { |
| 141 Expect.equals(42, bytes_read); | 161 Expect.equals(42, bytes_read); |
| 142 file.closeHandler = () { | 162 file.closeHandler = () { |
| 143 // Write the contents of the file just read into another file. | 163 // Write the contents of the file just read into another file. |
| 144 String outFilenameBase = | 164 String outFilename = tempDirectory.path + "/out_read_write"; |
| 145 getFilename("tests/vm/data/fixed_length_file"); | |
| 146 String outFilename = outFilenameBase + "_out_read_write"; | |
| 147 file = new File(outFilename); | 165 file = new File(outFilename); |
| 148 file.errorHandler = (s) { | 166 file.errorHandler = (s) { |
| 149 Expect.fail("No errors expected"); | 167 Expect.fail("No errors expected"); |
| 150 }; | 168 }; |
| 151 file.createHandler = () { | 169 file.createHandler = () { |
| 152 file.fullPathHandler = (s) { | 170 file.fullPathHandler = (s) { |
| 153 Expect.isTrue(new File(s).existsSync()); | 171 Expect.isTrue(new File(s).existsSync()); |
| 154 if (s[0] != '/' && s[0] != '\\' && s[1] != ':') { | 172 if (s[0] != '/' && s[0] != '\\' && s[1] != ':') { |
| 155 Expect.fail("Not a full path"); | 173 Expect.fail("Not a full path"); |
| 156 } | 174 } |
| (...skipping 13 matching lines...) Expand all Loading... |
| 170 // Now compare the two buffers to check if they | 188 // Now compare the two buffers to check if they |
| 171 // are identical. | 189 // are identical. |
| 172 Expect.equals(buffer1.length, buffer2.length); | 190 Expect.equals(buffer1.length, buffer2.length); |
| 173 for (int i = 0; i < buffer1.length; i++) { | 191 for (int i = 0; i < buffer1.length; i++) { |
| 174 Expect.equals(buffer1[i], buffer2[i]); | 192 Expect.equals(buffer1[i], buffer2[i]); |
| 175 } | 193 } |
| 176 // Delete the output file. | 194 // Delete the output file. |
| 177 file.deleteHandler = () { | 195 file.deleteHandler = () { |
| 178 file.existsHandler = (exists) { | 196 file.existsHandler = (exists) { |
| 179 Expect.isFalse(exists); | 197 Expect.isFalse(exists); |
| 198 asyncTestDone(); |
| 180 }; | 199 }; |
| 181 file.exists(); | 200 file.exists(); |
| 182 }; | 201 }; |
| 183 file.delete(); | 202 file.delete(); |
| 184 }; | 203 }; |
| 185 file.close(); | 204 file.close(); |
| 186 }; | 205 }; |
| 187 file.readList(buffer2, 0, 42); | 206 file.readList(buffer2, 0, 42); |
| 188 }; | 207 }; |
| 189 file.open(); | 208 file.open(); |
| 190 }; | 209 }; |
| 191 file.close(); | 210 file.close(); |
| 192 }; | 211 }; |
| 193 file.writeList(buffer1, 0, bytes_read); | 212 file.writeList(buffer1, 0, bytes_read); |
| 194 }; | 213 }; |
| 195 file.open(true); | 214 file.open(true); |
| 196 }; | 215 }; |
| 197 file.fullPath(); | 216 file.fullPath(); |
| 198 }; | 217 }; |
| 199 file.create(); | 218 file.create(); |
| 200 }; | 219 }; |
| 201 file.close(); | 220 file.close(); |
| 202 }; | 221 }; |
| 203 file.readList(buffer1, 0, 42); | 222 file.readList(buffer1, 0, 42); |
| 204 }; | 223 }; |
| 224 asyncTestStarted(); |
| 205 file.open(); | 225 file.open(); |
| 206 return 1; | 226 return 1; |
| 207 | 227 |
| 208 } | 228 } |
| 209 | 229 |
| 210 static int testReadWriteSync() { | 230 static int testReadWriteSync() { |
| 211 // Read a file. | 231 // Read a file. |
| 212 String inFilename = getFilename("tests/vm/data/fixed_length_file"); | 232 String inFilename = getFilename("tests/vm/data/fixed_length_file"); |
| 213 File file = new File(inFilename); | 233 File file = new File(inFilename); |
| 214 file.openSync(); | 234 file.openSync(); |
| 215 List<int> buffer1 = new List<int>(42); | 235 List<int> buffer1 = new List<int>(42); |
| 216 int bytes_read = 0; | 236 int bytes_read = 0; |
| 217 int bytes_written = 0; | 237 int bytes_written = 0; |
| 218 bytes_read = file.readListSync(buffer1, 0, 42); | 238 bytes_read = file.readListSync(buffer1, 0, 42); |
| 219 Expect.equals(42, bytes_read); | 239 Expect.equals(42, bytes_read); |
| 220 file.closeSync(); | 240 file.closeSync(); |
| 221 // Write the contents of the file just read into another file. | 241 // Write the contents of the file just read into another file. |
| 222 String outFilenameBase = getFilename("tests/vm/data/fixed_length_file"); | 242 String outFilename = tempDirectory.path + "/out_read_write_sync"; |
| 223 String outFilename = outFilenameBase + "_out_read_write_sync"; | |
| 224 file = new File(outFilename); | 243 file = new File(outFilename); |
| 225 file.createSync(); | 244 file.createSync(); |
| 226 String path = file.fullPathSync(); | 245 String path = file.fullPathSync(); |
| 227 if (path[0] != '/' && path[0] != '\\' && path[1] != ':') { | 246 if (path[0] != '/' && path[0] != '\\' && path[1] != ':') { |
| 228 Expect.fail("Not a full path"); | 247 Expect.fail("Not a full path"); |
| 229 } | 248 } |
| 230 Expect.isTrue(new File(path).existsSync()); | 249 Expect.isTrue(new File(path).existsSync()); |
| 231 file.openSync(true); | 250 file.openSync(true); |
| 232 file.writeListSync(buffer1, 0, bytes_read); | 251 file.writeListSync(buffer1, 0, bytes_read); |
| 233 file.closeSync(); | 252 file.closeSync(); |
| (...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 321 Expect.equals(12, input.positionSync()); | 340 Expect.equals(12, input.positionSync()); |
| 322 input.readListSync(buffer, 12, 6); | 341 input.readListSync(buffer, 12, 6); |
| 323 Expect.equals(18, input.positionSync()); | 342 Expect.equals(18, input.positionSync()); |
| 324 input.setPositionSync(8); | 343 input.setPositionSync(8); |
| 325 Expect.equals(8, input.positionSync()); | 344 Expect.equals(8, input.positionSync()); |
| 326 input.closeSync(); | 345 input.closeSync(); |
| 327 return 1; | 346 return 1; |
| 328 } | 347 } |
| 329 | 348 |
| 330 static int testTruncate() { | 349 static int testTruncate() { |
| 331 String filename = getFilename("tests/vm/data/fixed_length_file"); | 350 File file = new File(tempDirectory.path + "/out_truncate"); |
| 332 File file = new File("${filename}_out_truncate"); | |
| 333 List buffer = const [65, 65, 65, 65, 65, 65, 65, 65, 65, 65]; | 351 List buffer = const [65, 65, 65, 65, 65, 65, 65, 65, 65, 65]; |
| 334 file.errorHandler = (error) { | 352 file.errorHandler = (error) { |
| 335 Expect.fail("testTruncate: No errors expected"); | 353 Expect.fail("testTruncate: No errors expected"); |
| 336 }; | 354 }; |
| 337 file.openHandler = () { | 355 file.openHandler = () { |
| 338 file.noPendingWriteHandler = () { | 356 file.noPendingWriteHandler = () { |
| 339 file.lengthHandler = (length) { | 357 file.lengthHandler = (length) { |
| 340 Expect.equals(10, length); | 358 Expect.equals(10, length); |
| 341 file.truncateHandler = () { | 359 file.truncateHandler = () { |
| 342 file.lengthHandler = (length) { | 360 file.lengthHandler = (length) { |
| 343 Expect.equals(5, length); | 361 Expect.equals(5, length); |
| 344 file.closeHandler = () { | 362 file.closeHandler = () { |
| 345 file.deleteHandler = () { | 363 file.deleteHandler = () { |
| 346 file.existsHandler = (exists) { | 364 file.existsHandler = (exists) { |
| 347 Expect.isFalse(exists); | 365 Expect.isFalse(exists); |
| 366 asyncTestDone(); |
| 348 }; | 367 }; |
| 349 file.exists(); | 368 file.exists(); |
| 350 }; | 369 }; |
| 351 file.delete(); | 370 file.delete(); |
| 352 }; | 371 }; |
| 353 file.close(); | 372 file.close(); |
| 354 }; | 373 }; |
| 355 file.length(); | 374 file.length(); |
| 356 }; | 375 }; |
| 357 file.truncate(5); | 376 file.truncate(5); |
| 358 }; | 377 }; |
| 359 file.length(); | 378 file.length(); |
| 360 }; | 379 }; |
| 361 file.writeList(buffer, 0, 10); | 380 file.writeList(buffer, 0, 10); |
| 362 }; | 381 }; |
| 382 asyncTestStarted(); |
| 363 file.open(true); | 383 file.open(true); |
| 364 return 1; | 384 return 1; |
| 365 } | 385 } |
| 366 | 386 |
| 367 static int testTruncateSync() { | 387 static int testTruncateSync() { |
| 368 String filename = getFilename("tests/vm/data/fixed_length_file"); | 388 File file = new File(tempDirectory.path + "/out_truncate_sync"); |
| 369 File file = new File("${filename}_out_truncate_sync"); | |
| 370 List buffer = const [65, 65, 65, 65, 65, 65, 65, 65, 65, 65]; | 389 List buffer = const [65, 65, 65, 65, 65, 65, 65, 65, 65, 65]; |
| 371 file.openSync(true); | 390 file.openSync(true); |
| 372 file.writeListSync(buffer, 0, 10); | 391 file.writeListSync(buffer, 0, 10); |
| 373 Expect.equals(10, file.lengthSync()); | 392 Expect.equals(10, file.lengthSync()); |
| 374 file.truncateSync(5); | 393 file.truncateSync(5); |
| 375 Expect.equals(5, file.lengthSync()); | 394 Expect.equals(5, file.lengthSync()); |
| 376 file.closeSync(); | 395 file.closeSync(); |
| 377 file.deleteSync(); | 396 file.deleteSync(); |
| 378 Expect.isFalse(file.existsSync()); | 397 Expect.isFalse(file.existsSync()); |
| 379 return 1; | 398 return 1; |
| 380 } | 399 } |
| 381 | 400 |
| 382 // Tests exception handling after file was closed. | 401 // Tests exception handling after file was closed. |
| 383 static int testCloseException() { | 402 static int testCloseException() { |
| 384 bool exceptionCaught = false; | 403 bool exceptionCaught = false; |
| 385 bool wrongExceptionCaught = false; | 404 bool wrongExceptionCaught = false; |
| 386 String filename = getFilename("tests/vm/data/fixed_length_file"); | 405 File input = new File(tempDirectory.path + "/out_close_exception"); |
| 387 File input = new File(filename + "_out_close_exception"); | |
| 388 input.openSync(true); | 406 input.openSync(true); |
| 389 input.closeSync(); | 407 input.closeSync(); |
| 390 try { | 408 try { |
| 391 input.readByteSync(); | 409 input.readByteSync(); |
| 392 } catch (FileIOException ex) { | 410 } catch (FileIOException ex) { |
| 393 exceptionCaught = true; | 411 exceptionCaught = true; |
| 394 } catch (Exception ex) { | 412 } catch (Exception ex) { |
| 395 wrongExceptionCaught = true; | 413 wrongExceptionCaught = true; |
| 396 } | 414 } |
| 397 Expect.equals(true, exceptionCaught); | 415 Expect.equals(true, exceptionCaught); |
| (...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 469 Expect.equals(true, exceptionCaught); | 487 Expect.equals(true, exceptionCaught); |
| 470 Expect.equals(true, !wrongExceptionCaught); | 488 Expect.equals(true, !wrongExceptionCaught); |
| 471 input.deleteSync(); | 489 input.deleteSync(); |
| 472 return 1; | 490 return 1; |
| 473 } | 491 } |
| 474 | 492 |
| 475 // Tests stream exception handling after file was closed. | 493 // Tests stream exception handling after file was closed. |
| 476 static int testCloseExceptionStream() { | 494 static int testCloseExceptionStream() { |
| 477 bool exceptionCaught = false; | 495 bool exceptionCaught = false; |
| 478 bool wrongExceptionCaught = false; | 496 bool wrongExceptionCaught = false; |
| 479 String filename = getFilename("tests/vm/data/fixed_length_file"); | 497 File file = new File(tempDirectory.path + "/out_close_exception_stream"); |
| 480 File file = new File(filename + "_out_close_exception_stream"); | |
| 481 file.createSync(); | 498 file.createSync(); |
| 482 FileInputStream input = file.openInputStream(); | 499 FileInputStream input = file.openInputStream(); |
| 483 input.close(); | 500 input.close(); |
| 484 try { | 501 try { |
| 485 List<int> buffer = new List<int>(42); | 502 List<int> buffer = new List<int>(42); |
| 486 input.readInto(buffer, 0, 12); | 503 input.readInto(buffer, 0, 12); |
| 487 } catch (FileIOException ex) { | 504 } catch (FileIOException ex) { |
| 488 exceptionCaught = true; | 505 exceptionCaught = true; |
| 489 } catch (Exception ex) { | 506 } catch (Exception ex) { |
| 490 wrongExceptionCaught = true; | 507 wrongExceptionCaught = true; |
| (...skipping 14 matching lines...) Expand all Loading... |
| 505 Expect.equals(true, exceptionCaught); | 522 Expect.equals(true, exceptionCaught); |
| 506 Expect.equals(true, !wrongExceptionCaught); | 523 Expect.equals(true, !wrongExceptionCaught); |
| 507 file.deleteSync(); | 524 file.deleteSync(); |
| 508 return 1; | 525 return 1; |
| 509 } | 526 } |
| 510 | 527 |
| 511 // Tests buffer out of bounds exception. | 528 // Tests buffer out of bounds exception. |
| 512 static int testBufferOutOfBoundsException() { | 529 static int testBufferOutOfBoundsException() { |
| 513 bool exceptionCaught = false; | 530 bool exceptionCaught = false; |
| 514 bool wrongExceptionCaught = false; | 531 bool wrongExceptionCaught = false; |
| 515 String filename = getFilename("tests/vm/data/fixed_length_file"); | 532 File file = new File(tempDirectory.path + "/out_buffer_out_of_bounds"); |
| 516 File file = new File(filename + "_out_buffer_out_of_bounds"); | |
| 517 file.openSync(true); | 533 file.openSync(true); |
| 518 try { | 534 try { |
| 519 List<int> buffer = new List<int>(10); | 535 List<int> buffer = new List<int>(10); |
| 520 bool readDone = file.readListSync(buffer, 0, 12); | 536 bool readDone = file.readListSync(buffer, 0, 12); |
| 521 } catch (IndexOutOfRangeException ex) { | 537 } catch (IndexOutOfRangeException ex) { |
| 522 exceptionCaught = true; | 538 exceptionCaught = true; |
| 523 } catch (Exception ex) { | 539 } catch (Exception ex) { |
| 524 wrongExceptionCaught = true; | 540 wrongExceptionCaught = true; |
| 525 } | 541 } |
| 526 Expect.equals(true, exceptionCaught); | 542 Expect.equals(true, exceptionCaught); |
| (...skipping 100 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 627 | 643 |
| 628 // Helper method to be able to run the test from the runtime | 644 // Helper method to be able to run the test from the runtime |
| 629 // directory, or the top directory. | 645 // directory, or the top directory. |
| 630 static String getFilename(String path) => | 646 static String getFilename(String path) => |
| 631 new File(path).existsSync() ? path : 'runtime/' + path; | 647 new File(path).existsSync() ? path : 'runtime/' + path; |
| 632 | 648 |
| 633 // Main test entrypoint. | 649 // Main test entrypoint. |
| 634 static testMain() { | 650 static testMain() { |
| 635 Expect.equals(1, testRead()); | 651 Expect.equals(1, testRead()); |
| 636 Expect.equals(1, testReadSync()); | 652 Expect.equals(1, testReadSync()); |
| 637 Expect.equals(1, testReadWrite()); | |
| 638 Expect.equals(1, testReadWriteSync()); | |
| 639 Expect.equals(1, testReadStream()); | 653 Expect.equals(1, testReadStream()); |
| 640 Expect.equals(1, testReadWriteStream()); | |
| 641 Expect.equals(1, testLength()); | 654 Expect.equals(1, testLength()); |
| 642 Expect.equals(1, testLengthSync()); | 655 Expect.equals(1, testLengthSync()); |
| 643 Expect.equals(1, testPosition()); | 656 Expect.equals(1, testPosition()); |
| 644 Expect.equals(1, testPositionSync()); | 657 Expect.equals(1, testPositionSync()); |
| 645 Expect.equals(1, testTruncate()); | |
| 646 Expect.equals(1, testTruncateSync()); | |
| 647 Expect.equals(1, testCloseException()); | |
| 648 Expect.equals(1, testCloseExceptionStream()); | |
| 649 Expect.equals(1, testBufferOutOfBoundsException()); | |
| 650 Expect.equals(1, testMixedSyncAndAsync()); | 658 Expect.equals(1, testMixedSyncAndAsync()); |
| 659 asyncTestStarted(); |
| 660 createTempDirectory(() { |
| 661 Expect.equals(1, testReadWrite()); |
| 662 Expect.equals(1, testReadWriteSync()); |
| 663 Expect.equals(1, testReadWriteStream()); |
| 664 Expect.equals(1, testTruncate()); |
| 665 Expect.equals(1, testTruncateSync()); |
| 666 Expect.equals(1, testCloseException()); |
| 667 Expect.equals(1, testCloseExceptionStream()); |
| 668 Expect.equals(1, testBufferOutOfBoundsException()); |
| 669 asyncTestDone(); |
| 670 }); |
| 651 } | 671 } |
| 652 } | 672 } |
| 653 | 673 |
| 654 main() { | 674 main() { |
| 655 FileTest.testMain(); | 675 FileTest.testMain(); |
| 656 } | 676 } |
| OLD | NEW |