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 // Tests socket exceptions. | 5 // Tests socket exceptions. |
6 | 6 |
7 #import("dart:isolate"); | 7 #import("dart:isolate"); |
8 #import("dart:io"); | 8 #import("dart:io"); |
9 | 9 |
10 class SocketExceptionTest { | 10 class SocketExceptionTest { |
11 | 11 |
12 static const PORT = 0; | 12 static const PORT = 0; |
13 static const HOST = "127.0.0.1"; | 13 static const HOST = "127.0.0.1"; |
14 | 14 |
15 static void serverSocketExceptionTest() { | 15 static void serverSocketExceptionTest() { |
16 bool exceptionCaught = false; | 16 bool exceptionCaught = false; |
17 bool wrongExceptionCaught = false; | 17 bool wrongExceptionCaught = false; |
18 | 18 |
19 ServerSocket server = new ServerSocket(HOST, PORT, 10); | 19 ServerSocket server = new ServerSocket(HOST, PORT, 10); |
20 Expect.equals(true, server !== null); | 20 Expect.equals(true, server != null); |
21 server.close(); | 21 server.close(); |
22 try { | 22 try { |
23 server.close(); | 23 server.close(); |
24 } on SocketIOException catch(ex) { | 24 } on SocketIOException catch(ex) { |
25 exceptionCaught = true; | 25 exceptionCaught = true; |
26 } on Exception catch (ex) { | 26 } on Exception catch (ex) { |
27 wrongExceptionCaught = true; | 27 wrongExceptionCaught = true; |
28 } | 28 } |
29 Expect.equals(false, exceptionCaught); | 29 Expect.equals(false, exceptionCaught); |
30 Expect.equals(true, !wrongExceptionCaught); | 30 Expect.equals(true, !wrongExceptionCaught); |
31 | 31 |
32 // Test invalid host. | 32 // Test invalid host. |
33 Expect.throws(() => new ServerSocket("__INVALID_HOST__", PORT, 10), | 33 Expect.throws(() => new ServerSocket("__INVALID_HOST__", PORT, 10), |
34 (e) => e is SocketIOException); | 34 (e) => e is SocketIOException); |
35 } | 35 } |
36 | 36 |
37 static void clientSocketExceptionTest() { | 37 static void clientSocketExceptionTest() { |
38 bool exceptionCaught = false; | 38 bool exceptionCaught = false; |
39 bool wrongExceptionCaught = false; | 39 bool wrongExceptionCaught = false; |
40 | 40 |
41 ServerSocket server = new ServerSocket(HOST, PORT, 10); | 41 ServerSocket server = new ServerSocket(HOST, PORT, 10); |
42 Expect.equals(true, server !== null); | 42 Expect.equals(true, server != null); |
43 int port = server.port; | 43 int port = server.port; |
44 Socket client = new Socket(HOST, port); | 44 Socket client = new Socket(HOST, port); |
45 client.onConnect = () { | 45 client.onConnect = () { |
46 Expect.equals(true, client !== null); | 46 Expect.equals(true, client != null); |
47 InputStream input = client.inputStream; | 47 InputStream input = client.inputStream; |
48 OutputStream output = client.outputStream; | 48 OutputStream output = client.outputStream; |
49 client.close(); | 49 client.close(); |
50 try { | 50 try { |
51 client.close(); | 51 client.close(); |
52 } on SocketIOException catch(ex) { | 52 } on SocketIOException catch(ex) { |
53 exceptionCaught = true; | 53 exceptionCaught = true; |
54 } on Exception catch (ex) { | 54 } on Exception catch (ex) { |
55 wrongExceptionCaught = true; | 55 wrongExceptionCaught = true; |
56 } | 56 } |
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
113 | 113 |
114 server.close(); | 114 server.close(); |
115 }; | 115 }; |
116 } | 116 } |
117 | 117 |
118 static void indexOutOfRangeExceptionTest() { | 118 static void indexOutOfRangeExceptionTest() { |
119 bool exceptionCaught = false; | 119 bool exceptionCaught = false; |
120 bool wrongExceptionCaught = false; | 120 bool wrongExceptionCaught = false; |
121 | 121 |
122 ServerSocket server = new ServerSocket(HOST, PORT, 10); | 122 ServerSocket server = new ServerSocket(HOST, PORT, 10); |
123 Expect.equals(true, server !== null); | 123 Expect.equals(true, server != null); |
124 int port = server.port; | 124 int port = server.port; |
125 Socket client = new Socket(HOST, port); | 125 Socket client = new Socket(HOST, port); |
126 client.onConnect = () { | 126 client.onConnect = () { |
127 Expect.equals(true, client !== null); | 127 Expect.equals(true, client != null); |
128 try { | 128 try { |
129 List<int> buffer = new List<int>(10); | 129 List<int> buffer = new List<int>(10); |
130 client.readList(buffer, -1, 1); | 130 client.readList(buffer, -1, 1); |
131 } on RangeError catch (ex) { | 131 } on RangeError catch (ex) { |
132 exceptionCaught = true; | 132 exceptionCaught = true; |
133 } on Exception catch (ex) { | 133 } on Exception catch (ex) { |
134 wrongExceptionCaught = true; | 134 wrongExceptionCaught = true; |
135 } | 135 } |
136 Expect.equals(true, exceptionCaught); | 136 Expect.equals(true, exceptionCaught); |
137 Expect.equals(true, !wrongExceptionCaught); | 137 Expect.equals(true, !wrongExceptionCaught); |
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
192 clientSocketExceptionTest(); | 192 clientSocketExceptionTest(); |
193 indexOutOfRangeExceptionTest(); | 193 indexOutOfRangeExceptionTest(); |
194 unknownHostTest(); | 194 unknownHostTest(); |
195 } | 195 } |
196 } | 196 } |
197 | 197 |
198 main() { | 198 main() { |
199 SocketExceptionTest.testMain(); | 199 SocketExceptionTest.testMain(); |
200 } | 200 } |
201 | 201 |
OLD | NEW |