| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // BSD-style license that can be found in the LICENSE file. | |
| 4 // | |
| 5 // VMOptions= | |
| 6 // VMOptions=--short_socket_read | |
| 7 // VMOptions=--short_socket_write | |
| 8 // VMOptions=--short_socket_read --short_socket_write | |
| 9 | |
| 10 // TODO(7157): Remove this test once the bug is fixed. | |
| 11 // This is a copy of web_socket_test.dart with the secure connection | |
| 12 // tests disabled, so it does not crash on Windows. | |
| 13 | |
| 14 import "dart:async"; | |
| 15 import "dart:io"; | |
| 16 import "dart:isolate"; | |
| 17 import "dart:scalarlist"; | |
| 18 import "dart:uri"; | |
| 19 | |
| 20 const String CERT_NAME = 'localhost_cert'; | |
| 21 const String SERVER_ADDRESS = '127.0.0.1'; | |
| 22 const String HOST_NAME = 'localhost'; | |
| 23 | |
| 24 /** | |
| 25 * A SecurityConfiguration lets us run the tests over HTTP or HTTPS. | |
| 26 */ | |
| 27 class SecurityConfiguration { | |
| 28 final bool secure; | |
| 29 | |
| 30 SecurityConfiguration({bool this.secure}); | |
| 31 | |
| 32 Future<HttpServer> createServer({int backlog: 0}) => | |
| 33 secure ? HttpServer.bindSecure(SERVER_ADDRESS, | |
| 34 0, | |
| 35 backlog: backlog, | |
| 36 certificateName: CERT_NAME) | |
| 37 : HttpServer.bind(SERVER_ADDRESS, | |
| 38 0, | |
| 39 backlog); | |
| 40 | |
| 41 Future<WebSocket> createClient(int port) => | |
| 42 WebSocket.connect('${secure ? "wss" : "ws"}://$HOST_NAME:$port/'); | |
| 43 | |
| 44 void testRequestResponseClientCloses(int totalConnections, | |
| 45 int closeStatus, | |
| 46 String closeReason) { | |
| 47 createServer().then((server) { | |
| 48 server.transform(new WebSocketTransformer()).listen((webSocket) { | |
| 49 webSocket.listen((event) { | |
| 50 if (event is MessageEvent) { | |
| 51 webSocket.send(event.data); | |
| 52 } else if (event is CloseEvent) { | |
| 53 Expect.equals(closeStatus == null | |
| 54 ? WebSocketStatus.NO_STATUS_RECEIVED | |
| 55 : closeStatus, event.code); | |
| 56 Expect.equals(closeReason == null ? "" : closeReason, event.reason); | |
| 57 } | |
| 58 }); | |
| 59 }); | |
| 60 | |
| 61 int closeCount = 0; | |
| 62 String messageText = "Hello, world!"; | |
| 63 for (int i = 0; i < totalConnections; i++) { | |
| 64 int messageCount = 0; | |
| 65 createClient(server.port).then((webSocket) { | |
| 66 webSocket.send(messageText); | |
| 67 webSocket.listen((event) { | |
| 68 if (event is MessageEvent) { | |
| 69 messageCount++; | |
| 70 if (messageCount < 1 ) { | |
| 71 Expect.equals(messageText, event.data); | |
| 72 webSocket.send(event.data); | |
| 73 } else { | |
| 74 webSocket.close(closeStatus, closeReason); | |
| 75 } | |
| 76 } else if (event is CloseEvent) { | |
| 77 Expect.equals(closeStatus == null | |
| 78 ? WebSocketStatus.NO_STATUS_RECEIVED | |
| 79 : closeStatus, event.code); | |
| 80 Expect.equals("", event.reason); | |
| 81 closeCount++; | |
| 82 if (closeCount == totalConnections) { | |
| 83 server.close(); | |
| 84 } | |
| 85 } | |
| 86 }); | |
| 87 }); | |
| 88 } | |
| 89 }); | |
| 90 } | |
| 91 | |
| 92 void testRequestResponseServerCloses(int totalConnections, | |
| 93 int closeStatus, | |
| 94 String closeReason) { | |
| 95 createServer().then((server) { | |
| 96 int closeCount = 0; | |
| 97 server.transform(new WebSocketTransformer()).listen((webSocket) { | |
| 98 String messageText = "Hello, world!"; | |
| 99 int messageCount = 0; | |
| 100 webSocket.listen((event) { | |
| 101 if (event is MessageEvent) { | |
| 102 messageCount++; | |
| 103 if (messageCount < 10) { | |
| 104 Expect.equals(messageText, event.data); | |
| 105 webSocket.send(event.data); | |
| 106 } else { | |
| 107 webSocket.close(closeStatus, closeReason); | |
| 108 } | |
| 109 } else if (event is CloseEvent) { | |
| 110 Expect.equals(closeStatus == null | |
| 111 ? WebSocketStatus.NO_STATUS_RECEIVED | |
| 112 : closeStatus, event.code); | |
| 113 Expect.equals("", event.reason); | |
| 114 closeCount++; | |
| 115 if (closeCount == totalConnections) { | |
| 116 server.close(); | |
| 117 } | |
| 118 } | |
| 119 }); | |
| 120 webSocket.send(messageText); | |
| 121 }); | |
| 122 | |
| 123 for (int i = 0; i < totalConnections; i++) { | |
| 124 createClient(server.port).then((webSocket) { | |
| 125 webSocket.listen((event) { | |
| 126 if (event is MessageEvent) { | |
| 127 webSocket.send(event.data); | |
| 128 } else if (event is CloseEvent) { | |
| 129 Expect.equals(closeStatus == null | |
| 130 ? WebSocketStatus.NO_STATUS_RECEIVED | |
| 131 : closeStatus, event.code); | |
| 132 Expect.equals( | |
| 133 closeReason == null ? "" : closeReason, event.reason); | |
| 134 } | |
| 135 }); | |
| 136 }); | |
| 137 } | |
| 138 | |
| 139 }); | |
| 140 } | |
| 141 | |
| 142 | |
| 143 void testMessageLength(int messageLength) { | |
| 144 createServer().then((server) { | |
| 145 Uint8List originalMessage = new Uint8List(messageLength); | |
| 146 server.transform(new WebSocketTransformer()).listen((webSocket) { | |
| 147 webSocket.listen((event) { | |
| 148 if (event is MessageEvent) { | |
| 149 Expect.listEquals(originalMessage, event.data); | |
| 150 webSocket.send(event.data); | |
| 151 } else if (event is CloseEvent) { | |
| 152 } | |
| 153 }); | |
| 154 }); | |
| 155 | |
| 156 createClient(server.port).then((webSocket) { | |
| 157 webSocket.listen((event) { | |
| 158 if (event is MessageEvent) { | |
| 159 Expect.listEquals(originalMessage, event.data); | |
| 160 webSocket.close(); | |
| 161 } else if (event is CloseEvent) { | |
| 162 server.close(); | |
| 163 } | |
| 164 }); | |
| 165 webSocket.send(originalMessage); | |
| 166 }); | |
| 167 }); | |
| 168 } | |
| 169 | |
| 170 | |
| 171 void testNoUpgrade() { | |
| 172 createServer().then((server) { | |
| 173 // Create a server which always responds with NOT_FOUND. | |
| 174 server.listen((request) { | |
| 175 request.response.statusCode = HttpStatus.NOT_FOUND; | |
| 176 request.response.close(); | |
| 177 }); | |
| 178 | |
| 179 createClient(server.port).catchError((error) { | |
| 180 server.close(); | |
| 181 }); | |
| 182 }); | |
| 183 } | |
| 184 | |
| 185 | |
| 186 void testUsePOST() { | |
| 187 createServer().then((server) { | |
| 188 var errorPort = new ReceivePort(); | |
| 189 server.transform(new WebSocketTransformer()).listen((webSocket) { | |
| 190 Expect.fail("No connection expected"); | |
| 191 }, onError: (e) { | |
| 192 errorPort.close(); | |
| 193 }); | |
| 194 | |
| 195 HttpClient client = new HttpClient(); | |
| 196 client.postUrl(Uri.parse( | |
| 197 "${secure ? 'https:' : 'http:'}//$HOST_NAME:${server.port}/")) | |
| 198 .then((request) => request.close()) | |
| 199 .then((response) { | |
| 200 Expect.equals(HttpStatus.BAD_REQUEST, response.statusCode); | |
| 201 client.close(); | |
| 202 server.close(); | |
| 203 }); | |
| 204 }); | |
| 205 } | |
| 206 | |
| 207 void testW3CInterface(int totalConnections, | |
| 208 int closeStatus, | |
| 209 String closeReason) { | |
| 210 createServer().then((server) { | |
| 211 int closeCount = 0; | |
| 212 server.transform(new WebSocketTransformer()).listen((webSocket) { | |
| 213 String messageText = "Hello, world!"; | |
| 214 int messageCount = 0; | |
| 215 webSocket.listen((event) { | |
| 216 if (event is MessageEvent) { | |
| 217 messageCount++; | |
| 218 if (messageCount < 10) { | |
| 219 Expect.equals(messageText, event.data); | |
| 220 webSocket.send(event.data); | |
| 221 } else { | |
| 222 webSocket.close(closeStatus, closeReason); | |
| 223 } | |
| 224 } else if (event is CloseEvent) { | |
| 225 Expect.equals(closeStatus, event.code); | |
| 226 Expect.equals("", event.reason); | |
| 227 closeCount++; | |
| 228 if (closeCount == totalConnections) { | |
| 229 server.close(); | |
| 230 } | |
| 231 } | |
| 232 }); | |
| 233 webSocket.send(messageText); | |
| 234 }); | |
| 235 | |
| 236 void webSocketConnection() { | |
| 237 bool onopenCalled = false; | |
| 238 int onmessageCalled = 0; | |
| 239 bool oncloseCalled = false; | |
| 240 | |
| 241 createClient(server.port).then((webSocket) { | |
| 242 Expect.isFalse(onopenCalled); | |
| 243 Expect.equals(0, onmessageCalled); | |
| 244 Expect.isFalse(oncloseCalled); | |
| 245 onopenCalled = true; | |
| 246 Expect.equals(WebSocket.OPEN, webSocket.readyState); | |
| 247 webSocket.listen((event) { | |
| 248 if (event is MessageEvent) { | |
| 249 onmessageCalled++; | |
| 250 Expect.isTrue(onopenCalled); | |
| 251 Expect.isFalse(oncloseCalled); | |
| 252 Expect.equals(WebSocket.OPEN, webSocket.readyState); | |
| 253 webSocket.send(event.data); | |
| 254 } else if (event is CloseEvent) { | |
| 255 Expect.isTrue(onopenCalled); | |
| 256 Expect.equals(10, onmessageCalled); | |
| 257 Expect.isFalse(oncloseCalled); | |
| 258 oncloseCalled = true; | |
| 259 Expect.isTrue(event.wasClean); | |
| 260 Expect.equals(3002, event.code); | |
| 261 Expect.equals("Got tired", event.reason); | |
| 262 Expect.equals(WebSocket.CLOSED, webSocket.readyState); | |
| 263 } | |
| 264 }); | |
| 265 }); | |
| 266 } | |
| 267 | |
| 268 for (int i = 0; i < totalConnections; i++) { | |
| 269 webSocketConnection(); | |
| 270 } | |
| 271 }); | |
| 272 } | |
| 273 | |
| 274 void runTests() { | |
| 275 testRequestResponseClientCloses(2, null, null); | |
| 276 testRequestResponseClientCloses(2, 3001, null); | |
| 277 testRequestResponseClientCloses(2, 3002, "Got tired"); | |
| 278 testRequestResponseServerCloses(2, null, null); | |
| 279 testRequestResponseServerCloses(2, 3001, null); | |
| 280 testRequestResponseServerCloses(2, 3002, "Got tired"); | |
| 281 testMessageLength(125); | |
| 282 testMessageLength(126); | |
| 283 testMessageLength(127); | |
| 284 testMessageLength(65535); | |
| 285 testMessageLength(65536); | |
| 286 testNoUpgrade(); | |
| 287 testUsePOST(); | |
| 288 testW3CInterface(10, 3002, "Got tired"); | |
| 289 } | |
| 290 } | |
| 291 | |
| 292 | |
| 293 main() { | |
| 294 new SecurityConfiguration(secure: false).runTests(); | |
| 295 } | |
| OLD | NEW |