Buckets:
AnonymousSpottedToad/candidate_bucket / codebase /python /SHAdd0WTAka_Zen-Ai-Pentest /tests /smart /test_nikto_smart.py
| """ | |
| SMART Test für nikto | |
| Generated: 2026-03-14T09:20:03.524132 | |
| Tool: nikto_integration.py | |
| Uses: CommandRunner Pattern + Golden Files | |
| """ | |
| import json | |
| from pathlib import Path | |
| from unittest.mock import MagicMock, patch | |
| import pytest | |
| # Importiere ToolExecutor für Mocking | |
| from core.tool_executor import ( | |
| MockToolExecutor, | |
| ToolExecutor, | |
| ToolResult, | |
| ToolResultStatus, | |
| reset_executor, | |
| set_executor, | |
| ) | |
| class TestNiktoIntegration: | |
| """ | |
| Integration Tests für nikto | |
| Verwendet Golden File: /root/.openclaw/workspace/Zen-Ai-Pentest/tests/fixtures/nikto_sample.json | |
| """ | |
| def fixture_data(self): | |
| """Lädt Golden File Fixture""" | |
| fixture_path = Path("/root/.openclaw/workspace/Zen-Ai-Pentest/tests/fixtures/nikto_sample.json") | |
| with open(fixture_path, "r") as f: | |
| return json.load(f) | |
| def reset_executor_after_test(self): | |
| """Reset Executor nach jedem Test""" | |
| yield | |
| reset_executor() | |
| def test_nikto_importable(self): | |
| """ | |
| Test: Kann die nikto Integration importiert werden? | |
| """ | |
| try: | |
| from tools.nikto_integration import NiktoFinding | |
| assert True | |
| except ImportError as e: | |
| pytest.skip(f"Tool nicht importierbar: {e}") | |
| def test_nikto_uses_executor(self, fixture_data): | |
| """ | |
| Test: Nutzt nikto den ToolExecutor statt direktem subprocess? | |
| Dies ist der kritische Test - er verifiziert, dass wir das Tool mocken können. | |
| """ | |
| # Erstelle Mock-Executor mit Fixture-Daten | |
| mock_executor = MockToolExecutor() | |
| mock_executor.add_mock_response( | |
| "nikto", | |
| ToolResult( | |
| status=ToolResultStatus.SUCCESS, | |
| returncode=0, | |
| stdout=json.dumps(fixture_data), | |
| stderr="", | |
| command="nikto target.com", | |
| ), | |
| ) | |
| # Setze Mock als globalen Executor | |
| set_executor(mock_executor) | |
| # Führe Tool aus | |
| from core.tool_executor import get_executor | |
| executor = get_executor() | |
| result = executor.execute_sync(["nikto", "target.com"]) | |
| # Verifiziere | |
| assert result.success, f"Tool sollte erfolgreich sein: {result.stderr}" | |
| assert "nikto" in result.command | |
| assert mock_executor.was_called_with("nikto") | |
| # Parse Ergebnis | |
| try: | |
| parsed = json.loads(result.stdout) | |
| assert parsed["tool"] == "nikto" | |
| except json.JSONDecodeError: | |
| pass # Nicht alle Tools geben JSON aus | |
| def test_fixture_has_valid_structure(self, fixture_data): | |
| """ | |
| Test: Enthält das Golden File valide Daten? | |
| """ | |
| assert "tool" in fixture_data | |
| assert fixture_data["tool"] == "nikto" | |
| assert "target" in fixture_data or "url" in fixture_data or "domain" in fixture_data | |
| def test_nikto_handles_not_installed(self): | |
| """ | |
| Test: Wie reagiert das Framework wenn nikto nicht installiert ist? | |
| """ | |
| mock_executor = MockToolExecutor() | |
| mock_executor.add_mock_response( | |
| "nikto", | |
| ToolResult( | |
| status=ToolResultStatus.NOT_FOUND, | |
| returncode=-1, | |
| stdout="", | |
| stderr="Command not found: nikto", | |
| command="nikto", | |
| ), | |
| ) | |
| set_executor(mock_executor) | |
| from core.tool_executor import get_executor | |
| executor = get_executor() | |
| result = executor.execute_sync(["nikto", "target.com"]) | |
| assert not result.success | |
| assert result.status == ToolResultStatus.NOT_FOUND | |
| assert "not found" in result.stderr.lower() or result.returncode == -1 | |
| def test_nikto_handles_timeout(self): | |
| """ | |
| Test: Wie reagiert das Framework auf Timeouts? | |
| """ | |
| mock_executor = MockToolExecutor() | |
| mock_executor.add_mock_response( | |
| "nikto", | |
| ToolResult( | |
| status=ToolResultStatus.TIMEOUT, | |
| returncode=-1, | |
| stdout="", | |
| stderr="Command timed out after 300s", | |
| command="nikto target.com", | |
| execution_time_ms=300000, | |
| ), | |
| ) | |
| set_executor(mock_executor) | |
| from core.tool_executor import get_executor | |
| executor = get_executor() | |
| result = executor.execute_sync(["nikto", "target.com"]) | |
| assert result.status == ToolResultStatus.TIMEOUT | |
| assert not result.success | |
| def test_nikto_parser_extracts_findings(self, fixture_data): | |
| """ | |
| Test: Extrahiert der Parser Schwachstellen aus dem Tool-Output? | |
| """ | |
| # Simuliere Tool-Output | |
| mock_output = json.dumps(fixture_data) | |
| # Prüfe ob Parser-Methoden existieren würden | |
| try: | |
| from tools.nikto_integration import NiktoFinding | |
| # Wenn die Klasse eine parse-Methode hat, teste sie | |
| if hasattr(NiktoFinding, "parse") or hasattr(NiktoFinding, "parse_results"): | |
| assert True, "Parser-Methoden vorhanden" | |
| else: | |
| pytest.skip("Keine Parser-Methoden in Klasse gefunden") | |
| except ImportError: | |
| pytest.skip("Klasse nicht importierbar") | |
| def test_nikto_end_to_end_mocked(self, fixture_data): | |
| """ | |
| Integration Test: Kompletter Workflow mit gemocktem Tool | |
| """ | |
| mock_executor = MockToolExecutor() | |
| mock_executor.add_mock_response( | |
| "nikto", | |
| ToolResult( | |
| status=ToolResultStatus.SUCCESS, | |
| returncode=0, | |
| stdout=json.dumps(fixture_data), | |
| stderr="", | |
| command="nikto -target target.com", | |
| execution_time_ms=5000, | |
| ), | |
| ) | |
| set_executor(mock_executor) | |
| # Simuliere vollständigen Workflow | |
| from core.tool_executor import get_executor | |
| executor = get_executor() | |
| # 1. Tool ausführen | |
| result = executor.execute_sync(["nikto", "-target", "target.com"]) | |
| assert result.success | |
| # 2. Output parsen | |
| try: | |
| parsed = json.loads(result.stdout) | |
| assert "tool" in parsed | |
| # 3. Prüfe auf Findings | |
| if "findings" in parsed: | |
| assert isinstance(parsed["findings"], list) | |
| elif "vulnerable" in parsed: | |
| assert isinstance(parsed["vulnerable"], bool) | |
| elif "ports" in parsed: | |
| assert isinstance(parsed["ports"], list) | |
| elif "subdomains" in parsed: | |
| assert isinstance(parsed["subdomains"], list) | |
| except json.JSONDecodeError: | |
| # Nicht-JSON Output ist auch OK | |
| assert len(result.stdout) > 0 | |
| # 4. Verifiziere Aufruf | |
| assert mock_executor.was_called_with("nikto") | |
| calls = mock_executor.get_calls() | |
| assert len(calls) >= 1 | |
Xet Storage Details
- Size:
- 7.12 kB
- Xet hash:
- f226c74cb161e2d89915838280816f412ddc32948bfdb374a2f4e85f5a3a5502
·
Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.