Buckets:

download
raw
3.75 kB
"""Vertical Slice: Nmap Integration - End-to-End Scan Flow.
Tests the complete flow:
1. Scanner initialization
2. Nmap result parsing
3. Cache storage of results
"""
from unittest.mock import patch
import pytest
@pytest.mark.asyncio
async def test_nmap_scan_end_to_end():
"""Startet Scan, prüft Cache hat Ergebnisse.
Vertical slice covering:
- tools.nmap_integration.NmapScanner
- core.cache.MemoryCache
"""
from core.cache import MemoryCache
from tools.nmap_integration import NmapResult, NmapScanner
cache = MemoryCache()
# Mock nmap path validation
with patch.object(NmapScanner, "_validate_nmap_path", return_value="/usr/bin/nmap"):
scanner = NmapScanner(target="127.0.0.1", options={"ports": "22,80", "scan_type": "syn"})
# Mock the actual scan to return a controlled result
mock_result = NmapResult(
success=True,
hosts=[
{
"address": "127.0.0.1",
"ports": [{"portid": "22"}, {"portid": "80"}],
}
],
command="nmap -p 22,80 127.0.0.1",
scan_time=1.5,
error=None,
raw_xml="<xml>mock</xml>",
summary={"total_hosts": 1, "up_hosts": 1, "open_ports": 2},
)
with patch.object(scanner, "scan", return_value=mock_result):
result = await scanner.scan()
# Assertions
assert isinstance(result, NmapResult)
assert result.success is True
assert len(result.hosts) > 0
# Critical: Cache results (vertical slice)
cached_key = "nmap:scan:127.0.0.1"
await cache.set(
cached_key,
{
"success": result.success,
"hosts": result.hosts,
"command": result.command,
},
ttl=3600,
)
cached = await cache.get(cached_key)
assert cached is not None
assert cached.get("success") is True
@pytest.mark.asyncio
async def test_nmap_scan_with_service_detection():
"""Prüft Service-Erkennung."""
from tools.nmap_integration import NmapResult, NmapScanner
with patch.object(NmapScanner, "_validate_nmap_path", return_value="/usr/bin/nmap"):
scanner = NmapScanner(
target="192.168.1.1",
options={"ports": "443", "service_detection": True},
)
mock_result = NmapResult(
success=True,
hosts=[
{
"address": "192.168.1.1",
"ports": [{"portid": "443", "service": {"name": "https"}}],
}
],
command="nmap -sV -p 443 192.168.1.1",
scan_time=2.0,
error=None,
raw_xml="<xml>mock</xml>",
summary={"total_hosts": 1, "up_hosts": 1, "open_ports": 1},
)
with patch.object(scanner, "scan", return_value=mock_result):
result = await scanner.scan()
assert result.success is True
assert len(result.hosts) == 1
@pytest.mark.asyncio
async def test_nmap_scan_error_handling():
"""Prüft Fehlerbehandlung."""
from tools.nmap_integration import NmapResult, NmapScanner
with patch.object(NmapScanner, "_validate_nmap_path", return_value="/usr/bin/nmap"):
scanner = NmapScanner(target="invalid", options={"ports": "80"})
# Simulate error result
mock_result = NmapResult(
success=False,
hosts=[],
command="nmap invalid",
scan_time=0.5,
error="Failed to resolve",
raw_xml="",
summary={"total_hosts": 0, "up_hosts": 0, "open_ports": 0},
)
with patch.object(scanner, "scan", return_value=mock_result):
result = await scanner.scan()
assert result.success is False
assert result.error is not None

Xet Storage Details

Size:
3.75 kB
·
Xet hash:
60f03e49c083341550da984777a521aac17a75c903c5e68e6452e57b7024fee2

Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.