Skip to content

Commit

Permalink
test(access-point/wifi): adds ability to set ap/adhoc IP address
Browse files Browse the repository at this point in the history
  • Loading branch information
HipsterBrown committed Oct 9, 2016
1 parent 60c67ef commit 65d6570
Show file tree
Hide file tree
Showing 3 changed files with 184 additions and 9 deletions.
58 changes: 51 additions & 7 deletions test/unit/access-point.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ exports['Tessel.prototype.createAccessPoint'] = {
this.reconnectWifi = this.sandbox.spy(commands, 'reconnectWifi');
this.reconnectDnsmasq = this.sandbox.spy(commands, 'reconnectDnsmasq');
this.reconnectDhcp = this.sandbox.spy(commands, 'reconnectDhcp');
this.getWifiSettings = this.sandbox.spy(commands, 'getWifiSettings');

this.tessel = TesselSimulator();
// These are needed because the sheer number of commands run within
Expand All @@ -46,7 +47,7 @@ exports['Tessel.prototype.createAccessPoint'] = {
},

newAccessPoint: function(test) {
test.expect(21);
test.expect(23);
var creds = {
ssid: 'test',
password: 'test-password',
Expand Down Expand Up @@ -94,10 +95,12 @@ exports['Tessel.prototype.createAccessPoint'] = {
test.equal(this.reconnectWifi.callCount, 1);
test.equal(this.reconnectDnsmasq.callCount, 1);
test.equal(this.reconnectDhcp.callCount, 1);
test.equal(this.getWifiSettings.callCount, 1);
test.ok(this.setAccessPointSSID.lastCall.calledWith(creds.ssid));
test.ok(this.setAccessPointPassword.lastCall.calledWith(creds.password));
test.ok(this.setAccessPointSecurity.lastCall.calledWith(creds.security));
test.ok(this.setAccessPointMode.lastCall.calledWith(creds.mode));
test.ok(this.setLanNetworkIP.lastCall.calledWith('192.168.1.101'));

test.done();
})
Expand All @@ -108,7 +111,7 @@ exports['Tessel.prototype.createAccessPoint'] = {
},

newAdhoc: function(test) {
test.expect(21);
test.expect(23);
var creds = {
ssid: 'test',
password: 'test-password',
Expand Down Expand Up @@ -156,10 +159,12 @@ exports['Tessel.prototype.createAccessPoint'] = {
test.equal(this.reconnectWifi.callCount, 1);
test.equal(this.reconnectDnsmasq.callCount, 1);
test.equal(this.reconnectDhcp.callCount, 1);
test.equal(this.getWifiSettings.callCount, 1);
test.ok(this.setAccessPointSSID.lastCall.calledWith(creds.ssid));
test.ok(this.setAccessPointPassword.lastCall.calledWith(creds.password));
test.ok(this.setAccessPointSecurity.lastCall.calledWith(creds.security));
test.ok(this.setAccessPointMode.lastCall.calledWith(creds.mode));
test.ok(this.setLanNetworkIP.lastCall.calledWith('192.168.1.101'));

test.done();
})
Expand Down Expand Up @@ -206,12 +211,13 @@ exports['Tessel.prototype.createAccessPoint'] = {
},

properCredentials: function(test) {
test.expect(11);
test.expect(13);
var creds = {
ssid: 'test',
password: 'test-password',
security: 'psk2',
mode: 'ap'
mode: 'ap',
ip: '192.168.1.113'
};

// Test is expecting two closes...;
Expand All @@ -228,12 +234,14 @@ exports['Tessel.prototype.createAccessPoint'] = {
test.equal(this.setAccessPointPassword.callCount, 1);
test.equal(this.setAccessPointSecurity.callCount, 1);
test.equal(this.setAccessPointMode.callCount, 1);
test.equal(this.setLanNetworkIP.callCount, 1);
test.equal(this.reconnectWifi.callCount, 1);
test.equal(this.reconnectDnsmasq.callCount, 1);
test.equal(this.reconnectDhcp.callCount, 1);
test.ok(this.setAccessPointSSID.lastCall.calledWith(creds.ssid));
test.ok(this.setAccessPointPassword.lastCall.calledWith(creds.password));
test.ok(this.setAccessPointSecurity.lastCall.calledWith(creds.security));
test.ok(this.setLanNetworkIP.lastCall.calledWith(creds.ip));
test.done();
})
.catch(error => {
Expand Down Expand Up @@ -277,6 +285,42 @@ exports['Tessel.prototype.createAccessPoint'] = {
test.ok(false, error.toString());
test.done();
});
},

createAdhocWhenWifiEnabled: function(test) {
test.expect(2);
var creds = {
ssid: 'test',
mode: 'adhoc'
};

// Test is expecting two closes...;
this.tessel._rps.on('control', (command) => {
if (command.toString() === 'uci show wireless.@wifi-iface[0]') {
var info = new Buffer(tags.stripIndent `
wireless.cfg053579.disabled='0'`);

setImmediate(() => {
this.tessel._rps.stdout.emit('data', info);
this.tessel._rps.emit('close');
});
} else {
setImmediate(() => {
this.tessel._rps.emit('close');
});
}
});

this.tessel.createAccessPoint(creds)
.then(() => {
test.fail('Creating an adhoc network should fail while wifi is enabled.');
test.done();
})
.catch(error => {
test.equal(this.getWifiSettings.callCount, 1);
test.ok(error.toString());
test.done();
});
}
};

Expand Down Expand Up @@ -346,7 +390,7 @@ exports['Tessel.prototype.enableAccessPoint'] = {
}
});

