Skip to content

Commit e9f0893

Browse files
authored
Merge pull request #3105 from guedou/Issue_#3051
Test get_if_addr() errors
2 parents 086fb5e + 6f519be commit e9f0893

File tree

3 files changed

+42
-14
lines changed

3 files changed

+42
-14
lines changed

scapy/arch/bpf/core.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -70,10 +70,10 @@ def get_if_raw_addr(ifname):
7070
)
7171
stdout, stderr = subproc.communicate()
7272
if subproc.returncode:
73-
warning("Failed to execute ifconfig: (%s)", plain_str(stderr))
73+
warning("Failed to execute ifconfig: (%s)", plain_str(stderr).strip())
7474
return b"\0\0\0\0"
75-
# Get IPv4 addresses
7675

76+
# Get IPv4 addresses
7777
addresses = [
7878
line.strip() for line in plain_str(stdout).splitlines()
7979
if "inet " in line
@@ -108,7 +108,7 @@ def get_if_raw_hwaddr(ifname):
108108
stdout, stderr = subproc.communicate()
109109
if subproc.returncode:
110110
raise Scapy_Exception("Failed to execute ifconfig: (%s)" %
111-
(plain_str(stderr)))
111+
plain_str(stderr).strip())
112112

113113
# Get MAC addresses
114114
addresses = [

scapy/arch/unix.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -118,23 +118,22 @@ def read_routes():
118118
from scapy.arch import get_if_addr
119119
try:
120120
ifaddr = get_if_addr(netif)
121-
routes.append((dest, netmask, gw, netif, ifaddr, metric))
122-
except OSError as exc:
123-
if 'Device not configured' in str(exc):
121+
if ifaddr == "0.0.0.0":
124122
# This means the interface name is probably truncated by
125123
# netstat -nr. We attempt to guess it's name and if not we
126124
# ignore it.
127125
guessed_netif = _guess_iface_name(netif)
128126
if guessed_netif is not None:
129127
ifaddr = get_if_addr(guessed_netif)
130-
routes.append((dest, netmask, gw, guessed_netif, ifaddr, metric)) # noqa: E501
128+
netif = guessed_netif
131129
else:
132130
log_runtime.info(
133131
"Could not guess partial interface name: %s",
134132
netif
135133
)
136-
else:
137-
raise
134+
routes.append((dest, netmask, gw, netif, ifaddr, metric))
135+
except OSError:
136+
raise
138137
else:
139138
pending_if.append((dest, netmask, gw))
140139
f.close()

test/regression.uts

Lines changed: 34 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2320,7 +2320,7 @@ default link#11 UCSI 1 0 bridge1
23202320
def se_get_if_addr(iface):
23212321
"""Perform specific side effects"""
23222322
if iface == "bridge1":
2323-
raise OSError("Device not configured")
2323+
return "0.0.0.0"
23242324
return "1.2.3.4"
23252325
mock_get_if_addr.side_effect = se_get_if_addr
23262326
# Test the function
@@ -2457,9 +2457,10 @@ test_osx_10_15_ipv4()
24572457
import mock
24582458
from io import StringIO
24592459

2460+
@mock.patch("scapy.arch.get_if_addr")
24602461
@mock.patch("scapy.arch.unix.OPENBSD")
24612462
@mock.patch("scapy.arch.unix.os")
2462-
def test_openbsd_6_3(mock_os, mock_openbsd):
2463+
def test_openbsd_6_3(mock_os, mock_openbsd, mock_get_if_addr):
24632464
"""Test read_routes() on OpenBSD 6.3"""
24642465
# 'netstat -rn -f inet' output
24652466
netstat_output = u"""
@@ -2489,6 +2490,16 @@ default 10.0.1.254 UGS 0 0 - 8 bge0
24892490

24902491
# Mocked OpenBSD parsing behavior
24912492
mock_openbsd = True
2493+
2494+
# Mocked get_if_addr() output
2495+
def se_get_if_addr(iface):
2496+
"""Perform specific side effects"""
2497+
import socket
2498+
if iface == "bge0":
2499+
return "192.168.122.42"
2500+
return "10.0.1.26"
2501+
mock_get_if_addr.side_effect = se_get_if_addr
2502+
24922503
# Test the function
24932504
from scapy.arch.unix import read_routes
24942505
return read_routes()
@@ -2518,9 +2529,10 @@ from io import StringIO
25182529

25192530
# Mocked Solaris 11.1 parsing behavior
25202531

2532+
@mock.patch("scapy.arch.get_if_addr")
25212533
@mock.patch("scapy.arch.unix.SOLARIS", True)
25222534
@mock.patch("scapy.arch.unix.os")
2523-
def test_solaris_111(mock_os):
2535+
def test_solaris_111(mock_os, mock_get_if_addr):
25242536
"""Test read_routes() on Solaris 11.1"""
25252537
# 'netstat -rvn -f inet' output
25262538
netstat_output = u"""
@@ -2536,6 +2548,15 @@ default 0.0.0.0 10.0.2.2 net0 1500 2 UG
25362548
mock_os.popen = mock.MagicMock(return_value=strio)
25372549
print(scapy.arch.unix.SOLARIS)
25382550

2551+
# Mocked get_if_addr() output
2552+
def se_get_if_addr(iface):
2553+
"""Perform specific side effects"""
2554+
import socket
2555+
if iface == "net0":
2556+
return "10.0.2.15"
2557+
return "127.0.0.1"
2558+
mock_get_if_addr.side_effect = se_get_if_addr
2559+
25392560
# Test the function
25402561
from scapy.arch.unix import read_routes
25412562
return read_routes()
@@ -2857,9 +2878,10 @@ test_freebsd_10_2()
28572878

28582879
import mock
28592880
from io import StringIO
2860-
2881+
2882+
@mock.patch("scapy.arch.get_if_addr")
28612883
@mock.patch("scapy.arch.unix.os")
2862-
def test_freebsd_13(mock_os):
2884+
def test_freebsd_13(mock_os, mock_get_if_addr):
28632885
"""Test read_routes() on FreeBSD 13"""
28642886
# 'netstat -rnW -f inet' output
28652887
netstat_output = u"""
@@ -2875,6 +2897,13 @@ default 10.0.0.1 UGS 3 1500 vtnet0
28752897
# Mocked file descriptor
28762898
strio = StringIO(netstat_output)
28772899
mock_os.popen = mock.MagicMock(return_value=strio)
2900+
# Mocked get_if_addr() behavior
2901+
def se_get_if_addr(iface):
2902+
"""Perform specific side effects"""
2903+
if iface == "vtnet0":
2904+
return "10.0.0.1"
2905+
return "1.2.3.4"
2906+
mock_get_if_addr.side_effect = se_get_if_addr
28782907
# Test the function
28792908
from scapy.arch.unix import read_routes
28802909
routes = read_routes()

0 commit comments

Comments
 (0)