Reorganize project structure: move code to src/, docs to docs/, config to config/, scripts to scripts/, results to results/, tests to tests/. Keep only main script and latest scan results in root.
This commit is contained in:
248
src/test_system.py
Executable file
248
src/test_system.py
Executable file
@@ -0,0 +1,248 @@
|
||||
#!/usr/bin/env python3
|
||||
"""
|
||||
Test script to verify network scanner functionality
|
||||
Run this to check if everything is working correctly
|
||||
"""
|
||||
|
||||
import sys
|
||||
import subprocess
|
||||
import socket
|
||||
import json
|
||||
from pathlib import Path
|
||||
|
||||
def test_python_version():
|
||||
"""Check Python version"""
|
||||
print("Testing Python version...", end=" ")
|
||||
version = sys.version_info
|
||||
if version.major >= 3 and version.minor >= 8:
|
||||
print(f"✓ Python {version.major}.{version.minor}.{version.micro}")
|
||||
return True
|
||||
else:
|
||||
print(f"❌ Python {version.major}.{version.minor} (need 3.8+)")
|
||||
return False
|
||||
|
||||
def test_network_connectivity():
|
||||
"""Test basic network connectivity"""
|
||||
print("Testing network connectivity...", end=" ")
|
||||
try:
|
||||
# Try to connect to common DNS server
|
||||
socket.create_connection(("8.8.8.8", 53), timeout=3)
|
||||
print("✓")
|
||||
return True
|
||||
except:
|
||||
print("❌ No network connectivity")
|
||||
return False
|
||||
|
||||
def test_commands():
|
||||
"""Test required system commands"""
|
||||
commands = ['ping', 'ip', 'ssh']
|
||||
all_ok = True
|
||||
|
||||
for cmd in commands:
|
||||
print(f"Testing command '{cmd}'...", end=" ")
|
||||
try:
|
||||
result = subprocess.run(['which', cmd], capture_output=True, timeout=2)
|
||||
if result.returncode == 0:
|
||||
print("✓")
|
||||
else:
|
||||
print(f"❌ '{cmd}' not found")
|
||||
all_ok = False
|
||||
except:
|
||||
print(f"❌ Error checking '{cmd}'")
|
||||
all_ok = False
|
||||
|
||||
return all_ok
|
||||
|
||||
def test_scripts_exist():
|
||||
"""Check if all scripts exist"""
|
||||
scripts = [
|
||||
'src/network_scanner.py',
|
||||
'src/pfsense_scanner.py',
|
||||
'src/svg_generator.py',
|
||||
'src/integrated_scanner.py',
|
||||
'scripts/quickstart.sh'
|
||||
]
|
||||
|
||||
all_ok = True
|
||||
for script in scripts:
|
||||
print(f"Checking {script}...", end=" ")
|
||||
if Path(script).exists():
|
||||
print("✓")
|
||||
else:
|
||||
print(f"❌ Missing")
|
||||
all_ok = False
|
||||
|
||||
return all_ok
|
||||
|
||||
def test_script_syntax():
|
||||
"""Test Python script syntax"""
|
||||
scripts = [
|
||||
'src/network_scanner.py',
|
||||
'src/pfsense_scanner.py',
|
||||
'src/svg_generator.py',
|
||||
'src/integrated_scanner.py'
|
||||
]
|
||||
|
||||
all_ok = True
|
||||
for script in scripts:
|
||||
print(f"Testing {script} syntax...", end=" ")
|
||||
try:
|
||||
result = subprocess.run(
|
||||
['python3', '-m', 'py_compile', script],
|
||||
capture_output=True,
|
||||
timeout=5
|
||||
)
|
||||
if result.returncode == 0:
|
||||
print("✓")
|
||||
else:
|
||||
print(f"❌ Syntax error")
|
||||
all_ok = False
|
||||
except:
|
||||
print(f"❌ Error")
|
||||
all_ok = False
|
||||
|
||||
return all_ok
|
||||
|
||||
def test_local_network_detection():
|
||||
"""Test local network detection"""
|
||||
print("Detecting local networks...", end=" ")
|
||||
try:
|
||||
result = subprocess.run(
|
||||
['ip', 'route', 'show'],
|
||||
capture_output=True,
|
||||
text=True,
|
||||
timeout=5
|
||||
)
|
||||
|
||||
networks = []
|
||||
for line in result.stdout.splitlines():
|
||||
if '/' in line and not line.startswith('default'):
|
||||
parts = line.split()
|
||||
if parts and '/' in parts[0]:
|
||||
networks.append(parts[0])
|
||||
|
||||
if networks:
|
||||
print(f"✓ Found {len(networks)} network(s)")
|
||||
for net in networks[:3]:
|
||||
print(f" - {net}")
|
||||
return True
|
||||
else:
|
||||
print("⚠️ No networks detected")
|
||||
return True
|
||||
except:
|
||||
print("❌ Error detecting networks")
|
||||
return False
|
||||
|
||||
def test_ssh_config():
|
||||
"""Check SSH configuration"""
|
||||
print("Checking SSH keys...", end=" ")
|
||||
|
||||
ssh_dir = Path.home() / '.ssh'
|
||||
keys = []
|
||||
|
||||
for key_file in ['id_rsa', 'id_ed25519', 'id_ecdsa']:
|
||||
key_path = ssh_dir / key_file
|
||||
if key_path.exists():
|
||||
keys.append(key_file)
|
||||
|
||||
if keys:
|
||||
print(f"✓ Found {len(keys)} key(s): {', '.join(keys)}")
|
||||
return True
|
||||
else:
|
||||
print("⚠️ No SSH keys found (needed for device access)")
|
||||
return True # Not critical
|
||||
|
||||
def create_test_config():
|
||||
"""Create a test configuration"""
|
||||
print("Creating test configuration...", end=" ")
|
||||
|
||||
config = {
|
||||
"ssh_user": "root",
|
||||
"ssh_key_path": None,
|
||||
"timeout": 2,
|
||||
"additional_networks": [],
|
||||
"special_devices": {},
|
||||
"scan_options": {
|
||||
"max_workers": 10,
|
||||
"ping_timeout": 2,
|
||||
"port_scan_timeout": 1
|
||||
}
|
||||
}
|
||||
|
||||
try:
|
||||
with open('test_config.json', 'w') as f:
|
||||
json.dump(config, f, indent=2)
|
||||
print("✓ Created test_config.json")
|
||||
return True
|
||||
except Exception as e:
|
||||
print(f"❌ Error: {e}")
|
||||
return False
|
||||
|
||||
def main():
|
||||
print("="*60)
|
||||
print("Network Scanner - System Test")
|
||||
print("="*60)
|
||||
print()
|
||||
|
||||
tests = [
|
||||
("Python Version", test_python_version),
|
||||
("Network Connectivity", test_network_connectivity),
|
||||
("System Commands", test_commands),
|
||||
("Script Files", test_scripts_exist),
|
||||
("Script Syntax", test_script_syntax),
|
||||
("Network Detection", test_local_network_detection),
|
||||
("SSH Configuration", test_ssh_config),
|
||||
("Test Config", create_test_config),
|
||||
]
|
||||
|
||||
results = []
|
||||
|
||||
for test_name, test_func in tests:
|
||||
print(f"\n--- {test_name} ---")
|
||||
try:
|
||||
result = test_func()
|
||||
results.append((test_name, result))
|
||||
except Exception as e:
|
||||
print(f"❌ Test failed with error: {e}")
|
||||
results.append((test_name, False))
|
||||
print()
|
||||
|
||||
# Summary
|
||||
print("="*60)
|
||||
print("Test Summary")
|
||||
print("="*60)
|
||||
print()
|
||||
|
||||
passed = sum(1 for _, result in results if result)
|
||||
total = len(results)
|
||||
|
||||
for test_name, result in results:
|
||||
status = "✓ PASS" if result else "❌ FAIL"
|
||||
print(f"{status:8} {test_name}")
|
||||
|
||||
print()
|
||||
print(f"Results: {passed}/{total} tests passed")
|
||||
print()
|
||||
|
||||
if passed == total:
|
||||
print("🎉 All tests passed! The scanner is ready to use.")
|
||||
print()
|
||||
print("Next steps:")
|
||||
print("1. Edit config.json with your network details")
|
||||
print("2. Run: ./quickstart.sh")
|
||||
print(" or: ./integrated_scanner.py --generate-svg")
|
||||
else:
|
||||
print("⚠️ Some tests failed. Please check the errors above.")
|
||||
print()
|
||||
print("Common issues:")
|
||||
print("- Missing system commands: install net-tools, iproute2, openssh-client")
|
||||
print("- No SSH keys: run 'ssh-keygen' to create one")
|
||||
print("- Script syntax errors: check Python version")
|
||||
|
||||
print()
|
||||
print("="*60)
|
||||
|
||||
return 0 if passed == total else 1
|
||||
|
||||
if __name__ == '__main__':
|
||||
sys.exit(main())
|
||||
Reference in New Issue
Block a user