author | Edouard Tisserant <edouard.tisserant@gmail.com> |
Tue, 20 Aug 2024 01:26:58 +0200 | |
changeset 3975 | 2ef76b61bf1e |
parent 3820 | 46f3ca3f0157 |
permissions | -rw-r--r-- |
3543
6210c08c1c41
tests: CLI: add simple test running python example
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
1 |
#!/bin/bash |
6210c08c1c41
tests: CLI: add simple test running python example
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
2 |
|
3820
46f3ca3f0157
OPC-UA client : python3 + AsyncUA fixes
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
3718
diff
changeset
|
3 |
set -x -e |
46f3ca3f0157
OPC-UA client : python3 + AsyncUA fixes
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
3718
diff
changeset
|
4 |
|
3676
2f79540660f6
Tests: add encrypted OPCUA browsing test.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
3672
diff
changeset
|
5 |
echo "Instant encrypted OPC-UA server for test" |
2f79540660f6
Tests: add encrypted OPCUA browsing test.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
3672
diff
changeset
|
6 |
|
3820
46f3ca3f0157
OPC-UA client : python3 + AsyncUA fixes
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
3718
diff
changeset
|
7 |
rm -f my_cert.pem my_cert.der my_private_key.pem |
46f3ca3f0157
OPC-UA client : python3 + AsyncUA fixes
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
3718
diff
changeset
|
8 |
|
3676
2f79540660f6
Tests: add encrypted OPCUA browsing test.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
3672
diff
changeset
|
9 |
yes "" | openssl req -x509 -newkey rsa:2048 -keyout my_private_key.pem -out my_cert.pem \ |
2f79540660f6
Tests: add encrypted OPCUA browsing test.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
3672
diff
changeset
|
10 |
-days 355 -nodes -addext "subjectAltName = URI:urn:example.org:FreeOpcUa:python-opcua" |
2f79540660f6
Tests: add encrypted OPCUA browsing test.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
3672
diff
changeset
|
11 |
openssl x509 -outform der -in my_cert.pem -out my_cert.der |
2f79540660f6
Tests: add encrypted OPCUA browsing test.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
3672
diff
changeset
|
12 |
|
2f79540660f6
Tests: add encrypted OPCUA browsing test.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
3672
diff
changeset
|
13 |
PROJECT_FILES_DIR=$BEREMIZPATH/tests/projects/opcua_browse_encrypted/project_files |
3820
46f3ca3f0157
OPC-UA client : python3 + AsyncUA fixes
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
3718
diff
changeset
|
14 |
mkdir $PROJECT_FILES_DIR -p |
3676
2f79540660f6
Tests: add encrypted OPCUA browsing test.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
3672
diff
changeset
|
15 |
cp my_cert.der my_private_key.pem $PROJECT_FILES_DIR |
2f79540660f6
Tests: add encrypted OPCUA browsing test.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
3672
diff
changeset
|
16 |
|
2f79540660f6
Tests: add encrypted OPCUA browsing test.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
3672
diff
changeset
|
17 |
echo "CERTS READY" |
3543
6210c08c1c41
tests: CLI: add simple test running python example
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
18 |
|
3549
0af7b6a96c53
CLI: fix fake_wx for OPC-UA in CLI, match with test and requirements
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
3543
diff
changeset
|
19 |
# Run server |
3672
230c8b90f3dc
Tests: Add OPC-UA server browsing and veriable picking UI test.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
3549
diff
changeset
|
20 |
exec $BEREMIZPYTHONPATH - << EOF |
3543
6210c08c1c41
tests: CLI: add simple test running python example
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
21 |
|
3549
0af7b6a96c53
CLI: fix fake_wx for OPC-UA in CLI, match with test and requirements
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
3543
diff
changeset
|
22 |
import sys |
3718
7841b651d601
Tests: OPCUA tests can now use OPCUA_DEFAULT_HOST environment variable to force host to something else than "127.0.0.1" or "localhost"
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
3676
diff
changeset
|
23 |
import os |
3549
0af7b6a96c53
CLI: fix fake_wx for OPC-UA in CLI, match with test and requirements
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
3543
diff
changeset
|
24 |
import time |
3820
46f3ca3f0157
OPC-UA client : python3 + AsyncUA fixes
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
3718
diff
changeset
|
25 |
import asyncio |
3549
0af7b6a96c53
CLI: fix fake_wx for OPC-UA in CLI, match with test and requirements
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
3543
diff
changeset
|
26 |
|
3820
46f3ca3f0157
OPC-UA client : python3 + AsyncUA fixes
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
3718
diff
changeset
|
27 |
from asyncua import ua, Server |
46f3ca3f0157
OPC-UA client : python3 + AsyncUA fixes
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
3718
diff
changeset
|
28 |
from asyncua.server.users import User, UserRole |
3549
0af7b6a96c53
CLI: fix fake_wx for OPC-UA in CLI, match with test and requirements
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
3543
diff
changeset
|
29 |
|
3820
46f3ca3f0157
OPC-UA client : python3 + AsyncUA fixes
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
3718
diff
changeset
|
30 |
# Asyncua can't work without (over)simple shared cerificates/privkey. |
46f3ca3f0157
OPC-UA client : python3 + AsyncUA fixes
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
3718
diff
changeset
|
31 |
# No user is involved in that case, but asyncua needs it. |
46f3ca3f0157
OPC-UA client : python3 + AsyncUA fixes
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
3718
diff
changeset
|
32 |
# Over permessive User Manager hereafter helps cuting that corner. |
46f3ca3f0157
OPC-UA client : python3 + AsyncUA fixes
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
3718
diff
changeset
|
33 |
class AllAdminUserManager: |
46f3ca3f0157
OPC-UA client : python3 + AsyncUA fixes
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
3718
diff
changeset
|
34 |
def get_user(self, iserver, username=None, password=None, certificate=None): |
46f3ca3f0157
OPC-UA client : python3 + AsyncUA fixes
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
3718
diff
changeset
|
35 |
return User(role=UserRole.Admin) |
3549
0af7b6a96c53
CLI: fix fake_wx for OPC-UA in CLI, match with test and requirements
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
3543
diff
changeset
|
36 |
|
3820
46f3ca3f0157
OPC-UA client : python3 + AsyncUA fixes
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
3718
diff
changeset
|
37 |
async def main(): |
46f3ca3f0157
OPC-UA client : python3 + AsyncUA fixes
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
3718
diff
changeset
|
38 |
server = Server(user_manager=AllAdminUserManager()) |
46f3ca3f0157
OPC-UA client : python3 + AsyncUA fixes
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
3718
diff
changeset
|
39 |
host = os.environ.get("OPCUA_DEFAULT_HOST", "127.0.0.1") |
46f3ca3f0157
OPC-UA client : python3 + AsyncUA fixes
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
3718
diff
changeset
|
40 |
endpoint = "opc.tcp://"+host+":4840/freeopcua/server/" |
46f3ca3f0157
OPC-UA client : python3 + AsyncUA fixes
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
3718
diff
changeset
|
41 |
await server.init() |
46f3ca3f0157
OPC-UA client : python3 + AsyncUA fixes
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
3718
diff
changeset
|
42 |
server.set_endpoint(endpoint) |
3676
2f79540660f6
Tests: add encrypted OPCUA browsing test.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
3672
diff
changeset
|
43 |
|
3820
46f3ca3f0157
OPC-UA client : python3 + AsyncUA fixes
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
3718
diff
changeset
|
44 |
server.set_security_policy([ua.SecurityPolicyType.Basic256Sha256_SignAndEncrypt]) |
46f3ca3f0157
OPC-UA client : python3 + AsyncUA fixes
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
3718
diff
changeset
|
45 |
await server.load_certificate("my_cert.der") |
46f3ca3f0157
OPC-UA client : python3 + AsyncUA fixes
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
3718
diff
changeset
|
46 |
await server.load_private_key("my_private_key.pem") |
3549
0af7b6a96c53
CLI: fix fake_wx for OPC-UA in CLI, match with test and requirements
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
3543
diff
changeset
|
47 |
|
3820
46f3ca3f0157
OPC-UA client : python3 + AsyncUA fixes
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
3718
diff
changeset
|
48 |
uri = "http://beremiz.github.io" |
46f3ca3f0157
OPC-UA client : python3 + AsyncUA fixes
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
3718
diff
changeset
|
49 |
idx = await server.register_namespace(uri) |
3549
0af7b6a96c53
CLI: fix fake_wx for OPC-UA in CLI, match with test and requirements
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
3543
diff
changeset
|
50 |
|
3820
46f3ca3f0157
OPC-UA client : python3 + AsyncUA fixes
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
3718
diff
changeset
|
51 |
objects = server.get_objects_node() |
3549
0af7b6a96c53
CLI: fix fake_wx for OPC-UA in CLI, match with test and requirements
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
3543
diff
changeset
|
52 |
|
3820
46f3ca3f0157
OPC-UA client : python3 + AsyncUA fixes
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
3718
diff
changeset
|
53 |
testobj = await objects.add_object(idx, "TestObject") |
46f3ca3f0157
OPC-UA client : python3 + AsyncUA fixes
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
3718
diff
changeset
|
54 |
testvarout = await testobj.add_variable(idx, "TestOut", 1.2) |
46f3ca3f0157
OPC-UA client : python3 + AsyncUA fixes
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
3718
diff
changeset
|
55 |
testvar = await testobj.add_variable(idx, "TestIn", 5.6) |
46f3ca3f0157
OPC-UA client : python3 + AsyncUA fixes
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
3718
diff
changeset
|
56 |
await testvar.set_writable() |
3549
0af7b6a96c53
CLI: fix fake_wx for OPC-UA in CLI, match with test and requirements
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
3543
diff
changeset
|
57 |
|
3820
46f3ca3f0157
OPC-UA client : python3 + AsyncUA fixes
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
3718
diff
changeset
|
58 |
await server.start() |
46f3ca3f0157
OPC-UA client : python3 + AsyncUA fixes
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
3718
diff
changeset
|
59 |
|
46f3ca3f0157
OPC-UA client : python3 + AsyncUA fixes
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
3718
diff
changeset
|
60 |
try: |
46f3ca3f0157
OPC-UA client : python3 + AsyncUA fixes
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
3718
diff
changeset
|
61 |
while True: |
46f3ca3f0157
OPC-UA client : python3 + AsyncUA fixes
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
3718
diff
changeset
|
62 |
await asyncio.sleep(1) |
46f3ca3f0157
OPC-UA client : python3 + AsyncUA fixes
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
3718
diff
changeset
|
63 |
inval = await testvar.get_value() |
46f3ca3f0157
OPC-UA client : python3 + AsyncUA fixes
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
3718
diff
changeset
|
64 |
print(inval) |
46f3ca3f0157
OPC-UA client : python3 + AsyncUA fixes
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
3718
diff
changeset
|
65 |
await testvarout.set_value(inval*2) |
46f3ca3f0157
OPC-UA client : python3 + AsyncUA fixes
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
3718
diff
changeset
|
66 |
sys.stdout.flush() |
46f3ca3f0157
OPC-UA client : python3 + AsyncUA fixes
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
3718
diff
changeset
|
67 |
finally: |
46f3ca3f0157
OPC-UA client : python3 + AsyncUA fixes
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
3718
diff
changeset
|
68 |
await server.stop() |
46f3ca3f0157
OPC-UA client : python3 + AsyncUA fixes
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
3718
diff
changeset
|
69 |
|
46f3ca3f0157
OPC-UA client : python3 + AsyncUA fixes
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
3718
diff
changeset
|
70 |
asyncio.run(main()) |
46f3ca3f0157
OPC-UA client : python3 + AsyncUA fixes
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
3718
diff
changeset
|
71 |
|
3549
0af7b6a96c53
CLI: fix fake_wx for OPC-UA in CLI, match with test and requirements
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
3543
diff
changeset
|
72 |
EOF |