| 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 "dart:async"; | 10 import "dart:async"; |
| (...skipping 24 matching lines...) Expand all Loading... |
| 35 backlog); | 35 backlog); |
| 36 | 36 |
| 37 Future<WebSocket> createClient(int port) => | 37 Future<WebSocket> createClient(int port) => |
| 38 WebSocket.connect('${secure ? "wss" : "ws"}://$HOST_NAME:$port/'); | 38 WebSocket.connect('${secure ? "wss" : "ws"}://$HOST_NAME:$port/'); |
| 39 | 39 |
| 40 void testRequestResponseClientCloses(int totalConnections, | 40 void testRequestResponseClientCloses(int totalConnections, |
| 41 int closeStatus, | 41 int closeStatus, |
| 42 String closeReason) { | 42 String closeReason) { |
| 43 createServer().then((server) { | 43 createServer().then((server) { |
| 44 server.transform(new WebSocketTransformer()).listen((webSocket) { | 44 server.transform(new WebSocketTransformer()).listen((webSocket) { |
| 45 webSocket.listen((event) { | 45 webSocket.listen( |
| 46 if (event is MessageEvent) { | 46 webSocket.send, |
| 47 webSocket.send(event.data); | 47 onDone: () { |
| 48 } else if (event is CloseEvent) { | 48 Expect.equals(closeStatus == null |
| 49 Expect.equals(closeStatus == null | 49 ? WebSocketStatus.NO_STATUS_RECEIVED |
| 50 ? WebSocketStatus.NO_STATUS_RECEIVED | 50 : closeStatus, webSocket.closeCode); |
| 51 : closeStatus, event.code); | 51 Expect.equals(closeReason == null ? "" : closeReason, webSocket.cl
oseReason); |
| 52 Expect.equals(closeReason == null ? "" : closeReason, event.reason); | 52 }); |
| 53 } | |
| 54 }); | 53 }); |
| 55 }); | |
| 56 | 54 |
| 57 int closeCount = 0; | 55 int closeCount = 0; |
| 58 String messageText = "Hello, world!"; | 56 String messageText = "Hello, world!"; |
| 59 for (int i = 0; i < totalConnections; i++) { | 57 for (int i = 0; i < totalConnections; i++) { |
| 60 int messageCount = 0; | 58 int messageCount = 0; |
| 61 createClient(server.port).then((webSocket) { | 59 createClient(server.port).then((webSocket) { |
| 62 webSocket.send(messageText); | 60 webSocket.send(messageText); |
| 63 webSocket.listen((event) { | 61 webSocket.listen( |
| 64 if (event is MessageEvent) { | 62 (message) { |
| 65 messageCount++; | 63 messageCount++; |
| 66 if (messageCount < 1 ) { | 64 if (messageCount < 1 ) { |
| 67 Expect.equals(messageText, event.data); | 65 Expect.equals(messageText, message); |
| 68 webSocket.send(event.data); | 66 webSocket.send(message); |
| 69 } else { | 67 } else { |
| 70 webSocket.close(closeStatus, closeReason); | 68 webSocket.close(closeStatus, closeReason); |
| 71 } | 69 } |
| 72 } else if (event is CloseEvent) { | 70 }, |
| 73 Expect.equals(closeStatus == null | 71 onDone: () { |
| 74 ? WebSocketStatus.NO_STATUS_RECEIVED | 72 Expect.equals(closeStatus == null |
| 75 : closeStatus, event.code); | 73 ? WebSocketStatus.NO_STATUS_RECEIVED |
| 76 Expect.equals("", event.reason); | 74 : closeStatus, webSocket.closeCode); |
| 77 closeCount++; | 75 Expect.equals("", webSocket.closeReason); |
| 78 if (closeCount == totalConnections) { | 76 closeCount++; |
| 79 server.close(); | 77 if (closeCount == totalConnections) { |
| 80 } | 78 server.close(); |
| 81 } | 79 } |
| 80 }); |
| 82 }); | 81 }); |
| 83 }); | |
| 84 } | 82 } |
| 85 }); | 83 }); |
| 86 } | 84 } |
| 87 | 85 |
| 88 void testRequestResponseServerCloses(int totalConnections, | 86 void testRequestResponseServerCloses(int totalConnections, |
| 89 int closeStatus, | 87 int closeStatus, |
| 90 String closeReason) { | 88 String closeReason) { |
| 91 createServer().then((server) { | 89 createServer().then((server) { |
| 92 int closeCount = 0; | 90 int closeCount = 0; |
| 93 server.transform(new WebSocketTransformer()).listen((webSocket) { | 91 server.transform(new WebSocketTransformer()).listen((webSocket) { |
| 94 String messageText = "Hello, world!"; | 92 String messageText = "Hello, world!"; |
| 95 int messageCount = 0; | 93 int messageCount = 0; |
| 96 webSocket.listen((event) { | 94 webSocket.listen( |
| 97 if (event is MessageEvent) { | 95 (message) { |
| 98 messageCount++; | 96 messageCount++; |
| 99 if (messageCount < 10) { | 97 if (messageCount < 10) { |
| 100 Expect.equals(messageText, event.data); | 98 Expect.equals(messageText, message); |
| 101 webSocket.send(event.data); | 99 webSocket.send(message); |
| 102 } else { | 100 } else { |
| 103 webSocket.close(closeStatus, closeReason); | 101 webSocket.close(closeStatus, closeReason); |
| 104 } | 102 } |
| 105 } else if (event is CloseEvent) { | 103 }, |
| 106 Expect.equals(closeStatus == null | 104 onDone: () { |
| 107 ? WebSocketStatus.NO_STATUS_RECEIVED | 105 Expect.equals(closeStatus == null |
| 108 : closeStatus, event.code); | 106 ? WebSocketStatus.NO_STATUS_RECEIVED |
| 109 Expect.equals("", event.reason); | 107 : closeStatus, webSocket.closeCode); |
| 110 closeCount++; | 108 Expect.equals("", webSocket.closeReason); |
| 111 if (closeCount == totalConnections) { | 109 closeCount++; |
| 112 server.close(); | 110 if (closeCount == totalConnections) { |
| 113 } | 111 server.close(); |
| 114 } | 112 } |
| 115 }); | 113 }); |
| 116 webSocket.send(messageText); | 114 webSocket.send(messageText); |
| 117 }); | 115 }); |
| 118 | 116 |
| 119 for (int i = 0; i < totalConnections; i++) { | 117 for (int i = 0; i < totalConnections; i++) { |
| 120 createClient(server.port).then((webSocket) { | 118 createClient(server.port).then((webSocket) { |
| 121 webSocket.listen((event) { | 119 webSocket.listen( |
| 122 if (event is MessageEvent) { | 120 webSocket.send, |
| 123 webSocket.send(event.data); | 121 onDone: () { |
| 124 } else if (event is CloseEvent) { | 122 Expect.equals(closeStatus == null |
| 125 Expect.equals(closeStatus == null | 123 ? WebSocketStatus.NO_STATUS_RECEIVED |
| 126 ? WebSocketStatus.NO_STATUS_RECEIVED | 124 : closeStatus, event.code); |
| 127 : closeStatus, event.code); | 125 Expect.equals( |
| 128 Expect.equals( | 126 closeReason == null ? "" : closeReason, event.reason); |
| 129 closeReason == null ? "" : closeReason, event.reason); | 127 }); |
| 130 } | |
| 131 }); | 128 }); |
| 132 }); | |
| 133 } | 129 } |
| 134 | |
| 135 }); | 130 }); |
| 136 } | 131 } |
| 137 | 132 |
| 138 | 133 |
| 139 void testMessageLength(int messageLength) { | 134 void testMessageLength(int messageLength) { |
| 140 createServer().then((server) { | 135 createServer().then((server) { |
| 141 Uint8List originalMessage = new Uint8List(messageLength); | 136 Uint8List originalMessage = new Uint8List(messageLength); |
| 142 server.transform(new WebSocketTransformer()).listen((webSocket) { | 137 server.transform(new WebSocketTransformer()).listen((webSocket) { |
| 143 webSocket.listen((event) { | 138 webSocket.listen( |
| 144 if (event is MessageEvent) { | 139 (message) { |
| 145 Expect.listEquals(originalMessage, event.data); | 140 Expect.listEquals(originalMessage, message); |
| 146 webSocket.send(event.data); | 141 webSocket.send(message); |
| 147 } else if (event is CloseEvent) { | 142 }); |
| 148 } | |
| 149 }); | |
| 150 }); | 143 }); |
| 151 | 144 |
| 152 createClient(server.port).then((webSocket) { | 145 createClient(server.port).then((webSocket) { |
| 153 webSocket.listen((event) { | 146 webSocket.listen( |
| 154 if (event is MessageEvent) { | 147 (message) { |
| 155 Expect.listEquals(originalMessage, event.data); | 148 Expect.listEquals(originalMessage, message); |
| 156 webSocket.close(); | 149 webSocket.close(); |
| 157 } else if (event is CloseEvent) { | 150 }, |
| 158 server.close(); | 151 onDone: server.close); |
| 159 } | 152 webSocket.send(originalMessage); |
| 160 }); | 153 }); |
| 161 webSocket.send(originalMessage); | |
| 162 }); | |
| 163 }); | 154 }); |
| 164 } | 155 } |
| 165 | 156 |
| 166 | 157 |
| 167 void testDoubleCloseClient() { | 158 void testDoubleCloseClient() { |
| 168 createServer().then((server) { | 159 createServer().then((server) { |
| 169 server.transform(new WebSocketTransformer()).listen((webSocket) { | 160 server.transform(new WebSocketTransformer()).listen((webSocket) { |
| 170 server.close(); | 161 server.close(); |
| 171 webSocket.listen((event) { | 162 webSocket.listen((_) { }, onDone: webSocket.close); |
| 172 if (event is CloseEvent) { | |
| 173 webSocket.close(); | |
| 174 } | |
| 175 }); | |
| 176 }); | 163 }); |
| 177 | 164 |
| 178 createClient(server.port).then((webSocket) { | 165 createClient(server.port).then((webSocket) { |
| 179 webSocket.listen((event) { | 166 webSocket.listen((_) { }, onDone: webSocket.close); |
| 180 if (event is CloseEvent) { | |
| 181 webSocket.close(); | |
| 182 } | |
| 183 }); | |
| 184 webSocket.close(); | 167 webSocket.close(); |
| 185 }); | 168 }); |
| 186 }); | 169 }); |
| 187 } | 170 } |
| 188 | 171 |
| 189 | 172 |
| 190 void testDoubleCloseServer() { | 173 void testDoubleCloseServer() { |
| 191 createServer().then((server) { | 174 createServer().then((server) { |
| 192 server.transform(new WebSocketTransformer()).listen((webSocket) { | 175 server.transform(new WebSocketTransformer()).listen((webSocket) { |
| 193 server.close(); | 176 server.close(); |
| 194 webSocket.listen((event) { | 177 webSocket.listen((_) { }, onDone: webSocket.close); |
| 195 if (event is CloseEvent) { | |
| 196 webSocket.close(); | |
| 197 } | |
| 198 }); | |
| 199 webSocket.close(); | 178 webSocket.close(); |
| 200 }); | 179 }); |
| 201 | 180 |
| 202 createClient(server.port).then((webSocket) { | 181 createClient(server.port).then((webSocket) { |
| 203 webSocket.listen((event) { | 182 webSocket.listen((_) { }, onDone: webSocket.close); |
| 204 if (event is CloseEvent) { | |
| 205 webSocket.close(); | |
| 206 } | |
| 207 }); | |
| 208 }); | 183 }); |
| 209 }); | 184 }); |
| 210 } | 185 } |
| 211 | 186 |
| 212 | 187 |
| 213 void testNoUpgrade() { | 188 void testNoUpgrade() { |
| 214 createServer().then((server) { | 189 createServer().then((server) { |
| 215 // Create a server which always responds with NOT_FOUND. | 190 // Create a server which always responds with NOT_FOUND. |
| 216 server.listen((request) { | 191 server.listen((request) { |
| 217 request.response.statusCode = HttpStatus.NOT_FOUND; | 192 request.response.statusCode = HttpStatus.NOT_FOUND; |
| (...skipping 21 matching lines...) Expand all Loading... |
| 239 "${secure ? 'https:' : 'http:'}//$HOST_NAME:${server.port}/")) | 214 "${secure ? 'https:' : 'http:'}//$HOST_NAME:${server.port}/")) |
| 240 .then((request) => request.close()) | 215 .then((request) => request.close()) |
| 241 .then((response) { | 216 .then((response) { |
| 242 Expect.equals(HttpStatus.BAD_REQUEST, response.statusCode); | 217 Expect.equals(HttpStatus.BAD_REQUEST, response.statusCode); |
| 243 client.close(); | 218 client.close(); |
| 244 server.close(); | 219 server.close(); |
| 245 }); | 220 }); |
| 246 }); | 221 }); |
| 247 } | 222 } |
| 248 | 223 |
| 249 void testW3CInterface(int totalConnections, | 224 void testConnections(int totalConnections, |
| 250 int closeStatus, | 225 int closeStatus, |
| 251 String closeReason) { | 226 String closeReason) { |
| 252 createServer().then((server) { | 227 createServer().then((server) { |
| 253 int closeCount = 0; | 228 int closeCount = 0; |
| 254 server.transform(new WebSocketTransformer()).listen((webSocket) { | 229 server.transform(new WebSocketTransformer()).listen((webSocket) { |
| 255 String messageText = "Hello, world!"; | 230 String messageText = "Hello, world!"; |
| 256 int messageCount = 0; | 231 int messageCount = 0; |
| 257 webSocket.listen((event) { | 232 webSocket.listen( |
| 258 if (event is MessageEvent) { | 233 (message) { |
| 259 messageCount++; | 234 messageCount++; |
| 260 if (messageCount < 10) { | 235 if (messageCount < 10) { |
| 261 Expect.equals(messageText, event.data); | 236 Expect.equals(messageText, message); |
| 262 webSocket.send(event.data); | 237 webSocket.send(message); |
| 263 } else { | 238 } else { |
| 264 webSocket.close(closeStatus, closeReason); | 239 webSocket.close(closeStatus, closeReason); |
| 265 } | 240 } |
| 266 } else if (event is CloseEvent) { | 241 }, |
| 267 Expect.equals(closeStatus, event.code); | 242 onDone: () { |
| 268 Expect.equals("", event.reason); | 243 Expect.equals(closeStatus, webSocket.closeCode); |
| 269 closeCount++; | 244 Expect.equals("", webSocket.closeReason); |
| 270 if (closeCount == totalConnections) { | 245 closeCount++; |
| 271 server.close(); | 246 if (closeCount == totalConnections) { |
| 272 } | 247 server.close(); |
| 273 } | 248 } |
| 274 }); | 249 }); |
| 275 webSocket.send(messageText); | 250 webSocket.send(messageText); |
| 276 }); | 251 }); |
| 277 | 252 |
| 278 void webSocketConnection() { | 253 void webSocketConnection() { |
| 279 bool onopenCalled = false; | 254 bool onopenCalled = false; |
| 280 int onmessageCalled = 0; | 255 int onmessageCalled = 0; |
| 281 bool oncloseCalled = false; | 256 bool oncloseCalled = false; |
| 282 | 257 |
| 283 createClient(server.port).then((webSocket) { | 258 createClient(server.port).then((webSocket) { |
| 284 Expect.isFalse(onopenCalled); | 259 Expect.isFalse(onopenCalled); |
| 285 Expect.equals(0, onmessageCalled); | 260 Expect.equals(0, onmessageCalled); |
| 286 Expect.isFalse(oncloseCalled); | 261 Expect.isFalse(oncloseCalled); |
| 287 onopenCalled = true; | 262 onopenCalled = true; |
| 288 Expect.equals(WebSocket.OPEN, webSocket.readyState); | 263 Expect.equals(WebSocket.OPEN, webSocket.readyState); |
| 289 webSocket.listen((event) { | 264 webSocket.listen( |
| 290 if (event is MessageEvent) { | 265 (message) { |
| 291 onmessageCalled++; | 266 onmessageCalled++; |
| 292 Expect.isTrue(onopenCalled); | 267 Expect.isTrue(onopenCalled); |
| 293 Expect.isFalse(oncloseCalled); | 268 Expect.isFalse(oncloseCalled); |
| 294 Expect.equals(WebSocket.OPEN, webSocket.readyState); | 269 Expect.equals(WebSocket.OPEN, webSocket.readyState); |
| 295 webSocket.send(event.data); | 270 webSocket.send(message); |
| 296 } else if (event is CloseEvent) { | 271 }, |
| 297 Expect.isTrue(onopenCalled); | 272 onDone: () { |
| 298 Expect.equals(10, onmessageCalled); | 273 Expect.isTrue(onopenCalled); |
| 299 Expect.isFalse(oncloseCalled); | 274 Expect.equals(10, onmessageCalled); |
| 300 oncloseCalled = true; | 275 Expect.isFalse(oncloseCalled); |
| 301 Expect.isTrue(event.wasClean); | 276 oncloseCalled = true; |
| 302 Expect.equals(3002, event.code); | 277 Expect.isTrue(event.wasClean); |
| 303 Expect.equals("Got tired", event.reason); | 278 Expect.equals(3002, event.code); |
| 304 Expect.equals(WebSocket.CLOSED, webSocket.readyState); | 279 Expect.equals("Got tired", event.reason); |
| 305 } | 280 Expect.equals(WebSocket.CLOSED, webSocket.readyState); |
| 306 }); | 281 }); |
| 307 }); | 282 }); |
| 308 } | 283 } |
| 309 | 284 |
| 310 for (int i = 0; i < totalConnections; i++) { | 285 for (int i = 0; i < totalConnections; i++) { |
| 311 webSocketConnection(); | 286 webSocketConnection(); |
| 312 } | 287 } |
| 313 }); | 288 }); |
| 314 } | 289 } |
| 315 | 290 |
| 316 void runTests() { | 291 void runTests() { |
| 317 testRequestResponseClientCloses(2, null, null); | 292 testRequestResponseClientCloses(2, null, null); |
| 318 testRequestResponseClientCloses(2, 3001, null); | 293 testRequestResponseClientCloses(2, 3001, null); |
| 319 testRequestResponseClientCloses(2, 3002, "Got tired"); | 294 testRequestResponseClientCloses(2, 3002, "Got tired"); |
| 320 testRequestResponseServerCloses(2, null, null); | 295 testRequestResponseServerCloses(2, null, null); |
| 321 testRequestResponseServerCloses(2, 3001, null); | 296 testRequestResponseServerCloses(2, 3001, null); |
| 322 testRequestResponseServerCloses(2, 3002, "Got tired"); | 297 testRequestResponseServerCloses(2, 3002, "Got tired"); |
| 323 testMessageLength(125); | 298 testMessageLength(125); |
| 324 testMessageLength(126); | 299 testMessageLength(126); |
| 325 testMessageLength(127); | 300 testMessageLength(127); |
| 326 testMessageLength(65535); | 301 testMessageLength(65535); |
| 327 testMessageLength(65536); | 302 testMessageLength(65536); |
| 328 testDoubleCloseClient(); | 303 testDoubleCloseClient(); |
| 329 testDoubleCloseServer(); | 304 testDoubleCloseServer(); |
| 330 testNoUpgrade(); | 305 testNoUpgrade(); |
| 331 testUsePOST(); | 306 testUsePOST(); |
| 332 testW3CInterface(10, 3002, "Got tired"); | 307 testConnections(10, 3002, "Got tired"); |
| 333 } | 308 } |
| 334 } | 309 } |
| 335 | 310 |
| 336 | 311 |
| 337 void initializeSSL() { | 312 void initializeSSL() { |
| 338 var testPkcertDatabase = | 313 var testPkcertDatabase = |
| 339 new Path(new Options().script).directoryPath.append("pkcert/"); | 314 new Path(new Options().script).directoryPath.append("pkcert/"); |
| 340 SecureSocket.initialize(database: testPkcertDatabase.toNativePath(), | 315 SecureSocket.initialize(database: testPkcertDatabase.toNativePath(), |
| 341 password: "dartdart"); | 316 password: "dartdart"); |
| 342 } | 317 } |
| 343 | 318 |
| 344 | 319 |
| 345 main() { | 320 main() { |
| 346 new SecurityConfiguration(secure: false).runTests(); | 321 new SecurityConfiguration(secure: false).runTests(); |
| 347 initializeSSL(); | 322 initializeSSL(); |
| 348 new SecurityConfiguration(secure: true).runTests(); | 323 new SecurityConfiguration(secure: true).runTests(); |
| 349 } | 324 } |
| OLD | NEW |