| 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'); |
| 9 | 10 |
| 10 final typeMapping = const { | 11 final typeMapping = const { |
| 12 'null': null, |
| 11 'int': 0, | 13 'int': 0, |
| 14 'bigint': 18446744073709551617, |
| 12 'String': 'a', | 15 'String': 'a', |
| 13 'FileMode': FileMode.READ, | 16 'FileMode': FileMode.READ, |
| 14 'num': 0.50, | 17 'num': 0.50, |
| 15 'List<int>': const [1, 2, 3] | 18 'List<int>': const [1, 2, 3], |
| 19 'Map<String, int>': const { "a": 23 } |
| 16 }; | 20 }; |
| 17 | 21 |
| 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. |
| 18 doItSync(Function f) { | 44 doItSync(Function f) { |
| 19 // Ignore all exceptions. | |
| 20 try { f(); } catch (var e) {} | 45 try { f(); } catch (var e) {} |
| 21 } | 46 } |
| 22 | 47 |
| 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 |
| 23 fuzzSyncMethods() { | 63 fuzzSyncMethods() { |
| 24 typeMapping.forEach((k, v) { | 64 typeMapping.forEach((k, v) { |
| 25 doItSync(() { | 65 doItSync(() { |
| 26 var f = new File(v); | 66 var f = new File(v); |
| 27 doItSync(f.existsSync); | 67 doItSync(f.existsSync); |
| 28 doItSync(f.createSync); | 68 doItSync(f.createSync); |
| 29 doItSync(f.deleteSync); | 69 doItSync(f.deleteSync); |
| 30 doItSync(f.directorySync); | 70 doItSync(f.directorySync); |
| 31 doItSync(f.lengthSync); | 71 doItSync(f.lengthSync); |
| 32 doItSync(f.fullPathSync); | 72 doItSync(f.fullPathSync); |
| 33 doItSync(() => f.openInputStream().onError = (e) => null); | 73 doItSync(() => f.openInputStream().onError = (e) => null); |
| 34 doItSync(f.readAsBytesSync); | 74 doItSync(f.readAsBytesSync); |
| 75 doItSync(f.readAsTextSync); |
| 76 doItSync(f.readAsLinesSync); |
| 35 typeMapping.forEach((k2, v2) { | 77 typeMapping.forEach((k2, v2) { |
| 36 doItSync(() => f.openSync(v2)); | 78 doItSync(() => f.openSync(v2)); |
| 37 doItSync(() => f.openOutputStream(v2).onError = (e) => null); | 79 doItSync(() => f.openOutputStream(v2).onError = (e) => null); |
| 38 doItSync(() => f.readAsTextSync(v2)); | 80 doItSync(() => f.readAsTextSync(v2)); |
| 39 doItSync(() => f.readAsLinesSync(v2)); | 81 doItSync(() => f.readAsLinesSync(v2)); |
| 40 }); | 82 }); |
| 41 }); | 83 }); |
| 42 }); | 84 }); |
| 43 } | 85 } |
| 44 | 86 |
| 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 |
| 45 main() { | 137 main() { |
| 46 fuzzSyncMethods(); | 138 fuzzSyncMethods(); |
| 139 fuzzAsyncMethods(); |
| 140 fuzzSyncRandomAccessMethods(); |
| 47 } | 141 } |
| OLD | NEW |