check_pubsub_pds.c 6.7 KB

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