Beispiel #1
0
def test_pack_tsresol():
    assert pack_timestamp_resolution(10, 0b00000000) == bytes((0b00000000, ))
    assert pack_timestamp_resolution(10, 0b00000011) == bytes((0b00000011, ))
    assert pack_timestamp_resolution(10, 0b00000100) == bytes((0b00000100, ))
    assert pack_timestamp_resolution(10, 0b00111100) == bytes((0b00111100, ))

    assert pack_timestamp_resolution(2, 0b00000000) == bytes((0b10000000, ))
    assert pack_timestamp_resolution(2, 0b00000011) == bytes((0b10000011, ))
    assert pack_timestamp_resolution(2, 0b00000100) == bytes((0b10000100, ))
    assert pack_timestamp_resolution(2, 0b00111100) == bytes((0b10111100, ))
Beispiel #2
0
def test_pack_tsresol():
    assert pack_timestamp_resolution(10, 0b00000000) == int2byte(0b00000000)
    assert pack_timestamp_resolution(10, 0b00000011) == int2byte(0b00000011)
    assert pack_timestamp_resolution(10, 0b00000100) == int2byte(0b00000100)
    assert pack_timestamp_resolution(10, 0b00111100) == int2byte(0b00111100)

    assert pack_timestamp_resolution(2, 0b00000000) == int2byte(0b10000000)
    assert pack_timestamp_resolution(2, 0b00000011) == int2byte(0b10000011)
    assert pack_timestamp_resolution(2, 0b00000100) == int2byte(0b10000100)
    assert pack_timestamp_resolution(2, 0b00111100) == int2byte(0b10111100)
Beispiel #3
0
def test_pack_tsresol():
    assert pack_timestamp_resolution(10, 0b00000000) == int2byte(0b00000000)
    assert pack_timestamp_resolution(10, 0b00000011) == int2byte(0b00000011)
    assert pack_timestamp_resolution(10, 0b00000100) == int2byte(0b00000100)
    assert pack_timestamp_resolution(10, 0b00111100) == int2byte(0b00111100)

    assert pack_timestamp_resolution(2, 0b00000000) == int2byte(0b10000000)
    assert pack_timestamp_resolution(2, 0b00000011) == int2byte(0b10000011)
    assert pack_timestamp_resolution(2, 0b00000100) == int2byte(0b10000100)
    assert pack_timestamp_resolution(2, 0b00111100) == int2byte(0b10111100)
def _generate_file_with_tsresol(base, exponent):
    tsresol = pack_timestamp_resolution(base, exponent)
    base_timestamp = 1420070400.0  # 2015-01-01 00:00 UTC
    timestamp = base_timestamp / (base**exponent)

    stream = io.BytesIO()

    # ---------- Section header
    stream.write(b"\x0a\x0d\x0d\x0a")  # Magic number
    stream.write(b"\x00\x00\x00\x20")  # Block size (32 bytes)
    stream.write(b"\x1a\x2b\x3c\x4d")  # Magic number
    stream.write(b"\x00\x01\x00\x00")  # Version
    stream.write(b"\xff\xff\xff\xff\xff\xff\xff\xff")  # Undefined length
    stream.write(b"\x00\x00\x00\x00")  # Empty options
    stream.write(b"\x00\x00\x00\x20")  # Block size (32 bytes)

    # ---------- Interface description
    stream.write(b"\x00\x00\x00\x01")  # block magic
    stream.write(b"\x00\x00\x00\x20")  # block syze
    stream.write(b"\x00\x01")  # link type
    stream.write(b"\x00\x00")  # reserved block
    stream.write(b"\x00\x00\xff\xff")  # size limit
    stream.write(b"\x00\x09\x00\x01")
    stream.write(tsresol)
    stream.write(b"\x00\x00\x00")  # if_tsresol (+padding)
    stream.write(b"\x00\x00\x00\x00")  # end of options
    stream.write(b"\x00\x00\x00\x20")  # block syze

    # ---------- Enhanced packet
    stream.write(b"\x00\x00\x00\x06")  # block magic
    stream.write(b"\x00\x00\x00\x24")  # block syze
    stream.write(b"\x00\x00\x00\x00")  # interface id (first one, eth0)
    stream.write(struct.pack(">Q", int(timestamp)))  # timestamp
    stream.write(b"\x00\x00\x00\x00")  # Captured length
    stream.write(b"\x00\x00\x00\x00")  # Original length
    # no packet data
    stream.write(b"\x00\x00\x00\x00")  # Empty options
    stream.write(b"\x00\x00\x00\x24")  # block syze

    return stream.getvalue()
