Example #1
0
    def test_data(self):
        # DHCP packet contains relay agent option 82
        data_str = ('0101 0600 6f75 7345 0000 0000 0000 0000'
                    '0000 0000 0000 0000 c0a8 3204 fa16 3ea5'
                    '9fa4 0000 0000 0000 0000 0000 0000 0000'
                    '0000 0000 0000 0000 0000 0000 0000 0000'
                    '0000 0000 0000 0000 0000 0000 0000 0000'
                    '0000 0000 0000 0000 0000 0000 0000 0000'
                    '0000 0000 0000 0000 0000 0000 0000 0000'
                    '0000 0000 0000 0000 0000 0000 0000 0000'
                    '0000 0000 0000 0000 0000 0000 0000 0000'
                    '0000 0000 0000 0000 0000 0000 0000 0000'
                    '0000 0000 0000 0000 0000 0000 0000 0000'
                    '0000 0000 0000 0000 0000 0000 0000 0000'
                    '0000 0000 0000 0000 0000 0000 0000 0000'
                    '0000 0000 0000 0000 0000 0000 0000 0000'
                    '0000 0000 0000 0000 0000 0000 6382 5363'
                    '3501 013d 0701 fa16 3ea5 9fa4 3902 0240'
                    '3707 0103 060c 0f1c 2a3c 0c75 6468 6370'
                    '2031 2e32 302e 3152 1898 000b 040a 0a01'
                    '0205 040a 0a01 0297 0801 a7fb 1cce 171a'
                    '81ff')
        buf = bytearray.fromhex(data_str)
        pktbuf = bytearray(4096)
        pktbuf[0:len(buf)] = buf
        packet = DhcpPacket.parse(pktbuf)
        hex_data = hexlify(packet.data())
        self.assertIn(hexlify(packet.ciaddr), hex_data)
        self.assertIn(hexlify(packet.giaddr), hex_data)

        expected_relay_options = {
            152: b'',
            11: '10.10.1.2',
            5: '10.10.1.2',
            151: b'a7fb1cce171a81'
        }
        # Find relay agent sub-options in data
        self.assertIn(self.get_relay_opt_hex(expected_relay_options[11]),
                      hex_data)
        self.assertIn(self.get_relay_opt_hex(expected_relay_options[5]),
                      hex_data)
        self.assertIn(b"01" + expected_relay_options[151], hex_data)
        self.assertIn(expected_relay_options[152], hex_data)
    def test_data(self):
        # DHCP packet contains relay agent option 82
        data_str = ('0101 0600 6f75 7345 0000 0000 0000 0000'
            '0000 0000 0000 0000 c0a8 3204 fa16 3ea5'
            '9fa4 0000 0000 0000 0000 0000 0000 0000'
            '0000 0000 0000 0000 0000 0000 0000 0000'
            '0000 0000 0000 0000 0000 0000 0000 0000'
            '0000 0000 0000 0000 0000 0000 0000 0000'
            '0000 0000 0000 0000 0000 0000 0000 0000'
            '0000 0000 0000 0000 0000 0000 0000 0000'
            '0000 0000 0000 0000 0000 0000 0000 0000'
            '0000 0000 0000 0000 0000 0000 0000 0000'
            '0000 0000 0000 0000 0000 0000 0000 0000'
            '0000 0000 0000 0000 0000 0000 0000 0000'
            '0000 0000 0000 0000 0000 0000 0000 0000'
            '0000 0000 0000 0000 0000 0000 0000 0000'
            '0000 0000 0000 0000 0000 0000 6382 5363'
            '3501 013d 0701 fa16 3ea5 9fa4 3902 0240'
            '3707 0103 060c 0f1c 2a3c 0c75 6468 6370'
            '2031 2e32 302e 3152 1898 000b 040a 0a01'
            '0205 040a 0a01 0297 0801 a7fb 1cce 171a'
            '81ff')
        buf = bytearray.fromhex(data_str)
        pktbuf = bytearray(4096)
        pktbuf[0:len(buf)] = buf
        packet = DhcpPacket.parse(pktbuf)
        hex_data = hexlify(packet.data())
        self.assertIn(hexlify(packet.ciaddr), hex_data)
        self.assertIn(hexlify(packet.giaddr), hex_data)

        expected_relay_options = {152: b'',
                                  11: '10.10.1.2',
                                  5: '10.10.1.2',
                                  151: b'a7fb1cce171a81'}
        # Find relay agent sub-options in data
        self.assertIn(self.get_relay_opt_hex(expected_relay_options[11]),
            hex_data)
        self.assertIn(self.get_relay_opt_hex(expected_relay_options[5]),
            hex_data)
        self.assertIn(b"01" + expected_relay_options[151],
            hex_data)
        self.assertIn(expected_relay_options[152],
            hex_data)
