check_pubsub_connection_udp.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. /* This Source Code Form is subject to the terms of the Mozilla Public
  2. * License, v. 2.0. If a copy of the MPL was not distributed with this
  3. * file, You can obtain one at http://mozilla.org/MPL/2.0/.
  4. *
  5. * Copyright (c) 2017 - 2018 Fraunhofer IOSB (Author: Andreas Ebner)
  6. */
  7. #include <open62541/plugin/pubsub_udp.h>
  8. #include <open62541/server_config_default.h>
  9. #include <open62541/server_pubsub.h>
  10. #include <open62541/types_generated_encoding_binary.h>
  11. #include "ua_server_internal.h"
  12. #include <check.h>
  13. UA_Server *server = NULL;
  14. static void setup(void) {
  15. server = UA_Server_new();
  16. UA_ServerConfig *config = UA_Server_getConfig(server);
  17. UA_ServerConfig_setDefault(config);
  18. config->pubsubTransportLayers = (UA_PubSubTransportLayer *)
  19. UA_malloc(sizeof(UA_PubSubTransportLayer));
  20. config->pubsubTransportLayers[0] = UA_PubSubTransportLayerUDPMP();
  21. config->pubsubTransportLayersSize++;
  22. UA_Server_run_startup(server);
  23. }
  24. static void teardown(void) {
  25. UA_Server_run_shutdown(server);
  26. UA_Server_delete(server);
  27. }
  28. START_TEST(AddConnectionsWithMinimalValidConfiguration){
  29. UA_StatusCode retVal;
  30. UA_PubSubConnectionConfig connectionConfig;
  31. memset(&connectionConfig, 0, sizeof(UA_PubSubConnectionConfig));
  32. connectionConfig.name = UA_STRING("UADP Connection");
  33. UA_NetworkAddressUrlDataType networkAddressUrl = {UA_STRING_NULL, UA_STRING("opc.udp://224.0.0.22:4840/")};
  34. UA_Variant_setScalar(&connectionConfig.address, &networkAddressUrl,
  35. &UA_TYPES[UA_TYPES_NETWORKADDRESSURLDATATYPE]);
  36. connectionConfig.transportProfileUri = UA_STRING("http://opcfoundation.org/UA-Profile/Transport/pubsub-udp-uadp");
  37. retVal = UA_Server_addPubSubConnection(server, &connectionConfig, NULL);
  38. ck_assert_int_eq(server->pubSubManager.connectionsSize, 1);
  39. ck_assert_int_eq(retVal, UA_STATUSCODE_GOOD);
  40. ck_assert(! TAILQ_EMPTY(&server->pubSubManager.connections));
  41. retVal = UA_Server_addPubSubConnection(server, &connectionConfig, NULL);
  42. ck_assert_int_eq(retVal, UA_STATUSCODE_GOOD);
  43. ck_assert(&server->pubSubManager.connections.tqh_first->listEntry.tqe_next != NULL);
  44. ck_assert_int_eq(server->pubSubManager.connectionsSize, 2);
  45. } END_TEST
  46. START_TEST(AddRemoveAddConnectionWithMinimalValidConfiguration){
  47. UA_StatusCode retVal;
  48. UA_PubSubConnectionConfig connectionConfig;
  49. memset(&connectionConfig, 0, sizeof(UA_PubSubConnectionConfig));
  50. connectionConfig.name = UA_STRING("UADP Connection");
  51. UA_NetworkAddressUrlDataType networkAddressUrl = {UA_STRING_NULL, UA_STRING("opc.udp://224.0.0.22:4840/")};
  52. UA_Variant_setScalar(&connectionConfig.address, &networkAddressUrl,
  53. &UA_TYPES[UA_TYPES_NETWORKADDRESSURLDATATYPE]);
  54. connectionConfig.transportProfileUri = UA_STRING("http://opcfoundation.org/UA-Profile/Transport/pubsub-udp-uadp");
  55. UA_NodeId connectionIdent;
  56. retVal = UA_Server_addPubSubConnection(server, &connectionConfig, &connectionIdent);
  57. ck_assert_int_eq(server->pubSubManager.connectionsSize, 1);
  58. ck_assert_int_eq(retVal, UA_STATUSCODE_GOOD);
  59. ck_assert(! TAILQ_EMPTY(&server->pubSubManager.connections));
  60. retVal |= UA_Server_removePubSubConnection(server, connectionIdent);
  61. ck_assert_int_eq(server->pubSubManager.connectionsSize, 0);
  62. ck_assert_int_eq(retVal, UA_STATUSCODE_GOOD);
  63. retVal = UA_Server_addPubSubConnection(server, &connectionConfig, &connectionIdent);
  64. ck_assert_int_eq(server->pubSubManager.connectionsSize, 1);
  65. ck_assert(&server->pubSubManager.connections.tqh_first->listEntry.tqe_next != NULL);
  66. ck_assert_int_eq(retVal, UA_STATUSCODE_GOOD);
  67. } END_TEST
  68. START_TEST(AddConnectionWithInvalidAddress){
  69. UA_StatusCode retVal;
  70. UA_PubSubConnectionConfig connectionConfig;
  71. memset(&connectionConfig, 0, sizeof(UA_PubSubConnectionConfig));
  72. connectionConfig.name = UA_STRING("UADP Connection");
  73. UA_NetworkAddressUrlDataType networkAddressUrl = {UA_STRING_NULL, UA_STRING("opc.udp://256.0.0.22:4840/")};
  74. UA_Variant_setScalar(&connectionConfig.address, &networkAddressUrl,
  75. &UA_TYPES[UA_TYPES_NETWORKADDRESSURLDATATYPE]);
  76. connectionConfig.transportProfileUri = UA_STRING("http://opcfoundation.org/UA-Profile/Transport/pubsub-udp-uadp");
  77. retVal = UA_Server_addPubSubConnection(server, &connectionConfig, NULL);
  78. ck_assert_int_eq(server->pubSubManager.connectionsSize, 0);
  79. ck_assert_int_ne(retVal, UA_STATUSCODE_GOOD);
  80. retVal = UA_Server_addPubSubConnection(server, &connectionConfig, NULL);
  81. ck_assert_int_ne(retVal, UA_STATUSCODE_GOOD);
  82. ck_assert_int_eq(server->pubSubManager.connectionsSize, 0);
  83. } END_TEST
  84. START_TEST(AddConnectionWithUnknownTransportURL){
  85. UA_StatusCode retVal;
  86. UA_PubSubConnectionConfig connectionConfig;
  87. memset(&connectionConfig, 0, sizeof(UA_PubSubConnectionConfig));
  88. connectionConfig.name = UA_STRING("UADP Connection");
  89. UA_NetworkAddressUrlDataType networkAddressUrl = {UA_STRING_NULL, UA_STRING("opc.udp://224.0.0.22:4840/")};
  90. UA_Variant_setScalar(&connectionConfig.address, &networkAddressUrl,
  91. &UA_TYPES[UA_TYPES_NETWORKADDRESSURLDATATYPE]);
  92. connectionConfig.transportProfileUri = UA_STRING("http://opcfoundation.org/UA-Profile/Transport/unknown-udp-uadp");
  93. UA_NodeId connectionIdent;
  94. retVal = UA_Server_addPubSubConnection(server, &connectionConfig, &connectionIdent);
  95. ck_assert_int_eq(server->pubSubManager.connectionsSize, 0);
  96. ck_assert_int_ne(retVal, UA_STATUSCODE_GOOD);
  97. } END_TEST
  98. START_TEST(AddConnectionWithNullConfig){
  99. UA_StatusCode retVal;
  100. retVal = UA_Server_addPubSubConnection(server, NULL, NULL);
  101. ck_assert_int_eq(server->pubSubManager.connectionsSize, 0);
  102. ck_assert_int_ne(retVal, UA_STATUSCODE_GOOD);
  103. } END_TEST
  104. START_TEST(AddSingleConnectionWithMaximalConfiguration){
  105. UA_NetworkAddressUrlDataType networkAddressUrlData = {UA_STRING("127.0.0.1"), UA_STRING("opc.udp://224.0.0.22:4840/")};
  106. UA_Variant address;
  107. UA_Variant_setScalar(&address, &networkAddressUrlData, &UA_TYPES[UA_TYPES_NETWORKADDRESSURLDATATYPE]);
  108. UA_KeyValuePair connectionOptions[3];
  109. connectionOptions[0].key = UA_QUALIFIEDNAME(0, "ttl");
  110. UA_UInt32 ttl = 10;
  111. UA_Variant_setScalar(&connectionOptions[0].value, &ttl, &UA_TYPES[UA_TYPES_UINT32]);
  112. connectionOptions[1].key = UA_QUALIFIEDNAME(0, "loopback");
  113. UA_Boolean loopback = UA_FALSE;
  114. UA_Variant_setScalar(&connectionOptions[1].value, &loopback, &UA_TYPES[UA_TYPES_BOOLEAN]);
  115. connectionOptions[2].key = UA_QUALIFIEDNAME(0, "reuse");
  116. UA_Boolean reuse = UA_TRUE;
  117. UA_Variant_setScalar(&connectionOptions[2].value, &reuse, &UA_TYPES[UA_TYPES_BOOLEAN]);
  118. UA_PubSubConnectionConfig connectionConf;
  119. memset(&connectionConf, 0, sizeof(UA_PubSubConnectionConfig));
  120. connectionConf.name = UA_STRING("UADP Connection");
  121. connectionConf.transportProfileUri = UA_STRING("http://opcfoundation.org/UA-Profile/Transport/pubsub-udp-uadp");
  122. connectionConf.enabled = true;
  123. connectionConf.publisherId.numeric = 223344;
  124. connectionConf.connectionPropertiesSize = 3;
  125. connectionConf.connectionProperties = connectionOptions;
  126. connectionConf.address = address;
  127. UA_NodeId connection;
  128. UA_StatusCode retVal = UA_Server_addPubSubConnection(server, &connectionConf, &connection);
  129. ck_assert_int_eq(server->pubSubManager.connectionsSize, 1);
  130. ck_assert_int_eq(retVal, UA_STATUSCODE_GOOD);
  131. ck_assert(! TAILQ_EMPTY(&server->pubSubManager.connections));
  132. } END_TEST
  133. START_TEST(GetMaximalConnectionConfigurationAndCompareValues){
  134. UA_NetworkAddressUrlDataType networkAddressUrlData = {UA_STRING("127.0.0.1"), UA_STRING("opc.udp://224.0.0.22:4840/")};
  135. UA_Variant address;
  136. UA_Variant_setScalar(&address, &networkAddressUrlData, &UA_TYPES[UA_TYPES_NETWORKADDRESSURLDATATYPE]);
  137. UA_KeyValuePair connectionOptions[3];
  138. connectionOptions[0].key = UA_QUALIFIEDNAME(0, "ttl");
  139. UA_UInt32 ttl = 10;
  140. UA_Variant_setScalar(&connectionOptions[0].value, &ttl, &UA_TYPES[UA_TYPES_UINT32]);
  141. connectionOptions[1].key = UA_QUALIFIEDNAME(0, "loopback");
  142. UA_Boolean loopback = UA_FALSE;
  143. UA_Variant_setScalar(&connectionOptions[1].value, &loopback, &UA_TYPES[UA_TYPES_BOOLEAN]);
  144. connectionOptions[2].key = UA_QUALIFIEDNAME(0, "reuse");
  145. UA_Boolean reuse = UA_TRUE;
  146. UA_Variant_setScalar(&connectionOptions[2].value, &reuse, &UA_TYPES[UA_TYPES_BOOLEAN]);
  147. UA_PubSubConnectionConfig connectionConf;
  148. memset(&connectionConf, 0, sizeof(UA_PubSubConnectionConfig));
  149. connectionConf.name = UA_STRING("UADP Connection");
  150. connectionConf.transportProfileUri = UA_STRING("http://opcfoundation.org/UA-Profile/Transport/pubsub-udp-uadp");
  151. connectionConf.enabled = true;
  152. connectionConf.publisherId.numeric = 223344;
  153. connectionConf.connectionPropertiesSize = 3;
  154. connectionConf.connectionProperties = connectionOptions;
  155. connectionConf.address = address;
  156. UA_NodeId connection;
  157. UA_StatusCode retVal = UA_Server_addPubSubConnection(server, &connectionConf, &connection);
  158. ck_assert_int_eq(retVal, UA_STATUSCODE_GOOD);
  159. UA_PubSubConnectionConfig connectionConfig;
  160. memset(&connectionConfig, 0, sizeof(UA_PubSubConnectionConfig));
  161. retVal |= UA_Server_getPubSubConnectionConfig(server, connection, &connectionConfig);
  162. ck_assert_int_eq(retVal, UA_STATUSCODE_GOOD);
  163. ck_assert(connectionConfig.connectionPropertiesSize == connectionConf.connectionPropertiesSize);
  164. ck_assert(UA_String_equal(&connectionConfig.name, &connectionConf.name) == UA_TRUE);
  165. ck_assert(UA_String_equal(&connectionConfig.transportProfileUri, &connectionConf.transportProfileUri) == UA_TRUE);
  166. UA_NetworkAddressUrlDataType networkAddressUrlDataCopy = *((UA_NetworkAddressUrlDataType *)connectionConfig.address.data);
  167. ck_assert(UA_NetworkAddressUrlDataType_calcSizeBinary(&networkAddressUrlDataCopy) == UA_NetworkAddressUrlDataType_calcSizeBinary(&networkAddressUrlData));
  168. for(size_t i = 0; i < connectionConfig.connectionPropertiesSize; i++){
  169. ck_assert(UA_String_equal(&connectionConfig.connectionProperties[i].key.name, &connectionConf.connectionProperties[i].key.name) == UA_TRUE);
  170. ck_assert(UA_Variant_calcSizeBinary(&connectionConfig.connectionProperties[i].value) == UA_Variant_calcSizeBinary(&connectionConf.connectionProperties[i].value));
  171. }
  172. UA_PubSubConnectionConfig_clear(&connectionConfig);
  173. } END_TEST
  174. int main(void) {
  175. TCase *tc_add_pubsub_connections_minimal_config = tcase_create("Create PubSub UDP Connections with minimal valid config");
  176. tcase_add_checked_fixture(tc_add_pubsub_connections_minimal_config, setup, teardown);
  177. tcase_add_test(tc_add_pubsub_connections_minimal_config, AddConnectionsWithMinimalValidConfiguration);
  178. tcase_add_test(tc_add_pubsub_connections_minimal_config, AddRemoveAddConnectionWithMinimalValidConfiguration);
  179. TCase *tc_add_pubsub_connections_invalid_config = tcase_create("Create PubSub UDP Connections with invalid configurations");
  180. tcase_add_checked_fixture(tc_add_pubsub_connections_invalid_config, setup, teardown);
  181. tcase_add_test(tc_add_pubsub_connections_invalid_config, AddConnectionWithInvalidAddress);
  182. tcase_add_test(tc_add_pubsub_connections_invalid_config, AddConnectionWithUnknownTransportURL);
  183. tcase_add_test(tc_add_pubsub_connections_invalid_config, AddConnectionWithNullConfig);
  184. TCase *tc_add_pubsub_connections_maximal_config = tcase_create("Create PubSub UDP Connections with maximal valid config");
  185. tcase_add_checked_fixture(tc_add_pubsub_connections_maximal_config, setup, teardown);
  186. tcase_add_test(tc_add_pubsub_connections_maximal_config, AddSingleConnectionWithMaximalConfiguration);
  187. tcase_add_test(tc_add_pubsub_connections_maximal_config, GetMaximalConnectionConfigurationAndCompareValues);
  188. Suite *s = suite_create("PubSub UDP connection creation");
  189. suite_add_tcase(s, tc_add_pubsub_connections_minimal_config);
  190. suite_add_tcase(s, tc_add_pubsub_connections_invalid_config);
  191. suite_add_tcase(s, tc_add_pubsub_connections_maximal_config);
  192. //suite_add_tcase(s, tc_decode);
  193. SRunner *sr = srunner_create(s);
  194. srunner_set_fork_status(sr, CK_NOFORK);
  195. srunner_run_all(sr,CK_NORMAL);
  196. int number_failed = srunner_ntests_failed(sr);
  197. srunner_free(sr);
  198. return (number_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
  199. }