Ejemplo n.º 1
0
 def assert_block32_bytes_packing(data_bytes):
     orig = to_bytes(data_bytes)
     extra_bytes = to_bytes('dummy-start') + orig + to_bytes('dummy-end')
     unpacked, remaining = util.block32_lv_bytes_unpack_rolling(
         util.block32_lv_bytes_pack(orig) + extra_bytes)
     assert unpacked == orig
     assert remaining == extra_bytes
Ejemplo n.º 2
0
def test_to_bytes():
    assert 'abc' == to_bytes('abc')
    assert 'abc' == to_bytes([97, 98, 99])
    if util.is_python2():
        assert str('abc') == to_bytes('abc')
    if util.is_python3():
        assert bytes([97, 98, 99]) == to_bytes([97, 98, 99])
Ejemplo n.º 3
0
 def assert_block32_labelled_bytes_packing(data_bytes):
     orig = to_bytes(data_bytes)
     extra_bytes = to_bytes('dummy-start') + orig + to_bytes('dummy-end')
     label, unpacked, remaining = util.block32_tlv_bytes_unpack_rolling(
         util.block32_tlv_bytes_pack(block_label, orig) + extra_bytes)
     assert label == block_label
     assert unpacked == orig
     assert remaining == extra_bytes
Ejemplo n.º 4
0
 def pack(self):  #todo needs test
     "Serialize to packed bytes"
     content = to_bytes(self.addr_bytes) + to_bytes([self.prefix_len])
     content_len = len(content)
     assert content_len == 17
     content_pad = util.block32_pad_bytes(content)
     packed_bytes = struct.pack('=HH', self.type_code,
                                content_len) + content_pad
     util.assert_block32_length(packed_bytes)  #todo add to all
     return packed_bytes
Ejemplo n.º 5
0
    def __init__(self,
                 interface_id,
                 pkt_data_captured,
                 pkt_data_orig_len=None,
                 options_lst=[],
                 timestamp=None):
        """Creates an EPB object. If 'timestamp' is not supplied, uses the current UTC time in the
        default format (uint64 microseconds since unix epoch). User-supplied timestamp must also be
        in this format unless IDB options uses option.IdbTsResol to specify alternate."""
        util.assert_uint32(interface_id)  #todo verify args in all fns
        pkt_data_captured = to_bytes(
            pkt_data_captured)  #todo is list & tuple & str ok?
        if pkt_data_orig_len is None:
            pkt_data_orig_len = len(pkt_data_captured)
        else:
            util.assert_uint32(pkt_data_orig_len)
            assert len(pkt_data_captured) <= pkt_data_orig_len
        util.assert_type_list(options_lst)  #todo check type on all fns
        for opt in options_lst:
            assert self.is_epb_option(opt)
        if timestamp is None:
            time_utc_micros = util.curr_time_utc_micros()
        else:
            time_utc_micros = timestamp

        self.interface_id = interface_id
        self.pkt_data_captured = pkt_data_captured
        self.pkt_data_orig_len = pkt_data_orig_len
        self.options_lst = options_lst
        self.time_utc_micros = time_utc_micros
Ejemplo n.º 6
0
 def assert_cmib_codec(content):
     content_bytes = to_bytes(content)
     cmib_obj = block.CustomMrtIsisBlock(content_bytes)
     mrt_info = block.CustomMrtIsisBlock.unpack(cmib_obj.pack())
     assert mrt_info['mrt_type'] == mrt.ISIS
     assert mrt_info['mrt_subtype'] == 0
     assert mrt_info['content'] == content_bytes
Ejemplo n.º 7
0
 def pack(self):
     "Serialize to packed bytes"
     content = to_bytes(self.addr_bytes)
     content_len = len(content)
     assert content_len == 8
     packed_bytes = add_header(self.type_code, content_len, content)
     return packed_bytes
Ejemplo n.º 8
0
def get_dummy_pkt():
    global pkt_len_curr, pkt_len_min, pkt_len_max
    next_len = pkt_len_curr + 1
    if (pkt_len_min <= next_len <= pkt_len_max):
        pkt_len_curr = next_len
    else:
        pkt_len_curr = pkt_len_min
    result = to_bytes(range(pkt_len_min, pkt_len_curr + 1))
    return result
