Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(53)

Side by Side Diff: chrome/test/data/extensions/api_test/socket/api/background.js

Issue 10790137: Adds socket.getInfo to the socket API (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: Check for NULL TCPSocket, updated test Created 8 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 24 matching lines...) Expand all
35 function arrayBuffer2String(buf, callback) { 35 function arrayBuffer2String(buf, callback) {
36 var blob = new Blob([new Uint8Array(buf)]); 36 var blob = new Blob([new Uint8Array(buf)]);
37 var f = new FileReader(); 37 var f = new FileReader();
38 f.onload = function(e) { 38 f.onload = function(e) {
39 callback(e.target.result); 39 callback(e.target.result);
40 }; 40 };
41 f.readAsText(blob); 41 f.readAsText(blob);
42 } 42 }
43 43
44 var testSocketCreation = function() { 44 var testSocketCreation = function() {
45 function onCreate(socketInfo) { 45 function onGetInfo(info) {
46 chrome.test.assertTrue(socketInfo.socketId > 0); 46 chrome.test.assertEq(info.socketType, protocol);
47 chrome.test.assertFalse(info.connected);
48
49 if (info.peerAddress || info.peerPort) {
50 chrome.test.fail('Unconnected socket should not have peer');
51 }
52 if (info.localAddress || info.localPort) {
53 chrome.test.fail('Unconnected socket should not have local binding');
54 }
47 55
48 // TODO(miket): this doesn't work yet. It's possible this will become 56 // TODO(miket): this doesn't work yet. It's possible this will become
49 // automatic, but either way we can't forget to clean up. 57 // automatic, but either way we can't forget to clean up.
50 // 58 //
51 //socket.destroy(socketInfo.socketId); 59 //socket.destroy(socketInfo.socketId);
52 60
53 chrome.test.succeed(); 61 chrome.test.succeed();
54 } 62 }
55 63
64 function onCreate(socketInfo) {
65 chrome.test.assertTrue(socketInfo.socketId > 0);
66
67 // Obtaining socket information before a connect() call should be safe, but
68 // return empty values.
69 socket.getInfo(socketInfo.socketId, onGetInfo);
70 }
71
56 socket.create(protocol, {}, onCreate); 72 socket.create(protocol, {}, onCreate);
57 }; 73 };
58 74
75
76 var testGetInfo = function() {
77 };
78
59 function onDataRead(readInfo) { 79 function onDataRead(readInfo) {
60 if (readInfo.resultCode > 0 || readInfo.data.byteLength > 0) { 80 if (readInfo.resultCode > 0 || readInfo.data.byteLength > 0) {
61 chrome.test.assertEq(readInfo.resultCode, readInfo.data.byteLength); 81 chrome.test.assertEq(readInfo.resultCode, readInfo.data.byteLength);
62 } 82 }
63 83
64 arrayBuffer2String(readInfo.data, function(s) { 84 arrayBuffer2String(readInfo.data, function(s) {
65 dataAsString = s; // save this for error reporting 85 dataAsString = s; // save this for error reporting
66 var match = !!s.match(expectedResponsePattern); 86 var match = !!s.match(expectedResponsePattern);
67 chrome.test.assertTrue(match, "Received data does not match."); 87 chrome.test.assertTrue(match, "Received data does not match.");
68 succeeded = true; 88 succeeded = true;
(...skipping 27 matching lines...) Expand all
96 } 116 }
97 117
98 function onSetNoDelay(result) { 118 function onSetNoDelay(result) {
99 if (protocol == "tcp") 119 if (protocol == "tcp")
100 chrome.test.assertTrue(result, "setNoDelay failed for TCP."); 120 chrome.test.assertTrue(result, "setNoDelay failed for TCP.");
101 else 121 else
102 chrome.test.assertFalse(result, "setNoDelay did not fail for UDP."); 122 chrome.test.assertFalse(result, "setNoDelay did not fail for UDP.");
103 socket.setKeepAlive(socketId, true, 1000, onSetKeepAlive); 123 socket.setKeepAlive(socketId, true, 1000, onSetKeepAlive);
104 } 124 }
105 125
126 function onGetInfo(result) {
127 chrome.test.assertTrue(!!result.localAddress,
128 "Bound socket should always have local address");
129 chrome.test.assertTrue(!!result.localPort,
130 "Bound socket should always have local port");
131 chrome.test.assertEq(result.socketType, protocol, "Unexpected socketType");
132
133 if (protocol == "tcp") {
134 // NOTE: We're always called with 'localhost', but getInfo will only return
135 // IPs, not names.
136 chrome.test.assertEq(result.peerAddress, "127.0.0.1",
137 "Peer addresss should be the listen server");
138 chrome.test.assertEq(result.peerPort, port,
139 "Peer port should be the listen server");
140 chrome.test.assertTrue(result.connected, "Socket should be connected");
141 } else {
142 chrome.test.assertFalse(result.connected, "UDP socket was not connected");
143 chrome.test.assertTrue(!result.peerAddress,
144 "Unconnected UDP socket should not have peer address");
145 chrome.test.assertTrue(!result.peerPort,
146 "Unconnected UDP socket should not have peer port");
147 }
148
149 socket.setNoDelay(socketId, true, onSetNoDelay);
150 }
151
106 function onConnectOrBindComplete(result) { 152 function onConnectOrBindComplete(result) {
107 chrome.test.assertEq(0, result, 153 chrome.test.assertEq(0, result,
108 "Connect or bind failed with error " + result); 154 "Connect or bind failed with error " + result);
109 if (result == 0) { 155 if (result == 0) {
110 socket.setNoDelay(socketId, true, onSetNoDelay); 156 socket.getInfo(socketId, onGetInfo);
111 } 157 }
112 } 158 }
113 159
114 function onCreate(socketInfo) { 160 function onCreate(socketInfo) {
115 socketId = socketInfo.socketId; 161 socketId = socketInfo.socketId;
116 chrome.test.assertTrue(socketId > 0, "failed to create socket"); 162 chrome.test.assertTrue(socketId > 0, "failed to create socket");
117 if (protocol == "tcp") 163 if (protocol == "tcp")
118 socket.connect(socketId, address, port, onConnectOrBindComplete); 164 socket.connect(socketId, address, port, onConnectOrBindComplete);
119 else 165 else
120 socket.bind(socketId, "0.0.0.0", 0, onConnectOrBindComplete); 166 socket.bind(socketId, "0.0.0.0", 0, onConnectOrBindComplete);
(...skipping 24 matching lines...) Expand all
145 address = parts[1]; 191 address = parts[1];
146 port = parseInt(parts[2]); 192 port = parseInt(parts[2]);
147 console.log("Running tests, protocol " + protocol + ", echo server " + 193 console.log("Running tests, protocol " + protocol + ", echo server " +
148 address + ":" + port); 194 address + ":" + port);
149 chrome.test.runTests([ testSocketCreation, testSending ]); 195 chrome.test.runTests([ testSocketCreation, testSending ]);
150 }; 196 };
151 197
152 // Find out which protocol we're supposed to test, and which echo server we 198 // Find out which protocol we're supposed to test, and which echo server we
153 // should be using, then kick off the tests. 199 // should be using, then kick off the tests.
154 chrome.test.sendMessage("info_please", onMessageReply); 200 chrome.test.sendMessage("info_please", onMessageReply);
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698