import unittest from pathlib import Path from unittest.mock import patch from librenet_scanner.intelligence import enrich_host from librenet_scanner.models import Host from librenet_scanner.network import ( NetworkInterface, interface_mac_address, normalize_interface_mac, target_contains_ip, ) ROOT = Path(__file__).resolve().parents[1] class LocalHost046Tests(unittest.TestCase): def test_mac_normalization(self): self.assertEqual(normalize_interface_mac("aa:bb:cc:dd:ee:ff\n"), "AA:BB:CC:DD:EE:FF") self.assertEqual(normalize_interface_mac("00:00:00:00:00:00"), "") self.assertEqual(normalize_interface_mac("invalid"), "") def test_target_contains_local_ip_for_cidr_and_range(self): self.assertTrue(target_contains_ip("192.168.10.0/24", "192.168.10.1")) self.assertTrue(target_contains_ip("192.168.10.1-254", "192.168.10.1")) self.assertFalse(target_contains_ip("192.168.10.100-254", "192.168.10.1")) self.assertFalse(target_contains_ip("192.168.5.0/24", "192.168.10.1")) def test_sysfs_mac_is_preferred(self): with patch("pathlib.Path.read_text", return_value="a6:2b:b0:a5:49:a7\n"): self.assertEqual(interface_mac_address("enp42s0"), "A6:2B:B0:A5:49:A7") def test_local_host_classification_has_priority(self): host = Host("192.168.10.1", is_local=True) enrich_host(host) self.assertEqual(host.device_type, "Ce poste") def test_local_flag_survives_merge(self): current = Host("192.168.10.1", is_local=True, mac="AA:BB:CC:DD:EE:FF") current.merge(Host("192.168.10.1", device_type="Serveur Linux")) enrich_host(current) self.assertTrue(current.is_local) self.assertEqual(current.device_type, "Ce poste") def test_interface_keeps_mac_without_breaking_old_positional_signature(self): old_style = NetworkInterface("enp42s0", "192.168.10.1", 24, "192.168.10.0/24", False) self.assertFalse(old_style.is_virtual) self.assertEqual(old_style.mac, "") new_style = NetworkInterface("enp42s0", "192.168.10.1", 24, "192.168.10.0/24", False, "AA:BB:CC:DD:EE:FF") self.assertEqual(new_style.mac, "AA:BB:CC:DD:EE:FF") def test_scanner_injects_local_host_additively(self): scanner = (ROOT / "src/librenet_scanner/scanner.py").read_text() block = scanner[scanner.index("def _discover_hosts"):scanner.index("def run(self)")] self.assertIn("local_hosts = self._emit_local_host()", block) self.assertIn("known_ips.update(union_host_ips(local_hosts))", block) self.assertIn("interface_mac_address(iface.name)", scanner) if __name__ == "__main__": unittest.main()