1
|
|
|
# encoding: utf-8 |
2
|
|
|
# ! /usr/bin/env python |
3
|
|
|
""" |
4
|
|
|
Simple unit test that do not need to setup a server or a client |
5
|
|
|
""" |
6
|
|
|
|
7
|
|
|
import io |
8
|
|
|
import os |
9
|
|
|
import uuid |
10
|
|
|
import pytest |
11
|
|
|
import logging |
12
|
|
|
from datetime import datetime |
13
|
|
|
|
14
|
|
|
from asyncua import ua |
15
|
|
|
from asyncua.ua.ua_binary import extensionobject_from_binary |
16
|
|
|
from asyncua.ua.ua_binary import extensionobject_to_binary |
17
|
|
|
from asyncua.ua.ua_binary import nodeid_to_binary, variant_to_binary, _reshape, variant_from_binary, nodeid_from_binary |
18
|
|
|
from asyncua.ua.ua_binary import struct_to_binary, struct_from_binary |
19
|
|
|
from asyncua.ua import flatten, get_shape |
20
|
|
|
from asyncua.server.internal_subscription import WhereClauseEvaluator |
21
|
|
|
from asyncua.common.event_objects import BaseEvent |
22
|
|
|
from asyncua.common.ua_utils import string_to_variant, variant_to_string, string_to_val, val_to_string |
23
|
|
|
from asyncua.common.xmlimporter import XmlImporter |
24
|
|
|
from asyncua.ua.uatypes import _MaskEnum |
25
|
|
|
from asyncua.common.structures import StructGenerator |
26
|
|
|
from asyncua.common.connection import MessageChunk |
27
|
|
|
|
28
|
|
|
EXAMPLE_BSD_PATH = os.path.abspath(os.path.join(os.path.dirname(__file__), "example.bsd")) |
29
|
|
|
|
30
|
|
|
|
31
|
|
|
def test_variant_array_none(): |
32
|
|
|
v = ua.Variant(None, varianttype=ua.VariantType.Int32, is_array=True) |
33
|
|
|
data = variant_to_binary(v) |
34
|
|
|
v2 = variant_from_binary(ua.utils.Buffer(data)) |
35
|
|
|
assert v == v2 |
36
|
|
|
assert v2.is_array |
37
|
|
|
|
38
|
|
|
v = ua.Variant(None, varianttype=ua.VariantType.Null, is_array=True) |
39
|
|
|
data = variant_to_binary(v) |
40
|
|
|
v2 = variant_from_binary(ua.utils.Buffer(data)) |
41
|
|
|
assert v == v2 |
42
|
|
|
assert v2.is_array |
43
|
|
|
|
44
|
|
|
|
45
|
|
|
def test_variant_empty_list(): |
46
|
|
|
v = ua.Variant([], varianttype=ua.VariantType.Int32, is_array=True) |
47
|
|
|
data = variant_to_binary(v) |
48
|
|
|
v2 = variant_from_binary(ua.utils.Buffer(data)) |
49
|
|
|
assert v == v2 |
50
|
|
|
assert v2.is_array |
51
|
|
|
|
52
|
|
|
|
53
|
|
|
def test_custom_structs(tmpdir): |
54
|
|
|
c = StructGenerator() |
55
|
|
|
c.make_model_from_file(EXAMPLE_BSD_PATH) |
56
|
|
|
output_path = tmpdir.join("test_custom_structs.py").strpath |
57
|
|
|
c.save_to_file(output_path) |
58
|
|
|
ns = {} |
59
|
|
|
with open(output_path) as s: |
60
|
|
|
exec(s.read(), ns) |
61
|
|
|
# test with default values |
62
|
|
|
v = ns["ScalarValueDataType"]() |
63
|
|
|
data = struct_to_binary(v) |
64
|
|
|
v2 = struct_from_binary(ns["ScalarValueDataType"], ua.utils.Buffer(data)) |
65
|
|
|
|
66
|
|
|
# set some values |
67
|
|
|
v = ns["ScalarValueDataType"]() |
68
|
|
|
v.SbyteValue = 1 |
69
|
|
|
v.ByteValue = 2 |
70
|
|
|
v.Int16Value = 3 |
71
|
|
|
v.UInt16Value = 4 |
72
|
|
|
v.Int32Value = 5 |
73
|
|
|
v.UInt32Value = 6 |
74
|
|
|
v.Int64Value = 7 |
75
|
|
|
v.UInt64Value = 8 |
76
|
|
|
v.FloatValue = 9.0 |
77
|
|
|
v.DoubleValue = 10.0 |
78
|
|
|
v.StringValue = "elleven" |
79
|
|
|
v.DateTimeValue = datetime.utcnow() |
80
|
|
|
# self.GuidValue = uuid.uudib"14" |
81
|
|
|
v.ByteStringValue = b"fifteen" |
82
|
|
|
v.XmlElementValue = ua.XmlElement("<toto>titi</toto>") |
83
|
|
|
v.NodeIdValue = ua.NodeId.from_string("ns=4;i=9999") |
84
|
|
|
# self.ExpandedNodeIdValue = |
85
|
|
|
# self.QualifiedNameValue = |
86
|
|
|
# self.LocalizedTextValue = |
87
|
|
|
# self.StatusCodeValue = |
88
|
|
|
# self.VariantValue = |
89
|
|
|
# self.EnumerationValue = |
90
|
|
|
# self.StructureValue = |
91
|
|
|
# self.Number = |
92
|
|
|
# self.Integer = |
93
|
|
|
# self.UInteger = |
94
|
|
|
|
95
|
|
|
data = struct_to_binary(v) |
96
|
|
|
v2 = struct_from_binary(ns["ScalarValueDataType"], ua.utils.Buffer(data)) |
97
|
|
|
assert v.NodeIdValue == v2.NodeIdValue |
98
|
|
|
|
99
|
|
|
|
100
|
|
|
def test_custom_structs_array(tmpdir): |
101
|
|
|
c = StructGenerator() |
102
|
|
|
c.make_model_from_file(EXAMPLE_BSD_PATH) |
103
|
|
|
ns = {} |
104
|
|
|
output_path = tmpdir.join("test_custom_structs_array.py").strpath |
105
|
|
|
c.save_to_file(output_path) |
106
|
|
|
with open(output_path) as s: |
107
|
|
|
exec(s.read(), ns) |
108
|
|
|
|
109
|
|
|
# test with default values |
110
|
|
|
v = ns["ArrayValueDataType"]() |
111
|
|
|
data = struct_to_binary(v) |
112
|
|
|
v2 = struct_from_binary(ns["ArrayValueDataType"], ua.utils.Buffer(data)) |
113
|
|
|
|
114
|
|
|
# set some values |
115
|
|
|
v = ns["ArrayValueDataType"]() |
116
|
|
|
v.SbyteValue = [1] |
117
|
|
|
v.ByteValue = [2] |
118
|
|
|
v.Int16Value = [3] |
119
|
|
|
v.UInt16Value = [4] |
120
|
|
|
v.Int32Value = [5] |
121
|
|
|
v.UInt32Value = [6] |
122
|
|
|
v.Int64Value = [7] |
123
|
|
|
v.UInt64Value = [8] |
124
|
|
|
v.FloatValue = [9.0] |
125
|
|
|
v.DoubleValue = [10.0] |
126
|
|
|
v.StringValue = ["elleven"] |
127
|
|
|
v.DateTimeValue = [datetime.utcnow()] |
128
|
|
|
# self.GuidValue = uuid.uudib"14" |
129
|
|
|
v.ByteStringValue = [b"fifteen", b"sixteen"] |
130
|
|
|
v.XmlElementValue = [ua.XmlElement("<toto>titi</toto>")] |
131
|
|
|
v.NodeIdValue = [ua.NodeId.from_string("ns=4;i=9999"), ua.NodeId.from_string("i=6")] |
132
|
|
|
data = struct_to_binary(v) |
133
|
|
|
v2 = struct_from_binary(ns["ArrayValueDataType"], ua.utils.Buffer(data)) |
134
|
|
|
assert v.NodeIdValue == v2.NodeIdValue |
135
|
|
|
# print(v2.NodeIdValue) |
136
|
|
|
|
137
|
|
|
|
138
|
|
|
def test_nodeid_nsu(): |
139
|
|
|
n = ua.NodeId(100, 2) |
140
|
|
|
n.NamespaceUri = "http://freeopcua/tests" |
141
|
|
|
n.ServerIndex = 4 |
142
|
|
|
data = nodeid_to_binary(n) |
143
|
|
|
n2 = nodeid_from_binary(ua.utils.Buffer(data)) |
144
|
|
|
assert n == n2 |
145
|
|
|
n3 = ua.NodeId.from_string(n.to_string()) |
146
|
|
|
assert n == n3 |
147
|
|
|
|
148
|
|
|
|
149
|
|
|
def test_nodeid_ordering(): |
150
|
|
|
a = ua.NodeId(2000, 1) |
151
|
|
|
b = ua.NodeId(3000, 1) |
152
|
|
|
c = ua.NodeId(20, 0) |
153
|
|
|
d = ua.NodeId("tititu", 1) |
154
|
|
|
e = ua.NodeId("aaaaa", 1) |
155
|
|
|
f = ua.NodeId("aaaaa", 2) |
156
|
|
|
g = ua.NodeId(uuid.uuid4(), 1) |
157
|
|
|
h = ua.TwoByteNodeId(2001) |
158
|
|
|
i = ua.NodeId(b"lkjkl", 1, ua.NodeIdType.ByteString) |
159
|
|
|
j = ua.NodeId(b"aaa", 5, ua.NodeIdType.ByteString) |
160
|
|
|
|
161
|
|
|
mylist = [a, b, c, d, e, f, g, h, i, j] |
162
|
|
|
mylist.sort() |
163
|
|
|
expected = [h, c, a, b, e, d, f, g, i, j] |
164
|
|
|
assert mylist == expected |
165
|
|
|
|
166
|
|
|
|
167
|
|
|
def test_string_to_variant_int(): |
168
|
|
|
s_arr_uint = "[1, 2, 3, 4]" |
169
|
|
|
arr_uint = [1, 2, 3, 4] |
170
|
|
|
assert arr_uint == string_to_val(s_arr_uint, ua.VariantType.UInt32) |
171
|
|
|
assert arr_uint == string_to_val(s_arr_uint, ua.VariantType.UInt16) |
172
|
|
|
assert s_arr_uint == val_to_string(arr_uint) |
173
|
|
|
|
174
|
|
|
|
175
|
|
|
def test_string_to_variant_float(): |
176
|
|
|
s_arr_float = "[1.1, 2.1, 3, 4.0]" |
177
|
|
|
arr_float = [1.1, 2.1, 3, 4.0] |
178
|
|
|
s_float = "1.9" |
179
|
|
|
assert 1.9 == string_to_val(s_float, ua.VariantType.Float) |
180
|
|
|
assert s_arr_float == val_to_string(arr_float) |
181
|
|
|
|
182
|
|
|
|
183
|
|
|
def test_string_to_variant_datetime_string(): |
184
|
|
|
s_arr_datetime = "[2014-05-6, 2016-10-3]" |
185
|
|
|
arr_string = ['2014-05-6', '2016-10-3'] |
186
|
|
|
arr_datetime = [datetime(2014, 5, 6), datetime(2016, 10, 3)] |
187
|
|
|
assert s_arr_datetime == val_to_string(arr_string) |
188
|
|
|
assert arr_string == string_to_val(s_arr_datetime, ua.VariantType.String) |
189
|
|
|
assert arr_datetime == string_to_val(s_arr_datetime, ua.VariantType.DateTime) |
190
|
|
|
|
191
|
|
|
|
192
|
|
|
def test_string_to_variant_nodeid(): |
193
|
|
|
s_arr_nodeid = "[ns=2;i=56, i=45]" |
194
|
|
|
arr_nodeid = [ua.NodeId.from_string("ns=2;i=56"), ua.NodeId.from_string("i=45")] |
195
|
|
|
assert arr_nodeid == string_to_val(s_arr_nodeid, ua.VariantType.NodeId) |
196
|
|
|
|
197
|
|
|
|
198
|
|
|
def test_string_to_variant_status_code(): |
199
|
|
|
s_statuscode = "Good" |
200
|
|
|
statuscode = ua.StatusCode(ua.StatusCodes.Good) |
201
|
|
|
s_statuscode2 = "Uncertain" |
202
|
|
|
statuscode2 = ua.StatusCode(ua.StatusCodes.Uncertain) |
203
|
|
|
assert statuscode == string_to_val(s_statuscode, ua.VariantType.StatusCode) |
204
|
|
|
assert statuscode2 == string_to_val(s_statuscode2, ua.VariantType.StatusCode) |
205
|
|
|
|
206
|
|
|
|
207
|
|
|
def test_string_to_variant_qname(): |
208
|
|
|
string = "2:name" |
209
|
|
|
obj = ua.QualifiedName("name", 2) |
210
|
|
|
assert obj == string_to_val(string, ua.VariantType.QualifiedName) |
211
|
|
|
assert string == val_to_string(obj) |
212
|
|
|
|
213
|
|
|
|
214
|
|
|
def test_string_to_variant_localized_text(): |
215
|
|
|
string = "_This is my nøåæ" |
216
|
|
|
obj = ua.LocalizedText(string) |
217
|
|
|
assert obj == string_to_val(string, ua.VariantType.LocalizedText) |
218
|
|
|
assert string == val_to_string(obj) |
219
|
|
|
|
220
|
|
|
|
221
|
|
|
def test_string_to_val_xml_element(): |
222
|
|
|
string = "<p> titi toto </p>" |
223
|
|
|
obj = ua.XmlElement(string) |
224
|
|
|
assert obj == string_to_val(string, ua.VariantType.XmlElement) |
225
|
|
|
assert string == val_to_string(obj) |
226
|
|
|
b = struct_to_binary(obj) |
227
|
|
|
obj2 = struct_from_binary(ua.XmlElement, ua.utils.Buffer(b)) |
228
|
|
|
assert obj == obj2 |
229
|
|
|
|
230
|
|
|
|
231
|
|
|
def test_variant_dimensions(): |
232
|
|
|
l = [[[1.0, 1.0, 1.0, 1.0], [2.0, 2.0, 2.0, 2.0], [3.0, 3.0, 3.0, 3.0]], |
233
|
|
|
[[5.0, 5.0, 5.0, 5.0], [7.0, 8.0, 9.0, 01.0], [1.0, 1.0, 1.0, 1.0]]] |
234
|
|
|
v = ua.Variant(l) |
235
|
|
|
assert [2, 3, 4] == v.Dimensions |
236
|
|
|
v2 = variant_from_binary(ua.utils.Buffer(variant_to_binary(v))) |
237
|
|
|
assert v == v2 |
238
|
|
|
assert v.Dimensions == v2.Dimensions |
239
|
|
|
|
240
|
|
|
# very special case |
241
|
|
|
l = [[[], [], []], [[], [], []]] |
242
|
|
|
v = ua.Variant(l, ua.VariantType.UInt32) |
243
|
|
|
assert [2, 3, 0] == v.Dimensions |
244
|
|
|
v2 = variant_from_binary(ua.utils.Buffer(variant_to_binary(v))) |
245
|
|
|
assert v.Dimensions == v2.Dimensions |
246
|
|
|
assert v == v2 |
247
|
|
|
|
248
|
|
|
|
249
|
|
|
def test_flatten(): |
250
|
|
|
l = [[[1.0, 1.0, 1.0, 1.0], [1.0, 1.0, 1.0, 1.0], [1.0, 1.0, 1.0, 1.0]], |
251
|
|
|
[[1.0, 1.0, 1.0, 1.0], [1.0, 1.0, 1.0, 1.0], [1.0, 1.0, 1.0, 1.0]]] |
252
|
|
|
l2 = flatten(l) |
253
|
|
|
dims = get_shape(l) |
254
|
|
|
assert [2, 3, 4] == dims |
255
|
|
|
assert l != l2 |
256
|
|
|
|
257
|
|
|
l3 = _reshape(l2, (2, 3, 4)) |
258
|
|
|
assert l == l3 |
259
|
|
|
|
260
|
|
|
l = [[[], [], []], [[], [], []]] |
261
|
|
|
l2 = flatten(l) |
262
|
|
|
dims = get_shape(l) |
263
|
|
|
assert dims == [2, 3, 0] |
264
|
|
|
|
265
|
|
|
l = [1, 2, 3, 4] |
266
|
|
|
l2 = flatten(l) |
267
|
|
|
dims = get_shape(l) |
268
|
|
|
assert dims == [4] |
269
|
|
|
assert l == l2 |
270
|
|
|
|
271
|
|
|
|
272
|
|
|
def test_custom_variant(): |
273
|
|
|
with pytest.raises(ua.UaError): |
274
|
|
|
v = ua.Variant(b"ljsdfljds", ua.VariantTypeCustom(89)) |
275
|
|
|
v = ua.Variant(b"ljsdfljds", ua.VariantTypeCustom(61)) |
276
|
|
|
v2 = variant_from_binary(ua.utils.Buffer(variant_to_binary(v))) |
277
|
|
|
assert v.VariantType == v2.VariantType |
278
|
|
|
assert v == v2 |
279
|
|
|
|
280
|
|
|
|
281
|
|
|
def test_custom_variant_array(): |
282
|
|
|
v = ua.Variant([b"ljsdfljds", b"lkjsdljksdf"], ua.VariantTypeCustom(40)) |
283
|
|
|
v2 = variant_from_binary(ua.utils.Buffer(variant_to_binary(v))) |
284
|
|
|
assert v.VariantType == v2.VariantType |
285
|
|
|
assert v == v2 |
286
|
|
|
|
287
|
|
|
|
288
|
|
|
def test_guid(): |
289
|
|
|
v = ua.Variant(uuid.uuid4(), ua.VariantType.Guid) |
290
|
|
|
v2 = variant_from_binary(ua.utils.Buffer(variant_to_binary(v))) |
291
|
|
|
assert v.VariantType == v2.VariantType |
292
|
|
|
assert v == v2 |
293
|
|
|
|
294
|
|
|
|
295
|
|
|
def test_nodeid(): |
296
|
|
|
nid = ua.NodeId() |
297
|
|
|
assert nid.NodeIdType == ua.NodeIdType.TwoByte |
298
|
|
|
nid = ua.NodeId(446, 3, ua.NodeIdType.FourByte) |
299
|
|
|
assert nid.NodeIdType == ua.NodeIdType.FourByte |
300
|
|
|
d = nodeid_to_binary(nid) |
301
|
|
|
new_nid = nodeid_from_binary(io.BytesIO(d)) |
302
|
|
|
assert new_nid == nid |
303
|
|
|
assert new_nid.NodeIdType == ua.NodeIdType.FourByte |
304
|
|
|
assert new_nid.Identifier == 446 |
305
|
|
|
assert new_nid.NamespaceIndex == 3 |
306
|
|
|
|
307
|
|
|
tb = ua.TwoByteNodeId(53) |
308
|
|
|
fb = ua.FourByteNodeId(53) |
309
|
|
|
n = ua.NumericNodeId(53) |
310
|
|
|
n1 = ua.NumericNodeId(53, 0) |
311
|
|
|
s1 = ua.StringNodeId("53", 0) |
312
|
|
|
bs = ua.ByteStringNodeId(b"53", 0) |
313
|
|
|
gid = uuid.uuid4() |
314
|
|
|
g = ua.ByteStringNodeId(str(gid), 0) |
315
|
|
|
guid = ua.GuidNodeId(gid) |
316
|
|
|
assert tb == fb |
317
|
|
|
assert tb == n |
318
|
|
|
assert tb == n1 |
319
|
|
|
assert n1 == fb |
320
|
|
|
assert g != guid |
321
|
|
|
assert tb == nodeid_from_binary(ua.utils.Buffer(nodeid_to_binary(tb))) |
322
|
|
|
assert fb == nodeid_from_binary(ua.utils.Buffer(nodeid_to_binary(fb))) |
323
|
|
|
assert n == nodeid_from_binary(ua.utils.Buffer(nodeid_to_binary(n))) |
324
|
|
|
assert s1 == nodeid_from_binary(ua.utils.Buffer(nodeid_to_binary(s1))) |
325
|
|
|
assert bs == nodeid_from_binary(ua.utils.Buffer(nodeid_to_binary(bs))) |
326
|
|
|
assert guid == nodeid_from_binary(ua.utils.Buffer(nodeid_to_binary(guid))) |
327
|
|
|
|
328
|
|
|
|
329
|
|
|
def test_nodeid_string(): |
330
|
|
|
nid0 = ua.NodeId(45) |
331
|
|
|
assert nid0 == ua.NodeId.from_string("i=45") |
332
|
|
|
assert nid0 == ua.NodeId.from_string("ns=0;i=45") |
333
|
|
|
nid = ua.NodeId(45, 10) |
334
|
|
|
assert nid == ua.NodeId.from_string("i=45; ns=10") |
335
|
|
|
assert nid != ua.NodeId.from_string("i=45; ns=11") |
336
|
|
|
assert nid != ua.NodeId.from_string("i=5; ns=10") |
337
|
|
|
# not sure the next one is correct... |
338
|
|
|
assert nid == ua.NodeId.from_string("i=45; ns=10; srv=serverid") |
339
|
|
|
nid1 = ua.NodeId("myid.mynodeid", 7) |
340
|
|
|
assert nid1 == ua.NodeId.from_string("ns=7; s=myid.mynodeid") |
341
|
|
|
# with pytest.raises(ua.UaError): |
342
|
|
|
# nid1 = ua.NodeId(7, "myid.mynodeid") |
343
|
|
|
# with pytest.raises(ua.UaError): |
344
|
|
|
# nid1 = ua.StringNodeId(1, 2) |
345
|
|
|
|
346
|
|
|
|
347
|
|
|
def test_bad_string(): |
348
|
|
|
with pytest.raises(ua.UaStringParsingError): |
349
|
|
|
ua.NodeId.from_string("ns=r;s=yu") |
350
|
|
|
with pytest.raises(ua.UaStringParsingError): |
351
|
|
|
ua.NodeId.from_string("i=r;ns=1") |
352
|
|
|
with pytest.raises(ua.UaStringParsingError): |
353
|
|
|
ua.NodeId.from_string("ns=1") |
354
|
|
|
with pytest.raises(ua.UaError): |
355
|
|
|
ua.QualifiedName.from_string("i:yu") |
356
|
|
|
with pytest.raises(ua.UaError): |
357
|
|
|
ua.QualifiedName.from_string("i:::yu") |
358
|
|
|
|
359
|
|
|
|
360
|
|
|
def test_expandednodeid(): |
361
|
|
|
nid = ua.ExpandedNodeId() |
362
|
|
|
assert nid.NodeIdType == ua.NodeIdType.TwoByte |
363
|
|
|
nid2 = nodeid_from_binary(ua.utils.Buffer(nodeid_to_binary(nid))) |
364
|
|
|
assert nid == nid2 |
365
|
|
|
|
366
|
|
|
|
367
|
|
|
def test_null_string(): |
368
|
|
|
v = ua.Variant(None, ua.VariantType.String) |
369
|
|
|
b = variant_to_binary(v) |
370
|
|
|
v2 = variant_from_binary(ua.utils.Buffer(b)) |
371
|
|
|
assert v.Value == v2.Value |
372
|
|
|
v = ua.Variant("", ua.VariantType.String) |
373
|
|
|
b = variant_to_binary(v) |
374
|
|
|
v2 = variant_from_binary(ua.utils.Buffer(b)) |
375
|
|
|
assert v.Value == v2.Value |
376
|
|
|
|
377
|
|
|
|
378
|
|
|
def test_extension_object(): |
379
|
|
|
obj = ua.UserNameIdentityToken() |
380
|
|
|
obj.UserName = "admin" |
381
|
|
|
obj.Password = b"pass" |
382
|
|
|
obj2 = extensionobject_from_binary(ua.utils.Buffer(extensionobject_to_binary(obj))) |
383
|
|
|
assert type(obj) == type(obj2) |
384
|
|
|
assert obj.UserName == obj2.UserName |
385
|
|
|
assert obj.Password == obj2.Password |
386
|
|
|
v1 = ua.Variant(obj) |
387
|
|
|
v2 = variant_from_binary(ua.utils.Buffer(variant_to_binary(v1))) |
388
|
|
|
assert type(v1) == type(v2) |
389
|
|
|
assert v1.VariantType == v2.VariantType |
390
|
|
|
|
391
|
|
|
|
392
|
|
|
def test_unknown_extension_object(): |
393
|
|
|
obj = ua.ExtensionObject() |
394
|
|
|
obj.Body = b'example of data in custom format' |
395
|
|
|
obj.TypeId = ua.NodeId.from_string('ns=3;i=42') |
396
|
|
|
data = ua.utils.Buffer(extensionobject_to_binary(obj)) |
397
|
|
|
obj2 = extensionobject_from_binary(data) |
398
|
|
|
assert type(obj2) == ua.ExtensionObject |
399
|
|
|
assert obj2.TypeId == obj.TypeId |
400
|
|
|
assert obj2.Body == b'example of data in custom format' |
401
|
|
|
|
402
|
|
|
|
403
|
|
|
def test_datetime(): |
404
|
|
|
now = datetime.utcnow() |
405
|
|
|
epch = ua.datetime_to_win_epoch(now) |
406
|
|
|
dt = ua.win_epoch_to_datetime(epch) |
407
|
|
|
assert now == dt |
408
|
|
|
# python's datetime has a range from Jan 1, 0001 to the end of year 9999 |
409
|
|
|
# windows' filetime has a range from Jan 1, 1601 to approx. year 30828 |
410
|
|
|
# let's test an overlapping range [Jan 1, 1601 - Dec 31, 9999] |
411
|
|
|
dt = datetime(1601, 1, 1) |
412
|
|
|
assert ua.win_epoch_to_datetime(ua.datetime_to_win_epoch(dt)) == dt |
413
|
|
|
dt = datetime(9999, 12, 31, 23, 59, 59) |
414
|
|
|
assert ua.win_epoch_to_datetime(ua.datetime_to_win_epoch(dt)) == dt |
415
|
|
|
epch = 128930364000001000 |
416
|
|
|
dt = ua.win_epoch_to_datetime(epch) |
417
|
|
|
epch2 = ua.datetime_to_win_epoch(dt) |
418
|
|
|
assert epch == epch2 |
419
|
|
|
epch = 0 |
420
|
|
|
assert ua.datetime_to_win_epoch(ua.win_epoch_to_datetime(epch)) == epch |
421
|
|
|
|
422
|
|
|
|
423
|
|
|
def test_equal_nodeid(): |
424
|
|
|
nid1 = ua.NodeId(999, 2) |
425
|
|
|
nid2 = ua.NodeId(999, 2) |
426
|
|
|
assert nid1 == nid2 |
427
|
|
|
assert id(nid1) != id(nid2) |
428
|
|
|
|
429
|
|
|
|
430
|
|
|
def test_zero_nodeid(): |
431
|
|
|
assert ua.NodeId() == ua.NodeId(0, 0) |
432
|
|
|
assert ua.NodeId() == ua.NodeId.from_string('ns=0;i=0;') |
433
|
|
|
|
434
|
|
|
|
435
|
|
|
def test_string_nodeid(): |
436
|
|
|
nid = ua.NodeId('titi', 1) |
437
|
|
|
assert nid.NamespaceIndex == 1 |
438
|
|
|
assert nid.Identifier == 'titi' |
439
|
|
|
assert nid.NodeIdType == ua.NodeIdType.String |
440
|
|
|
|
441
|
|
|
|
442
|
|
|
def test_unicode_string_nodeid(): |
443
|
|
|
nid = ua.NodeId('hëllò', 1) |
444
|
|
|
assert nid.NamespaceIndex == 1 |
445
|
|
|
assert nid.Identifier == 'hëllò' |
446
|
|
|
assert nid.NodeIdType == ua.NodeIdType.String |
447
|
|
|
d = nodeid_to_binary(nid) |
448
|
|
|
new_nid = nodeid_from_binary(io.BytesIO(d)) |
449
|
|
|
assert new_nid == nid |
450
|
|
|
assert new_nid.Identifier == 'hëllò' |
451
|
|
|
assert new_nid.NodeIdType == ua.NodeIdType.String |
452
|
|
|
|
453
|
|
|
|
454
|
|
|
def test_numeric_nodeid(): |
455
|
|
|
nid = ua.NodeId(999, 2) |
456
|
|
|
assert nid.NamespaceIndex == 2 |
457
|
|
|
assert nid.Identifier == 999 |
458
|
|
|
assert nid.NodeIdType == ua.NodeIdType.Numeric |
459
|
|
|
|
460
|
|
|
|
461
|
|
|
def test_qualifiedstring_nodeid(): |
462
|
|
|
nid = ua.NodeId.from_string('ns=2;s=PLC1.Manufacturer;') |
463
|
|
|
assert nid.NamespaceIndex == 2 |
464
|
|
|
assert nid.Identifier == 'PLC1.Manufacturer' |
465
|
|
|
|
466
|
|
|
|
467
|
|
|
def test_strrepr_nodeid(): |
468
|
|
|
nid = ua.NodeId.from_string('ns=2;s=PLC1.Manufacturer;') |
469
|
|
|
assert nid.to_string() == 'ns=2;s=PLC1.Manufacturer' |
470
|
|
|
# assert repr(nid) == 'ns=2;s=PLC1.Manufacturer;' |
471
|
|
|
|
472
|
|
|
|
473
|
|
|
def test_qualified_name(): |
474
|
|
|
qn = ua.QualifiedName('qname', 2) |
475
|
|
|
assert qn.NamespaceIndex == 2 |
476
|
|
|
assert qn.Name == 'qname' |
477
|
|
|
assert qn.to_string() == '2:qname' |
478
|
|
|
|
479
|
|
|
|
480
|
|
|
def test_datavalue(): |
481
|
|
|
dv = ua.DataValue(123) |
482
|
|
|
assert dv.Value == ua.Variant(123) |
483
|
|
|
assert type(dv.Value) == ua.Variant |
484
|
|
|
dv = ua.DataValue('abc') |
485
|
|
|
assert dv.Value == ua.Variant('abc') |
486
|
|
|
now = datetime.utcnow() |
487
|
|
|
dv.SourceTimestamp = now |
488
|
|
|
|
489
|
|
|
|
490
|
|
|
def test_variant(): |
491
|
|
|
dv = ua.Variant(True, ua.VariantType.Boolean) |
492
|
|
|
assert dv.Value == True |
493
|
|
|
assert type(dv.Value) == bool |
494
|
|
|
now = datetime.utcnow() |
495
|
|
|
v = ua.Variant(now) |
496
|
|
|
assert v.Value == now |
497
|
|
|
assert v.VariantType == ua.VariantType.DateTime |
498
|
|
|
v2 = variant_from_binary(ua.utils.Buffer(variant_to_binary(v))) |
499
|
|
|
assert v.Value == v2.Value |
500
|
|
|
assert v.VariantType == v2.VariantType |
501
|
|
|
# commonity method: |
502
|
|
|
assert v == ua.Variant(v) |
503
|
|
|
|
504
|
|
|
|
505
|
|
|
def test_variant_array(): |
506
|
|
|
v = ua.Variant([1, 2, 3, 4, 5]) |
507
|
|
|
assert v.Value[1] == 2 |
508
|
|
|
# assert v.VarianType, ua.VariantType.Int64) # we do not care, we should aonly test for sutff that matter |
509
|
|
|
v2 = variant_from_binary(ua.utils.Buffer(variant_to_binary(v))) |
510
|
|
|
assert v.Value == v2.Value |
511
|
|
|
assert v.VariantType == v2.VariantType |
512
|
|
|
|
513
|
|
|
now = datetime.utcnow() |
514
|
|
|
v = ua.Variant([now]) |
515
|
|
|
assert v.Value[0] == now |
516
|
|
|
assert v.VariantType == ua.VariantType.DateTime |
517
|
|
|
v2 = variant_from_binary(ua.utils.Buffer(variant_to_binary(v))) |
518
|
|
|
assert v.Value == v2.Value |
519
|
|
|
assert v.VariantType == v2.VariantType |
520
|
|
|
|
521
|
|
|
|
522
|
|
|
def test_variant_array_dim(): |
523
|
|
|
v = ua.Variant([1, 2, 3, 4, 5, 6], dimensions=[2, 3]) |
524
|
|
|
assert v.Value[1] == 2 |
525
|
|
|
v2 = variant_from_binary(ua.utils.Buffer(variant_to_binary(v))) |
526
|
|
|
assert _reshape(v.Value, (2, 3)) == v2.Value |
527
|
|
|
assert v.VariantType == v2.VariantType |
528
|
|
|
assert v.Dimensions == v2.Dimensions |
529
|
|
|
assert v2.Dimensions == [2, 3] |
530
|
|
|
|
531
|
|
|
|
532
|
|
|
def test_text(): |
533
|
|
|
t1 = ua.LocalizedText('Root') |
534
|
|
|
t2 = ua.LocalizedText('Root') |
535
|
|
|
t3 = ua.LocalizedText('root') |
536
|
|
|
assert t1 == t2 |
537
|
|
|
assert t1 != t3 |
538
|
|
|
t4 = struct_from_binary(ua.LocalizedText, ua.utils.Buffer(struct_to_binary(t1))) |
539
|
|
|
assert t1 == t4 |
540
|
|
|
|
541
|
|
|
|
542
|
|
|
def test_message_chunk(): |
543
|
|
|
pol = ua.SecurityPolicy() |
544
|
|
|
chunks = MessageChunk.message_to_chunks(pol, b'123', 65536) |
545
|
|
|
assert len(chunks) == 1 |
546
|
|
|
seq = 0 |
547
|
|
|
for chunk in chunks: |
548
|
|
|
seq += 1 |
549
|
|
|
chunk.SequenceHeader.SequenceNumber = seq |
550
|
|
|
chunk2 = MessageChunk.from_binary(pol, ua.utils.Buffer(chunks[0].to_binary())) |
551
|
|
|
assert chunks[0].to_binary() == chunk2.to_binary() |
552
|
|
|
|
553
|
|
|
# for policy None, MessageChunk overhead is 12+4+8 = 24 bytes |
554
|
|
|
# Let's pack 11 bytes into 28-byte chunks. The message must be split as 4+4+3 |
555
|
|
|
chunks = MessageChunk.message_to_chunks(pol, b'12345678901', 28) |
556
|
|
|
assert len(chunks) == 3 |
557
|
|
|
assert chunks[0].Body == b'1234' |
558
|
|
|
assert chunks[1].Body == b'5678' |
559
|
|
|
assert chunks[2].Body == b'901' |
560
|
|
|
for chunk in chunks: |
561
|
|
|
seq += 1 |
562
|
|
|
chunk.SequenceHeader.SequenceNumber = seq |
563
|
|
|
assert len(chunk.to_binary()) <= 28 |
564
|
|
|
|
565
|
|
|
|
566
|
|
|
def test_null(): |
567
|
|
|
n = ua.NodeId(b'000000', 0, nodeidtype=ua.NodeIdType.Guid) |
568
|
|
|
assert n.is_null() |
569
|
|
|
assert n.has_null_identifier() |
570
|
|
|
|
571
|
|
|
n = ua.NodeId(b'000000', 1, nodeidtype=ua.NodeIdType.Guid) |
572
|
|
|
assert n.is_null() is False |
573
|
|
|
assert n.has_null_identifier() |
574
|
|
|
|
575
|
|
|
n = ua.NodeId() |
576
|
|
|
assert n.is_null() |
577
|
|
|
assert n.has_null_identifier() |
578
|
|
|
|
579
|
|
|
n = ua.NodeId(0, 0) |
580
|
|
|
assert n.is_null() |
581
|
|
|
assert n.has_null_identifier() |
582
|
|
|
|
583
|
|
|
n = ua.NodeId("", 0) |
584
|
|
|
assert n.is_null() |
585
|
|
|
assert n.has_null_identifier() |
586
|
|
|
|
587
|
|
|
n = ua.TwoByteNodeId(0) |
588
|
|
|
assert n.is_null() |
589
|
|
|
assert n.has_null_identifier() |
590
|
|
|
|
591
|
|
|
n = ua.NodeId(0, 3) |
592
|
|
|
assert n.is_null() is False |
593
|
|
|
assert n.has_null_identifier() |
594
|
|
|
|
595
|
|
|
|
596
|
|
|
def test_where_clause(): |
597
|
|
|
cf = ua.ContentFilter() |
598
|
|
|
el = ua.ContentFilterElement() |
599
|
|
|
op = ua.SimpleAttributeOperand() |
600
|
|
|
op.BrowsePath.append(ua.QualifiedName("property", 2)) |
601
|
|
|
el.FilterOperands.append(op) |
602
|
|
|
for i in range(10): |
603
|
|
|
op = ua.LiteralOperand() |
604
|
|
|
op.Value = ua.Variant(i) |
605
|
|
|
el.FilterOperands.append(op) |
606
|
|
|
el.FilterOperator = ua.FilterOperator.InList |
607
|
|
|
cf.Elements.append(el) |
608
|
|
|
wce = WhereClauseEvaluator(logging.getLogger(__name__), None, cf) |
609
|
|
|
ev = BaseEvent() |
610
|
|
|
ev._freeze = False |
611
|
|
|
ev.property = 3 |
612
|
|
|
assert wce.eval(ev) |
613
|
|
|
|
614
|
|
|
|
615
|
|
|
class MyEnum(_MaskEnum): |
616
|
|
|
member1 = 0 |
617
|
|
|
member2 = 1 |
618
|
|
|
|
619
|
|
|
|
620
|
|
|
def test_invalid_input(): |
621
|
|
|
with pytest.raises(ValueError): |
622
|
|
|
MyEnum(12345) |
623
|
|
|
|
624
|
|
|
|
625
|
|
|
def test_parsing(): |
626
|
|
|
assert MyEnum.parse_bitfield(0b0) == set() |
627
|
|
|
assert MyEnum.parse_bitfield(0b1) == {MyEnum.member1} |
628
|
|
|
assert MyEnum.parse_bitfield(0b10) == {MyEnum.member2} |
629
|
|
|
assert MyEnum.parse_bitfield(0b11) == {MyEnum.member1, MyEnum.member2} |
630
|
|
|
|
631
|
|
|
|
632
|
|
|
def test_identity(): |
633
|
|
|
bitfields = [0b00, 0b01, 0b10, 0b11] |
634
|
|
|
|
635
|
|
|
for bitfield in bitfields: |
636
|
|
|
as_set = MyEnum.parse_bitfield(bitfield) |
637
|
|
|
back_to_bitfield = MyEnum.to_bitfield(as_set) |
638
|
|
|
assert back_to_bitfield == bitfield |
639
|
|
|
|
640
|
|
|
|
641
|
|
|
def test_variant_intenum(): |
642
|
|
|
ase = ua.AxisScaleEnumeration(ua.AxisScaleEnumeration.Linear) # Just pick an existing IntEnum class |
643
|
|
|
vAse = ua.Variant(ase) |
644
|
|
|
assert vAse.VariantType == ua.VariantType.Int32 |
645
|
|
|
|