| 1 | #!/usr/bin/env python |
|---|
| 2 | # -*- coding: utf-8 -*- |
|---|
| 3 | |
|---|
| 4 | # Copyright (C) 2009 Adriano Monteiro Marques. |
|---|
| 5 | # |
|---|
| 6 | # Author: Bartosz SKOWRON <getxsick at gmail dot com> |
|---|
| 7 | # |
|---|
| 8 | # This library is free software; you can redistribute it and/or modify |
|---|
| 9 | # it under the terms of the GNU Lesser General Public License as published |
|---|
| 10 | # by the Free Software Foundation; either version 2.1 of the License, or |
|---|
| 11 | # (at your option) any later version. |
|---|
| 12 | # |
|---|
| 13 | # This library is distributed in the hope that it will be useful, but |
|---|
| 14 | # WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY |
|---|
| 15 | # or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public |
|---|
| 16 | # License for more details. |
|---|
| 17 | # |
|---|
| 18 | # You should have received a copy of the GNU Lesser General Public License |
|---|
| 19 | # along with this library; if not, write to the Free Software Foundation, |
|---|
| 20 | # Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
|---|
| 21 | |
|---|
| 22 | import py.test |
|---|
| 23 | |
|---|
| 24 | from cStringIO import StringIO |
|---|
| 25 | import tempfile |
|---|
| 26 | import os |
|---|
| 27 | |
|---|
| 28 | import umpa |
|---|
| 29 | from umpa.protocols import IP, TCP, Payload |
|---|
| 30 | from umpa.protocols._fields import Flags |
|---|
| 31 | from umpa.extensions import XML |
|---|
| 32 | |
|---|
| 33 | class TestExtensionXML(object): |
|---|
| 34 | example_xml = """<?xml version="1.0" ?> |
|---|
| 35 | <UMPA> |
|---|
| 36 | <packet id="0" strict="True"> |
|---|
| 37 | <protocol class="umpa.protocols.IP.IP"> |
|---|
| 38 | <_version type="int"> |
|---|
| 39 | 4 |
|---|
| 40 | </_version> |
|---|
| 41 | <_ihl type="NoneType"> |
|---|
| 42 | None |
|---|
| 43 | </_ihl> |
|---|
| 44 | <type_of_service type="bits"> |
|---|
| 45 | <precedence0 type="bool"> |
|---|
| 46 | False |
|---|
| 47 | </precedence0> |
|---|
| 48 | <precedence1 type="bool"> |
|---|
| 49 | False |
|---|
| 50 | </precedence1> |
|---|
| 51 | <precedence2 type="bool"> |
|---|
| 52 | False |
|---|
| 53 | </precedence2> |
|---|
| 54 | <delay type="bool"> |
|---|
| 55 | False |
|---|
| 56 | </delay> |
|---|
| 57 | <throughput type="bool"> |
|---|
| 58 | False |
|---|
| 59 | </throughput> |
|---|
| 60 | <relibility type="bool"> |
|---|
| 61 | False |
|---|
| 62 | </relibility> |
|---|
| 63 | <reserved0 type="bool"> |
|---|
| 64 | False |
|---|
| 65 | </reserved0> |
|---|
| 66 | <reserved1 type="bool"> |
|---|
| 67 | False |
|---|
| 68 | </reserved1> |
|---|
| 69 | </type_of_service> |
|---|
| 70 | <_total_length type="NoneType"> |
|---|
| 71 | None |
|---|
| 72 | </_total_length> |
|---|
| 73 | <_identification type="int"> |
|---|
| 74 | 0 |
|---|
| 75 | </_identification> |
|---|
| 76 | <flags type="bits"> |
|---|
| 77 | <reserved type="bool"> |
|---|
| 78 | False |
|---|
| 79 | </reserved> |
|---|
| 80 | <df type="bool"> |
|---|
| 81 | False |
|---|
| 82 | </df> |
|---|
| 83 | <mf type="bool"> |
|---|
| 84 | False |
|---|
| 85 | </mf> |
|---|
| 86 | </flags> |
|---|
| 87 | <_fragment_offset type="int"> |
|---|
| 88 | 0 |
|---|
| 89 | </_fragment_offset> |
|---|
| 90 | <time_to_live type="NoneType"> |
|---|
| 91 | None |
|---|
| 92 | </time_to_live> |
|---|
| 93 | <_protocol type="NoneType"> |
|---|
| 94 | None |
|---|
| 95 | </_protocol> |
|---|
| 96 | <_header_checksum type="int"> |
|---|
| 97 | 0 |
|---|
| 98 | </_header_checksum> |
|---|
| 99 | <source_address type="str"> |
|---|
| 100 | 127.0.0.1 |
|---|
| 101 | </source_address> |
|---|
| 102 | <destination_address type="str"> |
|---|
| 103 | 67.205.14.183 |
|---|
| 104 | </destination_address> |
|---|
| 105 | <options type="bits"/> |
|---|
| 106 | <_padding type="int"> |
|---|
| 107 | 0 |
|---|
| 108 | </_padding> |
|---|
| 109 | </protocol> |
|---|
| 110 | <protocol class="umpa.protocols.TCP.TCP"> |
|---|
| 111 | <source_port type="int"> |
|---|
| 112 | 2958 |
|---|
| 113 | </source_port> |
|---|
| 114 | <destination_port type="int"> |
|---|
| 115 | 0 |
|---|
| 116 | </destination_port> |
|---|
| 117 | <_sequence_number type="NoneType"> |
|---|
| 118 | None |
|---|
| 119 | </_sequence_number> |
|---|
| 120 | <_acknowledgment_number type="NoneType"> |
|---|
| 121 | None |
|---|
| 122 | </_acknowledgment_number> |
|---|
| 123 | <_data_offset type="NoneType"> |
|---|
| 124 | None |
|---|
| 125 | </_data_offset> |
|---|
| 126 | <_reserved type="int"> |
|---|
| 127 | 0 |
|---|
| 128 | </_reserved> |
|---|
| 129 | <control_bits type="bits"> |
|---|
| 130 | <urg type="bool"> |
|---|
| 131 | False |
|---|
| 132 | </urg> |
|---|
| 133 | <ack type="bool"> |
|---|
| 134 | False |
|---|
| 135 | </ack> |
|---|
| 136 | <psh type="bool"> |
|---|
| 137 | False |
|---|
| 138 | </psh> |
|---|
| 139 | <rst type="bool"> |
|---|
| 140 | False |
|---|
| 141 | </rst> |
|---|
| 142 | <syn type="bool"> |
|---|
| 143 | True |
|---|
| 144 | </syn> |
|---|
| 145 | <fin type="bool"> |
|---|
| 146 | False |
|---|
| 147 | </fin> |
|---|
| 148 | </control_bits> |
|---|
| 149 | <_window type="NoneType"> |
|---|
| 150 | None |
|---|
| 151 | </_window> |
|---|
| 152 | <_checksum type="NoneType"> |
|---|
| 153 | None |
|---|
| 154 | </_checksum> |
|---|
| 155 | <_urgent_pointer type="NoneType"> |
|---|
| 156 | None |
|---|
| 157 | </_urgent_pointer> |
|---|
| 158 | <options type="bits"/> |
|---|
| 159 | <_padding type="int"> |
|---|
| 160 | 0 |
|---|
| 161 | </_padding> |
|---|
| 162 | </protocol> |
|---|
| 163 | <protocol class="umpa.protocols.Payload.Payload"> |
|---|
| 164 | <data type="str"> |
|---|
| 165 | this is umpa! |
|---|
| 166 | </data> |
|---|
| 167 | </protocol> |
|---|
| 168 | </packet> |
|---|
| 169 | </UMPA> |
|---|
| 170 | """ |
|---|
| 171 | example_xml2 = """ <packet id="1" strict="True"> |
|---|
| 172 | <protocol class="umpa.protocols.TCP.TCP"> |
|---|
| 173 | <source_port type="int"> |
|---|
| 174 | 123 |
|---|
| 175 | </source_port> |
|---|
| 176 | <destination_port type="int"> |
|---|
| 177 | 321 |
|---|
| 178 | </destination_port> |
|---|
| 179 | <_sequence_number type="NoneType"> |
|---|
| 180 | None |
|---|
| 181 | </_sequence_number> |
|---|
| 182 | <_acknowledgment_number type="NoneType"> |
|---|
| 183 | None |
|---|
| 184 | </_acknowledgment_number> |
|---|
| 185 | <_data_offset type="NoneType"> |
|---|
| 186 | None |
|---|
| 187 | </_data_offset> |
|---|
| 188 | <_reserved type="int"> |
|---|
| 189 | 0 |
|---|
| 190 | </_reserved> |
|---|
| 191 | <control_bits type="bits"> |
|---|
| 192 | <urg type="bool"> |
|---|
| 193 | False |
|---|
| 194 | </urg> |
|---|
| 195 | <ack type="bool"> |
|---|
| 196 | False |
|---|
| 197 | </ack> |
|---|
| 198 | <psh type="bool"> |
|---|
| 199 | False |
|---|
| 200 | </psh> |
|---|
| 201 | <rst type="bool"> |
|---|
| 202 | False |
|---|
| 203 | </rst> |
|---|
| 204 | <syn type="bool"> |
|---|
| 205 | False |
|---|
| 206 | </syn> |
|---|
| 207 | <fin type="bool"> |
|---|
| 208 | False |
|---|
| 209 | </fin> |
|---|
| 210 | </control_bits> |
|---|
| 211 | <_window type="NoneType"> |
|---|
| 212 | None |
|---|
| 213 | </_window> |
|---|
| 214 | <_checksum type="NoneType"> |
|---|
| 215 | None |
|---|
| 216 | </_checksum> |
|---|
| 217 | <_urgent_pointer type="NoneType"> |
|---|
| 218 | None |
|---|
| 219 | </_urgent_pointer> |
|---|
| 220 | <options type="bits"/> |
|---|
| 221 | <_padding type="int"> |
|---|
| 222 | 0 |
|---|
| 223 | </_padding> |
|---|
| 224 | </protocol> |
|---|
| 225 | <protocol class="umpa.protocols.Payload.Payload"> |
|---|
| 226 | <data type="str"> |
|---|
| 227 | another umpa packet! |
|---|
| 228 | </data> |
|---|
| 229 | </protocol> |
|---|
| 230 | </packet> |
|---|
| 231 | </UMPA> |
|---|
| 232 | """ |
|---|
| 233 | |
|---|
| 234 | ip = IP() |
|---|
| 235 | ip.source_address = "127.0.0.1" |
|---|
| 236 | ip.destination_address = "67.205.14.183" |
|---|
| 237 | |
|---|
| 238 | # TCP header |
|---|
| 239 | tcp = TCP() |
|---|
| 240 | tcp.source_port = 2958 |
|---|
| 241 | tcp.destination_port = 0 |
|---|
| 242 | tcp.set_flags('control_bits', syn=True) |
|---|
| 243 | |
|---|
| 244 | # Payload |
|---|
| 245 | data = Payload() |
|---|
| 246 | data.data = "this is umpa!" |
|---|
| 247 | |
|---|
| 248 | # packet |
|---|
| 249 | example_packet = umpa.Packet(ip, tcp, data) |
|---|
| 250 | |
|---|
| 251 | tcp2 = TCP() |
|---|
| 252 | tcp2.source_port = 123 |
|---|
| 253 | tcp2.destination_port = 321 |
|---|
| 254 | tcp2.set_flags('control_bits', syn=False) |
|---|
| 255 | data2 = Payload() |
|---|
| 256 | data2.data = "another umpa packet!" |
|---|
| 257 | example_packet2 = umpa.Packet(tcp2, data2, strict=True) |
|---|
| 258 | |
|---|
| 259 | def test_packet_attrs(self): |
|---|
| 260 | assert hasattr(umpa.Packet, 'save_xml') |
|---|
| 261 | assert hasattr(umpa.Packet, 'load_xml') |
|---|
| 262 | |
|---|
| 263 | def test_xml_load(self): |
|---|
| 264 | f = StringIO() |
|---|
| 265 | f.write(self.example_xml) |
|---|
| 266 | f.seek(0) |
|---|
| 267 | |
|---|
| 268 | packets = XML.load(f) |
|---|
| 269 | for p in packets: |
|---|
| 270 | for i, proto in enumerate(p.protos): |
|---|
| 271 | for fieldname in proto.get_fields_keys(): |
|---|
| 272 | assert proto.get_field(fieldname).fillout() == \ |
|---|
| 273 | self.example_packet.protos[i].get_field(fieldname).fillout() |
|---|
| 274 | |
|---|
| 275 | def test_xml_load_multiple(self): |
|---|
| 276 | l_xml= (self.example_xml, self.example_xml2) |
|---|
| 277 | l_packet = (self.example_packet, self.example_packet2) |
|---|
| 278 | f = StringIO() |
|---|
| 279 | f.write(l_xml[0][:-8]) # </UMPA>\n |
|---|
| 280 | f.write(l_xml[1]) |
|---|
| 281 | f.seek(0) |
|---|
| 282 | |
|---|
| 283 | packets = XML.load(f) |
|---|
| 284 | for j, p in enumerate(packets): |
|---|
| 285 | for i, proto in enumerate(p.protos): |
|---|
| 286 | for fieldname in proto.get_fields_keys(): |
|---|
| 287 | assert proto.get_field(fieldname).fillout() == \ |
|---|
| 288 | l_packet[j].protos[i].get_field(fieldname).fillout() |
|---|
| 289 | |
|---|
| 290 | def test_xml_load_proto_only(self): |
|---|
| 291 | f = StringIO() |
|---|
| 292 | f.write(self.example_xml) |
|---|
| 293 | f.seek(0) |
|---|
| 294 | |
|---|
| 295 | protos = XML.load(f, proto_only=True) |
|---|
| 296 | for i, p in enumerate((IP, TCP, Payload)): |
|---|
| 297 | assert isinstance(protos[i], p) |
|---|
| 298 | |
|---|
| 299 | def test_xml_save(self): |
|---|
| 300 | output = StringIO() |
|---|
| 301 | XML.save(output, (self.example_packet,)) |
|---|
| 302 | a = output.getvalue() |
|---|
| 303 | b = self.example_xml.replace(' ','\t') |
|---|
| 304 | assert a == b |
|---|
| 305 | |
|---|
| 306 | def test_xml_save_filename(self): |
|---|
| 307 | if os.name != 'posix': |
|---|
| 308 | py.test.skip('only for POSIX platforms') |
|---|
| 309 | output = tempfile.NamedTemporaryFile(mode='w+t') |
|---|
| 310 | XML.save(output.name, (self.example_packet,)) |
|---|
| 311 | a = output.read() |
|---|
| 312 | b = self.example_xml.replace(' ','\t') |
|---|
| 313 | assert a == b |
|---|
| 314 | output.close() |
|---|
| 315 | |
|---|
| 316 | def test_xml_save_multiple(self): |
|---|
| 317 | output = StringIO() |
|---|
| 318 | XML.save(output, (self.example_packet,self.example_packet2)) |
|---|
| 319 | a = output.getvalue() |
|---|
| 320 | b = self.example_xml[:-8] # </UMPA>\n |
|---|
| 321 | b += self.example_xml2 |
|---|
| 322 | b = b.replace(' ','\t') |
|---|
| 323 | assert a == b |
|---|