import unittest from poc import cli def parse(argv): return cli.build_parser().parse_args(argv) class SeparationTest(unittest.TestCase): def test_no_lab_subcommand(self): with self.assertRaises(SystemExit): parse(["lab"]) def test_subcommands_are_exploit_only(self): cases = {"check": [], "leak": [], "exploit": [], "status": [], "exec": ["--cmd", "id"], "offsets": ["--lib", "x"], "selftest": []} for name, extra in cases.items(): self.assertTrue(callable(parse([name] + extra).func), name) def test_exploit_keeps_its_flags(self): ns = parse(["exploit", "--cmd", "id>/out/x", "--retries", "1"]) self.assertEqual(ns.cmd, "id>/out/x") self.assertEqual(ns.retries, 1) def test_exploit_and_leak_are_separate_commands(self): self.assertIs(parse(["exploit"]).func, cli.cmd_exploit) self.assertIs(parse(["leak"]).func, cli.cmd_leak) self.assertIs(parse(["check"]).func, cli.cmd_check) class UrlTargetTest(unittest.TestCase): def test_defaults_without_url(self): ns = cli.resolve_target(parse(["exploit"])) self.assertEqual((ns.host, ns.port, ns.path), ("127.0.0.1", 18110, "")) def test_url_sets_host_port_and_path(self): ns = cli.resolve_target(parse(["exploit", "--url", "http://10.0.0.5:8080/app/index.php"])) self.assertEqual(ns.host, "10.0.0.5") self.assertEqual(ns.port, 8080) self.assertEqual(ns.path, "/app/index.php") def test_url_default_port_80(self): ns = cli.resolve_target(parse(["leak", "--url", "http://lab.internal/index.php"])) self.assertEqual(ns.port, 80) def test_url_root_path_defaults(self): ns = cli.resolve_target(parse(["leak", "--url", "http://lab.internal:9000"])) self.assertEqual(ns.path, "/index.php") def test_https_enables_tls(self): ns = cli.resolve_target(parse(["exploit", "--url", "https://lab.internal/"])) self.assertTrue(ns.tls) self.assertEqual(ns.port, 443) self.assertEqual(ns.path, "/") def test_https_default_port_443(self): ns = cli.resolve_target(parse(["leak", "--url", "https://lab.internal/index.php"])) self.assertEqual((ns.tls, ns.port, ns.path), (True, 443, "/index.php")) def test_http_leaves_tls_off(self): ns = cli.resolve_target(parse(["exploit", "--url", "http://lab.internal/"])) self.assertFalse(ns.tls) self.assertEqual(ns.port, 80) def test_unsupported_scheme_rejected(self): with self.assertRaises(SystemExit): cli.resolve_target(parse(["exploit", "--url", "ftp://lab.internal/"])) def test_insecure_flag_parses(self): ns = parse(["exploit", "--url", "https://lab.internal", "--insecure"]) self.assertTrue(ns.insecure) def test_explicit_flags_win_over_url(self): ns = cli.resolve_target(parse(["exploit", "--url", "http://a:1/x.php", "--port", "7000"])) self.assertEqual(ns.port, 7000) class BannerTest(unittest.TestCase): def test_banner_carries_author_and_org(self): import contextlib import io import poc buf = io.StringIO() with contextlib.redirect_stdout(buf): cli.banner() out = buf.getvalue() self.assertIn(poc.__author__, out) self.assertIn(poc.__org__, out) self.assertIn(poc.__version__, out) class ExecWrapperTest(unittest.TestCase): def test_wrapper_redirects_output_to_mount(self): wrapped = cli.exec_wrapper("id") self.assertIn("id", wrapped) self.assertIn("/out/.poc-exec", wrapped) self.assertTrue(wrapped.endswith("2>&1")) self.assertIn(chr(10), wrapped) def test_wrapper_survives_multiple_commands(self): wrapped = cli.exec_wrapper("id; uname -a") self.assertIn("id; uname -a", wrapped) def test_fits_boundary(self): self.assertTrue(cli.exec_fits("A" * 150)) self.assertFalse(cli.exec_fits("A" * 300)) def test_limit_matches_forge_carrier(self): from poc import forge self.assertEqual(cli.CARRIER_LIMIT, 200) with self.assertRaises(ValueError): forge.build_ht(0, 0, b"A" * (cli.CARRIER_LIMIT + 1)) def test_exec_command_parses_and_resolves(self): ns = cli.resolve_target(parse(["exec", "--cmd", "id", "--url", "http://h:9/x.php"])) self.assertIs(ns.func, cli.cmd_exec) self.assertEqual(ns.cmd, "id") self.assertEqual((ns.host, ns.port, ns.path), ("h", 9, "/x.php"))