File: test_report_diff.py

package info (click to toggle)
python-libnmap 0.7.3-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,416 kB
  • sloc: xml: 5,572; python: 4,299; makefile: 149
file content (91 lines) | stat: -rw-r--r-- 2,802 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#!/usr/bin/env python
# -*- coding: utf-8 -*-

import os
import unittest

from libnmap.parser import NmapParser


class TestNmapReportDiff(unittest.TestCase):
    def setUp(self):
        fdir = os.path.dirname(os.path.realpath(__file__))
        self.flist_full = [
            {"file": "%s/%s" % (fdir, "files/2_hosts.xml"), "hosts": 2},
            {"file": "%s/%s" % (fdir, "files/1_hosts.xml"), "hosts": 1},
        ]
        self.flist = self.flist_full

    def test_diff_host_list(self):
        fdir = os.path.dirname(os.path.realpath(__file__))
        r1 = NmapParser.parse_fromfile("%s/%s" % (fdir, "files/1_hosts.xml"))
        r2 = NmapParser.parse_fromfile("%s/%s" % (fdir, "files/2_hosts.xml"))
        r3 = NmapParser.parse_fromfile("%s/%s" % (fdir, "files/1_hosts.xml"))
        r4 = NmapParser.parse_fromfile(
            "%s/%s" % (fdir, "files/2_hosts_achange.xml")
        )

        d1 = r1.diff(r2)
        self.assertEqual(
            d1.changed(),
            set(
                [
                    "hosts_total",
                    "commandline",
                    "hosts_up",
                    "scan_type",
                    "elapsed",
                ]
            ),
        )
        self.assertEqual(
            d1.unchanged(),
            set(["hosts_down", "version", "NmapHost::127.0.0.1"]),
        )
        self.assertEqual(d1.removed(), set(["NmapHost::74.207.244.221"]))

        d2 = r1.diff(r3)
        self.assertEqual(d2.changed(), set([]))
        self.assertEqual(
            d2.unchanged(),
            set(
                [
                    "hosts_total",
                    "commandline",
                    "hosts_up",
                    "NmapHost::127.0.0.1",
                    "elapsed",
                    "version",
                    "scan_type",
                    "hosts_down",
                ]
            ),
        )
        self.assertEqual(d2.added(), set([]))
        self.assertEqual(d2.removed(), set([]))

        d3 = r2.diff(r4)
        self.assertEqual(d3.changed(), set(["NmapHost::127.0.0.1"]))
        self.assertEqual(
            d3.unchanged(),
            set(
                [
                    "hosts_total",
                    "commandline",
                    "hosts_up",
                    "NmapHost::74.207.244.221",
                    "version",
                    "elapsed",
                    "scan_type",
                    "hosts_down",
                ]
            ),
        )
        self.assertEqual(d3.added(), set([]))
        self.assertEqual(d3.removed(), set([]))


if __name__ == "__main__":
    test_suite = ["test_diff_host_list"]
    suite = unittest.TestSuite(map(TestNmapReportDiff, test_suite))
    test_result = unittest.TextTestRunner(verbosity=2).run(suite)