| 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 // Tests socket exceptions. | 5 // Tests socket exceptions. |
| 6 | 6 |
| 7 import "package:expect/expect.dart"; | 7 import "package:expect/expect.dart"; |
| 8 import "dart:async"; | 8 import "dart:async"; |
| 9 import "dart:isolate"; | 9 import "dart:isolate"; |
| 10 import "dart:io"; | 10 import "dart:io"; |
| 11 | 11 |
| 12 class SocketExceptionTest { | 12 class SocketExceptionTest { |
| 13 | 13 |
| 14 static void serverSocketExceptionTest() { | 14 static void serverSocketExceptionTest() { |
| 15 bool exceptionCaught = false; | 15 bool exceptionCaught = false; |
| 16 bool wrongExceptionCaught = false; | 16 bool wrongExceptionCaught = false; |
| 17 | 17 |
| 18 ServerSocket.bind().then((server) { | 18 ServerSocket.bind("127.0.0.1", 0).then((server) { |
| 19 Expect.isNotNull(server); | 19 Expect.isNotNull(server); |
| 20 server.close(); | 20 server.close(); |
| 21 try { | 21 try { |
| 22 server.close(); | 22 server.close(); |
| 23 } on SocketIOException catch(ex) { | 23 } on SocketIOException catch(ex) { |
| 24 exceptionCaught = true; | 24 exceptionCaught = true; |
| 25 } catch (ex) { | 25 } catch (ex) { |
| 26 wrongExceptionCaught = true; | 26 wrongExceptionCaught = true; |
| 27 } | 27 } |
| 28 Expect.equals(false, exceptionCaught); | 28 Expect.equals(false, exceptionCaught); |
| 29 Expect.equals(true, !wrongExceptionCaught); | 29 Expect.equals(true, !wrongExceptionCaught); |
| 30 | 30 |
| 31 // Test invalid host. | 31 // Test invalid host. |
| 32 ServerSocket.bind("__INVALID_HOST__") | 32 ServerSocket.bind("__INVALID_HOST__", 0) |
| 33 .then((server) { }) | 33 .then((server) { }) |
| 34 .catchError((e) => e is SocketIOException); | 34 .catchError((e) => e is SocketIOException); |
| 35 }); | 35 }); |
| 36 } | 36 } |
| 37 | 37 |
| 38 static void serverSocketCloseListenTest() { | 38 static void serverSocketCloseListenTest() { |
| 39 var port = new ReceivePort(); | 39 var port = new ReceivePort(); |
| 40 ServerSocket.bind().then((server) { | 40 ServerSocket.bind("127.0.0.1", 0).then((server) { |
| 41 Socket.connect("127.0.0.1", server.port).then((socket) { | 41 Socket.connect("127.0.0.1", server.port).then((socket) { |
| 42 server.close(); | 42 server.close(); |
| 43 server.listen( | 43 server.listen( |
| 44 (incoming) => Expect.fail("Unexpected socket"), | 44 (incoming) => Expect.fail("Unexpected socket"), |
| 45 onDone: port.close); | 45 onDone: port.close); |
| 46 }); | 46 }); |
| 47 }); | 47 }); |
| 48 } | 48 } |
| 49 | 49 |
| 50 static void serverSocketListenCloseTest() { | 50 static void serverSocketListenCloseTest() { |
| 51 var port = new ReceivePort(); | 51 var port = new ReceivePort(); |
| 52 ServerSocket.bind().then((server) { | 52 ServerSocket.bind("127.0.0.1", 0).then((server) { |
| 53 Socket.connect("127.0.0.1", server.port).then((socket) { | 53 Socket.connect("127.0.0.1", server.port).then((socket) { |
| 54 server.listen( | 54 server.listen( |
| 55 (incoming) => server.close(), | 55 (incoming) => server.close(), |
| 56 onDone: port.close); | 56 onDone: port.close); |
| 57 }); | 57 }); |
| 58 }); | 58 }); |
| 59 } | 59 } |
| 60 | 60 |
| 61 static void clientSocketExceptionTest() { | 61 static void clientSocketExceptionTest() { |
| 62 bool exceptionCaught = false; | 62 bool exceptionCaught = false; |
| 63 bool wrongExceptionCaught = false; | 63 bool wrongExceptionCaught = false; |
| 64 | 64 |
| 65 ServerSocket.bind().then((server) { | 65 ServerSocket.bind("127.0.0.1", 0).then((server) { |
| 66 Expect.isNotNull(server); | 66 Expect.isNotNull(server); |
| 67 int port = server.port; | 67 int port = server.port; |
| 68 Socket.connect("127.0.0.1", port).then((client) { | 68 Socket.connect("127.0.0.1", port).then((client) { |
| 69 Expect.isNotNull(client); | 69 Expect.isNotNull(client); |
| 70 client.close(); | 70 client.close(); |
| 71 try { | 71 try { |
| 72 client.close(); | 72 client.close(); |
| 73 } on SocketIOException catch(ex) { | 73 } on SocketIOException catch(ex) { |
| 74 exceptionCaught = true; | 74 exceptionCaught = true; |
| 75 } catch (ex) { | 75 } catch (ex) { |
| (...skipping 21 matching lines...) Expand all Loading... |
| 97 } | 97 } |
| 98 Expect.isTrue(exceptionCaught); | 98 Expect.isTrue(exceptionCaught); |
| 99 Expect.isFalse(wrongExceptionCaught); | 99 Expect.isFalse(wrongExceptionCaught); |
| 100 | 100 |
| 101 server.close(); | 101 server.close(); |
| 102 }); | 102 }); |
| 103 }); | 103 }); |
| 104 } | 104 } |
| 105 | 105 |
| 106 static void clientSocketDestroyNoErrorTest() { | 106 static void clientSocketDestroyNoErrorTest() { |
| 107 ServerSocket.bind().then((server) { | 107 ServerSocket.bind("127.0.0.1", 0).then((server) { |
| 108 server.listen((socket) { | 108 server.listen((socket) { |
| 109 socket.pipe(socket); | 109 socket.pipe(socket); |
| 110 }); | 110 }); |
| 111 Socket.connect("127.0.0.1", server.port).then((client) { | 111 Socket.connect("127.0.0.1", server.port).then((client) { |
| 112 client.listen((data) {}, onDone: server.close); | 112 client.listen((data) {}, onDone: server.close); |
| 113 client.destroy(); | 113 client.destroy(); |
| 114 }); | 114 }); |
| 115 }); | 115 }); |
| 116 } | 116 } |
| 117 | 117 |
| 118 static void clientSocketAddDestroyNoErrorTest() { | 118 static void clientSocketAddDestroyNoErrorTest() { |
| 119 ServerSocket.bind().then((server) { | 119 ServerSocket.bind("127.0.0.1", 0).then((server) { |
| 120 server.listen((socket) { | 120 server.listen((socket) { |
| 121 // Passive block data by not sobscribing to socket. | 121 // Passive block data by not sobscribing to socket. |
| 122 }); | 122 }); |
| 123 Socket.connect("127.0.0.1", server.port).then((client) { | 123 Socket.connect("127.0.0.1", server.port).then((client) { |
| 124 client.listen((data) {}, onDone: server.close); | 124 client.listen((data) {}, onDone: server.close); |
| 125 client.add(new List.filled(1024 * 1024, 0)); | 125 client.add(new List.filled(1024 * 1024, 0)); |
| 126 client.destroy(); | 126 client.destroy(); |
| 127 }); | 127 }); |
| 128 }); | 128 }); |
| 129 } | 129 } |
| 130 | 130 |
| 131 static void clientSocketAddCloseNoErrorTest() { | 131 static void clientSocketAddCloseNoErrorTest() { |
| 132 ServerSocket.bind().then((server) { | 132 ServerSocket.bind("127.0.0.1", 0).then((server) { |
| 133 var completer = new Completer(); | 133 var completer = new Completer(); |
| 134 server.listen((socket) { | 134 server.listen((socket) { |
| 135 // The socket is 'paused' until the future completes. | 135 // The socket is 'paused' until the future completes. |
| 136 completer.future.then((_) => socket.pipe(socket)); | 136 completer.future.then((_) => socket.pipe(socket)); |
| 137 }); | 137 }); |
| 138 Socket.connect("127.0.0.1", server.port).then((client) { | 138 Socket.connect("127.0.0.1", server.port).then((client) { |
| 139 const int SIZE = 1024 * 1024; | 139 const int SIZE = 1024 * 1024; |
| 140 int count = 0; | 140 int count = 0; |
| 141 client.listen( | 141 client.listen( |
| 142 (data) => count += data.length, | 142 (data) => count += data.length, |
| 143 onDone: () { | 143 onDone: () { |
| 144 Expect.equals(SIZE, count); | 144 Expect.equals(SIZE, count); |
| 145 server.close(); | 145 server.close(); |
| 146 }); | 146 }); |
| 147 client.add(new List.filled(SIZE, 0)); | 147 client.add(new List.filled(SIZE, 0)); |
| 148 client.close(); | 148 client.close(); |
| 149 // Start piping now. | 149 // Start piping now. |
| 150 completer.complete(null); | 150 completer.complete(null); |
| 151 }); | 151 }); |
| 152 }); | 152 }); |
| 153 } | 153 } |
| 154 | 154 |
| 155 static void clientSocketAddCloseErrorTest() { | 155 static void clientSocketAddCloseErrorTest() { |
| 156 ServerSocket.bind().then((server) { | 156 ServerSocket.bind("127.0.0.1", 0).then((server) { |
| 157 var completer = new Completer(); | 157 var completer = new Completer(); |
| 158 server.listen((socket) { | 158 server.listen((socket) { |
| 159 completer.future.then((_) => socket.destroy()); | 159 completer.future.then((_) => socket.destroy()); |
| 160 }); | 160 }); |
| 161 Socket.connect("127.0.0.1", server.port).then((client) { | 161 Socket.connect("127.0.0.1", server.port).then((client) { |
| 162 const int SIZE = 1024 * 1024; | 162 const int SIZE = 1024 * 1024; |
| 163 int errors = 0; | 163 int errors = 0; |
| 164 client.listen( | 164 client.listen( |
| 165 (data) => Expect.fail("Unexpected data"), | 165 (data) => Expect.fail("Unexpected data"), |
| 166 onError: (error) { | 166 onError: (error) { |
| (...skipping 18 matching lines...) Expand all Loading... |
| 185 }, | 185 }, |
| 186 onError: (error) { | 186 onError: (error) { |
| 187 Expect.isTrue(error is SocketIOException); | 187 Expect.isTrue(error is SocketIOException); |
| 188 port.close(); | 188 port.close(); |
| 189 }); | 189 }); |
| 190 }); | 190 }); |
| 191 }); | 191 }); |
| 192 } | 192 } |
| 193 | 193 |
| 194 static void clientSocketAddCloseResultErrorTest() { | 194 static void clientSocketAddCloseResultErrorTest() { |
| 195 ServerSocket.bind().then((server) { | 195 ServerSocket.bind("127.0.0.1", 0).then((server) { |
| 196 var completer = new Completer(); | 196 var completer = new Completer(); |
| 197 server.listen((socket) { | 197 server.listen((socket) { |
| 198 completer.future.then((_) => socket.destroy()); | 198 completer.future.then((_) => socket.destroy()); |
| 199 }); | 199 }); |
| 200 Socket.connect("127.0.0.1", server.port).then((client) { | 200 Socket.connect("127.0.0.1", server.port).then((client) { |
| 201 const int SIZE = 1024 * 1024; | 201 const int SIZE = 1024 * 1024; |
| 202 int errors = 0; | 202 int errors = 0; |
| 203 client.add(new List.filled(SIZE, 0)); | 203 client.add(new List.filled(SIZE, 0)); |
| 204 client.close(); | 204 client.close(); |
| 205 client.done.catchError((error) { | 205 client.done.catchError((error) { |
| (...skipping 27 matching lines...) Expand all Loading... |
| 233 clientSocketAddCloseErrorTest(); | 233 clientSocketAddCloseErrorTest(); |
| 234 clientSocketAddCloseResultErrorTest(); | 234 clientSocketAddCloseResultErrorTest(); |
| 235 unknownHostTest(); | 235 unknownHostTest(); |
| 236 } | 236 } |
| 237 } | 237 } |
| 238 | 238 |
| 239 main() { | 239 main() { |
| 240 SocketExceptionTest.testMain(); | 240 SocketExceptionTest.testMain(); |
| 241 } | 241 } |
| 242 | 242 |
| OLD | NEW |