| 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"; | |
| 11 import "dart:async"; | 10 import "dart:async"; |
| 12 import "dart:io"; | 11 import "dart:io"; |
| 13 import "dart:isolate"; | 12 |
| 13 import "package:async_helper/async_helper.dart"; |
| 14 import "package:expect/expect.dart"; |
| 14 | 15 |
| 15 void testArguments() { | 16 void testArguments() { |
| 16 Expect.throws(() => ServerSocket.bind("127.0.0.1", 65536)); | 17 Expect.throws(() => ServerSocket.bind("127.0.0.1", 65536)); |
| 17 Expect.throws(() => ServerSocket.bind("127.0.0.1", -1)); | 18 Expect.throws(() => ServerSocket.bind("127.0.0.1", -1)); |
| 18 Expect.throws(() => ServerSocket.bind("127.0.0.1", 0, backlog: -1)); | 19 Expect.throws(() => ServerSocket.bind("127.0.0.1", 0, backlog: -1)); |
| 19 } | 20 } |
| 20 | 21 |
| 21 void testSimpleBind() { | 22 void testSimpleBind() { |
| 22 ReceivePort port = new ReceivePort(); | 23 asyncStart(); |
| 23 ServerSocket.bind(InternetAddress.LOOPBACK_IP_V4, 0).then((s) { | 24 ServerSocket.bind(InternetAddress.LOOPBACK_IP_V4, 0).then((s) { |
| 24 Expect.isTrue(s.port > 0); | 25 Expect.isTrue(s.port > 0); |
| 25 port.close(); | 26 asyncEnd(); |
| 26 }); | 27 }); |
| 27 } | 28 } |
| 28 | 29 |
| 29 void testInvalidBind() { | 30 void testInvalidBind() { |
| 30 int count = 0; | |
| 31 ReceivePort port = new ReceivePort(); | |
| 32 port.receive((_, __) { count++; if (count == 3) port.close(); }); | |
| 33 | |
| 34 // Bind to a unknown DNS name. | 31 // Bind to a unknown DNS name. |
| 32 asyncStart(); |
| 35 ServerSocket.bind("ko.faar.__hest__", 0) | 33 ServerSocket.bind("ko.faar.__hest__", 0) |
| 36 .then((_) { Expect.fail("Failure expected"); } ) | 34 .then((_) { Expect.fail("Failure expected"); } ) |
| 37 .catchError((error) { | 35 .catchError((error) { |
| 38 Expect.isTrue(error is SocketException); | 36 Expect.isTrue(error is SocketException); |
| 39 port.toSendPort().send(1); | 37 asyncEnd(); |
| 40 }); | 38 }); |
| 41 | 39 |
| 42 // Bind to an unavaliable IP-address. | 40 // Bind to an unavaliable IP-address. |
| 41 asyncStart(); |
| 43 ServerSocket.bind("8.8.8.8", 0) | 42 ServerSocket.bind("8.8.8.8", 0) |
| 44 .then((_) { Expect.fail("Failure expected"); } ) | 43 .then((_) { Expect.fail("Failure expected"); } ) |
| 45 .catchError((error) { | 44 .catchError((error) { |
| 46 Expect.isTrue(error is SocketException); | 45 Expect.isTrue(error is SocketException); |
| 47 port.toSendPort().send(1); | 46 asyncEnd(); |
| 48 }); | 47 }); |
| 49 | 48 |
| 50 // Bind to a port already in use. | 49 // Bind to a port already in use. |
| 50 asyncStart(); |
| 51 ServerSocket.bind("127.0.0.1", 0) | 51 ServerSocket.bind("127.0.0.1", 0) |
| 52 .then((s) { | 52 .then((s) { |
| 53 ServerSocket.bind("127.0.0.1", s.port) | 53 ServerSocket.bind("127.0.0.1", s.port) |
| 54 .then((t) { | 54 .then((t) { |
| 55 Expect.fail("Multiple listens on same port"); | 55 Expect.fail("Multiple listens on same port"); |
| 56 port.toSendPort().send(1); | 56 port.toSendPort().send(1); |
| 57 }) | 57 }) |
| 58 .catchError((error) { | 58 .catchError((error) { |
| 59 Expect.isTrue(error is SocketException); | 59 Expect.isTrue(error is SocketException); |
| 60 s.close(); | 60 s.close(); |
| 61 port.toSendPort().send(1); | 61 asyncEnd(); |
| 62 }); | 62 }); |
| 63 }); | 63 }); |
| 64 } | 64 } |
| 65 | 65 |
| 66 void testConnectNoDestroy() { | 66 void testConnectNoDestroy() { |
| 67 ReceivePort port = new ReceivePort(); | 67 asyncStart(); |
| 68 ServerSocket.bind(InternetAddress.LOOPBACK_IP_V4, 0).then((server) { | 68 ServerSocket.bind(InternetAddress.LOOPBACK_IP_V4, 0).then((server) { |
| 69 server.listen((_) { }); | 69 server.listen((_) { }); |
| 70 Socket.connect("127.0.0.1", server.port).then((_) { | 70 Socket.connect("127.0.0.1", server.port).then((_) { |
| 71 server.close(); | 71 server.close(); |
| 72 port.close(); | 72 asyncEnd(); |
| 73 }); | 73 }); |
| 74 }); | 74 }); |
| 75 } | 75 } |
| 76 | 76 |
| 77 void testConnectImmediateDestroy() { | 77 void testConnectImmediateDestroy() { |
| 78 ReceivePort port = new ReceivePort(); | 78 asyncStart(); |
| 79 ServerSocket.bind(InternetAddress.LOOPBACK_IP_V4, 0).then((server) { | 79 ServerSocket.bind(InternetAddress.LOOPBACK_IP_V4, 0).then((server) { |
| 80 server.listen((_) { }); | 80 server.listen((_) { }); |
| 81 Socket.connect("127.0.0.1", server.port).then((socket) { | 81 Socket.connect("127.0.0.1", server.port).then((socket) { |
| 82 socket.destroy(); | 82 socket.destroy(); |
| 83 server.close(); | 83 server.close(); |
| 84 port.close(); | 84 asyncEnd(); |
| 85 }); | 85 }); |
| 86 }); | 86 }); |
| 87 } | 87 } |
| 88 | 88 |
| 89 void testConnectConsumerClose() { | 89 void testConnectConsumerClose() { |
| 90 // Connect socket then immediate close the consumer without | 90 // Connect socket then immediate close the consumer without |
| 91 // listening on the stream. | 91 // listening on the stream. |
| 92 ReceivePort port = new ReceivePort(); | 92 asyncStart(); |
| 93 ServerSocket.bind(InternetAddress.LOOPBACK_IP_V4, 0).then((server) { | 93 ServerSocket.bind(InternetAddress.LOOPBACK_IP_V4, 0).then((server) { |
| 94 server.listen((_) { }); | 94 server.listen((_) { }); |
| 95 Socket.connect("127.0.0.1", server.port).then((socket) { | 95 Socket.connect("127.0.0.1", server.port).then((socket) { |
| 96 socket.close(); | 96 socket.close(); |
| 97 socket.done.then((_) { | 97 socket.done.then((_) { |
| 98 socket.destroy(); | 98 socket.destroy(); |
| 99 server.close(); | 99 server.close(); |
| 100 port.close(); | 100 asyncEnd(); |
| 101 }); | 101 }); |
| 102 }); | 102 }); |
| 103 }); | 103 }); |
| 104 } | 104 } |
| 105 | 105 |
| 106 void testConnectConsumerWriteClose() { | 106 void testConnectConsumerWriteClose() { |
| 107 // Connect socket write some data immediate close the consumer | 107 // Connect socket write some data immediate close the consumer |
| 108 // without listening on the stream. | 108 // without listening on the stream. |
| 109 ReceivePort port = new ReceivePort(); | 109 asyncStart(); |
| 110 ServerSocket.bind(InternetAddress.LOOPBACK_IP_V4, 0).then((server) { | 110 ServerSocket.bind(InternetAddress.LOOPBACK_IP_V4, 0).then((server) { |
| 111 server.listen((_) { }); | 111 server.listen((_) { }); |
| 112 Socket.connect("127.0.0.1", server.port).then((socket) { | 112 Socket.connect("127.0.0.1", server.port).then((socket) { |
| 113 socket.add([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]); | 113 socket.add([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]); |
| 114 socket.close(); | 114 socket.close(); |
| 115 socket.done.then((_) { | 115 socket.done.then((_) { |
| 116 socket.destroy(); | 116 socket.destroy(); |
| 117 server.close(); | 117 server.close(); |
| 118 port.close(); | 118 asyncEnd(); |
| 119 }); | 119 }); |
| 120 }); | 120 }); |
| 121 }); | 121 }); |
| 122 } | 122 } |
| 123 | 123 |
| 124 void testConnectStreamClose() { | 124 void testConnectStreamClose() { |
| 125 // Connect socket and listen on the stream. The server closes | 125 // Connect socket and listen on the stream. The server closes |
| 126 // immediately so only a done event is received. | 126 // immediately so only a done event is received. |
| 127 ReceivePort port = new ReceivePort(); | 127 asyncStart(); |
| 128 ServerSocket.bind(InternetAddress.LOOPBACK_IP_V4, 0).then((server) { | 128 ServerSocket.bind(InternetAddress.LOOPBACK_IP_V4, 0).then((server) { |
| 129 server.listen((client) { | 129 server.listen((client) { |
| 130 client.close(); | 130 client.close(); |
| 131 client.done.then((_) => client.destroy()); | 131 client.done.then((_) => client.destroy()); |
| 132 }); | 132 }); |
| 133 Socket.connect("127.0.0.1", server.port).then((socket) { | 133 Socket.connect("127.0.0.1", server.port).then((socket) { |
| 134 bool onDoneCalled = false; | 134 bool onDoneCalled = false; |
| 135 socket.listen((_) { Expect.fail("Unexpected data"); }, | 135 socket.listen((_) { Expect.fail("Unexpected data"); }, |
| 136 onDone: () { | 136 onDone: () { |
| 137 Expect.isFalse(onDoneCalled); | 137 Expect.isFalse(onDoneCalled); |
| 138 onDoneCalled = true; | 138 onDoneCalled = true; |
| 139 socket.close(); | 139 socket.close(); |
| 140 server.close(); | 140 server.close(); |
| 141 port.close(); | 141 asyncEnd(); |
| 142 }); | 142 }); |
| 143 }); | 143 }); |
| 144 }); | 144 }); |
| 145 } | 145 } |
| 146 | 146 |
| 147 void testConnectStreamDataClose(bool useDestroy) { | 147 void testConnectStreamDataClose(bool useDestroy) { |
| 148 // Connect socket and listen on the stream. The server sends data | 148 // Connect socket and listen on the stream. The server sends data |
| 149 // and then closes so both data and a done event is received. | 149 // and then closes so both data and a done event is received. |
| 150 List<int> sendData = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]; | 150 List<int> sendData = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]; |
| 151 ReceivePort port = new ReceivePort(); | 151 asyncStart(); |
| 152 ServerSocket.bind(InternetAddress.LOOPBACK_IP_V4, 0).then((server) { | 152 ServerSocket.bind(InternetAddress.LOOPBACK_IP_V4, 0).then((server) { |
| 153 server.listen( | 153 server.listen( |
| 154 (client) { | 154 (client) { |
| 155 client.add(sendData); | 155 client.add(sendData); |
| 156 if (useDestroy) { | 156 if (useDestroy) { |
| 157 client.destroy(); | 157 client.destroy(); |
| 158 } else { | 158 } else { |
| 159 client.close(); | 159 client.close(); |
| 160 } | 160 } |
| 161 client.done.then((_) { if (!useDestroy) { client.destroy(); } }); | 161 client.done.then((_) { if (!useDestroy) { client.destroy(); } }); |
| 162 }); | 162 }); |
| 163 Socket.connect("127.0.0.1", server.port).then((socket) { | 163 Socket.connect("127.0.0.1", server.port).then((socket) { |
| 164 List<int> data = []; | 164 List<int> data = []; |
| 165 bool onDoneCalled = false; | 165 bool onDoneCalled = false; |
| 166 socket.listen(data.addAll, | 166 socket.listen(data.addAll, |
| 167 onDone: () { | 167 onDone: () { |
| 168 Expect.isFalse(onDoneCalled); | 168 Expect.isFalse(onDoneCalled); |
| 169 onDoneCalled = true; | 169 onDoneCalled = true; |
| 170 if (!useDestroy) Expect.listEquals(sendData, data); | 170 if (!useDestroy) Expect.listEquals(sendData, data); |
| 171 socket.add([0]); | 171 socket.add([0]); |
| 172 socket.close(); | 172 socket.close(); |
| 173 server.close(); | 173 server.close(); |
| 174 port.close(); | 174 asyncEnd(); |
| 175 }); | 175 }); |
| 176 }); | 176 }); |
| 177 }); | 177 }); |
| 178 } | 178 } |
| 179 | 179 |
| 180 void testConnectStreamDataCloseCancel(bool useDestroy) { | 180 void testConnectStreamDataCloseCancel(bool useDestroy) { |
| 181 List<int> sendData = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]; | 181 List<int> sendData = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]; |
| 182 ReceivePort port = new ReceivePort(); | 182 asyncStart(); |
| 183 ServerSocket.bind(InternetAddress.LOOPBACK_IP_V4, 0).then((server) { | 183 ServerSocket.bind(InternetAddress.LOOPBACK_IP_V4, 0).then((server) { |
| 184 server.listen( | 184 server.listen( |
| 185 (client) { | 185 (client) { |
| 186 client.add(sendData); | 186 client.add(sendData); |
| 187 if (useDestroy) { | 187 if (useDestroy) { |
| 188 client.destroy(); | 188 client.destroy(); |
| 189 } else { | 189 } else { |
| 190 client.close(); | 190 client.close(); |
| 191 } | 191 } |
| 192 client.done | 192 client.done |
| 193 .then((_) { | 193 .then((_) { |
| 194 if (!useDestroy) client.destroy(); | 194 if (!useDestroy) client.destroy(); |
| 195 }) | 195 }) |
| 196 .catchError((e) { /* can happen with short writes */ }); | 196 .catchError((e) { /* can happen with short writes */ }); |
| 197 }); | 197 }); |
| 198 Socket.connect("127.0.0.1", server.port).then((socket) { | 198 Socket.connect("127.0.0.1", server.port).then((socket) { |
| 199 List<int> data = []; | 199 List<int> data = []; |
| 200 bool onDoneCalled = false; | 200 bool onDoneCalled = false; |
| 201 var subscription; | 201 var subscription; |
| 202 subscription = socket.listen( | 202 subscription = socket.listen( |
| 203 (_) { | 203 (_) { |
| 204 subscription.cancel(); | 204 subscription.cancel(); |
| 205 socket.close(); | 205 socket.close(); |
| 206 server.close(); | 206 server.close(); |
| 207 port.close(); | 207 asyncEnd(); |
| 208 }, | 208 }, |
| 209 onDone: () { Expect.fail("Unexpected pipe completion"); }); | 209 onDone: () { Expect.fail("Unexpected pipe completion"); }); |
| 210 }); | 210 }); |
| 211 }); | 211 }); |
| 212 } | 212 } |
| 213 | 213 |
| 214 main() { | 214 main() { |
| 215 asyncStart(); |
| 215 testArguments(); | 216 testArguments(); |
| 216 testSimpleBind(); | 217 testSimpleBind(); |
| 217 testInvalidBind(); | 218 testInvalidBind(); |
| 218 testConnectNoDestroy(); | 219 testConnectNoDestroy(); |
| 219 testConnectImmediateDestroy(); | 220 testConnectImmediateDestroy(); |
| 220 testConnectConsumerClose(); | 221 testConnectConsumerClose(); |
| 221 testConnectConsumerWriteClose(); | 222 testConnectConsumerWriteClose(); |
| 222 testConnectStreamClose(); | 223 testConnectStreamClose(); |
| 223 testConnectStreamDataClose(true); | 224 testConnectStreamDataClose(true); |
| 224 testConnectStreamDataClose(false); | 225 testConnectStreamDataClose(false); |
| 225 testConnectStreamDataCloseCancel(true); | 226 testConnectStreamDataCloseCancel(true); |
| 226 testConnectStreamDataCloseCancel(false); | 227 testConnectStreamDataCloseCancel(false); |
| 228 asyncEnd(); |
| 227 } | 229 } |
| OLD | NEW |