xref: /aosp_15_r20/external/scapy/test/linux.uts (revision 7dc08ffc4802948ccbc861daaf1e81c405c2c4bd)
1*7dc08ffcSJunyu Lai% Regression tests for Linux only
2*7dc08ffcSJunyu Lai
3*7dc08ffcSJunyu Lai# More informations at http://www.secdev.org/projects/UTscapy/
4*7dc08ffcSJunyu Lai
5*7dc08ffcSJunyu Lai
6*7dc08ffcSJunyu Lai############
7*7dc08ffcSJunyu Lai############
8*7dc08ffcSJunyu Lai
9*7dc08ffcSJunyu Lai+ Linux only test
10*7dc08ffcSJunyu Lai
11*7dc08ffcSJunyu Lai= TCP client automaton
12*7dc08ffcSJunyu Lai~ automaton netaccess linux needs_root
13*7dc08ffcSJunyu Lai* This test retries on failure because it often fails
14*7dc08ffcSJunyu Lai
15*7dc08ffcSJunyu Laifrom __future__ import print_function
16*7dc08ffcSJunyu Laiimport os
17*7dc08ffcSJunyu Laiimport time
18*7dc08ffcSJunyu Laiimport signal
19*7dc08ffcSJunyu Lai
20*7dc08ffcSJunyu Laifrom scapy.modules.six.moves import range
21*7dc08ffcSJunyu Lai
22*7dc08ffcSJunyu Laidef handler(signum, stack_frame):
23*7dc08ffcSJunyu Lai    raise Exception("Timer expired !")
24*7dc08ffcSJunyu Lai
25*7dc08ffcSJunyu Laitmp = signal.signal(signal.SIGALRM, handler)
26*7dc08ffcSJunyu Lai
27*7dc08ffcSJunyu LaiSECDEV_IP4 = "203.178.141.194"
28*7dc08ffcSJunyu LaiIPTABLE_RULE = "iptables -%c INPUT -s %s -p tcp --sport 80 -j DROP"
29*7dc08ffcSJunyu Lai
30*7dc08ffcSJunyu Lai# Drop packets from SECDEV_IP4
31*7dc08ffcSJunyu Laiassert(os.system(IPTABLE_RULE % ('A', SECDEV_IP4)) == 0)
32*7dc08ffcSJunyu Lai
33*7dc08ffcSJunyu Laisuccess = False
34*7dc08ffcSJunyu Laifor i in range(10):
35*7dc08ffcSJunyu Lai    tmp = signal.alarm(5)
36*7dc08ffcSJunyu Lai    try:
37*7dc08ffcSJunyu Lai        r, w = os.pipe()
38*7dc08ffcSJunyu Lai        t = TCP_client(SECDEV_IP4, 80, external_fd={ "tcp": (r,w) })
39*7dc08ffcSJunyu Lai        tmp = os.write(w, b"HEAD / HTTP/1.0\r\n\r\n")
40*7dc08ffcSJunyu Lai        t.runbg()
41*7dc08ffcSJunyu Lai        time.sleep(0.5)
42*7dc08ffcSJunyu Lai        response = os.read(r, 4096)
43*7dc08ffcSJunyu Lai        tmp = signal.alarm(0)  # cancel the alarm
44*7dc08ffcSJunyu Lai        t.stop()
45*7dc08ffcSJunyu Lai        os.close(r)
46*7dc08ffcSJunyu Lai        os.close(w)
47*7dc08ffcSJunyu Lai        if response.startswith(b"HTTP/1.1 200 OK"):
48*7dc08ffcSJunyu Lai            success = True
49*7dc08ffcSJunyu Lai            break
50*7dc08ffcSJunyu Lai        else:
51*7dc08ffcSJunyu Lai            time.sleep(0.5)
52*7dc08ffcSJunyu Lai    except Exception as e:
53*7dc08ffcSJunyu Lai        print(e)
54*7dc08ffcSJunyu Lai
55*7dc08ffcSJunyu Lai# Remove the iptables rule
56*7dc08ffcSJunyu Laiassert(os.system(IPTABLE_RULE % ('D', SECDEV_IP4)) == 0)
57*7dc08ffcSJunyu Lai
58*7dc08ffcSJunyu Laiassert(success)
59*7dc08ffcSJunyu Lai
60*7dc08ffcSJunyu Lai= L3RawSocket
61*7dc08ffcSJunyu Lai~ netaccess IP ICMP linux needs_root
62*7dc08ffcSJunyu Lai
63*7dc08ffcSJunyu Laiold_l3socket = conf.L3socket
64*7dc08ffcSJunyu Laiold_debug_dissector = conf.debug_dissector
65*7dc08ffcSJunyu Laiconf.debug_dissector = False
66*7dc08ffcSJunyu Laiconf.L3socket = L3RawSocket
67*7dc08ffcSJunyu Laix = sr1(IP(dst="www.google.com")/ICMP(),timeout=3)
68*7dc08ffcSJunyu Laiconf.debug_dissector = old_debug_dissector
69*7dc08ffcSJunyu Laiconf.L3socket = old_l3socket
70*7dc08ffcSJunyu Laix
71*7dc08ffcSJunyu Laiassert x[IP].ottl() in [32, 64, 128, 255]
72*7dc08ffcSJunyu Laiassert 0 <= x[IP].hops() <= 126
73*7dc08ffcSJunyu Laix is not None and ICMP in x and x[ICMP].type == 0
74*7dc08ffcSJunyu Lai
75*7dc08ffcSJunyu Lai# TODO: fix this test (randomly stuck)
76*7dc08ffcSJunyu Lai# ex: https://travis-ci.org/secdev/scapy/jobs/247473497
77*7dc08ffcSJunyu Lai
78*7dc08ffcSJunyu Lai#= Supersocket _flush_fd
79*7dc08ffcSJunyu Lai#~ needs_root linux
80*7dc08ffcSJunyu Lai#
81*7dc08ffcSJunyu Lai#import select
82*7dc08ffcSJunyu Lai#
83*7dc08ffcSJunyu Lai#from scapy.arch.linux import _flush_fd
84*7dc08ffcSJunyu Lai#socket = conf.L2listen()
85*7dc08ffcSJunyu Lai#select.select([socket],[],[],2)
86*7dc08ffcSJunyu Lai#_flush_fd(socket.ins)
87*7dc08ffcSJunyu Lai
88*7dc08ffcSJunyu Lai= Test legacy attach_filter function
89*7dc08ffcSJunyu Lai~ linux needs_root
90*7dc08ffcSJunyu Laifrom scapy.arch.common import get_bpf_pointer
91*7dc08ffcSJunyu Lai
92*7dc08ffcSJunyu Laiold_pypy = conf.use_pypy
93*7dc08ffcSJunyu Laiconf.use_pypy = True
94*7dc08ffcSJunyu Lai
95*7dc08ffcSJunyu Laitcpdump_lines = ['12\n', '40 0 0 12\n', '21 0 5 34525\n', '48 0 0 20\n', '21 6 0 6\n', '21 0 6 44\n', '48 0 0 54\n', '21 3 4 6\n', '21 0 3 2048\n', '48 0 0 23\n', '21 0 1 6\n', '6 0 0 1600\n', '6 0 0 0\n']
96*7dc08ffcSJunyu Laipointer = get_bpf_pointer(tcpdump_lines)
97*7dc08ffcSJunyu Laiassert six.PY3 or isinstance(pointer, str)
98*7dc08ffcSJunyu Laiassert six.PY3 or len(pointer) > 1
99*7dc08ffcSJunyu Lai
100*7dc08ffcSJunyu Laiconf.use_pypy = old_pypy
101*7dc08ffcSJunyu Lai
102*7dc08ffcSJunyu Lai= Interface aliases & sub-interfaces
103*7dc08ffcSJunyu Lai~ linux needs_root
104*7dc08ffcSJunyu Lai
105*7dc08ffcSJunyu Laiimport os
106*7dc08ffcSJunyu Laiexit_status = os.system("ip link add name scapy0 type dummy")
107*7dc08ffcSJunyu Laiexit_status = os.system("ip addr add 192.0.2.1/24 dev scapy0")
108*7dc08ffcSJunyu Laiexit_status = os.system("ip link set scapy0 up")
109*7dc08ffcSJunyu Laiexit_status = os.system("ifconfig scapy0:0 inet 198.51.100.1/24 up")
110*7dc08ffcSJunyu Laiexit_status = os.system("ip addr show scapy0")
111*7dc08ffcSJunyu Laiprint(get_if_list())
112*7dc08ffcSJunyu Laiconf.route.resync()
113*7dc08ffcSJunyu Laiprint(conf.route.routes)
114*7dc08ffcSJunyu Laiassert(conf.route.route("198.51.100.254") == ("scapy0", "198.51.100.1", "0.0.0.0"))
115*7dc08ffcSJunyu Lairoute_alias = (3325256704, 4294967040, "0.0.0.0", "scapy0", "198.51.100.1", 0)
116*7dc08ffcSJunyu Laiassert(route_alias in conf.route.routes)
117*7dc08ffcSJunyu Laiexit_status = os.system("ip link add link scapy0 name scapy0.42 type vlan id 42")
118*7dc08ffcSJunyu Laiexit_status = os.system("ip addr add 203.0.113.42/24 dev scapy0.42")
119*7dc08ffcSJunyu Laiexit_status = os.system("ip link set scapy0.42 up")
120*7dc08ffcSJunyu Laiexit_status = os.system("ip route add 192.0.2.43/32 via 203.0.113.41")
121*7dc08ffcSJunyu Laiprint(get_if_list())
122*7dc08ffcSJunyu Laiconf.route.resync()
123*7dc08ffcSJunyu Laiprint(conf.route.routes)
124*7dc08ffcSJunyu Laiassert(conf.route.route("192.0.2.43") == ("scapy0.42", "203.0.113.42", "203.0.113.41"))
125*7dc08ffcSJunyu Lairoute_specific = (3221226027, 4294967295, "203.0.113.41", "scapy0.42", "203.0.113.42", 0)
126*7dc08ffcSJunyu Laiassert(route_specific in conf.route.routes)
127*7dc08ffcSJunyu Laiexit_status = os.system("ip link del name dev scapy0")
128