Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
xxtob = lambda s: xtob(''.join(s.split()).replace('.', ''))
def context_endec(tnum, x, y):
"""Convert the value (a primitive object) to a hex encoded string,
convert the hex encoded string to and object, and compare the results to
each other."""
if _debug: context_endec._debug("context_endec %r %r %r", tnum, x, y)
# convert the hex strings to a blobs
tdata = xtob(x)
blob1 = xtob(y)
# make a context tag
tag1 = ContextTag(tnum, tdata)
# decode the blob into a tag
tag2 = context_decode(blob1)
if _debug: context_endec._debug(" - tag: %r", tag)
# encode the tag into a blob
blob2 = context_encode(tag1)
if _debug: context_endec._debug(" - blob2: %r", blob2)
# compare the results
assert tag1 == tag2
assert blob1 == blob2
def null_tag(x):
"""Convert a hex string to an integer application tag."""
if _debug: null_tag._debug("null_tag %r", x)
b = xtob(x)
tag = Tag(Tag.applicationTagClass, Tag.nullAppTag, len(b), b)
if _debug: integer_endec._debug(" - tag: %r", tag)
return tag
def time_tag(x):
"""Convert a hex string to an time application tag."""
if _debug: time_tag._debug("time_tag %r", x)
b = xtob(x)
tag = Tag(Tag.applicationTagClass, Tag.timeAppTag, len(b), b)
if _debug: time_endec._debug(" - tag: %r", tag)
return tag
def octet_string_tag(x):
"""Convert a hex string to an octet_string application tag."""
if _debug: octet_string_tag._debug("octet_string_tag %r", x)
b = xtob(x)
tag = Tag(Tag.applicationTagClass, Tag.octetStringAppTag, len(b), b)
if _debug: octet_string_endec._debug(" - tag: %r", tag)
return tag
def octet_string_endec(x):
"""Pass the value to OctetString, construct a tag from the hex string,
and compare results of encode and decoding each other."""
if _debug: octet_string_endec._debug("octet_string_endec %r", x)
tag = octet_string_tag(x)
if _debug: octet_string_endec._debug(" - tag: %r, %r", tag, tag.tagData)
obj = OctetString(xtob(x))
if _debug: octet_string_endec._debug(" - obj: %r, %r", obj, obj.value)
assert octet_string_encode(obj) == tag
assert octet_string_decode(tag) == obj
def test_character_string_unicode_with_latin(self):
if _debug: TestCharacterString._debug("test_character_string_unicode_with_latin")
# some controllers encoding character string mixing latin-1 and utf-8
# try to cover those cases without failing
b = xtob('0030b043') # zero degress celsius
tag = Tag(Tag.applicationTagClass, Tag.characterStringAppTag, len(b), b)
obj = CharacterString()
obj.decode(tag)
assert str(obj) == "CharacterString(0,X'30b043')"
if sys.version_info[0] == 2:
assert obj.value == "0C" # degree symbol dropped, see unicodedata.normalize()
elif sys.version_info[0] == 3:
assert obj.value == "0°C"
else:
raise RuntimeError("unsupported version")
def test_time_tag(self):
if _debug: TestTime._debug("test_time_tag")
tag = Tag(Tag.applicationTagClass, Tag.timeAppTag, 1, xtob('01020304'))
obj = Time(tag)
assert obj.value == (1, 2, 3, 4)
tag = Tag(Tag.applicationTagClass, Tag.booleanAppTag, 0, xtob(''))
with self.assertRaises(InvalidTag):
Time(tag)
tag = Tag(Tag.contextTagClass, 0, 1, xtob('ff'))
with self.assertRaises(InvalidTag):
Time(tag)
tag = Tag(Tag.openingTagClass, 0)
with self.assertRaises(InvalidTag):
Time(tag)
def test_initialize_routing_table_01(self):
"""Test the RouterAvailableToNetwork with a routing table entry."""
if _debug: TestNPDUCodec._debug("test_initialize_routing_table_01")
# build a routing table entry
rte = RoutingTableEntry(1, 2, xtob(''))
rt_entries = [rte]
# Request successful
pdu_bytes = xtob('01.80' # version, network layer message
'06 01' # message type and list length
'0001 02 00' # network, port number, port info
)
self.request(InitializeRoutingTable(rt_entries))
self.indication(pduData=pdu_bytes)
self.response(PDU(pdu_bytes))
self.confirmation(InitializeRoutingTable, irtTable=rt_entries)