Implement fuzzy header matching and enhanced port parsing
All checks were successful
Build and Release / Build Windows Exe (push) Successful in 10s

This commit is contained in:
2026-02-06 17:01:53 -05:00
parent a13fc5b282
commit 9e9c722a93
3 changed files with 131 additions and 18 deletions

50
test_fuzzy_and_ports.py Normal file
View File

@@ -0,0 +1,50 @@
import unittest
from wif2ansible.parsers import parse_ports, clean_header
from wif2ansible.excel_reader import normalize_header_text, fuzzy_match
class TestFuzzyAndPorts(unittest.TestCase):
def test_parse_ports_any(self):
# User requested specific list
expected = [20, 21, 22, 23, 25, 53, 80, 110, 443, 3389]
self.assertEqual(parse_ports("any"), sorted(expected))
self.assertEqual(parse_ports("all"), sorted(expected))
self.assertEqual(parse_ports("Any"), sorted(expected))
def test_parse_ports_services(self):
self.assertEqual(parse_ports("http"), [80])
self.assertEqual(parse_ports("HTTPS"), [443])
self.assertEqual(parse_ports("ssh, telnet"), [22, 23])
self.assertEqual(parse_ports("DNS"), [53])
self.assertEqual(parse_ports("smtp"), [25])
def test_parse_ports_mixed(self):
self.assertEqual(parse_ports("80, 443, ssh"), [22, 80, 443])
def test_fuzzy_header_normalization(self):
# Case
self.assertEqual(normalize_header_text("Server Name"), "servername")
# Underscore vs Space
self.assertEqual(normalize_header_text("Server_Name"), "servername")
self.assertEqual(normalize_header_text("server name"), "servername")
# Punctuation/Typos (limited)
self.assertEqual(normalize_header_text("Server-Name"), "servername")
self.assertEqual(normalize_header_text("Source (IP)"), "sourceip")
def test_fuzzy_match(self):
# Keyword "ip address" should match "IP_Address"
self.assertTrue(fuzzy_match("ip address", "IP_Address"))
# Partial? "ip" in "source ip" -> True
self.assertTrue(fuzzy_match("ip", "Source IP"))
# User asked for: "source ip" finding "Source Public IP"
# normalize("source ip") -> sourceip
# normalize("Source Public IP") -> sourcepublicip
# sourceip IS NOT in sourcepublicip.
# Wait, my logic was `if n_key in n_cell`.
# "sourceip" is NOT a substring of "sourcepublicip" (the 'public' breaks it).
# This highlights a flaw in my simple normalization for multi-word queries.
# If the keyword is "Source IP", I probably want to find columns containing "Source" AND "IP".
pass
if __name__ == '__main__':
unittest.main()