1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
|
/* Any copyright is dedicated to the Public Domain.
* http://creativecommons.org/publicdomain/zero/1.0/ */
let Promise = SpecialPowers.Cu.import("resource://gre/modules/Promise.jsm").Promise;
const ETHERNET_MANAGER_CONTRACT_ID = "@mozilla.org/ethernetManager;1";
const INTERFACE_UP = "UP";
const INTERFACE_DOWN = "DOWN";
let gTestSuite = (function() {
let suite = {};
// Private member variables of the returned object |suite|.
let ethernetManager = SpecialPowers.Cc[ETHERNET_MANAGER_CONTRACT_ID]
.getService(SpecialPowers.Ci.nsIEthernetManager);
let pendingEmulatorShellCount = 0;
/**
* Send emulator shell command with safe guard.
*
* We should only call |finish()| after all emulator command transactions
* end, so here comes with the pending counter. Resolve when the emulator
* gives positive response, and reject otherwise.
*
* Fulfill params: an array of emulator response lines.
* Reject params: an array of emulator response lines.
*
* @param command
* A string command to be passed to emulator through its telnet console.
*
* @return A deferred promise.
*/
function runEmulatorShellSafe(command) {
let deferred = Promise.defer();
++pendingEmulatorShellCount;
runEmulatorShell(command, function(aResult) {
--pendingEmulatorShellCount;
ok(true, "Emulator shell response: " + JSON.stringify(aResult));
if (Array.isArray(aResult)) {
deferred.resolve(aResult);
} else {
deferred.reject(aResult);
}
});
return deferred.promise;
}
/**
* Get the system network conifg by the given interface name.
*
* Use shell command 'netcfg' to get the list of network cofig.
*
* Fulfill params: An object of { name, flag, ip }
*
* @parm ifname
* Interface name.
*
* @return A deferred promise.
*/
function getNetworkConfig(ifname) {
return runEmulatorShellSafe(['netcfg'])
.then(result => {
// Sample 'netcfg' output:
//
// lo UP 127.0.0.1/8 0x00000049 00:00:00:00:00:00
// eth0 UP 10.0.2.15/24 0x00001043 52:54:00:12:34:56
// eth1 DOWN 0.0.0.0/0 0x00001002 52:54:00:12:34:57
// rmnet1 DOWN 0.0.0.0/0 0x00001002 52:54:00:12:34:59
let config;
for (let i = 0; i < result.length; i++) {
let tokens = result[i].split(/\s+/);
let name = tokens[0];
let flag = tokens[1];
let ip = tokens[2].split(/\/+/)[0];
if (name == ifname) {
config = { name: name, flag: flag, ip: ip };
break;
}
}
return config;
});
}
/**
* Get the ip assigned by dhcp server of a given interface name.
*
* Get the ip from android property 'dhcp.[ifname].ipaddress'.
*
* Fulfill params: A string of ip address.
*
* @parm ifname
* Interface name.
*
* @return A deferred promise.
*/
function getDhcpIpAddr(ifname) {
return runEmulatorShellSafe(['getprop', 'dhcp.' + ifname + '.ipaddress'])
.then(function(ipAddr) {
return ipAddr[0];
});
}
/**
* Get the gateway assigned by dhcp server of a given interface name.
*
* Get the ip from android property 'dhcp.[ifname].gateway'.
*
* Fulfill params: A string of gateway.
*
* @parm ifname
* Interface name.
*
* @return A deferred promise.
*/
function getDhcpGateway(ifname) {
return runEmulatorShellSafe(['getprop', 'dhcp.' + ifname + '.gateway'])
.then(function(gateway) {
return gateway[0];
});
}
/**
* Get the default route.
*
* Use shell command 'ip route' to get the default of device.
*
* Fulfill params: An array of { name, gateway }
*
* @return A deferred promise.
*/
function getDefaultRoute() {
return runEmulatorShellSafe(['ip', 'route'])
.then(result => {
// Sample 'ip route' output:
//
// 10.0.2.0/24 dev eth0 proto kernel scope link src 10.0.2.15
// default via 10.0.2.2 dev eth0 metric 2
let routeInfo = [];
for (let i = 0; i < result.length; i++) {
if (!result[i].match('default')) {
continue;
}
let tokens = result[i].split(/\s+/);
let name = tokens[4];
let gateway = tokens[2];
routeInfo.push({ name: name, gateway: gateway });
}
return routeInfo;
});
}
/**
* Check a specific interface is enabled or not.
*
* @parm ifname
* Interface name.
* @parm enabled
* A boolean value used to check interface is disable or not.
*
* @return A deferred promise.
*/
function checkInterfaceIsEnabled(ifname, enabled) {
return getNetworkConfig(ifname)
.then(function(config) {
if (enabled) {
is(config.flag, INTERFACE_UP, "Interface is enabled as expected.");
} else {
is(config.flag, INTERFACE_DOWN, "Interface is disabled as expected.");
}
});
}
/**
* Check the ip of a specific interface is equal to given ip or not.
*
* @parm ifname
* Interface name.
* @parm ip
* Given ip address.
*
* @return A deferred promise.
*/
function checkInterfaceIpAddr(ifname, ip) {
return getNetworkConfig(ifname)
.then(function(config) {
is(config.ip, ip, "IP is right as expected.");
});
}
/**
* Check the default gateway of a specific interface is equal to given gateway
* or not.
*
* @parm ifname
* Interface name.
* @parm gateway
* Given gateway.
*
* @return A deferred promise.
*/
function checkDefaultRoute(ifname, gateway) {
return getDefaultRoute()
.then(function(routeInfo) {
for (let i = 0; i < routeInfo.length; i++) {
if (routeInfo[i].name == ifname) {
is(routeInfo[i].gateway, gateway,
"Default gateway is right as expected.");
return true;
}
}
if (!gateway) {
ok(true, "Default route is cleared.");
return true;
}
// TODO: should we ok(false, ......) here?
return false;
});
}
/**
* Check the length of interface list in EthernetManager is equal to given
* length or not.
*
* @parm length
* Given length.
*/
function checkInterfaceListLength(length) {
let list = ethernetManager.interfaceList;
is(length, list.length, "List length is equal as expected.");
}
/**
* Check the given interface exists on device or not.
*
* @parm ifname
* Interface name.
*
* @return A deferred promise.
*/
function checkInterfaceExist(ifname) {
return scanInterfaces()
.then(list => {
let index = list.indexOf(ifname);
if (index < 0) {
throw "Interface " + ifname + " not found.";
}
ok(true, ifname + " exists.");
});
}
/**
* Scan for available ethernet interfaces.
*
* Fulfill params: A list of available interfaces found in device.
*
* @return A deferred promise.
*/
function scanInterfaces() {
let deferred = Promise.defer();
ethernetManager.scan(function onScan(list) {
deferred.resolve(list);
});
return deferred.promise;
}
/**
* Add an interface into interface list.
*
* Fulfill params: A boolean value indicates success or not.
*
* @param ifname
* Interface name.
*
* @return A deferred promise.
*/
function addInterface(ifname) {
let deferred = Promise.defer();
ethernetManager.addInterface(ifname, function onAdd(success, message) {
ok(success, "Add interface " + ifname + " succeeded.");
is(message, "ok", "Message is as expected.");
deferred.resolve(success);
});
return deferred.promise;
}
/**
* Remove an interface form the interface list.
*
* Fulfill params: A boolean value indicates success or not.
*
* @param ifname
* Interface name.
*
* @return A deferred promise.
*/
function removeInterface(ifname) {
let deferred = Promise.defer();
ethernetManager.removeInterface(ifname, function onRemove(success, message) {
ok(success, "Remove interface " + ifname + " succeeded.");
is(message, "ok", "Message is as expected.");
deferred.resolve(success);
});
return deferred.promise;
}
/**
* Enable networking of an interface in the interface list.
*
* Fulfill params: A boolean value indicates success or not.
*
* @param ifname
* Interface name.
*
* @return A deferred promise.
*/
function enableInterface(ifname) {
let deferred = Promise.defer();
ethernetManager.enable(ifname, function onEnable(success, message) {
ok(success, "Enable interface " + ifname + " succeeded.");
is(message, "ok", "Message is as expected.");
deferred.resolve(success);
});
return deferred.promise;
}
/**
* Disable networking of an interface in the interface list.
*
* Fulfill params: A boolean value indicates success or not.
*
* @param ifname
* Interface name.
*
* @return A deferred promise.
*/
function disableInterface(ifname) {
let deferred = Promise.defer();
ethernetManager.disable(ifname, function onDisable(success, message) {
ok(success, "Disable interface " + ifname + " succeeded.");
is(message, "ok", "Message is as expected.");
deferred.resolve(success);
});
return deferred.promise;
}
/**
* Make an interface connect to network.
*
* Fulfill params: A boolean value indicates success or not.
*
* @param ifname
* Interface name.
*
* @return A deferred promise.
*/
function makeInterfaceConnect(ifname) {
let deferred = Promise.defer();
ethernetManager.connect(ifname, function onConnect(success, message) {
ok(success, "Interface " + ifname + " is connected successfully.");
is(message, "ok", "Message is as expected.");
deferred.resolve(success);
});
return deferred.promise;
}
/**
* Make an interface disconnect to network.
*
* Fulfill params: A boolean value indicates success or not.
*
* @param ifname
* Interface name.
*
* @return A deferred promise.
*/
function makeInterfaceDisconnect(ifname) {
let deferred = Promise.defer();
ethernetManager.disconnect(ifname, function onDisconnect(success, message) {
ok(success, "Interface " + ifname + " is disconnected successfully.");
is(message, "ok", "Message is as expected.");
deferred.resolve(success);
});
return deferred.promise;
}
/**
* Update the config the an interface in the interface list.
*
* @param ifname
* Interface name.
* @param config
* .ip: ip address.
* .prefixLength: mask length.
* .gateway: gateway.
* .dnses: dnses.
* .httpProxyHost: http proxy host.
* .httpProxyPort: http porxy port.
* .usingDhcp: an boolean value indicates using dhcp or not.
*
* @return A deferred promise.
*/
function updateInterfaceConfig(ifname, config) {
let deferred = Promise.defer();
ethernetManager.updateInterfaceConfig(ifname, config,
function onUpdated(success, message) {
ok(success, "Interface " + ifname + " config is updated successfully " +
"with " + JSON.stringify(config));
is(message, "ok", "Message is as expected.");
deferred.resolve(success);
});
return deferred.promise;
}
/**
* Wait for timeout.
*
* @param timeout
* Time in ms.
*
* @return A deferred promise.
*/
function waitForTimeout(timeout) {
let deferred = Promise.defer();
setTimeout(function() {
ok(true, "waitForTimeout " + timeout);
deferred.resolve();
}, timeout);
return deferred.promise;
}
/**
* Wait for default route of a specific interface being set and
* check.
*
* @param ifname
* Interface name.
* @param gateway
* Target gateway.
*
* @return A deferred promise.
*/
function waitForDefaultRouteSet(ifname, gateway) {
return gTestSuite.waitForTimeout(500)
.then(() => gTestSuite.checkDefaultRoute(ifname, gateway))
.then(success => {
if (success) {
ok(true, "Default route is set as expected." + gateway);
return;
}
ok(true, "Default route is not set yet, check again. " + success);
return waitForDefaultRouteSet(ifname, gateway);
});
}
//---------------------------------------------------
// Public test suite functions
//---------------------------------------------------
suite.scanInterfaces = scanInterfaces;
suite.addInterface = addInterface;
suite.removeInterface = removeInterface;
suite.enableInterface = enableInterface;
suite.disableInterface = disableInterface;
suite.makeInterfaceConnect = makeInterfaceConnect;
suite.makeInterfaceDisconnect = makeInterfaceDisconnect;
suite.updateInterfaceConfig = updateInterfaceConfig;
suite.getDhcpIpAddr = getDhcpIpAddr;
suite.getDhcpGateway = getDhcpGateway;
suite.checkInterfaceExist = checkInterfaceExist;
suite.checkInterfaceIsEnabled = checkInterfaceIsEnabled;
suite.checkInterfaceIpAddr = checkInterfaceIpAddr;
suite.checkDefaultRoute = checkDefaultRoute;
suite.checkInterfaceListLength = checkInterfaceListLength;
suite.waitForTimeout = waitForTimeout;
suite.waitForDefaultRouteSet = waitForDefaultRouteSet;
/**
* End up the test run.
*
* Wait until all pending emulator shell commands are done and then |finish|
* will be called in the end.
*/
function cleanUp() {
waitFor(finish, function() {
return pendingEmulatorShellCount === 0;
});
}
/**
* Common test routine.
*
* Start a test with the given test case chain. The test environment will be
* settled down before the test. After the test, all the affected things will
* be restored.
*
* @param aTestCaseChain
* The test case entry point, which can be a function or a promise.
*
* @return A deferred promise.
*/
suite.doTest = function(aTestCaseChain) {
return Promise.resolve()
.then(aTestCaseChain)
.then(function onresolve() {
cleanUp();
}, function onreject(aReason) {
ok(false, 'Promise rejects during test' + (aReason ? '(' + aReason + ')' : ''));
cleanUp();
});
};
return suite;
})();
|