check_services_subscriptions.c 12 KB

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