check_services_subscriptions.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  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. /* Sleep until the publishing interval times out */
  127. usleep((useconds_t)(publishingInterval * 1000) + 1000);
  128. /* Keepalive is set to max initially */
  129. UA_Subscription *sub;
  130. LIST_FOREACH(sub, &adminSession.serverSubscriptions, listEntry)
  131. ck_assert_uint_eq(sub->currentKeepAliveCount, sub->maxKeepAliveCount);
  132. UA_Server_run_iterate(server, false);
  133. #ifdef UA_ENABLE_MULTITHREADING
  134. usleep((useconds_t)(publishingInterval * 1000) + 1000);
  135. UA_Server_run_iterate(server, false);
  136. usleep((useconds_t)(publishingInterval * 1000) + 1000);
  137. #endif
  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. static Suite* testSuite_Client(void) {
  246. Suite *s = suite_create("Server Subscription");
  247. TCase *tc_server = tcase_create("Server Subscription Basic");
  248. tcase_add_checked_fixture(tc_server, setup, teardown);
  249. tcase_add_test(tc_server, Server_createSubscription);
  250. tcase_add_test(tc_server, Server_modifySubscription);
  251. tcase_add_test(tc_server, Server_setPublishingMode);
  252. tcase_add_test(tc_server, Server_createMonitoredItems);
  253. tcase_add_test(tc_server, Server_modifyMonitoredItems);
  254. tcase_add_test(tc_server, Server_setMonitoringMode);
  255. tcase_add_test(tc_server, Server_deleteMonitoredItems);
  256. tcase_add_test(tc_server, Server_republish);
  257. tcase_add_test(tc_server, Server_deleteSubscription);
  258. tcase_add_test(tc_server, Server_republish_invalid);
  259. tcase_add_test(tc_server, Server_publishCallback);
  260. suite_add_tcase(s, tc_server);
  261. return s;
  262. }
  263. int main(void) {
  264. Suite *s = testSuite_Client();
  265. SRunner *sr = srunner_create(s);
  266. srunner_set_fork_status(sr, CK_NOFORK);
  267. srunner_run_all(sr,CK_NORMAL);
  268. int number_failed = srunner_ntests_failed(sr);
  269. srunner_free(sr);
  270. return (number_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
  271. }