root/branch/UMPA/tests/unit/test_extensions/test_xml.py @ 4637

Revision 4637, 12.2 kB (checked in by getxsick, 4 years ago)

remove done TODO and related fix to tests. it should be done in r4626

Line 
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
22from cStringIO import StringIO
23import tempfile
24
25import umpa
26from umpa.protocols import IP, TCP, Payload
27from umpa.protocols._fields import Flags
28from umpa.extensions import XML
29
30class TestExtensionXML(object):
31    example_xml = """<?xml version="1.0" ?>
32<UMPA>
33        <packet id="0" strict="True">
34                <protocol class="umpa.protocols.IP.IP">
35                        <_version type="int">
36                                4
37                        </_version>
38                        <_ihl type="NoneType">
39                                None
40                        </_ihl>
41                        <type_of_service type="bits">
42                                <precedence0 type="bool">
43                                        False
44                                </precedence0>
45                                <precedence1 type="bool">
46                                        False
47                                </precedence1>
48                                <precedence2 type="bool">
49                                        False
50                                </precedence2>
51                                <delay type="bool">
52                                        False
53                                </delay>
54                                <throughput type="bool">
55                                        False
56                                </throughput>
57                                <relibility type="bool">
58                                        False
59                                </relibility>
60                                <reserved0 type="bool">
61                                        False
62                                </reserved0>
63                                <reserved1 type="bool">
64                                        False
65                                </reserved1>
66                        </type_of_service>
67                        <_total_length type="NoneType">
68                                None
69                        </_total_length>
70                        <_identification type="int">
71                                0
72                        </_identification>
73                        <flags type="bits">
74                                <reserved type="bool">
75                                        False
76                                </reserved>
77                                <df type="bool">
78                                        False
79                                </df>
80                                <mf type="bool">
81                                        False
82                                </mf>
83                        </flags>
84                        <_fragment_offset type="int">
85                                0
86                        </_fragment_offset>
87                        <time_to_live type="NoneType">
88                                None
89                        </time_to_live>
90                        <_protocol type="NoneType">
91                                None
92                        </_protocol>
93                        <_header_checksum type="int">
94                                0
95                        </_header_checksum>
96                        <source_address type="str">
97                                127.0.0.1
98                        </source_address>
99                        <destination_address type="str">
100                                67.205.14.183
101                        </destination_address>
102                        <options type="bits"/>
103                        <_padding type="int">
104                                0
105                        </_padding>
106                </protocol>
107                <protocol class="umpa.protocols.TCP.TCP">
108                        <source_port type="int">
109                                2958
110                        </source_port>
111                        <destination_port type="int">
112                                0
113                        </destination_port>
114                        <_sequence_number type="NoneType">
115                                None
116                        </_sequence_number>
117                        <_acknowledgment_number type="NoneType">
118                                None
119                        </_acknowledgment_number>
120                        <_data_offset type="NoneType">
121                                None
122                        </_data_offset>
123                        <_reserved type="int">
124                                0
125                        </_reserved>
126                        <control_bits type="bits">
127                                <urg type="bool">
128                                        False
129                                </urg>
130                                <ack type="bool">
131                                        False
132                                </ack>
133                                <psh type="bool">
134                                        False
135                                </psh>
136                                <rst type="bool">
137                                        False
138                                </rst>
139                                <syn type="bool">
140                                        True
141                                </syn>
142                                <fin type="bool">
143                                        False
144                                </fin>
145                        </control_bits>
146                        <_window type="NoneType">
147                                None
148                        </_window>
149                        <_checksum type="NoneType">
150                                None
151                        </_checksum>
152                        <_urgent_pointer type="NoneType">
153                                None
154                        </_urgent_pointer>
155                        <options type="bits"/>
156                        <_padding type="int">
157                                0
158                        </_padding>
159                </protocol>
160                <protocol class="umpa.protocols.Payload.Payload">
161                        <data type="str">
162                                this is umpa!
163                        </data>
164                </protocol>
165        </packet>
166</UMPA>
167"""
168    example_xml2 = """        <packet id="1" strict="True">
169                <protocol class="umpa.protocols.TCP.TCP">
170                        <source_port type="int">
171                                123
172                        </source_port>
173                        <destination_port type="int">
174                                321
175                        </destination_port>
176                        <_sequence_number type="NoneType">
177                                None
178                        </_sequence_number>
179                        <_acknowledgment_number type="NoneType">
180                                None
181                        </_acknowledgment_number>
182                        <_data_offset type="NoneType">
183                                None
184                        </_data_offset>
185                        <_reserved type="int">
186                                0
187                        </_reserved>
188                        <control_bits type="bits">
189                                <urg type="bool">
190                                        False
191                                </urg>
192                                <ack type="bool">
193                                        False
194                                </ack>
195                                <psh type="bool">
196                                        False
197                                </psh>
198                                <rst type="bool">
199                                        False
200                                </rst>
201                                <syn type="bool">
202                                        False
203                                </syn>
204                                <fin type="bool">
205                                        False
206                                </fin>
207                        </control_bits>
208                        <_window type="NoneType">
209                                None
210                        </_window>
211                        <_checksum type="NoneType">
212                                None
213                        </_checksum>
214                        <_urgent_pointer type="NoneType">
215                                None
216                        </_urgent_pointer>
217                        <options type="bits"/>
218                        <_padding type="int">
219                                0
220                        </_padding>
221                </protocol>
222                <protocol class="umpa.protocols.Payload.Payload">
223                        <data type="str">
224                                another umpa packet!
225                        </data>
226                </protocol>
227        </packet>
228</UMPA>
229"""
230
231    ip = IP()
232    ip.source_address = "127.0.0.1"
233    ip.destination_address = "67.205.14.183"
234
235    # TCP header
236    tcp = TCP()
237    tcp.source_port = 2958
238    tcp.destination_port = 0
239    tcp.set_flags('control_bits', syn=True)
240
241    # Payload
242    data = Payload()
243    data.data = "this is umpa!"
244
245    # packet
246    example_packet = umpa.Packet(ip, tcp, data)
247   
248    tcp2 = TCP()
249    tcp2.source_port = 123
250    tcp2.destination_port = 321
251    tcp2.set_flags('control_bits', syn=False)
252    data2 = Payload()
253    data2.data = "another umpa packet!"
254    example_packet2 = umpa.Packet(tcp2, data2, strict=True)
255
256    def test_packet_attrs(self):
257        assert hasattr(umpa.Packet, 'save_xml')
258        assert hasattr(umpa.Packet, 'load_xml')
259
260    def test_xml_load(self):
261        f = StringIO()
262        f.write(self.example_xml)
263        f.seek(0)
264
265        packets = XML.load(f)
266        for p in packets:
267            for i, proto in enumerate(p.protos):
268                for fieldname in proto.get_fields_keys():
269                    assert proto.get_field(fieldname).fillout() == \
270            self.example_packet.protos[i].get_field(fieldname).fillout()
271
272    def test_xml_load_multiple(self):
273        l_xml= (self.example_xml, self.example_xml2)
274        l_packet = (self.example_packet, self.example_packet2)
275        f = StringIO()
276        f.write(l_xml[0][:-8]) # </UMPA>\n
277        f.write(l_xml[1])
278        f.seek(0)
279
280        packets = XML.load(f)
281        for j, p in enumerate(packets):
282            for i, proto in enumerate(p.protos):
283                for fieldname in proto.get_fields_keys():
284                    assert proto.get_field(fieldname).fillout() == \
285            l_packet[j].protos[i].get_field(fieldname).fillout()
286
287    def test_xml_load_proto_only(self):
288        f = StringIO()
289        f.write(self.example_xml)
290        f.seek(0)
291
292        protos = XML.load(f, proto_only=True)
293        for i, p in enumerate((IP, TCP, Payload)):
294            assert isinstance(protos[i], p)
295
296    def test_xml_save(self):
297        output = StringIO()
298        XML.save(output, (self.example_packet,))
299        a = output.getvalue()
300        b = self.example_xml.replace('        ','\t')
301        assert a == b
302
303        output = tempfile.NamedTemporaryFile(mode='w+t')
304        XML.save(output.name, (self.example_packet,))
305        a = output.read()
306        b = self.example_xml.replace('        ','\t')
307        assert a == b
308        output.close()
309
310    def test_xml_save_multiple(self):
311        output = StringIO()
312        XML.save(output, (self.example_packet,self.example_packet2))
313        a = output.getvalue()
314        b = self.example_xml[:-8] # </UMPA>\n
315        b += self.example_xml2
316        b = b.replace('        ','\t')
317        assert a == b
Note: See TracBrowser for help on using the browser.