Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed edge case where wpa_cli scan does not return any ssid #63

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,5 +51,8 @@
"email": "[email protected]",
"url": "https://www.wink.com/"
}
]
],
"dependencies": {
"lodash": "^4.17.21"
}
}
38 changes: 38 additions & 0 deletions test/wpa_cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,13 @@ var WPA_CLI_SCAN_RESULTS = [
'2c:f5:d3:02:ea:d9 2472 -31 [WPA-PSK-CCMP+TKIP][WPA2-PSK-CCMP+TKIP][ESS] FakeWifi2'
].join('\n');

var WPA_CLI_SCAN_RESULTS_NO_SSID = [
'bssid / frequency / signal level / flags / ssid',
'2c:f5:d3:02:ea:d9 2472 -31 [ESS]',
'2c:f5:d3:02:ea:d9 2472 -31 [WPA-PSK-CCMP+TKIP][WPA2-PSK-CCMP+TKIP][ESS] FakeWifi2',
'2c:f5:d3:02:ea:d9 2472 -31 [WPA-PSK-CCMP+TKIP]'
].join('\n');

var WPA_CLI_SCAN_NORESULTS = [
''
].join('\n');
Expand Down Expand Up @@ -605,6 +612,37 @@ describe('wpa_cli', function() {
});
});

it('scan_results NO_SSID', function(done) {
this.OUTPUT = WPA_CLI_SCAN_RESULTS_NO_SSID;
wpa_cli.scan_results('wlan0', function(err, results) {
should(results).eql([
{
bssid: '2c:f5:d3:02:ea:d9',
frequency: 2472,
signalLevel: -31,
flags: '[ESS]',
ssid: ''
},
{
bssid: '2c:f5:d3:02:ea:d9',
frequency: 2472,
signalLevel: -31,
flags: '[WPA-PSK-CCMP+TKIP][WPA2-PSK-CCMP+TKIP][ESS]',
ssid: 'FakeWifi2'
},
{
bssid: '2c:f5:d3:02:ea:d9',
frequency: 2472,
signalLevel: -31,
flags: '[WPA-PSK-CCMP+TKIP]',
ssid: ''
}
]);

done();
});
});

it('should handle errors', function(done) {
wpa_cli.exec = function(command, callback) {
callback('error');
Expand Down
44 changes: 17 additions & 27 deletions wpa_cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
*/

var child_process = require('child_process');
var camelCase = require('lodash/camelCase');

/**
* The **wpa_cli** command is used to configure wpa network interfaces.
Expand Down Expand Up @@ -189,37 +190,26 @@ function parse_command_interface(callback) {
* @returns {object} The parsed scan results.
*/
function parse_scan_results(block) {
var match;
var keys;
var results = [];
var lines;

lines = block.split('\n').map(function(item) { return item + "\n"; });
lines.forEach(function(entry){
var parsed = {};
if ((match = entry.match(/([A-Fa-f0-9:]{17})\t/))) {
parsed.bssid = match[1].toLowerCase();
}

if ((match = entry.match(/\t([\d]+)\t+/))) {
parsed.frequency = parseInt(match[1], 10);
}

if ((match = entry.match(/([-][0-9]+)\t/))) {
parsed.signalLevel = parseInt(match[1], 10);
}

if ((match = entry.match(/\t(\[.+\])\t/))) {
parsed.flags = match[1];
}

if ((match = entry.match(/\t([^\t]{1,32}(?=\n))/))) {
parsed.ssid = match[1];
}

if(!(Object.keys(parsed).length === 0 && parsed.constructor === Object)){
results.push(parsed);
lines = block.split('\n').map(function(item) { return item; });
lines.reduce((results, entry, lineNo) => {
if (lineNo === 0) {
keys = entry.split(' / ');
} else {
var result = {};
var values = entry.split('\t');
keys.forEach(function(key, i){
var intValue = parseInt(values[i]);
var value = intValue == values[i] ? intValue : values[i]
result[camelCase(key)] = value || ''
})
results.push(result);
}
});
return results
}, results)

return results;
}
Expand Down