def _generate_file_with_tsresol(base, exponent):
    tsresol = pack_timestamp_resolution(base, exponent)
    base_timestamp = 1420070400.0  # 2015-01-01 00:00 UTC
    timestamp = base_timestamp / (base ** exponent)

    stream = io.BytesIO()

    # ---------- Section header
    stream.write(b"\x0a\x0d\x0d\x0a")  # Magic number
    stream.write(b"\x00\x00\x00\x20")  # Block size (32 bytes)
    stream.write(b"\x1a\x2b\x3c\x4d")  # Magic number
    stream.write(b"\x00\x01\x00\x00")  # Version
    stream.write(b"\xff\xff\xff\xff\xff\xff\xff\xff")  # Undefined length
    stream.write(b"\x00\x00\x00\x00")  # Empty options
    stream.write(b"\x00\x00\x00\x20")  # Block size (32 bytes)

    # ---------- Interface description
    stream.write(b'\x00\x00\x00\x01')  # block magic
    stream.write(b'\x00\x00\x00\x20')  # block syze
    stream.write(b'\x00\x01')  # link type
    stream.write(b'\x00\x00')  # reserved block
    stream.write(b'\x00\x00\xff\xff')  # size limit
    stream.write(b'\x00\x09\x00\x01')
    stream.write(tsresol)
    stream.write(b'\x00\x00\x00')  # if_tsresol (+padding)
    stream.write(b'\x00\x00\x00\x00')  # end of options
    stream.write(b'\x00\x00\x00\x20')  # block syze

    # ---------- Enhanced packet
    stream.write(b'\x00\x00\x00\x06')  # block magic
    stream.write(b'\x00\x00\x00\x24')  # block syze
    stream.write(b'\x00\x00\x00\x00')  # interface id (first one, eth0)
    stream.write(struct.pack('>Q', int(timestamp)))  # timestamp
    stream.write(b'\x00\x00\x00\x00')  # Captured length
    stream.write(b'\x00\x00\x00\x00')  # Original length
    # no packet data
    stream.write(b'\x00\x00\x00\x00')  # Empty options
    stream.write(b'\x00\x00\x00\x24')  # block syze

    return stream.getvalue()
Beispiel #6
0
def _generate_file_with_tsresol(base, exponent):
    tsresol = pack_timestamp_resolution(base, exponent)
    base_timestamp = 1420070400.0  # 2015-01-01 00:00 UTC
    timestamp = base_timestamp / (base**exponent)

    data = (
        # ---------- Section header
        "\x0a\x0d\x0d\x0a"  # Magic number
        "\x00\x00\x00\x20"  # Block size (32 bytes)
        "\x1a\x2b\x3c\x4d"  # Magic number
        "\x00\x01\x00\x00"  # Version
        "\xff\xff\xff\xff\xff\xff\xff\xff"  # Undefined section length
        "\x00\x00\x00\x00"  # Empty options
        "\x00\x00\x00\x20"  # Block size (32 bytes)

        # ---------- Interface description
        '\x00\x00\x00\x01'  # block magic
        '\x00\x00\x00\x20'  # block syze
        '\x00\x01'  # link type
        '\x00\x00'  # reserved block
        '\x00\x00\xff\xff'  # size limit
        '\x00\x09\x00\x01'
        '{tsresol}\x00\x00\x00'  # if_tsresol (+padding)
        '\x00\x00\x00\x00'  # end of options
        '\x00\x00\x00\x20'  # block syze

        # ---------- Enhanced packet
        '\x00\x00\x00\x06'  # block magic
        '\x00\x00\x00\x24'  # block syze
        '\x00\x00\x00\x00'  # interface id (first one, eth0)
        '{timestamp}'  # timestamp
        '\x00\x00\x00\x00'  # Captured length
        '\x00\x00\x00\x00'  # Original length
        # no packet data
        '\x00\x00\x00\x00'  # Empty options
        '\x00\x00\x00\x24'  # block syze
    ).format(timestamp=struct.pack('>Q', timestamp), tsresol=tsresol)

    return data
