From ad17842c7121bc9f7f3924ffbf73dde9dbdea960 Mon Sep 17 00:00:00 2001 From: Oscar Date: Mon, 30 Aug 2021 10:46:38 -0700 Subject: [PATCH] proper unit test to address coverage drop (#613) --- dpkt/dpkt.py | 31 +++++++++++++++++++++++++------ 1 file changed, 25 insertions(+), 6 deletions(-) diff --git a/dpkt/dpkt.py b/dpkt/dpkt.py index 6ba45169..29de5951 100644 --- a/dpkt/dpkt.py +++ b/dpkt/dpkt.py @@ -500,8 +500,15 @@ def unpack(self, buf): def test_repr(): + """complex test for __repr__, __public_fields__""" + class TestPacket(Packet): - __hdr__ = (('_a_b', 'B', 0),) + __hdr__ = ( + ('_a_b', 'B', 1), # 'a' and 'b' bit fields + ('_rsv', 'B', 0), # hidden reserved field + ('_c_flag', 'B', 1), # 'c_flag' property + ('d', 'B', 0) # regular field + ) __bit_fields__ = { '_a_b': ( @@ -510,10 +517,22 @@ class TestPacket(Packet): ), } - # default values so no output + @property + def c_flag(self): + return (self.a | self.b) + + # init with default values test_packet = TestPacket() - assert repr(test_packet) == "TestPacket()" - # non-default values - test_packet = TestPacket(b'\x12') - assert repr(test_packet) == "TestPacket(a=1, b=2)" + # test repr with all default values so expect no output + # (except for the explicitly defined property, where dpkt doesn't process defaults yet) + assert repr(test_packet) == "TestPacket(c_flag=1)" + + # init with non-default values + test_packet = TestPacket(b'\x12\x11\x00\x04') + + # ensure the display fields were cached and propagated via class attribute + assert test_packet.__public_fields__ == ['a', 'b', 'c_flag', 'd'] + + # verify repr + assert repr(test_packet) == "TestPacket(a=1, b=2, c_flag=3, d=4)"