File size: 3,580 Bytes
63a98a0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
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("<ossec_config>\n</ossec_config>\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("<!-- cyber-range-waf -->"), 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 = (
            "<ossec_config>\n<global/>\n</ossec_config>\n\n"
            "<ossec_config>\n<localfile/>\n</ossec_config>\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("<!-- cyber-range-waf -->"), text.index("</ossec_config>"))
        self.assertEqual(text.count("</ossec_config>"), 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()