OLD | NEW |
1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2013, 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 "package:expect/expect.dart"; | 7 import "package:expect/expect.dart"; |
8 import "dart:io"; | 8 import "dart:io"; |
9 import "dart:isolate"; | 9 import "dart:isolate"; |
10 | 10 |
11 Directory tempDir() { | 11 Directory tempDir() { |
12 return new Directory('').createTempSync(); | 12 return new Directory('').createTempSync(); |
13 } | 13 } |
14 | 14 |
15 | 15 |
16 bool checkNonExistentFileException(e, str) { | 16 bool checkNonExistentFileException(e, str) { |
17 Expect.isTrue(e is FileIOException); | 17 Expect.isTrue(e is FileException); |
18 Expect.isTrue(e.osError != null); | 18 Expect.isTrue(e.osError != null); |
19 Expect.isTrue(e.toString().indexOf(str) != -1); | 19 Expect.isTrue(e.toString().indexOf(str) != -1); |
20 // File not not found has error code 2 on all supported platforms. | 20 // File not not found has error code 2 on all supported platforms. |
21 Expect.equals(2, e.osError.errorCode); | 21 Expect.equals(2, e.osError.errorCode); |
22 return true; | 22 return true; |
23 } | 23 } |
24 | 24 |
25 | 25 |
26 bool checkOpenNonExistentFileException(e) { | 26 bool checkOpenNonExistentFileException(e) { |
27 return checkNonExistentFileException(e, "Cannot open file"); | 27 return checkNonExistentFileException(e, "Cannot open file"); |
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
98 var lenFuture = file.length(); | 98 var lenFuture = file.length(); |
99 lenFuture.then((len) => Expect.fail("Unreachable code")) | 99 lenFuture.then((len) => Expect.fail("Unreachable code")) |
100 .catchError((error) { | 100 .catchError((error) { |
101 checkLengthNonExistentFileException(error); | 101 checkLengthNonExistentFileException(error); |
102 p.toSendPort().send(null); | 102 p.toSendPort().send(null); |
103 }); | 103 }); |
104 } | 104 } |
105 | 105 |
106 | 106 |
107 bool checkCreateInNonExistentDirectoryException(e) { | 107 bool checkCreateInNonExistentDirectoryException(e) { |
108 Expect.isTrue(e is FileIOException); | 108 Expect.isTrue(e is FileException); |
109 Expect.isTrue(e.osError != null); | 109 Expect.isTrue(e.osError != null); |
110 Expect.isTrue(e.toString().indexOf("Cannot create file") != -1); | 110 Expect.isTrue(e.toString().indexOf("Cannot create file") != -1); |
111 if (Platform.operatingSystem == "linux") { | 111 if (Platform.operatingSystem == "linux") { |
112 Expect.equals(2, e.osError.errorCode); | 112 Expect.equals(2, e.osError.errorCode); |
113 } else if (Platform.operatingSystem == "macos") { | 113 } else if (Platform.operatingSystem == "macos") { |
114 Expect.equals(2, e.osError.errorCode); | 114 Expect.equals(2, e.osError.errorCode); |
115 } else if (Platform.operatingSystem == "windows") { | 115 } else if (Platform.operatingSystem == "windows") { |
116 Expect.equals(3, e.osError.errorCode); | 116 Expect.equals(3, e.osError.errorCode); |
117 } | 117 } |
118 | 118 |
(...skipping 15 matching lines...) Expand all Loading... |
134 | 134 |
135 var create = file.create(); | 135 var create = file.create(); |
136 create.then((ignore) => Expect.fail("Unreachable code")) | 136 create.then((ignore) => Expect.fail("Unreachable code")) |
137 .catchError((error) { | 137 .catchError((error) { |
138 checkCreateInNonExistentDirectoryException(error); | 138 checkCreateInNonExistentDirectoryException(error); |
139 p.toSendPort().send(null); | 139 p.toSendPort().send(null); |
140 }); | 140 }); |
141 } | 141 } |
142 | 142 |
143 bool checkFullPathOnNonExistentDirectoryException(e) { | 143 bool checkFullPathOnNonExistentDirectoryException(e) { |
144 Expect.isTrue(e is FileIOException); | 144 Expect.isTrue(e is FileException); |
145 Expect.isTrue(e.osError != null); | 145 Expect.isTrue(e.osError != null); |
146 Expect.isTrue(e.toString().indexOf("Cannot retrieve full path") != -1); | 146 Expect.isTrue(e.toString().indexOf("Cannot retrieve full path") != -1); |
147 // File not not found has error code 2 on all supported platforms. | 147 // File not not found has error code 2 on all supported platforms. |
148 Expect.equals(2, e.osError.errorCode); | 148 Expect.equals(2, e.osError.errorCode); |
149 | 149 |
150 return true; | 150 return true; |
151 } | 151 } |
152 | 152 |
153 void testFullPathOnNonExistentDirectory() { | 153 void testFullPathOnNonExistentDirectory() { |
154 Directory temp = tempDir(); | 154 Directory temp = tempDir(); |
(...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
228 | 228 |
229 var readAsLinesFuture = file.readAsLines(encoding: Encoding.ASCII); | 229 var readAsLinesFuture = file.readAsLines(encoding: Encoding.ASCII); |
230 readAsLinesFuture.then((data) => Expect.fail("Unreachable code")) | 230 readAsLinesFuture.then((data) => Expect.fail("Unreachable code")) |
231 .catchError((error) { | 231 .catchError((error) { |
232 checkOpenNonExistentFileException(error); | 232 checkOpenNonExistentFileException(error); |
233 p.toSendPort().send(null); | 233 p.toSendPort().send(null); |
234 }); | 234 }); |
235 } | 235 } |
236 | 236 |
237 bool checkWriteReadOnlyFileException(e) { | 237 bool checkWriteReadOnlyFileException(e) { |
238 Expect.isTrue(e is FileIOException); | 238 Expect.isTrue(e is FileException); |
239 Expect.isTrue(e.osError != null); | 239 Expect.isTrue(e.osError != null); |
240 Expect.isTrue(e.osError.errorCode != OSError.noErrorCode); | 240 Expect.isTrue(e.osError.errorCode != OSError.noErrorCode); |
241 return true; | 241 return true; |
242 } | 242 } |
243 | 243 |
244 | 244 |
245 // Create a test file in a temporary directory. Setup a port to signal | 245 // Create a test file in a temporary directory. Setup a port to signal |
246 // when the temporary directory should be deleted. Pass the file and | 246 // when the temporary directory should be deleted. Pass the file and |
247 // the port to the callback argument. | 247 // the port to the callback argument. |
248 createTestFile(callback) { | 248 createTestFile(callback) { |
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
306 var truncateFuture = openedFile.truncate(0); | 306 var truncateFuture = openedFile.truncate(0); |
307 truncateFuture.then((ignore) => Expect.fail("Unreachable code")) | 307 truncateFuture.then((ignore) => Expect.fail("Unreachable code")) |
308 .catchError((error) { | 308 .catchError((error) { |
309 checkWriteReadOnlyFileException(error); | 309 checkWriteReadOnlyFileException(error); |
310 openedFile.close().then((ignore) => port.send(null)); | 310 openedFile.close().then((ignore) => port.send(null)); |
311 }); | 311 }); |
312 }); | 312 }); |
313 } | 313 } |
314 | 314 |
315 bool checkFileClosedException(e) { | 315 bool checkFileClosedException(e) { |
316 Expect.isTrue(e is FileIOException); | 316 Expect.isTrue(e is FileException); |
317 Expect.isTrue(e.toString().indexOf("File closed") != -1); | 317 Expect.isTrue(e.toString().indexOf("File closed") != -1); |
318 Expect.isTrue(e.osError == null); | 318 Expect.isTrue(e.osError == null); |
319 return true; | 319 return true; |
320 } | 320 } |
321 | 321 |
322 testOperateOnClosedFile() { | 322 testOperateOnClosedFile() { |
323 createTestFile((file, port) { | 323 createTestFile((file, port) { |
324 var openedFile = file.openSync(mode: FileMode.READ); | 324 var openedFile = file.openSync(mode: FileMode.READ); |
325 openedFile.closeSync(); | 325 openedFile.closeSync(); |
326 | 326 |
(...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
398 }); | 398 }); |
399 } | 399 } |
400 | 400 |
401 testRepeatedlyCloseFile() { | 401 testRepeatedlyCloseFile() { |
402 createTestFile((file, port) { | 402 createTestFile((file, port) { |
403 var openedFile = file.openSync(); | 403 var openedFile = file.openSync(); |
404 openedFile.close().then((ignore) { | 404 openedFile.close().then((ignore) { |
405 var closeFuture = openedFile.close(); | 405 var closeFuture = openedFile.close(); |
406 closeFuture.then((ignore) => null) | 406 closeFuture.then((ignore) => null) |
407 .catchError((error) { | 407 .catchError((error) { |
408 Expect.isTrue(error is FileIOException); | 408 Expect.isTrue(error is FileException); |
409 port.send(null); | 409 port.send(null); |
410 }); | 410 }); |
411 }); | 411 }); |
412 }); | 412 }); |
413 } | 413 } |
414 | 414 |
415 testRepeatedlyCloseFileSync() { | 415 testRepeatedlyCloseFileSync() { |
416 createTestFile((file, port) { | 416 createTestFile((file, port) { |
417 var openedFile = file.openSync(); | 417 var openedFile = file.openSync(); |
418 openedFile.closeSync(); | 418 openedFile.closeSync(); |
419 Expect.throws(openedFile.closeSync, | 419 Expect.throws(openedFile.closeSync, |
420 (e) => e is FileIOException); | 420 (e) => e is FileException); |
421 port.send(null); | 421 port.send(null); |
422 }); | 422 }); |
423 } | 423 } |
424 | 424 |
425 testReadSyncBigInt() { | 425 testReadSyncBigInt() { |
426 createTestFile((file, port) { | 426 createTestFile((file, port) { |
427 var bigint = 100000000000000000000000000000000000000000; | 427 var bigint = 100000000000000000000000000000000000000000; |
428 var openedFile = file.openSync(); | 428 var openedFile = file.openSync(); |
429 Expect.throws(() => openedFile.readSync(bigint), | 429 Expect.throws(() => openedFile.readSync(bigint), |
430 (e) => e is FileIOException); | 430 (e) => e is FileException); |
431 openedFile.closeSync(); | 431 openedFile.closeSync(); |
432 port.send(null); | 432 port.send(null); |
433 }); | 433 }); |
434 } | 434 } |
435 | 435 |
436 testReadSyncClosedFile() { | 436 testReadSyncClosedFile() { |
437 createTestFile((file, port) { | 437 createTestFile((file, port) { |
438 var openedFile = file.openSync(); | 438 var openedFile = file.openSync(); |
439 openedFile.closeSync(); | 439 openedFile.closeSync(); |
440 Expect.throws(() => openedFile.readSync(1), | 440 Expect.throws(() => openedFile.readSync(1), |
441 (e) => e is FileIOException); | 441 (e) => e is FileException); |
442 port.send(null); | 442 port.send(null); |
443 }); | 443 }); |
444 } | 444 } |
445 | 445 |
446 main() { | 446 main() { |
447 testOpenNonExistent(); | 447 testOpenNonExistent(); |
448 testDeleteNonExistent(); | 448 testDeleteNonExistent(); |
449 testLengthNonExistent(); | 449 testLengthNonExistent(); |
450 testCreateInNonExistentDirectory(); | 450 testCreateInNonExistentDirectory(); |
451 testFullPathOnNonExistentDirectory(); | 451 testFullPathOnNonExistentDirectory(); |
452 testReadAsBytesNonExistent(); | 452 testReadAsBytesNonExistent(); |
453 testReadAsTextNonExistent(); | 453 testReadAsTextNonExistent(); |
454 testReadAsLinesNonExistent(); | 454 testReadAsLinesNonExistent(); |
455 testWriteByteToReadOnlyFile(); | 455 testWriteByteToReadOnlyFile(); |
456 testWriteFromToReadOnlyFile(); | 456 testWriteFromToReadOnlyFile(); |
457 testTruncateReadOnlyFile(); | 457 testTruncateReadOnlyFile(); |
458 testOperateOnClosedFile(); | 458 testOperateOnClosedFile(); |
459 testRepeatedlyCloseFile(); | 459 testRepeatedlyCloseFile(); |
460 testRepeatedlyCloseFileSync(); | 460 testRepeatedlyCloseFileSync(); |
461 testReadSyncBigInt(); | 461 testReadSyncBigInt(); |
462 testReadSyncClosedFile(); | 462 testReadSyncClosedFile(); |
463 } | 463 } |
OLD | NEW |