check_services_subscriptions.c 12 KB

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