| 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 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 65 chrome.test.assertTrue(socketInfo.socketId > 0); | 65 chrome.test.assertTrue(socketInfo.socketId > 0); |
| 66 | 66 |
| 67 // TODO(miket): this doesn't work yet. It's possible this will become | 67 // TODO(miket): this doesn't work yet. It's possible this will become |
| 68 // automatic, but either way we can't forget to clean up. | 68 // automatic, but either way we can't forget to clean up. |
| 69 // | 69 // |
| 70 //socket.destroy(socketInfo.socketId); | 70 //socket.destroy(socketInfo.socketId); |
| 71 | 71 |
| 72 chrome.test.succeed(); | 72 chrome.test.succeed(); |
| 73 } | 73 } |
| 74 | 74 |
| 75 socket.create(protocol, {onEvent: function(e) {}}, onCreate); | 75 socket.create(protocol, {}, onCreate); |
| 76 }; | 76 }; |
| 77 | 77 |
| 78 function onDataRead(readInfo) { | 78 function onDataRead(readInfo) { |
| 79 if (readInfo.resultCode > 0 || readInfo.data.length > 0) { | 79 if (readInfo.resultCode > 0 || readInfo.data.length > 0) { |
| 80 chrome.test.assertEq(readInfo.resultCode, readInfo.data.length); | 80 chrome.test.assertEq(readInfo.resultCode, readInfo.data.length); |
| 81 } | 81 } |
| 82 | 82 |
| 83 // TODO(miket): this isn't correct for multiple calls of onDataRead. | 83 // TODO(miket): this isn't correct for multiple calls of onDataRead. |
| 84 arrayBuffer2String(arrayOfLongsToArrayBuffer(readInfo.data), function(s) { | 84 arrayBuffer2String(arrayOfLongsToArrayBuffer(readInfo.data), function(s) { |
| 85 dataAsString = s; // save this for error reporting | 85 dataAsString = s; // save this for error reporting |
| 86 if (s.match(expectedResponsePattern)) { | 86 if (s.match(expectedResponsePattern)) { |
| 87 succeeded = true; | 87 succeeded = true; |
| 88 chrome.test.succeed(); | 88 chrome.test.succeed(); |
| 89 } | 89 } |
| 90 }); | 90 }); |
| 91 // Blocked. Wait for onEvent. | |
| 92 } | 91 } |
| 93 | 92 |
| 94 function onWriteOrSendToComplete(writeInfo) { | 93 function onWriteOrSendToComplete(writeInfo) { |
| 95 bytesWritten += writeInfo.bytesWritten; | 94 bytesWritten += writeInfo.bytesWritten; |
| 96 if (bytesWritten == request.length) { | 95 if (bytesWritten == request.length) { |
| 97 if (protocol == "tcp") | 96 if (protocol == "tcp") |
| 98 socket.read(socketId, onDataRead); | 97 socket.read(socketId, onDataRead); |
| 99 else | 98 else |
| 100 socket.recvFrom(socketId, onDataRead); | 99 socket.recvFrom(socketId, onDataRead); |
| 101 } | 100 } |
| 102 // Blocked. Wait for onEvent. | |
| 103 } | 101 } |
| 104 | 102 |
| 105 function onConnectOrBindComplete(connectResult) { | 103 function onConnectOrBindComplete(connectResult) { |
| 106 if (connectResult == 0) { | 104 if (connectResult == 0) { |
| 107 string2ArrayBuffer(request, function(arrayBuffer) { | 105 string2ArrayBuffer(request, function(arrayBuffer) { |
| 108 var longs = arrayBufferToArrayOfLongs(arrayBuffer); | 106 var longs = arrayBufferToArrayOfLongs(arrayBuffer); |
| 109 if (protocol == "tcp") | 107 if (protocol == "tcp") |
| 110 socket.write(socketId, longs, onWriteOrSendToComplete); | 108 socket.write(socketId, longs, onWriteOrSendToComplete); |
| 111 else | 109 else |
| 112 socket.sendTo(socketId, longs, address, port, | 110 socket.sendTo(socketId, longs, address, port, |
| 113 onWriteOrSendToComplete); | 111 onWriteOrSendToComplete); |
| 114 }); | 112 }); |
| 115 } | 113 } |
| 116 // Blocked. Wait for onEvent. | |
| 117 } | 114 } |
| 118 | 115 |
| 119 function onCreate(socketInfo) { | 116 function onCreate(socketInfo) { |
| 120 socketId = socketInfo.socketId; | 117 socketId = socketInfo.socketId; |
| 121 chrome.test.assertTrue(socketId > 0, "failed to create socket"); | 118 chrome.test.assertTrue(socketId > 0, "failed to create socket"); |
| 122 if (protocol == "tcp") | 119 if (protocol == "tcp") |
| 123 socket.connect(socketId, address, port, onConnectOrBindComplete); | 120 socket.connect(socketId, address, port, onConnectOrBindComplete); |
| 124 else | 121 else |
| 125 socket.bind(socketId, "0.0.0.0", 0, onConnectOrBindComplete); | 122 socket.bind(socketId, "0.0.0.0", 0, onConnectOrBindComplete); |
| 126 } | 123 } |
| 127 | 124 |
| 128 function onEvent(socketEvent) { | |
| 129 if (socketEvent.type == "connectComplete") { | |
| 130 onConnectOrBindComplete(socketEvent.resultCode); | |
| 131 } else if (socketEvent.type == "dataRead") { | |
| 132 onDataRead({resultCode: socketEvent.resultCode, data: socketEvent.data}); | |
| 133 } else if (socketEvent.type == "writeComplete") { | |
| 134 onWriteOnSendToComplete(socketEvent.resultCode); | |
| 135 } else { | |
| 136 console.log("Received unhandled socketEvent of type " + socketEvent.type); | |
| 137 } | |
| 138 }; | |
| 139 | |
| 140 function waitForBlockingOperation() { | 125 function waitForBlockingOperation() { |
| 141 if (++waitCount < 10) { | 126 if (++waitCount < 10) { |
| 142 setTimeout(waitForBlockingOperation, 1000); | 127 setTimeout(waitForBlockingOperation, 1000); |
| 143 } else { | 128 } else { |
| 144 // We weren't able to succeed in the given time. | 129 // We weren't able to succeed in the given time. |
| 145 chrome.test.fail("Operations didn't complete after " + waitCount + " " + | 130 chrome.test.fail("Operations didn't complete after " + waitCount + " " + |
| 146 "seconds. Response so far was <" + dataAsString + ">."); | 131 "seconds. Response so far was <" + dataAsString + ">."); |
| 147 } | 132 } |
| 148 } | 133 } |
| 149 | 134 |
| 150 var testSending = function() { | 135 var testSending = function() { |
| 151 dataRead = ""; | 136 dataRead = ""; |
| 152 succeeded = false; | 137 succeeded = false; |
| 153 waitCount = 0; | 138 waitCount = 0; |
| 154 | 139 |
| 155 setTimeout(waitForBlockingOperation, 1000); | 140 setTimeout(waitForBlockingOperation, 1000); |
| 156 socket.create(protocol, {onEvent: onEvent}, onCreate); | 141 socket.create(protocol, {}, onCreate); |
| 157 }; | 142 }; |
| 158 | 143 |
| 159 var onMessageReply = function(message) { | 144 var onMessageReply = function(message) { |
| 160 var parts = message.split(":"); | 145 var parts = message.split(":"); |
| 161 protocol = parts[0]; | 146 protocol = parts[0]; |
| 162 address = parts[1]; | 147 address = parts[1]; |
| 163 port = parseInt(parts[2]); | 148 port = parseInt(parts[2]); |
| 164 console.log("Running tests, protocol " + protocol + ", echo server " + | 149 console.log("Running tests, protocol " + protocol + ", echo server " + |
| 165 address + ":" + port); | 150 address + ":" + port); |
| 166 chrome.test.runTests([ testSocketCreation, testSending ]); | 151 chrome.test.runTests([ testSocketCreation, testSending ]); |
| 167 }; | 152 }; |
| 168 | 153 |
| 169 // Find out which protocol we're supposed to test, and which echo server we | 154 // Find out which protocol we're supposed to test, and which echo server we |
| 170 // should be using, then kick off the tests. | 155 // should be using, then kick off the tests. |
| 171 chrome.test.sendMessage("info_please", onMessageReply); | 156 chrome.test.sendMessage("info_please", onMessageReply); |
| OLD | NEW |