import unittest from pathlib import Path from tempfile import TemporaryDirectory from lab import ( build_nmap_command, detect_memory_gib, patch_wazuh_manager_config, profile_budget_gib, validate_nmap_target, ) class TargetValidationTests(unittest.TestCase): def test_accepts_explicit_lab_targets(self): for target in ("juice-shop", "127.0.0.1", "10.0.0.0/24", "host.example.com"): with self.subTest(target=target): self.assertEqual(validate_nmap_target(target), target) def test_rejects_option_and_shell_injection(self): for target in ("-Pn", "host;id", "host && id", "", "--script=vuln", "host..example"): with self.subTest(target=target): with self.assertRaises(ValueError): validate_nmap_target(target) class NmapCommandTests(unittest.TestCase): def test_builds_argument_array_without_shell(self): command = build_nmap_command("juice-shop", ["-sV"]) self.assertEqual(command[-2:], ["-sV", "juice-shop"]) self.assertIn("--cap-add=NET_RAW", command) def test_accepts_documented_option_values(self): command = build_nmap_command("waf", ["--top-ports", "100", "-p", "80,443"]) self.assertEqual(command[-5:], ["--top-ports", "100", "-p", "80,443", "waf"]) def test_rejects_arbitrary_nmap_options(self): for options in (["--script=vuln"], ["--top-ports", "1;id"], ["juice-shop"]): with self.subTest(options=options): with self.assertRaises(ValueError): build_nmap_command("juice-shop", options) class WazuhConfigurationTests(unittest.TestCase): def test_patch_is_idempotent(self): with TemporaryDirectory() as directory: path = Path(directory) / "manager.conf" path.write_text("\n\n", encoding="utf-8") patch_wazuh_manager_config(path) patch_wazuh_manager_config(path) text = path.read_text(encoding="utf-8") self.assertEqual(text.count(""), 1) self.assertIn("/var/log/waf/error.log", text) def test_patch_rejects_unexpected_config(self): with TemporaryDirectory() as directory: path = Path(directory) / "manager.conf" path.write_text("not-xml", encoding="utf-8") with self.assertRaises(ValueError): patch_wazuh_manager_config(path) def test_patch_supports_official_two_block_layout(self): official_shape = ( "\n\n\n\n" "\n\n\n" ) with TemporaryDirectory() as directory: path = Path(directory) / "manager.conf" path.write_text(official_shape, encoding="utf-8") patch_wazuh_manager_config(path) text = path.read_text(encoding="utf-8") self.assertLess(text.index(""), text.index("")) self.assertEqual(text.count(""), 2) class ResourceTests(unittest.TestCase): def test_profiles_have_ordered_budgets(self): self.assertLess(profile_budget_gib("core"), profile_budget_gib("blue")) self.assertEqual(profile_budget_gib("blue"), 9.0) self.assertGreater(profile_budget_gib("all-containers"), profile_budget_gib("ai")) def test_memory_detection_is_positive(self): self.assertGreater(detect_memory_gib(), 0) if __name__ == "__main__": unittest.main()