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 chrome.experimental.socket.accept(socketId, onServerSocketAccept); | |
208 | |
209 // Create a new socket to connect to the TCP server. | |
210 socket.create('tcp', {}, function(socketInfo) { | |
211 tmpSocketId = socketInfo.socketId; | |
212 socket.connect(tmpSocketId, address, port, | |
213 function(result) { | |
214 chrome.test.assertEq(0, result, "Connect failed"); | |
215 | |
216 // Write. | |
217 string2ArrayBuffer(request, function(buf) { | |
218 socket.write(tmpSocketId, buf, function() {}); | |
219 }); | |
220 }); | |
221 }); | |
222 } | |
223 | |
224 function onServerSocketCreate(socketInfo) { | |
225 socketId = socketInfo.socketId; | |
226 chrome.experimental.socket.listen(socketId, address, port, 5, onListen); | |
227 } | |
228 | |
229 socket.create('tcp', {}, onServerSocketCreate); | |
230 }; | |
231 | |
188 var onMessageReply = function(message) { | 232 var onMessageReply = function(message) { |
189 var parts = message.split(":"); | 233 var parts = message.split(":"); |
190 protocol = parts[0]; | 234 protocol = parts[0]; |
191 address = parts[1]; | 235 address = parts[1]; |
192 port = parseInt(parts[2]); | 236 port = parseInt(parts[2]); |
193 console.log("Running tests, protocol " + protocol + ", echo server " + | 237 console.log("Running tests, protocol " + protocol + ", echo server " + |
194 address + ":" + port); | 238 address + ":" + port); |
195 chrome.test.runTests([ testSocketCreation, testSending ]); | 239 if (protocol == 'tcp_server') |
miket_OOO
2012/09/24 18:14:21
It'd make more sense to change "protocol" to somet
justinlin
2012/09/26 08:59:59
Done.
| |
240 chrome.test.runTests([ testSocketListening]); | |
241 else | |
242 chrome.test.runTests([ testSocketCreation, testSending ]); | |
196 }; | 243 }; |
197 | 244 |
198 // Find out which protocol we're supposed to test, and which echo server we | 245 // Find out which protocol we're supposed to test, and which echo server we |
199 // should be using, then kick off the tests. | 246 // should be using, then kick off the tests. |
200 chrome.test.sendMessage("info_please", onMessageReply); | 247 chrome.test.sendMessage("info_please", onMessageReply); |
OLD | NEW |