1
0
mirror of https://github.com/JoelBender/bacpypes synced 2025-09-28 22:15:23 +08:00
This commit is contained in:
Joel Bender 2017-10-07 23:41:52 -04:00
commit d71b7eb9a6
9 changed files with 254 additions and 38 deletions

View File

@ -91,7 +91,7 @@ class Address:
self.addrAddr = struct.pack('B', addr)
self.addrLen = 1
elif isinstance(addr, str):
elif isinstance(addr, basestring):
if _debug: Address._debug(" - str")
m = ip_address_mask_port_re.match(addr)
@ -263,7 +263,7 @@ class Address:
addr, port = addr
self.addrPort = int(port)
if isinstance(addr, str):
if isinstance(addr, basestring):
if not addr:
# when ('', n) is passed it is the local host address, but that
# could be more than one on a multihomed machine, the empty string

View File

@ -1073,7 +1073,7 @@ class Enumerated(Atomic):
# convert it to a string if you can
self.value = self._xlate_table.get(arg, arg)
elif isinstance(arg, str):
elif isinstance(arg, basestring):
if arg not in self._xlate_table:
raise ValueError("undefined enumeration '%s'" % (arg,))
self.value = arg
@ -1088,7 +1088,7 @@ class Enumerated(Atomic):
def get_long(self):
if isinstance(self.value, (int, long)):
return self.value
elif isinstance(self.value, str):
elif isinstance(self.value, basestring):
return long(self._xlate_table[self.value])
else:
raise TypeError("%s is an invalid enumeration value datatype" % (type(self.value),))
@ -1132,7 +1132,7 @@ class Enumerated(Atomic):
value = long(self.value)
elif isinstance(self.value, long):
value = self.value
elif isinstance(self.value, str):
elif isinstance(self.value, basestring):
value = self._xlate_table[self.value]
else:
raise TypeError("%s is an invalid enumeration value datatype" % (type(self.value),))
@ -1170,7 +1170,7 @@ class Enumerated(Atomic):
value is wrong for the enumeration, the encoding will fail.
"""
return (isinstance(arg, (int, long)) and (arg >= 0)) or \
isinstance(arg, str)
isinstance(arg, basestring)
def __str__(self):
return "%s(%s)" % (self.__class__.__name__, self.value)
@ -1607,7 +1607,7 @@ class ObjectIdentifier(Atomic):
objType = self.objectTypeClass._xlate_table.get(objType, objType)
elif isinstance(objType, long):
objType = self.objectTypeClass._xlate_table.get(objType, int(objType))
elif isinstance(objType, str):
elif isinstance(objType, basestring):
# make sure the type is known
if objType not in self.objectTypeClass._xlate_table:
raise ValueError("unrecognized object type '%s'" % (objType,))
@ -1629,7 +1629,7 @@ class ObjectIdentifier(Atomic):
pass
elif isinstance(objType, long):
objType = int(objType)
elif isinstance(objType, str):
elif isinstance(objType, basestring):
# turn it back into an integer
objType = self.objectTypeClass()[objType]
else:
@ -1680,7 +1680,7 @@ class ObjectIdentifier(Atomic):
# rip it apart
objType, objInstance = self.value
if isinstance(objType, str):
if isinstance(objType, basestring):
typestr = objType
elif objType < 0:
typestr = "Bad %d" % (objType,)

View File

@ -93,7 +93,7 @@ class Address:
self.addrAddr = struct.pack('B', addr)
self.addrLen = 1
elif isinstance(addr, str):
elif isinstance(addr, basestring):
if _debug: Address._debug(" - str")
m = ip_address_mask_port_re.match(addr)
@ -259,7 +259,7 @@ class Address:
addr, port = addr
self.addrPort = int(port)
if isinstance(addr, str):
if isinstance(addr, basestring):
if not addr:
# when ('', n) is passed it is the local host address, but that
# could be more than one on a multihomed machine, the empty string

View File

@ -1078,7 +1078,7 @@ class Enumerated(Atomic):
# convert it to a string if you can
self.value = self._xlate_table.get(arg, arg)
elif isinstance(arg, str):
elif isinstance(arg, basestring):
if arg not in self._xlate_table:
raise ValueError("undefined enumeration '%s'" % (arg,))
self.value = arg
@ -1093,7 +1093,7 @@ class Enumerated(Atomic):
def get_long(self):
if isinstance(self.value, (int, long)):
return self.value
elif isinstance(self.value, str):
elif isinstance(self.value, basestring):
return long(self._xlate_table[self.value])
else:
raise TypeError("%s is an invalid enumeration value datatype" % (type(self.value),))
@ -1137,7 +1137,7 @@ class Enumerated(Atomic):
value = long(self.value)
elif isinstance(self.value, long):
value = self.value
elif isinstance(self.value, str):
elif isinstance(self.value, basestring):
value = self._xlate_table[self.value]
else:
raise TypeError("%s is an invalid enumeration value datatype" % (type(self.value),))
@ -1613,7 +1613,7 @@ class ObjectIdentifier(Atomic):
objType = self.objectTypeClass._xlate_table.get(objType, objType)
elif isinstance(objType, long):
objType = self.objectTypeClass._xlate_table.get(objType, int(objType))
elif isinstance(objType, str):
elif isinstance(objType, basestring):
# make sure the type is known
if objType not in self.objectTypeClass._xlate_table:
raise ValueError("unrecognized object type '%s'" % (objType,))
@ -1635,7 +1635,7 @@ class ObjectIdentifier(Atomic):
pass
elif isinstance(objType, long):
objType = int(objType)
elif isinstance(objType, str):
elif isinstance(objType, basestring):
# turn it back into an integer
objType = self.objectTypeClass()[objType]
else:
@ -1686,7 +1686,7 @@ class ObjectIdentifier(Atomic):
# rip it apart
objType, objInstance = self.value
if isinstance(objType, str):
if isinstance(objType, basestring):
typestr = objType
elif objType < 0:
typestr = "Bad %d" % (objType,)

View File

@ -74,8 +74,8 @@ class TestAddress(unittest.TestCase, MatchAddressMixin):
with self.assertRaises(ValueError):
Address(256)
def test_address_ipv4(self):
if _debug: TestAddress._debug("test_address_ipv4")
def test_address_ipv4_str(self):
if _debug: TestAddress._debug("test_address_ipv4_str")
# test IPv4 local station address
test_addr = Address("1.2.3.4")
@ -92,16 +92,42 @@ class TestAddress(unittest.TestCase, MatchAddressMixin):
self.match_address(test_addr, 2, None, 6, '01020304bb7f')
assert str(test_addr) == "0x01020304bb7f"
def test_address_eth(self):
if _debug: TestAddress._debug("test_address_eth")
def test_address_ipv4_unicode(self):
if _debug: TestAddress._debug("test_address_ipv4_unicode")
# test IPv4 local station address
test_addr = Address(u"1.2.3.4")
self.match_address(test_addr, 2, None, 6, '01020304BAC0')
assert str(test_addr) == u"1.2.3.4"
# test IPv4 local station address with non-standard port
test_addr = Address(u"1.2.3.4:47809")
self.match_address(test_addr, 2, None, 6, '01020304BAC1')
assert str(test_addr) == u"1.2.3.4:47809"
# test IPv4 local station address with unrecognized port
test_addr = Address(u"1.2.3.4:47999")
self.match_address(test_addr, 2, None, 6, '01020304bb7f')
assert str(test_addr) == u"0x01020304bb7f"
def test_address_eth_str(self):
if _debug: TestAddress._debug("test_address_eth_str")
# test Ethernet local station address
test_addr = Address("01:02:03:04:05:06")
self.match_address(test_addr, 2, None, 6, '010203040506')
assert str(test_addr) == "0x010203040506"
def test_address_local_station(self):
if _debug: TestAddress._debug("test_address_local_station")
def test_address_eth_unicode(self):
if _debug: TestAddress._debug("test_address_eth_unicode")
# test Ethernet local station address
test_addr = Address(u"01:02:03:04:05:06")
self.match_address(test_addr, 2, None, 6, '010203040506')
assert str(test_addr) == u"0x010203040506"
def test_address_local_station_str(self):
if _debug: TestAddress._debug("test_address_local_station_str")
# test integer local station
test_addr = Address("1")
@ -134,16 +160,58 @@ class TestAddress(unittest.TestCase, MatchAddressMixin):
self.match_address(test_addr, 2, None, 2, '0102')
assert str(test_addr) == "0x0102"
def test_address_local_broadcast(self):
if _debug: TestAddress._debug("test_address_local_broadcast")
def test_address_local_station_unicode(self):
if _debug: TestAddress._debug("test_address_local_station_unicode")
# test integer local station
test_addr = Address(u"1")
self.match_address(test_addr, 2, None, 1, '01')
assert str(test_addr) == u"1"
test_addr = Address(u"254")
self.match_address(test_addr, 2, None, 1, 'fe')
assert str(test_addr) == u"254"
# test bad integer string
with self.assertRaises(ValueError):
Address("256")
# test modern hex string
test_addr = Address(u"0x01")
self.match_address(test_addr, 2, None, 1, '01')
assert str(test_addr) == u"1"
test_addr = Address(u"0x0102")
self.match_address(test_addr, 2, None, 2, '0102')
assert str(test_addr) == u"0x0102"
# test old school hex string
test_addr = Address(u"X'01'")
self.match_address(test_addr, 2, None, 1, '01')
assert str(test_addr) == u"1"
test_addr = Address(u"X'0102'")
self.match_address(test_addr, 2, None, 2, '0102')
assert str(test_addr) == u"0x0102"
def test_address_local_broadcast_str(self):
if _debug: TestAddress._debug("test_address_local_broadcast_str")
# test local broadcast
test_addr = Address("*")
self.match_address(test_addr, 1, None, None, None)
assert str(test_addr) == "*"
def test_address_remote_broadcast(self):
if _debug: TestAddress._debug("test_address_remote_broadcast")
def test_address_local_broadcast_unicode(self):
if _debug: TestAddress._debug("test_address_local_broadcast_unicode")
# test local broadcast
test_addr = Address(u"*")
self.match_address(test_addr, 1, None, None, None)
assert str(test_addr) == u"*"
def test_address_remote_broadcast_str(self):
if _debug: TestAddress._debug("test_address_remote_broadcast_str")
# test remote broadcast
test_addr = Address("1:*")
@ -154,8 +222,20 @@ class TestAddress(unittest.TestCase, MatchAddressMixin):
with self.assertRaises(ValueError):
Address("65536:*")
def test_address_remote_station(self):
if _debug: TestAddress._debug("test_address_remote_station")
def test_address_remote_broadcast_unicode(self):
if _debug: TestAddress._debug("test_address_remote_broadcast_unicode")
# test remote broadcast
test_addr = Address(u"1:*")
self.match_address(test_addr, 3, 1, None, None)
assert str(test_addr) == u"1:*"
# test remote broadcast bad network
with self.assertRaises(ValueError):
Address("65536:*")
def test_address_remote_station_str(self):
if _debug: TestAddress._debug("test_address_remote_station_str")
# test integer remote station
test_addr = Address("1:2")
@ -198,14 +278,66 @@ class TestAddress(unittest.TestCase, MatchAddressMixin):
with self.assertRaises(ValueError):
Address("65536:X'02'")
def test_address_global_broadcast(self):
if _debug: TestAddress._debug("test_address_global_broadcast")
def test_address_remote_station_unicode(self):
if _debug: TestAddress._debug("test_address_remote_station_unicode")
# test integer remote station
test_addr = Address(u"1:2")
self.match_address(test_addr, 4, 1, 1, '02')
assert str(test_addr) == u"1:2"
test_addr = Address(u"1:254")
self.match_address(test_addr, 4, 1, 1, 'fe')
assert str(test_addr) == u"1:254"
# test bad network and node
with self.assertRaises(ValueError):
Address(u"65536:2")
with self.assertRaises(ValueError):
Address(u"1:256")
# test modern hex string
test_addr = Address(u"1:0x02")
self.match_address(test_addr, 4, 1, 1, '02')
assert str(test_addr) == u"1:2"
# test bad network
with self.assertRaises(ValueError):
Address(u"65536:0x02")
test_addr = Address(u"1:0x0203")
self.match_address(test_addr, 4, 1, 2, '0203')
assert str(test_addr) == u"1:0x0203"
# test old school hex string
test_addr = Address(u"1:X'02'")
self.match_address(test_addr, 4, 1, 1, '02')
assert str(test_addr) == u"1:2"
test_addr = Address(u"1:X'0203'")
self.match_address(test_addr, 4, 1, 2, '0203')
assert str(test_addr) == u"1:0x0203"
# test bad network
with self.assertRaises(ValueError):
Address(u"65536:X'02'")
def test_address_global_broadcast_str(self):
if _debug: TestAddress._debug("test_address_global_broadcast_str")
# test local broadcast
test_addr = Address("*:*")
self.match_address(test_addr, 5, None, None, None)
assert str(test_addr) == "*:*"
def test_address_global_broadcast_unicode(self):
if _debug: TestAddress._debug("test_address_global_broadcast_unicode")
# test local broadcast
test_addr = Address(u"*:*")
self.match_address(test_addr, 5, None, None, None)
assert str(test_addr) == u"*:*"
@bacpypes_debugging
class TestLocalStation(unittest.TestCase, MatchAddressMixin):
@ -368,8 +500,8 @@ class TestGlobalBroadcast(unittest.TestCase, MatchAddressMixin):
@bacpypes_debugging
class TestAddressEquality(unittest.TestCase, MatchAddressMixin):
def test_address_equality(self):
if _debug: TestAddressEquality._debug("test_address_equality")
def test_address_equality_str(self):
if _debug: TestAddressEquality._debug("test_address_equality_str")
assert Address(1) == LocalStation(1)
assert Address("2") == LocalStation(2)
@ -377,3 +509,14 @@ class TestAddressEquality(unittest.TestCase, MatchAddressMixin):
assert Address("3:4") == RemoteStation(3, 4)
assert Address("5:*") == RemoteBroadcast(5)
assert Address("*:*") == GlobalBroadcast()
def test_address_equality_unicode(self):
if _debug: TestAddressEquality._debug("test_address_equality_unicode")
assert Address(1) == LocalStation(1)
assert Address(u"2") == LocalStation(2)
assert Address(u"*") == LocalBroadcast()
assert Address(u"3:4") == RemoteStation(3, 4)
assert Address(u"5:*") == RemoteBroadcast(5)
assert Address(u"*:*") == GlobalBroadcast()

View File

@ -93,6 +93,13 @@ class TestCharacterString(unittest.TestCase):
assert obj.value == "hello"
assert str(obj) == "CharacterString(0,X'68656c6c6f')"
def test_character_string_unicode(self):
if _debug: TestCharacterString._debug("test_character_string_unicode")
obj = CharacterString(u"hello")
assert obj.value == u"hello"
assert str(obj) == "CharacterString(0,X'68656c6c6f')"
def test_character_string_tag(self):
if _debug: TestCharacterString._debug("test_character_string_tag")
@ -126,4 +133,4 @@ class TestCharacterString(unittest.TestCase):
obj = CharacterString(character_string_tag(''))
character_string_endec("", '00')
character_string_endec("abc", '00616263')
character_string_endec("abc", '00616263')

View File

@ -18,6 +18,14 @@ _debug = 0
_log = ModuleLogger(globals())
class QuickBrownFox(Enumerated):
enumerations = {
'quick': 0,
'brown': 1,
'fox': 2,
}
@bacpypes_debugging
def enumerated_tag(x):
"""Convert a hex string to an enumerated application tag."""
@ -93,6 +101,38 @@ class TestEnumerated(unittest.TestCase):
with self.assertRaises(ValueError):
Enumerated(-1)
def test_enumerated_str(self):
if _debug: TestEnumerated._debug("test_enumerated_str")
obj = QuickBrownFox('quick')
assert obj.value == 'quick'
assert str(obj) == "QuickBrownFox(quick)"
with self.assertRaises(ValueError):
QuickBrownFox(-1)
with self.assertRaises(ValueError):
QuickBrownFox('lazyDog')
tag = Tag(Tag.applicationTagClass, Tag.enumeratedAppTag, 1, xtob('01'))
obj = QuickBrownFox(tag)
assert obj.value == 'brown'
def test_enumerated_unicode(self):
if _debug: TestEnumerated._debug("test_enumerated_unicode")
obj = QuickBrownFox(u'quick')
assert obj.value == u'quick'
assert str(obj) == "QuickBrownFox(quick)"
with self.assertRaises(ValueError):
QuickBrownFox(-1)
with self.assertRaises(ValueError):
QuickBrownFox(u'lazyDog')
tag = Tag(Tag.applicationTagClass, Tag.enumeratedAppTag, 1, xtob('01'))
obj = QuickBrownFox(tag)
assert obj.value == u'brown'
def test_enumerated_tag(self):
if _debug: TestEnumerated._debug("test_enumerated_tag")
@ -138,4 +178,4 @@ class TestEnumerated(unittest.TestCase):
enumerated_endec(8388608, '800000')
enumerated_endec(2147483647, '7fffffff')
enumerated_endec(2147483648, '80000000')
enumerated_endec(2147483648, '80000000')

View File

@ -97,6 +97,12 @@ class TestObjectIdentifier(unittest.TestCase):
def test_object_identifier_tuple(self):
if _debug: TestObjectIdentifier._debug("test_object_identifier_tuple")
obj = ObjectIdentifier(('analogInput', 0))
assert obj.value == ('analogInput', 0)
obj = ObjectIdentifier((u'analogInput', 0))
assert obj.value == (u'analogInput', 0)
with self.assertRaises(ValueError):
ObjectIdentifier((0, -1))
with self.assertRaises(ValueError):
@ -136,5 +142,7 @@ class TestObjectIdentifier(unittest.TestCase):
# test standard types
object_identifier_endec(('analogInput', 0), '00000000')
object_identifier_endec((u'analogInput', 0), '00000000')
# test vendor types
# test vendor types

View File

@ -23,7 +23,7 @@ class MyObjectType(ObjectType):
'myAnalogInput': 128,
'myAnalogOutput': 129,
'myAnalogValue': 130,
}
}
expand_enumerations(MyObjectType)
@ -113,6 +113,13 @@ class TestObjectType(unittest.TestCase):
obj = ObjectType('analogInput')
assert obj.value == 'analogInput'
def test_object_type_unicode(self):
if _debug: TestObjectType._debug("test_object_type_unicode")
# known strings are accepted
obj = ObjectType(u'analogInput')
assert obj.value == u'analogInput'
def test_extended_object_type_int(self):
if _debug: TestObjectType._debug("test_extended_object_type_int")
@ -137,6 +144,17 @@ class TestObjectType(unittest.TestCase):
with self.assertRaises(ValueError):
MyObjectType('snork')
def test_extended_object_type_unicode(self):
if _debug: TestObjectType._debug("test_extended_object_type_unicode")
# known strings are accepted
obj = MyObjectType(u'myAnalogInput')
assert obj.value == u'myAnalogInput'
# unknown strings are rejected
with self.assertRaises(ValueError):
MyObjectType(u'snork')
def test_object_type_tag(self):
if _debug: TestObjectType._debug("test_object_type_tag")
@ -174,4 +192,4 @@ class TestObjectType(unittest.TestCase):
object_type_endec('analogOutput', '01')
object_type_endec(127, '7f')
object_type_endec(128, '80')
object_type_endec(128, '80')