root/umpa/branches/protocols/tests/a_unit/test_protocols/test_icmp.py @ 5804

Revision 5804, 6.6 kB (checked in by kosma, 3 years ago)

ICMP: load_raw support; tests for get_raw and load_raw

Line 
1#!/usr/bin/env python
2# -*- coding: utf-8 -*-
3
4# Copyright (C) 2010 Adriano Monteiro Marques.
5#
6# Author: Kosma Moczek <kosma at kosma dot pl>
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
22import py.test
23from umit.umpa.protocols import ICMP
24from umit.umpa.utils.exceptions import UMPAException, UMPAAttributeException
25
26def raw(obj):
27    return obj._raw(0, 0, [obj], 0)
28
29class TestICMP(object):
30
31    def test_active_fields(self):
32        """
33        test if the active fields are set properly for supported ICMP types
34        """
35
36        i = ICMP()
37        # default - no type
38        for name in ( 'ident', 'seq', 'redir_gw', 'pointer', 'pointer_unused',
39                      'addressmask', 'originate', 'receive', 'transmit' ):
40            assert i.get_field(name).active == False
41        for name in ( 'unused', 'data', ):
42            assert i.get_field(name).active == True
43        # id+seq types
44        for type in ( 0, 8, 13, 14, 15, 16, 17, 18 ):
45            i.type = type
46            for name in ( 'redir_gw', 'pointer', 'pointer_unused', 'unused', ):
47                assert i.get_field(name).active == False
48            for name in ( 'ident', 'seq', ):
49                assert i.get_field(name).active == True
50        # pointer+unused3 types
51        for type in ( 12, ):
52            i.type = type
53            for name in ( 'ident', 'seq', 'redir_gw', 'unused', ):
54                assert i.get_field(name).active == False
55            for name in ( 'pointer', 'pointer_unused', ):
56                assert i.get_field(name).active == True
57        # GIA types
58        for type in ( 5, ):
59            i.type = type
60            for name in ( 'ident', 'seq', 'pointer', 'pointer_unused', 'unused', ):
61                assert i.get_field(name).active == False
62            for name in ( 'redir_gw', ):
63                assert i.get_field(name).active == True
64        # timestamp data fields
65        for type in ( 13, 14 ):
66            i.type = type
67            for name in ( 'data', 'addressmask', ):
68                assert i.get_field(name).active == False
69            for name in ( 'originate', 'receive', 'transmit', ):
70                assert i.get_field(name).active == True
71        # address mask request/response
72        for type in ( 17, 18 ):
73            i.type = type
74            for name in ( 'originate', 'receive', 'transmit', 'data' ):
75                assert i.get_field(name).active == False
76            for name in ( 'addressmask', ):
77                assert i.get_field(name).active == True
78
79    def test_get_raw(self):
80        # empty objects have no type set
81        py.test.raises(UMPAException, "raw(ICMP())")
82
83        # basic tests, no complicated headers
84        assert raw(ICMP(type=0))   == (0x0000ffff00000000, 64)
85        assert raw(ICMP(type=255)) == (0xff0000ff00000000, 64)
86        assert raw(ICMP(type=0, code=0x42)) == (0x0042bdff00000000, 64)
87
88        # Echo
89        ret = raw(ICMP(type='ECHO', ident=0x1234, seq=0x5678, data='ABCD'))
90        assert ret == (0x08000acd1234567841424344, 96)
91        ret = raw(ICMP(type='ECHO_REPLY', ident=1, seq=2, data='foo'))
92        assert ret == (0x00008d2a00010002666f6f, 88)
93        # Address Mask
94        ret = raw(ICMP(type='ADDRESS_MASK_REQUEST', seq=0xffff))
95        assert ret == (0x1100eeff0000ffff00000000, 96)
96        ret = raw(ICMP(type='ADDRESS_MASK_REPLY', ident=0xbeef, addressmask='255.255.255.0'))
97        assert ret == (0x1200300fbeef0000ffffff00, 96)
98        # Timestamp
99        ret = raw(ICMP(type='TIMESTAMP', originate=0x0666))
100        assert ret == (0x0d00ec9900000000000006660000000000000000, 160)
101        ret = raw(ICMP(type='TIMESTAMP_REPLY', receive=2, transmit=3))
102        assert ret == (0x0e00f1fa00000000000000000000000200000003, 160)
103        # Information
104        ret = raw(ICMP(type='INFORMATION_REQUEST', code=9, ident=0xdead, seq=0xbeef))
105        assert ret == (0x0f095359deadbeef, 64)
106        ret = raw(ICMP(type='INFORMATION_REPLY', code=1))
107        assert ret == (0x1001effe00000000, 64)
108        # Redirect
109        ret = raw(ICMP(type='REDIRECT', redir_gw='8.8.8.8', data='\0\0\0\x42'))
110        assert ret == (0x0500eaad0808080800000042, 96)
111        # Destination Unreachable
112        ret = raw(ICMP(type='DESTINATION_UNREACHABLE', code=10, data=' '))
113        assert ret == (0x030adcf50000000020, 72)
114        # Time Exceeded
115        ret = raw(ICMP(type='TIME_EXCEEDED'))
116        assert ret == (0x0b00f4ff00000000, 64)
117        # Parameter Problem
118        ret = raw(ICMP(type='PARAMETER_PROBLEM', pointer=0x42, pointer_unused=1))
119        assert ret == (0x0c00b1fe42000001, 64)
120        # Source Quench
121        ret = raw(ICMP(type='SOURCE_QUENCH', unused=0xcafebabe, data=''))
122        assert ret == (0x04007642cafebabe, 64)
123
124    def test_load_raw(self):
125        i = ICMP()
126
127        # code
128        i.load_raw("\x00\x42\xbd\xff\x00\x00\x00\x00")
129        assert i.type == 0
130        assert i.code == 0x42
131
132        # Echo
133        i.load_raw("\x08\x00\x0a\xcd\x12\x34\x56\x78\x41\x42\x43\x44")
134        assert i.type == 8
135        assert i.code == 0
136        assert i._checksum == 0x0acd
137        assert i.ident == 0x1234
138        assert i.seq == 0x5678
139        assert i.data == 'ABCD'
140
141        # Address Mask
142        i.load_raw("\x12\x00\x30\x0f\xbe\xef\x00\x00\xff\xff\xff\x00")
143        assert i.type == 18
144        assert i.code == 0
145        assert i._checksum == 0x300f
146        assert i.ident == 0xbeef
147        assert i.seq == 0x0000
148        assert i.addressmask == (255,255,255,0)
149
150        # Timestamp
151        i.load_raw("\x0e\x00\xf1\xfa\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\x00\x00\x03")
152        assert i.type == 14
153        assert i.code == 0
154        assert i._checksum == 0xf1fa
155        assert i.ident == 0x0000
156        assert i.seq == 0x0000
157        assert i.originate == 0
158        assert i.receive == 2
159        assert i.transmit == 3
160
161        # Redirect
162        i.load_raw("\x05\x00\xea\xad\x08\x08\x08\x08\x00\x00\x00\x42")
163        assert i.type == 5
164        assert i.code == 0
165        assert i._checksum == 0xeaad
166        assert i.redir_gw == (8,8,8,8)
167        assert i.data == '\0\0\0\x42'
Note: See TracBrowser for help on using the browser.