| 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 | |
|---|
| 22 | import py.test |
|---|
| 23 | from umit.umpa.protocols import ICMP, Payload |
|---|
| 24 | |
|---|
| 25 | class TestICMP(object): |
|---|
| 26 | |
|---|
| 27 | # test if the active fields are set properly for supported ICMP types |
|---|
| 28 | def test_active_fields(self): |
|---|
| 29 | i = ICMP() |
|---|
| 30 | # default - no type |
|---|
| 31 | for name in ( 'ident', 'seq', 'redir_gw', 'pointer', 'pointer_unused', |
|---|
| 32 | 'addressmask', 'originate', 'receive', 'transmit' ): |
|---|
| 33 | assert i.get_field(name).active == False |
|---|
| 34 | for name in ( 'unused', 'data', ): |
|---|
| 35 | assert i.get_field(name).active == True |
|---|
| 36 | # id+seq types |
|---|
| 37 | for type in ( 0, 8, 13, 14, 15, 16, 17, 18 ): |
|---|
| 38 | i.type = type |
|---|
| 39 | for name in ( 'redir_gw', 'pointer', 'pointer_unused', 'unused', ): |
|---|
| 40 | assert i.get_field(name).active == False |
|---|
| 41 | for name in ( 'ident', 'seq', ): |
|---|
| 42 | assert i.get_field(name).active == True |
|---|
| 43 | # pointer+unused3 types |
|---|
| 44 | for type in ( 12, ): |
|---|
| 45 | i.type = type |
|---|
| 46 | for name in ( 'ident', 'seq', 'redir_gw', 'unused', ): |
|---|
| 47 | assert i.get_field(name).active == False |
|---|
| 48 | for name in ( 'pointer', 'pointer_unused', ): |
|---|
| 49 | assert i.get_field(name).active == True |
|---|
| 50 | # GIA types |
|---|
| 51 | for type in ( 5, ): |
|---|
| 52 | i.type = type |
|---|
| 53 | for name in ( 'ident', 'seq', 'pointer', 'pointer_unused', 'unused', ): |
|---|
| 54 | assert i.get_field(name).active == False |
|---|
| 55 | for name in ( 'redir_gw', ): |
|---|
| 56 | assert i.get_field(name).active == True |
|---|
| 57 | # timestamp data fields |
|---|
| 58 | for type in ( 13, 14 ): |
|---|
| 59 | i.type = type |
|---|
| 60 | for name in ( 'data', 'addressmask', ): |
|---|
| 61 | assert i.get_field(name).active == False |
|---|
| 62 | for name in ( 'originate', 'receive', 'transmit', ): |
|---|
| 63 | assert i.get_field(name).active == True |
|---|
| 64 | # address mask request/response |
|---|
| 65 | for type in ( 17, 18 ): |
|---|
| 66 | i.type = type |
|---|
| 67 | for name in ( 'originate', 'receive', 'transmit', 'data' ): |
|---|
| 68 | assert i.get_field(name).active == False |
|---|
| 69 | for name in ( 'addressmask', ): |
|---|
| 70 | assert i.get_field(name).active == True |
|---|
| 71 | |
|---|
| 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) |
|---|