From 7087f9af69d13fc90544fc280100aa061f5a0265 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Przemys=C5=82aw=20Fierek?= Date: Wed, 14 Dec 2016 17:58:24 +0100 Subject: [PATCH] Fix spurious test errors in test_ipv6.py (#1072) --- tests/scripts/thread-cert/ipv6.py | 24 +++++++++++++++++------- tests/scripts/thread-cert/test_ipv6.py | 10 ++++++++-- 2 files changed, 25 insertions(+), 9 deletions(-) diff --git a/tests/scripts/thread-cert/ipv6.py b/tests/scripts/thread-cert/ipv6.py index 961959ab9..9a689c8d9 100644 --- a/tests/scripts/thread-cert/ipv6.py +++ b/tests/scripts/thread-cert/ipv6.py @@ -27,18 +27,19 @@ # POSSIBILITY OF SUCH DAMAGE. # -try: - from itertools import izip_longest as zip_longest -except ImportError: - from itertools import zip_longest - -from ipaddress import ip_address - import abc import io import struct import sys +from binascii import hexlify +from ipaddress import ip_address + +try: + from itertools import izip_longest as zip_longest +except ImportError: + from itertools import zip_longest + # Next headers for IPv6 protocols IPV6_NEXT_HEADER_HOP_BY_HOP = 0 @@ -696,6 +697,9 @@ class HopByHopOptionHeader(ConvertibleToBytes, BuildableFromBytes): def __len__(self): return self._header_length + def __repr__(self): + return "HopByHopOptionHeader(type={}, length={})".format(self.type, self.length) + class HopByHopOption(ConvertibleToBytes): @@ -724,6 +728,9 @@ class HopByHopOption(ConvertibleToBytes): def __len__(self): return len(self.header) + len(self.value) + def __repr__(self): + return "HopByHopOption(header={}, value={})".format(self.header, self.value) + class MPLOption(ConvertibleToBytes): @@ -766,6 +773,9 @@ class MPLOption(ConvertibleToBytes): def __len__(self): return self._header_length + self._seed_id_length[self.S] + def __repr__(self): + return "MPLOption(S={}, M={}, V={}, sequence={}, seed_id={})".format(self.S, self.M, self.V, self.sequence, hexlify(self.seed_id)) + class IPv6PacketFactory(PacketFactory): diff --git a/tests/scripts/thread-cert/test_ipv6.py b/tests/scripts/thread-cert/test_ipv6.py index e931feedb..8aa65b349 100755 --- a/tests/scripts/thread-cert/test_ipv6.py +++ b/tests/scripts/thread-cert/test_ipv6.py @@ -259,7 +259,13 @@ def any_mpl_option(): def any_hop_by_hop_bytes_option_header(length=4): - return HopByHopOptionHeader(any_type(), length) + _type = any_type() + + # 0 or 1 means padding, so type have to be higher than 1 + while _type <= 1: + _type = any_type() + + return HopByHopOptionHeader(_type, length) def any_hop_by_hop_bytes_value(length=2): @@ -925,7 +931,7 @@ class TestHopByHopFactory(unittest.TestCase): return bytearray([0x00]) elif padding_length > 1: padding_length -= 2 - return bytearray([0x01, padding_length]) + bytes([0x00 for _ in range(padding_length)]) + return bytearray([0x01, padding_length]) + bytearray([0x00 for _ in range(padding_length)]) return bytearray()