this.tessel.enableAccessPoint()
this.tessel.enableAccessPoint({mode: 'ap'})
.then(() => {
test.equal(this.turnAccessPointOn.callCount, 1);
test.equal(this.reconnectWifi.callCount, 1);
Expand Down Expand Up @@ -398,7 +442,7 @@ exports['Tessel.prototype.enableAccessPoint'] = {
}
});

this.tessel.enableAccessPoint()
this.tessel.enableAccessPoint({mode: 'ap'})
.then(() => {
test.fail('Should not pass');
test.done();
Expand Down Expand Up @@ -444,7 +488,7 @@ exports['Tessel.prototype.disableAccessPoint'] = {
});
});

this.tessel.disableAccessPoint()
this.tessel.disableAccessPoint({mode: 'ap'})
.then(() => {
test.equal(this.turnAccessPointOff.callCount, 1);
test.equal(this.reconnectWifi.callCount, 1);
Expand Down
30 changes: 30 additions & 0 deletions test/unit/controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -1156,6 +1156,36 @@ exports['controller.createAccessPoint'] = {
test.done();
});
},

invalidAccessPointIPAddress: function(test) {
test.expect(1);

controller.createAccessPoint({
ssid: 'test',
ip: '999.9999.9'
})
.catch(error => {
test.ok(error);
test.done();
});
},

validAccessPointIPAddress: function(test) {
test.expect(1);

controller.createAccessPoint({
ssid: 'test',
ip: '192.168.1.113'
})
.then((settings) => {
test.ok(settings);
test.done();
})
.catch((error) => {
test.fail(error.toString());
test.done();
});
},
};

