OLD | NEW |
1 // Copyright (c) 2012, 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 "dart:io"; | 7 import "dart:io"; |
8 import "dart:isolate"; | 8 import "dart:isolate"; |
9 | 9 |
10 Directory tempDir() { | 10 Directory tempDir() { |
11 return new Directory('').createTempSync(); | 11 return new Directory('').createTempSync(); |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
43 p.receive((x, y) { | 43 p.receive((x, y) { |
44 p.close(); | 44 p.close(); |
45 temp.deleteSync(recursive: true); | 45 temp.deleteSync(recursive: true); |
46 }); | 46 }); |
47 var file = new File("${temp.path}/nonExistentFile"); | 47 var file = new File("${temp.path}/nonExistentFile"); |
48 | 48 |
49 // Non-existing file should throw exception. | 49 // Non-existing file should throw exception. |
50 Expect.throws(() => file.openSync(), | 50 Expect.throws(() => file.openSync(), |
51 (e) => checkOpenNonExistentFileException(e)); | 51 (e) => checkOpenNonExistentFileException(e)); |
52 | 52 |
53 var openFuture = file.open(FileMode.READ); | 53 var openFuture = file.open(mode: FileMode.READ); |
54 openFuture.then((raf) => Expect.fail("Unreachable code")) | 54 openFuture.then((raf) => Expect.fail("Unreachable code")) |
55 .catchError((e) { | 55 .catchError((e) { |
56 checkOpenNonExistentFileException(e.error); | 56 checkOpenNonExistentFileException(e.error); |
57 p.toSendPort().send(null); | 57 p.toSendPort().send(null); |
58 }); | 58 }); |
59 } | 59 } |
60 | 60 |
61 | 61 |
62 void testDeleteNonExistent() { | 62 void testDeleteNonExistent() { |
63 Directory temp = tempDir(); | 63 Directory temp = tempDir(); |
(...skipping 165 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
229 p.receive((x, y) { | 229 p.receive((x, y) { |
230 p.close(); | 230 p.close(); |
231 temp.deleteSync(recursive: true); | 231 temp.deleteSync(recursive: true); |
232 }); | 232 }); |
233 var file = new File("${temp.path}/nonExistentFile4"); | 233 var file = new File("${temp.path}/nonExistentFile4"); |
234 | 234 |
235 // Non-existing file should throw exception. | 235 // Non-existing file should throw exception. |
236 Expect.throws(() => file.readAsStringSync(), | 236 Expect.throws(() => file.readAsStringSync(), |
237 (e) => checkOpenNonExistentFileException(e)); | 237 (e) => checkOpenNonExistentFileException(e)); |
238 | 238 |
239 var readAsStringFuture = file.readAsString(Encoding.ASCII); | 239 var readAsStringFuture = file.readAsString(encoding: Encoding.ASCII); |
240 readAsStringFuture.then((data) => Expect.fail("Unreachable code")) | 240 readAsStringFuture.then((data) => Expect.fail("Unreachable code")) |
241 .catchError((e) { | 241 .catchError((e) { |
242 checkOpenNonExistentFileException(e.error); | 242 checkOpenNonExistentFileException(e.error); |
243 p.toSendPort().send(null); | 243 p.toSendPort().send(null); |
244 }); | 244 }); |
245 } | 245 } |
246 | 246 |
247 testReadAsLinesNonExistent() { | 247 testReadAsLinesNonExistent() { |
248 Directory temp = tempDir(); | 248 Directory temp = tempDir(); |
249 ReceivePort p = new ReceivePort(); | 249 ReceivePort p = new ReceivePort(); |
250 p.receive((x, y) { | 250 p.receive((x, y) { |
251 p.close(); | 251 p.close(); |
252 temp.deleteSync(recursive: true); | 252 temp.deleteSync(recursive: true); |
253 }); | 253 }); |
254 var file = new File("${temp.path}/nonExistentFile5"); | 254 var file = new File("${temp.path}/nonExistentFile5"); |
255 | 255 |
256 // Non-existing file should throw exception. | 256 // Non-existing file should throw exception. |
257 Expect.throws(() => file.readAsLinesSync(), | 257 Expect.throws(() => file.readAsLinesSync(), |
258 (e) => checkOpenNonExistentFileException(e)); | 258 (e) => checkOpenNonExistentFileException(e)); |
259 | 259 |
260 var readAsLinesFuture = file.readAsLines(Encoding.ASCII); | 260 var readAsLinesFuture = file.readAsLines(encoding: Encoding.ASCII); |
261 readAsLinesFuture.then((data) => Expect.fail("Unreachable code")) | 261 readAsLinesFuture.then((data) => Expect.fail("Unreachable code")) |
262 .catchError((e) { | 262 .catchError((e) { |
263 checkOpenNonExistentFileException(e.error); | 263 checkOpenNonExistentFileException(e.error); |
264 p.toSendPort().send(null); | 264 p.toSendPort().send(null); |
265 }); | 265 }); |
266 } | 266 } |
267 | 267 |
268 bool checkWriteReadOnlyFileException(e) { | 268 bool checkWriteReadOnlyFileException(e) { |
269 Expect.isTrue(e is FileIOException); | 269 Expect.isTrue(e is FileIOException); |
270 Expect.isTrue(e.osError != null); | 270 Expect.isTrue(e.osError != null); |
(...skipping 14 matching lines...) Expand all Loading... |
285 }); | 285 }); |
286 | 286 |
287 var file = new File("${temp.path}/test_file"); | 287 var file = new File("${temp.path}/test_file"); |
288 file.createSync(); | 288 file.createSync(); |
289 callback(file, p.toSendPort()); | 289 callback(file, p.toSendPort()); |
290 } | 290 } |
291 | 291 |
292 | 292 |
293 testWriteByteToReadOnlyFile() { | 293 testWriteByteToReadOnlyFile() { |
294 createTestFile((file, port) { | 294 createTestFile((file, port) { |
295 var openedFile = file.openSync(FileMode.READ); | 295 var openedFile = file.openSync(mode: FileMode.READ); |
296 | 296 |
297 // Writing to read only file should throw an exception. | 297 // Writing to read only file should throw an exception. |
298 Expect.throws(() => openedFile.writeByteSync(0), | 298 Expect.throws(() => openedFile.writeByteSync(0), |
299 (e) => checkWriteReadOnlyFileException(e)); | 299 (e) => checkWriteReadOnlyFileException(e)); |
300 | 300 |
301 var writeByteFuture = openedFile.writeByte(0); | 301 var writeByteFuture = openedFile.writeByte(0); |
302 writeByteFuture.catchError((e) { | 302 writeByteFuture.catchError((e) { |
303 checkWriteReadOnlyFileException(e.error); | 303 checkWriteReadOnlyFileException(e.error); |
304 openedFile.close().then((ignore) => port.send(null)); | 304 openedFile.close().then((ignore) => port.send(null)); |
305 }); | 305 }); |
306 }); | 306 }); |
307 } | 307 } |
308 | 308 |
309 testWriteListToReadOnlyFile() { | 309 testWriteListToReadOnlyFile() { |
310 createTestFile((file, port) { | 310 createTestFile((file, port) { |
311 var openedFile = file.openSync(FileMode.READ); | 311 var openedFile = file.openSync(mode: FileMode.READ); |
312 | 312 |
313 List data = [0, 1, 2, 3]; | 313 List data = [0, 1, 2, 3]; |
314 // Writing to read only file should throw an exception. | 314 // Writing to read only file should throw an exception. |
315 Expect.throws(() => openedFile.writeListSync(data, 0, data.length), | 315 Expect.throws(() => openedFile.writeListSync(data, 0, data.length), |
316 (e) => checkWriteReadOnlyFileException(e)); | 316 (e) => checkWriteReadOnlyFileException(e)); |
317 | 317 |
318 var writeListFuture = openedFile.writeList(data, 0, data.length); | 318 var writeListFuture = openedFile.writeList(data, 0, data.length); |
319 writeListFuture.catchError((e) { | 319 writeListFuture.catchError((e) { |
320 checkWriteReadOnlyFileException(e.error); | 320 checkWriteReadOnlyFileException(e.error); |
321 openedFile.close().then((ignore) => port.send(null)); | 321 openedFile.close().then((ignore) => port.send(null)); |
322 }); | 322 }); |
323 }); | 323 }); |
324 } | 324 } |
325 | 325 |
326 testTruncateReadOnlyFile() { | 326 testTruncateReadOnlyFile() { |
327 createTestFile((file, port) { | 327 createTestFile((file, port) { |
328 var openedFile = file.openSync(FileMode.WRITE); | 328 var openedFile = file.openSync(mode: FileMode.WRITE); |
329 openedFile.writeByteSync(0); | 329 openedFile.writeByteSync(0); |
330 openedFile.closeSync(); | 330 openedFile.closeSync(); |
331 openedFile = file.openSync(FileMode.READ); | 331 openedFile = file.openSync(mode: FileMode.READ); |
332 | 332 |
333 // Truncating read only file should throw an exception. | 333 // Truncating read only file should throw an exception. |
334 Expect.throws(() => openedFile.truncateSync(0), | 334 Expect.throws(() => openedFile.truncateSync(0), |
335 (e) => checkWriteReadOnlyFileException(e)); | 335 (e) => checkWriteReadOnlyFileException(e)); |
336 | 336 |
337 var truncateFuture = openedFile.truncate(0); | 337 var truncateFuture = openedFile.truncate(0); |
338 truncateFuture.then((ignore) => Expect.fail("Unreachable code")) | 338 truncateFuture.then((ignore) => Expect.fail("Unreachable code")) |
339 .catchError((e) { | 339 .catchError((e) { |
340 checkWriteReadOnlyFileException(e.error); | 340 checkWriteReadOnlyFileException(e.error); |
341 openedFile.close().then((ignore) => port.send(null)); | 341 openedFile.close().then((ignore) => port.send(null)); |
342 }); | 342 }); |
343 }); | 343 }); |
344 } | 344 } |
345 | 345 |
346 bool checkFileClosedException(e) { | 346 bool checkFileClosedException(e) { |
347 Expect.isTrue(e is FileIOException); | 347 Expect.isTrue(e is FileIOException); |
348 Expect.isTrue(e.toString().indexOf("File closed") != -1); | 348 Expect.isTrue(e.toString().indexOf("File closed") != -1); |
349 Expect.isTrue(e.osError == null); | 349 Expect.isTrue(e.osError == null); |
350 return true; | 350 return true; |
351 } | 351 } |
352 | 352 |
353 testOperateOnClosedFile() { | 353 testOperateOnClosedFile() { |
354 createTestFile((file, port) { | 354 createTestFile((file, port) { |
355 var openedFile = file.openSync(FileMode.READ); | 355 var openedFile = file.openSync(mode: FileMode.READ); |
356 openedFile.closeSync(); | 356 openedFile.closeSync(); |
357 | 357 |
358 List data = [0, 1, 2, 3]; | 358 List data = [0, 1, 2, 3]; |
359 Expect.throws(() => openedFile.readByteSync(), | 359 Expect.throws(() => openedFile.readByteSync(), |
360 (e) => checkFileClosedException(e)); | 360 (e) => checkFileClosedException(e)); |
361 Expect.throws(() => openedFile.writeByteSync(0), | 361 Expect.throws(() => openedFile.writeByteSync(0), |
362 (e) => checkFileClosedException(e)); | 362 (e) => checkFileClosedException(e)); |
363 Expect.throws(() => openedFile.writeListSync(data, 0, data.length), | 363 Expect.throws(() => openedFile.writeListSync(data, 0, data.length), |
364 (e) => checkFileClosedException(e)); | 364 (e) => checkFileClosedException(e)); |
365 Expect.throws(() => openedFile.readListSync(data, 0, data.length), | 365 Expect.throws(() => openedFile.readListSync(data, 0, data.length), |
(...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
463 testReadAsBytesNonExistent(); | 463 testReadAsBytesNonExistent(); |
464 testReadAsTextNonExistent(); | 464 testReadAsTextNonExistent(); |
465 testReadAsLinesNonExistent(); | 465 testReadAsLinesNonExistent(); |
466 testWriteByteToReadOnlyFile(); | 466 testWriteByteToReadOnlyFile(); |
467 testWriteListToReadOnlyFile(); | 467 testWriteListToReadOnlyFile(); |
468 testTruncateReadOnlyFile(); | 468 testTruncateReadOnlyFile(); |
469 testOperateOnClosedFile(); | 469 testOperateOnClosedFile(); |
470 testRepeatedlyCloseFile(); | 470 testRepeatedlyCloseFile(); |
471 testRepeatedlyCloseFileSync(); | 471 testRepeatedlyCloseFileSync(); |
472 } | 472 } |
OLD | NEW |