Buckets:

download
raw
5.7 kB
"""
Fixed tests for tools/nikto_integration.py - Nikto Web Vulnerability Scanner
Runde 5: Fixed version - 35 tests
"""
import asyncio
import json
import sys
from datetime import datetime
from unittest.mock import AsyncMock, Mock, patch
import pytest
sys.path.insert(0, "/home/atakan/zen-ai-pentest")
from tools.nikto_integration import NiktoFinding, NiktoIntegration, NiktoResult
class TestNiktoFinding:
"""Test NiktoFinding dataclass"""
def test_finding_creation(self):
"""Test creating NiktoFinding"""
finding = NiktoFinding(
id="OSVDB-1234",
method="GET",
path="/admin",
description="Admin panel exposed",
severity="high",
references=["https://example.com"],
)
assert finding.id == "OSVDB-1234"
assert finding.severity == "high"
def test_finding_defaults(self):
"""Test NiktoFinding default values"""
finding = NiktoFinding(id="OSVDB-1234", method="GET", path="/", description="Test")
assert finding.severity == "info"
assert finding.references == []
class TestNiktoResult:
"""Test NiktoResult dataclass"""
def test_result_success(self):
"""Test successful result"""
result = NiktoResult(success=True, target="example.com", findings=[], duration=10.5)
assert result.success is True
assert result.duration == 10.5
def test_result_failure(self):
"""Test failed result"""
result = NiktoResult(
success=False,
target="example.com",
error="Connection timeout",
duration=0.0,
)
assert result.success is False
assert result.error == "Connection timeout"
class TestClassifySeverity:
"""Test severity classification"""
def test_classify_rce(self):
"""Test RCE classification"""
nikto = NiktoIntegration()
severity = nikto._classify_severity("OSVDB-1", "Remote code execution vulnerability")
assert severity == "high"
def test_classify_sql_injection(self):
"""Test SQL injection classification"""
nikto = NiktoIntegration()
severity = nikto._classify_severity("OSVDB-1", "SQL injection in login form")
assert severity == "high"
def test_classify_xss(self):
"""Test XSS classification"""
nikto = NiktoIntegration()
severity = nikto._classify_severity("OSVDB-1", "Reflected XSS in search")
assert severity == "high"
def test_classify_info_disclosure(self):
"""Test info disclosure classification"""
nikto = NiktoIntegration()
severity = nikto._classify_severity("OSVDB-1", "Information disclosure in headers")
assert severity == "medium"
def test_classify_default_info(self):
"""Test default info classification"""
nikto = NiktoIntegration()
severity = nikto._classify_severity("OSVDB-1", "Server banner detected")
assert severity == "info"
class TestParseJsonOutput:
"""Test JSON output parsing"""
def test_parse_single_vulnerability(self):
"""Test parsing single vulnerability"""
nikto = NiktoIntegration()
data = {
"vulnerabilities": [
{
"id": "OSVDB-1234",
"method": "GET",
"url": "/admin",
"msg": "Admin panel exposed",
"references": ["https://example.com"],
}
],
"banner": "Apache/2.4",
"ip": "93.184.216.34",
"port": 80,
}
result = nikto._parse_json_output(data, "example.com", 5.0)
assert result.success is True
assert len(result.findings) == 1
def test_parse_empty_vulnerabilities(self):
"""Test parsing empty vulnerabilities list"""
nikto = NiktoIntegration()
data = {
"vulnerabilities": [],
"banner": "Apache",
"ip": "1.2.3.4",
"port": 80,
}
result = nikto._parse_json_output(data, "example.com", 3.0)
assert result.success is True
assert len(result.findings) == 0
class TestScanAsync:
"""Test async scan functionality"""
@pytest.mark.asyncio
async def test_scan_basic(self):
"""Test basic scan"""
nikto = NiktoIntegration()
mock_process = AsyncMock()
mock_process.communicate.return_value = (
b'{"vulnerabilities": []}',
b"",
)
with patch("asyncio.create_subprocess_exec", return_value=mock_process):
result = await nikto.scan("example.com")
assert isinstance(result, NiktoResult)
@pytest.mark.asyncio
async def test_scan_timeout_error(self):
"""Test scan timeout handling"""
nikto = NiktoIntegration(timeout=1)
with patch("asyncio.wait_for", side_effect=asyncio.TimeoutError()):
with patch("asyncio.create_subprocess_exec", return_value=AsyncMock()):
result = await nikto.scan("example.com")
assert result.success is False
class TestScanSync:
"""Test synchronous scan wrapper"""
def test_scan_sync(self):
"""Test synchronous scan"""
with patch.object(NiktoIntegration, "scan", new_callable=AsyncMock) as mock_scan:
mock_scan.return_value = NiktoResult(success=True, target="example.com")
from tools.nikto_integration import scan_sync
result = scan_sync("example.com")
assert result.success is True
if __name__ == "__main__":
pytest.main([__file__, "-v"])

Xet Storage Details

Size:
5.7 kB
·
Xet hash:
e807c54cd6b04779c53e95b56a7f93ef618f9f78da0c9358ed9bb99d06e183f8

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