Show
Ignore:
Timestamp:
08/15/10 16:06:47 (3 years ago)
Author:
kosma
Message:

ICMP: load_raw support; tests for get_raw and load_raw

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • umpa/branches/protocols/tests/a_unit/test_protocols/test_icmp.py

    r5802 r5804  
    2121 
    2222import py.test 
    23 from umit.umpa.protocols import ICMP, Payload 
     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) 
    2428 
    2529class TestICMP(object): 
    2630 
    27     # test if the active fields are set properly for supported ICMP types 
    2831    def test_active_fields(self): 
     32        """ 
     33        test if the active fields are set properly for supported ICMP types 
     34        """ 
     35 
    2936        i = ICMP() 
    3037        # default - no type 
     
    7077                assert i.get_field(name).active == True 
    7178 
    72 #    # this tests pre/post raw methods as well 
    73 #    def test_get_raw(self): 
    74 #        ip = IP(src='1.2.3.4', dst='5.6.7.8') 
    75 #        udp = UDP(srcport=9, dstport=10) 
    76 # 
    77 #        assert udp._raw(0, 0, [ip, udp], 0) == (0x9000A0008EFB7, 64) 
    78 # 
    79 #    # test length and checksum calculation 
    80 #    def test_get_raw_with_payload(self): 
    81 #        ip = IP(src='1.2.3.4', dst='5.6.7.8') 
    82 #        udp = UDP(srcport=9, dstport=10) 
    83 #        payload = Payload('12345678') 
    84 # 
    85 #        # the next two lines are normally done by Packet.get_raw() 
    86 #        payload.get_raw([ip, udp, payload], 8*len(payload.data)) 
    87 #        udp.__dict__['payload'] = payload 
    88 # 
    89 #        assert udp._raw(0, 0, [ip, udp, payload], 8*len(payload.data)) == \ 
    90 #                                                       (0x9000a00101ed3L, 64) 
     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'