Chromium Code Reviews

Side by Side Diff: tests/standalone/io/socket_exception_test.dart

Issue 12316036: Merge IO v2 branch to bleeding edge (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 7 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff | | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2012, 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:isolate"; 8 import "dart:isolate";
8 import "dart:io"; 9 import "dart:io";
9 10
10 class SocketExceptionTest { 11 class SocketExceptionTest {
11 12
12 static const PORT = 0;
13 static const HOST = "127.0.0.1";
14
15 static void serverSocketExceptionTest() { 13 static void serverSocketExceptionTest() {
16 bool exceptionCaught = false; 14 bool exceptionCaught = false;
17 bool wrongExceptionCaught = false; 15 bool wrongExceptionCaught = false;
18 16
19 ServerSocket server = new ServerSocket(HOST, PORT, 10); 17 ServerSocket.bind().then((server) {
20 Expect.equals(true, server != null); 18 Expect.isNotNull(server);
21 server.close();
22 try {
23 server.close(); 19 server.close();
24 } on SocketIOException catch(ex) {
25 exceptionCaught = true;
26 } on Exception catch (ex) {
27 wrongExceptionCaught = true;
28 }
29 Expect.equals(false, exceptionCaught);
30 Expect.equals(true, !wrongExceptionCaught);
31
32 // Test invalid host.
33 Expect.throws(() => new ServerSocket("__INVALID_HOST__", PORT, 10),
34 (e) => e is SocketIOException);
35 }
36
37 static void clientSocketExceptionTest() {
38 bool exceptionCaught = false;
39 bool wrongExceptionCaught = false;
40
41 ServerSocket server = new ServerSocket(HOST, PORT, 10);
42 Expect.equals(true, server != null);
43 int port = server.port;
44 Socket client = new Socket(HOST, port);
45 client.onConnect = () {
46 Expect.equals(true, client != null);
47 InputStream input = client.inputStream;
48 OutputStream output = client.outputStream;
49 client.close();
50 try { 20 try {
51 client.close(); 21 server.close();
52 } on SocketIOException catch(ex) { 22 } on SocketIOException catch(ex) {
53 exceptionCaught = true; 23 exceptionCaught = true;
54 } on Exception catch (ex) { 24 } catch (ex) {
55 wrongExceptionCaught = true; 25 wrongExceptionCaught = true;
56 } 26 }
57 Expect.equals(false, exceptionCaught); 27 Expect.equals(false, exceptionCaught);
58 Expect.equals(true, !wrongExceptionCaught); 28 Expect.equals(true, !wrongExceptionCaught);
59 exceptionCaught = false; 29
60 try { 30 // Test invalid host.
61 client.available(); 31 ServerSocket.bind("__INVALID_HOST__")
62 } on SocketIOException catch(ex) { 32 .then((server) { })
63 exceptionCaught = true; 33 .catchError((e) => e is SocketIOException);
64 } on Exception catch (ex) { 34 });
65 wrongExceptionCaught = true; 35 }
66 } 36
67 Expect.equals(true, exceptionCaught); 37 static void serverSocketCloseListenTest() {
68 Expect.equals(true, !wrongExceptionCaught); 38 var port = new ReceivePort();
69 exceptionCaught = false; 39 ServerSocket.bind().then((server) {
70 try { 40 Socket.connect("127.0.0.1", server.port).then((socket) {
71 List<int> buffer = new List<int>.fixedLength(10); 41 server.close();
72 client.readList(buffer, 0 , 10); 42 server.listen(
73 } on SocketIOException catch(ex) { 43 (incoming) => Expect.fail("Unexpected socket"),
74 exceptionCaught = true; 44 onDone: port.close);
75 } on Exception catch (ex) { 45 });
76 wrongExceptionCaught = true; 46 });
77 } 47 }
78 Expect.equals(true, exceptionCaught); 48
79 Expect.equals(true, !wrongExceptionCaught); 49 static void serverSocketListenCloseTest() {
80 exceptionCaught = false; 50 var port = new ReceivePort();
81 try { 51 ServerSocket.bind().then((server) {
82 List<int> buffer = new List<int>.fixedLength(10); 52 Socket.connect("127.0.0.1", server.port).then((socket) {
83 client.writeList(buffer, 0, 10); 53 server.listen(
84 } on SocketIOException catch(ex) { 54 (incoming) => server.close(),
85 exceptionCaught = true; 55 onDone: port.close());
86 } on Exception catch (ex) { 56 });
87 wrongExceptionCaught = true; 57 });
88 } 58 }
89 Expect.equals(true, exceptionCaught); 59
90 Expect.equals(true, !wrongExceptionCaught); 60 static void clientSocketExceptionTest() {
91 exceptionCaught = false;
92 try {
93 List<int> buffer = new List<int>.fixedLength(42);
94 input.readInto(buffer, 0, 12);
95 } on SocketIOException catch(ex) {
96 exceptionCaught = true;
97 } on Exception catch (ex) {
98 wrongExceptionCaught = true;
99 }
100 Expect.equals(true, exceptionCaught);
101 Expect.equals(true, !wrongExceptionCaught);
102 exceptionCaught = false;
103 try {
104 List<int> buffer = new List<int>.fixedLength(42);
105 output.writeFrom(buffer, 0, 12);
106 } on SocketIOException catch(ex) {
107 exceptionCaught = true;
108 } on Exception catch (ex) {
109 wrongExceptionCaught = true;
110 }
111 Expect.equals(true, exceptionCaught);
112 Expect.equals(true, !wrongExceptionCaught);
113
114 server.close();
115 };
116 }
117
118 static void indexOutOfRangeExceptionTest() {
119 bool exceptionCaught = false; 61 bool exceptionCaught = false;
120 bool wrongExceptionCaught = false; 62 bool wrongExceptionCaught = false;
121 63
122 ServerSocket server = new ServerSocket(HOST, PORT, 10); 64 ServerSocket.bind().then((server) {
123 Expect.equals(true, server != null); 65 Expect.isNotNull(server);
124 int port = server.port; 66 int port = server.port;
125 Socket client = new Socket(HOST, port); 67 Socket.connect("127.0.0.1", port).then((client) {
126 client.onConnect = () { 68 Expect.isNotNull(client);
127 Expect.equals(true, client != null); 69 client.close();
128 try { 70 try {
129 List<int> buffer = new List<int>.fixedLength(10); 71 client.close();
130 client.readList(buffer, -1, 1); 72 } on SocketIOException catch(ex) {
131 } on RangeError catch (ex) { 73 exceptionCaught = true;
132 exceptionCaught = true; 74 } catch (ex) {
133 } on Exception catch (ex) { 75 wrongExceptionCaught = true;
134 wrongExceptionCaught = true; 76 }
135 } 77 Expect.isFalse(exceptionCaught);
136 Expect.equals(true, exceptionCaught); 78 Expect.isFalse(wrongExceptionCaught);
137 Expect.equals(true, !wrongExceptionCaught); 79 try {
138 exceptionCaught = false; 80 client.destroy();
139 81 } on SocketIOException catch(ex) {
140 try { 82 exceptionCaught = true;
141 List<int> buffer = new List<int>.fixedLength(10); 83 } catch (ex) {
142 client.readList(buffer, 0, -1); 84 print(ex);
143 } on RangeError catch (ex) { 85 wrongExceptionCaught = true;
144 exceptionCaught = true; 86 }
145 } on Exception catch (ex) { 87 Expect.isFalse(exceptionCaught);
146 wrongExceptionCaught = true; 88 Expect.isFalse(wrongExceptionCaught);
147 } 89 try {
148 Expect.equals(true, exceptionCaught); 90 List<int> buffer = new List<int>.fixedLength(10);
149 Expect.equals(true, !wrongExceptionCaught); 91 client.add(buffer);
150 exceptionCaught = false; 92 } on StateError catch (ex) {
151 93 exceptionCaught = true;
152 try { 94 } catch (ex) {
153 List<int> buffer = new List<int>.fixedLength(10); 95 wrongExceptionCaught = true;
154 client.writeList(buffer, -1, 1); 96 }
155 } on RangeError catch (ex) { 97 Expect.isTrue(exceptionCaught);
156 exceptionCaught = true; 98 Expect.isFalse(wrongExceptionCaught);
157 } on Exception catch (ex) { 99
158 wrongExceptionCaught = true; 100 server.close();
159 } 101 });
160 Expect.equals(true, exceptionCaught); 102 });
161 Expect.equals(true, !wrongExceptionCaught); 103 }
162 exceptionCaught = false; 104
163 105 static void clientSocketDestroyNoErrorTest() {
164 try { 106 ServerSocket.bind().then((server) {
165 List<int> buffer = new List<int>.fixedLength(10); 107 server.listen((socket) {
166 client.writeList(buffer, 0, -1); 108 socket.pipe(socket);
167 } on RangeError catch (ex) { 109 });
168 exceptionCaught = true; 110 Socket.connect("127.0.0.1", server.port).then((client) {
169 } on Exception catch (ex) { 111 client.listen((data) {}, onDone: server.close);
170 wrongExceptionCaught = true; 112 client.destroy();
171 } 113 });
172 Expect.equals(true, exceptionCaught); 114 });
173 Expect.equals(true, !wrongExceptionCaught); 115 }
174 116
175 server.close(); 117 static void clientSocketAddDestroyNoErrorTest() {
176 client.close(); 118 ServerSocket.bind().then((server) {
177 }; 119 server.listen((socket) {
120 // Passive block data by not sobscribing to socket.
121 });
122 Socket.connect("127.0.0.1", server.port).then((client) {
123 client.listen((data) {}, onDone: server.close);
124 client.add(new List.fixedLength(1024 * 1024, fill: 0));
125 client.destroy();
126 });
127 });
128 }
129
130 static void clientSocketAddCloseNoErrorTest() {
131 ServerSocket.bind().then((server) {
132 var completer = new Completer();
133 server.listen((socket) {
134 // The socket is 'paused' until the future completes.
135 completer.future.then((_) => socket.pipe(socket));
136 });
137 Socket.connect("127.0.0.1", server.port).then((client) {
138 const int SIZE = 1024 * 1024;
139 int count = 0;
140 client.listen(
141 (data) => count += data.length,
142 onDone: () {
143 Expect.equals(SIZE, count);
144 server.close();
145 });
146 client.add(new List.fixedLength(SIZE, fill: 0));
147 client.close();
148 // Start piping now.
149 completer.complete(null);
150 });
151 });
152 }
153
154 static void clientSocketAddCloseErrorTest() {
155 ServerSocket.bind().then((server) {
156 var completer = new Completer();
157 server.listen((socket) {
158 completer.future.then((_) => socket.destroy());
159 });
160 Socket.connect("127.0.0.1", server.port).then((client) {
161 const int SIZE = 1024 * 1024;
162 int errors = 0;
163 client.listen(
164 (data) => Expect.fail("Unexpected data"),
165 onError: (error) {
166 Expect.isTrue(error.error is SocketIOException);
167 errors++;
168 },
169 onDone: () {
170 // We get either a close or an error followed by a close
171 // on the socket. Whether we get both depends on
172 // whether the system notices the error for the read
173 // event or only for the write event.
174 Expect.isTrue(errors <= 1);
175 server.close();
176 });
177 client.add(new List.fixedLength(SIZE, fill: 0));
178 // Destroy other socket now.
179 completer.complete(null);
180 var port = new ReceivePort();
181 client.done.then(
182 (_) {
183 Expect.fail("Expected error");
184 },
185 onError: (error) {
186 Expect.isTrue(error.error is SocketIOException);
187 port.close();
188 });
189 });
190 });
191 }
192
193 static void clientSocketAddCloseResultErrorTest() {
194 ServerSocket.bind().then((server) {
195 var completer = new Completer();
196 server.listen((socket) {
197 completer.future.then((_) => socket.destroy());
198 });
199 Socket.connect("127.0.0.1", server.port).then((client) {
200 const int SIZE = 1024 * 1024;
201 int errors = 0;
202 client.add(new List.fixedLength(SIZE, fill: 0));
203 client.close();
204 client.done.catchError((error) {
205 server.close();
206 });
207 // Destroy other socket now.
208 completer.complete(null);
209 });
210 });
178 } 211 }
179 212
180 static void unknownHostTest() { 213 static void unknownHostTest() {
181 // Port to verify that the test completes. 214 // Port to verify that the test completes.
182 var port = new ReceivePort(); 215 var port = new ReceivePort();
183 port.receive((message, replyTo) => null); 216 port.receive((message, replyTo) => null);
184 217
185 Socket s = new Socket("hede.hule.hest", 1234); 218 Socket.connect("hede.hule.hest", 1234)
186 s.onError = (e) => port.close(); 219 .then((socket) => Expect.fail("Connection completed"))
187 s.onConnect = () => Expect.fail("Connection completed"); 220 .catchError((e) => port.close(), test: (e) => e is SocketIOException);
221
188 } 222 }
189 223
190 static void testMain() { 224 static void testMain() {
191 serverSocketExceptionTest(); 225 serverSocketExceptionTest();
226 serverSocketCloseListenTest();
227 serverSocketListenCloseTest();
192 clientSocketExceptionTest(); 228 clientSocketExceptionTest();
193 indexOutOfRangeExceptionTest(); 229 clientSocketDestroyNoErrorTest();
230 clientSocketAddDestroyNoErrorTest();
231 clientSocketAddCloseNoErrorTest();
232 clientSocketAddCloseErrorTest();
233 clientSocketAddCloseResultErrorTest();
194 unknownHostTest(); 234 unknownHostTest();
195 } 235 }
196 } 236 }
197 237
198 main() { 238 main() {
199 SocketExceptionTest.testMain(); 239 SocketExceptionTest.testMain();
200 } 240 }
201 241
OLDNEW

Powered by Google App Engine