OLD | NEW |
| (Empty) |
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 | |
3 // BSD-style license that can be found in the LICENSE file. | |
4 // | |
5 // Tests socket exceptions. | |
6 | |
7 #import("dart:isolate"); | |
8 #import("dart:io"); | |
9 | |
10 class SocketExceptionTest { | |
11 | |
12 static final PORT = 0; | |
13 static final HOST = "127.0.0.1"; | |
14 | |
15 static void serverSocketExceptionTest() { | |
16 bool exceptionCaught = false; | |
17 bool wrongExceptionCaught = false; | |
18 | |
19 ServerSocket server = new ServerSocket(HOST, PORT, 10); | |
20 Expect.equals(true, server !== null); | |
21 server.close(); | |
22 try { | |
23 server.close(); | |
24 } catch (SocketIOException ex) { | |
25 exceptionCaught = true; | |
26 } catch (Exception ex) { | |
27 wrongExceptionCaught = true; | |
28 } | |
29 Expect.equals(false, exceptionCaught); | |
30 Expect.equals(true, !wrongExceptionCaught); | |
31 } | |
32 | |
33 static void clientSocketExceptionTest() { | |
34 bool exceptionCaught = false; | |
35 bool wrongExceptionCaught = false; | |
36 | |
37 ServerSocket server = new ServerSocket(HOST, PORT, 10); | |
38 Expect.equals(true, server !== null); | |
39 int port = server.port; | |
40 Socket client = new Socket(HOST, port); | |
41 client.onConnect = () { | |
42 Expect.equals(true, client !== null); | |
43 InputStream input = client.inputStream; | |
44 OutputStream output = client.outputStream; | |
45 client.close(); | |
46 try { | |
47 client.close(); | |
48 } catch (SocketIOException ex) { | |
49 exceptionCaught = true; | |
50 } catch (Exception ex) { | |
51 wrongExceptionCaught = true; | |
52 } | |
53 Expect.equals(false, exceptionCaught); | |
54 Expect.equals(true, !wrongExceptionCaught); | |
55 exceptionCaught = false; | |
56 try { | |
57 client.available(); | |
58 } catch (SocketIOException ex) { | |
59 exceptionCaught = true; | |
60 } catch (Exception ex) { | |
61 wrongExceptionCaught = true; | |
62 } | |
63 Expect.equals(true, exceptionCaught); | |
64 Expect.equals(true, !wrongExceptionCaught); | |
65 exceptionCaught = false; | |
66 try { | |
67 List<int> buffer = new List<int>(10); | |
68 client.readList(buffer, 0 , 10); | |
69 } catch (SocketIOException ex) { | |
70 exceptionCaught = true; | |
71 } catch (Exception ex) { | |
72 wrongExceptionCaught = true; | |
73 } | |
74 Expect.equals(true, exceptionCaught); | |
75 Expect.equals(true, !wrongExceptionCaught); | |
76 exceptionCaught = false; | |
77 try { | |
78 List<int> buffer = new List<int>(10); | |
79 client.writeList(buffer, 0, 10); | |
80 } catch (SocketIOException ex) { | |
81 exceptionCaught = true; | |
82 } catch (Exception ex) { | |
83 wrongExceptionCaught = true; | |
84 } | |
85 Expect.equals(true, exceptionCaught); | |
86 Expect.equals(true, !wrongExceptionCaught); | |
87 exceptionCaught = false; | |
88 try { | |
89 List<int> buffer = new List<int>(42); | |
90 input.readInto(buffer, 0, 12); | |
91 } catch (SocketIOException ex) { | |
92 exceptionCaught = true; | |
93 } catch (Exception ex) { | |
94 wrongExceptionCaught = true; | |
95 } | |
96 Expect.equals(true, exceptionCaught); | |
97 Expect.equals(true, !wrongExceptionCaught); | |
98 exceptionCaught = false; | |
99 try { | |
100 List<int> buffer = new List<int>(42); | |
101 output.writeFrom(buffer, 0, 12); | |
102 } catch (SocketIOException ex) { | |
103 exceptionCaught = true; | |
104 } catch (Exception ex) { | |
105 wrongExceptionCaught = true; | |
106 } | |
107 Expect.equals(true, exceptionCaught); | |
108 Expect.equals(true, !wrongExceptionCaught); | |
109 | |
110 server.close(); | |
111 }; | |
112 } | |
113 | |
114 static void indexOutOfRangeExceptionTest() { | |
115 bool exceptionCaught = false; | |
116 bool wrongExceptionCaught = false; | |
117 | |
118 ServerSocket server = new ServerSocket(HOST, PORT, 10); | |
119 Expect.equals(true, server !== null); | |
120 int port = server.port; | |
121 Socket client = new Socket(HOST, port); | |
122 client.onConnect = () { | |
123 Expect.equals(true, client !== null); | |
124 try { | |
125 List<int> buffer = new List<int>(10); | |
126 client.readList(buffer, -1, 1); | |
127 } catch (IndexOutOfRangeException ex) { | |
128 exceptionCaught = true; | |
129 } catch (Exception ex) { | |
130 wrongExceptionCaught = true; | |
131 } | |
132 Expect.equals(true, exceptionCaught); | |
133 Expect.equals(true, !wrongExceptionCaught); | |
134 exceptionCaught = false; | |
135 | |
136 try { | |
137 List<int> buffer = new List<int>(10); | |
138 client.readList(buffer, 0, -1); | |
139 } catch (IndexOutOfRangeException ex) { | |
140 exceptionCaught = true; | |
141 } catch (Exception ex) { | |
142 wrongExceptionCaught = true; | |
143 } | |
144 Expect.equals(true, exceptionCaught); | |
145 Expect.equals(true, !wrongExceptionCaught); | |
146 exceptionCaught = false; | |
147 | |
148 try { | |
149 List<int> buffer = new List<int>(10); | |
150 client.writeList(buffer, -1, 1); | |
151 } catch (IndexOutOfRangeException ex) { | |
152 exceptionCaught = true; | |
153 } catch (Exception ex) { | |
154 wrongExceptionCaught = true; | |
155 } | |
156 Expect.equals(true, exceptionCaught); | |
157 Expect.equals(true, !wrongExceptionCaught); | |
158 exceptionCaught = false; | |
159 | |
160 try { | |
161 List<int> buffer = new List<int>(10); | |
162 client.writeList(buffer, 0, -1); | |
163 } catch (IndexOutOfRangeException ex) { | |
164 exceptionCaught = true; | |
165 } catch (Exception ex) { | |
166 wrongExceptionCaught = true; | |
167 } | |
168 Expect.equals(true, exceptionCaught); | |
169 Expect.equals(true, !wrongExceptionCaught); | |
170 | |
171 server.close(); | |
172 client.close(); | |
173 }; | |
174 } | |
175 | |
176 static void unknownHostTest() { | |
177 // Port to verify that the test completes. | |
178 var port = new ReceivePort(); | |
179 port.receive((message, replyTo) => null); | |
180 | |
181 Socket s = new Socket("hede.hule.hest", 1234); | |
182 s.onError = (e) => port.close(); | |
183 s.onConnect = () => Expect.fail("Connection completed"); | |
184 } | |
185 | |
186 static void unresponsiveHostTest() { | |
187 // Port to keep the VM alive until test completes. | |
188 var port = new ReceivePort(); | |
189 port.receive((message, replyTo) => null); | |
190 | |
191 Socket s = new Socket("127.0.0.1", 65535); | |
192 s.onError = (e) => port.close(); | |
193 s.onConnect = () => Expect.fail("Connection completed"); | |
194 } | |
195 | |
196 static void testMain() { | |
197 serverSocketExceptionTest(); | |
198 clientSocketExceptionTest(); | |
199 indexOutOfRangeExceptionTest(); | |
200 unknownHostTest(); | |
201 // TODO(sgjesse): This test seems to fail on the buildbot. | |
202 //unresponsiveHostTest(); | |
203 } | |
204 } | |
205 | |
206 main() { | |
207 SocketExceptionTest.testMain(); | |
208 } | |
OLD | NEW |