[GEMINI] Add unit tests for OOP RoutingEngine, BaseRoute, and MetadataRoute
This commit is contained in:
@@ -80,3 +80,68 @@ def test_cli_analyse_no_file():
|
|||||||
assert result.exit_code == 0
|
assert result.exit_code == 0
|
||||||
assert "No file attached to analyze" in result.output
|
assert "No file attached to analyze" in result.output
|
||||||
assert "No clear flag found yet" in result.output
|
assert "No clear flag found yet" in result.output
|
||||||
|
# }}}
|
||||||
|
|
||||||
|
# {{{ test_metadata_route_direct_execute
|
||||||
|
def test_metadata_route_direct_execute():
|
||||||
|
"""
|
||||||
|
Verifies that MetadataRoute can be instantiated and executed directly.
|
||||||
|
"""
|
||||||
|
from ctf.analysis import MetadataRoute, RouteResult
|
||||||
|
|
||||||
|
route = MetadataRoute()
|
||||||
|
assert route.route_name == "File Metadata Extraction"
|
||||||
|
assert route.is_applicable(Path("dummy.txt"))
|
||||||
|
|
||||||
|
# Executing on non-existent file
|
||||||
|
res = route.execute(Path("non_existent_file_xyz.bin"))
|
||||||
|
assert isinstance(res, RouteResult)
|
||||||
|
assert res.executed
|
||||||
|
assert not res.success
|
||||||
|
assert "File not found" in res.message
|
||||||
|
# }}}
|
||||||
|
|
||||||
|
# {{{ test_routing_engine_register_and_run
|
||||||
|
def test_routing_engine_register_and_run():
|
||||||
|
"""
|
||||||
|
Verifies that RoutingEngine successfully registers and runs applicable routes.
|
||||||
|
"""
|
||||||
|
from ctf.analysis import RoutingEngine, BaseRoute, RouteResult
|
||||||
|
|
||||||
|
class MockRoute(BaseRoute):
|
||||||
|
def __init__(self):
|
||||||
|
super().__init__("Mock Route")
|
||||||
|
def is_applicable(self, file_path: Path) -> bool:
|
||||||
|
return file_path.name.endswith(".mock")
|
||||||
|
def execute(self, file_path: Path) -> RouteResult:
|
||||||
|
return RouteResult(
|
||||||
|
route_name=self.route_name,
|
||||||
|
executed=True,
|
||||||
|
success=True,
|
||||||
|
message="Mock success",
|
||||||
|
extracted_flags=["flag{mock_success}"]
|
||||||
|
)
|
||||||
|
|
||||||
|
engine = RoutingEngine()
|
||||||
|
route = MockRoute()
|
||||||
|
engine.register_route(route)
|
||||||
|
|
||||||
|
# Run with no file path
|
||||||
|
res_no_path = engine.run_all(None)
|
||||||
|
assert not res_no_path.success
|
||||||
|
assert len(res_no_path.routes) == 1
|
||||||
|
assert not res_no_path.routes[0].executed
|
||||||
|
|
||||||
|
# Run with non-applicable file path
|
||||||
|
res_non_app = engine.run_all(Path("test.txt"))
|
||||||
|
assert not res_non_app.success
|
||||||
|
assert len(res_non_app.routes) == 0
|
||||||
|
|
||||||
|
# Run with applicable file path
|
||||||
|
res_app = engine.run_all(Path("test.mock"))
|
||||||
|
assert res_app.success
|
||||||
|
assert len(res_app.routes) == 1
|
||||||
|
assert res_app.routes[0].success
|
||||||
|
assert "flag{mock_success}" in res_app.routes[0].extracted_flags
|
||||||
|
# }}}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user