| 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 "dart:async"; |
| 8 import "dart:io"; |
| 9 |
| 10 import "package:async_helper/async_helper.dart"; |
| 7 import "package:expect/expect.dart"; | 11 import "package:expect/expect.dart"; |
| 8 import "dart:async"; | |
| 9 import "dart:isolate"; | |
| 10 import "dart:io"; | |
| 11 | 12 |
| 12 class SocketExceptionTest { | 13 class SocketExceptionTest { |
| 13 | 14 |
| 14 static void serverSocketExceptionTest() { | 15 static void serverSocketExceptionTest() { |
| 15 bool exceptionCaught = false; | 16 bool exceptionCaught = false; |
| 16 bool wrongExceptionCaught = false; | 17 bool wrongExceptionCaught = false; |
| 17 | 18 |
| 18 ServerSocket.bind("127.0.0.1", 0).then((server) { | 19 ServerSocket.bind("127.0.0.1", 0).then((server) { |
| 19 Expect.isNotNull(server); | 20 Expect.isNotNull(server); |
| 20 server.close(); | 21 server.close(); |
| 21 try { | 22 try { |
| 22 server.close(); | 23 server.close(); |
| 23 } on SocketException catch(ex) { | 24 } on SocketException catch(ex) { |
| 24 exceptionCaught = true; | 25 exceptionCaught = true; |
| 25 } catch (ex) { | 26 } catch (ex) { |
| 26 wrongExceptionCaught = true; | 27 wrongExceptionCaught = true; |
| 27 } | 28 } |
| 28 Expect.equals(false, exceptionCaught); | 29 Expect.equals(false, exceptionCaught); |
| 29 Expect.equals(true, !wrongExceptionCaught); | 30 Expect.equals(true, !wrongExceptionCaught); |
| 30 | 31 |
| 31 // Test invalid host. | 32 // Test invalid host. |
| 32 ServerSocket.bind("__INVALID_HOST__", 0) | 33 ServerSocket.bind("__INVALID_HOST__", 0) |
| 33 .then((server) { }) | 34 .then((server) { }) |
| 34 .catchError((e) => e is SocketException); | 35 .catchError((e) => e is SocketException); |
| 35 }); | 36 }); |
| 36 } | 37 } |
| 37 | 38 |
| 38 static void serverSocketCloseListenTest() { | 39 static void serverSocketCloseListenTest() { |
| 39 var port = new ReceivePort(); | 40 asyncStart(); |
| 40 ServerSocket.bind("127.0.0.1", 0).then((server) { | 41 ServerSocket.bind("127.0.0.1", 0).then((server) { |
| 41 Socket.connect("127.0.0.1", server.port).then((socket) { | 42 Socket.connect("127.0.0.1", server.port).then((socket) { |
| 42 server.close(); | 43 server.close(); |
| 43 server.listen( | 44 server.listen( |
| 44 (incoming) => Expect.fail("Unexpected socket"), | 45 (incoming) => Expect.fail("Unexpected socket"), |
| 45 onDone: port.close); | 46 onDone: asyncEnd); |
| 46 }); | 47 }); |
| 47 }); | 48 }); |
| 48 } | 49 } |
| 49 | 50 |
| 50 static void serverSocketListenCloseTest() { | 51 static void serverSocketListenCloseTest() { |
| 51 var port = new ReceivePort(); | 52 asyncStart(); |
| 52 ServerSocket.bind("127.0.0.1", 0).then((server) { | 53 ServerSocket.bind("127.0.0.1", 0).then((server) { |
| 53 Socket.connect("127.0.0.1", server.port).then((socket) { | 54 Socket.connect("127.0.0.1", server.port).then((socket) { |
| 54 server.listen( | 55 server.listen( |
| 55 (incoming) => server.close(), | 56 (incoming) => server.close(), |
| 56 onDone: port.close); | 57 onDone: asyncEnd); |
| 57 }); | 58 }); |
| 58 }); | 59 }); |
| 59 } | 60 } |
| 60 | 61 |
| 61 static void clientSocketExceptionTest() { | 62 static void clientSocketExceptionTest() { |
| 62 bool exceptionCaught = false; | 63 bool exceptionCaught = false; |
| 63 bool wrongExceptionCaught = false; | 64 bool wrongExceptionCaught = false; |
| 64 | 65 |
| 65 ServerSocket.bind("127.0.0.1", 0).then((server) { | 66 ServerSocket.bind("127.0.0.1", 0).then((server) { |
| 66 Expect.isNotNull(server); | 67 Expect.isNotNull(server); |
| (...skipping 104 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 171 // We get either a close or an error followed by a close | 172 // We get either a close or an error followed by a close |
| 172 // on the socket. Whether we get both depends on | 173 // on the socket. Whether we get both depends on |
| 173 // whether the system notices the error for the read | 174 // whether the system notices the error for the read |
| 174 // event or only for the write event. | 175 // event or only for the write event. |
| 175 Expect.isTrue(errors <= 1); | 176 Expect.isTrue(errors <= 1); |
| 176 server.close(); | 177 server.close(); |
| 177 }); | 178 }); |
| 178 client.add(new List.filled(SIZE, 0)); | 179 client.add(new List.filled(SIZE, 0)); |
| 179 // Destroy other socket now. | 180 // Destroy other socket now. |
| 180 completer.complete(null); | 181 completer.complete(null); |
| 181 var port = new ReceivePort(); | 182 asyncStart(); |
| 182 client.done.then( | 183 client.done.then( |
| 183 (_) { | 184 (_) { |
| 184 Expect.fail("Expected error"); | 185 Expect.fail("Expected error"); |
| 185 }, | 186 }, |
| 186 onError: (error) { | 187 onError: (error) { |
| 187 Expect.isTrue(error is SocketException); | 188 Expect.isTrue(error is SocketException); |
| 188 port.close(); | 189 asyncEnd(); |
| 189 }); | 190 }); |
| 190 }); | 191 }); |
| 191 }); | 192 }); |
| 192 } | 193 } |
| 193 | 194 |
| 194 static void clientSocketAddCloseResultErrorTest() { | 195 static void clientSocketAddCloseResultErrorTest() { |
| 195 ServerSocket.bind("127.0.0.1", 0).then((server) { | 196 ServerSocket.bind("127.0.0.1", 0).then((server) { |
| 196 var completer = new Completer(); | 197 var completer = new Completer(); |
| 197 server.listen((socket) { | 198 server.listen((socket) { |
| 198 completer.future.then((_) => socket.destroy()); | 199 completer.future.then((_) => socket.destroy()); |
| 199 }); | 200 }); |
| 200 Socket.connect("127.0.0.1", server.port).then((client) { | 201 Socket.connect("127.0.0.1", server.port).then((client) { |
| 201 const int SIZE = 1024 * 1024; | 202 const int SIZE = 1024 * 1024; |
| 202 int errors = 0; | 203 int errors = 0; |
| 203 client.add(new List.filled(SIZE, 0)); | 204 client.add(new List.filled(SIZE, 0)); |
| 204 client.close(); | 205 client.close(); |
| 205 client.done.catchError((error) { | 206 client.done.catchError((error) { |
| 206 server.close(); | 207 server.close(); |
| 207 }); | 208 }); |
| 208 // Destroy other socket now. | 209 // Destroy other socket now. |
| 209 completer.complete(null); | 210 completer.complete(null); |
| 210 }); | 211 }); |
| 211 }); | 212 }); |
| 212 } | 213 } |
| 213 | 214 |
| 214 static void unknownHostTest() { | 215 static void unknownHostTest() { |
| 215 // Port to verify that the test completes. | 216 asyncStart(); |
| 216 var port = new ReceivePort(); | |
| 217 port.receive((message, replyTo) => null); | |
| 218 | |
| 219 Socket.connect("hede.hule.hest", 1234) | 217 Socket.connect("hede.hule.hest", 1234) |
| 220 .then((socket) => Expect.fail("Connection completed")) | 218 .then((socket) => Expect.fail("Connection completed")) |
| 221 .catchError((e) => port.close(), test: (e) => e is SocketException); | 219 .catchError((e) => asyncEnd(), test: (e) => e is SocketException); |
| 222 | 220 |
| 223 } | 221 } |
| 224 | 222 |
| 225 static void testMain() { | 223 static void testMain() { |
| 226 serverSocketExceptionTest(); | 224 serverSocketExceptionTest(); |
| 227 serverSocketCloseListenTest(); | 225 serverSocketCloseListenTest(); |
| 228 serverSocketListenCloseTest(); | 226 serverSocketListenCloseTest(); |
| 229 clientSocketExceptionTest(); | 227 clientSocketExceptionTest(); |
| 230 clientSocketDestroyNoErrorTest(); | 228 clientSocketDestroyNoErrorTest(); |
| 231 clientSocketAddDestroyNoErrorTest(); | 229 clientSocketAddDestroyNoErrorTest(); |
| 232 clientSocketAddCloseNoErrorTest(); | 230 clientSocketAddCloseNoErrorTest(); |
| 233 clientSocketAddCloseErrorTest(); | 231 clientSocketAddCloseErrorTest(); |
| 234 clientSocketAddCloseResultErrorTest(); | 232 clientSocketAddCloseResultErrorTest(); |
| 235 unknownHostTest(); | 233 unknownHostTest(); |
| 236 } | 234 } |
| 237 } | 235 } |
| 238 | 236 |
| 239 main() { | 237 main() { |
| 238 asyncStart(); |
| 240 SocketExceptionTest.testMain(); | 239 SocketExceptionTest.testMain(); |
| 240 asyncEnd(); |
| 241 } | 241 } |
| 242 | 242 |
| OLD | NEW |