check_services_subscriptions.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  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. #include "ua_server.h"
  5. #include "server/ua_services.h"
  6. #include "server/ua_server_internal.h"
  7. #include "server/ua_subscription.h"
  8. #include "ua_config_standard.h"
  9. #include "check.h"
  10. #include "testing_clock.h"
  11. static UA_Server *server = NULL;
  12. static UA_ServerConfig *config = NULL;
  13. static void setup(void) {
  14. config = UA_ServerConfig_new_default();
  15. server = UA_Server_new(config);
  16. UA_Server_run_startup(server);
  17. }
  18. static void teardown(void) {
  19. UA_Server_run_shutdown(server);
  20. UA_Server_delete(server);
  21. UA_ServerConfig_delete(config);
  22. }
  23. UA_UInt32 subscriptionId;
  24. UA_UInt32 monitoredItemId;
  25. START_TEST(Server_createSubscription) {
  26. /* Create a subscription */
  27. UA_CreateSubscriptionRequest request;
  28. UA_CreateSubscriptionRequest_init(&request);
  29. request.publishingEnabled = true;
  30. UA_CreateSubscriptionResponse response;
  31. UA_CreateSubscriptionResponse_init(&response);
  32. Service_CreateSubscription(server, &adminSession, &request, &response);
  33. ck_assert_uint_eq(response.responseHeader.serviceResult, UA_STATUSCODE_GOOD);
  34. subscriptionId = response.subscriptionId;
  35. UA_CreateSubscriptionResponse_deleteMembers(&response);
  36. }
  37. END_TEST
  38. START_TEST(Server_modifySubscription) {
  39. UA_ModifySubscriptionRequest request;
  40. UA_ModifySubscriptionRequest_init(&request);
  41. request.subscriptionId = subscriptionId;
  42. // just some arbitrary numbers to test. They have no specific reason
  43. request.requestedPublishingInterval = 100; // in ms
  44. request.requestedLifetimeCount = 1000;
  45. request.requestedMaxKeepAliveCount = 1000;
  46. request.maxNotificationsPerPublish = 1;
  47. request.priority = 10;
  48. UA_ModifySubscriptionResponse response;
  49. UA_ModifySubscriptionResponse_init(&response);
  50. Service_ModifySubscription(server, &adminSession, &request, &response);
  51. ck_assert_uint_eq(response.responseHeader.serviceResult, UA_STATUSCODE_GOOD);
  52. UA_ModifySubscriptionResponse_deleteMembers(&response);
  53. }
  54. END_TEST
  55. START_TEST(Server_setPublishingMode) {
  56. UA_SetPublishingModeRequest request;
  57. UA_SetPublishingModeRequest_init(&request);
  58. request.publishingEnabled = UA_TRUE;
  59. request.subscriptionIdsSize = 1;
  60. request.subscriptionIds = &subscriptionId;
  61. UA_SetPublishingModeResponse response;
  62. UA_SetPublishingModeResponse_init(&response);
  63. Service_SetPublishingMode(server, &adminSession, &request, &response);
  64. ck_assert_uint_eq(response.responseHeader.serviceResult, UA_STATUSCODE_GOOD);
  65. ck_assert_uint_eq(response.resultsSize, 1);
  66. ck_assert_uint_eq(response.results[0], UA_STATUSCODE_GOOD);
  67. UA_SetPublishingModeResponse_deleteMembers(&response);
  68. }
  69. END_TEST
  70. START_TEST(Server_republish) {
  71. UA_RepublishRequest request;
  72. UA_RepublishRequest_init(&request);
  73. request.subscriptionId = subscriptionId;
  74. request.retransmitSequenceNumber = 0;
  75. UA_RepublishResponse response;
  76. UA_RepublishResponse_init(&response);
  77. Service_Republish(server, &adminSession, &request, &response);
  78. ck_assert_uint_eq(response.responseHeader.serviceResult, UA_STATUSCODE_BADMESSAGENOTAVAILABLE);
  79. UA_RepublishResponse_deleteMembers(&response);
  80. }
  81. END_TEST
  82. START_TEST(Server_republish_invalid) {
  83. UA_RepublishRequest request;
  84. UA_RepublishRequest_init(&request);
  85. request.subscriptionId = subscriptionId;
  86. request.retransmitSequenceNumber = 0;
  87. UA_RepublishResponse response;
  88. UA_RepublishResponse_init(&response);
  89. Service_Republish(server, &adminSession, &request, &response);
  90. ck_assert_uint_eq(response.responseHeader.serviceResult, UA_STATUSCODE_BADSUBSCRIPTIONIDINVALID);
  91. UA_RepublishResponse_deleteMembers(&response);
  92. }
  93. END_TEST
  94. START_TEST(Server_deleteSubscription) {
  95. /* Remove the subscription */
  96. UA_DeleteSubscriptionsRequest del_request;
  97. UA_DeleteSubscriptionsRequest_init(&del_request);
  98. del_request.subscriptionIdsSize = 1;
  99. del_request.subscriptionIds = &subscriptionId;
  100. UA_DeleteSubscriptionsResponse del_response;
  101. UA_DeleteSubscriptionsResponse_init(&del_response);
  102. Service_DeleteSubscriptions(server, &adminSession, &del_request, &del_response);
  103. ck_assert_uint_eq(del_response.resultsSize, 1);
  104. ck_assert_uint_eq(del_response.results[0], UA_STATUSCODE_GOOD);
  105. UA_DeleteSubscriptionsResponse_deleteMembers(&del_response);
  106. }
  107. END_TEST
  108. START_TEST(Server_publishCallback) {
  109. /* Create a subscription */
  110. UA_CreateSubscriptionRequest request;
  111. UA_CreateSubscriptionResponse response;
  112. UA_CreateSubscriptionRequest_init(&request);
  113. request.publishingEnabled = true;
  114. UA_CreateSubscriptionResponse_init(&response);
  115. Service_CreateSubscription(server, &adminSession, &request, &response);
  116. ck_assert_uint_eq(response.responseHeader.serviceResult, UA_STATUSCODE_GOOD);
  117. UA_UInt32 subscriptionId1 = response.subscriptionId;
  118. UA_CreateSubscriptionResponse_deleteMembers(&response);
  119. /* Create a second subscription */
  120. UA_CreateSubscriptionRequest_init(&request);
  121. request.publishingEnabled = true;
  122. UA_CreateSubscriptionResponse_init(&response);
  123. Service_CreateSubscription(server, &adminSession, &request, &response);
  124. ck_assert_uint_eq(response.responseHeader.serviceResult, UA_STATUSCODE_GOOD);
  125. UA_UInt32 subscriptionId2 = response.subscriptionId;
  126. UA_Double publishingInterval = response.revisedPublishingInterval;
  127. ck_assert(publishingInterval > 0.0f);
  128. UA_CreateSubscriptionResponse_deleteMembers(&response);
  129. /* Keepalive is set to max initially */
  130. UA_Subscription *sub;
  131. LIST_FOREACH(sub, &adminSession.serverSubscriptions, listEntry)
  132. ck_assert_uint_eq(sub->currentKeepAliveCount, sub->maxKeepAliveCount);
  133. /* Sleep until the publishing interval times out */
  134. UA_sleep((UA_UInt32)publishingInterval + 1);
  135. UA_Server_run_iterate(server, false);
  136. LIST_FOREACH(sub, &adminSession.serverSubscriptions, listEntry)
  137. ck_assert_uint_eq(sub->currentKeepAliveCount, sub->maxKeepAliveCount+1);
  138. /* Remove the subscriptions */
  139. UA_DeleteSubscriptionsRequest del_request;
  140. UA_DeleteSubscriptionsRequest_init(&del_request);
  141. UA_UInt32 removeIds[2] = {subscriptionId1, subscriptionId2};
  142. del_request.subscriptionIdsSize = 2;
  143. del_request.subscriptionIds = removeIds;
  144. UA_DeleteSubscriptionsResponse del_response;
  145. UA_DeleteSubscriptionsResponse_init(&del_response);
  146. Service_DeleteSubscriptions(server, &adminSession, &del_request, &del_response);
  147. ck_assert_uint_eq(del_response.responseHeader.serviceResult, UA_STATUSCODE_GOOD);
  148. ck_assert_uint_eq(del_response.resultsSize, 2);
  149. ck_assert_uint_eq(del_response.results[0], UA_STATUSCODE_GOOD);
  150. ck_assert_uint_eq(del_response.results[1], UA_STATUSCODE_GOOD);
  151. UA_DeleteSubscriptionsResponse_deleteMembers(&del_response);
  152. }
  153. END_TEST
  154. START_TEST(Server_createMonitoredItems) {
  155. UA_CreateMonitoredItemsRequest request;
  156. UA_CreateMonitoredItemsRequest_init(&request);
  157. request.subscriptionId = subscriptionId;
  158. request.timestampsToReturn = UA_TIMESTAMPSTORETURN_SERVER;
  159. UA_MonitoredItemCreateRequest item;
  160. UA_MonitoredItemCreateRequest_init(&item);
  161. UA_ReadValueId rvi;
  162. UA_ReadValueId_init(&rvi);
  163. rvi.nodeId = UA_NODEID_NUMERIC(0, UA_NS0ID_SERVER);
  164. rvi.attributeId = UA_ATTRIBUTEID_BROWSENAME;
  165. rvi.indexRange = UA_STRING_NULL;
  166. item.itemToMonitor = rvi;
  167. item.monitoringMode = UA_MONITORINGMODE_REPORTING;
  168. UA_MonitoringParameters params;
  169. UA_MonitoringParameters_init(&params);
  170. item.requestedParameters = params;
  171. request.itemsToCreateSize = 1;
  172. request.itemsToCreate = &item;
  173. UA_CreateMonitoredItemsResponse response;
  174. UA_CreateMonitoredItemsResponse_init(&response);
  175. Service_CreateMonitoredItems(server, &adminSession, &request, &response);
  176. ck_assert_uint_eq(response.responseHeader.serviceResult, UA_STATUSCODE_GOOD);
  177. ck_assert_uint_eq(response.resultsSize, 1);
  178. ck_assert_uint_eq(response.results[0].statusCode, UA_STATUSCODE_GOOD);
  179. monitoredItemId = response.results[0].monitoredItemId;
  180. UA_MonitoredItemCreateRequest_deleteMembers(&item);
  181. UA_CreateMonitoredItemsResponse_deleteMembers(&response);
  182. }
  183. END_TEST
  184. START_TEST(Server_modifyMonitoredItems) {
  185. UA_ModifyMonitoredItemsRequest request;
  186. UA_ModifyMonitoredItemsRequest_init(&request);
  187. request.subscriptionId = subscriptionId;
  188. request.timestampsToReturn = UA_TIMESTAMPSTORETURN_SERVER;
  189. UA_MonitoredItemModifyRequest item;
  190. UA_MonitoredItemModifyRequest_init(&item);
  191. UA_ReadValueId rvi;
  192. UA_ReadValueId_init(&rvi);
  193. rvi.nodeId = UA_NODEID_NUMERIC(0, UA_NS0ID_SERVER);
  194. rvi.attributeId = UA_ATTRIBUTEID_DESCRIPTION;
  195. rvi.indexRange = UA_STRING_NULL;
  196. item.monitoredItemId = monitoredItemId;
  197. UA_MonitoringParameters params;
  198. UA_MonitoringParameters_init(&params);
  199. item.requestedParameters = params;
  200. request.itemsToModifySize = 1;
  201. request.itemsToModify = &item;
  202. UA_ModifyMonitoredItemsResponse response;
  203. UA_ModifyMonitoredItemsResponse_init(&response);
  204. Service_ModifyMonitoredItems(server, &adminSession, &request, &response);
  205. ck_assert_uint_eq(response.responseHeader.serviceResult, UA_STATUSCODE_GOOD);
  206. ck_assert_uint_eq(response.resultsSize, 1);
  207. ck_assert_uint_eq(response.results[0].statusCode, UA_STATUSCODE_GOOD);
  208. UA_MonitoredItemModifyRequest_deleteMembers(&item);
  209. UA_ModifyMonitoredItemsResponse_deleteMembers(&response);
  210. }
  211. END_TEST
  212. START_TEST(Server_setMonitoringMode) {
  213. UA_SetMonitoringModeRequest request;
  214. UA_SetMonitoringModeRequest_init(&request);
  215. request.subscriptionId = subscriptionId;
  216. request.monitoringMode = UA_MONITORINGMODE_DISABLED;
  217. request.monitoredItemIdsSize = 1;
  218. request.monitoredItemIds = &monitoredItemId;
  219. UA_SetMonitoringModeResponse response;
  220. UA_SetMonitoringModeResponse_init(&response);
  221. Service_SetMonitoringMode(server, &adminSession, &request, &response);
  222. ck_assert_uint_eq(response.responseHeader.serviceResult, UA_STATUSCODE_GOOD);
  223. ck_assert_uint_eq(response.resultsSize, 1);
  224. ck_assert_uint_eq(response.results[0], UA_STATUSCODE_GOOD);
  225. UA_SetMonitoringModeResponse_deleteMembers(&response);
  226. }
  227. END_TEST
  228. START_TEST(Server_deleteMonitoredItems) {
  229. UA_DeleteMonitoredItemsRequest request;
  230. UA_DeleteMonitoredItemsRequest_init(&request);
  231. request.subscriptionId = subscriptionId;
  232. request.monitoredItemIdsSize = 1;
  233. request.monitoredItemIds = &monitoredItemId;
  234. UA_DeleteMonitoredItemsResponse response;
  235. UA_DeleteMonitoredItemsResponse_init(&response);
  236. Service_DeleteMonitoredItems(server, &adminSession, &request, &response);
  237. ck_assert_uint_eq(response.responseHeader.serviceResult, UA_STATUSCODE_GOOD);
  238. ck_assert_uint_eq(response.resultsSize, 1);
  239. ck_assert_uint_eq(response.results[0], UA_STATUSCODE_GOOD);
  240. UA_DeleteMonitoredItemsResponse_deleteMembers(&response);
  241. }
  242. END_TEST
  243. static Suite* testSuite_Client(void) {
  244. Suite *s = suite_create("Server Subscription");
  245. TCase *tc_server = tcase_create("Server Subscription Basic");
  246. tcase_add_checked_fixture(tc_server, setup, teardown);
  247. tcase_add_test(tc_server, Server_createSubscription);
  248. tcase_add_test(tc_server, Server_modifySubscription);
  249. tcase_add_test(tc_server, Server_setPublishingMode);
  250. tcase_add_test(tc_server, Server_createMonitoredItems);
  251. tcase_add_test(tc_server, Server_modifyMonitoredItems);
  252. tcase_add_test(tc_server, Server_setMonitoringMode);
  253. tcase_add_test(tc_server, Server_deleteMonitoredItems);
  254. tcase_add_test(tc_server, Server_republish);
  255. tcase_add_test(tc_server, Server_deleteSubscription);
  256. tcase_add_test(tc_server, Server_republish_invalid);
  257. tcase_add_test(tc_server, Server_publishCallback);
  258. suite_add_tcase(s, tc_server);
  259. return s;
  260. }
  261. int main(void) {
  262. Suite *s = testSuite_Client();
  263. SRunner *sr = srunner_create(s);
  264. srunner_set_fork_status(sr, CK_NOFORK);
  265. srunner_run_all(sr,CK_NORMAL);
  266. int number_failed = srunner_ntests_failed(sr);
  267. srunner_free(sr);
  268. return (number_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
  269. }