1#!/usr/bin/env python
2
3#Impact test version
4try:
5    from impacket import IP6_Address, IP6, ImpactDecoder
6except:
7    pass
8
9#Standalone test version
10try:
11    import sys
12    sys.path.insert(0,"../..")
13    import IP6_Address, IP6, ImpactDecoder
14except:
15    pass
16
17import unittest
18
19class TestIP6(unittest.TestCase):
20
21    def setUp(self):
22        #Version 6, traffic class 72, flow label 148997, payload length 1500
23        #next header 17 (UDP), hop limit 1
24        #source addr FE80::78F8:89D1:30FF:256B
25        #dest addr FF02::1:3
26        self.binary_packet = [
27                   0x64, 0x82, 0x46, 0x05,
28                   0x05, 0xdc, 0x11, 0x01,
29                   0xfe, 0x80, 0x00, 0x00,
30                   0x00, 0x00, 0x00, 0x00,
31                   0x78, 0xf8, 0x89, 0xd1,
32                   0x30, 0xff, 0x25, 0x6b,
33                   0xff, 0x02, 0x00, 0x00,
34                   0x00, 0x00, 0x00, 0x00,
35                   0x00, 0x00, 0x00, 0x00,
36                   0x00, 0x01, 0x00, 0x03]
37
38    def test_decoding(self):
39        '''Test IP6 Packet decoding.'''
40
41
42        d = ImpactDecoder.IP6Decoder()
43        parsed_packet = d.decode(self.binary_packet)
44
45        protocol_version = parsed_packet.get_ip_v()
46        traffic_class = parsed_packet.get_traffic_class()
47        flow_label = parsed_packet.get_flow_label()
48        payload_length = parsed_packet.get_payload_length()
49        next_header = parsed_packet.get_next_header()
50        hop_limit = parsed_packet.get_hop_limit()
51        source_address = parsed_packet.get_ip_src()
52        destination_address = parsed_packet.get_ip_dst()
53
54        self.assertEquals(protocol_version, 6, "IP6 parsing - Incorrect protocol version")
55        self.assertEquals(traffic_class, 72, "IP6 parsing - Incorrect traffic class")
56        self.assertEquals(flow_label, 148997, "IP6 parsing - Incorrect flow label")
57        self.assertEquals(payload_length, 1500, "IP6 parsing - Incorrect payload length")
58        self.assertEquals(next_header, 17, "IP6 parsing - Incorrect next header")
59        self.assertEquals(hop_limit, 1, "IP6 parsing - Incorrect hop limit")
60        self.assertEquals(source_address.as_string(), "FE80::78F8:89D1:30FF:256B", "IP6 parsing - Incorrect source address")
61        self.assertEquals(destination_address.as_string(), "FF02::1:3", "IP6 parsing - Incorrect destination address")
62
63    def test_creation(self):
64        '''Test IP6 Packet creation.'''
65
66        crafted_packet = IP6.IP6()
67        crafted_packet.set_traffic_class(72)
68        crafted_packet.set_flow_label(148997)
69        crafted_packet.set_payload_length(1500)
70        crafted_packet.set_next_header(17)
71        crafted_packet.set_hop_limit(1)
72        crafted_packet.set_ip_src("FE80::78F8:89D1:30FF:256B")
73        crafted_packet.set_ip_dst("FF02::1:3")
74        crafted_buffer = crafted_packet.get_bytes().tolist()
75        self.assertEquals(crafted_buffer, self.binary_packet, "IP6 creation - Buffer mismatch")
76
77
78suite = unittest.TestLoader().loadTestsFromTestCase(TestIP6)
79unittest.TextTestRunner(verbosity=1).run(suite)
80