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 167 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
178 | 178 |
179 var testSending = function() { | 179 var testSending = function() { |
180 dataRead = ""; | 180 dataRead = ""; |
181 succeeded = false; | 181 succeeded = false; |
182 waitCount = 0; | 182 waitCount = 0; |
183 | 183 |
184 setTimeout(waitForBlockingOperation, 1000); | 184 setTimeout(waitForBlockingOperation, 1000); |
185 socket.create(protocol, {}, onCreate); | 185 socket.create(protocol, {}, onCreate); |
186 }; | 186 }; |
187 | 187 |
| 188 // Tests listening on a socket and sending/receiving from accepted sockets. |
| 189 var testSocketListening = function() { |
| 190 var tmpSocketId = 0; |
| 191 |
| 192 function onServerSocketAccept(acceptInfo) { |
| 193 chrome.test.assertEq(0, acceptInfo.resultCode); |
| 194 var acceptedSocketId = acceptInfo.socketId; |
| 195 socket.read(acceptedSocketId, function(readInfo) { |
| 196 arrayBuffer2String(readInfo.data, function(s) { |
| 197 var match = !!s.match(request); |
| 198 chrome.test.assertTrue(match, "Received data does not match."); |
| 199 succeeded = true; |
| 200 chrome.test.succeed(); |
| 201 }); |
| 202 }); |
| 203 } |
| 204 |
| 205 function onListen(result) { |
| 206 chrome.test.assertEq(0, result, "Listen failed."); |
| 207 socket.accept(socketId, onServerSocketAccept); |
| 208 |
| 209 // Trying to schedule a second accept callback should fail. |
| 210 socket.accept(socketId, function(acceptInfo) { |
| 211 chrome.test.assertEq(-2, acceptInfo.resultCode); |
| 212 }); |
| 213 |
| 214 // Create a new socket to connect to the TCP server. |
| 215 socket.create('tcp', {}, function(socketInfo) { |
| 216 tmpSocketId = socketInfo.socketId; |
| 217 socket.connect(tmpSocketId, address, port, |
| 218 function(result) { |
| 219 chrome.test.assertEq(0, result, "Connect failed"); |
| 220 |
| 221 // Write. |
| 222 string2ArrayBuffer(request, function(buf) { |
| 223 socket.write(tmpSocketId, buf, function() {}); |
| 224 }); |
| 225 }); |
| 226 }); |
| 227 } |
| 228 |
| 229 function onServerSocketCreate(socketInfo) { |
| 230 socketId = socketInfo.socketId; |
| 231 socket.listen(socketId, address, port, onListen); |
| 232 } |
| 233 |
| 234 socket.create('tcp', {}, onServerSocketCreate); |
| 235 }; |
| 236 |
188 var onMessageReply = function(message) { | 237 var onMessageReply = function(message) { |
189 var parts = message.split(":"); | 238 var parts = message.split(":"); |
190 protocol = parts[0]; | 239 test_type = parts[0]; |
191 address = parts[1]; | 240 address = parts[1]; |
192 port = parseInt(parts[2]); | 241 port = parseInt(parts[2]); |
193 console.log("Running tests, protocol " + protocol + ", echo server " + | 242 console.log("Running tests, protocol " + protocol + ", echo server " + |
194 address + ":" + port); | 243 address + ":" + port); |
195 chrome.test.runTests([ testSocketCreation, testSending ]); | 244 if (test_type == 'tcp_server') { |
| 245 chrome.test.runTests([ testSocketListening ]); |
| 246 } else { |
| 247 protocol = test_type; |
| 248 chrome.test.runTests([ testSocketCreation, testSending ]); |
| 249 } |
196 }; | 250 }; |
197 | 251 |
198 // Find out which protocol we're supposed to test, and which echo server we | 252 // Find out which protocol we're supposed to test, and which echo server we |
199 // should be using, then kick off the tests. | 253 // should be using, then kick off the tests. |
200 chrome.test.sendMessage("info_please", onMessageReply); | 254 chrome.test.sendMessage("info_please", onMessageReply); |
OLD | NEW |