Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Final cleanups before version 2 can be released. #30

Merged
merged 9 commits into from
Sep 13, 2020
12 changes: 6 additions & 6 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v2.3.0
rev: v3.2.0
hooks:
- id: check-yaml
- id: end-of-file-fixer
- id: trailing-whitespace
- repo: https://github.com/timothycrosley/isort
rev: 4.3.21
- repo: https://github.com/PyCQA/isort
rev: 5.5.1
hooks:
- id: isort
additional_dependencies:
- toml
- repo: https://github.com/python/black
rev: 19.10b0
- repo: https://github.com/psf/black
rev: 20.8b1
hooks:
- id: black
- repo: https://gitlab.com/pycqa/flake8
rev: 3.7.9
rev: 3.8.3
hooks:
- id: flake8
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,4 @@ install:
- pip install .[dev]

script:
- py.test -vvv --pycodestyle --cov=pcapng --cov-report=term-missing tests
- py.test -vvv --cov=pcapng --cov-report=term-missing tests
4 changes: 2 additions & 2 deletions pcapng/flags.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ def __init__(self, owner, offset, size, extra=None):
if len(extra) > 2 ** size:
raise TypeError(
"{cls} iterable has too many values (got {got}, "
+ "{size} bits only address {max})".format(
"{size} bits only address {max})".format(
cls=self.__class__.__name__,
got=len(extra),
size=size,
Expand Down Expand Up @@ -156,7 +156,7 @@ def __init__(self, schema, nbits=32, initial=0):
if tot_bits > nbits:
raise TypeError(
"Too many fields for {nbits}-bit field "
+ "(schema defines {tot} bits)".format(nbits=nbits, tot=tot_bits)
"(schema defines {tot} bits)".format(nbits=nbits, tot=tot_bits)
)

bitn = 0
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,4 @@ multi_line_output = 3
include_trailing_comma = true

known_first_party = ['pcapng']
known_third_party = ['pytest', 'scapy', 'six']
known_third_party = ['pytest', 'scapy']
27 changes: 22 additions & 5 deletions setup.cfg
Original file line number Diff line number Diff line change
@@ -1,23 +1,40 @@
[metadata]
name = python-pcapng
description = Library to read/write the pcap-ng format used by various packet sniffers.
long_description = file: README.rst
long_description_content_type = text/x-rst
url = pythhttps://github.com/rshk/python-pcapng
author = Samuele Santi
author_email = [email protected]
description = Library to read/write the pcap-ng format used by various packet sniffers.
long_description = file: README.rst, CHANGELOG.rst
long_description_content_type = text/x-rst
license = Apache Software License 2.0
license = Apache-2.0
license_file = LICENSE
license_files =
LICENSE
classifiers =
License :: OSI Approved :: Apache Software License
Development Status :: 5 - Production/Stable
License :: OSI Approved :: Apache Software License
Programming Language :: Python :: 3.5
Programming Language :: Python :: 3.6
Programming Language :: Python :: 3.7
Programming Language :: Python :: 3.8
Programming Language :: Python :: Implementation :: CPython

[options]
packages = find:
python_requires = ~= 3.5
zip_safe = False

[options.extras_require]
dev =
flake8
isort
pre-commit
pytest>=5.4
pytest-cov
setuptools_scm[toml]
sphinx
sphinx-rtd-theme

[flake8]
max-line-length = 88

Expand Down
23 changes: 2 additions & 21 deletions setup.py
Original file line number Diff line number Diff line change
@@ -1,23 +1,4 @@
from setuptools import find_packages, setup

import setuptools_scm # noqa: F401
from setuptools import setup

setup(
packages=find_packages(),
install_requires=["six"],
extras_require={
"dev": [
"isort",
"pytest>=5.4",
"pytest-cov",
"pytest-pycodestyle",
"flake8",
"pre-commit",
"setuptools_scm[toml]",
"sphinx",
"sphinx-rtd-theme",
],
},
package_data={"": ["README.rst", "CHANGELOG.rst", "LICENSE"]},
zip_safe=False,
)
setup()
Empty file.
83 changes: 40 additions & 43 deletions tests/test_parse_enhanced_packet.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
import struct

import pytest
import six

from pcapng.blocks import EnhancedPacket, InterfaceDescription, SectionHeader
from pcapng.scanner import FileScanner
Expand All @@ -12,48 +11,46 @@
def test_read_block_enhanced_packet_bigendian():
scanner = FileScanner(
io.BytesIO(
six.b(
# ---------- 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\x40" # block syze (64 bytes)
"\x00\x01" # link type
"\x00\x00" # reserved block
"\x00\x00\xff\xff" # size limit
"\x00\x02\x00\x04"
"eth0" # if_name
"\x00\x09\x00\x01"
"\x06\x00\x00\x00" # if_tsresol (+padding)
"\x00\x0c\x00\x13"
"Linux 3.2.0-4-amd64\x00" # if_os
"\x00\x00\x00\x00" # end of options
"\x00\x00\x00\x40" # block syze (64 bytes)
# ---------- Enhanced packet
"\x00\x00\x00\x06" # block magic
"\x00\x00\x00\x78" # block syze (120 bytes)
"\x00\x00\x00\x00" # interface id (first one, eth0)
"\x00\x04\xf8\x1e"
"\x3c\x3e\xd5\xa9" # timestamp (microseconds)
"\x00\x00\x00\x51" # Captured length
"\x00\x00\x00\x51" # Original length
# Packet data (81 bytes)
"\x00\x02\x157\xa2D\x00\xae\xf3R\xaa\xd1\x08\x00" # Ethernet
"E\x00\x00C\x00\x01\x00\x00@\x06x<\xc0\xa8\x05\x15B#\xfa\x97" # IP
"\x00\x14\x00P\x00\x00\x00\x00\x00\x00\x00\x00P\x02 " # TCP
"\x00\xbb9\x00\x00" # TCP(cont)
"GET /index.html HTTP/1.0 \n\n" # HTTP
"\x00\x00\x00" # Padding
# todo: add options?
"\x00\x00\x00\x00" # Empty options
"\x00\x00\x00\x78" # block syze (120 bytes)
)
# ---------- Section header
b"\x0a\x0d\x0d\x0a" # Magic number
b"\x00\x00\x00\x20" # Block size (32 bytes)
b"\x1a\x2b\x3c\x4d" # Magic number
b"\x00\x01\x00\x00" # Version
b"\xff\xff\xff\xff\xff\xff\xff\xff" # Undefined section length
b"\x00\x00\x00\x00" # Empty options
b"\x00\x00\x00\x20" # Block size (32 bytes)
# ---------- Interface description
b"\x00\x00\x00\x01" # block magic
b"\x00\x00\x00\x40" # block syze (64 bytes)
b"\x00\x01" # link type
b"\x00\x00" # reserved block
b"\x00\x00\xff\xff" # size limit
b"\x00\x02\x00\x04"
b"eth0" # if_name
b"\x00\x09\x00\x01"
b"\x06\x00\x00\x00" # if_tsresol (+padding)
b"\x00\x0c\x00\x13"
b"Linux 3.2.0-4-amd64\x00" # if_os
b"\x00\x00\x00\x00" # end of options
b"\x00\x00\x00\x40" # block syze (64 bytes)
# ---------- Enhanced packet
b"\x00\x00\x00\x06" # block magic
b"\x00\x00\x00\x78" # block syze (120 bytes)
b"\x00\x00\x00\x00" # interface id (first one, eth0)
b"\x00\x04\xf8\x1e"
b"\x3c\x3e\xd5\xa9" # timestamp (microseconds)
b"\x00\x00\x00\x51" # Captured length
b"\x00\x00\x00\x51" # Original length
# Packet data (81 bytes)
b"\x00\x02\x157\xa2D\x00\xae\xf3R\xaa\xd1\x08\x00" # Ethernet
b"E\x00\x00C\x00\x01\x00\x00@\x06x<\xc0\xa8\x05\x15B#\xfa\x97" # IP
b"\x00\x14\x00P\x00\x00\x00\x00\x00\x00\x00\x00P\x02 " # TCP
b"\x00\xbb9\x00\x00" # TCP(cont)
b"GET /index.html HTTP/1.0 \n\n" # HTTP
b"\x00\x00\x00" # Padding
# todo: add options?
b"\x00\x00\x00\x00" # Empty options
b"\x00\x00\x00\x78" # block syze (120 bytes)
)
)

Expand Down
9 changes: 4 additions & 5 deletions tests/test_structs.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import struct

import pytest
import six

from pcapng.exceptions import BadMagic, CorruptedFile, StreamEmpty, TruncatedFile
from pcapng.structs import (
Expand Down Expand Up @@ -296,16 +295,16 @@ def test_options_object():
assert options["opt_comment"] == "Comment #1"
assert options[1] == "Comment #1"
assert options.get_all("opt_comment") == ["Comment #1", "Comment #2"]
assert isinstance(options["opt_comment"], six.text_type)
assert isinstance(options["opt_comment"], str)

assert options["spam"] == b"I love spam spam spam!"
assert isinstance(options["spam"], six.binary_type)
assert isinstance(options["spam"], bytes)

assert options["eggs"] == 0x100
assert isinstance(options["eggs"], six.integer_types)
assert isinstance(options["eggs"], int)

assert options["bacon"] == "Bacon is delicious!"
assert isinstance(options["bacon"], six.text_type)
assert isinstance(options["bacon"], str)

with pytest.raises(KeyError):
options["missing"]
Expand Down
36 changes: 17 additions & 19 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
from six import int2byte

from pcapng.utils import (
pack_timestamp_resolution,
unpack_euiaddr,
Expand Down Expand Up @@ -34,24 +32,24 @@ def test_unpack_euiaddr():


def test_unpack_tsresol():
assert unpack_timestamp_resolution(int2byte(0)) == 1
assert unpack_timestamp_resolution(int2byte(1)) == 1e-1
assert unpack_timestamp_resolution(int2byte(6)) == 1e-6
assert unpack_timestamp_resolution(int2byte(100)) == 1e-100
assert unpack_timestamp_resolution(bytes((0,))) == 1
assert unpack_timestamp_resolution(bytes((1,))) == 1e-1
assert unpack_timestamp_resolution(bytes((6,))) == 1e-6
assert unpack_timestamp_resolution(bytes((100,))) == 1e-100

assert unpack_timestamp_resolution(int2byte(0 | 0b10000000)) == 1
assert unpack_timestamp_resolution(int2byte(1 | 0b10000000)) == 2 ** -1
assert unpack_timestamp_resolution(int2byte(6 | 0b10000000)) == 2 ** -6
assert unpack_timestamp_resolution(int2byte(100 | 0b10000000)) == 2 ** -100
assert unpack_timestamp_resolution(bytes((0 | 0b10000000,))) == 1
assert unpack_timestamp_resolution(bytes((1 | 0b10000000,))) == 2 ** -1
assert unpack_timestamp_resolution(bytes((6 | 0b10000000,))) == 2 ** -6
assert unpack_timestamp_resolution(bytes((100 | 0b10000000,))) == 2 ** -100


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)
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,))