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"; | 7 import "dart:async"; |
8 import "dart:io"; | 8 import "dart:io"; |
9 | 9 |
10 import "package:async_helper/async_helper.dart"; | 10 import "package:async_helper/async_helper.dart"; |
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
64 } | 64 } |
65 | 65 |
66 static void clientSocketExceptionTest() { | 66 static void clientSocketExceptionTest() { |
67 bool exceptionCaught = false; | 67 bool exceptionCaught = false; |
68 bool wrongExceptionCaught = false; | 68 bool wrongExceptionCaught = false; |
69 | 69 |
70 ServerSocket.bind("127.0.0.1", 0).then((server) { | 70 ServerSocket.bind("127.0.0.1", 0).then((server) { |
71 Expect.isNotNull(server); | 71 Expect.isNotNull(server); |
72 int port = server.port; | 72 int port = server.port; |
73 Socket.connect("127.0.0.1", port).then((client) { | 73 Socket.connect("127.0.0.1", port).then((client) { |
74 Expect.isNotNull(client); | 74 Expect.isNotNull(client); |
75 client.close(); | 75 client.close(); |
| 76 // First calls for which exceptions are note expected. |
76 try { | 77 try { |
77 client.close(); | 78 client.close(); |
78 } on SocketException catch(ex) { | 79 } on SocketException catch(ex) { |
79 exceptionCaught = true; | 80 exceptionCaught = true; |
80 } catch (ex) { | 81 } catch (ex) { |
81 wrongExceptionCaught = true; | 82 wrongExceptionCaught = true; |
82 } | 83 } |
83 Expect.isFalse(exceptionCaught); | 84 Expect.isFalse(exceptionCaught); |
84 Expect.isFalse(wrongExceptionCaught); | 85 Expect.isFalse(wrongExceptionCaught); |
85 try { | 86 try { |
86 client.destroy(); | 87 client.destroy(); |
87 } on SocketException catch(ex) { | 88 } on SocketException catch(ex) { |
88 exceptionCaught = true; | 89 exceptionCaught = true; |
89 } catch (ex) { | 90 } catch (ex) { |
90 print(ex); | |
91 wrongExceptionCaught = true; | 91 wrongExceptionCaught = true; |
92 } | 92 } |
93 Expect.isFalse(exceptionCaught); | 93 Expect.isFalse(exceptionCaught); |
94 Expect.isFalse(wrongExceptionCaught); | 94 Expect.isFalse(wrongExceptionCaught); |
95 try { | 95 try { |
96 List<int> buffer = new List<int>(10); | 96 List<int> buffer = new List<int>(10); |
97 client.add(buffer); | 97 client.add(buffer); |
98 } on StateError catch (ex) { | 98 } on StateError catch (ex) { |
99 exceptionCaught = true; | 99 exceptionCaught = true; |
100 } catch (ex) { | 100 } catch (ex) { |
101 wrongExceptionCaught = true; | 101 wrongExceptionCaught = true; |
102 } | 102 } |
103 Expect.isFalse(exceptionCaught); | 103 Expect.isFalse(exceptionCaught); |
104 Expect.isFalse(wrongExceptionCaught); | 104 Expect.isFalse(wrongExceptionCaught); |
105 | 105 |
| 106 // From here exceptions are expected. |
| 107 exceptionCaught = false; |
| 108 try { |
| 109 client.port; |
| 110 } on SocketException catch(ex) { |
| 111 exceptionCaught = true; |
| 112 } catch (ex) { |
| 113 wrongExceptionCaught = true; |
| 114 } |
| 115 Expect.isTrue(exceptionCaught); |
| 116 Expect.isFalse(wrongExceptionCaught); |
| 117 exceptionCaught = false; |
| 118 try { |
| 119 client.remotePort; |
| 120 } on SocketException catch(ex) { |
| 121 exceptionCaught = true; |
| 122 } catch (ex) { |
| 123 wrongExceptionCaught = true; |
| 124 } |
| 125 Expect.isTrue(exceptionCaught); |
| 126 Expect.isFalse(wrongExceptionCaught); |
| 127 exceptionCaught = false; |
| 128 try { |
| 129 client.address; |
| 130 } on SocketException catch(ex) { |
| 131 exceptionCaught = true; |
| 132 } catch (ex) { |
| 133 wrongExceptionCaught = true; |
| 134 } |
| 135 Expect.isTrue(exceptionCaught); |
| 136 Expect.isFalse(wrongExceptionCaught); |
| 137 exceptionCaught = false; |
| 138 try { |
| 139 client.remoteAddress; |
| 140 } on SocketException catch(ex) { |
| 141 exceptionCaught = true; |
| 142 } catch (ex) { |
| 143 wrongExceptionCaught = true; |
| 144 } |
| 145 Expect.isTrue(exceptionCaught); |
| 146 Expect.isFalse(wrongExceptionCaught); |
| 147 |
106 server.close(); | 148 server.close(); |
107 }); | 149 }); |
108 }); | 150 }); |
109 } | 151 } |
110 | 152 |
111 static void clientSocketDestroyNoErrorTest() { | 153 static void clientSocketDestroyNoErrorTest() { |
112 ServerSocket.bind("127.0.0.1", 0).then((server) { | 154 ServerSocket.bind("127.0.0.1", 0).then((server) { |
113 server.listen((socket) { | 155 server.listen((socket) { |
114 socket.pipe(socket); | 156 socket.pipe(socket); |
115 }); | 157 }); |
(...skipping 123 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
239 unknownHostTest(); | 281 unknownHostTest(); |
240 } | 282 } |
241 } | 283 } |
242 | 284 |
243 main() { | 285 main() { |
244 asyncStart(); | 286 asyncStart(); |
245 SocketExceptionTest.testMain(); | 287 SocketExceptionTest.testMain(); |
246 asyncEnd(); | 288 asyncEnd(); |
247 } | 289 } |
248 | 290 |
OLD | NEW |