Ejemplo n.º 9
0
def test_EpbFlags():
    content = to_bytes([1, 2, 3, 4])
    c1 = option.EpbFlags(content)
    packed_bytes = c1.pack()
    c1u = option.EpbFlags.unpack(packed_bytes)
    assert len(packed_bytes) == 8  #todo add this test everywhere
    assert c1.content == c1u.content == content
    assert util.classname(c1) == util.classname(
        c1u) == 'pcapng.option.EpbFlags'
Ejemplo n.º 10
0
    def pack(self):
        """Serialize an CB object into packed bytes. NOTE: We are required to use
        Length-Value (LV) encoding for the custom content so that any options may also be recovered."""
        content_bytes = util.block32_lv_bytes_pack(to_bytes(self.content))
        options_bytes = option.pack_all(self.options_lst)
        block_total_len = 16 + len(content_bytes) + len(options_bytes)

        packed_bytes = (struct.pack(self.head_encoding, self.block_type,
                                    block_total_len, self.pen_val) +
                        content_bytes + options_bytes +
                        struct.pack(self.tail_encoding, block_total_len))
        return packed_bytes
Ejemplo n.º 11
0
 def assert_epb_codec(interface_id,
                      pkt_data,
                      pkt_data_orig_len=None,
                      options_lst=[]):
     pkt_data = to_bytes(pkt_data)
     if pkt_data_orig_len is None:
         pkt_data_orig_len = len(
             pkt_data)  #todo does not test None or invalid val
     epb_obj = block.EnhancedPacketBlock(interface_id, pkt_data,
                                         pkt_data_orig_len, options_lst)
     epb_bytes = epb_obj.pack()
     epb_obj_unpacked = block.EnhancedPacketBlock.unpack(epb_bytes)
     assert util.classname(
         epb_obj_unpacked) == 'pcapng.block.EnhancedPacketBlock'
     assert epb_obj_unpacked == epb_obj
Ejemplo n.º 12
0
def test_pad_to_block32():
    assert to_bytes([]) == util.block32_pad_bytes([])
    assert to_bytes([1, 0, 0, 0]) == util.block32_pad_bytes([1])
    assert to_bytes([1, 2, 0, 0]) == util.block32_pad_bytes([1, 2])
    assert to_bytes([1, 2, 3, 0]) == util.block32_pad_bytes([1, 2, 3])
    assert to_bytes([1, 2, 3, 4]) == util.block32_pad_bytes([1, 2, 3, 4])
    assert to_bytes([1, 2, 3, 4, 5, 0, 0,
                     0]) == util.block32_pad_bytes([1, 2, 3, 4, 5])
    assert to_bytes([1, 2, 3, 4, 5, 6, 0,
                     0]) == util.block32_pad_bytes([1, 2, 3, 4, 5, 6])
    assert to_bytes([1, 2, 3, 4, 5, 6, 7,
                     0]) == util.block32_pad_bytes([1, 2, 3, 4, 5, 6, 7])
    assert to_bytes([1, 2, 3, 4, 5, 6, 7,
                     8]) == util.block32_pad_bytes([1, 2, 3, 4, 5, 6, 7, 8])

    util.assert_block32_length([])
    util.assert_block32_length([1, 2, 3, 4])
    util.assert_block32_length([1, 2, 3, 4, 5, 6, 7, 8])
    with pytest.raises(AssertionError):
        util.assert_block32_length([1])
    with pytest.raises(AssertionError):
        util.assert_block32_length([1, 2])
    with pytest.raises(AssertionError):
        util.assert_block32_length([1, 2, 3])
Ejemplo n.º 13
0
    def assert_custom_block_codec(content_bytes):
        opts = [
            option.CustomStringCopyable(pen.BROCADE_PEN, "O"),
            option.CustomBinaryCopyable(pen.BROCADE_PEN, "Doh!"),
            option.CustomStringNonCopyable(pen.BROCADE_PEN,
                                           "Release the hounds!"),
            option.CustomBinaryNonCopyable(pen.BROCADE_PEN, [1, 2, 3])
        ]
        orig = to_bytes(content_bytes)

        cbc_obj = block.CustomBlockCopyable(pen.BROCADE_PEN, orig, opts)
        cbc_bytes = cbc_obj.pack()
        cbc_obj_unpack = block.CustomBlockCopyable.unpack(cbc_bytes)
        assert cbc_obj_unpack == cbc_obj

        cbnc_obj = block.CustomBlockNonCopyable(pen.BROCADE_PEN, orig, opts)
        cbnc_bytes = cbnc_obj.pack()
        cbnc_obj_unpack = block.CustomBlockNonCopyable.unpack(cbnc_bytes)
        print
        print('770', cbnc_obj)
        print('771', cbnc_obj_unpack)
        assert cbnc_obj_unpack == cbnc_obj
