Skip to content

Commit

Permalink
new: [tests] test timeliner analyser
Browse files Browse the repository at this point in the history
  • Loading branch information
cvandeplas committed Jun 14, 2024
1 parent a746246 commit 0a618d4
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 0 deletions.
8 changes: 8 additions & 0 deletions parsers/mobileactivation.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import glob
import os
from utils import multilinelog
import json

parser_description = "Parsing mobileactivation logs file"

Expand All @@ -27,3 +28,10 @@ def parse_path(path: str) -> list | dict:
for logfile in get_log_files(path):
result.extend(multilinelog.extract_from_file(logfile))
return result


def parse_path_to_folder(path: str, output_folder: str) -> bool:
result = parse_path(path)
output_file = os.path.join(output_folder, f"{__name__.split('.')[-1]}.json")
with open(output_file, 'w') as f:
json.dump(result, f, indent=4)
8 changes: 8 additions & 0 deletions parsers/powerlogs.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import glob
import os
from utils.misc import merge_dicts
import json


parser_description = "Parsing powerlogs database"
Expand All @@ -34,3 +35,10 @@ def parse_path(path: str) -> dict:
db_json = sqlite2json.sqlite2struct(logfile)
result = merge_dicts(result, db_json) # merge both
return result


def parse_path_to_folder(path: str, output_folder: str) -> bool:
result = parse_path(path)
output_file = os.path.join(output_folder, f"{__name__.split('.')[-1]}.json")
with open(output_file, 'w') as f:
json.dump(result, f, indent=4)
9 changes: 9 additions & 0 deletions parsers/shutdownlogs.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
import glob
import os
import re
import json


parser_description = "Parsing shutdown.log file"

Expand Down Expand Up @@ -63,3 +65,10 @@ def parse_path(path: str) -> list | dict:
index += 1

return parsed_data


def parse_path_to_folder(path: str, output_folder: str) -> bool:
result = parse_path(path)
output_file = os.path.join(output_folder, f"{__name__.split('.')[-1]}.json")
with open(output_file, 'w') as f:
json.dump(result, f, indent=4)
9 changes: 9 additions & 0 deletions parsers/swcutil.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

import glob
import os
import json


parser_description = "Parsing swcutil_show file"

Expand Down Expand Up @@ -76,6 +78,13 @@ def parse_path(path: str) -> list | dict:
return {'error': 'No swcutil_show.txt file present'}


def parse_path_to_folder(path: str, output_folder: str) -> bool:
result = parse_path(path)
output_file = os.path.join(output_folder, f"{__name__.split('.')[-1]}.json")
with open(output_file, 'w') as f:
json.dump(result, f, indent=4)


def parse_basic(data):
output = {}
for line in data:
Expand Down
8 changes: 8 additions & 0 deletions parsers/wifisecurity.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
# Author: [email protected]

import os
import json

parser_description = "Parsing WiFi Security logs"

Expand Down Expand Up @@ -55,3 +56,10 @@ def parse_path(path: str) -> list | dict:
except Exception as e:
print(f"Could not parse: {get_log_files(path)[0]}. Reason: {str(e)}")
return entries


def parse_path_to_folder(path: str, output_folder: str) -> bool:
result = parse_path(path)
output_file = os.path.join(output_folder, f"{__name__.split('.')[-1]}.json")
with open(output_file, 'w') as f:
json.dump(result, f, indent=4)
33 changes: 33 additions & 0 deletions tests/test_analysers_timeliner.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
from analysers.timeliner import analyse_path
from parsers import accessibility_tcc, logarchive, mobileactivation, powerlogs, swcutil, shutdownlogs, wifisecurity, wifi_known_networks
from tests import SysdiagnoseTestCase
import unittest
import os
import tempfile


class TestAnalysersTimeliner(SysdiagnoseTestCase):

def test_analyse_timeliner(self):
for log_root_path in self.log_root_paths:

with tempfile.TemporaryDirectory() as tmp_outpath:
# first run the parsers
accessibility_tcc.parse_path_to_folder(log_root_path, output_folder=tmp_outpath)
logarchive.parse_path_to_folder(log_root_path, output_folder=tmp_outpath)
mobileactivation.parse_path_to_folder(log_root_path, output_folder=tmp_outpath)
powerlogs.parse_path_to_folder(log_root_path, output_folder=tmp_outpath)
shutdownlogs.parse_path_to_folder(log_root_path, output_folder=tmp_outpath)
swcutil.parse_path_to_folder(log_root_path, output_folder=tmp_outpath)
wifi_known_networks.parse_path_to_folder(log_root_path, output_folder=tmp_outpath)
wifisecurity.parse_path_to_folder(log_root_path, output_folder=tmp_outpath)

# then run the analyser
output_file = os.path.join(tmp_outpath, 'timeliner.jsonl')
analyse_path(case_folder=tmp_outpath, output_file=output_file)
self.assertTrue(os.path.isfile(output_file))
self.assertGreater(os.path.getsize(output_file), 0)


if __name__ == '__main__':
unittest.main()

0 comments on commit 0a618d4

Please sign in to comment.