Skip to content

Commit

Permalink
implemented basic tests
Browse files Browse the repository at this point in the history
  • Loading branch information
lopes committed May 27, 2020
1 parent cb29536 commit 7c88cbf
Show file tree
Hide file tree
Showing 8 changed files with 603 additions and 7 deletions.
8 changes: 2 additions & 6 deletions nbs/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,8 @@

class NetBoxScanner(object):

def __init__(self, netbox, hosts, tag, cleanup):
self.netbox = api(
netbox['address'],
netbox['token'],
ssl_verify=netbox.getboolean('tls_verify')
)
def __init__(self, address, token, tls_verify, hosts, tag, cleanup):
self.netbox = api(address, token, ssl_verify=tls_verify)
self.hosts = hosts
self.tag = tag
self.cleanup = cleanup
Expand Down
4 changes: 3 additions & 1 deletion netbox-scanner.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,9 @@ def cmd_nmap(): # nmap handler
h = Nmap(nmap['path'], nmap['unknown'])
h.run()
scan = NetBoxScanner(
netbox,
netbox['address'],
netbox['token'],
netbox.getboolean('tls_verify'),
h.hosts,
nmap['tag'],
nmap.getboolean('cleanup')
Expand Down
72 changes: 72 additions & 0 deletions samples/nmap-1.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
<?xml version="1.0"?>
<?xml-stylesheet href="file:///usr/local/bin/../share/nmap/nmap.xsl" type="text/xsl"?>
<!-- Nmap 5.59BETA3 scan initiated Fri Sep 9 18:33:41 2011 as:
nmap -T4 -A -p 1-1000 -oX - scanme.nmap.org -->
<nmaprun scanner="nmap" args="nmap -T4 -A -p 1-1000 -oX - scanme.nmap.org" start="1315618421"
startstr="Fri Sep 9 18:33:41 2011" version="5.59BETA3" xmloutputversion="1.03">
<scaninfo type="syn" protocol="tcp" numservices="1000" services="1-1000"/>
<verbose level="0"/>
<debugging level="0"/>
<host starttime="1315618421" endtime="1315618434">
<status state="up" reason="echo-reply"/>
<address addr="74.207.244.221" addrtype="ipv4"/>
<hostnames>
<hostname name="scanme.nmap.org" type="user"/>
<hostname name="li86-221.members.linode.com" type="PTR"/>
</hostnames>
<ports>
<extraports state="closed" count="997">
<extrareasons reason="resets" count="997"/>
</extraports>
<port protocol="tcp" portid="22">
<state state="open" reason="syn-ack" reason_ttl="53"/>
<service name="ssh" product="OpenSSH" version="5.3p1 Debian 3ubuntu7"
extrainfo="protocol 2.0" ostype="Linux" method="probed" conf="10">
<cpe>cpe:/a:openbsd:openssh:5.3p1</cpe>
<cpe>cpe:/o:linux:kernel</cpe>
</service>
<script id="ssh-hostkey"
output="1024 8d:60:f1:7c:ca:b7:3d:0a:d6:67:54:9d:69:d9:b9:dd (DSA)&#xa;
2048 79:f8:09:ac:d4:e2:32:42:10:49:d3:bd:20:82:85:ec (RSA)"/>
</port>
<port protocol="tcp" portid="80">
<state state="open" reason="syn-ack" reason_ttl="53"/>
<service name="http" product="Apache httpd" version="2.2.14"
extrainfo="(Ubuntu)" method="probed" conf="10">
<cpe>cpe:/a:apache:http_server:2.2.14</cpe>
</service>
<script id="http-title" output="Go ahead and ScanMe!"/>
</port>
</ports>
<os>
<portused state="open" proto="tcp" portid="22"/>
<portused state="closed" proto="tcp" portid="1"/>
<portused state="closed" proto="udp" portid="31289"/>
<osclass type="general purpose" vendor="Linux" osfamily="Linux"
osgen="2.6.X" accuracy="100">
<cpe>cpe:/o:linux:linux_kernel:2.6.39</cpe>
</osclass>
<osmatch name="Linux 2.6.39" accuracy="100" line="39278"/>
</os>
<uptime seconds="23450" lastboot="Fri Sep 9 12:03:04 2011"/>
<distance value="11"/>
<tcpsequence index="199" difficulty="Good luck!"
values="49018209,48C3EBED,495A2E7F,493EF30C,48ED43B3,495A9B0C"/>
<ipidsequence class="All zeros" values="0,0,0,0,0,0"/>
<tcptssequence class="1000HZ"
values="165CC09,165CC6E,165CCD2,165CD36,165CD9A,165CE48"/>
<trace port="256" proto="tcp">
<!-- Several hop elements removed for brevity -->
<hop ttl="9" ipaddr="72.52.92.109" rtt="15.69" host="10gigabitethernet1-1.core1.fmt1.he.net"/>
<hop ttl="10" ipaddr="64.62.250.6" rtt="12.06" host="linode-llc.10gigabitethernet2-3.core1.fmt1.he.net"/>
<hop ttl="11" ipaddr="74.207.244.221" rtt="16.55" host="li86-221.members.linode.com"/>
</trace>
<times srtt="26517" rttvar="19989" to="106473"/>
</host>
<runstats>
<finished time="1315618434" timestr="Fri Sep 9 18:33:54 2011" elapsed="13.66"
summary="Nmap done at Fri Sep 9 18:33:54 2011; 1 IP address (1 host up)
scanned in 13.66 seconds" exit="success"/>
<hosts up="1" down="0" total="1"/>
</runstats>
</nmaprun>
480 changes: 480 additions & 0 deletions samples/nmap-2.xml

Large diffs are not rendered by default.

Empty file added tests/__init__.py
Empty file.
12 changes: 12 additions & 0 deletions tests/run_tests.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#!/bin/sh
#
# Before running these tests, you must define some
# environment variables, such as:
#
# $ export NETBOX_ADDRESS="https..."
# $ export NETBOX_TOKEN="..."
# $ export NMAP_PATH="..."
##

python -m unittest tests.test_netbox
python -m unittest tests.test_nmap
17 changes: 17 additions & 0 deletions tests/test_netbox.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import unittest
from os import environ
from nbs import NetBoxScanner


class TestRequest(unittest.TestCase):
def test_api(self):
address = environ.get('NETBOX_ADDRESS')
token = environ.get('NETBOX_TOKEN')

netbox = NetBoxScanner(address, token, False, [], 'test', False)
self.assertIsInstance(netbox, NetBoxScanner)
self.assertEqual(netbox.sync(), True)


if __name__ == '__main__':
unittest.main()
17 changes: 17 additions & 0 deletions tests/test_nmap.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import unittest
from os import environ
from nbs.nmap import Nmap


class TestRequest(unittest.TestCase):
def test_api(self):
path = environ.get('NMAP_PATH')

nmap = Nmap(path, 'test')
self.assertIsInstance(nmap, Nmap)
nmap.run()
self.assertIsInstance(nmap.hosts, list)


if __name__ == '__main__':
unittest.main()

0 comments on commit 7c88cbf

Please sign in to comment.