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
3 // found in the LICENSE file.
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
7 // it isn't an echo server at all.
9 // The response is based on the request but obfuscated using a random key.
10 const request = "0100000005320000005hello";
11 var expectedResponsePattern = /0100000005320000005.{11}/;
19 var succeeded = false;
22 // Many thanks to Dennis for his StackOverflow answer: http://goo.gl/UDanx
23 // Since amended to handle BlobBuilder deprecation.
24 function string2ArrayBuffer(string, callback) {
25 var blob = new Blob([string]);
26 var f = new FileReader();
27 f.onload = function(e) {
28 callback(e.target.result);
30 f.readAsArrayBuffer(blob);
33 function arrayBuffer2String(buf, callback) {
34 var blob = new Blob([new Uint8Array(buf)]);
35 var f = new FileReader();
36 f.onload = function(e) {
37 callback(e.target.result);
42 ///////////////////////////////////////////////////////////////////////////////
43 // Test socket creation
46 var testSocketCreation = function() {
47 function onCreate(createInfo) {
48 function onGetInfo(info) {
49 if (info.localAddress || info.localPort) {
50 chrome.test.fail('Unconnected socket should not have local binding');
53 chrome.test.assertEq(createInfo.socketId, info.socketId);
54 chrome.test.assertEq(false, info.persistent);
56 chrome.sockets.udp.close(createInfo.socketId, function() {
57 chrome.sockets.udp.getInfo(createInfo.socketId, function(info) {
58 chrome.test.assertEq(undefined, info);
59 chrome.test.succeed();
64 chrome.test.assertTrue(createInfo.socketId > 0);
66 // Obtaining socket information before a connect() call should be safe, but
67 // return empty values.
68 chrome.sockets.udp.getInfo(createInfo.socketId, onGetInfo);
71 chrome.sockets.udp.create({}, onCreate);
74 ///////////////////////////////////////////////////////////////////////////////
75 // Test socket send/receive
78 function waitForBlockingOperation() {
79 if (++waitCount < 10) {
80 setTimeout(waitForBlockingOperation, 1000);
82 // We weren't able to succeed in the given time.
83 chrome.test.fail("Operations didn't complete after " + waitCount + " " +
84 "seconds. Response so far was <" + dataAsString + ">.");
88 var testSending = function() {
95 console.log("testSending");
96 setTimeout(waitForBlockingOperation, 1000);
97 chrome.sockets.udp.create({}, onCreate);
99 function onCreate(socketInfo) {
100 console.log("socket created: " + socketInfo.socketId);
101 localSocketId = socketId = socketInfo.socketId;
102 chrome.test.assertTrue(localSocketId > 0, "failed to create socket");
103 chrome.sockets.udp.onReceive.addListener(onReceive);
104 chrome.sockets.udp.onReceiveError.addListener(onReceiveError);
105 chrome.sockets.udp.bind(localSocketId, "0.0.0.0", 0, onBind);
108 function onBind(result) {
109 console.log("socket bound to local host");
110 chrome.test.assertEq(0, result, "Bind failed with error: " + result);
114 chrome.sockets.udp.getInfo(localSocketId, onGetInfo);
117 function onGetInfo(result) {
118 console.log("got socket info");
119 chrome.test.assertTrue(!!result.localAddress,
120 "Bound socket should always have local address");
121 chrome.test.assertTrue(!!result.localPort,
122 "Bound socket should always have local port");
124 string2ArrayBuffer(request, onArrayBuffer);
127 function onArrayBuffer(arrayBuffer) {
128 console.log("sending bytes to echo server: " + arrayBuffer.byteLength);
129 chrome.sockets.udp.send(localSocketId, arrayBuffer, address, port,
131 chrome.test.assertEq(0, sendInfo.resultCode);
132 chrome.test.assertEq(sendInfo.bytesSent, arrayBuffer.byteLength);
136 function onReceiveError(info) {
137 chrome.test.fail("Socket receive error: " + info.resultCode);
140 function onReceive(info) {
141 console.log("received bytes on from echo server: " + info.data.byteLength +
142 "(" + info.socketId + ")");
143 if (localSocketId == info.socketId) {
144 arrayBuffer2String(info.data, function(s) {
145 dataAsString = s; // save this for error reporting
146 var match = !!s.match(expectedResponsePattern);
147 chrome.test.assertTrue(match, "Received data does not match.");
148 chrome.sockets.udp.close(localSocketId, function () {
149 chrome.sockets.udp.onReceive.removeListener(onReceive);
150 chrome.sockets.udp.onReceiveError.removeListener(onReceiveError);
152 chrome.test.succeed();
159 var testSetPaused = function() {
164 var localSocketId = 0;
167 console.log("testSetPaused");
168 setTimeout(waitForBlockingOperation, 1000);
169 chrome.sockets.udp.create({}, onCreate);
171 function onCreate(socketInfo) {
172 console.log("socket created: " + socketInfo.socketId);
173 localSocketId = socketId = socketInfo.socketId;
174 chrome.test.assertTrue(localSocketId > 0, "failed to create socket");
176 chrome.sockets.udp.onReceiveError.addListener(onReceiveError);
177 chrome.sockets.udp.onReceive.addListener(onReceive);
179 chrome.sockets.udp.setPaused(localSocketId, true, function () {
180 chrome.sockets.udp.bind(localSocketId, "0.0.0.0", 0, onBind);
184 function onBind(result) {
185 console.log("socket bound to local host");
186 chrome.test.assertEq(0, result, "Bind failed with error: " + result);
190 string2ArrayBuffer(request, onArrayBuffer);
193 function onArrayBuffer(arrayBuffer) {
194 console.log("sending bytes to echo server: " + arrayBuffer.byteLength);
195 chrome.sockets.udp.send(localSocketId, arrayBuffer, address, port,
197 chrome.test.assertEq(0, sendInfo.resultCode);
198 chrome.test.assertEq(sendInfo.bytesSent, arrayBuffer.byteLength);
199 receiveTimer = setTimeout(waitForReceiveEvents, 1000);
203 function waitForReceiveEvents() {
204 chrome.sockets.udp.close(localSocketId, function () {
205 chrome.sockets.udp.onReceive.removeListener(onReceive);
206 chrome.sockets.udp.onReceiveError.removeListener(onReceiveError);
208 chrome.test.succeed("No data received from echo server!");
212 function onReceiveError(info) {
213 if (localSocketId == info.socketId) {
215 clearTimeout(receiveTimer);
216 chrome.test.fail("Socket receive error: " + info.resultCode);
220 function onReceive(info) {
221 console.log("Received data on socket" + "(" + info.socketId + ")");
222 if (localSocketId == info.socketId) {
224 clearTimeout(receiveTimer);
225 chrome.test.fail("Should not receive data when socket is paused: " +
226 info.data.byteLength);
231 var testBroadcast = function() {
232 var listeningSocketId;
235 console.log("testBroadcast");
236 chrome.sockets.udp.create({}, onCreate);
238 function onCreate(socketInfo) {
239 console.log("socket created: " + socketInfo.socketId);
240 chrome.test.assertTrue(socketId > 0, "failed to create socket");
242 if (listeningSocketId == undefined) {
243 listeningSocketId = socketInfo.socketId;
244 chrome.sockets.udp.onReceive.addListener(onReceive);
245 chrome.sockets.udp.onReceiveError.addListener(onReceiveError);
246 chrome.sockets.udp.bind(
247 listeningSocketId, "0.0.0.0", 8000, onBindListening);
249 sendingSocketId = socketInfo.socketId;
250 chrome.sockets.udp.bind(
251 sendingSocketId, "127.0.0.1", 8001, onBindSending);
255 function onBindListening(result) {
256 chrome.test.assertEq(0, result, "Bind failed with error: " + result);
261 chrome.sockets.udp.setBroadcast(
262 listeningSocketId, true, onSetBroadcastListening);
265 function onSetBroadcastListening(result) {
266 chrome.test.assertEq(0, result, "Failed to enable broadcast: " + result);
271 // Create the sending socket.
272 chrome.sockets.udp.create({}, onCreate);
275 function onBindSending(result) {
276 chrome.test.assertEq(0, result, "Bind failed with error: " + result);
281 chrome.sockets.udp.setBroadcast(
282 sendingSocketId, true, onSetBroadcastSending);
285 function onSetBroadcastSending(result) {
286 chrome.test.assertEq(0, result, "Failed to enable broadcast: " + result);
291 string2ArrayBuffer("broadcast packet", onArrayBuffer);
294 function onArrayBuffer(arrayBuffer) {
295 console.log("sending bytes to broadcast: " + arrayBuffer.byteLength);
296 chrome.sockets.udp.send(sendingSocketId, arrayBuffer, "127.255.255.255",
297 8000, function(sendInfo) {
298 chrome.test.assertEq(0, sendInfo.resultCode);
299 chrome.test.assertEq(sendInfo.bytesSent, arrayBuffer.byteLength);
303 function onReceiveError(info) {
304 chrome.test.fail("Socket receive error: " + info.resultCode);
307 function onReceive(info) {
308 console.log("Received data on socket" + "(" + info.socketId + ")");
309 chrome.test.assertEq(listeningSocketId, info.socketId);
310 chrome.test.assertEq("127.0.0.1", info.remoteAddress);
311 chrome.test.assertEq(8001, info.remotePort);
312 arrayBuffer2String(info.data, function(string) {
313 chrome.test.assertEq("broadcast packet", string);
314 chrome.test.succeed();
319 ///////////////////////////////////////////////////////////////////////////////
323 var onMessageReply = function(message) {
324 var parts = message.split(":");
325 var test_type = parts[0];
327 port = parseInt(parts[2]);
328 console.log("Running tests, echo server " +
329 address + ":" + port);
330 if (test_type == 'multicast') {
331 console.log("Running multicast tests");
332 chrome.test.runTests([ testMulticast ]);
334 console.log("Running udp tests");
335 chrome.test.runTests([ testSocketCreation, testSending, testSetPaused,
340 // Find out which protocol we're supposed to test, and which echo server we
341 // should be using, then kick off the tests.
342 chrome.test.sendMessage("info_please", onMessageReply);