Example #3
0
    def test_parse(self):
        # DHCP packet contains relay agent option 82
        data_str = ('0101 0600 6f75 7345 0000 0000 0000 0000'
                    '0000 0000 0000 0000 c0a8 3204 fa16 3ea5'
                    '9fa4 0000 0000 0000 0000 0000 0000 0000'
                    '0000 0000 0000 0000 0000 0000 0000 0000'
                    '0000 0000 0000 0000 0000 0000 0000 0000'
                    '0000 0000 0000 0000 0000 0000 0000 0000'
                    '0000 0000 0000 0000 0000 0000 0000 0000'
                    '0000 0000 0000 0000 0000 0000 0000 0000'
                    '0000 0000 0000 0000 0000 0000 0000 0000'
                    '0000 0000 0000 0000 0000 0000 0000 0000'
                    '0000 0000 0000 0000 0000 0000 0000 0000'
                    '0000 0000 0000 0000 0000 0000 0000 0000'
                    '0000 0000 0000 0000 0000 0000 0000 0000'
                    '0000 0000 0000 0000 0000 0000 0000 0000'
                    '0000 0000 0000 0000 0000 0000 6382 5363'
                    '3501 013d 0701 fa16 3ea5 9fa4 3902 0240'
                    '3707 0103 060c 0f1c 2a3c 0c75 6468 6370'
                    '2031 2e32 302e 3152 1898 000b 040a 0a01'
                    '0205 040a 0a01 0297 0801 a7fb 1cce 171a'
                    '81ff')

        buf = bytearray.fromhex(data_str)
        packet = DhcpPacket.parse(buf)
        # Test client address
        self.assertEqual('0.0.0.0', packet.get_ciaddr())
        # Test relay agent options
        expected_relay_options = {
            152: b'',
            11: '10.10.1.2',
            5: '10.10.1.2',
            151: b'a7fb1cce171a81'
        }
        actual_packet_options = {
            code: packet.get_relay_option(code)
            for code in [152, 11, 5, 151]
        }
        self.assertEqual(expected_relay_options, actual_packet_options)

        # Unsuccessful case of undefined relay agent sub-options
        with self.assertRaises(KeyError):
            packet.get_relay_option(220)
    def test_parse(self):
        # DHCP packet contains relay agent option 82
        data_str = ('0101 0600 6f75 7345 0000 0000 0000 0000'
            '0000 0000 0000 0000 c0a8 3204 fa16 3ea5'
            '9fa4 0000 0000 0000 0000 0000 0000 0000'
            '0000 0000 0000 0000 0000 0000 0000 0000'
            '0000 0000 0000 0000 0000 0000 0000 0000'
            '0000 0000 0000 0000 0000 0000 0000 0000'
            '0000 0000 0000 0000 0000 0000 0000 0000'
            '0000 0000 0000 0000 0000 0000 0000 0000'
            '0000 0000 0000 0000 0000 0000 0000 0000'
            '0000 0000 0000 0000 0000 0000 0000 0000'
            '0000 0000 0000 0000 0000 0000 0000 0000'
            '0000 0000 0000 0000 0000 0000 0000 0000'
            '0000 0000 0000 0000 0000 0000 0000 0000'
            '0000 0000 0000 0000 0000 0000 0000 0000'
            '0000 0000 0000 0000 0000 0000 6382 5363'
            '3501 013d 0701 fa16 3ea5 9fa4 3902 0240'
            '3707 0103 060c 0f1c 2a3c 0c75 6468 6370'
            '2031 2e32 302e 3152 1898 000b 040a 0a01'
            '0205 040a 0a01 0297 0801 a7fb 1cce 171a'
            '81ff')

        buf = bytearray.fromhex(data_str)
        packet = DhcpPacket.parse(buf)
        # Test client address
        self.assertEqual('0.0.0.0', packet.get_ciaddr())
        # Test relay agent options
        expected_relay_options = {152: b'',
                                  11: '10.10.1.2',
                                  5: '10.10.1.2',
                                  151: b'a7fb1cce171a81'}
        actual_packet_options = {code: packet.get_relay_option(code)
                                 for code in [152, 11, 5, 151]}
        self.assertEqual(expected_relay_options, actual_packet_options)

        # Unsuccessful case of undefined relay agent sub-options
        with self.assertRaises(KeyError):
            packet.get_relay_option(220)