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("127.0.0.1", 0).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 SocketException 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__", 0) | 32 ServerSocket.bind("__INVALID_HOST__", 0) |
33 .then((server) { }) | 33 .then((server) { }) |
34 .catchError((e) => e is SocketIOException); | 34 .catchError((e) => e is SocketException); |
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("127.0.0.1", 0).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"), |
(...skipping 18 matching lines...) Expand all Loading... |
63 bool wrongExceptionCaught = false; | 63 bool wrongExceptionCaught = false; |
64 | 64 |
65 ServerSocket.bind("127.0.0.1", 0).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 SocketException catch(ex) { |
74 exceptionCaught = true; | 74 exceptionCaught = true; |
75 } catch (ex) { | 75 } catch (ex) { |
76 wrongExceptionCaught = true; | 76 wrongExceptionCaught = true; |
77 } | 77 } |
78 Expect.isFalse(exceptionCaught); | 78 Expect.isFalse(exceptionCaught); |
79 Expect.isFalse(wrongExceptionCaught); | 79 Expect.isFalse(wrongExceptionCaught); |
80 try { | 80 try { |
81 client.destroy(); | 81 client.destroy(); |
82 } on SocketIOException catch(ex) { | 82 } on SocketException catch(ex) { |
83 exceptionCaught = true; | 83 exceptionCaught = true; |
84 } catch (ex) { | 84 } catch (ex) { |
85 print(ex); | 85 print(ex); |
86 wrongExceptionCaught = true; | 86 wrongExceptionCaught = true; |
87 } | 87 } |
88 Expect.isFalse(exceptionCaught); | 88 Expect.isFalse(exceptionCaught); |
89 Expect.isFalse(wrongExceptionCaught); | 89 Expect.isFalse(wrongExceptionCaught); |
90 try { | 90 try { |
91 List<int> buffer = new List<int>(10); | 91 List<int> buffer = new List<int>(10); |
92 client.add(buffer); | 92 client.add(buffer); |
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
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) { |
167 Expect.isTrue(error is SocketIOException); | 167 Expect.isTrue(error is SocketException); |
168 errors++; | 168 errors++; |
169 }, | 169 }, |
170 onDone: () { | 170 onDone: () { |
171 // We get either a close or an error followed by a close | 171 // We get either a close or an error followed by a close |
172 // on the socket. Whether we get both depends on | 172 // on the socket. Whether we get both depends on |
173 // whether the system notices the error for the read | 173 // whether the system notices the error for the read |
174 // event or only for the write event. | 174 // event or only for the write event. |
175 Expect.isTrue(errors <= 1); | 175 Expect.isTrue(errors <= 1); |
176 server.close(); | 176 server.close(); |
177 }); | 177 }); |
178 client.add(new List.filled(SIZE, 0)); | 178 client.add(new List.filled(SIZE, 0)); |
179 // Destroy other socket now. | 179 // Destroy other socket now. |
180 completer.complete(null); | 180 completer.complete(null); |
181 var port = new ReceivePort(); | 181 var port = new ReceivePort(); |
182 client.done.then( | 182 client.done.then( |
183 (_) { | 183 (_) { |
184 Expect.fail("Expected error"); | 184 Expect.fail("Expected error"); |
185 }, | 185 }, |
186 onError: (error) { | 186 onError: (error) { |
187 Expect.isTrue(error is SocketIOException); | 187 Expect.isTrue(error is SocketException); |
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("127.0.0.1", 0).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) { |
(...skipping 13 matching lines...) Expand all Loading... |
211 }); | 211 }); |
212 } | 212 } |
213 | 213 |
214 static void unknownHostTest() { | 214 static void unknownHostTest() { |
215 // Port to verify that the test completes. | 215 // Port to verify that the test completes. |
216 var port = new ReceivePort(); | 216 var port = new ReceivePort(); |
217 port.receive((message, replyTo) => null); | 217 port.receive((message, replyTo) => null); |
218 | 218 |
219 Socket.connect("hede.hule.hest", 1234) | 219 Socket.connect("hede.hule.hest", 1234) |
220 .then((socket) => Expect.fail("Connection completed")) | 220 .then((socket) => Expect.fail("Connection completed")) |
221 .catchError((e) => port.close(), test: (e) => e is SocketIOException); | 221 .catchError((e) => port.close(), test: (e) => e is SocketException); |
222 | 222 |
223 } | 223 } |
224 | 224 |
225 static void testMain() { | 225 static void testMain() { |
226 serverSocketExceptionTest(); | 226 serverSocketExceptionTest(); |
227 serverSocketCloseListenTest(); | 227 serverSocketCloseListenTest(); |
228 serverSocketListenCloseTest(); | 228 serverSocketListenCloseTest(); |
229 clientSocketExceptionTest(); | 229 clientSocketExceptionTest(); |
230 clientSocketDestroyNoErrorTest(); | 230 clientSocketDestroyNoErrorTest(); |
231 clientSocketAddDestroyNoErrorTest(); | 231 clientSocketAddDestroyNoErrorTest(); |
232 clientSocketAddCloseNoErrorTest(); | 232 clientSocketAddCloseNoErrorTest(); |
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 |