Skip to content

Commit dff786b

Browse files
committed
added unit test for os fingerprint class
1 parent 6b3b191 commit dff786b

File tree

2 files changed

+45
-1
lines changed

2 files changed

+45
-1
lines changed

libnmap/objects.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,10 @@ def __init__(self, starttime='', endtime='', address=None, status=None,
101101
self._address = address if address is not None else {}
102102
self._services = services if services is not None else []
103103
self._extras = extras if extras is not None else {}
104-
self.os = self.NmapOSFingerprint(self._extras['os'])
104+
self._osfingerprinted = False
105+
if 'os' in self._extras:
106+
self.os = self.NmapOSFingerprint(self._extras['os'])
107+
self._osfingerprinted = True
105108

106109
def __eq__(self, other):
107110
"""
@@ -312,6 +315,15 @@ def os_match_probabilities(self):
312315
pass
313316
return rval
314317

318+
@property
319+
def os_fingerprinted(self):
320+
"""
321+
Specify if the host has OS fingerprint data available
322+
323+
:return: Boolean
324+
"""
325+
return self._osfingerprinted
326+
315327
@property
316328
def os_fingerprint(self):
317329
"""

libnmap/test/test_fp.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#!/usr/bin/env python
2+
3+
import unittest
4+
import os
5+
from libnmap.parser import NmapParser, NmapParserException
6+
7+
8+
class TestNmapFP(unittest.TestCase):
9+
def setUp(self):
10+
fdir = os.path.dirname(os.path.realpath(__file__))
11+
self.flist_full = [{ 'file': "%s/%s" % (fdir, 'files/1_os_banner_scripts.xml'), 'os': 1},
12+
{ 'file': "%s/%s" % (fdir, 'files/2_hosts_version.xml'), 'os': 1},
13+
{ 'file': "%s/%s" % (fdir, 'files/1_hosts_banner_ports_notsyn.xml'), 'os': 0},
14+
{ 'file': "%s/%s" % (fdir, 'files/1_hosts_banner.xml'), 'os': 0},
15+
{ 'file': "%s/%s" % (fdir, 'files/1_hosts_down.xml'), 'os': 0}]
16+
self.flist = self.flist_full
17+
18+
def test_fp(self):
19+
for file_e in self.flist_full:
20+
rep = NmapParser.parse_fromfile(file_e['file'])
21+
for _host in rep.hosts:
22+
if file_e['os'] != 0:
23+
self.assertTrue(_host.os_fingerprinted)
24+
elif file_e['os'] == 0:
25+
self.assertFalse(_host.os_fingerprinted)
26+
else:
27+
raise Exception
28+
29+
if __name__ == '__main__':
30+
test_suite = ['test_fp']
31+
suite = unittest.TestSuite(map(TestNmapFP, test_suite))
32+
test_result = unittest.TextTestRunner(verbosity=2).run(suite)

0 commit comments

Comments
 (0)