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 // VMOptions= | 5 // VMOptions= |
6 // VMOptions=--short_socket_read | 6 // VMOptions=--short_socket_read |
7 // VMOptions=--short_socket_write | 7 // VMOptions=--short_socket_write |
8 // VMOptions=--short_socket_read --short_socket_write | 8 // VMOptions=--short_socket_read --short_socket_write |
9 | 9 |
10 import "package:expect/expect.dart"; | 10 import "package:expect/expect.dart"; |
11 import "dart:async"; | 11 import "dart:async"; |
12 import "dart:io"; | 12 import "dart:io"; |
13 import "dart:isolate"; | 13 import "dart:isolate"; |
14 | 14 |
15 void testArguments() { | 15 void testArguments() { |
16 Expect.throws(() => RawServerSocket.bind("127.0.0.1", 65536)); | 16 Expect.throws(() => RawServerSocket.bind("127.0.0.1", 65536)); |
17 Expect.throws(() => RawServerSocket.bind("127.0.0.1", -1)); | 17 Expect.throws(() => RawServerSocket.bind("127.0.0.1", -1)); |
18 Expect.throws(() => RawServerSocket.bind("127.0.0.1", 0, -1)); | 18 Expect.throws(() => RawServerSocket.bind("127.0.0.1", 0, backlog: -1)); |
19 } | 19 } |
20 | 20 |
21 void testSimpleBind() { | 21 void testSimpleBind() { |
22 ReceivePort port = new ReceivePort(); | 22 ReceivePort port = new ReceivePort(); |
23 RawServerSocket.bind().then((s) { | 23 RawServerSocket.bind(InternetAddress.LOOPBACK_IP_V4, 0).then((s) { |
24 Expect.isTrue(s.port > 0); | 24 Expect.isTrue(s.port > 0); |
25 port.close(); | 25 port.close(); |
26 }); | 26 }); |
27 } | 27 } |
28 | 28 |
29 void testInvalidBind() { | 29 void testInvalidBind() { |
30 int count = 0; | 30 int count = 0; |
31 ReceivePort port = new ReceivePort(); | 31 ReceivePort port = new ReceivePort(); |
32 port.receive((_, __) { count++; if (count == 3) port.close(); }); | 32 port.receive((_, __) { count++; if (count == 3) port.close(); }); |
33 | 33 |
34 // Bind to a unknown DNS name. | 34 // Bind to a unknown DNS name. |
35 RawServerSocket.bind("ko.faar.__hest__") | 35 RawServerSocket.bind("ko.faar.__hest__", 0) |
36 .then((_) { Expect.fail("Failure expected"); } ) | 36 .then((_) { Expect.fail("Failure expected"); } ) |
37 .catchError((error) { | 37 .catchError((error) { |
38 Expect.isTrue(error is SocketIOException); | 38 Expect.isTrue(error is SocketIOException); |
39 port.toSendPort().send(1); | 39 port.toSendPort().send(1); |
40 }); | 40 }); |
41 | 41 |
42 // Bind to an unavaliable IP-address. | 42 // Bind to an unavaliable IP-address. |
43 RawServerSocket.bind("8.8.8.8") | 43 RawServerSocket.bind("8.8.8.8", 0) |
44 .then((_) { Expect.fail("Failure expected"); } ) | 44 .then((_) { Expect.fail("Failure expected"); } ) |
45 .catchError((error) { | 45 .catchError((error) { |
46 Expect.isTrue(error is SocketIOException); | 46 Expect.isTrue(error is SocketIOException); |
47 port.toSendPort().send(1); | 47 port.toSendPort().send(1); |
48 }); | 48 }); |
49 | 49 |
50 // Bind to a port already in use. | 50 // Bind to a port already in use. |
51 // Either an error or a successful bind is allowed. | 51 // Either an error or a successful bind is allowed. |
52 // Windows platforms allow multiple binding to the same socket, with | 52 // Windows platforms allow multiple binding to the same socket, with |
53 // unpredictable results. | 53 // unpredictable results. |
54 RawServerSocket.bind("127.0.0.1") | 54 RawServerSocket.bind(InternetAddress.LOOPBACK_IP_V4, 0) |
55 .then((s) { | 55 .then((s) { |
56 RawServerSocket.bind("127.0.0.1", s.port) | 56 RawServerSocket.bind(InternetAddress.LOOPBACK_IP_V4, s.port) |
57 .then((t) { | 57 .then((t) { |
58 Expect.equals('windows', Platform.operatingSystem); | 58 Expect.equals('windows', Platform.operatingSystem); |
59 Expect.equals(s.port, t.port); | 59 Expect.equals(s.port, t.port); |
60 port.toSendPort().send(1); | 60 port.toSendPort().send(1); |
61 }) | 61 }) |
62 .catchError((error) { | 62 .catchError((error) { |
63 Expect.notEquals('windows', Platform.operatingSystem); | 63 Expect.notEquals('windows', Platform.operatingSystem); |
64 Expect.isTrue(error is SocketIOException); | 64 Expect.isTrue(error is SocketIOException); |
65 port.toSendPort().send(1); | 65 port.toSendPort().send(1); |
66 }); | 66 }); |
67 }); | 67 }); |
68 } | 68 } |
69 | 69 |
70 void testSimpleConnect() { | 70 void testSimpleConnect() { |
71 ReceivePort port = new ReceivePort(); | 71 ReceivePort port = new ReceivePort(); |
72 RawServerSocket.bind().then((server) { | 72 RawServerSocket.bind(InternetAddress.LOOPBACK_IP_V4, 0).then((server) { |
73 server.listen((_) { }); | 73 server.listen((_) { }); |
74 RawSocket.connect("127.0.0.1", server.port).then((_) { | 74 RawSocket.connect("127.0.0.1", server.port).then((_) { |
75 server.close(); | 75 server.close(); |
76 port.close(); | 76 port.close(); |
77 }); | 77 }); |
78 }); | 78 }); |
79 } | 79 } |
80 | 80 |
81 void testCloseOneEnd(String toClose) { | 81 void testCloseOneEnd(String toClose) { |
82 ReceivePort port = new ReceivePort(); | 82 ReceivePort port = new ReceivePort(); |
83 Completer serverDone = new Completer(); | 83 Completer serverDone = new Completer(); |
84 Completer serverEndDone = new Completer(); | 84 Completer serverEndDone = new Completer(); |
85 Completer clientEndDone = new Completer(); | 85 Completer clientEndDone = new Completer(); |
86 Future.wait([serverDone.future, serverEndDone.future, clientEndDone.future]) | 86 Future.wait([serverDone.future, serverEndDone.future, clientEndDone.future]) |
87 .then((_) { | 87 .then((_) { |
88 port.close(); | 88 port.close(); |
89 }); | 89 }); |
90 RawServerSocket.bind().then((server) { | 90 RawServerSocket.bind(InternetAddress.LOOPBACK_IP_V4, 0).then((server) { |
91 server.listen((serverConnection) { | 91 server.listen((serverConnection) { |
92 serverConnection.listen((event) { | 92 serverConnection.listen((event) { |
93 if (toClose == "server" || event == RawSocketEvent.READ_CLOSED) { | 93 if (toClose == "server" || event == RawSocketEvent.READ_CLOSED) { |
94 serverConnection.shutdown(SocketDirection.SEND); | 94 serverConnection.shutdown(SocketDirection.SEND); |
95 } | 95 } |
96 }, | 96 }, |
97 onDone: () { | 97 onDone: () { |
98 serverEndDone.complete(null); | 98 serverEndDone.complete(null); |
99 }); | 99 }); |
100 }, | 100 }, |
101 onDone:() { | 101 onDone:() { |
102 serverDone.complete(null); | 102 serverDone.complete(null); |
103 }); | 103 }); |
104 RawSocket.connect("127.0.0.1", server.port).then((clientConnection) { | 104 RawSocket.connect("127.0.0.1", server.port).then((clientConnection) { |
105 clientConnection.listen((event){ | 105 clientConnection.listen((event){ |
106 if (toClose == "client" || event == RawSocketEvent.READ_CLOSED) { | 106 if (toClose == "client" || event == RawSocketEvent.READ_CLOSED) { |
107 clientConnection.shutdown(SocketDirection.SEND); | 107 clientConnection.shutdown(SocketDirection.SEND); |
108 } | 108 } |
109 }, | 109 }, |
110 onDone: () { | 110 onDone: () { |
111 clientEndDone.complete(null); | 111 clientEndDone.complete(null); |
112 server.close(); | 112 server.close(); |
113 }); | 113 }); |
114 }); | 114 }); |
115 }); | 115 }); |
116 } | 116 } |
117 | 117 |
118 void testServerListenAfterConnect() { | 118 void testServerListenAfterConnect() { |
119 ReceivePort port = new ReceivePort(); | 119 ReceivePort port = new ReceivePort(); |
120 RawServerSocket.bind().then((server) { | 120 RawServerSocket.bind(InternetAddress.LOOPBACK_IP_V4, 0).then((server) { |
121 Expect.isTrue(server.port > 0); | 121 Expect.isTrue(server.port > 0); |
122 RawSocket.connect("127.0.0.1", server.port).then((_) { | 122 RawSocket.connect("127.0.0.1", server.port).then((_) { |
123 server.listen((_) { | 123 server.listen((_) { |
124 server.close(); | 124 server.close(); |
125 port.close(); | 125 port.close(); |
126 }); | 126 }); |
127 }); | 127 }); |
128 }); | 128 }); |
129 } | 129 } |
130 | 130 |
(...skipping 15 matching lines...) Expand all Loading... |
146 } | 146 } |
147 | 147 |
148 void verifyTestData(List<int> data) { | 148 void verifyTestData(List<int> data) { |
149 Expect.equals(messageSize, data.length); | 149 Expect.equals(messageSize, data.length); |
150 List<int> expected = createTestData(); | 150 List<int> expected = createTestData(); |
151 for (int i = 0; i < messageSize; i++) { | 151 for (int i = 0; i < messageSize; i++) { |
152 Expect.equals(expected[i], data[i]); | 152 Expect.equals(expected[i], data[i]); |
153 } | 153 } |
154 } | 154 } |
155 | 155 |
156 RawServerSocket.bind().then((server) { | 156 RawServerSocket.bind(InternetAddress.LOOPBACK_IP_V4, 0).then((server) { |
157 server.listen((client) { | 157 server.listen((client) { |
158 int bytesRead = 0; | 158 int bytesRead = 0; |
159 int bytesWritten = 0; | 159 int bytesWritten = 0; |
160 List<int> data = new List<int>(messageSize); | 160 List<int> data = new List<int>(messageSize); |
161 | 161 |
162 client.writeEventsEnabled = false; | 162 client.writeEventsEnabled = false; |
163 client.listen((event) { | 163 client.listen((event) { |
164 switch (event) { | 164 switch (event) { |
165 case RawSocketEvent.READ: | 165 case RawSocketEvent.READ: |
166 Expect.isTrue(bytesWritten == 0); | 166 Expect.isTrue(bytesWritten == 0); |
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
228 }); | 228 }); |
229 } | 229 } |
230 | 230 |
231 testPauseServerSocket() { | 231 testPauseServerSocket() { |
232 const int socketCount = 10; | 232 const int socketCount = 10; |
233 var acceptCount = 0; | 233 var acceptCount = 0; |
234 var resumed = false; | 234 var resumed = false; |
235 | 235 |
236 ReceivePort port = new ReceivePort(); | 236 ReceivePort port = new ReceivePort(); |
237 | 237 |
238 RawServerSocket.bind().then((server) { | 238 RawServerSocket.bind(InternetAddress.LOOPBACK_IP_V4, 0).then((server) { |
239 Expect.isTrue(server.port > 0); | 239 Expect.isTrue(server.port > 0); |
240 var subscription = server.listen((_) { | 240 var subscription = server.listen((_) { |
241 Expect.isTrue(resumed); | 241 Expect.isTrue(resumed); |
242 if (++acceptCount == socketCount) { | 242 if (++acceptCount == socketCount) { |
243 server.close(); | 243 server.close(); |
244 port.close(); | 244 port.close(); |
245 } | 245 } |
246 }); | 246 }); |
247 | 247 |
248 // Pause the server socket subscription and resume it after having | 248 // Pause the server socket subscription and resume it after having |
(...skipping 20 matching lines...) Expand all Loading... |
269 const loopCount = 10; | 269 const loopCount = 10; |
270 Completer connected = new Completer(); | 270 Completer connected = new Completer(); |
271 int pauseResumeCount = 0; | 271 int pauseResumeCount = 0; |
272 int bytesWritten = 0; | 272 int bytesWritten = 0; |
273 int bytesRead = 0; | 273 int bytesRead = 0; |
274 var writeSubscription; | 274 var writeSubscription; |
275 var readSubscription; | 275 var readSubscription; |
276 | 276 |
277 ReceivePort port = new ReceivePort(); | 277 ReceivePort port = new ReceivePort(); |
278 | 278 |
279 RawServerSocket.bind().then((server) { | 279 RawServerSocket.bind(InternetAddress.LOOPBACK_IP_V4, 0).then((server) { |
280 Expect.isTrue(server.port > 0); | 280 Expect.isTrue(server.port > 0); |
281 server.listen((client) { | 281 server.listen((client) { |
282 List<int> data = new List<int>.filled(messageSize, 0); | 282 List<int> data = new List<int>.filled(messageSize, 0); |
283 writeSubscription = client.listen((event) { | 283 writeSubscription = client.listen((event) { |
284 switch (event) { | 284 switch (event) { |
285 case RawSocketEvent.READ: | 285 case RawSocketEvent.READ: |
286 throw "Unexpected read event"; | 286 throw "Unexpected read event"; |
287 case RawSocketEvent.WRITE: | 287 case RawSocketEvent.WRITE: |
288 if (pauseResumeCount == loopCount) return; | 288 if (pauseResumeCount == loopCount) return; |
289 Expect.isFalse(client.writeEventsEnabled); | 289 Expect.isFalse(client.writeEventsEnabled); |
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
349 testSimpleBind(); | 349 testSimpleBind(); |
350 testCloseOneEnd("client"); | 350 testCloseOneEnd("client"); |
351 testCloseOneEnd("server"); | 351 testCloseOneEnd("server"); |
352 testInvalidBind(); | 352 testInvalidBind(); |
353 testSimpleConnect(); | 353 testSimpleConnect(); |
354 testServerListenAfterConnect(); | 354 testServerListenAfterConnect(); |
355 testSimpleReadWrite(); | 355 testSimpleReadWrite(); |
356 testPauseServerSocket(); | 356 testPauseServerSocket(); |
357 testPauseSocket(); | 357 testPauseSocket(); |
358 } | 358 } |
OLD | NEW |