exports['controller.root'] = {
Expand Down
105 changes: 103 additions & 2 deletions test/unit/wifi.js
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,7 @@ module.exports['Tessel.prototype.connectToNetwork'] = {
this.commitWirelessCredentials = this.sandbox.spy(commands, 'commitWirelessCredentials');
this.reconnectWifi = this.sandbox.spy(commands, 'reconnectWifi');
this.getWifiInfo = this.sandbox.spy(commands, 'getWifiInfo');
this.getAccessPointConfig = this.sandbox.spy(commands, 'getAccessPointConfig');
this.tessel = TesselSimulator();

done();
Expand All @@ -242,7 +243,7 @@ module.exports['Tessel.prototype.connectToNetwork'] = {
done();
},
noPassword: function(test) {
test.expect(8);
test.expect(9);
var creds = {
ssid: 'tank',
password: undefined
Expand All @@ -257,6 +258,15 @@ module.exports['Tessel.prototype.connectToNetwork'] = {
this.tessel._rps.stdout.emit('data', new Buffer('signal'));
this.tessel._rps.emit('close');
});
} else if (command.toString() === 'uci show wireless.@wifi-iface[1]') {
var info = new Buffer(tags.stripIndent `
wireless.cfg053579.mode='ap'
wireless.cfg053579.disabled='1'`);

setImmediate(() => {
this.tessel._rps.stdout.emit('data', info);
this.tessel._rps.emit('close');
});
} else {
setImmediate(() => {
// Remove any listeners on stdout so we don't break anything when we write to it
Expand All @@ -273,9 +283,10 @@ module.exports['Tessel.prototype.connectToNetwork'] = {
test.equal(this.setNetworkEncryption.callCount, 1);
test.equal(this.commitWirelessCredentials.callCount, 1);
test.equal(this.reconnectWifi.callCount, 1);
test.equal(this.getWifiInfo.callCount, 1);
test.equal(this.getAccessPointConfig.callCount, 1);
test.ok(this.setNetworkSSID.lastCall.calledWith(creds.ssid));
test.ok(this.setNetworkEncryption.lastCall.calledWith('none'));
test.ok(this.getWifiInfo.callCount, 1);
test.done();
})
.catch(error => {
Expand All @@ -299,6 +310,15 @@ module.exports['Tessel.prototype.connectToNetwork'] = {
this.tessel._rps.stdout.emit('data', new Buffer('signal'));
this.tessel._rps.emit('close');
});
} else if (command.toString() === 'uci show wireless.@wifi-iface[1]') {
var info = new Buffer(tags.stripIndent `
wireless.cfg053579.mode='ap'
wireless.cfg053579.disabled='1'`);

setImmediate(() => {
this.tessel._rps.stdout.emit('data', info);
this.tessel._rps.emit('close');
});
} else {
setImmediate(() => {
// Remove any listeners on stdout so we don't break anything when we write to it
Expand Down Expand Up @@ -344,6 +364,15 @@ module.exports['Tessel.prototype.connectToNetwork'] = {
this.tessel._rps.stdout.emit('data', new Buffer('signal'));
this.tessel._rps.emit('close');
});
} else if (command.toString() === 'uci show wireless.@wifi-iface[1]') {
var info = new Buffer(tags.stripIndent `
wireless.cfg053579.mode='ap'
wireless.cfg053579.disabled='1'`);

setImmediate(() => {
this.tessel._rps.stdout.emit('data', info);
this.tessel._rps.emit('close');
});
} else {
setImmediate(() => {
// Remove any listeners on stdout so we don't break anything when we write to it
Expand Down Expand Up @@ -388,6 +417,15 @@ module.exports['Tessel.prototype.connectToNetwork'] = {
this.tessel._rps.stderr.emit('data', new Buffer('Unable to connect to the network.'));
this.tessel._rps.emit('close');
});
} else if (command.toString() === 'uci show wireless.@wifi-iface[1]') {
var info = new Buffer(tags.stripIndent `
wireless.cfg053579.mode='ap'
wireless.cfg053579.disabled='1'`);

setImmediate(() => {
this.tessel._rps.stdout.emit('data', info);
this.tessel._rps.emit('close');
});
} else {
setImmediate(() => {
// Remove any listeners on stderr so we don't break anything when we write to it
Expand Down Expand Up @@ -435,6 +473,15 @@ module.exports['Tessel.prototype.connectToNetwork'] = {
this.tessel._rps.stderr.removeAllListeners();
this.tessel._rps.emit('close');
});
} else if (command.toString() === 'uci show wireless.@wifi-iface[1]') {
var info = new Buffer(tags.stripIndent `
wireless.cfg053579.mode='ap'
wireless.cfg053579.disabled='1'`);

setImmediate(() => {
this.tessel._rps.stdout.emit('data', info);
this.tessel._rps.emit('close');
});
} else {
setImmediate(() => {
// Remove any listeners on stderr so we don't break anything when we write to it
Expand Down Expand Up @@ -464,6 +511,51 @@ module.exports['Tessel.prototype.connectToNetwork'] = {
test.done();
});
},
connectFailsWhileAdhocEnabled: function(test) {
test.expect(2);
var creds = {
ssid: 'tank',
password: 'not_gonna_work'
};
// Test is expecting several closes...;
this.tessel._rps.on('control', (command) => {
if (command.toString() === 'ubus call iwinfo info {"device":"wlan0"}') {
// Write to stdout so it completes as expected
// Wrap in setImmediate to make sure listener is set up before emitting
setImmediate(() => {
this.tessel._rps.stdout.emit('data', new Buffer('signal'));
this.tessel._rps.emit('close');
});
} else if (command.toString() === 'uci show wireless.@wifi-iface[1]') {
var info = new Buffer(tags.stripIndent `
wireless.cfg053579.mode='adhoc'
wireless.cfg053579.disabled='0'`);

setImmediate(() => {
this.tessel._rps.stdout.emit('data', info);
this.tessel._rps.emit('close');
});
} else {
setImmediate(() => {
// Remove any listeners on stderr so we don't break anything when we write to it
this.tessel._rps.stdout.removeAllListeners();
this.tessel._rps.stderr.removeAllListeners();
this.tessel._rps.emit('close');
});
}
});

this.tessel.connectToNetwork(creds)
.then(function() {
test.ok(false, 'Test should have rejected with an error.');
test.done();
})
.catch((error) => {
test.equal(error.message.includes('Tessel must have adhoc disabled'), true);
test.equal(this.getAccessPointConfig.callCount, 1);
test.done();
});
},
// Sometimes the keyword for success (signal) is not in the first batch
// of stdout data
batchedResponse: function(test) {
Expand All @@ -484,6 +576,15 @@ module.exports['Tessel.prototype.connectToNetwork'] = {
this.tessel._rps.stdout.emit('data', new Buffer('signal'));
this.tessel._rps.emit('close');
});
} else if (command.toString() === 'uci show wireless.@wifi-iface[1]') {
var info = new Buffer(tags.stripIndent `
wireless.cfg053579.mode='ap'
wireless.cfg053579.disabled='1'`);

setImmediate(() => {
this.tessel._rps.stdout.emit('data', info);
this.tessel._rps.emit('close');
});
} else {
setImmediate(() => {
// Remove any listeners on stderr so we don't break anything when we write to it
Expand Down

0 comments on commit 65d6570

Please sign in to comment.