Skip to content

Commit

Permalink
fix: tests
Browse files Browse the repository at this point in the history
  • Loading branch information
zeefxd committed Dec 4, 2024
1 parent 93921d4 commit f0bf57f
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 40 deletions.
14 changes: 7 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,29 +42,29 @@ pip install -r requirements.txt

### Run with default settings
```bash
python -m src.main
python -m src
```

## 💻 **Usage Examples**

### Basic Monitoring:
```bash
python -m src.main --target 192.168.1.1
python -m src --target 192.168.1.1
```

### Pattern Matching:
```bash
python -m src.main --patterns "HTTP,FTP"
python -m src --patterns "HTTP,FTP"
```

### Custom Export:
```bash
python -m src.main --format json
python -m src --format json
```

### Example:
```bash
python -m src.main -t 192.168.1.10 -g 192.168.1.1 -p "password" "secret" -f html
python -m src -t 192.168.1.10 -g 192.168.1.1 -p "password" "secret" -f html
```

## 📋 **Command Options**
Expand All @@ -82,7 +82,7 @@ src/
├── core/ # Core functionality
├── network/ # Network operations
├── spoofer/ # ARP implementation
├── types/ # Data models
├── models/ # Data models
└── utils/ # Helper functions
```

Expand Down Expand Up @@ -119,4 +119,4 @@ GitHub: [@zeefxd](https://github.com/zeefxd)
Twitter: [@zeefxd](https://x.com/zeefxd)

💬 **Support**
Report issues on GitHub
Report issues on GitHub
2 changes: 1 addition & 1 deletion src/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__all__ = ["core", "network", "spoofer", "types", "utils"]
__all__ = ["core", "models", "network", "spoofer", "utils"]
2 changes: 1 addition & 1 deletion src/network/network_utils.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import socket
from rich.table import Table
from rich.console import Console
from src.types.models import Device
from src.models.models import Device

"""Network utility functions for device discovery and hostname resolution."""

Expand Down
2 changes: 1 addition & 1 deletion src/spoofer/arp_spoofer.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
from src.network.network_utils import get_hostname, display_devices
from src.utils.logger import setup_logger
from src.core.exceptions import NetworkException
from src.types.models import PacketInfo, Device
from src.models.models import PacketInfo, Device
from src.utils.validators import validate_ip
from jinja2 import Environment, FileSystemLoader

Expand Down
61 changes: 31 additions & 30 deletions tests/test_arp_spoofer.py
Original file line number Diff line number Diff line change
@@ -1,41 +1,42 @@
import pytest
from unittest.mock import Mock, patch
from unittest.mock import patch, MagicMock
from src.spoofer.arp_spoofer import ARPSpoofer
from src.core.exceptions import NetworkException

@pytest.fixture
def spoofer():
with patch('src.spoofer.arp_spoofer.Console'), \
patch('src.spoofer.arp_spoofer.setup_logger'):
return ARPSpoofer()
return ARPSpoofer(target_ip="192.168.1.10", gateway_ip="192.168.1.1")

def test_validate_ip_valid(spoofer):
assert spoofer.validate_ip('192.168.1.1') == True
assert spoofer.validate_ip('10.0.0.1') == True
def test_get_mac_valid_ip(spoofer):
with patch('src.spoofer.arp_spoofer.scapy.srp') as mock_srp:
mock_response = MagicMock()
mock_response.__getitem__.return_value = MagicMock(hwsrc="00:11:22:33:44:55")
mock_srp.return_value = ([mock_response], None)

mac = spoofer.get_mac("192.168.1.10")
assert mac == "00:11:22:33:44:55"

def test_validate_ip_invalid(spoofer):
assert spoofer.validate_ip('256.256.256.256') == False
assert spoofer.validate_ip('invalid_ip') == False
def test_get_mac_invalid_ip(spoofer):
with patch('src.spoofer.arp_spoofer.validate_ip', return_value=False):
with pytest.raises(NetworkException, match="Invalid IP address: 256.256.256.256"):
spoofer.get_mac("256.256.256.256")

def test_get_mac_valid_ip(spoofer, mocker):
mock_srp = mocker.patch('scapy.all.srp')
mock_srp.return_value = ([Mock(hwsrc='00:11:22:33:44:55')], None)

mac = spoofer.get_mac('192.168.1.1')
assert mac == '00:11:22:33:44:55'
def test_spoof(spoofer):
with patch.object(spoofer, 'get_mac', return_value="00:11:22:33:44:55"):
with patch('src.spoofer.arp_spoofer.scapy.sendp') as mock_sendp:
spoofer.spoof("192.168.1.10", "192.168.1.1")
assert mock_sendp.called

def test_get_mac_invalid_ip(spoofer):
with pytest.raises(NetworkException):
spoofer.get_mac('invalid_ip')
def test_restore(spoofer):
with patch.object(spoofer, 'get_mac', return_value="00:11:22:33:44:55"):
with patch('src.spoofer.arp_spoofer.scapy.sendp') as mock_sendp:
spoofer.restore("192.168.1.10", "192.168.1.1")
assert mock_sendp.called

def test_scan_network(spoofer, mocker):
mock_srp = mocker.patch('scapy.all.srp')
mock_srp.return_value = (
[Mock(psrc='192.168.1.1', hwsrc='00:11:22:33:44:55')],
None
)

devices = spoofer.scan_network('192.168.1.0/24')
assert len(devices) == 1
assert devices[0].ip == '192.168.1.1'
assert devices[0].mac == '00:11:22:33:44:55'
def test_save_captured_data_txt(spoofer):
spoofer.captured_packets = [
MagicMock(time="2023-01-01T00:00:00", src="192.168.1.10", dst="192.168.1.1", payload="test payload")
]
with patch('builtins.open', new_callable=MagicMock) as mock_open:
spoofer.save_captured_data(format="txt")
mock_open.assert_called_once()

0 comments on commit f0bf57f

Please sign in to comment.