From a1390e885b640f5f49579769d392c50ca68bc7b2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D8=B3=D9=84=D9=8A=D9=85=20=D8=B9=D8=A8=D8=AF=20=D8=A7?= =?UTF-8?q?=D9=84=D8=AD=D9=85=D9=8A=D8=AF?= Date: Mon, 5 Jul 2021 14:56:11 -0400 Subject: [PATCH 1/2] Added failing test for scan result without ssid --- test/wpa_cli.js | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/test/wpa_cli.js b/test/wpa_cli.js index fe004dc..9f47650 100644 --- a/test/wpa_cli.js +++ b/test/wpa_cli.js @@ -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'); @@ -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'); From 6e5d48fecdd15f1ff7e944400e3ecdc3577440e2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D8=B3=D9=84=D9=8A=D9=85=20=D8=B9=D8=A8=D8=AF=20=D8=A7?= =?UTF-8?q?=D9=84=D8=AD=D9=85=D9=8A=D8=AF?= Date: Mon, 5 Jul 2021 14:56:32 -0400 Subject: [PATCH 2/2] Added more robust parsing of scan results --- package.json | 5 ++++- wpa_cli.js | 44 +++++++++++++++++--------------------------- 2 files changed, 21 insertions(+), 28 deletions(-) diff --git a/package.json b/package.json index 9fc9a6c..f443d77 100644 --- a/package.json +++ b/package.json @@ -51,5 +51,8 @@ "email": "support@wink.com", "url": "https://www.wink.com/" } - ] + ], + "dependencies": { + "lodash": "^4.17.21" + } } diff --git a/wpa_cli.js b/wpa_cli.js index f4fa8fb..1a4d2da 100644 --- a/wpa_cli.js +++ b/wpa_cli.js @@ -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. @@ -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; }