check_pubsub_pds.c 6.6 KB

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