check_pubsub_connection_mqtt.c 12 KB

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