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

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

Issue 10134008: Add bind(), recvFrom(), sendTo() for UDP socket. (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: Reenable api tests Created 8 years, 8 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 54 matching lines...) Expand 10 before | Expand all | Expand 10 after
65 chrome.test.assertTrue(socketInfo.socketId > 0); 65 chrome.test.assertTrue(socketInfo.socketId > 0);
66 66
67 // TODO(miket): this doesn't work yet. It's possible this will become 67 // TODO(miket): this doesn't work yet. It's possible this will become
68 // automatic, but either way we can't forget to clean up. 68 // automatic, but either way we can't forget to clean up.
69 // 69 //
70 //socket.destroy(socketInfo.socketId); 70 //socket.destroy(socketInfo.socketId);
71 71
72 chrome.test.succeed(); 72 chrome.test.succeed();
73 } 73 }
74 74
75 socket.create(protocol, address, port, {onEvent: function(e) {}}, onCreate); 75 socket.create(protocol, {onEvent: function(e) {}}, onCreate);
76 }; 76 };
77 77
78 function onDataRead(readInfo) { 78 function onDataRead(readInfo) {
79 // TODO(miket): this isn't correct for multiple calls of onDataRead. 79 // TODO(miket): this isn't correct for multiple calls of onDataRead.
80 arrayBuffer2String(arrayOfLongsToArrayBuffer(readInfo.data), function(s) { 80 arrayBuffer2String(arrayOfLongsToArrayBuffer(readInfo.data), function(s) {
81 dataAsString = s; // save this for error reporting 81 dataAsString = s; // save this for error reporting
82 if (s.match(expectedResponsePattern)) { 82 if (s.match(expectedResponsePattern)) {
83 succeeded = true; 83 succeeded = true;
84 chrome.test.succeed(); 84 chrome.test.succeed();
85 } 85 }
86 }); 86 });
87 // Blocked. Wait for onEvent. 87 // Blocked. Wait for onEvent.
88 } 88 }
89 89
90 function onWriteComplete(writeInfo) { 90 function onWriteOrSendToComplete(writeInfo) {
91 bytesWritten += writeInfo.bytesWritten; 91 bytesWritten += writeInfo.bytesWritten;
92 if (bytesWritten == request.length) { 92 if (bytesWritten == request.length) {
93 socket.read(socketId, onDataRead); 93 if (protocol == "tcp")
94 socket.read(socketId, onDataRead);
95 else
96 socket.recvFrom(socketId, onDataRead);
94 } 97 }
95 // Blocked. Wait for onEvent. 98 // Blocked. Wait for onEvent.
96 } 99 }
97 100
98 function onConnectComplete(connectResult) { 101 function onConnectOrBindComplete(connectResult) {
99 if (connectResult == 0) { 102 if (connectResult == 0) {
100 string2ArrayBuffer(request, function(arrayBuffer) { 103 string2ArrayBuffer(request, function(arrayBuffer) {
101 var longs = arrayBufferToArrayOfLongs(arrayBuffer); 104 var longs = arrayBufferToArrayOfLongs(arrayBuffer);
102 socket.write(socketId, longs, onWriteComplete); 105 if (protocol == "tcp")
106 socket.write(socketId, longs, onWriteOrSendToComplete);
107 else
108 socket.sendTo(socketId, longs, address, port,
109 onWriteOrSendToComplete);
103 }); 110 });
104 } 111 }
105 // Blocked. Wait for onEvent. 112 // Blocked. Wait for onEvent.
106 } 113 }
107 114
108 function onCreate(socketInfo) { 115 function onCreate(socketInfo) {
109 socketId = socketInfo.socketId; 116 socketId = socketInfo.socketId;
110 chrome.test.assertTrue(socketId > 0, "failed to create socket"); 117 chrome.test.assertTrue(socketId > 0, "failed to create socket");
111 socket.connect(socketId, onConnectComplete); 118 if (protocol == "tcp")
119 socket.connect(socketId, address, port, onConnectOrBindComplete);
120 else
121 socket.bind(socketId, "0.0.0.0", 0, onConnectOrBindComplete);
112 } 122 }
113 123
114 function onEvent(socketEvent) { 124 function onEvent(socketEvent) {
125 console.log(socketEvent);
115 if (socketEvent.type == "connectComplete") { 126 if (socketEvent.type == "connectComplete") {
116 onConnectComplete(socketEvent.resultCode); 127 onConnectOrBindComplete(socketEvent.resultCode);
117 } else if (socketEvent.type == "dataRead") { 128 } else if (socketEvent.type == "dataRead") {
118 onDataRead({data: socketEvent.data}); 129 onDataRead({data: socketEvent.data});
119 } else if (socketEvent.type == "writeComplete") { 130 } else if (socketEvent.type == "writeComplete") {
120 onWriteComplete(socketEvent.resultCode); 131 onWriteOnSendToComplete(socketEvent.resultCode);
121 } else { 132 } else {
122 console.log("Received unhandled socketEvent of type " + socketEvent.type); 133 console.log("Received unhandled socketEvent of type " + socketEvent.type);
123 } 134 }
124 }; 135 };
125 136
126 function waitForBlockingOperation() { 137 function waitForBlockingOperation() {
127 if (++waitCount < 10) { 138 if (++waitCount < 10) {
128 setTimeout(waitForBlockingOperation, 1000); 139 setTimeout(waitForBlockingOperation, 1000);
129 } else { 140 } else {
130 // We weren't able to succeed in the given time. 141 // We weren't able to succeed in the given time.
131 chrome.test.fail("Operations didn't complete after " + waitCount + " " + 142 chrome.test.fail("Operations didn't complete after " + waitCount + " " +
132 "seconds. Response so far was <" + dataAsString + ">."); 143 "seconds. Response so far was <" + dataAsString + ">.");
133 } 144 }
134 } 145 }
135 146
136 var testSending = function() { 147 var testSending = function() {
137 dataRead = ""; 148 dataRead = "";
138 succeeded = false; 149 succeeded = false;
139 waitCount = 0; 150 waitCount = 0;
140 151
141 setTimeout(waitForBlockingOperation, 1000); 152 setTimeout(waitForBlockingOperation, 1000);
142 socket.create(protocol, address, port, { onEvent: onEvent }, onCreate); 153 socket.create(protocol, {onEvent: onEvent}, onCreate);
143 }; 154 };
144 155
145 var onMessageReply = function(message) { 156 var onMessageReply = function(message) {
146 var parts = message.split(":"); 157 var parts = message.split(":");
147 protocol = parts[0]; 158 protocol = parts[0];
148 address = parts[1]; 159 address = parts[1];
149 port = parseInt(parts[2]); 160 port = parseInt(parts[2]);
150 console.log("Running tests, protocol " + protocol + ", echo server " + 161 console.log("Running tests, protocol " + protocol + ", echo server " +
151 address + ":" + port); 162 address + ":" + port);
152 chrome.test.runTests([ testSocketCreation, testSending ]); 163 chrome.test.runTests([ testSocketCreation, testSending ]);
153 }; 164 };
154 165
155 // Find out which protocol we're supposed to test, and which echo server we 166 // Find out which protocol we're supposed to test, and which echo server we
156 // should be using, then kick off the tests. 167 // should be using, then kick off the tests.
157 chrome.test.sendMessage("info_please", onMessageReply); 168 chrome.test.sendMessage("info_please", onMessageReply);
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698