| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 64 chrome.test.assertTrue(createInfo.socketId > 0); | 64 chrome.test.assertTrue(createInfo.socketId > 0); |
| 65 | 65 |
| 66 // Obtaining socket information before a connect() call should be safe, but | 66 // Obtaining socket information before a connect() call should be safe, but |
| 67 // return empty values. | 67 // return empty values. |
| 68 chrome.sockets.udp.getInfo(createInfo.socketId, onGetInfo); | 68 chrome.sockets.udp.getInfo(createInfo.socketId, onGetInfo); |
| 69 } | 69 } |
| 70 | 70 |
| 71 chrome.sockets.udp.create({}, onCreate); | 71 chrome.sockets.udp.create({}, onCreate); |
| 72 }; | 72 }; |
| 73 | 73 |
| 74 var testSocketReusable = function() { |
| 75 var address = '224.0.0.251'; |
| 76 var port = 5353; |
| 77 |
| 78 var numSocketsCreated = 0; |
| 79 var numSocketsExpected = 2; |
| 80 var numSocketsClosed = 0; |
| 81 |
| 82 var socketIds = []; |
| 83 |
| 84 function onCreate(createInfo) { |
| 85 function onGetInfoBeforeBind(info) { |
| 86 if (info.localAddress || info.localPort) { |
| 87 chrome.test.fail('Unconnected socket should not have local binding'); |
| 88 } |
| 89 |
| 90 chrome.test.assertEq(true, info.addressReusable, 'Should be reusable'); |
| 91 |
| 92 chrome.sockets.udp.bind(createInfo.socketId, address, port, onBind); |
| 93 } |
| 94 |
| 95 function onBind(result) { |
| 96 chrome.test.assertEq(0, result, 'Bind failed with error: ' + result); |
| 97 chrome.sockets.udp.getInfo(createInfo.socketId, onGetInfoAfterBind); |
| 98 } |
| 99 |
| 100 function onGetInfoAfterBind(info) { |
| 101 numSocketsCreated += 1; |
| 102 chrome.test.assertEq(address, info.localAddress, |
| 103 'Bound to wrong address'); |
| 104 chrome.test.assertEq(port, info.localPort, 'Bound to wrong port'); |
| 105 chrome.test.assertEq(true, info.addressReusable, 'Should be reusable'); |
| 106 |
| 107 if (numSocketsCreated < numSocketsExpected) { |
| 108 // Bind again to ensure the address is reusable. |
| 109 createAndBindToSharedAddress(); |
| 110 } else { |
| 111 for (var i = 0; i < socketIds.length; i++) { |
| 112 chrome.sockets.udp.close(socketIds[i], onClose); |
| 113 } |
| 114 } |
| 115 } |
| 116 |
| 117 function onClose() { |
| 118 numSocketsClosed += 1; |
| 119 if (numSocketsClosed == numSocketsExpected) { |
| 120 chrome.test.succeed(); |
| 121 } |
| 122 } |
| 123 |
| 124 chrome.test.assertTrue(createInfo.socketId > 0); |
| 125 socketIds.push(createInfo.socketId); |
| 126 |
| 127 chrome.sockets.udp.getInfo(createInfo.socketId, onGetInfoBeforeBind); |
| 128 } |
| 129 |
| 130 function createAndBindToSharedAddress() { |
| 131 chrome.sockets.udp.create({ "allowAddressReuse": true }, onCreate); |
| 132 } |
| 133 |
| 134 createAndBindToSharedAddress(); |
| 135 }; |
| 136 |
| 137 |
| 74 /////////////////////////////////////////////////////////////////////////////// | 138 /////////////////////////////////////////////////////////////////////////////// |
| 75 // Test socket send/receive | 139 // Test socket send/receive |
| 76 // | 140 // |
| 77 | 141 |
| 78 function waitForBlockingOperation() { | 142 function waitForBlockingOperation() { |
| 79 if (++waitCount < 10) { | 143 if (++waitCount < 10) { |
| 80 setTimeout(waitForBlockingOperation, 1000); | 144 setTimeout(waitForBlockingOperation, 1000); |
| 81 } else { | 145 } else { |
| 82 // We weren't able to succeed in the given time. | 146 // We weren't able to succeed in the given time. |
| 83 chrome.test.fail("Operations didn't complete after " + waitCount + " " + | 147 chrome.test.fail("Operations didn't complete after " + waitCount + " " + |
| (...skipping 241 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 325 var test_type = parts[0]; | 389 var test_type = parts[0]; |
| 326 address = parts[1]; | 390 address = parts[1]; |
| 327 port = parseInt(parts[2]); | 391 port = parseInt(parts[2]); |
| 328 console.log("Running tests, echo server " + | 392 console.log("Running tests, echo server " + |
| 329 address + ":" + port); | 393 address + ":" + port); |
| 330 if (test_type == 'multicast') { | 394 if (test_type == 'multicast') { |
| 331 console.log("Running multicast tests"); | 395 console.log("Running multicast tests"); |
| 332 chrome.test.runTests([ testMulticast ]); | 396 chrome.test.runTests([ testMulticast ]); |
| 333 } else { | 397 } else { |
| 334 console.log("Running udp tests"); | 398 console.log("Running udp tests"); |
| 335 chrome.test.runTests([ testSocketCreation, testSending, testSetPaused, | 399 chrome.test.runTests([ testSocketCreation, testSocketReusable, testSending, |
| 336 testBroadcast ]); | 400 testSetPaused, testBroadcast ]); |
| 337 } | 401 } |
| 338 }; | 402 }; |
| 339 | 403 |
| 340 // Find out which protocol we're supposed to test, and which echo server we | 404 // Find out which protocol we're supposed to test, and which echo server we |
| 341 // should be using, then kick off the tests. | 405 // should be using, then kick off the tests. |
| 342 chrome.test.sendMessage("info_please", onMessageReply); | 406 chrome.test.sendMessage("info_please", onMessageReply); |
| OLD | NEW |