| 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 import "package:expect/expect.dart"; | 5 import "package:expect/expect.dart"; |
| 6 import "dart:io"; | 6 import "dart:io"; |
| 7 import "dart:isolate"; | 7 import "dart:isolate"; |
| 8 | 8 |
| 9 void testReadInvalidArgs(arg) { | 9 void testReadInvalidArgs(arg) { |
| 10 var port = new ReceivePort(); | 10 var port = new ReceivePort(); |
| 11 String filename = getFilename("tests/vm/data/fixed_length_file"); | 11 String filename = getFilename("tests/vm/data/fixed_length_file"); |
| 12 var file = (new File(filename)).openSync(); | 12 var file = (new File(filename)).openSync(); |
| 13 try { | 13 try { |
| 14 file.readSync(arg); | 14 file.readSync(arg); |
| 15 Expect.fail('exception expected'); | 15 Expect.fail('exception expected'); |
| 16 } catch (e) { | 16 } catch (e) { |
| 17 Expect.isTrue(e is FileIOException); | 17 Expect.isTrue(e is FileIOException); |
| 18 Expect.isTrue(e.toString().contains('Invalid arguments')); | 18 Expect.isTrue(e.toString().contains('Invalid arguments')); |
| 19 } | 19 } |
| 20 | 20 |
| 21 var errors = 0; | 21 var errors = 0; |
| 22 var readFuture = file.read(arg); | 22 var readFuture = file.read(arg); |
| 23 readFuture.then((bytes) { | 23 readFuture.then((bytes) { |
| 24 Expect.fail('exception expected'); | 24 Expect.fail('exception expected'); |
| 25 }).catchError((e) { | 25 }).catchError((error) { |
| 26 errors++; | 26 errors++; |
| 27 Expect.isTrue(e.error is FileIOException); | 27 Expect.isTrue(error is FileIOException); |
| 28 Expect.isTrue(e.error.toString().contains('Invalid arguments')); | 28 Expect.isTrue(error.toString().contains('Invalid arguments')); |
| 29 file.close().then((ignore) { | 29 file.close().then((ignore) { |
| 30 Expect.equals(1, errors); | 30 Expect.equals(1, errors); |
| 31 port.close(); | 31 port.close(); |
| 32 }); | 32 }); |
| 33 }); | 33 }); |
| 34 } | 34 } |
| 35 | 35 |
| 36 void testReadIntoInvalidArgs(buffer, start, end) { | 36 void testReadIntoInvalidArgs(buffer, start, end) { |
| 37 var port = new ReceivePort(); | 37 var port = new ReceivePort(); |
| 38 String filename = getFilename("tests/vm/data/fixed_length_file"); | 38 String filename = getFilename("tests/vm/data/fixed_length_file"); |
| 39 var file = (new File(filename)).openSync(); | 39 var file = (new File(filename)).openSync(); |
| 40 try { | 40 try { |
| 41 file.readIntoSync(buffer, start, end); | 41 file.readIntoSync(buffer, start, end); |
| 42 Expect.fail('exception expected'); | 42 Expect.fail('exception expected'); |
| 43 } catch (e) { | 43 } catch (e) { |
| 44 Expect.isTrue(e is FileIOException); | 44 Expect.isTrue(e is FileIOException); |
| 45 Expect.isTrue(e.toString().contains('Invalid arguments')); | 45 Expect.isTrue(e.toString().contains('Invalid arguments')); |
| 46 } | 46 } |
| 47 | 47 |
| 48 var errors = 0; | 48 var errors = 0; |
| 49 var readIntoFuture = file.readInto(buffer, start, end); | 49 var readIntoFuture = file.readInto(buffer, start, end); |
| 50 readIntoFuture.then((bytes) { | 50 readIntoFuture.then((bytes) { |
| 51 Expect.fail('exception expected'); | 51 Expect.fail('exception expected'); |
| 52 }).catchError((e) { | 52 }).catchError((error) { |
| 53 errors++; | 53 errors++; |
| 54 Expect.isTrue(e.error is FileIOException); | 54 Expect.isTrue(error is FileIOException); |
| 55 Expect.isTrue(e.error.toString().contains('Invalid arguments')); | 55 Expect.isTrue(error.toString().contains('Invalid arguments')); |
| 56 file.close().then((ignore) { | 56 file.close().then((ignore) { |
| 57 Expect.equals(1, errors); | 57 Expect.equals(1, errors); |
| 58 port.close(); | 58 port.close(); |
| 59 }); | 59 }); |
| 60 }); | 60 }); |
| 61 } | 61 } |
| 62 | 62 |
| 63 void testWriteByteInvalidArgs(value) { | 63 void testWriteByteInvalidArgs(value) { |
| 64 var port = new ReceivePort(); | 64 var port = new ReceivePort(); |
| 65 String filename = getFilename("tests/vm/data/fixed_length_file"); | 65 String filename = getFilename("tests/vm/data/fixed_length_file"); |
| 66 var file = (new File("${filename}_out")).openSync(mode: FileMode.WRITE); | 66 var file = (new File("${filename}_out")).openSync(mode: FileMode.WRITE); |
| 67 try { | 67 try { |
| 68 file.writeByteSync(value); | 68 file.writeByteSync(value); |
| 69 Expect.fail('exception expected'); | 69 Expect.fail('exception expected'); |
| 70 } catch (e) { | 70 } catch (e) { |
| 71 Expect.isTrue(e is FileIOException); | 71 Expect.isTrue(e is FileIOException); |
| 72 Expect.isTrue(e.toString().contains('Invalid argument')); | 72 Expect.isTrue(e.toString().contains('Invalid argument')); |
| 73 } | 73 } |
| 74 | 74 |
| 75 var writeByteFuture = file.writeByte(value); | 75 var writeByteFuture = file.writeByte(value); |
| 76 writeByteFuture.then((ignore) { | 76 writeByteFuture.then((ignore) { |
| 77 Expect.fail('exception expected'); | 77 Expect.fail('exception expected'); |
| 78 }).catchError((s) { | 78 }).catchError((error) { |
| 79 Expect.isTrue(s.error is FileIOException); | 79 Expect.isTrue(error is FileIOException); |
| 80 Expect.isTrue(s.error.toString().contains('Invalid argument')); | 80 Expect.isTrue(error.toString().contains('Invalid argument')); |
| 81 file.close().then((ignore) { | 81 file.close().then((ignore) { |
| 82 port.close(); | 82 port.close(); |
| 83 }); | 83 }); |
| 84 }); | 84 }); |
| 85 } | 85 } |
| 86 | 86 |
| 87 void testWriteFromInvalidArgs(buffer, start, end) { | 87 void testWriteFromInvalidArgs(buffer, start, end) { |
| 88 var port = new ReceivePort(); | 88 var port = new ReceivePort(); |
| 89 String filename = getFilename("tests/vm/data/fixed_length_file"); | 89 String filename = getFilename("tests/vm/data/fixed_length_file"); |
| 90 var file = (new File("${filename}_out")).openSync(mode: FileMode.WRITE); | 90 var file = (new File("${filename}_out")).openSync(mode: FileMode.WRITE); |
| 91 try { | 91 try { |
| 92 file.writeFromSync(buffer, start, end); | 92 file.writeFromSync(buffer, start, end); |
| 93 Expect.fail('exception expected'); | 93 Expect.fail('exception expected'); |
| 94 } catch (e) { | 94 } catch (e) { |
| 95 Expect.isTrue(e is FileIOException); | 95 Expect.isTrue(e is FileIOException); |
| 96 Expect.isTrue(e.toString().contains('Invalid arguments')); | 96 Expect.isTrue(e.toString().contains('Invalid arguments')); |
| 97 } | 97 } |
| 98 | 98 |
| 99 var writeFromFuture = file.writeFrom(buffer, start, end); | 99 var writeFromFuture = file.writeFrom(buffer, start, end); |
| 100 writeFromFuture.then((ignore) { | 100 writeFromFuture.then((ignore) { |
| 101 Expect.fail('exception expected'); | 101 Expect.fail('exception expected'); |
| 102 }).catchError((s) { | 102 }).catchError((error) { |
| 103 Expect.isTrue(s.error is FileIOException); | 103 Expect.isTrue(error is FileIOException); |
| 104 Expect.isTrue(s.error.toString().contains('Invalid arguments')); | 104 Expect.isTrue(error.toString().contains('Invalid arguments')); |
| 105 file.close().then((ignore) { | 105 file.close().then((ignore) { |
| 106 port.close(); | 106 port.close(); |
| 107 }); | 107 }); |
| 108 }); | 108 }); |
| 109 } | 109 } |
| 110 | 110 |
| 111 void testWriteStringInvalidArgs(string, encoding) { | 111 void testWriteStringInvalidArgs(string, encoding) { |
| 112 var port = new ReceivePort(); | 112 var port = new ReceivePort(); |
| 113 String filename = getFilename("tests/vm/data/fixed_length_file"); | 113 String filename = getFilename("tests/vm/data/fixed_length_file"); |
| 114 var file = new File("${filename}_out").openSync(mode: FileMode.WRITE); | 114 var file = new File("${filename}_out").openSync(mode: FileMode.WRITE); |
| 115 try { | 115 try { |
| 116 file.writeStringSync(string, encoding: encoding); | 116 file.writeStringSync(string, encoding: encoding); |
| 117 Expect.fail('exception expected'); | 117 Expect.fail('exception expected'); |
| 118 } catch (e) { | 118 } catch (e) { |
| 119 Expect.isTrue(e is FileIOException); | 119 Expect.isTrue(e is FileIOException); |
| 120 } | 120 } |
| 121 | 121 |
| 122 var writeStringFuture = file.writeString(string, encoding: encoding); | 122 var writeStringFuture = file.writeString(string, encoding: encoding); |
| 123 writeStringFuture.then((ignore) { | 123 writeStringFuture.then((ignore) { |
| 124 Expect.fail('exception expected'); | 124 Expect.fail('exception expected'); |
| 125 }).catchError((s) { | 125 }).catchError((error) { |
| 126 Expect.isTrue(s.error is FileIOException); | 126 Expect.isTrue(error is FileIOException); |
| 127 file.close().then((ignore) { | 127 file.close().then((ignore) { |
| 128 port.close(); | 128 port.close(); |
| 129 }); | 129 }); |
| 130 }); | 130 }); |
| 131 } | 131 } |
| 132 | 132 |
| 133 String getFilename(String path) { | 133 String getFilename(String path) { |
| 134 return new File(path).existsSync() ? path : 'runtime/$path'; | 134 return new File(path).existsSync() ? path : 'runtime/$path'; |
| 135 } | 135 } |
| 136 | 136 |
| 137 main() { | 137 main() { |
| 138 testReadInvalidArgs('asdf'); | 138 testReadInvalidArgs('asdf'); |
| 139 testReadIntoInvalidArgs(12, 0, 1); | 139 testReadIntoInvalidArgs(12, 0, 1); |
| 140 testReadIntoInvalidArgs(new List(10), '0', 1); | 140 testReadIntoInvalidArgs(new List(10), '0', 1); |
| 141 testReadIntoInvalidArgs(new List(10), 0, '1'); | 141 testReadIntoInvalidArgs(new List(10), 0, '1'); |
| 142 testWriteByteInvalidArgs('asdf'); | 142 testWriteByteInvalidArgs('asdf'); |
| 143 testWriteFromInvalidArgs(12, 0, 1); | 143 testWriteFromInvalidArgs(12, 0, 1); |
| 144 testWriteFromInvalidArgs(new List(10), '0', 1); | 144 testWriteFromInvalidArgs(new List(10), '0', 1); |
| 145 testWriteFromInvalidArgs(new List(10), 0, '1'); | 145 testWriteFromInvalidArgs(new List(10), 0, '1'); |
| 146 testWriteStringInvalidArgs("Hello, world", 42); | 146 testWriteStringInvalidArgs("Hello, world", 42); |
| 147 } | 147 } |
| OLD | NEW |