Skip to content

Commit 7c88cbf

Browse files
committed
implemented basic tests
1 parent cb29536 commit 7c88cbf

File tree

8 files changed

+603
-7
lines changed

8 files changed

+603
-7
lines changed

nbs/__init__.py

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,8 @@
55

66
class NetBoxScanner(object):
77

8-
def __init__(self, netbox, hosts, tag, cleanup):
9-
self.netbox = api(
10-
netbox['address'],
11-
netbox['token'],
12-
ssl_verify=netbox.getboolean('tls_verify')
13-
)
8+
def __init__(self, address, token, tls_verify, hosts, tag, cleanup):
9+
self.netbox = api(address, token, ssl_verify=tls_verify)
1410
self.hosts = hosts
1511
self.tag = tag
1612
self.cleanup = cleanup

netbox-scanner.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,9 @@ def cmd_nmap(): # nmap handler
5353
h = Nmap(nmap['path'], nmap['unknown'])
5454
h.run()
5555
scan = NetBoxScanner(
56-
netbox,
56+
netbox['address'],
57+
netbox['token'],
58+
netbox.getboolean('tls_verify'),
5759
h.hosts,
5860
nmap['tag'],
5961
nmap.getboolean('cleanup')

samples/nmap-1.xml

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
<?xml version="1.0"?>
2+
<?xml-stylesheet href="file:///usr/local/bin/../share/nmap/nmap.xsl" type="text/xsl"?>
3+
<!-- Nmap 5.59BETA3 scan initiated Fri Sep 9 18:33:41 2011 as:
4+
nmap -T4 -A -p 1-1000 -oX - scanme.nmap.org -->
5+
<nmaprun scanner="nmap" args="nmap -T4 -A -p 1-1000 -oX - scanme.nmap.org" start="1315618421"
6+
startstr="Fri Sep 9 18:33:41 2011" version="5.59BETA3" xmloutputversion="1.03">
7+
<scaninfo type="syn" protocol="tcp" numservices="1000" services="1-1000"/>
8+
<verbose level="0"/>
9+
<debugging level="0"/>
10+
<host starttime="1315618421" endtime="1315618434">
11+
<status state="up" reason="echo-reply"/>
12+
<address addr="74.207.244.221" addrtype="ipv4"/>
13+
<hostnames>
14+
<hostname name="scanme.nmap.org" type="user"/>
15+
<hostname name="li86-221.members.linode.com" type="PTR"/>
16+
</hostnames>
17+
<ports>
18+
<extraports state="closed" count="997">
19+
<extrareasons reason="resets" count="997"/>
20+
</extraports>
21+
<port protocol="tcp" portid="22">
22+
<state state="open" reason="syn-ack" reason_ttl="53"/>
23+
<service name="ssh" product="OpenSSH" version="5.3p1 Debian 3ubuntu7"
24+
extrainfo="protocol 2.0" ostype="Linux" method="probed" conf="10">
25+
<cpe>cpe:/a:openbsd:openssh:5.3p1</cpe>
26+
<cpe>cpe:/o:linux:kernel</cpe>
27+
</service>
28+
<script id="ssh-hostkey"
29+
output="1024 8d:60:f1:7c:ca:b7:3d:0a:d6:67:54:9d:69:d9:b9:dd (DSA)&#xa;
30+
2048 79:f8:09:ac:d4:e2:32:42:10:49:d3:bd:20:82:85:ec (RSA)"/>
31+
</port>
32+
<port protocol="tcp" portid="80">
33+
<state state="open" reason="syn-ack" reason_ttl="53"/>
34+
<service name="http" product="Apache httpd" version="2.2.14"
35+
extrainfo="(Ubuntu)" method="probed" conf="10">
36+
<cpe>cpe:/a:apache:http_server:2.2.14</cpe>
37+
</service>
38+
<script id="http-title" output="Go ahead and ScanMe!"/>
39+
</port>
40+
</ports>
41+
<os>
42+
<portused state="open" proto="tcp" portid="22"/>
43+
<portused state="closed" proto="tcp" portid="1"/>
44+
<portused state="closed" proto="udp" portid="31289"/>
45+
<osclass type="general purpose" vendor="Linux" osfamily="Linux"
46+
osgen="2.6.X" accuracy="100">
47+
<cpe>cpe:/o:linux:linux_kernel:2.6.39</cpe>
48+
</osclass>
49+
<osmatch name="Linux 2.6.39" accuracy="100" line="39278"/>
50+
</os>
51+
<uptime seconds="23450" lastboot="Fri Sep 9 12:03:04 2011"/>
52+
<distance value="11"/>
53+
<tcpsequence index="199" difficulty="Good luck!"
54+
values="49018209,48C3EBED,495A2E7F,493EF30C,48ED43B3,495A9B0C"/>
55+
<ipidsequence class="All zeros" values="0,0,0,0,0,0"/>
56+
<tcptssequence class="1000HZ"
57+
values="165CC09,165CC6E,165CCD2,165CD36,165CD9A,165CE48"/>
58+
<trace port="256" proto="tcp">
59+
<!-- Several hop elements removed for brevity -->
60+
<hop ttl="9" ipaddr="72.52.92.109" rtt="15.69" host="10gigabitethernet1-1.core1.fmt1.he.net"/>
61+
<hop ttl="10" ipaddr="64.62.250.6" rtt="12.06" host="linode-llc.10gigabitethernet2-3.core1.fmt1.he.net"/>
62+
<hop ttl="11" ipaddr="74.207.244.221" rtt="16.55" host="li86-221.members.linode.com"/>
63+
</trace>
64+
<times srtt="26517" rttvar="19989" to="106473"/>
65+
</host>
66+
<runstats>
67+
<finished time="1315618434" timestr="Fri Sep 9 18:33:54 2011" elapsed="13.66"
68+
summary="Nmap done at Fri Sep 9 18:33:54 2011; 1 IP address (1 host up)
69+
scanned in 13.66 seconds" exit="success"/>
70+
<hosts up="1" down="0" total="1"/>
71+
</runstats>
72+
</nmaprun>

samples/nmap-2.xml

Lines changed: 480 additions & 0 deletions
Large diffs are not rendered by default.

tests/__init__.py

Whitespace-only changes.

tests/run_tests.sh

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#!/bin/sh
2+
#
3+
# Before running these tests, you must define some
4+
# environment variables, such as:
5+
#
6+
# $ export NETBOX_ADDRESS="https..."
7+
# $ export NETBOX_TOKEN="..."
8+
# $ export NMAP_PATH="..."
9+
##
10+
11+
python -m unittest tests.test_netbox
12+
python -m unittest tests.test_nmap

tests/test_netbox.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import unittest
2+
from os import environ
3+
from nbs import NetBoxScanner
4+
5+
6+
class TestRequest(unittest.TestCase):
7+
def test_api(self):
8+
address = environ.get('NETBOX_ADDRESS')
9+
token = environ.get('NETBOX_TOKEN')
10+
11+
netbox = NetBoxScanner(address, token, False, [], 'test', False)
12+
self.assertIsInstance(netbox, NetBoxScanner)
13+
self.assertEqual(netbox.sync(), True)
14+
15+
16+
if __name__ == '__main__':
17+
unittest.main()

tests/test_nmap.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import unittest
2+
from os import environ
3+
from nbs.nmap import Nmap
4+
5+
6+
class TestRequest(unittest.TestCase):
7+
def test_api(self):
8+
path = environ.get('NMAP_PATH')
9+
10+
nmap = Nmap(path, 'test')
11+
self.assertIsInstance(nmap, Nmap)
12+
nmap.run()
13+
self.assertIsInstance(nmap.hosts, list)
14+
15+
16+
if __name__ == '__main__':
17+
unittest.main()

0 commit comments

Comments
 (0)