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(() => ServerSocket.bind("127.0.0.1", 65536)); | 16 Expect.throws(() => ServerSocket.bind("127.0.0.1", 65536)); |
17 Expect.throws(() => ServerSocket.bind("127.0.0.1", -1)); | 17 Expect.throws(() => ServerSocket.bind("127.0.0.1", -1)); |
18 Expect.throws(() => ServerSocket.bind("127.0.0.1", 0, -1)); | 18 Expect.throws(() => ServerSocket.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 ServerSocket.bind().then((s) { | 23 ServerSocket.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 ServerSocket.bind("ko.faar.__hest__") | 35 ServerSocket.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 ServerSocket.bind("8.8.8.8") | 43 ServerSocket.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 ServerSocket.bind("127.0.0.1") | 54 ServerSocket.bind("127.0.0.1", 0) |
55 .then((s) { | 55 .then((s) { |
56 ServerSocket.bind("127.0.0.1", s.port) | 56 ServerSocket.bind("127.0.0.1", 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 testConnectNoDestroy() { | 70 void testConnectNoDestroy() { |
71 ReceivePort port = new ReceivePort(); | 71 ReceivePort port = new ReceivePort(); |
72 ServerSocket.bind().then((server) { | 72 ServerSocket.bind(InternetAddress.LOOPBACK_IP_V4, 0).then((server) { |
73 server.listen((_) { }); | 73 server.listen((_) { }); |
74 Socket.connect("127.0.0.1", server.port).then((_) { | 74 Socket.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 testConnectImmediateDestroy() { | 81 void testConnectImmediateDestroy() { |
82 ReceivePort port = new ReceivePort(); | 82 ReceivePort port = new ReceivePort(); |
83 ServerSocket.bind().then((server) { | 83 ServerSocket.bind(InternetAddress.LOOPBACK_IP_V4, 0).then((server) { |
84 server.listen((_) { }); | 84 server.listen((_) { }); |
85 Socket.connect("127.0.0.1", server.port).then((socket) { | 85 Socket.connect("127.0.0.1", server.port).then((socket) { |
86 socket.destroy(); | 86 socket.destroy(); |
87 server.close(); | 87 server.close(); |
88 port.close(); | 88 port.close(); |
89 }); | 89 }); |
90 }); | 90 }); |
91 } | 91 } |
92 | 92 |
93 void testConnectConsumerClose() { | 93 void testConnectConsumerClose() { |
94 // Connect socket then immediate close the consumer without | 94 // Connect socket then immediate close the consumer without |
95 // listening on the stream. | 95 // listening on the stream. |
96 ReceivePort port = new ReceivePort(); | 96 ReceivePort port = new ReceivePort(); |
97 ServerSocket.bind().then((server) { | 97 ServerSocket.bind(InternetAddress.LOOPBACK_IP_V4, 0).then((server) { |
98 server.listen((_) { }); | 98 server.listen((_) { }); |
99 Socket.connect("127.0.0.1", server.port).then((socket) { | 99 Socket.connect("127.0.0.1", server.port).then((socket) { |
100 socket.close(); | 100 socket.close(); |
101 socket.done.then((_) { | 101 socket.done.then((_) { |
102 socket.destroy(); | 102 socket.destroy(); |
103 server.close(); | 103 server.close(); |
104 port.close(); | 104 port.close(); |
105 }); | 105 }); |
106 }); | 106 }); |
107 }); | 107 }); |
108 } | 108 } |
109 | 109 |
110 void testConnectConsumerWriteClose() { | 110 void testConnectConsumerWriteClose() { |
111 // Connect socket write some data immediate close the consumer | 111 // Connect socket write some data immediate close the consumer |
112 // without listening on the stream. | 112 // without listening on the stream. |
113 ReceivePort port = new ReceivePort(); | 113 ReceivePort port = new ReceivePort(); |
114 ServerSocket.bind().then((server) { | 114 ServerSocket.bind(InternetAddress.LOOPBACK_IP_V4, 0).then((server) { |
115 server.listen((_) { }); | 115 server.listen((_) { }); |
116 Socket.connect("127.0.0.1", server.port).then((socket) { | 116 Socket.connect("127.0.0.1", server.port).then((socket) { |
117 socket.add([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]); | 117 socket.add([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]); |
118 socket.close(); | 118 socket.close(); |
119 socket.done.then((_) { | 119 socket.done.then((_) { |
120 socket.destroy(); | 120 socket.destroy(); |
121 server.close(); | 121 server.close(); |
122 port.close(); | 122 port.close(); |
123 }); | 123 }); |
124 }); | 124 }); |
125 }); | 125 }); |
126 } | 126 } |
127 | 127 |
128 void testConnectStreamClose() { | 128 void testConnectStreamClose() { |
129 // Connect socket and listen on the stream. The server closes | 129 // Connect socket and listen on the stream. The server closes |
130 // immediately so only a done event is received. | 130 // immediately so only a done event is received. |
131 ReceivePort port = new ReceivePort(); | 131 ReceivePort port = new ReceivePort(); |
132 ServerSocket.bind().then((server) { | 132 ServerSocket.bind(InternetAddress.LOOPBACK_IP_V4, 0).then((server) { |
133 server.listen((client) { | 133 server.listen((client) { |
134 client.close(); | 134 client.close(); |
135 client.done.then((_) => client.destroy()); | 135 client.done.then((_) => client.destroy()); |
136 }); | 136 }); |
137 Socket.connect("127.0.0.1", server.port).then((socket) { | 137 Socket.connect("127.0.0.1", server.port).then((socket) { |
138 bool onDoneCalled = false; | 138 bool onDoneCalled = false; |
139 socket.listen((_) { Expect.fail("Unexpected data"); }, | 139 socket.listen((_) { Expect.fail("Unexpected data"); }, |
140 onDone: () { | 140 onDone: () { |
141 Expect.isFalse(onDoneCalled); | 141 Expect.isFalse(onDoneCalled); |
142 onDoneCalled = true; | 142 onDoneCalled = true; |
143 socket.close(); | 143 socket.close(); |
144 server.close(); | 144 server.close(); |
145 port.close(); | 145 port.close(); |
146 }); | 146 }); |
147 }); | 147 }); |
148 }); | 148 }); |
149 } | 149 } |
150 | 150 |
151 void testConnectStreamDataClose(bool useDestroy) { | 151 void testConnectStreamDataClose(bool useDestroy) { |
152 // Connect socket and listen on the stream. The server sends data | 152 // Connect socket and listen on the stream. The server sends data |
153 // and then closes so both data and a done event is received. | 153 // and then closes so both data and a done event is received. |
154 List<int> sendData = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]; | 154 List<int> sendData = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]; |
155 ReceivePort port = new ReceivePort(); | 155 ReceivePort port = new ReceivePort(); |
156 ServerSocket.bind().then((server) { | 156 ServerSocket.bind(InternetAddress.LOOPBACK_IP_V4, 0).then((server) { |
157 server.listen( | 157 server.listen( |
158 (client) { | 158 (client) { |
159 client.add(sendData); | 159 client.add(sendData); |
160 if (useDestroy) { | 160 if (useDestroy) { |
161 client.destroy(); | 161 client.destroy(); |
162 } else { | 162 } else { |
163 client.close(); | 163 client.close(); |
164 } | 164 } |
165 client.done.then((_) { if (!useDestroy) { client.destroy(); } }); | 165 client.done.then((_) { if (!useDestroy) { client.destroy(); } }); |
166 }); | 166 }); |
(...skipping 10 matching lines...) Expand all Loading... |
177 server.close(); | 177 server.close(); |
178 port.close(); | 178 port.close(); |
179 }); | 179 }); |
180 }); | 180 }); |
181 }); | 181 }); |
182 } | 182 } |
183 | 183 |
184 void testConnectStreamDataCloseCancel(bool useDestroy) { | 184 void testConnectStreamDataCloseCancel(bool useDestroy) { |
185 List<int> sendData = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]; | 185 List<int> sendData = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]; |
186 ReceivePort port = new ReceivePort(); | 186 ReceivePort port = new ReceivePort(); |
187 ServerSocket.bind().then((server) { | 187 ServerSocket.bind(InternetAddress.LOOPBACK_IP_V4, 0).then((server) { |
188 server.listen( | 188 server.listen( |
189 (client) { | 189 (client) { |
190 client.add(sendData); | 190 client.add(sendData); |
191 if (useDestroy) { | 191 if (useDestroy) { |
192 client.destroy(); | 192 client.destroy(); |
193 } else { | 193 } else { |
194 client.close(); | 194 client.close(); |
195 } | 195 } |
196 client.done | 196 client.done |
197 .then((_) { | 197 .then((_) { |
(...skipping 24 matching lines...) Expand all Loading... |
222 testConnectNoDestroy(); | 222 testConnectNoDestroy(); |
223 testConnectImmediateDestroy(); | 223 testConnectImmediateDestroy(); |
224 testConnectConsumerClose(); | 224 testConnectConsumerClose(); |
225 testConnectConsumerWriteClose(); | 225 testConnectConsumerWriteClose(); |
226 testConnectStreamClose(); | 226 testConnectStreamClose(); |
227 testConnectStreamDataClose(true); | 227 testConnectStreamDataClose(true); |
228 testConnectStreamDataClose(false); | 228 testConnectStreamDataClose(false); |
229 testConnectStreamDataCloseCancel(true); | 229 testConnectStreamDataCloseCancel(true); |
230 testConnectStreamDataCloseCancel(false); | 230 testConnectStreamDataCloseCancel(false); |
231 } | 231 } |
OLD | NEW |