check_services_subscriptions.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701
  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. /* TODO add event based testing */
  5. #include "ua_server.h"
  6. #include "server/ua_services.h"
  7. #include "server/ua_server_internal.h"
  8. #include "server/ua_subscription.h"
  9. #include "ua_config_default.h"
  10. #include "check.h"
  11. #include "testing_clock.h"
  12. static UA_Server *server = NULL;
  13. static UA_ServerConfig *config = NULL;
  14. static void setup(void) {
  15. config = UA_ServerConfig_new_default();
  16. server = UA_Server_new(config);
  17. UA_Server_run_startup(server);
  18. }
  19. static void teardown(void) {
  20. UA_Server_run_shutdown(server);
  21. UA_Server_delete(server);
  22. UA_ServerConfig_delete(config);
  23. }
  24. #ifdef UA_ENABLE_SUBSCRIPTIONS
  25. UA_UInt32 subscriptionId;
  26. UA_UInt32 monitoredItemId;
  27. START_TEST(Server_createSubscription) {
  28. /* Create a subscription */
  29. UA_CreateSubscriptionRequest request;
  30. UA_CreateSubscriptionRequest_init(&request);
  31. request.publishingEnabled = true;
  32. UA_CreateSubscriptionResponse response;
  33. UA_CreateSubscriptionResponse_init(&response);
  34. Service_CreateSubscription(server, &adminSession, &request, &response);
  35. ck_assert_uint_eq(response.responseHeader.serviceResult, UA_STATUSCODE_GOOD);
  36. subscriptionId = response.subscriptionId;
  37. UA_CreateSubscriptionResponse_deleteMembers(&response);
  38. }
  39. END_TEST
  40. START_TEST(Server_modifySubscription) {
  41. UA_ModifySubscriptionRequest request;
  42. UA_ModifySubscriptionRequest_init(&request);
  43. request.subscriptionId = subscriptionId;
  44. // just some arbitrary numbers to test. They have no specific reason
  45. request.requestedPublishingInterval = 100; // in ms
  46. request.requestedLifetimeCount = 1000;
  47. request.requestedMaxKeepAliveCount = 1000;
  48. request.maxNotificationsPerPublish = 1;
  49. request.priority = 10;
  50. UA_ModifySubscriptionResponse response;
  51. UA_ModifySubscriptionResponse_init(&response);
  52. Service_ModifySubscription(server, &adminSession, &request, &response);
  53. ck_assert_uint_eq(response.responseHeader.serviceResult, UA_STATUSCODE_GOOD);
  54. UA_ModifySubscriptionResponse_deleteMembers(&response);
  55. }
  56. END_TEST
  57. START_TEST(Server_setPublishingMode) {
  58. UA_SetPublishingModeRequest request;
  59. UA_SetPublishingModeRequest_init(&request);
  60. request.publishingEnabled = UA_TRUE;
  61. request.subscriptionIdsSize = 1;
  62. request.subscriptionIds = &subscriptionId;
  63. UA_SetPublishingModeResponse response;
  64. UA_SetPublishingModeResponse_init(&response);
  65. Service_SetPublishingMode(server, &adminSession, &request, &response);
  66. ck_assert_uint_eq(response.responseHeader.serviceResult, UA_STATUSCODE_GOOD);
  67. ck_assert_uint_eq(response.resultsSize, 1);
  68. ck_assert_uint_eq(response.results[0], UA_STATUSCODE_GOOD);
  69. UA_SetPublishingModeResponse_deleteMembers(&response);
  70. }
  71. END_TEST
  72. START_TEST(Server_republish) {
  73. UA_RepublishRequest request;
  74. UA_RepublishRequest_init(&request);
  75. request.subscriptionId = subscriptionId;
  76. request.retransmitSequenceNumber = 0;
  77. UA_RepublishResponse response;
  78. UA_RepublishResponse_init(&response);
  79. Service_Republish(server, &adminSession, &request, &response);
  80. ck_assert_uint_eq(response.responseHeader.serviceResult, UA_STATUSCODE_BADMESSAGENOTAVAILABLE);
  81. UA_RepublishResponse_deleteMembers(&response);
  82. }
  83. END_TEST
  84. START_TEST(Server_republish_invalid) {
  85. UA_RepublishRequest request;
  86. UA_RepublishRequest_init(&request);
  87. request.subscriptionId = subscriptionId;
  88. request.retransmitSequenceNumber = 0;
  89. UA_RepublishResponse response;
  90. UA_RepublishResponse_init(&response);
  91. Service_Republish(server, &adminSession, &request, &response);
  92. ck_assert_uint_eq(response.responseHeader.serviceResult, UA_STATUSCODE_BADSUBSCRIPTIONIDINVALID);
  93. UA_RepublishResponse_deleteMembers(&response);
  94. }
  95. END_TEST
  96. START_TEST(Server_deleteSubscription) {
  97. /* Remove the subscription */
  98. UA_DeleteSubscriptionsRequest del_request;
  99. UA_DeleteSubscriptionsRequest_init(&del_request);
  100. del_request.subscriptionIdsSize = 1;
  101. del_request.subscriptionIds = &subscriptionId;
  102. UA_DeleteSubscriptionsResponse del_response;
  103. UA_DeleteSubscriptionsResponse_init(&del_response);
  104. Service_DeleteSubscriptions(server, &adminSession, &del_request, &del_response);
  105. ck_assert_uint_eq(del_response.resultsSize, 1);
  106. ck_assert_uint_eq(del_response.results[0], UA_STATUSCODE_GOOD);
  107. UA_DeleteSubscriptionsResponse_deleteMembers(&del_response);
  108. }
  109. END_TEST
  110. START_TEST(Server_publishCallback) {
  111. /* Create a subscription */
  112. UA_CreateSubscriptionRequest request;
  113. UA_CreateSubscriptionResponse response;
  114. UA_CreateSubscriptionRequest_init(&request);
  115. request.publishingEnabled = true;
  116. UA_CreateSubscriptionResponse_init(&response);
  117. Service_CreateSubscription(server, &adminSession, &request, &response);
  118. ck_assert_uint_eq(response.responseHeader.serviceResult, UA_STATUSCODE_GOOD);
  119. UA_UInt32 subscriptionId1 = response.subscriptionId;
  120. UA_CreateSubscriptionResponse_deleteMembers(&response);
  121. /* Create a second subscription */
  122. UA_CreateSubscriptionRequest_init(&request);
  123. request.publishingEnabled = true;
  124. UA_CreateSubscriptionResponse_init(&response);
  125. Service_CreateSubscription(server, &adminSession, &request, &response);
  126. ck_assert_uint_eq(response.responseHeader.serviceResult, UA_STATUSCODE_GOOD);
  127. UA_UInt32 subscriptionId2 = response.subscriptionId;
  128. UA_Double publishingInterval = response.revisedPublishingInterval;
  129. ck_assert(publishingInterval > 0.0f);
  130. UA_CreateSubscriptionResponse_deleteMembers(&response);
  131. /* Keepalive is set to max initially */
  132. UA_Subscription *sub;
  133. LIST_FOREACH(sub, &adminSession.serverSubscriptions, listEntry)
  134. ck_assert_uint_eq(sub->currentKeepAliveCount, sub->maxKeepAliveCount);
  135. /* Sleep until the publishing interval times out */
  136. UA_fakeSleep((UA_UInt32)publishingInterval + 1);
  137. UA_Server_run_iterate(server, false);
  138. UA_realSleep(100);
  139. LIST_FOREACH(sub, &adminSession.serverSubscriptions, listEntry) {
  140. if ((sub->subscriptionId == subscriptionId1) || (sub->subscriptionId == subscriptionId2))
  141. ck_assert_uint_eq(sub->currentKeepAliveCount, sub->maxKeepAliveCount+1);
  142. }
  143. /* Remove the subscriptions */
  144. UA_DeleteSubscriptionsRequest del_request;
  145. UA_DeleteSubscriptionsRequest_init(&del_request);
  146. UA_UInt32 removeIds[2] = {subscriptionId1, subscriptionId2};
  147. del_request.subscriptionIdsSize = 2;
  148. del_request.subscriptionIds = removeIds;
  149. UA_DeleteSubscriptionsResponse del_response;
  150. UA_DeleteSubscriptionsResponse_init(&del_response);
  151. Service_DeleteSubscriptions(server, &adminSession, &del_request, &del_response);
  152. ck_assert_uint_eq(del_response.responseHeader.serviceResult, UA_STATUSCODE_GOOD);
  153. ck_assert_uint_eq(del_response.resultsSize, 2);
  154. ck_assert_uint_eq(del_response.results[0], UA_STATUSCODE_GOOD);
  155. ck_assert_uint_eq(del_response.results[1], UA_STATUSCODE_GOOD);
  156. UA_DeleteSubscriptionsResponse_deleteMembers(&del_response);
  157. }
  158. END_TEST
  159. START_TEST(Server_createMonitoredItems) {
  160. UA_CreateMonitoredItemsRequest request;
  161. UA_CreateMonitoredItemsRequest_init(&request);
  162. request.subscriptionId = subscriptionId;
  163. request.timestampsToReturn = UA_TIMESTAMPSTORETURN_SERVER;
  164. UA_MonitoredItemCreateRequest item;
  165. UA_MonitoredItemCreateRequest_init(&item);
  166. UA_ReadValueId rvi;
  167. UA_ReadValueId_init(&rvi);
  168. rvi.nodeId = UA_NODEID_NUMERIC(0, UA_NS0ID_SERVER);
  169. rvi.attributeId = UA_ATTRIBUTEID_BROWSENAME;
  170. rvi.indexRange = UA_STRING_NULL;
  171. item.itemToMonitor = rvi;
  172. item.monitoringMode = UA_MONITORINGMODE_REPORTING;
  173. UA_MonitoringParameters params;
  174. UA_MonitoringParameters_init(&params);
  175. item.requestedParameters = params;
  176. request.itemsToCreateSize = 1;
  177. request.itemsToCreate = &item;
  178. UA_CreateMonitoredItemsResponse response;
  179. UA_CreateMonitoredItemsResponse_init(&response);
  180. Service_CreateMonitoredItems(server, &adminSession, &request, &response);
  181. ck_assert_uint_eq(response.responseHeader.serviceResult, UA_STATUSCODE_GOOD);
  182. ck_assert_uint_eq(response.resultsSize, 1);
  183. ck_assert_uint_eq(response.results[0].statusCode, UA_STATUSCODE_GOOD);
  184. monitoredItemId = response.results[0].monitoredItemId;
  185. ck_assert_uint_gt(monitoredItemId, 0);
  186. UA_MonitoredItemCreateRequest_deleteMembers(&item);
  187. UA_CreateMonitoredItemsResponse_deleteMembers(&response);
  188. }
  189. END_TEST
  190. START_TEST(Server_modifyMonitoredItems) {
  191. UA_ModifyMonitoredItemsRequest request;
  192. UA_ModifyMonitoredItemsRequest_init(&request);
  193. request.subscriptionId = subscriptionId;
  194. request.timestampsToReturn = UA_TIMESTAMPSTORETURN_SERVER;
  195. UA_MonitoredItemModifyRequest item;
  196. UA_MonitoredItemModifyRequest_init(&item);
  197. UA_ReadValueId rvi;
  198. UA_ReadValueId_init(&rvi);
  199. rvi.nodeId = UA_NODEID_NUMERIC(0, UA_NS0ID_SERVER);
  200. rvi.attributeId = UA_ATTRIBUTEID_DESCRIPTION;
  201. rvi.indexRange = UA_STRING_NULL;
  202. item.monitoredItemId = monitoredItemId;
  203. UA_MonitoringParameters params;
  204. UA_MonitoringParameters_init(&params);
  205. item.requestedParameters = params;
  206. request.itemsToModifySize = 1;
  207. request.itemsToModify = &item;
  208. UA_ModifyMonitoredItemsResponse response;
  209. UA_ModifyMonitoredItemsResponse_init(&response);
  210. Service_ModifyMonitoredItems(server, &adminSession, &request, &response);
  211. ck_assert_uint_eq(response.responseHeader.serviceResult, UA_STATUSCODE_GOOD);
  212. ck_assert_uint_eq(response.resultsSize, 1);
  213. ck_assert_uint_eq(response.results[0].statusCode, UA_STATUSCODE_GOOD);
  214. UA_MonitoredItemModifyRequest_deleteMembers(&item);
  215. UA_ModifyMonitoredItemsResponse_deleteMembers(&response);
  216. }
  217. END_TEST
  218. START_TEST(Server_overflow) {
  219. /* Create a subscription */
  220. UA_CreateSubscriptionRequest createSubscriptionRequest;
  221. UA_CreateSubscriptionResponse createSubscriptionResponse;
  222. UA_CreateSubscriptionRequest_init(&createSubscriptionRequest);
  223. createSubscriptionRequest.publishingEnabled = true;
  224. UA_CreateSubscriptionResponse_init(&createSubscriptionResponse);
  225. Service_CreateSubscription(server, &adminSession, &createSubscriptionRequest, &createSubscriptionResponse);
  226. ck_assert_uint_eq(createSubscriptionResponse.responseHeader.serviceResult, UA_STATUSCODE_GOOD);
  227. UA_UInt32 localSubscriptionId = createSubscriptionResponse.subscriptionId;
  228. UA_Double publishingInterval = createSubscriptionResponse.revisedPublishingInterval;
  229. ck_assert(publishingInterval > 0.0f);
  230. UA_CreateSubscriptionResponse_deleteMembers(&createSubscriptionResponse);
  231. /* Create a monitoredItem */
  232. UA_CreateMonitoredItemsRequest createMonitoredItemsRequest;
  233. UA_CreateMonitoredItemsRequest_init(&createMonitoredItemsRequest);
  234. createMonitoredItemsRequest.subscriptionId = localSubscriptionId;
  235. createMonitoredItemsRequest.timestampsToReturn = UA_TIMESTAMPSTORETURN_SERVER;
  236. UA_MonitoredItemCreateRequest item;
  237. UA_MonitoredItemCreateRequest_init(&item);
  238. UA_ReadValueId rvi;
  239. UA_ReadValueId_init(&rvi);
  240. rvi.nodeId = UA_NODEID_NUMERIC(0, UA_NS0ID_SERVER_SERVERSTATUS_CURRENTTIME);
  241. rvi.attributeId = UA_ATTRIBUTEID_BROWSENAME;
  242. rvi.indexRange = UA_STRING_NULL;
  243. item.itemToMonitor = rvi;
  244. item.monitoringMode = UA_MONITORINGMODE_REPORTING;
  245. UA_MonitoringParameters params;
  246. UA_MonitoringParameters_init(&params);
  247. item.requestedParameters = params;
  248. item.requestedParameters.queueSize = 3;
  249. item.requestedParameters.discardOldest = true;
  250. createMonitoredItemsRequest.itemsToCreateSize = 1;
  251. createMonitoredItemsRequest.itemsToCreate = &item;
  252. UA_CreateMonitoredItemsResponse createMonitoredItemsResponse;
  253. UA_CreateMonitoredItemsResponse_init(&createMonitoredItemsResponse);
  254. Service_CreateMonitoredItems(server, &adminSession, &createMonitoredItemsRequest, &createMonitoredItemsResponse);
  255. ck_assert_uint_eq(createMonitoredItemsResponse.responseHeader.serviceResult, UA_STATUSCODE_GOOD);
  256. ck_assert_uint_eq(createMonitoredItemsResponse.resultsSize, 1);
  257. ck_assert_uint_eq(createMonitoredItemsResponse.results[0].statusCode, UA_STATUSCODE_GOOD);
  258. UA_UInt32 localMonitoredItemId = createMonitoredItemsResponse.results[0].monitoredItemId;
  259. ck_assert_uint_gt(localMonitoredItemId, 0);
  260. UA_MonitoredItemCreateRequest_deleteMembers(&item);
  261. UA_CreateMonitoredItemsResponse_deleteMembers(&createMonitoredItemsResponse);
  262. UA_MonitoredItem *mon = NULL;
  263. UA_Subscription *sub;
  264. LIST_FOREACH(sub, &adminSession.serverSubscriptions, listEntry) {
  265. if(sub->subscriptionId == localSubscriptionId)
  266. mon = UA_Subscription_getMonitoredItem(sub, localMonitoredItemId);
  267. }
  268. ck_assert_ptr_ne(mon, NULL);
  269. UA_assert(mon);
  270. ck_assert_uint_eq(mon->queueSize, 1);
  271. ck_assert_uint_eq(mon->maxQueueSize, 3);
  272. UA_Notification *notification;
  273. notification = TAILQ_LAST(&mon->queue, NotificationQueue);
  274. ck_assert_uint_eq(notification->data.value.hasStatus, false);
  275. UA_ByteString_deleteMembers(&mon->lastSampledValue);
  276. UA_MonitoredItem_SampleCallback(server, mon);
  277. ck_assert_uint_eq(mon->queueSize, 2);
  278. ck_assert_uint_eq(mon->maxQueueSize, 3);
  279. notification = TAILQ_LAST(&mon->queue, NotificationQueue);
  280. ck_assert_uint_eq(notification->data.value.hasStatus, false);
  281. UA_ByteString_deleteMembers(&mon->lastSampledValue);
  282. UA_MonitoredItem_SampleCallback(server, mon);
  283. ck_assert_uint_eq(mon->queueSize, 3);
  284. ck_assert_uint_eq(mon->maxQueueSize, 3);
  285. notification = TAILQ_LAST(&mon->queue, NotificationQueue);
  286. ck_assert_uint_eq(notification->data.value.hasStatus, false);
  287. UA_ByteString_deleteMembers(&mon->lastSampledValue);
  288. UA_MonitoredItem_SampleCallback(server, mon);
  289. ck_assert_uint_eq(mon->queueSize, 3);
  290. ck_assert_uint_eq(mon->maxQueueSize, 3);
  291. notification = TAILQ_FIRST(&mon->queue);
  292. ck_assert_uint_eq(notification->data.value.hasStatus, true);
  293. ck_assert_uint_eq(notification->data.value.status,
  294. UA_STATUSCODE_INFOTYPE_DATAVALUE | UA_STATUSCODE_INFOBITS_OVERFLOW);
  295. /* Remove status for next test */
  296. notification->data.value.hasStatus = false;
  297. notification->data.value.status = 0;
  298. /* Modify the MonitoredItem */
  299. UA_ModifyMonitoredItemsRequest modifyMonitoredItemsRequest;
  300. UA_ModifyMonitoredItemsRequest_init(&modifyMonitoredItemsRequest);
  301. modifyMonitoredItemsRequest.subscriptionId = localSubscriptionId;
  302. modifyMonitoredItemsRequest.timestampsToReturn = UA_TIMESTAMPSTORETURN_SERVER;
  303. UA_MonitoredItemModifyRequest itemToModify;
  304. UA_MonitoredItemModifyRequest_init(&itemToModify);
  305. itemToModify.monitoredItemId = localMonitoredItemId;
  306. UA_MonitoringParameters_init(&params);
  307. itemToModify.requestedParameters = params;
  308. itemToModify.requestedParameters.queueSize = 2;
  309. itemToModify.requestedParameters.discardOldest = true;
  310. modifyMonitoredItemsRequest.itemsToModifySize = 1;
  311. modifyMonitoredItemsRequest.itemsToModify = &itemToModify;
  312. UA_ModifyMonitoredItemsResponse modifyMonitoredItemsResponse;
  313. UA_ModifyMonitoredItemsResponse_init(&modifyMonitoredItemsResponse);
  314. Service_ModifyMonitoredItems(server, &adminSession, &modifyMonitoredItemsRequest,
  315. &modifyMonitoredItemsResponse);
  316. ck_assert_uint_eq(modifyMonitoredItemsResponse.responseHeader.serviceResult, UA_STATUSCODE_GOOD);
  317. ck_assert_uint_eq(modifyMonitoredItemsResponse.resultsSize, 1);
  318. ck_assert_uint_eq(modifyMonitoredItemsResponse.results[0].statusCode, UA_STATUSCODE_GOOD);
  319. UA_MonitoredItemModifyRequest_deleteMembers(&itemToModify);
  320. UA_ModifyMonitoredItemsResponse_deleteMembers(&modifyMonitoredItemsResponse);
  321. ck_assert_uint_eq(mon->queueSize, 2);
  322. ck_assert_uint_eq(mon->maxQueueSize, 2);
  323. notification = TAILQ_FIRST(&mon->queue);
  324. ck_assert_uint_eq(notification->data.value.hasStatus, true);
  325. ck_assert_uint_eq(notification->data.value.status,
  326. UA_STATUSCODE_INFOTYPE_DATAVALUE | UA_STATUSCODE_INFOBITS_OVERFLOW);
  327. /* Modify the MonitoredItem */
  328. UA_ModifyMonitoredItemsRequest_init(&modifyMonitoredItemsRequest);
  329. modifyMonitoredItemsRequest.subscriptionId = localSubscriptionId;
  330. modifyMonitoredItemsRequest.timestampsToReturn = UA_TIMESTAMPSTORETURN_SERVER;
  331. UA_MonitoredItemModifyRequest_init(&itemToModify);
  332. itemToModify.monitoredItemId = localMonitoredItemId;
  333. UA_MonitoringParameters_init(&params);
  334. itemToModify.requestedParameters = params;
  335. itemToModify.requestedParameters.queueSize = 1;
  336. modifyMonitoredItemsRequest.itemsToModifySize = 1;
  337. modifyMonitoredItemsRequest.itemsToModify = &itemToModify;
  338. UA_ModifyMonitoredItemsResponse_init(&modifyMonitoredItemsResponse);
  339. Service_ModifyMonitoredItems(server, &adminSession, &modifyMonitoredItemsRequest,
  340. &modifyMonitoredItemsResponse);
  341. ck_assert_uint_eq(modifyMonitoredItemsResponse.responseHeader.serviceResult, UA_STATUSCODE_GOOD);
  342. ck_assert_uint_eq(modifyMonitoredItemsResponse.resultsSize, 1);
  343. ck_assert_uint_eq(modifyMonitoredItemsResponse.results[0].statusCode, UA_STATUSCODE_GOOD);
  344. UA_MonitoredItemModifyRequest_deleteMembers(&itemToModify);
  345. UA_ModifyMonitoredItemsResponse_deleteMembers(&modifyMonitoredItemsResponse);
  346. ck_assert_uint_eq(mon->queueSize, 1);
  347. ck_assert_uint_eq(mon->maxQueueSize, 1);
  348. notification = TAILQ_LAST(&mon->queue, NotificationQueue);
  349. ck_assert_uint_eq(notification->data.value.hasStatus, false);
  350. /* Modify the MonitoredItem */
  351. UA_ModifyMonitoredItemsRequest_init(&modifyMonitoredItemsRequest);
  352. modifyMonitoredItemsRequest.subscriptionId = localSubscriptionId;
  353. modifyMonitoredItemsRequest.timestampsToReturn = UA_TIMESTAMPSTORETURN_SERVER;
  354. UA_MonitoredItemModifyRequest_init(&itemToModify);
  355. itemToModify.monitoredItemId = localMonitoredItemId;
  356. UA_MonitoringParameters_init(&params);
  357. itemToModify.requestedParameters = params;
  358. itemToModify.requestedParameters.discardOldest = false;
  359. itemToModify.requestedParameters.queueSize = 1;
  360. modifyMonitoredItemsRequest.itemsToModifySize = 1;
  361. modifyMonitoredItemsRequest.itemsToModify = &itemToModify;
  362. UA_ModifyMonitoredItemsResponse_init(&modifyMonitoredItemsResponse);
  363. Service_ModifyMonitoredItems(server, &adminSession, &modifyMonitoredItemsRequest,
  364. &modifyMonitoredItemsResponse);
  365. ck_assert_uint_eq(modifyMonitoredItemsResponse.responseHeader.serviceResult, UA_STATUSCODE_GOOD);
  366. ck_assert_uint_eq(modifyMonitoredItemsResponse.resultsSize, 1);
  367. ck_assert_uint_eq(modifyMonitoredItemsResponse.results[0].statusCode, UA_STATUSCODE_GOOD);
  368. UA_MonitoredItemModifyRequest_deleteMembers(&itemToModify);
  369. UA_ModifyMonitoredItemsResponse_deleteMembers(&modifyMonitoredItemsResponse);
  370. UA_MonitoredItem_SampleCallback(server, mon);
  371. ck_assert_uint_eq(mon->queueSize, 1);
  372. ck_assert_uint_eq(mon->maxQueueSize, 1);
  373. notification = TAILQ_FIRST(&mon->queue);
  374. ck_assert_uint_eq(notification->data.value.hasStatus, false); /* the infobit is only set if the queue is larger than one */
  375. /* Remove the subscriptions */
  376. UA_DeleteSubscriptionsRequest deleteSubscriptionsRequest;
  377. UA_DeleteSubscriptionsRequest_init(&deleteSubscriptionsRequest);
  378. UA_UInt32 removeId = localSubscriptionId;
  379. deleteSubscriptionsRequest.subscriptionIdsSize = 1;
  380. deleteSubscriptionsRequest.subscriptionIds = &removeId;
  381. UA_DeleteSubscriptionsResponse deleteSubscriptionsResponse;
  382. UA_DeleteSubscriptionsResponse_init(&deleteSubscriptionsResponse);
  383. Service_DeleteSubscriptions(server, &adminSession, &deleteSubscriptionsRequest,
  384. &deleteSubscriptionsResponse);
  385. ck_assert_uint_eq(deleteSubscriptionsResponse.responseHeader.serviceResult, UA_STATUSCODE_GOOD);
  386. ck_assert_uint_eq(deleteSubscriptionsResponse.resultsSize, 1);
  387. ck_assert_uint_eq(deleteSubscriptionsResponse.results[0], UA_STATUSCODE_GOOD);
  388. UA_DeleteSubscriptionsResponse_deleteMembers(&deleteSubscriptionsResponse);
  389. }
  390. END_TEST
  391. START_TEST(Server_setMonitoringMode) {
  392. UA_SetMonitoringModeRequest request;
  393. UA_SetMonitoringModeRequest_init(&request);
  394. request.subscriptionId = subscriptionId;
  395. request.monitoringMode = UA_MONITORINGMODE_DISABLED;
  396. request.monitoredItemIdsSize = 1;
  397. request.monitoredItemIds = &monitoredItemId;
  398. UA_SetMonitoringModeResponse response;
  399. UA_SetMonitoringModeResponse_init(&response);
  400. Service_SetMonitoringMode(server, &adminSession, &request, &response);
  401. ck_assert_uint_eq(response.responseHeader.serviceResult, UA_STATUSCODE_GOOD);
  402. ck_assert_uint_eq(response.resultsSize, 1);
  403. ck_assert_uint_eq(response.results[0], UA_STATUSCODE_GOOD);
  404. UA_SetMonitoringModeResponse_deleteMembers(&response);
  405. }
  406. END_TEST
  407. START_TEST(Server_deleteMonitoredItems) {
  408. UA_DeleteMonitoredItemsRequest request;
  409. UA_DeleteMonitoredItemsRequest_init(&request);
  410. request.subscriptionId = subscriptionId;
  411. request.monitoredItemIdsSize = 1;
  412. request.monitoredItemIds = &monitoredItemId;
  413. UA_DeleteMonitoredItemsResponse response;
  414. UA_DeleteMonitoredItemsResponse_init(&response);
  415. Service_DeleteMonitoredItems(server, &adminSession, &request, &response);
  416. ck_assert_uint_eq(response.responseHeader.serviceResult, UA_STATUSCODE_GOOD);
  417. ck_assert_uint_eq(response.resultsSize, 1);
  418. ck_assert_uint_eq(response.results[0], UA_STATUSCODE_GOOD);
  419. UA_DeleteMonitoredItemsResponse_deleteMembers(&response);
  420. }
  421. END_TEST
  422. START_TEST(Server_lifeTimeCount) {
  423. /* Create a subscription */
  424. UA_CreateSubscriptionRequest request;
  425. UA_CreateSubscriptionResponse response;
  426. UA_CreateSubscriptionRequest_init(&request);
  427. request.publishingEnabled = true;
  428. request.requestedLifetimeCount = 3;
  429. request.requestedMaxKeepAliveCount = 1;
  430. UA_CreateSubscriptionResponse_init(&response);
  431. Service_CreateSubscription(server, &adminSession, &request, &response);
  432. ck_assert_uint_eq(response.responseHeader.serviceResult, UA_STATUSCODE_GOOD);
  433. ck_assert_uint_eq(response.revisedMaxKeepAliveCount, 1);
  434. ck_assert_uint_eq(response.revisedLifetimeCount, 3);
  435. UA_CreateSubscriptionResponse_deleteMembers(&response);
  436. /* Create a second subscription */
  437. UA_CreateSubscriptionRequest_init(&request);
  438. request.publishingEnabled = true;
  439. request.requestedLifetimeCount = 4;
  440. request.requestedMaxKeepAliveCount = 2;
  441. UA_CreateSubscriptionResponse_init(&response);
  442. Service_CreateSubscription(server, &adminSession, &request, &response);
  443. ck_assert_uint_eq(response.responseHeader.serviceResult, UA_STATUSCODE_GOOD);
  444. ck_assert_uint_eq(response.revisedMaxKeepAliveCount, 2);
  445. /* revisedLifetimeCount is revised to 3*MaxKeepAliveCount == 3 */
  446. ck_assert_uint_eq(response.revisedLifetimeCount, 6);
  447. UA_Double publishingInterval = response.revisedPublishingInterval;
  448. ck_assert(publishingInterval > 0.0f);
  449. subscriptionId = response.subscriptionId;
  450. UA_CreateSubscriptionResponse_deleteMembers(&response);
  451. /* Add a MonitoredItem to the second subscription */
  452. UA_CreateMonitoredItemsRequest mrequest;
  453. UA_CreateMonitoredItemsRequest_init(&mrequest);
  454. mrequest.subscriptionId = subscriptionId;
  455. mrequest.timestampsToReturn = UA_TIMESTAMPSTORETURN_SERVER;
  456. UA_MonitoredItemCreateRequest item;
  457. UA_MonitoredItemCreateRequest_init(&item);
  458. UA_ReadValueId rvi;
  459. UA_ReadValueId_init(&rvi);
  460. rvi.nodeId = UA_NODEID_NUMERIC(0, UA_NS0ID_SERVER);
  461. rvi.attributeId = UA_ATTRIBUTEID_BROWSENAME;
  462. rvi.indexRange = UA_STRING_NULL;
  463. item.itemToMonitor = rvi;
  464. item.requestedParameters.samplingInterval = publishingInterval / 5.0;
  465. item.monitoringMode = UA_MONITORINGMODE_REPORTING;
  466. UA_MonitoringParameters params;
  467. UA_MonitoringParameters_init(&params);
  468. item.requestedParameters = params;
  469. mrequest.itemsToCreateSize = 1;
  470. mrequest.itemsToCreate = &item;
  471. UA_CreateMonitoredItemsResponse mresponse;
  472. UA_CreateMonitoredItemsResponse_init(&mresponse);
  473. Service_CreateMonitoredItems(server, &adminSession, &mrequest, &mresponse);
  474. ck_assert_uint_eq(mresponse.responseHeader.serviceResult, UA_STATUSCODE_GOOD);
  475. ck_assert_uint_eq(mresponse.resultsSize, 1);
  476. ck_assert_uint_eq(mresponse.results[0].statusCode, UA_STATUSCODE_GOOD);
  477. monitoredItemId = mresponse.results[0].monitoredItemId;
  478. ck_assert_uint_gt(monitoredItemId, 0);
  479. UA_MonitoredItemCreateRequest_deleteMembers(&item);
  480. UA_CreateMonitoredItemsResponse_deleteMembers(&mresponse);
  481. UA_Server_run_iterate(server, false);
  482. UA_UInt32 count = 0;
  483. UA_Subscription *sub;
  484. LIST_FOREACH(sub, &adminSession.serverSubscriptions, listEntry) {
  485. ck_assert_uint_eq(sub->currentLifetimeCount, 0);
  486. count++;
  487. }
  488. ck_assert_uint_eq(count, 2);
  489. UA_fakeSleep((UA_UInt32)publishingInterval + 1);
  490. UA_Server_run_iterate(server, false);
  491. count = 0;
  492. LIST_FOREACH(sub, &adminSession.serverSubscriptions, listEntry) {
  493. ck_assert_uint_eq(sub->currentLifetimeCount, 1);
  494. count++;
  495. }
  496. ck_assert_uint_eq(count, 2);
  497. /* Sleep until the publishing interval times out */
  498. UA_fakeSleep((UA_UInt32)publishingInterval + 1);
  499. UA_Server_run_iterate(server, false);
  500. count = 0;
  501. LIST_FOREACH(sub, &adminSession.serverSubscriptions, listEntry) {
  502. ck_assert_uint_eq(sub->currentLifetimeCount, 2);
  503. count++;
  504. }
  505. ck_assert_uint_eq(count, 2);
  506. /* Sleep until the publishing interval times out */
  507. UA_fakeSleep((UA_UInt32)publishingInterval + 1);
  508. UA_Server_run_iterate(server, false);
  509. count = 0;
  510. LIST_FOREACH(sub, &adminSession.serverSubscriptions, listEntry) {
  511. ck_assert_uint_eq(sub->currentLifetimeCount, 3);
  512. count++;
  513. }
  514. ck_assert_uint_eq(count, 2);
  515. /* Sleep until the publishing interval times out */
  516. UA_fakeSleep((UA_UInt32)publishingInterval + 1);
  517. UA_Server_run_iterate(server, false);
  518. count = 0;
  519. LIST_FOREACH(sub, &adminSession.serverSubscriptions, listEntry) {
  520. ck_assert_uint_eq(sub->currentLifetimeCount, 4);
  521. count++;
  522. }
  523. ck_assert_uint_eq(count, 1);
  524. /* Sleep until the publishing interval times out */
  525. UA_fakeSleep((UA_UInt32)publishingInterval + 1);
  526. UA_Server_run_iterate(server, false);
  527. count = 0;
  528. LIST_FOREACH(sub, &adminSession.serverSubscriptions, listEntry) {
  529. ck_assert_uint_eq(sub->currentLifetimeCount, 5);
  530. count++;
  531. }
  532. ck_assert_uint_eq(count, 1);
  533. /* Sleep until the publishing interval times out */
  534. UA_fakeSleep((UA_UInt32)publishingInterval + 1);
  535. UA_Server_run_iterate(server, false);
  536. count = 0;
  537. LIST_FOREACH(sub, &adminSession.serverSubscriptions, listEntry) {
  538. ck_assert_uint_eq(sub->currentLifetimeCount, 6);
  539. count++;
  540. }
  541. ck_assert_uint_eq(count, 1);
  542. /* Sleep until the publishing interval times out. The next iteration removes
  543. * the subscription. */
  544. UA_fakeSleep((UA_UInt32)publishingInterval + 1);
  545. UA_Server_run_iterate(server, false);
  546. count = 0;
  547. LIST_FOREACH(sub, &adminSession.serverSubscriptions, listEntry) {
  548. count++;
  549. }
  550. ck_assert_uint_eq(count, 0);
  551. }
  552. END_TEST
  553. #endif /* UA_ENABLE_SUBSCRIPTIONS */
  554. static Suite* testSuite_Client(void) {
  555. Suite *s = suite_create("Server Subscription");
  556. TCase *tc_server = tcase_create("Server Subscription Basic");
  557. tcase_add_checked_fixture(tc_server, setup, teardown);
  558. #ifdef UA_ENABLE_SUBSCRIPTIONS
  559. tcase_add_test(tc_server, Server_createSubscription);
  560. tcase_add_test(tc_server, Server_modifySubscription);
  561. tcase_add_test(tc_server, Server_setPublishingMode);
  562. tcase_add_test(tc_server, Server_createMonitoredItems);
  563. tcase_add_test(tc_server, Server_modifyMonitoredItems);
  564. tcase_add_test(tc_server, Server_overflow);
  565. tcase_add_test(tc_server, Server_setMonitoringMode);
  566. tcase_add_test(tc_server, Server_deleteMonitoredItems);
  567. tcase_add_test(tc_server, Server_republish);
  568. tcase_add_test(tc_server, Server_deleteSubscription);
  569. tcase_add_test(tc_server, Server_republish_invalid);
  570. tcase_add_test(tc_server, Server_publishCallback);
  571. tcase_add_test(tc_server, Server_lifeTimeCount);
  572. #endif /* UA_ENABLE_SUBSCRIPTIONS */
  573. suite_add_tcase(s, tc_server);
  574. return s;
  575. }
  576. int main(void) {
  577. Suite *s = testSuite_Client();
  578. SRunner *sr = srunner_create(s);
  579. srunner_set_fork_status(sr, CK_NOFORK);
  580. srunner_run_all(sr,CK_NORMAL);
  581. int number_failed = srunner_ntests_failed(sr);
  582. srunner_free(sr);
  583. return (number_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
  584. }