def _generate_file_with_tsresol(base, exponent):
    tsresol = pack_timestamp_resolution(base, exponent)
    base_timestamp = 1420070400.0  # 2015-01-01 00:00 UTC
    timestamp = base_timestamp / (base ** exponent)

    data = (
        # ---------- Section header
        "\x0a\x0d\x0d\x0a"  # Magic number
        "\x00\x00\x00\x20"  # Block size (32 bytes)
        "\x1a\x2b\x3c\x4d"  # Magic number
        "\x00\x01\x00\x00"  # Version
        "\xff\xff\xff\xff\xff\xff\xff\xff"  # Undefined section length
        "\x00\x00\x00\x00"  # Empty options
        "\x00\x00\x00\x20"  # Block size (32 bytes)

        # ---------- Interface description
        '\x00\x00\x00\x01'  # block magic
        '\x00\x00\x00\x20'  # block syze
        '\x00\x01'  # link type
        '\x00\x00'  # reserved block
        '\x00\x00\xff\xff'  # size limit
        '\x00\x09\x00\x01''{tsresol}\x00\x00\x00'  # if_tsresol (+padding)
        '\x00\x00\x00\x00'  # end of options
        '\x00\x00\x00\x20'  # block syze

        # ---------- Enhanced packet
        '\x00\x00\x00\x06'  # block magic
        '\x00\x00\x00\x24'  # block syze
        '\x00\x00\x00\x00'  # interface id (first one, eth0)
        '{timestamp}'  # timestamp
        '\x00\x00\x00\x00'  # Captured length
        '\x00\x00\x00\x00'  # Original length
        # no packet data
        '\x00\x00\x00\x00'  # Empty options
        '\x00\x00\x00\x24'  # block syze
    ).format(timestamp=struct.pack('>Q', timestamp), tsresol=tsresol)

    return data
def test_read_block_enhanced_packet_tsresol_bigendian(tsr_base, tsr_exp):
    data = _generate_file_with_tsresol(tsr_base, tsr_exp)
    scanner = FileScanner(io.BytesIO(data))

    blocks = list(scanner)
    assert len(blocks) == 3

    assert isinstance(blocks[0], SectionHeader)
    assert blocks[0].endianness == ">"
    assert blocks[0].interfaces == {0: blocks[1]}

    assert isinstance(blocks[1], InterfaceDescription)
    assert len(blocks[1].options) == 1  # Just if_tsresol
    assert blocks[1].options["if_tsresol"] == pack_timestamp_resolution(
        tsr_base, tsr_exp)

    assert isinstance(blocks[2], EnhancedPacket)
    assert blocks[2].section == blocks[0]
    assert blocks[2].interface_id == 0
    assert blocks[2].interface == blocks[1]

    resol = tsr_base**tsr_exp
    assert blocks[2].timestamp_resolution == resol
    assert blocks[2].timestamp == 1420070400.0
def test_read_block_enhanced_packet_tsresol_bigendian(tsr_base, tsr_exp):
    data = _generate_file_with_tsresol(tsr_base, tsr_exp)
    scanner = FileScanner(io.BytesIO(data))

    blocks = list(scanner)
    assert len(blocks) == 3

    assert isinstance(blocks[0], SectionHeader)
    assert blocks[0].endianness == '>'
    assert blocks[0].interfaces == {0: blocks[1]}

    assert isinstance(blocks[1], InterfaceDescription)
    assert len(blocks[1].options) == 1  # Just if_tsresol
    assert blocks[1].options['if_tsresol'] == \
        pack_timestamp_resolution(tsr_base, tsr_exp)

    assert isinstance(blocks[2], EnhancedPacket)
    assert blocks[2].section == blocks[0]
    assert blocks[2].interface_id == 0
    assert blocks[2].interface == blocks[1]

    resol = tsr_base ** tsr_exp
    assert blocks[2].timestamp_resolution == resol
    assert blocks[2].timestamp == 1420070400.0