xref: /aosp_15_r20/external/scapy/test/nmap.uts (revision 7dc08ffc4802948ccbc861daaf1e81c405c2c4bd)
1*7dc08ffcSJunyu Lai% Regression tests for Scapy Nmap module
2*7dc08ffcSJunyu Lai
3*7dc08ffcSJunyu Lai~ nmap
4*7dc08ffcSJunyu Lai
5*7dc08ffcSJunyu Lai############
6*7dc08ffcSJunyu Lai############
7*7dc08ffcSJunyu Lai+ Basic Nmap OS fingerprints tests
8*7dc08ffcSJunyu Lai
9*7dc08ffcSJunyu Lai= Module loading
10*7dc08ffcSJunyu Laiload_module('nmap')
11*7dc08ffcSJunyu Lai
12*7dc08ffcSJunyu Lai= Test functions
13*7dc08ffcSJunyu Lai
14*7dc08ffcSJunyu Laid = nmap_udppacket_sig(IP()/UDP(), IP(raw(IP()/ICMP(type=3, code=2)/IPerror()/UDPerror())))
15*7dc08ffcSJunyu Laiassert len(d) == 9
16*7dc08ffcSJunyu Lai
17*7dc08ffcSJunyu Laid = nmap_tcppacket_sig(IP()/TCP())
18*7dc08ffcSJunyu Laiassert len(d) == 5
19*7dc08ffcSJunyu Lai
20*7dc08ffcSJunyu Lai= Fetch database
21*7dc08ffcSJunyu Laifrom __future__ import print_function
22*7dc08ffcSJunyu Laitry:
23*7dc08ffcSJunyu Lai    from urllib.request import urlopen
24*7dc08ffcSJunyu Laiexcept ImportError:
25*7dc08ffcSJunyu Lai    from urllib2 import urlopen
26*7dc08ffcSJunyu Lai
27*7dc08ffcSJunyu Laifor i in range(10):
28*7dc08ffcSJunyu Lai    try:
29*7dc08ffcSJunyu Lai        open('nmap-os-fingerprints', 'wb').write(urlopen('https://raw.githubusercontent.com/nmap/nmap/9efe1892/nmap-os-fingerprints').read())
30*7dc08ffcSJunyu Lai        break
31*7dc08ffcSJunyu Lai    except:
32*7dc08ffcSJunyu Lai        pass
33*7dc08ffcSJunyu Lai
34*7dc08ffcSJunyu Laiconf.nmap_base = 'nmap-os-fingerprints'
35*7dc08ffcSJunyu Lai
36*7dc08ffcSJunyu Lai= Database loading
37*7dc08ffcSJunyu Laiassert len(nmap_kdb.get_base()) > 100
38*7dc08ffcSJunyu Lai
39*7dc08ffcSJunyu Lai= fingerprint test: www.secdev.org
40*7dc08ffcSJunyu Lai~ netaccess
41*7dc08ffcSJunyu Laiscore, fprint = nmap_fp('www.secdev.org')
42*7dc08ffcSJunyu Laiprint(score, fprint)
43*7dc08ffcSJunyu Laiassert score > 0.5
44*7dc08ffcSJunyu Laiassert fprint
45*7dc08ffcSJunyu Lai
46*7dc08ffcSJunyu Lai= fingerprint test: gateway
47*7dc08ffcSJunyu Lai~ netaccess
48*7dc08ffcSJunyu Laiscore, fprint = nmap_fp(conf.route.route('0.0.0.0')[2])
49*7dc08ffcSJunyu Laiprint(score, fprint)
50*7dc08ffcSJunyu Laiassert score > 0.5
51*7dc08ffcSJunyu Laiassert fprint
52*7dc08ffcSJunyu Lai
53*7dc08ffcSJunyu Lai= fingerprint test: to text
54*7dc08ffcSJunyu Lai~  netaccess
55*7dc08ffcSJunyu Lai
56*7dc08ffcSJunyu Laiimport re as re_
57*7dc08ffcSJunyu Lai
58*7dc08ffcSJunyu Laia = nmap_sig("www.secdev.org", 80, 81)
59*7dc08ffcSJunyu Laia
60*7dc08ffcSJunyu Laifor x in nmap_sig2txt(a).split("\n"):
61*7dc08ffcSJunyu Lai    assert re_.match(r"\w{2,4}\(.*\)", x)
62*7dc08ffcSJunyu Lai
63*7dc08ffcSJunyu Lai= nmap_udppacket_sig test: www.google.com
64*7dc08ffcSJunyu Lai~ netaccess
65*7dc08ffcSJunyu Lai
66*7dc08ffcSJunyu Laia = nmap_sig("www.google.com", ucport=80)
67*7dc08ffcSJunyu Laiassert len(a) > 3
68*7dc08ffcSJunyu Laiassert len(a["PU"]) > 0
69*7dc08ffcSJunyu Lai
70*7dc08ffcSJunyu Lai+ Nmap errors triggering
71*7dc08ffcSJunyu Lai
72*7dc08ffcSJunyu Lai= Nmap base not available
73*7dc08ffcSJunyu Lai
74*7dc08ffcSJunyu Lainmap_kdb.filename = "invalid"
75*7dc08ffcSJunyu Lainmap_kdb.reload()
76*7dc08ffcSJunyu Laiassert nmap_kdb.filename == None
77*7dc08ffcSJunyu Lai
78*7dc08ffcSJunyu Lai= Clear temp files
79*7dc08ffcSJunyu Laitry:
80*7dc08ffcSJunyu Lai    os.remove('nmap-os-fingerprints')
81*7dc08ffcSJunyu Laiexcept:
82*7dc08ffcSJunyu Lai    pass
83*7dc08ffcSJunyu Lai
84