| 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 // 'fuzz' test the file APIs by providing unexpected type arguments. The test | 5 // 'fuzz' test the file APIs by providing unexpected type arguments. The test |
| 6 // passes if the VM does not crash. | 6 // passes if the VM does not crash. |
| 7 | 7 |
| 8 #import('dart:io'); | 8 #import('dart:io'); |
| 9 #import('dart:isolate'); | |
| 10 | 9 |
| 11 final typeMapping = const { | 10 final typeMapping = const { |
| 12 'null': null, | |
| 13 'int': 0, | 11 'int': 0, |
| 14 'bigint': 18446744073709551617, | |
| 15 'String': 'a', | 12 'String': 'a', |
| 16 'FileMode': FileMode.READ, | 13 'FileMode': FileMode.READ, |
| 17 'num': 0.50, | 14 'num': 0.50, |
| 18 'List<int>': const [1, 2, 3], | 15 'List<int>': const [1, 2, 3] |
| 19 'Map<String, int>': const { "a": 23 } | |
| 20 }; | 16 }; |
| 21 | 17 |
| 22 typePermutations(int argCount) { | |
| 23 var result = []; | |
| 24 if (argCount == 2) { | |
| 25 typeMapping.forEach((k, v) { | |
| 26 typeMapping.forEach((k2, v2) { | |
| 27 result.add([v, v2]); | |
| 28 }); | |
| 29 }); | |
| 30 } else { | |
| 31 Expect.isTrue(argCount == 3); | |
| 32 typeMapping.forEach((k, v) { | |
| 33 typeMapping.forEach((k2, v2) { | |
| 34 typeMapping.forEach((k3, v3) { | |
| 35 result.add([v, v2, v3]); | |
| 36 }); | |
| 37 }); | |
| 38 }); | |
| 39 } | |
| 40 return result; | |
| 41 } | |
| 42 | |
| 43 // Perform sync operation and ignore all exceptions. | |
| 44 doItSync(Function f) { | 18 doItSync(Function f) { |
| 19 // Ignore all exceptions. |
| 45 try { f(); } catch (var e) {} | 20 try { f(); } catch (var e) {} |
| 46 } | 21 } |
| 47 | 22 |
| 48 // Perform async operation and transform the future for the operation | |
| 49 // into a future that never fails by treating errors as normal | |
| 50 // completion. | |
| 51 Future doItAsync(Function f) { | |
| 52 // Ignore value and errors. | |
| 53 var completer = new Completer(); | |
| 54 var future = f(); | |
| 55 future.handleException((e) { | |
| 56 completer.complete(true); | |
| 57 return true; | |
| 58 }); | |
| 59 future.then((v) => completer.complete(true)); | |
| 60 return completer.future; | |
| 61 } | |
| 62 | |
| 63 fuzzSyncMethods() { | 23 fuzzSyncMethods() { |
| 64 typeMapping.forEach((k, v) { | 24 typeMapping.forEach((k, v) { |
| 65 doItSync(() { | 25 doItSync(() { |
| 66 var f = new File(v); | 26 var f = new File(v); |
| 67 doItSync(f.existsSync); | 27 doItSync(f.existsSync); |
| 68 doItSync(f.createSync); | 28 doItSync(f.createSync); |
| 69 doItSync(f.deleteSync); | 29 doItSync(f.deleteSync); |
| 70 doItSync(f.directorySync); | 30 doItSync(f.directorySync); |
| 71 doItSync(f.lengthSync); | 31 doItSync(f.lengthSync); |
| 72 doItSync(f.fullPathSync); | 32 doItSync(f.fullPathSync); |
| 73 doItSync(() => f.openInputStream().onError = (e) => null); | 33 doItSync(() => f.openInputStream().onError = (e) => null); |
| 74 doItSync(f.readAsBytesSync); | 34 doItSync(f.readAsBytesSync); |
| 75 doItSync(f.readAsTextSync); | |
| 76 doItSync(f.readAsLinesSync); | |
| 77 typeMapping.forEach((k2, v2) { | 35 typeMapping.forEach((k2, v2) { |
| 78 doItSync(() => f.openSync(v2)); | 36 doItSync(() => f.openSync(v2)); |
| 79 doItSync(() => f.openOutputStream(v2).onError = (e) => null); | 37 doItSync(() => f.openOutputStream(v2).onError = (e) => null); |
| 80 doItSync(() => f.readAsTextSync(v2)); | 38 doItSync(() => f.readAsTextSync(v2)); |
| 81 doItSync(() => f.readAsLinesSync(v2)); | 39 doItSync(() => f.readAsLinesSync(v2)); |
| 82 }); | 40 }); |
| 83 }); | 41 }); |
| 84 }); | 42 }); |
| 85 } | 43 } |
| 86 | 44 |
| 87 fuzzAsyncMethods() { | |
| 88 var port = new ReceivePort(); | |
| 89 var futures = []; | |
| 90 typeMapping.forEach((k, v) { | |
| 91 doItSync(() { | |
| 92 var f = new File(v); | |
| 93 futures.add(doItAsync(f.exists)); | |
| 94 futures.add(doItAsync(f.delete)); | |
| 95 futures.add(doItAsync(f.directory)); | |
| 96 futures.add(doItAsync(f.length)); | |
| 97 futures.add(doItAsync(f.open)); | |
| 98 futures.add(doItAsync(f.fullPath)); | |
| 99 futures.add(doItAsync(f.readAsBytes)); | |
| 100 futures.add(doItAsync(f.readAsLines)); | |
| 101 futures.add(doItAsync(f.readAsText)); | |
| 102 typeMapping.forEach((k2, v2) { | |
| 103 futures.add(doItAsync(() => f.open(v2))); | |
| 104 futures.add(doItAsync(() => f.readAsText(v2))); | |
| 105 futures.add(doItAsync(() => f.readAsLines(v2))); | |
| 106 }); | |
| 107 }); | |
| 108 }); | |
| 109 Futures.wait(futures).then((ignore) => port.close()); | |
| 110 } | |
| 111 | |
| 112 | |
| 113 // TODO(ager): Finish implementation. | |
| 114 fuzzSyncRandomAccessMethods() { | |
| 115 var d = new Directory(''); | |
| 116 var temp = d.createTempSync(); | |
| 117 var file = new File('${temp.path}/x'); | |
| 118 file.createSync(); | |
| 119 var modes = [ FileMode.READ, FileMode.WRITE, FileMode.APPEND ]; | |
| 120 for (var m in modes) { | |
| 121 var opened = file.openSync(m); | |
| 122 typeMapping.forEach((k, v) { | |
| 123 doItSync(() => opened.setPositionSync(v)); | |
| 124 doItSync(() => opened.writeByteSync(v)); | |
| 125 }); | |
| 126 for (var p in typePermutations(2)) { | |
| 127 doItSync(() => opened.writeStringSync(p[0], p[1])); | |
| 128 } | |
| 129 for (var p in typePermutations(3)) { | |
| 130 doItSync(() => opened.readListSync(p[0], p[1], p[2])); | |
| 131 doItSync(() => opened.writeList(p[0], p[1], p[2])); | |
| 132 } | |
| 133 } | |
| 134 temp.deleteRecursivelySync(); | |
| 135 } | |
| 136 | |
| 137 main() { | 45 main() { |
| 138 fuzzSyncMethods(); | 46 fuzzSyncMethods(); |
| 139 fuzzAsyncMethods(); | |
| 140 fuzzSyncRandomAccessMethods(); | |
| 141 } | 47 } |
| OLD | NEW |