PageRenderTime 60ms CodeModel.GetById 2ms app.highlight 53ms RepoModel.GetById 1ms app.codeStats 0ms

/chrome-cordova/chrome-apps-api-tests/tests/auto/test.chrome.socket.js

https://github.com/engmsaleh/mobile-chrome-apps
JavaScript | 601 lines | 496 code | 100 blank | 5 comment | 24 complexity | f4cefc5b023711d7262b85b60cdbc614 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
  5registerAutoTests('chrome.socket', function() {
  6  'use strict';
  7  // constants
  8  var bindAddr = '0.0.0.0';
  9  var connectAddr = '127.0.0.1';
 10  var multicastAddr = '224.0.1.' + Math.floor(Math.random()*256); // 224.0.1.0 to 239.255.255.255
 11  var multicastAddr2 = '224.0.2.' + Math.floor(Math.random()*256);
 12  var port = Math.floor(Math.random() * (65535-1024)) + 1024; // random in 1024 -> 65535
 13  var arr = new Uint8Array(256);
 14  for (var i = 0; i < arr.length; i++) {
 15    arr[i] = i;
 16  }
 17  var data = arr.buffer;
 18
 19  // Socket management -- Make sure we clean up sockets after each test, even upon failure
 20  var sockets = [];
 21  afterEach(function() {
 22    sockets.forEach(function(createInfo) {
 23      chrome.socket.disconnect(createInfo.socketId);
 24      chrome.socket.destroy(createInfo.socketId);
 25    });
 26    sockets = [];
 27  });
 28
 29  function createSocket(type, callback) {
 30    chrome.socket.create(type, function(createInfo) {
 31      expect(createInfo).toBeTruthy();
 32      expect(createInfo.socketId).toBeDefined();
 33      sockets.push(createInfo);
 34      callback();
 35    });
 36  }
 37  function createSockets(type, count, callback) {
 38    if (!count)
 39      return setTimeout(callback, 0);
 40    createSocket(type, createSockets.bind(null, type, count-1, callback));
 41  }
 42
 43  it('should contain definitions', function() {
 44    expect(chrome.socket).toBeDefined();
 45    expect(chrome.socket.destroy).toBeDefined();
 46    expect(chrome.socket.connect).toBeDefined();
 47    expect(chrome.socket.bind).toBeDefined();
 48    expect(chrome.socket.disconnect).toBeDefined();
 49    expect(chrome.socket.read).toBeDefined();
 50    expect(chrome.socket.write).toBeDefined();
 51    expect(chrome.socket.recvFrom).toBeDefined();
 52    expect(chrome.socket.sendTo).toBeDefined();
 53    expect(chrome.socket.listen).toBeDefined();
 54    expect(chrome.socket.accept).toBeDefined();
 55    expect(chrome.socket.setKeepAlive).toBeDefined();
 56    expect(chrome.socket.setNoDelay).toBeDefined();
 57    expect(chrome.socket.getInfo).toBeDefined();
 58    expect(chrome.socket.getNetworkList).toBeDefined();
 59    expect(chrome.socket.joinGroup).toBeDefined();
 60    expect(chrome.socket.leaveGroup).toBeDefined();
 61    expect(chrome.socket.setMulticastTimeToLive).toBeDefined();
 62    expect(chrome.socket.setMulticastLoopbackMode).toBeDefined();
 63    expect(chrome.socket.getJoinedGroups).toBeDefined();
 64  });
 65
 66  describe('System', function() {
 67    it('getNetworkList', function(done) {
 68      chrome.socket.getNetworkList(function(result) {
 69        expect(result).toBeTruthy();
 70        expect(result.length).toBeGreaterThan(0);
 71        result.forEach(function(networkInterface) {
 72          expect(networkInterface.name).toBeTruthy();
 73          expect(networkInterface.address).toBeTruthy();
 74        });
 75        done();
 76      });
 77    });
 78  });
 79
 80  describe('TCP', function() {
 81    beforeEach(function() {
 82      var customMatchers = {
 83        toBeValidTcpReadResultEqualTo: function(util, customEqualityTesters) {
 84          return {
 85            compare: function(actual, expected) {
 86              if (Object.prototype.toString.call(expected).slice(8, -1) !== "ArrayBuffer")
 87                throw new Error("toBeValidTcpReadResultEqualTo expects an ArrayBuffer");
 88              var result = { pass: true };
 89
 90              if (!actual) result.pass = false;
 91              if (actual.resultCode <= 0) result.pass = false;
 92              if (actual.resultCode != expected.byteLength) result.pass = false;
 93              if (!actual.data) result.pass = false;
 94              if (Object.prototype.toString.call(actual.data).slice(8, -1) !== "ArrayBuffer") result.pass = false;
 95
 96              var sent = new Uint8Array(expected);
 97              var recv = new Uint8Array(actual.data);
 98              if (recv.length !== sent.length) result.pass = false;
 99
100              for (var i = 0; i < recv.length; i++) {
101                if (recv[i] !== sent[i]) result.pass = false;
102              }
103              return result;
104            }
105          };
106        }
107      };
108
109      addMatchers(customMatchers);
110    });
111
112    beforeEach(function(done) {
113      createSockets('tcp', 2, done);
114    });
115
116
117    it('port is available (sanity test)', function(done) {
118      chrome.socket.listen(sockets[0].socketId, bindAddr, port, function(listenResult) {
119        expect(listenResult).toEqual(0);
120        done();
121      });
122    });
123
124    it('accept connect read write', function(done) {
125      chrome.socket.listen(sockets[0].socketId, bindAddr, port, function(listenResult) {
126        expect(listenResult).toEqual(0);
127
128        chrome.socket.accept(sockets[0].socketId, function(acceptInfo) {
129          sockets.push(acceptInfo);
130          expect(acceptInfo).toBeTruthy();
131          expect(acceptInfo.resultCode).toEqual(0);
132          expect(acceptInfo.socketId).toBeDefined();
133
134          chrome.socket.read(acceptInfo.socketId, function(readResult) {
135            expect(readResult).toBeValidTcpReadResultEqualTo(data);
136            done();
137          });
138        });
139
140        chrome.socket.connect(sockets[1].socketId, connectAddr, port, function(connectResult) {
141          expect(connectResult).toEqual(0);
142
143          chrome.socket.write(sockets[1].socketId, data, function(writeResult) {
144            expect(writeResult.bytesWritten).toBeGreaterThan(0);
145          });
146        });
147      });
148    });
149
150    it('read with larger bufferSize', function(done) {
151      chrome.socket.listen(sockets[0].socketId, bindAddr, port, function(listenResult) {
152        expect(listenResult).toEqual(0);
153
154        chrome.socket.accept(sockets[0].socketId, function(acceptInfo) {
155          sockets.push(acceptInfo);
156          expect(acceptInfo).toBeTruthy();
157          expect(acceptInfo.resultCode).toEqual(0);
158          expect(acceptInfo.socketId).toBeDefined();
159
160          chrome.socket.read(acceptInfo.socketId, data.byteLength * 2, function(readResult) {
161            expect(readResult).toBeValidTcpReadResultEqualTo(data);
162            done();
163          });
164        });
165
166        chrome.socket.connect(sockets[1].socketId, connectAddr, port, function(connectResult) {
167          expect(connectResult).toEqual(0);
168
169          chrome.socket.write(sockets[1].socketId, data, function(writeResult) {
170            expect(writeResult.bytesWritten).toBeGreaterThan(0);
171          });
172        });
173      });
174    });
175
176    it('read with null bufferSize', function(done) {
177      chrome.socket.listen(sockets[0].socketId, bindAddr, port, function(listenResult) {
178        expect(listenResult).toEqual(0);
179
180        chrome.socket.accept(sockets[0].socketId, function(acceptInfo) {
181          sockets.push(acceptInfo);
182          expect(acceptInfo).toBeTruthy();
183          expect(acceptInfo.resultCode).toEqual(0);
184          expect(acceptInfo.socketId).toBeDefined();
185
186          chrome.socket.read(acceptInfo.socketId, null, function(readResult) {
187            expect(readResult).toBeValidTcpReadResultEqualTo(data);
188            done();
189          });
190        });
191
192        chrome.socket.connect(sockets[1].socketId, connectAddr, port, function(connectResult) {
193          expect(connectResult).toEqual(0);
194
195          chrome.socket.write(sockets[1].socketId, data, function(writeResult) {
196            expect(writeResult.bytesWritten).toBeGreaterThan(0);
197          });
198        });
199      });
200    });
201
202    it('connect before accept', function(done) {
203      chrome.socket.listen(sockets[0].socketId, bindAddr, port, function(listenResult) {
204        expect(listenResult).toEqual(0);
205
206        chrome.socket.connect(sockets[1].socketId, connectAddr, port, function(connectResult) {
207          expect(connectResult).toEqual(0);
208        });
209
210        setTimeout(function() {
211          chrome.socket.accept(sockets[0].socketId, function(acceptInfo) {
212            sockets.push(acceptInfo);
213            expect(acceptInfo).toBeTruthy();
214            expect(acceptInfo.resultCode).toEqual(0);
215            expect(acceptInfo.socketId).toBeDefined();
216
217            done();
218          });
219        }, 50);
220      });
221    });
222
223    it('getInfo works', function(done) {
224      chrome.socket.getInfo(sockets[0].socketId, function(socketInfo) {
225        expect(socketInfo.socketType).toEqual('tcp');
226        expect(socketInfo.connected).toBeFalsy();
227        expect(socketInfo.localAddress).toBeFalsy();
228        expect(socketInfo.localPort).toBeFalsy();
229        expect(socketInfo.peerAddress).toBeFalsy();
230        expect(socketInfo.peerPort).toBeFalsy();
231
232        chrome.socket.listen(sockets[0].socketId, bindAddr, port, function(listenResult) {
233          expect(listenResult).toEqual(0);
234
235          chrome.socket.getInfo(sockets[0].socketId, function(socketInfo) {
236            expect(socketInfo.socketType).toEqual('tcp');
237            expect(socketInfo.connected).toBeFalsy();
238            expect(socketInfo.localAddress).toBeTruthy();
239            expect(socketInfo.localPort).toEqual(port);
240            expect(socketInfo.peerAddress).toBeFalsy();
241            expect(socketInfo.peerPort).toBeFalsy();
242
243            chrome.socket.accept(sockets[0].socketId, function(acceptInfo) {
244              sockets.push(acceptInfo);
245              expect(acceptInfo).toBeTruthy();
246              expect(acceptInfo.resultCode).toEqual(0);
247              expect(acceptInfo.socketId).toBeDefined();
248
249              chrome.socket.getInfo(acceptInfo.socketId, function(socketInfo) {
250                expect(socketInfo.socketType).toEqual('tcp');
251                expect(socketInfo.connected).toBeTruthy();
252                expect(socketInfo.localAddress).toBeTruthy();
253                expect(socketInfo.localPort).toBeGreaterThan(0);
254                expect(socketInfo.peerAddress).toBeTruthy();
255                expect(socketInfo.peerPort).toBeGreaterThan(0);
256
257                chrome.socket.read(acceptInfo.socketId, function(readResult) {
258                  done();
259                });
260              });
261            });
262
263            chrome.socket.connect(sockets[1].socketId, connectAddr, port, function(connectResult) {
264              expect(connectResult).toEqual(0);
265
266              chrome.socket.getInfo(sockets[1].socketId, function(socketInfo) {
267                expect(socketInfo.socketType).toEqual('tcp');
268                expect(socketInfo.connected).toBeTruthy();
269                expect(socketInfo.localAddress).toBeTruthy();
270                expect(socketInfo.localPort).toBeGreaterThan(0);
271                expect(socketInfo.peerAddress).toBeTruthy();
272                expect(socketInfo.peerPort).toBeGreaterThan(0);
273
274                chrome.socket.write(sockets[1].socketId, data, function(writeResult) {
275                });
276              });
277            });
278          });
279        });
280      });
281    });
282
283  });
284
285  describe('UDP', function() {
286
287    beforeEach(function() {
288      var customMatchers = {
289        toBeValidUdpReadResultEqualTo: function(util, customEqualityTesters) {
290          return {
291            compare: function(actual, expected) {
292              if (Object.prototype.toString.call(expected).slice(8, -1) !== "ArrayBuffer")
293                throw new Error("toBeValidUdpReadResultEqualTo expects an ArrayBuffer");
294              var result = { pass: true };
295              if (!actual) result.pass = false;
296              if (actual.resultCode <= 0) result.pass = false;
297              if (actual.resultCode != expected.byteLength) result.pass = false;
298              if (!actual.data) result.pass = false;
299              if (Object.prototype.toString.call(actual.data).slice(8, -1) !== "ArrayBuffer") result.pass = false;
300
301              var sent = new Uint8Array(expected);
302              var recv = new Uint8Array(actual.data);
303              if (recv.length !== sent.length) result.pass = false;
304
305              for (var i = 0; i < recv.length; i++) {
306                if (recv[i] !== sent[i]) result.pass = false;
307              }
308              return result;
309            }
310          };
311        },
312        toBeValidUdpRecvFromResultEqualTo: function(util, customEqualityTesters) {
313          return {
314            compare: function(actual, expected) {
315              var result = customMatchers.toBeValidUdpReadResultEqualTo(util, customEqualityTesters).compare(actual, expected);
316              if (!actual.address) result.pass = false;
317              if (!actual.port) result.pass = false;
318              return result;
319            }
320          };
321        }
322      };
323
324      addMatchers(customMatchers);
325    });
326
327    beforeEach(function(done) {
328      createSockets('udp', 2, done);
329    });
330
331
332    it('port is available (sanity test)', function(done) {
333      chrome.socket.bind(sockets[0].socketId, bindAddr, port, function(bindResult) {
334        expect(bindResult).toEqual(0);
335        done();
336      });
337    });
338
339    it('bind to port 0 works', function(done) {
340      chrome.socket.bind(sockets[0].socketId, bindAddr, 0, function(bindResult) {
341        expect(bindResult).toEqual(0);
342        done();
343      });
344    });
345
346    it('bind to addr 0.0.0.0 works', function(done) {
347      chrome.socket.bind(sockets[0].socketId, '0.0.0.0', 0, function(bindResult) {
348        expect(bindResult).toEqual(0);
349        done();
350      });
351    });
352
353    it('getInfo works', function(done) {
354      chrome.socket.bind(sockets[0].socketId, bindAddr, port, function(bindResult) {
355        expect(bindResult).toEqual(0);
356
357        chrome.socket.getInfo(sockets[0].socketId, function(socketInfo) {
358          expect(socketInfo.socketType).toEqual('udp');
359          expect(socketInfo.connected).toBeFalsy();
360          expect(socketInfo.peerAddress).toBeFalsy();
361          expect(socketInfo.peerPort).toBeFalsy();
362          expect(socketInfo.localAddress).toBeTruthy();
363          expect(socketInfo.localPort).toBeTruthy();
364
365          done();
366        });
367      });
368    });
369
370    it('bind recvFrom sendTo with reply', function(done) {
371      chrome.socket.bind(sockets[0].socketId, bindAddr, port, function(bindResult) {
372        expect(bindResult).toEqual(0);
373
374        chrome.socket.recvFrom(sockets[0].socketId, function(readResult) {
375          expect(readResult).toBeValidUdpRecvFromResultEqualTo(data);
376
377          chrome.socket.sendTo(sockets[0].socketId, data, readResult.address, readResult.port, function(writeResult) {
378            expect(writeResult).toBeTruthy();
379            expect(writeResult.bytesWritten).toBeGreaterThan(0);
380          });
381        });
382
383        chrome.socket.bind(sockets[1].socketId, bindAddr, port+1, function(bindResult) {
384          expect(bindResult).toEqual(0);
385
386          chrome.socket.sendTo(sockets[1].socketId, data, connectAddr, port, function(writeResult) {
387            expect(writeResult).toBeTruthy();
388            expect(writeResult.bytesWritten).toBeGreaterThan(0);
389
390            chrome.socket.recvFrom(sockets[1].socketId, function(readResult) {
391              done();
392            });
393          });
394        });
395      });
396    });
397
398    it('recvFrom with larger bufferSize', function(done) {
399      chrome.socket.bind(sockets[0].socketId, bindAddr, port, function(bindResult) {
400        expect(bindResult).toEqual(0);
401
402        chrome.socket.recvFrom(sockets[0].socketId, data.byteLength * 2, function(readResult) {
403          expect(readResult).toBeValidUdpRecvFromResultEqualTo(data);
404
405          chrome.socket.sendTo(sockets[0].socketId, data, readResult.address, readResult.port, function(writeResult) {
406            expect(writeResult).toBeTruthy();
407            expect(writeResult.bytesWritten).toBeGreaterThan(0);
408          });
409        });
410
411        chrome.socket.bind(sockets[1].socketId, bindAddr, port+1, function(bindResult) {
412          expect(bindResult).toEqual(0);
413
414          chrome.socket.sendTo(sockets[1].socketId, data, connectAddr, port, function(writeResult) {
415            expect(writeResult).toBeTruthy();
416            expect(writeResult.bytesWritten).toBeGreaterThan(0);
417
418            chrome.socket.recvFrom(sockets[1].socketId, function(readResult) {
419              done();
420            });
421          });
422        });
423      });
424    });
425
426    it('recvFrom with null bufferSize', function(done) {
427      chrome.socket.bind(sockets[0].socketId, bindAddr, port, function(bindResult) {
428        expect(bindResult).toEqual(0);
429
430        chrome.socket.recvFrom(sockets[0].socketId, null, function(readResult) {
431          expect(readResult).toBeValidUdpRecvFromResultEqualTo(data);
432
433          chrome.socket.sendTo(sockets[0].socketId, data, readResult.address, readResult.port, function(writeResult) {
434            expect(writeResult).toBeTruthy();
435            expect(writeResult.bytesWritten).toBeGreaterThan(0);
436          });
437        });
438
439        chrome.socket.bind(sockets[1].socketId, bindAddr, port+1, function(bindResult) {
440          expect(bindResult).toEqual(0);
441
442          chrome.socket.sendTo(sockets[1].socketId, data, connectAddr, port, function(writeResult) {
443            expect(writeResult).toBeTruthy();
444            expect(writeResult.bytesWritten).toBeGreaterThan(0);
445
446            chrome.socket.recvFrom(sockets[1].socketId, function(readResult) {
447              done();
448            });
449          });
450        });
451      });
452    });
453
454
455    describeCordovaOnly('fail on desktop', function() {
456
457    it('bind connect x2 read write', function(done) {
458      chrome.socket.bind(sockets[0].socketId, bindAddr, port, function(bindResult1) {
459        expect(bindResult1).toEqual(0);
460        chrome.socket.bind(sockets[1].socketId, bindAddr, port+1, function(bindResult2) {
461          expect(bindResult2).toEqual(0);
462
463          chrome.socket.getInfo(sockets[0].socketId, function(info1) {
464            chrome.socket.getInfo(sockets[1].socketId, function(info2) {
465              chrome.socket.connect(sockets[0].socketId, connectAddr, info2.localPort, function(connectResult1) {
466                expect(connectResult1).toEqual(0);
467
468                chrome.socket.connect(sockets[1].socketId, connectAddr, info1.localPort, function(connectResult2) {
469                  expect(connectResult2).toEqual(0);
470
471                  chrome.socket.getInfo(sockets[0].socketId, function(info1) {
472                    chrome.socket.getInfo(sockets[1].socketId, function(info2) {
473
474                      chrome.socket.read(sockets[0].socketId, function(readResult) {
475                        expect(readResult).toBeValidUdpReadResultEqualTo(data);
476                        done();
477                      });
478
479                      chrome.socket.write(sockets[1].socketId, data, function(writeResult) {
480                        expect(writeResult).toBeTruthy();
481                        expect(writeResult.bytesWritten).toBe(data.byteLength);
482                      });
483                    });
484                  });
485                });
486              });
487            });
488          });
489        });
490      });
491    });
492
493    it('read with null bufferSize', function(done) {
494      chrome.socket.bind(sockets[0].socketId, bindAddr, port, function(bindResult1) {
495        expect(bindResult1).toEqual(0);
496        chrome.socket.bind(sockets[1].socketId, bindAddr, port+1, function(bindResult2) {
497          expect(bindResult2).toEqual(0);
498
499          chrome.socket.getInfo(sockets[0].socketId, function(info1) {
500            chrome.socket.getInfo(sockets[1].socketId, function(info2) {
501              chrome.socket.connect(sockets[0].socketId, connectAddr, info2.localPort, function(connectResult1) {
502                expect(connectResult1).toEqual(0);
503
504                chrome.socket.connect(sockets[1].socketId, connectAddr, info1.localPort, function(connectResult2) {
505                  expect(connectResult2).toEqual(0);
506
507                  chrome.socket.getInfo(sockets[0].socketId, function(info1) {
508                    chrome.socket.getInfo(sockets[1].socketId, function(info2) {
509
510                      chrome.socket.read(sockets[0].socketId, null, function(readResult) {
511                        expect(readResult).toBeValidUdpReadResultEqualTo(data);
512                        done();
513                      });
514
515                      chrome.socket.write(sockets[1].socketId, data, function(writeResult) {
516                        expect(writeResult).toBeTruthy();
517                        expect(writeResult.bytesWritten).toBe(data.byteLength);
518                      });
519                    });
520                  });
521                });
522              });
523            });
524          });
525        });
526      });
527    });
528
529    });
530
531
532    it('multiCast joinGroup single local client', function(done) {
533      chrome.socket.bind(sockets[0].socketId, bindAddr, port, function(bindResult1) {
534        expect(bindResult1).toEqual(0);
535        chrome.socket.joinGroup(sockets[0].socketId, multicastAddr, function(joinResult1) {
536          expect(joinResult1).toEqual(0);
537          chrome.socket.recvFrom(sockets[0].socketId, function(readResult) {
538            expect(readResult).toBeValidUdpRecvFromResultEqualTo(data);
539            done();
540          });
541
542          chrome.socket.bind(sockets[1].socketId, bindAddr, 0, function(bindResult2) {
543            expect(bindResult2).toEqual(0);
544            chrome.socket.sendTo(sockets[1].socketId, data, multicastAddr, port, function(writeResult) {
545              expect(writeResult).toBeTruthy();
546              expect(writeResult.bytesWritten).toBeGreaterThan(0);
547            });
548
549          });
550        });
551
552      });
553    });
554
555    it('multiCast leaveGroup', function(done) {
556      chrome.socket.bind(sockets[0].socketId, bindAddr, port, function(bindResult1) {
557        expect(bindResult1).toEqual(0);
558        chrome.socket.joinGroup(sockets[0].socketId, multicastAddr, function(joinResult1) {
559          expect(joinResult1).toEqual(0);
560          chrome.socket.leaveGroup(sockets[0].socketId, multicastAddr, function(leaveResult1) {
561            expect(leaveResult1).toEqual(0);
562            chrome.socket.recvFrom(sockets[0].socketId, function(readResult) {
563              expect(readResult).toBeNull();
564            });
565            setTimeout(done, 100);
566
567            chrome.socket.bind(sockets[1].socketId, bindAddr, 0, function(bindResult2) {
568              expect(bindResult2).toEqual(0);
569              chrome.socket.sendTo(sockets[1].socketId, data, multicastAddr, port, function(writeResult) {
570                expect(writeResult).toBeTruthy();
571                expect(writeResult.bytesWritten).toBeGreaterThan(0);
572              });
573
574            });
575          });
576        });
577
578      });
579    });
580
581    it('multiCast getJoinedGroups', function(done) {
582      chrome.socket.bind(sockets[0].socketId, bindAddr, port, function(bindResult1) {
583        expect(bindResult1).toEqual(0);
584        chrome.socket.joinGroup(sockets[0].socketId, multicastAddr, function(joinResult1) {
585          expect(joinResult1).toEqual(0);
586          chrome.socket.joinGroup(sockets[0].socketId, multicastAddr2, function(joinResult2) {
587            expect(joinResult2).toEqual(0);
588            chrome.socket.getJoinedGroups(sockets[0].socketId, function(joinedGroups) {
589              expect(joinedGroups.length).toBe(2);
590              expect(joinedGroups).toContain(multicastAddr);
591              expect(joinedGroups).toContain(multicastAddr2);
592              done();
593            });
594          });
595        });
596
597      });
598    });
599
600  });
601});