Test files overview
| File | What it covers |
|---|---|
test_scapy.py | Verifies Scapy can list interfaces and capture packets |
test_scapy_l3.py | Tests Layer 3 raw socket sniffing on Windows |
test_ips_mocked.py | Unit tests for IPS blocking logic with all heavy dependencies mocked |
test_ips_simulation.py | Integration test that exercises guardar_ataque() with a live ML override |
Running the tests
test_ips_mocked.py — IPS blocking logic
This is the primary unit test for the IPS decision engine. It mocks every heavy dependency (Scapy, PyQt5, sklearn, joblib, xgboost) so the test runs in any Python environment without requiring a GPU, trained model files, or a live interface. What it mocks:clasificar_ataque_ml to return ("SQL Injection", 0.95) and then calls guardar_ataque(). The test asserts that _ejecutar_comando_firewall was called and that the generated command contains the correct attacker IP.
The 70% confidence threshold governs when the ML verdict alone triggers a block. When ML confidence is below 70%, the heuristic engine takes over — if the heuristic classifies the event as a critical attack (Exploit, SYN Flood, etc.), the IP is still blocked.
test_ips_simulation.py — Integration simulation
This test exercises the fullguardar_ataque() pipeline against real module code (no mocks), using a monkey-patch to control the ML model’s output.
It simulates a SQL Injection attack from 10.99.99.99 with 98% model confidence:
logs_bloqueos.txt— the block log file should contain an entry for10.99.99.99.intrusiones.db→bloqueostable — the SQLite database should have a new row for the blocked IP.
test_scapy.py — Interface discovery and basic capture
This diagnostic script confirms that Scapy can enumerate network interfaces and capture live packets through Npcap:test_scapy_l3.py — Layer 3 raw socket sniffing
This test verifies the alternative Layer 3 capture path usingL3RawSocket, which is a lower-level Windows socket that bypasses the Npcap PCAP layer:
The production IDS uses
conf.use_pcap = True (Npcap/Layer 2) rather than L3RawSocket. This test was used during development to diagnose why the original sniffer wasn’t capturing packets on Windows — Layer 3 raw sockets are invisible to the Npcap driver, confirming that sendp() and conf.use_pcap = True are the correct approach.End-to-end verification
To confirm the complete detection and blocking pipeline is working:Start the IDS
Run
python interfasc.py as administrator. Confirm the sniffer starts and the live traffic table is active.Run the simulator
In a second elevated terminal, run
python simular_varios_ataques.py. Select an attack mode — DDoS or Port Scan produces the clearest detections.Observe detections
Watch the Tráfico en Vivo table in the IDS interface. New rows should appear with the attack type and ML confidence score within seconds of the packets being sent.
Verify the IPS block
Switch to the Respuesta Activa tab. The attacker IP should appear as Bloqueado with a countdown timer. If the system lacks admin rights, the status will show Bloqueo simulado instead — monitoring and alerting still work normally.