Ejemplo n.º 14
0
 def __init__(self, pen_val, content):
     "Create a PCAPNG Custom String Copyable Option"
     pen.assert_valid_pen(pen_val)
     self.type_code = self.SPEC_CODE
     self.pen_val = pen_val
     self.content = to_bytes(content)
Ejemplo n.º 15
0
def test_pad_to_len():
    with pytest.raises(AssertionError):
        util.pad_bytes([1, 2, 3, 4], 3)
    with pytest.raises(AssertionError):
        util.pad_bytes('superlong', 3)

    assert to_bytes('superlong' + chr(0) * 23) == util.pad_bytes(
        'superlong', 32)
    assert to_bytes([0, 0, 0, 0]) == util.pad_bytes([], 4)
    assert to_bytes([1, 0, 0, 0]) == util.pad_bytes([
        1,
    ], 4)
    assert to_bytes([1, 2, 0, 0]) == util.pad_bytes([1, 2], 4)
    assert to_bytes([1, 2, 3, 0]) == util.pad_bytes([1, 2, 3], 4)
    assert to_bytes([1, 2, 3, 4]) == util.pad_bytes([1, 2, 3, 4], 4)

    assert to_bytes([9, 9, 9, 9]) == util.pad_bytes([], 4, 9)
    assert to_bytes([1, 9, 9, 9]) == util.pad_bytes([
        1,
    ], 4, 9)
    assert to_bytes([1, 2, 9, 9]) == util.pad_bytes([1, 2], 4, 9)
    assert to_bytes([1, 2, 3, 9]) == util.pad_bytes([1, 2, 3], 4, 9)
    assert to_bytes([1, 2, 3, 4]) == util.pad_bytes([1, 2, 3, 4], 4, 9)
Ejemplo n.º 16
0
 def __init__(self, pkt_data):
     "Creates an SPB object"
     self.pkt_data = to_bytes(pkt_data)  #todo is list & tuple & str ok?
Ejemplo n.º 17
0
 def __init__(self, type_code, content):
     "Creates an Option block"
     #todo assert valid type_code?
     self.type_code = type_code
     self.content = to_bytes(content)
Ejemplo n.º 18
0
 def __init__(self, pkt_data):
     "Creates an ISIS MRT Block object"
     self.pkt_data = to_bytes(pkt_data)
Ejemplo n.º 19
0
 def __init__(self, pen_val, content):
     "Create an instance"
     pen.assert_valid_pen(pen_val)
     self.type_code = self.SPEC_CODE
     self.pen_val = pen_val
     self.content = to_bytes(content)
Ejemplo n.º 20
0
 def pack(self):  #todo needs test
     "Serialize to packed bytes"
     packed_bytes = (struct.pack('=HH', self.type_code, 8) +
                     to_bytes(self.addr_bytes) +
                     to_bytes(self.netmask_bytes))
     return packed_bytes
Ejemplo n.º 21
0
 def assert_option_codec(type_code, opt_value):
     opt = option.Option(type_code, opt_value)
     opt_unpacked = option.Option.unpack(opt.pack())
     assert opt.type_code == type_code
     assert opt.content == to_bytes(opt_value)
     assert opt == opt_unpacked
Ejemplo n.º 22
0
def test_str_to_bytes():
    assert to_bytes([97, 98, 99]) == str_to_bytes('abc')
Ejemplo n.º 23
0
def test_EpbHash():
    content = to_bytes("<generic hash spec here>")
    c1 = option.EpbHash(content)
    c1u = option.EpbHash.unpack(c1.pack())
    assert c1.content == c1u.content == content
    assert util.classname(c1) == util.classname(c1u) == 'pcapng.option.EpbHash'
Ejemplo n.º 24
0
 def __init__(self, content):
     "Create an instance"
     content = to_bytes(content)
     assert len(content) == 4
     EpbOption.__init__(self, self.SPEC_CODE, content)
Ejemplo n.º 25
0
def string_utf8_pack(str_val):
    content = to_bytes(str_val)
    packed_bytes = util.block32_pad_bytes(
        struct.pack('=HH', STRING_UTF8, len(content)) + content)
    return packed_bytes