-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
41 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
__all__ = ["core", "network", "spoofer", "types", "utils"] | ||
__all__ = ["core", "models", "network", "spoofer", "utils"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |