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