PageRenderTime 139ms CodeModel.GetById 21ms app.highlight 22ms RepoModel.GetById 87ms app.codeStats 0ms

/chrome-cordova/chrome-apps-api-tests/tests/manual/manual.chrome.socket.js

https://github.com/engmsaleh/mobile-chrome-apps
JavaScript | 315 lines | 257 code | 55 blank | 3 comment | 29 complexity | 18f1748f5e3da93885ce1e72e49f0558 MD5 | raw file
  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
  3// found in the LICENSE file.
  4
  5registerManualTests('chrome.socket', function(rootEl, addButton) {
  6  var addr = '127.0.0.1';
  7  var port = 1234;
  8
  9  function connectAndWrite(type, data) {
 10    chrome.socket.create(type, {}, function(socketInfo) {
 11      chrome.socket.connect(socketInfo.socketId, addr, port, function(connectResult) {
 12        var connected = (connectResult === 0);
 13        if (connected) {
 14          chrome.socket.write(socketInfo.socketId, data, function(writeResult) {
 15            logger('connectAndWrite: success');
 16            chrome.socket.disconnect(socketInfo.socketId);
 17            chrome.socket.destroy(socketInfo.socketId);
 18          });
 19        }
 20      });
 21    });
 22  }
 23
 24  function connectAndRead(type, data) {
 25    chrome.socket.create(type, {}, function(socketInfo) {
 26      chrome.socket.connect(socketInfo.socketId, addr, port, function(connectResult) {
 27        var connected = (connectResult === 0);
 28        if (connected) {
 29          chrome.socket.write(socketInfo.socketId, data, function(writeResult) {
 30            chrome.socket.read(socketInfo.socketId, function(readResult) {
 31              logger('connectAndRead: success');
 32              chrome.socket.disconnect(socketInfo.socketId);
 33              chrome.socket.destroy(socketInfo.socketId);
 34            });
 35          });
 36        }
 37      });
 38    });
 39  }
 40
 41  function acceptAndWrite() {
 42  }
 43
 44  function acceptAndRead() {
 45    chrome.socket.create('tcp', {}, function(socketInfo) {
 46      chrome.socket.listen(socketInfo.socketId, addr, port, function(listenResult) {
 47        chrome.socket.accept(socketInfo.socketId, function(acceptInfo) {
 48          chrome.socket.read(acceptInfo.socketId, function(readResult) {
 49            logger('acceptAndRead: success');
 50            chrome.socket.disconnect(acceptInfo.socketId);
 51            chrome.socket.destroy(acceptInfo.socketId);
 52            chrome.socket.disconnect(socketInfo.socketId);
 53            chrome.socket.destroy(socketInfo.socketId);
 54          });
 55        });
 56      });
 57    });
 58  }
 59
 60  function acceptConnectReadWrite(data) {
 61    chrome.socket.create('tcp', {}, function(socketInfo) {
 62      chrome.socket.listen(socketInfo.socketId, addr, port, function(listenResult) {
 63
 64        chrome.socket.accept(socketInfo.socketId, function(acceptInfo) {
 65          chrome.socket.read(acceptInfo.socketId, function(readResult) {
 66            var sent = new Uint8Array(data);
 67            var recv = new Uint8Array(readResult.data);
 68
 69            chrome.socket.disconnect(acceptInfo.socketId);
 70            chrome.socket.destroy(acceptInfo.socketId);
 71            chrome.socket.disconnect(socketInfo.socketId);
 72            chrome.socket.destroy(socketInfo.socketId);
 73
 74            if (recv.length != sent.length) {
 75              return;
 76            }
 77
 78            for (var i = 0; i < recv.length; i++) {
 79              if (recv[i] != sent[i]) {
 80                return;
 81              }
 82            }
 83
 84            logger('acceptConnectReadWrite: success');
 85          });
 86        });
 87
 88        chrome.socket.create('tcp', {}, function(socketInfo) {
 89          chrome.socket.connect(socketInfo.socketId, addr, port, function(connectResult) {
 90            var connected = (connectResult === 0);
 91            if (connected) {
 92              chrome.socket.write(socketInfo.socketId, data, function(writeResult) {
 93                chrome.socket.disconnect(socketInfo.socketId);
 94                chrome.socket.destroy(socketInfo.socketId);
 95              });
 96            }
 97          });
 98        });
 99      });
100    });
101  }
102
103  function acceptConnectReadWriteGetInfo(data) {
104    chrome.socket.create('tcp', {}, function(socketInfo) {
105      chrome.socket.listen(socketInfo.socketId, addr, port, function(listenResult) {
106
107        chrome.socket.accept(socketInfo.socketId, function(acceptInfo) {
108          chrome.socket.read(acceptInfo.socketId, function(readResult) {
109            var sent = new Uint8Array(data);
110            var recv = new Uint8Array(readResult.data);
111
112            chrome.socket.disconnect(acceptInfo.socketId);
113            chrome.socket.destroy(acceptInfo.socketId);
114            chrome.socket.disconnect(socketInfo.socketId);
115            chrome.socket.destroy(socketInfo.socketId);
116
117            if (recv.length != sent.length) {
118              return;
119            }
120
121            for (var i = 0; i < recv.length; i++) {
122              if (recv[i] != sent[i]) {
123                return;
124              }
125            }
126
127            logger('acceptConnectReadWrite: success');
128          });
129        });
130
131        chrome.socket.create('tcp', {}, function(socketInfo) {
132          chrome.socket.connect(socketInfo.socketId, addr, port, function(connectResult) {
133            var connected = (connectResult === 0);
134            if (connected) {
135              chrome.socket.getInfo(socketInfo.socketId, function(info) {
136                if (info) {
137                  logger(info.socketType + ': connected(' + info.connected + ') ' + (info.connected ? info.peerAddress + ':' + info.peerPort + ' -> ' + info.localAddress + ':' + info.localPort : ''));
138                }
139
140                chrome.socket.write(socketInfo.socketId, data, function(writeResult) {
141                  chrome.socket.disconnect(socketInfo.socketId);
142                  chrome.socket.destroy(socketInfo.socketId);
143                });
144              });
145            }
146          });
147        });
148      });
149    });
150  }
151
152  function sendTo(data) {
153    chrome.socket.create('udp', {}, function(socketInfo) {
154      chrome.socket.sendTo(socketInfo.socketId, data, addr, port, function(result) {
155        logger('sendTo: success');
156        chrome.socket.destroy(socketInfo.socketId);
157      });
158    });
159  }
160
161  function bindAndRecvFrom(data) {
162    chrome.socket.create('udp', {}, function(socketInfo) {
163      chrome.socket.bind(socketInfo.socketId, addr, port, function(result) {
164        chrome.socket.recvFrom(socketInfo.socketId, function(readResult) {
165          logger('recvFrom: success');
166          chrome.socket.destroy(socketInfo.socketId);
167        });
168      });
169    });
170  }
171
172  function bindRecvFromSendTo(data) {
173    chrome.socket.create('udp', {}, function(socketInfo) {
174      chrome.socket.bind(socketInfo.socketId, addr, port, function(result) {
175        chrome.socket.recvFrom(socketInfo.socketId, function(readResult) {
176          var sent = new Uint8Array(data);
177          var recv = new Uint8Array(readResult.data);
178
179          if (recv.length != sent.length) {
180            return;
181          }
182
183          for (var i = 0; i < recv.length; i++) {
184            if (recv[i] != sent[i]) {
185              return;
186            }
187          }
188
189          logger('bindRecvFromSendTo: success');
190          chrome.socket.destroy(socketInfo.socketId);
191        });
192
193        chrome.socket.create('udp', {}, function(socketInfo) {
194          chrome.socket.sendTo(socketInfo.socketId, data, addr, port, function(result) {
195            chrome.socket.destroy(socketInfo.socketId);
196          });
197        });
198      });
199    });
200  }
201
202  function bindConnectReadWrite(data) {
203    chrome.socket.create('udp', {}, function(socketInfo1) {
204      chrome.socket.bind(socketInfo1.socketId, addr, port, function(result) {
205
206        chrome.socket.create('udp', {}, function(socketInfo2) {
207          chrome.socket.bind(socketInfo2.socketId, addr, port+1, function(result) {
208
209            chrome.socket.connect(socketInfo1.socketId, addr, port+1, function(result) {
210              chrome.socket.connect(socketInfo2.socketId, addr, port, function(result) {
211
212                chrome.socket.read(socketInfo1.socketId, function(readResult) {
213                  var sent = new Uint8Array(data);
214                  var recv = new Uint8Array(readResult.data);
215
216                  if (recv.length != sent.length) {
217                    return;
218                  }
219
220                  for (var i = 0; i < recv.length; i++) {
221                    if (recv[i] != sent[i]) {
222                      return;
223                    }
224                  }
225
226                  logger('bindConnectReadWrite: success');
227                  chrome.socket.destroy(socketInfo1.socketId);
228                });
229
230                chrome.socket.write(socketInfo2.socketId, data, function(result) {
231                  chrome.socket.destroy(socketInfo2.socketId);
232                });
233
234              });
235            });
236
237          });
238        });
239
240      });
241    });
242  }
243
244  function getNetworkList() {
245    chrome.socket.getNetworkList(function(info) {
246      if (!info) return;
247      for (var i = 0; i < info.length; i++) {
248        logger(info[i].name + ': ' + info[i].address);
249      }
250    });
251  }
252
253  function initPage() {
254    logger('Run this in terminal:');
255    logger('while true; do');
256    logger('  (nc -lv 1234 | xxd) || break; echo;');
257    logger('done');
258
259    var arr = new Uint8Array(256);
260    for (var i = 0; i < arr.length; i++) {
261      arr[i] = i;
262    }
263
264    addButton('TCP: connect & write', function() {
265      connectAndWrite('tcp', arr.buffer);
266    });
267
268    addButton('TCP: connect & read', function() {
269      connectAndRead('tcp', arr.buffer);
270    });
271
272    addButton('TCP: accept & read', function() {
273      acceptAndRead();
274    });
275
276    addButton('TCP: all-in-one', function() {
277      acceptConnectReadWrite(arr.buffer);
278    });
279
280    addButton('TCP: all-in-one with getInfo', function() {
281      acceptConnectReadWriteGetInfo(arr.buffer);
282    });
283
284
285    addButton('UDP: connect & write', function() {
286      connectAndWrite('udp', arr.buffer);
287    });
288
289    addButton('UDP: connect & read', function() {
290      connectAndRead('udp', arr.buffer);
291    });
292
293    addButton('UDP: sendTo', function() {
294      sendTo(arr.buffer);
295    });
296
297    addButton('UDP: bind & recvFrom', function() {
298      bindAndRecvFrom(arr.buffer);
299    });
300
301    addButton('UDP: all-in-one connectionless', function() {
302      bindRecvFromSendTo(arr.buffer);
303    });
304
305    addButton('UDP: all-in-one w/ connections', function() {
306      bindConnectReadWrite(arr.buffer);
307    });
308
309    addButton('getNetworkList', function() {
310      getNetworkList();
311    });
312  }
313
314  initPage();
315});