| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 // net/tools/testserver/testserver.py is picky about the format of what it | 5 // net/tools/testserver/testserver.py is picky about the format of what it |
| 6 // calls its "echo" messages. One might go so far as to mutter to oneself that | 6 // calls its "echo" messages. One might go so far as to mutter to oneself that |
| 7 // it isn't an echo server at all. | 7 // it isn't an echo server at all. |
| 8 // | 8 // |
| 9 // The response is based on the request but obfuscated using a random key. | 9 // The response is based on the request but obfuscated using a random key. |
| 10 const request = "0100000005320000005hello"; | 10 const request = "0100000005320000005hello"; |
| (...skipping 188 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 199 var acceptedSocketId = acceptInfo.socketId; | 199 var acceptedSocketId = acceptInfo.socketId; |
| 200 socket.read(acceptedSocketId, function(readInfo) { | 200 socket.read(acceptedSocketId, function(readInfo) { |
| 201 arrayBuffer2String(readInfo.data, function (s) { | 201 arrayBuffer2String(readInfo.data, function (s) { |
| 202 assertDataMatch(request, s); | 202 assertDataMatch(request, s); |
| 203 succeeded = true; | 203 succeeded = true; |
| 204 // Test whether socket.getInfo correctly reflects the connection status | 204 // Test whether socket.getInfo correctly reflects the connection status |
| 205 // if the peer has closed the connection. | 205 // if the peer has closed the connection. |
| 206 setTimeout(function() { | 206 setTimeout(function() { |
| 207 socket.getInfo(acceptedSocketId, function(info) { | 207 socket.getInfo(acceptedSocketId, function(info) { |
| 208 chrome.test.assertFalse(info.connected); | 208 chrome.test.assertFalse(info.connected); |
| 209 socket.destroy(socketId); |
| 209 chrome.test.succeed(); | 210 chrome.test.succeed(); |
| 210 }); | 211 }); |
| 211 }, 500); | 212 }, 500); |
| 212 }); | 213 }); |
| 213 }); | 214 }); |
| 214 } | 215 } |
| 215 | 216 |
| 216 function onListen(result) { | 217 function onListen(result) { |
| 217 chrome.test.assertEq(0, result, "Listen failed."); | 218 chrome.test.assertEq(0, result, "Listen failed."); |
| 218 socket.accept(socketId, onServerSocketAccept); | 219 socket.accept(socketId, onServerSocketAccept); |
| (...skipping 21 matching lines...) Expand all Loading... |
| 240 } | 241 } |
| 241 | 242 |
| 242 function onServerSocketCreate(socketInfo) { | 243 function onServerSocketCreate(socketInfo) { |
| 243 socketId = socketInfo.socketId; | 244 socketId = socketInfo.socketId; |
| 244 socket.listen(socketId, address, port, onListen); | 245 socket.listen(socketId, address, port, onListen); |
| 245 } | 246 } |
| 246 | 247 |
| 247 socket.create('tcp', {}, onServerSocketCreate); | 248 socket.create('tcp', {}, onServerSocketCreate); |
| 248 }; | 249 }; |
| 249 | 250 |
| 251 // Tests creation of a TCP listening socket on a port that is already in use. |
| 252 var testSocketListenInUse = function() { |
| 253 var tmpSocketId; |
| 254 |
| 255 function onAccept(result) { |
| 256 chrome.test.assertNoLastError(); |
| 257 chrome.test.assertEq(-2, result.resultCode); |
| 258 socket.destroy(socketId); |
| 259 socket.destroy(tmpSocketId); |
| 260 chrome.test.succeed(); |
| 261 } |
| 262 |
| 263 function onSecondSocketListen(result) { |
| 264 chrome.test.assertLastError("Could not listen on the specified port."); |
| 265 chrome.test.assertEq(-147, result); |
| 266 // Calling accept on this socket should fail since it isn't listening. |
| 267 socket.accept(tmpSocketId, onAccept); |
| 268 } |
| 269 |
| 270 function onSecondSocketCreate(socketInfo) { |
| 271 chrome.test.assertNoLastError(); |
| 272 tmpSocketId = socketInfo.socketId; |
| 273 socket.listen(tmpSocketId, address, port, onSecondSocketListen); |
| 274 } |
| 275 |
| 276 function onFirstSocketListen(result) { |
| 277 chrome.test.assertNoLastError(); |
| 278 chrome.test.assertEq(0, result); |
| 279 socket.create('tcp', {}, onSecondSocketCreate); |
| 280 } |
| 281 |
| 282 function onFirstSocketCreate(socketInfo) { |
| 283 chrome.test.assertNoLastError(); |
| 284 socketId = socketInfo.socketId; |
| 285 socket.listen(socketId, address, port, onFirstSocketListen); |
| 286 } |
| 287 |
| 288 socket.create('tcp', {}, onFirstSocketCreate); |
| 289 }; |
| 290 |
| 250 var testPendingCallback = function() { | 291 var testPendingCallback = function() { |
| 251 dataRead = ""; | 292 dataRead = ""; |
| 252 succeeded = false; | 293 succeeded = false; |
| 253 waitCount = 0; | 294 waitCount = 0; |
| 254 | 295 |
| 255 console.log("calling create"); | 296 console.log("calling create"); |
| 256 chrome.socket.create(protocol, null, onCreate); | 297 chrome.socket.create(protocol, null, onCreate); |
| 257 | 298 |
| 258 function onCreate(createInfo) { | 299 function onCreate(createInfo) { |
| 259 chrome.test.assertTrue(createInfo.socketId > 0, "failed to create socket"); | 300 chrome.test.assertTrue(createInfo.socketId > 0, "failed to create socket"); |
| (...skipping 107 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 367 }; | 408 }; |
| 368 | 409 |
| 369 var onMessageReply = function(message) { | 410 var onMessageReply = function(message) { |
| 370 var parts = message.split(":"); | 411 var parts = message.split(":"); |
| 371 var test_type = parts[0]; | 412 var test_type = parts[0]; |
| 372 address = parts[1]; | 413 address = parts[1]; |
| 373 port = parseInt(parts[2]); | 414 port = parseInt(parts[2]); |
| 374 console.log("Running tests, protocol " + protocol + ", echo server " + | 415 console.log("Running tests, protocol " + protocol + ", echo server " + |
| 375 address + ":" + port); | 416 address + ":" + port); |
| 376 if (test_type == 'tcp_server') { | 417 if (test_type == 'tcp_server') { |
| 377 chrome.test.runTests([ testSocketListening ]); | 418 chrome.test.runTests([ |
| 419 testSocketListening, |
| 420 testSocketListenInUse |
| 421 ]); |
| 378 } else if (test_type == 'multicast') { | 422 } else if (test_type == 'multicast') { |
| 379 console.log("Running multicast tests"); | 423 console.log("Running multicast tests"); |
| 380 chrome.test.runTests([ testMulticast ]); | 424 chrome.test.runTests([ testMulticast ]); |
| 381 } else { | 425 } else { |
| 382 protocol = test_type; | 426 protocol = test_type; |
| 383 chrome.test.runTests([ | 427 chrome.test.runTests([ |
| 384 testSocketCreation, | 428 testSocketCreation, |
| 385 testSending, | 429 testSending, |
| 386 testPendingCallback, | 430 testPendingCallback, |
| 387 testUsingTCPSocketOnUDPMethods]); | 431 testUsingTCPSocketOnUDPMethods]); |
| 388 } | 432 } |
| 389 }; | 433 }; |
| 390 | 434 |
| 391 // Find out which protocol we're supposed to test, and which echo server we | 435 // Find out which protocol we're supposed to test, and which echo server we |
| 392 // should be using, then kick off the tests. | 436 // should be using, then kick off the tests. |
| 393 chrome.test.sendMessage("info_please", onMessageReply); | 437 chrome.test.sendMessage("info_please", onMessageReply); |
| OLD | NEW |