From f7096084e242d1c51f42bf805091028eab15c4b3 Mon Sep 17 00:00:00 2001 From: Christophe Vandeplas Date: Tue, 30 Apr 2024 14:42:37 +0200 Subject: [PATCH] fix: [wifiscan] fixes wifiscan parsing problem + test --- parsers/wifiscan.py | 64 +++++++++++++++++++++------------- tests/test_parsers_wifiscan.py | 27 ++++++++++++++ 2 files changed, 66 insertions(+), 25 deletions(-) create mode 100644 tests/test_parsers_wifiscan.py diff --git a/parsers/wifiscan.py b/parsers/wifiscan.py index 875cea5..e3b40f4 100644 --- a/parsers/wifiscan.py +++ b/parsers/wifiscan.py @@ -1,9 +1,5 @@ #! /usr/bin/env python3 -# For Python3 -# Script to parse the swcutil_show.txt file -# Author: Emilien Le Jamtel - """sysdiagnose intialize. Usage: @@ -16,9 +12,6 @@ -v --version Show version. """ -import sys -from optparse import OptionParser -import plistlib import json from docopt import docopt import glob @@ -32,39 +25,60 @@ # --------------------------------------------------------------------------- # -def parsewifiscan(wifi_data): + +def parsewifiscan(wifi_data: list): output = [] for data in wifi_data: if data.endswith('.txt'): print('parsing: ' + data) with open(data, 'r') as f: for line in f: - if line.strip(): - content = line.split(',') - # check if the first entry of the dict contains the ssid - if 'ssid=' in content[0]: - parsed_data = {} - for item in content: - key_value = item.split("=") - parsed_data[key_value[0].strip()] = key_value[1] - # cleaning SSID entries - for key in parsed_data.copy().keys(): - if ' - ssid' in key: - up_parsed_data = {'ssid': re.sub(' - ssid', '', key), 'ssid_hex': parsed_data[key]} - del parsed_data[key] - up_parsed_data.update(parsed_data) - parsed_data = up_parsed_data - output.append(parsed_data) + line = line.strip() + # skip empty lines + if not line: + continue + parsed_data = {} + # process header + if line.startswith('total='): + items = line.split(',') + for item in items: + key, value = item.split('=') + parsed_data[key.strip()] = value.strip() + else: + # extract key-value by string + # key = first place with = + # check what is after =, if normal char then value is until next , + # if [ then value is until ] + # if { then value is until } + # first ssid and ssid_hex + m = re.match(r"'(?P[^\']+)' \((?P[^\)]+)\)", line) + parsed_data['ssid'] = m.group('ssid') + parsed_data['ssid_hex'] = m.group('ssid_hex') + index_now = line.index(',') + 1 + # now the rest + while index_now < len(line): + index_equals = line.index('=', index_now) + key = line[index_now:index_equals].strip() + if line[index_equals + 1] in ['[']: + index_close = line.index(']', index_now) + value = line[index_equals + 2:index_close].strip() + else: + index_close = line.index(',', index_now) + value = line[index_equals + 1:index_close].strip() + index_now = index_close + 2 + parsed_data[key] = value + output.append(parsed_data) return output # --------------------------------------------------------------------------- # + def main(): """ Main function, to be called when used as CLI tool """ - arguments = docopt(__doc__, version='parser for wifi_scan files v0.3') + arguments = docopt(__doc__, version='parser for wifi_scan files v0.4') if arguments['-i']: # list scan files in folder and build a list diff --git a/tests/test_parsers_wifiscan.py b/tests/test_parsers_wifiscan.py new file mode 100644 index 0000000..ccafdda --- /dev/null +++ b/tests/test_parsers_wifiscan.py @@ -0,0 +1,27 @@ +import os +import unittest + +from parsers.wifiscan import parsewifiscan + + +class TestParsersWifiScan(unittest.TestCase): + + log_root_path = 'tests/testdata/iOS15/sysdiagnose_2023.05.24_13-29-15-0700_iPhone-OS_iPhone_19H349/' + log_files = [ + "WiFi/wifi_scan.txt" + ] + + def test_parsewifiscan(self): + files = [os.path.join(self.log_root_path, log_file) for log_file in self.log_files] + result = parsewifiscan(files) + self.assertGreater(len(result), 0) + self.assertTrue('total' in result[0]) + + for item in result: + if 'total' in item: + continue + self.assertTrue('ssid' in item) + + +if __name__ == '__main__': + unittest.main()