check_services_subscriptions.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  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 <unistd.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_Server_run_iterate(server, false);
  132. usleep((useconds_t)(publishingInterval * 1000) + 1000);
  133. UA_Server_run_iterate(server, false);
  134. usleep((useconds_t)(publishingInterval * 1000) + 1000);
  135. LIST_FOREACH(sub, &adminSession.serverSubscriptions, listEntry)
  136. ck_assert_uint_eq(sub->currentKeepAliveCount, sub->maxKeepAliveCount+1);
  137. /* Remove the subscriptions */
  138. UA_DeleteSubscriptionsRequest del_request;
  139. UA_DeleteSubscriptionsRequest_init(&del_request);
  140. UA_UInt32 removeIds[2] = {subscriptionId1, subscriptionId2};
  141. del_request.subscriptionIdsSize = 2;
  142. del_request.subscriptionIds = removeIds;
  143. UA_DeleteSubscriptionsResponse del_response;
  144. UA_DeleteSubscriptionsResponse_init(&del_response);
  145. Service_DeleteSubscriptions(server, &adminSession, &del_request, &del_response);
  146. ck_assert_uint_eq(del_response.responseHeader.serviceResult, UA_STATUSCODE_GOOD);
  147. ck_assert_uint_eq(del_response.resultsSize, 2);
  148. ck_assert_uint_eq(del_response.results[0], UA_STATUSCODE_GOOD);
  149. ck_assert_uint_eq(del_response.results[1], UA_STATUSCODE_GOOD);
  150. UA_DeleteSubscriptionsResponse_deleteMembers(&del_response);
  151. }
  152. END_TEST
  153. START_TEST(Server_createMonitoredItems) {
  154. UA_CreateMonitoredItemsRequest request;
  155. UA_CreateMonitoredItemsRequest_init(&request);
  156. request.subscriptionId = subscriptionId;
  157. request.timestampsToReturn = UA_TIMESTAMPSTORETURN_SERVER;
  158. UA_MonitoredItemCreateRequest item;
  159. UA_MonitoredItemCreateRequest_init(&item);
  160. UA_ReadValueId rvi;
  161. UA_ReadValueId_init(&rvi);
  162. rvi.nodeId = UA_NODEID_NUMERIC(0, UA_NS0ID_SERVER);
  163. rvi.attributeId = UA_ATTRIBUTEID_BROWSENAME;
  164. rvi.indexRange = UA_STRING_NULL;
  165. item.itemToMonitor = rvi;
  166. item.monitoringMode = UA_MONITORINGMODE_REPORTING;
  167. UA_MonitoringParameters params;
  168. UA_MonitoringParameters_init(&params);
  169. item.requestedParameters = params;
  170. request.itemsToCreateSize = 1;
  171. request.itemsToCreate = &item;
  172. UA_CreateMonitoredItemsResponse response;
  173. UA_CreateMonitoredItemsResponse_init(&response);
  174. Service_CreateMonitoredItems(server, &adminSession, &request, &response);
  175. ck_assert_uint_eq(response.responseHeader.serviceResult, UA_STATUSCODE_GOOD);
  176. ck_assert_uint_eq(response.resultsSize, 1);
  177. ck_assert_uint_eq(response.results[0].statusCode, UA_STATUSCODE_GOOD);
  178. monitoredItemId = response.results[0].monitoredItemId;
  179. UA_MonitoredItemCreateRequest_deleteMembers(&item);
  180. UA_CreateMonitoredItemsResponse_deleteMembers(&response);
  181. }
  182. END_TEST
  183. START_TEST(Server_modifyMonitoredItems) {
  184. UA_ModifyMonitoredItemsRequest request;
  185. UA_ModifyMonitoredItemsRequest_init(&request);
  186. request.subscriptionId = subscriptionId;
  187. request.timestampsToReturn = UA_TIMESTAMPSTORETURN_SERVER;
  188. UA_MonitoredItemModifyRequest item;
  189. UA_MonitoredItemModifyRequest_init(&item);
  190. UA_ReadValueId rvi;
  191. UA_ReadValueId_init(&rvi);
  192. rvi.nodeId = UA_NODEID_NUMERIC(0, UA_NS0ID_SERVER);
  193. rvi.attributeId = UA_ATTRIBUTEID_DESCRIPTION;
  194. rvi.indexRange = UA_STRING_NULL;
  195. item.monitoredItemId = monitoredItemId;
  196. UA_MonitoringParameters params;
  197. UA_MonitoringParameters_init(&params);
  198. item.requestedParameters = params;
  199. request.itemsToModifySize = 1;
  200. request.itemsToModify = &item;
  201. UA_ModifyMonitoredItemsResponse response;
  202. UA_ModifyMonitoredItemsResponse_init(&response);
  203. Service_ModifyMonitoredItems(server, &adminSession, &request, &response);
  204. ck_assert_uint_eq(response.responseHeader.serviceResult, UA_STATUSCODE_GOOD);
  205. ck_assert_uint_eq(response.resultsSize, 1);
  206. ck_assert_uint_eq(response.results[0].statusCode, UA_STATUSCODE_GOOD);
  207. UA_MonitoredItemModifyRequest_deleteMembers(&item);
  208. UA_ModifyMonitoredItemsResponse_deleteMembers(&response);
  209. }
  210. END_TEST
  211. START_TEST(Server_setMonitoringMode) {
  212. UA_SetMonitoringModeRequest request;
  213. UA_SetMonitoringModeRequest_init(&request);
  214. request.subscriptionId = subscriptionId;
  215. request.monitoringMode = UA_MONITORINGMODE_DISABLED;
  216. request.monitoredItemIdsSize = 1;
  217. request.monitoredItemIds = &monitoredItemId;
  218. UA_SetMonitoringModeResponse response;
  219. UA_SetMonitoringModeResponse_init(&response);
  220. Service_SetMonitoringMode(server, &adminSession, &request, &response);
  221. ck_assert_uint_eq(response.responseHeader.serviceResult, UA_STATUSCODE_GOOD);
  222. ck_assert_uint_eq(response.resultsSize, 1);
  223. ck_assert_uint_eq(response.results[0], UA_STATUSCODE_GOOD);
  224. UA_SetMonitoringModeResponse_deleteMembers(&response);
  225. }
  226. END_TEST
  227. START_TEST(Server_deleteMonitoredItems) {
  228. UA_DeleteMonitoredItemsRequest request;
  229. UA_DeleteMonitoredItemsRequest_init(&request);
  230. request.subscriptionId = subscriptionId;
  231. request.monitoredItemIdsSize = 1;
  232. request.monitoredItemIds = &monitoredItemId;
  233. UA_DeleteMonitoredItemsResponse response;
  234. UA_DeleteMonitoredItemsResponse_init(&response);
  235. Service_DeleteMonitoredItems(server, &adminSession, &request, &response);
  236. ck_assert_uint_eq(response.responseHeader.serviceResult, UA_STATUSCODE_GOOD);
  237. ck_assert_uint_eq(response.resultsSize, 1);
  238. ck_assert_uint_eq(response.results[0], UA_STATUSCODE_GOOD);
  239. UA_DeleteMonitoredItemsResponse_deleteMembers(&response);
  240. }
  241. END_TEST
  242. static Suite* testSuite_Client(void) {
  243. Suite *s = suite_create("Server Subscription");
  244. TCase *tc_server = tcase_create("Server Subscription Basic");
  245. tcase_add_checked_fixture(tc_server, setup, teardown);
  246. tcase_add_test(tc_server, Server_createSubscription);
  247. tcase_add_test(tc_server, Server_modifySubscription);
  248. tcase_add_test(tc_server, Server_setPublishingMode);
  249. tcase_add_test(tc_server, Server_createMonitoredItems);
  250. tcase_add_test(tc_server, Server_modifyMonitoredItems);
  251. tcase_add_test(tc_server, Server_setMonitoringMode);
  252. tcase_add_test(tc_server, Server_deleteMonitoredItems);
  253. tcase_add_test(tc_server, Server_republish);
  254. tcase_add_test(tc_server, Server_deleteSubscription);
  255. tcase_add_test(tc_server, Server_republish_invalid);
  256. tcase_add_test(tc_server, Server_publishCallback);
  257. suite_add_tcase(s, tc_server);
  258. return s;
  259. }
  260. int main(void) {
  261. Suite *s = testSuite_Client();
  262. SRunner *sr = srunner_create(s);
  263. srunner_set_fork_status(sr, CK_NOFORK);
  264. srunner_run_all(sr,CK_NORMAL);
  265. int number_failed = srunner_ntests_failed(sr);
  266. srunner_free(sr);
  267. return (number_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
  268. }