Skip to content

Commit

Permalink
decoder: add console script, add basic tests
Browse files Browse the repository at this point in the history
  • Loading branch information
svinota committed Mar 19, 2024
1 parent 29e6f3c commit 3ee38a9
Show file tree
Hide file tree
Showing 7 changed files with 1,674 additions and 15 deletions.
8 changes: 8 additions & 0 deletions noxfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,14 @@ def unit(session, config):
session.run(*options('test_unit', config))


@nox.session
@add_session_config
def decoder(session, config):
'''Run decoder tests.'''
setup_venv_dev(session)
session.run(*options('test_decoder', config))


@nox.session
@add_session_config
def integration(session, config):
Expand Down
28 changes: 16 additions & 12 deletions pyroute2/decoder/loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,17 +56,17 @@ def decode(self):
self.exception = repr(e)
self.msg = hexdump(self.data)

def dump(self):
return {
"pcap header": repr(self.packet_header),
"link layer header": repr(self.ll_header),
"message class": repr(self.cls),
"exception": self.exception,
"data": self.msg,
}

def __repr__(self):
return json.dumps(
{
"pcap header": repr(self.packet_header),
"link layer header": repr(self.ll_header),
"message class": repr(self.cls),
"exception": self.exception,
"data": self.msg,
},
indent=4,
)
return json.dumps(self.dump(), indent=4)


class MatchOps:
Expand All @@ -91,8 +91,9 @@ def f(packet_header, ll_header, raw, data_offset, stack):

@staticmethod
def ll_header(family):
if not isinstance(family, int) or family < 0 or family > 0xffff:
if not isinstance(family, int) or family < 0 or family > 0xFFFF:
raise TypeError('family must be unsigned short integer')

def f(packet_header, ll_header, raw, data_offset, stack):
return ll_header.family == family

Expand All @@ -104,6 +105,7 @@ def data(fmt, offset, value):
raise TypeError('format must be string')
if not isinstance(offset, int) or not isinstance(value, int):
raise TypeError('offset and value must be integers')

def f(packet_header, ll_header, raw, data_offset, stack):
o = data_offset + offset
s = struct.calcsize(fmt)
Expand Down Expand Up @@ -193,7 +195,9 @@ def data(self):
ll_header = self.decode_ll_header(self.raw, self.offset)
self.offset += ll_header.header_len
length = packet_header.incl_len - ll_header.header_len
if self.matcher.match(packet_header, ll_header, self.raw, self.offset):
if self.matcher.match(
packet_header, ll_header, self.raw, self.offset
):
msg = Message(
packet_header,
ll_header,
Expand Down
10 changes: 7 additions & 3 deletions pyroute2/decoder/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,18 @@
A cli tool to decode netlink buffers.
'''

from args import args
from loader import get_loader
import json

from pyroute2.decoder.args import args
from pyroute2.decoder.loader import get_loader


def run():
loader = get_loader(args)
ret = []
for message in loader.data:
print(message)
ret.append(message.dump())
print(json.dumps(ret, indent=4))


if __name__ == "__main__":
Expand Down
1 change: 1 addition & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -39,5 +39,6 @@ packages = find:
console_scripts =
ss2 = pyroute2.netlink.diag.ss2:run [psutil]
pyroute2-cli = pyroute2.ndb.cli:run
pyroute2-decoder = pyroute2.decoder.main:run
pyroute2-dhcp-client = pyroute2.dhcp.client:run
pyroute2-test-platform = pyroute2.config.test_platform:run
Loading

0 comments on commit 3ee38a9

Please sign in to comment.