tests/cli_tests/opcua_test_encrypted.bash
branchwxPython4
changeset 3631 176e1f218c61
parent 3549 0af7b6a96c53
child 3634 db3ea47b0d0b
equal deleted inserted replaced
3630:921f620577e8 3631:176e1f218c61
       
     1 #!/bin/bash
       
     2 
       
     3 rm -f ./SRVOK ./PLCOK
       
     4 
       
     5 
       
     6 yes "" | openssl req -x509 -newkey rsa:2048 -keyout my_private_key.pem -out my_cert.pem \
       
     7         -days 355 -nodes -addext "subjectAltName = URI:urn:example.org:FreeOpcUa:python-opcua"
       
     8 openssl x509 -outform der -in my_cert.pem -out my_cert.der
       
     9 
       
    10 # Run server
       
    11 $BEREMIZPYTHONPATH - > >(
       
    12     echo "Start SRV loop"
       
    13     while read line; do 
       
    14         # Wait for server to print modified value
       
    15         echo "SRV>> $line"
       
    16         if [[ "$line" == 3.4 ]]; then
       
    17             echo "PLC could write value"
       
    18             touch ./SRVOK
       
    19         fi
       
    20     done
       
    21     echo "End SRV loop"
       
    22 ) << EOF &
       
    23 
       
    24 import sys
       
    25 import time
       
    26 
       
    27 from opcua import ua, Server
       
    28 
       
    29 server = Server()
       
    30 server.set_endpoint("opc.tcp://127.0.0.1:4840/freeopcua/server/")
       
    31 
       
    32 server.set_security_policy([ua.SecurityPolicyType.Basic256Sha256_SignAndEncrypt])
       
    33 server.load_certificate("my_cert.der")
       
    34 server.load_private_key("my_private_key.pem")
       
    35 
       
    36 uri = "http://beremiz.github.io"
       
    37 idx = server.register_namespace(uri)
       
    38 
       
    39 objects = server.get_objects_node()
       
    40 
       
    41 testobj = objects.add_object(idx, "TestObject")
       
    42 testvarout = testobj.add_variable(idx, "TestOut", 1.2)
       
    43 testvar = testobj.add_variable(idx, "TestIn", 5.6)
       
    44 testvar.set_writable()
       
    45 
       
    46 server.start()
       
    47 
       
    48 try:
       
    49     while True:
       
    50         time.sleep(1)
       
    51         print testvar.get_value()
       
    52         sys.stdout.flush()
       
    53 finally:
       
    54     server.stop()
       
    55 EOF
       
    56 SERVER_PID=$!
       
    57 
       
    58 cp my_cert.der my_private_key.pem \
       
    59    $BEREMIZPATH/tests/projects/opcua_client_encrypted/project_files
       
    60 
       
    61 # Start PLC with opcua test
       
    62 setsid $BEREMIZPYTHONPATH $BEREMIZPATH/Beremiz_cli.py -k \
       
    63      --project-home $BEREMIZPATH/tests/projects/opcua_client_encrypted build transfer run > >(
       
    64 echo "Start PLC loop"
       
    65 while read line; do 
       
    66     # Wait for PLC runtime to output expected value on stdout
       
    67     echo "PLC>> $line"
       
    68     if [[ "$line" == 1.2 ]]; then
       
    69         echo "PLC could read value"
       
    70         touch ./PLCOK
       
    71     fi
       
    72 done
       
    73 echo "End PLC loop"
       
    74 ) &
       
    75 PLC_PID=$!
       
    76 
       
    77 echo all subprocess started, start polling results
       
    78 res=110  # default to ETIMEDOUT
       
    79 c=45
       
    80 while ((c--)); do
       
    81     if [[ -a ./SRVOK && -a ./PLCOK ]]; then
       
    82         echo got results.
       
    83         res=0  # OK success
       
    84         break
       
    85     else
       
    86         echo waiting.... $c
       
    87         sleep 1
       
    88     fi
       
    89 done
       
    90 
       
    91 # Kill PLC and subprocess
       
    92 echo will kill PLC:$PLC_PID and SERVER:$SERVER_PID
       
    93 pkill -s $PLC_PID 
       
    94 kill $SERVER_PID
       
    95 
       
    96 exit $res