@@ -2320,7 +2320,7 @@ default link#11 UCSI 1 0 bridge1
2320
2320
def se_get_if_addr(iface):
2321
2321
"""Perform specific side effects"""
2322
2322
if iface == "bridge1":
2323
- raise OSError("Device not configured")
2323
+ return "0.0.0.0"
2324
2324
return "1.2.3.4"
2325
2325
mock_get_if_addr.side_effect = se_get_if_addr
2326
2326
# Test the function
@@ -2457,9 +2457,10 @@ test_osx_10_15_ipv4()
2457
2457
import mock
2458
2458
from io import StringIO
2459
2459
2460
+ @mock.patch("scapy.arch.get_if_addr")
2460
2461
@mock.patch("scapy.arch.unix.OPENBSD")
2461
2462
@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 ):
2463
2464
"""Test read_routes() on OpenBSD 6.3"""
2464
2465
# 'netstat -rn -f inet' output
2465
2466
netstat_output = u"""
@@ -2489,6 +2490,16 @@ default 10.0.1.254 UGS 0 0 - 8 bge0
2489
2490
2490
2491
# Mocked OpenBSD parsing behavior
2491
2492
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
+
2492
2503
# Test the function
2493
2504
from scapy.arch.unix import read_routes
2494
2505
return read_routes()
@@ -2518,9 +2529,10 @@ from io import StringIO
2518
2529
2519
2530
# Mocked Solaris 11.1 parsing behavior
2520
2531
2532
+ @mock.patch("scapy.arch.get_if_addr")
2521
2533
@mock.patch("scapy.arch.unix.SOLARIS", True)
2522
2534
@mock.patch("scapy.arch.unix.os")
2523
- def test_solaris_111(mock_os):
2535
+ def test_solaris_111(mock_os, mock_get_if_addr ):
2524
2536
"""Test read_routes() on Solaris 11.1"""
2525
2537
# 'netstat -rvn -f inet' output
2526
2538
netstat_output = u"""
@@ -2536,6 +2548,15 @@ default 0.0.0.0 10.0.2.2 net0 1500 2 UG
2536
2548
mock_os.popen = mock.MagicMock(return_value=strio)
2537
2549
print(scapy.arch.unix.SOLARIS)
2538
2550
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
+
2539
2560
# Test the function
2540
2561
from scapy.arch.unix import read_routes
2541
2562
return read_routes()
@@ -2857,9 +2878,10 @@ test_freebsd_10_2()
2857
2878
2858
2879
import mock
2859
2880
from io import StringIO
2860
-
2881
+
2882
+ @mock.patch("scapy.arch.get_if_addr")
2861
2883
@mock.patch("scapy.arch.unix.os")
2862
- def test_freebsd_13(mock_os):
2884
+ def test_freebsd_13(mock_os, mock_get_if_addr ):
2863
2885
"""Test read_routes() on FreeBSD 13"""
2864
2886
# 'netstat -rnW -f inet' output
2865
2887
netstat_output = u"""
@@ -2875,6 +2897,13 @@ default 10.0.0.1 UGS 3 1500 vtnet0
2875
2897
# Mocked file descriptor
2876
2898
strio = StringIO(netstat_output)
2877
2899
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
2878
2907
# Test the function
2879
2908
from scapy.arch.unix import read_routes
2880
2909
routes = read_routes()
0 commit comments