| ///////////////////////////////////////////////////////////////////////////////////// | |
| // Copyright (c) 2017-2018 Ericsson AB | |
| // All rights reserved. This program and the accompanying materials | |
| // are made available under the terms of the Eclipse Public License v1.0 | |
| // which accompanies this distribution, and is available at | |
| // http://www.eclipse.org/legal/epl-v10.html | |
| ///////////////////////////////////////////////////////////////////////////////////// | |
| // Contributors: | |
| // Akos Makovics | |
| // | |
| // File: UpcUa_Test.ttcn | |
| // Rev: <RnXnn> | |
| // Prodnr: CNL113861 | |
| ///////////////////////////////////////////////////////////////////////////////////// | |
| module OpcUa_Test { | |
| import from IPL4asp_PortType all; | |
| import from OpcUa_Functions all; | |
| import from OpcUa_Types_Binary all; | |
| import from OpcUa_Encoded_Messages all; | |
| import from OpcUa_Message_Templates all; | |
| type component MyComp {} | |
| type component SendReceiveComp { | |
| port IPL4asp_PT testPort; | |
| var integer connId; | |
| } | |
| // local and remote TCP host/port | |
| modulepar { OpcUa_Functions.ConnectionData LocalTCPConnection, RemoteTCPConnection } | |
| function f_encDec_Binary_Message (in MessageChunk pl_in) runs on MyComp { | |
| log("=========================================================================="); | |
| log("f_encDec_Binary_Message Enter"); | |
| var octetstring v_encodedMessage; | |
| var octetstring v_secondEncodedMessage; | |
| var MessageChunk v_decodedMessage; | |
| var integer v_messageType := pl_in.header.messageType; | |
| if (v_messageType == 4998472 or v_messageType == 4932417 or v_messageType == 5395013 or v_messageType == 4540498 ) { | |
| log("Message type: Transport Message"); | |
| } else { | |
| log("Message type: Service, message contains body"); | |
| var ServiceBody body; | |
| if (dec_ServiceBody(pl_in.body, body) == -1) { | |
| setverdict(fail, "Failed to decode service body!"); | |
| } | |
| var octetstring encodedBody := enc_ServiceBody(body); | |
| if (encodedBody != pl_in.body) { | |
| setverdict(fail, "Service body encoding has failed!"); | |
| } | |
| } | |
| v_encodedMessage := enc_MessageChunk(pl_in); | |
| if (dec_MessageChunk(v_encodedMessage, v_decodedMessage) == -1) { | |
| setverdict ( fail, "Failed to encode message!" ); | |
| return; | |
| } | |
| v_secondEncodedMessage := enc_MessageChunk(v_decodedMessage); | |
| if (v_encodedMessage == v_secondEncodedMessage) { | |
| setverdict ( pass, "Message succesfully encoded and decoded!" ); | |
| } else { | |
| setverdict ( fail, "First and second encoding yielded a different result!" ); | |
| } | |
| } | |
| function f_encDec_OpcUa_MessageChunk(in octetstring pl_in) runs on MyComp | |
| { | |
| log("=========================================================================="); | |
| log("f_encDec_OpcUa_Message Enter"); | |
| var MessageChunk v_decodedChunk; | |
| var octetstring v_encodedChunk; | |
| if (dec_MessageChunk(pl_in, v_decodedChunk) == -1) { | |
| setverdict(fail, "Failed to decode message chunk!"); | |
| return; | |
| } | |
| var integer v_messageType := v_decodedChunk.header.messageType; | |
| if (v_messageType == 4998472 or v_messageType == 4932417 or v_messageType == 5395013 or v_messageType == 4540498 ) { | |
| log("Message type: Transport Message"); | |
| } else { | |
| log("Message type: Service"); | |
| var ServiceBody body; | |
| if (dec_ServiceBody(v_decodedChunk.body, body) == -1) { | |
| setverdict(fail, "Failed to decode service!"); | |
| } | |
| v_decodedChunk.body := enc_ServiceBody(body); | |
| } | |
| v_encodedChunk := enc_MessageChunk(v_decodedChunk); | |
| if (pl_in == v_encodedChunk) { | |
| setverdict(pass); | |
| } else { | |
| setverdict(fail); | |
| } | |
| } | |
| function f_connectToServer() runs on SendReceiveComp { | |
| connId := CreateConnection(testPort, LocalTCPConnection, RemoteTCPConnection); | |
| if (connId != -1) { | |
| setverdict(pass, "Connection established!"); | |
| } else { | |
| setverdict(fail, "Failed to create connection!"); | |
| } | |
| } | |
| function f_closeConnectToServer() runs on SendReceiveComp { | |
| if (CloseConnection(testPort, connId)) { | |
| setverdict(pass, "Connection closed!"); | |
| } else { | |
| setverdict(fail, "Failed to close connection!"); | |
| } | |
| } | |
| function f_sendMessagesToServer() runs on SendReceiveComp { | |
| connId := CreateConnection(testPort, LocalTCPConnection, RemoteTCPConnection); | |
| if (connId == -1) { | |
| setverdict(fail, "Failed to create connection!"); | |
| return; | |
| } | |
| f_sendReceive_Message(f_GetHelloMessage(), f_GetAckMessage(), true); | |
| f_sendReceive_Message(f_GetOpenSecureChannelRequest(), f_GetOpenSecureChannelResponse(), true); | |
| f_sendReceive_Message(f_GetEndpointsRequest(), f_GetServiceResponse(), true); | |
| f_sendReceive_Message(f_GetCreateSessionRequest(), f_GetServiceResponse(), true); | |
| f_sendReceive_Message(f_GetActivateSessionRequest(), f_GetServiceResponse(), true); | |
| f_sendReceive_Message(f_GetBrowseRequest(), f_GetServiceResponse(), true); | |
| f_sendReceive_Message(f_GetReadRequest(), f_GetServiceResponse(), true); | |
| f_sendReceive_Message(f_GetCreateSubscRequest(), f_GetServiceResponse(), true); | |
| f_sendReceive_Message(f_GetCloseSessionRequest(), f_GetServiceResponse(), true); | |
| f_sendReceive_Message(f_GetCloseSecureChannelRequest(), omit, false); | |
| if (not CloseConnection(testPort, connId)) { | |
| setverdict(fail, "Failed to close connection!"); | |
| } | |
| } | |
| //Sends an encoded message, and matches the reply with a template | |
| function f_sendReceive_Message(in octetstring msgToSend, in template MessageChunk expectedMsg, in boolean expectReply) runs on SendReceiveComp { | |
| var octetstring actualMsg; | |
| SendMessage(testPort, RemoteTCPConnection, connId, msgToSend); | |
| if (expectReply) { | |
| if (ReceiveMessage(testPort, actualMsg)) { | |
| var MessageChunk v_decodedMessage; //Expected msg | |
| //Decode message | |
| if (dec_MessageChunk(actualMsg, v_decodedMessage) == -1) { | |
| setverdict(fail, "Received reply, but unable to decode it!"); | |
| } | |
| if (match(v_decodedMessage, expectedMsg)) { | |
| setverdict(pass, "Received expected reply!"); | |
| } else { | |
| setverdict(fail, "Received unexpeted reply!"); | |
| } | |
| } else { | |
| setverdict(fail, "Failed to receive response!"); | |
| } | |
| } else { | |
| setverdict(pass, "Message sent!"); | |
| } | |
| } | |
| testcase tc_dec_Hello() runs on MyComp { | |
| const octetstring v_msg := encodedBinaryHello; | |
| f_encDec_OpcUa_MessageChunk(v_msg); | |
| } | |
| testcase tc_dec_Ack() runs on MyComp { | |
| const octetstring v_msg := encodedBinaryAck; | |
| f_encDec_OpcUa_MessageChunk(v_msg); | |
| } | |
| testcase tc_dec_Err() runs on MyComp { | |
| const octetstring v_msg := encodedBinaryError; | |
| f_encDec_OpcUa_MessageChunk(v_msg); | |
| } | |
| testcase tc_dec_OpenSecReq() runs on MyComp { | |
| const octetstring v_msg := encodedBinaryOpenSecReq; | |
| f_encDec_OpcUa_MessageChunk(v_msg); | |
| } | |
| testcase tc_dec_OpenSecResp() runs on MyComp { | |
| const octetstring v_msg := encodedBinaryOpenSecResp; | |
| f_encDec_OpcUa_MessageChunk(v_msg); | |
| } | |
| testcase tc_dec_CloseSecReq() runs on MyComp { | |
| const octetstring v_msg := encodedBinaryCloseSecReq; | |
| f_encDec_OpcUa_MessageChunk(v_msg); | |
| } | |
| testcase tc_dec_GetEndpointsReq() runs on MyComp { | |
| const octetstring v_msg := encodedBinaryGetEndointsReq; | |
| f_encDec_OpcUa_MessageChunk(v_msg); | |
| } | |
| testcase tc_dec_GetEndpointsResp() runs on MyComp { | |
| const octetstring v_msg := encodedBinaryGetEndointsResp; | |
| f_encDec_OpcUa_MessageChunk(v_msg); | |
| } | |
| testcase tc_dec_CreateSessionReq() runs on MyComp { | |
| const octetstring v_msg := encodedBinaryCreateSessionReq; | |
| f_encDec_OpcUa_MessageChunk(v_msg); | |
| } | |
| testcase tc_dec_CreateSessionResp() runs on MyComp { | |
| const octetstring v_msg := encodedBinaryCreateSessionResp; | |
| f_encDec_OpcUa_MessageChunk(v_msg); | |
| } | |
| testcase tc_dec_ActivateSessionReq() runs on MyComp { | |
| const octetstring v_msg := encodedBinaryActivateSessionReq; | |
| f_encDec_OpcUa_MessageChunk(v_msg); | |
| } | |
| testcase tc_dec_ActivateSessionResp() runs on MyComp { | |
| const octetstring v_msg := encodedBinaryActivateSessionResp; | |
| f_encDec_OpcUa_MessageChunk(v_msg); | |
| } | |
| testcase tc_dec_BrowseReq() runs on MyComp { | |
| const octetstring v_msg := encodedBinaryBrowseReq; | |
| f_encDec_OpcUa_MessageChunk(v_msg); | |
| } | |
| testcase tc_dec_BrowseResp() runs on MyComp { | |
| const octetstring v_msg := encodedBinaryBrowseResp; | |
| f_encDec_OpcUa_MessageChunk(v_msg); | |
| } | |
| testcase tc_dec_ReadReq() runs on MyComp { | |
| const octetstring v_msg := encodedBinaryReadReq; | |
| f_encDec_OpcUa_MessageChunk(v_msg); | |
| } | |
| testcase tc_dec_ReadResp() runs on MyComp { | |
| const octetstring v_msg := encodedBinaryReadResp; | |
| f_encDec_OpcUa_MessageChunk(v_msg); | |
| } | |
| testcase tc_dec_CreateSubscReq() runs on MyComp { | |
| const octetstring v_msg := encodedBinaryCreateSubscReq; | |
| f_encDec_OpcUa_MessageChunk(v_msg); | |
| } | |
| testcase tc_dec_CreateSubscResp() runs on MyComp { | |
| const octetstring v_msg := encodedBinaryCreateSubscResp; | |
| f_encDec_OpcUa_MessageChunk(v_msg); | |
| } | |
| testcase tc_dec_CreateMonitoredItemReq() runs on MyComp { | |
| const octetstring v_msg := encodedBinaryCreateMonitoredItemReq; | |
| f_encDec_OpcUa_MessageChunk(v_msg); | |
| } | |
| testcase tc_dec_CreateMonitoredItemResp() runs on MyComp { | |
| const octetstring v_msg := encodedBinaryCreateMonitoredItemResp; | |
| f_encDec_OpcUa_MessageChunk(v_msg); | |
| } | |
| testcase tc_dec_PublishReq() runs on MyComp { | |
| const octetstring v_msg := encodedBinaryPublishReq; | |
| f_encDec_OpcUa_MessageChunk(v_msg); | |
| } | |
| testcase tc_dec_PublishResp() runs on MyComp { | |
| const octetstring v_msg := encodedBinaryPublishResp; | |
| f_encDec_OpcUa_MessageChunk(v_msg); | |
| } | |
| testcase tc_dec_DeleteSubscReq() runs on MyComp { | |
| const octetstring v_msg := encodedBinaryDeleteSubsReq; | |
| f_encDec_OpcUa_MessageChunk(v_msg); | |
| } | |
| testcase tc_dec_DeleteSubscResp() runs on MyComp { | |
| const octetstring v_msg := encodedBinaryDeleteSubsResp; | |
| f_encDec_OpcUa_MessageChunk(v_msg); | |
| } | |
| testcase tc_dec_CloseSessionReq() runs on MyComp { | |
| const octetstring v_msg := encodedBinaryClodeSessionReq; | |
| f_encDec_OpcUa_MessageChunk(v_msg); | |
| } | |
| testcase tc_dec_CloseSessionResp() runs on MyComp { | |
| const octetstring v_msg := encodedBinaryClodeSessionResp; | |
| f_encDec_OpcUa_MessageChunk(v_msg); | |
| } | |
| testcase tc_dec_TranslateBrowsePathsToNodeIdsReq() runs on MyComp { | |
| const octetstring v_msg := encodedBinaryTranslateBrowsePathsToNodeIdsReq; | |
| f_encDec_OpcUa_MessageChunk(v_msg); | |
| } | |
| testcase tc_dec_TranslateBrowsePathsToNodeIdsResp() runs on MyComp { | |
| const octetstring v_msg := encodedBinaryTranslateBrowsePathsToNodeIdsResp; | |
| f_encDec_OpcUa_MessageChunk(v_msg); | |
| } | |
| testcase tc_dec_SetPublishingModeReq() runs on MyComp { | |
| const octetstring v_msg := encodedBinarySetPublishingModeReq; | |
| f_encDec_OpcUa_MessageChunk(v_msg); | |
| } | |
| testcase tc_dec_SetPublishingModeResp() runs on MyComp { | |
| const octetstring v_msg := encodedBinarySetPublishingModeResp; | |
| f_encDec_OpcUa_MessageChunk(v_msg); | |
| } | |
| testcase tc_dec_CallReq() runs on MyComp { | |
| const octetstring v_msg := encodedBinaryCallReq; | |
| f_encDec_OpcUa_MessageChunk(v_msg); | |
| } | |
| testcase tc_dec_CallResp() runs on MyComp { | |
| const octetstring v_msg := encodedBinaryCallResp; | |
| f_encDec_OpcUa_MessageChunk(v_msg); | |
| } | |
| testcase tc_dec_SetMonitoringModeReq() runs on MyComp { | |
| const octetstring v_msg := encodedBinarySetMonitoringModeReq; | |
| f_encDec_OpcUa_MessageChunk(v_msg); | |
| } | |
| testcase tc_dec_SetMonitoringModeResp() runs on MyComp { | |
| const octetstring v_msg := encodedBinarySetMonitoringModeResp; | |
| f_encDec_OpcUa_MessageChunk(v_msg); | |
| } | |
| testcase tc_dec_ModifyMonitoredItemReq() runs on MyComp { | |
| const octetstring v_msg := encodedBinaryModifyMonitoredItemReq; | |
| f_encDec_OpcUa_MessageChunk(v_msg); | |
| } | |
| testcase tc_dec_ModifyMonitoredItemResp() runs on MyComp { | |
| const octetstring v_msg := encodedBinaryModifyMonitoredItemResp; | |
| f_encDec_OpcUa_MessageChunk(v_msg); | |
| } | |
| testcase tc_dec_WriteReq() runs on MyComp { | |
| const octetstring v_msg := encodedBinaryWriteReq; | |
| f_encDec_OpcUa_MessageChunk(v_msg); | |
| } | |
| testcase tc_dec_WriteResp() runs on MyComp { | |
| const octetstring v_msg := encodedBinaryWriteResp; | |
| f_encDec_OpcUa_MessageChunk(v_msg); | |
| } | |
| testcase tc_encDec_Hello() runs on MyComp { | |
| f_encDec_OpcUa_MessageChunk(f_GetHelloMessage()); | |
| } | |
| testcase tc_encDec_OpenSecureChannelReq() runs on MyComp { | |
| f_encDec_OpcUa_MessageChunk(f_GetOpenSecureChannelRequest()); | |
| } | |
| testcase tc_encDec_EndpointsReq() runs on MyComp { | |
| f_encDec_OpcUa_MessageChunk(f_GetEndpointsRequest()); | |
| } | |
| testcase tc_encDec_CreateSessionReq() runs on MyComp { | |
| f_encDec_OpcUa_MessageChunk(f_GetCreateSessionRequest()); | |
| } | |
| testcase tc_encDec_ActivateSessionReq() runs on MyComp { | |
| f_encDec_OpcUa_MessageChunk(f_GetActivateSessionRequest()); | |
| } | |
| testcase tc_encDec_GetBrowseReq() runs on MyComp { | |
| f_encDec_OpcUa_MessageChunk(f_GetBrowseRequest()); | |
| } | |
| testcase tc_encDec_GetReadReq() runs on MyComp { | |
| f_encDec_OpcUa_MessageChunk(f_GetReadRequest()); | |
| } | |
| testcase tc_encDec_CreateSubscReq() runs on MyComp { | |
| f_encDec_OpcUa_MessageChunk(f_GetCreateSubscRequest()); | |
| } | |
| testcase tc_encDec_CloseSessionReq() runs on MyComp { | |
| f_encDec_OpcUa_MessageChunk(f_GetCloseSessionRequest()); | |
| } | |
| testcase tc_encDec_CloseSecureChannelReq() runs on MyComp { | |
| f_encDec_OpcUa_MessageChunk(f_GetCloseSecureChannelRequest()); | |
| } | |
| testcase tc_sendMessagesToServer() runs on SendReceiveComp { | |
| map(mtc:testPort, system:testPort); | |
| f_sendMessagesToServer(); | |
| } | |
| control { | |
| //Decode and test encoded messages from Wireshark | |
| execute(tc_dec_Hello()); | |
| execute(tc_dec_Ack()); | |
| execute(tc_dec_Err()); | |
| execute(tc_dec_OpenSecReq()); | |
| execute(tc_dec_OpenSecResp()); | |
| execute(tc_dec_CloseSecReq()); | |
| execute(tc_dec_GetEndpointsReq()); | |
| execute(tc_dec_GetEndpointsResp()); | |
| execute(tc_dec_CreateSessionReq()); | |
| execute(tc_dec_CreateSessionResp()); | |
| execute(tc_dec_ActivateSessionReq()); | |
| execute(tc_dec_ActivateSessionResp()); | |
| execute(tc_dec_BrowseReq()); | |
| execute(tc_dec_BrowseResp()); | |
| execute(tc_dec_ReadReq()); | |
| execute(tc_dec_ReadResp()); | |
| execute(tc_dec_CreateSubscReq()); | |
| execute(tc_dec_CreateSubscResp()); | |
| execute(tc_dec_CreateMonitoredItemReq()); | |
| execute(tc_dec_CreateMonitoredItemResp()); | |
| execute(tc_dec_DeleteSubscReq()); | |
| execute(tc_dec_DeleteSubscResp()); | |
| execute(tc_dec_PublishReq()); | |
| execute(tc_dec_PublishResp()); | |
| execute(tc_dec_CloseSessionReq()); | |
| execute(tc_dec_CloseSessionResp()); | |
| execute(tc_dec_TranslateBrowsePathsToNodeIdsReq()); | |
| execute(tc_dec_TranslateBrowsePathsToNodeIdsResp()); | |
| execute(tc_dec_SetPublishingModeReq()); | |
| execute(tc_dec_SetPublishingModeResp()); | |
| execute(tc_dec_SetMonitoringModeReq()); | |
| execute(tc_dec_SetMonitoringModeResp()); | |
| execute(tc_dec_CallReq()); | |
| execute(tc_dec_CallResp()); | |
| execute(tc_dec_ModifyMonitoredItemReq()); | |
| execute(tc_dec_ModifyMonitoredItemResp()); | |
| execute(tc_dec_WriteReq()); | |
| execute(tc_dec_WriteResp()); | |
| //Encode and decode Messages created from TTCN structures | |
| execute(tc_encDec_Hello()); | |
| execute(tc_encDec_OpenSecureChannelReq()); | |
| execute(tc_encDec_EndpointsReq()); | |
| execute(tc_encDec_CreateSessionReq()); | |
| execute(tc_encDec_ActivateSessionReq()); | |
| execute(tc_encDec_GetBrowseReq()); | |
| execute(tc_encDec_GetReadReq()); | |
| execute(tc_encDec_CreateSubscReq()); | |
| execute(tc_encDec_CloseSessionReq()); | |
| execute(tc_encDec_CloseSecureChannelReq()); | |
| //Encode and send TTCN messages to a local OPC server | |
| execute (tc_sendMessagesToServer()); | |
| } | |
| } |