|
2 | 2 |
|
3 | 3 | import unittest |
4 | 4 | import os |
5 | | -from libnmap import NmapParser, NmapParserException |
| 5 | +from libnmap.parser import NmapParser, NmapParserException |
6 | 6 |
|
7 | 7 |
|
8 | 8 | class TestNmapParser(unittest.TestCase): |
@@ -35,47 +35,47 @@ def setUp(self): |
35 | 35 | self.flist = self.flist_full |
36 | 36 |
|
37 | 37 | self.ports_string = """<ports><extraports state="closed" count="996"> |
38 | | -<extrareasons reason="resets" count="996"/> |
39 | | -</extraports> |
40 | | -<port protocol="tcp" portid="22"> |
41 | | - <state state="open" reason="syn-ack" reason_ttl="53"/> |
42 | | - <service name="ssh" method="table" conf="3"/> |
43 | | -</port> |
44 | | -<port protocol="tcp" portid="25"> |
45 | | - <state state="filtered" reason="admin-prohibited" \ |
46 | | - reason_ttl="253" reason_ip="109.133.192.1"/> |
47 | | -<service name="smtp" method="table" conf="3"/> |
48 | | -</port> |
49 | | -<port protocol="tcp" portid="80"> |
50 | | - <state state="open" reason="syn-ack" reason_ttl="51"/> |
51 | | - <service name="http" method="table" conf="3"/> |
52 | | -</port> |
53 | | -<port protocol="tcp" portid="9929"> |
54 | | - <state state="open" reason="syn-ack" reason_ttl="53"/> |
55 | | - <service name="nping-echo" method="table" conf="3"/> |
56 | | -</port> |
57 | | -</ports>""" |
| 38 | + <extrareasons reason="resets" count="996"/> |
| 39 | + </extraports> |
| 40 | + <port protocol="tcp" portid="22"> |
| 41 | + <state state="open" reason="syn-ack" reason_ttl="53"/> |
| 42 | + <service name="ssh" method="table" conf="3"/> |
| 43 | + </port> |
| 44 | + <port protocol="tcp" portid="25"> |
| 45 | + <state state="filtered" reason="admin-prohibited" \ |
| 46 | + reason_ttl="253" reason_ip="109.133.192.1"/> |
| 47 | + <service name="smtp" method="table" conf="3"/> |
| 48 | + </port> |
| 49 | + <port protocol="tcp" portid="80"> |
| 50 | + <state state="open" reason="syn-ack" reason_ttl="51"/> |
| 51 | + <service name="http" method="table" conf="3"/> |
| 52 | + </port> |
| 53 | + <port protocol="tcp" portid="9929"> |
| 54 | + <state state="open" reason="syn-ack" reason_ttl="53"/> |
| 55 | + <service name="nping-echo" method="table" conf="3"/> |
| 56 | + </port> |
| 57 | + </ports>""" |
58 | 58 |
|
59 | 59 | self.ports_string2 = """<ports><extraports state="closed" count="996"> |
60 | | -<extrareasons reason="resets" count="996"/> |
61 | | -</extraports> |
62 | | -<port protocol="tcp" portid="A2"> |
63 | | - <state state="open" reason="syn-ack" reason_ttl="53"/> |
64 | | - <service name="ssh" method="table" conf="3"/> |
65 | | -</port> |
66 | | -<port protocol="tcp" portid="25"> |
67 | | - <state state="filtered" reason="admin-prohibited" \ |
68 | | - reason_ttl="253" reason_ip="109.133.192.1"/> |
69 | | - <service name="smtp" method="table" conf="3"/> |
70 | | -</port> |
71 | | -<port protocol="tcp" portid="80"> |
72 | | - <state state="open" reason="syn-ack" reason_ttl="51"/>i |
73 | | - <service name="http" method="table" conf="3"/></port> |
74 | | -<port protocol="tcp" portid="9929"> |
75 | | - <state state="open" reason="syn-ack" reason_ttl="53"/> |
76 | | - <service name="nping-echo" method="table" conf="3"/> |
77 | | -</port> |
78 | | -</ports>""" |
| 60 | + <extrareasons reason="resets" count="996"/> |
| 61 | + </extraports> |
| 62 | + <port protocol="tcp" portid="A2"> |
| 63 | + <state state="open" reason="syn-ack" reason_ttl="53"/> |
| 64 | + <service name="ssh" method="table" conf="3"/> |
| 65 | + </port> |
| 66 | + <port protocol="tcp" portid="25"> |
| 67 | + <state state="filtered" reason="admin-prohibited" \ |
| 68 | + reason_ttl="253" reason_ip="109.133.192.1"/> |
| 69 | + <service name="smtp" method="table" conf="3"/> |
| 70 | + </port> |
| 71 | + <port protocol="tcp" portid="80"> |
| 72 | + <state state="open" reason="syn-ack" reason_ttl="51"/>i |
| 73 | + <service name="http" method="table" conf="3"/></port> |
| 74 | + <port protocol="tcp" portid="9929"> |
| 75 | + <state state="open" reason="syn-ack" reason_ttl="53"/> |
| 76 | + <service name="nping-echo" method="table" conf="3"/> |
| 77 | + </port> |
| 78 | + </ports>""" |
79 | 79 |
|
80 | 80 | self.port_string = """ |
81 | 81 | <port protocol="tcp" portid="25"> |
@@ -125,51 +125,51 @@ def test_class_parser(self): |
125 | 125 | NmapParser.parse(s) |
126 | 126 |
|
127 | 127 | def test_class_ports_parser(self): |
128 | | - plist = NmapParser.parse_ports(self.ports_string) |
129 | | - self.assertEqual(len(plist), 4) |
130 | | - self.assertEqual(sorted([p.port for p in plist]), |
131 | | - sorted([22, 25, 9929, 80])) |
132 | | - self.assertRaises(ValueError, |
133 | | - NmapParser.parse_ports, |
| 128 | + plist = NmapParser.parse(self.ports_string) |
| 129 | + self.assertEqual(len(plist), 4) |
| 130 | + self.assertEqual(sorted([p.port for p in plist]), |
| 131 | + sorted([22, 25, 9929, 80])) |
| 132 | + self.assertRaises(ValueError, |
| 133 | + NmapParser.parse, |
134 | 134 | self.ports_string2) |
135 | 135 |
|
136 | 136 | def test_class_port_parser(self): |
137 | | - p = NmapParser.parse_port(self.port_string) |
| 137 | + p = NmapParser.parse(self.port_string) |
138 | 138 | self.assertEqual(p.port, 25) |
139 | 139 | self.assertNotEqual(p.state, "open") |
140 | 140 | self.assertEqual(p.state, "filtered") |
141 | 141 | self.assertEqual(p.service, "smtp") |
142 | 142 |
|
143 | 143 | def test_port_except(self): |
144 | 144 | self.assertRaises(ValueError, |
145 | | - NmapParser.parse_port, |
| 145 | + NmapParser.parse, |
146 | 146 | self.port_string2) |
147 | 147 | self.assertRaises(ValueError, |
148 | | - NmapParser.parse_port, |
| 148 | + NmapParser.parse, |
149 | 149 | self.port_string3) |
150 | 150 | self.assertRaises(NmapParserException, |
151 | | - NmapParser.parse_port, |
| 151 | + NmapParser.parse, |
152 | 152 | self.port_string4) |
153 | 153 | self.assertRaises(NmapParserException, |
154 | | - NmapParser.parse_port, |
| 154 | + NmapParser.parse, |
155 | 155 | self.port_string5) |
156 | 156 | self.assertRaises(ValueError, |
157 | | - NmapParser.parse_port, |
| 157 | + NmapParser.parse, |
158 | 158 | self.port_string6) |
159 | 159 | self.assertRaises(NmapParserException, |
160 | | - NmapParser.parse_port, |
| 160 | + NmapParser.parse, |
161 | 161 | self.port_string7) |
162 | 162 | self.assertRaises(NmapParserException, |
163 | | - NmapParser.parse_port, |
| 163 | + NmapParser.parse, |
164 | 164 | self.port_string8) |
165 | 165 | self.assertRaises(NmapParserException, |
166 | | - NmapParser.parse_port, |
| 166 | + NmapParser.parse, |
167 | 167 | self.port_string9) |
168 | 168 |
|
169 | 169 | def test_parser_generic(self): |
170 | 170 | plist = NmapParser.parse_fromstring(self.ports_string) |
171 | | - for ks in plist.keys(): |
172 | | - print plist[ks] |
| 171 | + for p in plist: |
| 172 | + print p |
173 | 173 |
|
174 | 174 | if __name__ == '__main__': |
175 | 175 | test_suite = ['test_class_parser', 'test_class_ports_parser', |
|
0 commit comments