Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_text(self):
t1 = ua.LocalizedText('Root')
t2 = ua.LocalizedText('Root')
t3 = ua.LocalizedText('root')
self.assertEqual(t1, t2)
self.assertNotEqual(t1, t3)
t4 = ua.LocalizedText.from_binary(ua.utils.Buffer(t1.to_binary()))
self.assertEqual(t1, t4)
def test_variant_array(self):
v = ua.Variant([1, 2, 3, 4, 5])
self.assertEqual(v.Value[1], 2)
v2 = variant_from_binary(ua.utils.Buffer(variant_to_binary(v)))
self.assertEqual(v.Value, v2.Value)
self.assertEqual(v.VariantType, v2.VariantType)
now = datetime.utcnow()
v = ua.Variant([now])
self.assertEqual(v.Value[0], now)
self.assertEqual(v.VariantType, ua.VariantType.DateTime)
v2 = variant_from_binary(ua.utils.Buffer(variant_to_binary(v)))
self.assertEqual(v.Value, v2.Value)
self.assertEqual(v.VariantType, v2.VariantType)
def test_message_chunk(self):
pol = ua.SecurityPolicy()
chunks = ua.MessageChunk.message_to_chunks(pol, b'123', 65536)
self.assertEqual(len(chunks), 1)
seq = 0
for chunk in chunks:
seq += 1
chunk.SequenceHeader.SequenceNumber = seq
chunk2 = ua.MessageChunk.from_binary(pol, ua.utils.Buffer(chunks[0].to_binary()))
self.assertEqual(chunks[0].to_binary(), chunk2.to_binary())
# for policy None, MessageChunk overhead is 12+4+8 = 24 bytes
# Let's pack 11 bytes into 28-byte chunks. The message must be split as 4+4+3
chunks = ua.MessageChunk.message_to_chunks(pol, b'12345678901', 28)
self.assertEqual(len(chunks), 3)
self.assertEqual(chunks[0].Body, b'1234')
self.assertEqual(chunks[1].Body, b'5678')
self.assertEqual(chunks[2].Body, b'901')
for chunk in chunks:
seq += 1
chunk.SequenceHeader.SequenceNumber = seq
self.assertTrue(len(chunk.to_binary()) <= 28)
def test_unknown_extension_object(self):
obj = ua.ExtensionObject()
obj.Body = b'example of data in custom format'
obj.TypeId = ua.NodeId.from_string('ns=3;i=42')
data = ua.utils.Buffer(extensionobject_to_binary(obj))
obj2 = ua.extensionobject_from_binary(data)
self.assertEqual(type(obj2), ua.ExtensionObject)
self.assertEqual(obj2.TypeId, obj.TypeId)
self.assertEqual(obj2.Body, b'example of data in custom format')
def test_custom_variant(self):
with self.assertRaises(ua.UaError):
v = ua.Variant(b"ljsdfljds", ua.VariantTypeCustom(89))
v = ua.Variant(b"ljsdfljds", ua.VariantTypeCustom(61))
v2 = ua.Variant.from_binary(ua.utils.Buffer(v.to_binary()))
self.assertEqual(v.VariantType, v2.VariantType)
self.assertEqual(v, v2)
def test_custom_structs(self):
xmlpath = "tests/example.bsd"
c = StructGenerator()
c.make_model_from_file(xmlpath)
c.save_to_file("tests/structures.py")
import structures as s
# test with default values
v = s.ScalarValueDataType()
data = struct_to_binary(v)
v2 = struct_from_binary(s.ScalarValueDataType, ua.utils.Buffer(data))
# set some values
v = s.ScalarValueDataType()
v.SbyteValue = 1
v.ByteValue = 2
v.Int16Value = 3
v.UInt16Value = 4
v.Int32Value = 5
v.UInt32Value = 6
v.Int64Value = 7
v.UInt64Value = 8
v.FloatValue = 9.0
v.DoubleValue = 10.0
v.StringValue = "elleven"
v.DateTimeValue = datetime.utcnow()
def test_extension_object(self):
obj = ua.UserNameIdentityToken()
obj.UserName = "admin"
obj.Password = b"pass"
obj2 = ua.extensionobject_from_binary(ua.utils.Buffer(extensionobject_to_binary(obj)))
self.assertEqual(type(obj), type(obj2))
self.assertEqual(obj.UserName, obj2.UserName)
self.assertEqual(obj.Password, obj2.Password)
v1 = ua.Variant(obj)
v2 = ua.Variant.from_binary(ua.utils.Buffer(v1.to_binary()))
self.assertEqual(type(v1), type(v2))
self.assertEqual(v1.VariantType, v2.VariantType)
def test_variant_array(self):
v = ua.Variant([1, 2, 3, 4, 5])
self.assertEqual(v.Value[1], 2)
# self.assertEqual(v.VarianType, ua.VariantType.Int64) # we do not care, we should aonly test for sutff that matter
v2 = ua.Variant.from_binary(ua.utils.Buffer(v.to_binary()))
self.assertEqual(v.Value, v2.Value)
self.assertEqual(v.VariantType, v2.VariantType)
now = datetime.utcnow()
v = ua.Variant([now])
self.assertEqual(v.Value[0], now)
self.assertEqual(v.VariantType, ua.VariantType.DateTime)
v2 = ua.Variant.from_binary(ua.utils.Buffer(v.to_binary()))
self.assertEqual(v.Value, v2.Value)
self.assertEqual(v.VariantType, v2.VariantType)
def test_extension_object(self):
obj = ua.UserNameIdentityToken()
obj.UserName = "admin"
obj.Password = b"pass"
obj2 = extensionobject_from_binary(ua.utils.Buffer(extensionobject_to_binary(obj)))
self.assertEqual(type(obj), type(obj2))
self.assertEqual(obj.UserName, obj2.UserName)
self.assertEqual(obj.Password, obj2.Password)
v1 = ua.Variant(obj)
v2 = variant_from_binary(ua.utils.Buffer(variant_to_binary(v1)))
self.assertEqual(type(v1), type(v2))
self.assertEqual(v1.VariantType, v2.VariantType)
def handle(self):
sock = ua.utils.SocketWrapper(self.request)
processor = UaProcessor(self.server.internal_server, sock, self.client_address)
try:
while True:
hdr = ua.Header.from_string(sock)
body = sock.read(hdr.body_size)
ret = processor.process(hdr, ua.utils.Buffer(body))
if not ret:
break
except ua.utils.SocketClosedException:
logger.warning("Server has closed connection")
except Exception:
logger.exception("Exception raised while parsing message from client, closing")