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";
15 var succeeded = false;
17 // Many thanks to Dennis for his StackOverflow answer: http://goo.gl/UDanx
18 // Since amended to handle BlobBuilder deprecation.
19 function string2ArrayBuffer(string, callback) {
20 var blob = new Blob([string]);
21 var f = new FileReader();
22 f.onload = function(e) {
23 callback(e.target.result);
25 f.readAsArrayBuffer(blob);
28 function arrayBuffer2String(buf, callback) {
29 var blob = new Blob([new Uint8Array(buf)]);
30 var f = new FileReader();
31 f.onload = function(e) {
32 callback(e.target.result);
37 // Tests listening on a socket and sending/receiving from accepted sockets.
38 var testSocketListening = function() {
41 chrome.sockets.tcpServer.create({}, onServerSocketCreate);
43 function onServerSocketCreate(socketInfo) {
44 console.log("Server socket created: sd=" + socketInfo.socketId);
45 socketId = socketInfo.socketId;
46 chrome.sockets.tcpServer.listen(socketId, address, port, onListen);
49 function onListen(result) {
50 console.log("Server socket 'listen' completed: sd=" + socketId +
51 ", result=" + result);
52 chrome.test.assertEq(0, result, "Listen failed.");
53 chrome.sockets.tcpServer.onAccept.addListener(onServerSocketAccept);
54 chrome.sockets.tcp.onReceive.addListener(onReceive);
55 chrome.sockets.tcp.onReceiveError.addListener(onReceiveError);
57 // Create a new socket to connect to the TCP server.
58 chrome.sockets.tcp.create({}, function(socketInfo) {
59 console.log("Client socket created: sd=" + socketInfo.socketId);
60 tmpSocketId = socketInfo.socketId;
61 chrome.sockets.tcp.connect(tmpSocketId, address, port,
63 console.log("Client socket connected: sd=" + tmpSocketId);
64 chrome.test.assertEq(0, result, "Connect failed");
67 string2ArrayBuffer(request, function(buf) {
68 chrome.sockets.tcp.send(tmpSocketId, buf, function(sendInfo) {
69 console.log("Client socket data sent: sd=" + tmpSocketId
70 + ", result=" + sendInfo.resultCode);
71 chrome.sockets.tcp.disconnect(tmpSocketId, function() {
72 console.log("Client socket disconnected: sd=" + tmpSocketId);
82 function onServerSocketAccept(acceptInfo) {
83 console.log("Server socket 'accept' event: sd=" + acceptInfo.socketId +
84 ", client sd=" + acceptInfo.clientSocketId);
85 chrome.test.assertEq(socketId, acceptInfo.socketId, "Wrong server socket.");
86 chrome.test.assertTrue(acceptInfo.clientSocketId > 0);
87 clientSocketId = acceptInfo.clientSocketId;
88 chrome.sockets.tcp.setPaused(clientSocketId, false, function() {});
91 function onReceive(receiveInfo) {
92 console.log("Client socket 'receive' event: sd=" + receiveInfo.socketId
93 + ", bytes=" + receiveInfo.data.byteLength);
94 chrome.test.assertEq(clientSocketId, receiveInfo.socketId,
95 "Received data on wrong socket");
96 if (receiveInfo.data.byteLength == 0)
98 arrayBuffer2String(receiveInfo.data, function(s) {
99 var match = !!s.match(request);
100 chrome.test.assertTrue(match, "Received data does not match.");
102 // Test whether socket.getInfo correctly reflects the connection status
103 // if the peer has closed the connection.
104 setTimeout(function() {
105 chrome.sockets.tcp.getInfo(receiveInfo.socketId, function(info) {
106 chrome.test.assertFalse(info.connected);
107 chrome.test.succeed();
113 function onReceiveError(receiveInfo) {
114 console.log("Client socket 'receive error' event: sd="
115 + receiveInfo.socketId + ", result=" + receiveInfo.resultCode);
116 //chrome.test.fail("Receive failed.");
120 var onMessageReply = function(message) {
121 var parts = message.split(":");
122 var test_type = parts[0];
124 port = parseInt(parts[2]);
125 console.log("Running tests, protocol " + test_type + ", echo server " +
126 address + ":" + port);
127 if (test_type == 'tcp_server') {
128 chrome.test.runTests([testSocketListening]);
132 // Find out which protocol we're supposed to test, and which echo server we
133 // should be using, then kick off the tests.
134 chrome.test.sendMessage("info_please", onMessageReply);