check_pubsub_pds.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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 <check.h>
  8. #include <open62541/server_pubsub.h>
  9. #include <open62541/plugin/pubsub_udp.h>
  10. #include <open62541/server_config_default.h>
  11. #include "open62541/types_generated_encoding_binary.h"
  12. #include "ua_server_internal.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(AddPDSWithMinimalValidConfiguration){
  32. UA_StatusCode retVal = UA_STATUSCODE_GOOD;
  33. UA_PublishedDataSetConfig pdsConfig;
  34. memset(&pdsConfig, 0, sizeof(UA_PublishedDataSetConfig));
  35. pdsConfig.publishedDataSetType = UA_PUBSUB_DATASET_PUBLISHEDITEMS;
  36. pdsConfig.name = UA_STRING("TEST PDS 1");
  37. retVal |= UA_Server_addPublishedDataSet(server, &pdsConfig, NULL).addResult;
  38. ck_assert_int_eq(server->pubSubManager.publishedDataSetsSize, 1);
  39. ck_assert_int_eq(retVal, UA_STATUSCODE_GOOD);
  40. UA_NodeId newPDSNodeID;
  41. retVal |= UA_Server_addPublishedDataSet(server, &pdsConfig, &newPDSNodeID).addResult;
  42. ck_assert_int_eq(server->pubSubManager.publishedDataSetsSize, 2);
  43. ck_assert_int_eq(newPDSNodeID.identifierType, UA_NODEIDTYPE_NUMERIC);
  44. ck_assert_int_ne(newPDSNodeID.identifier.numeric, 0);
  45. ck_assert_int_eq(retVal, UA_STATUSCODE_GOOD);
  46. } END_TEST
  47. START_TEST(AddRemoveAddPDSWithMinimalValidConfiguration){
  48. UA_StatusCode retVal = UA_STATUSCODE_GOOD;
  49. UA_PublishedDataSetConfig pdsConfig;
  50. memset(&pdsConfig, 0, sizeof(UA_PublishedDataSetConfig));
  51. pdsConfig.publishedDataSetType = UA_PUBSUB_DATASET_PUBLISHEDITEMS;
  52. pdsConfig.name = UA_STRING("TEST PDS 1");
  53. UA_NodeId newPDSNodeID;
  54. retVal |= UA_Server_addPublishedDataSet(server, &pdsConfig, &newPDSNodeID).addResult;
  55. ck_assert_int_eq(server->pubSubManager.publishedDataSetsSize, 1);
  56. ck_assert_int_eq(retVal, UA_STATUSCODE_GOOD);
  57. retVal |= UA_Server_removePublishedDataSet(server, newPDSNodeID);
  58. ck_assert_int_eq(server->pubSubManager.publishedDataSetsSize, 0);
  59. ck_assert_int_eq(retVal, UA_STATUSCODE_GOOD);
  60. retVal |= UA_Server_addPublishedDataSet(server, &pdsConfig, &newPDSNodeID).addResult;
  61. ck_assert_int_eq(server->pubSubManager.publishedDataSetsSize, 1);
  62. ck_assert_int_eq(retVal, UA_STATUSCODE_GOOD);
  63. } END_TEST
  64. START_TEST(AddPDSWithNullConfig){
  65. UA_StatusCode retVal = UA_STATUSCODE_GOOD;
  66. retVal |= UA_Server_addPublishedDataSet(server, NULL, NULL).addResult;
  67. ck_assert_int_eq(server->pubSubManager.publishedDataSetsSize, 0);
  68. ck_assert_int_ne(retVal, UA_STATUSCODE_GOOD);
  69. } END_TEST
  70. START_TEST(AddPDSWithUnsupportedType){
  71. UA_StatusCode retVal = UA_STATUSCODE_GOOD;
  72. UA_PublishedDataSetConfig pdsConfig;
  73. memset(&pdsConfig, 0, sizeof(UA_PublishedDataSetConfig));
  74. pdsConfig.name = UA_STRING("TEST PDS 1");
  75. pdsConfig.publishedDataSetType = UA_PUBSUB_DATASET_PUBLISHEDITEMS_TEMPLATE;
  76. retVal |= UA_Server_addPublishedDataSet(server, &pdsConfig, NULL).addResult;
  77. ck_assert_int_eq(server->pubSubManager.publishedDataSetsSize, 0);
  78. ck_assert_int_ne(retVal, UA_STATUSCODE_GOOD);
  79. pdsConfig.publishedDataSetType = UA_PUBSUB_DATASET_PUBLISHEDEVENTS;
  80. ck_assert_int_eq(server->pubSubManager.publishedDataSetsSize, 0);
  81. ck_assert_int_ne(retVal, UA_STATUSCODE_GOOD);
  82. pdsConfig.publishedDataSetType = UA_PUBSUB_DATASET_PUBLISHEDEVENTS_TEMPLATE;
  83. ck_assert_int_eq(server->pubSubManager.publishedDataSetsSize, 0);
  84. ck_assert_int_ne(retVal, UA_STATUSCODE_GOOD);
  85. } END_TEST
  86. START_TEST(GetPDSConfigurationAndCompareValues){
  87. UA_StatusCode retVal = UA_STATUSCODE_GOOD;
  88. UA_PublishedDataSetConfig pdsConfig;
  89. memset(&pdsConfig, 0, sizeof(UA_PublishedDataSetConfig));
  90. pdsConfig.publishedDataSetType = UA_PUBSUB_DATASET_PUBLISHEDITEMS;
  91. pdsConfig.name = UA_STRING("TEST PDS 1");
  92. UA_NodeId pdsIdentifier;
  93. retVal |= UA_Server_addPublishedDataSet(server, &pdsConfig, &pdsIdentifier).addResult;
  94. ck_assert_int_eq(server->pubSubManager.publishedDataSetsSize, 1);
  95. ck_assert_int_eq(retVal, UA_STATUSCODE_GOOD);
  96. UA_PublishedDataSetConfig pdsConfigCopy;
  97. memset(&pdsConfigCopy, 0, sizeof(UA_PublishedDataSetConfig));
  98. UA_Server_getPublishedDataSetConfig(server, pdsIdentifier, &pdsConfigCopy);
  99. ck_assert_int_eq(UA_String_equal(&pdsConfig.name, &pdsConfigCopy.name), UA_TRUE);
  100. UA_PublishedDataSetConfig_deleteMembers(&pdsConfigCopy);
  101. } END_TEST
  102. int main(void) {
  103. TCase *tc_add_pubsub_pds_minimal_config = tcase_create("Create PubSub PublishedDataItem with minimal valid config");
  104. tcase_add_checked_fixture(tc_add_pubsub_pds_minimal_config, setup, teardown);
  105. tcase_add_test(tc_add_pubsub_pds_minimal_config, AddPDSWithMinimalValidConfiguration);
  106. tcase_add_test(tc_add_pubsub_pds_minimal_config, AddRemoveAddPDSWithMinimalValidConfiguration);
  107. TCase *tc_add_pubsub_pds_invalid_config = tcase_create("Create PubSub PublishedDataItem with minimal invalid config");
  108. tcase_add_checked_fixture(tc_add_pubsub_pds_invalid_config, setup, teardown);
  109. tcase_add_test(tc_add_pubsub_pds_invalid_config, AddPDSWithNullConfig);
  110. tcase_add_test(tc_add_pubsub_pds_invalid_config, AddPDSWithUnsupportedType);
  111. TCase *tc_add_pubsub_pds_handling_utils = tcase_create("PubSub PublishedDataSet handling");
  112. tcase_add_checked_fixture(tc_add_pubsub_pds_handling_utils, setup, teardown);
  113. tcase_add_test(tc_add_pubsub_pds_handling_utils, GetPDSConfigurationAndCompareValues);
  114. //tcase_add_test(tc_add_pubsub_connections_maximal_config, GetMaximalConnectionConfigurationAndCompareValues);
  115. Suite *s = suite_create("PubSub PublishedDataSets handling");
  116. suite_add_tcase(s, tc_add_pubsub_pds_minimal_config);
  117. suite_add_tcase(s, tc_add_pubsub_pds_invalid_config);
  118. suite_add_tcase(s, tc_add_pubsub_pds_handling_utils);
  119. //suite_add_tcase(s, tc_add_pubsub_connections_maximal_config);
  120. //suite_add_tcase(s, tc_decode);
  121. SRunner *sr = srunner_create(s);
  122. srunner_set_fork_status(sr, CK_NOFORK);
  123. srunner_run_all(sr,CK_NORMAL);
  124. int number_failed = srunner_ntests_failed(sr);
  125. srunner_free(sr);
  126. return (number_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
  127. }