check_services_subscriptions.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697
  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->currentQueueSize, 1);
  271. ck_assert_uint_eq(mon->maxQueueSize, 3);
  272. MonitoredItem_queuedValue *queueItem;
  273. queueItem = TAILQ_LAST(&mon->queue, QueuedValueQueue);
  274. ck_assert_uint_eq(queueItem->data.value.hasStatus, false);
  275. UA_ByteString_deleteMembers(&mon->lastSampledValue);
  276. UA_MonitoredItem_SampleCallback(server, mon);
  277. ck_assert_uint_eq(mon->currentQueueSize, 2);
  278. ck_assert_uint_eq(mon->maxQueueSize, 3);
  279. queueItem = TAILQ_LAST(&mon->queue, QueuedValueQueue);
  280. ck_assert_uint_eq(queueItem->data.value.hasStatus, false);
  281. UA_ByteString_deleteMembers(&mon->lastSampledValue);
  282. UA_MonitoredItem_SampleCallback(server, mon);
  283. ck_assert_uint_eq(mon->currentQueueSize, 3);
  284. ck_assert_uint_eq(mon->maxQueueSize, 3);
  285. queueItem = TAILQ_LAST(&mon->queue, QueuedValueQueue);
  286. ck_assert_uint_eq(queueItem->data.value.hasStatus, false);
  287. UA_ByteString_deleteMembers(&mon->lastSampledValue);
  288. UA_MonitoredItem_SampleCallback(server, mon);
  289. ck_assert_uint_eq(mon->currentQueueSize, 3);
  290. ck_assert_uint_eq(mon->maxQueueSize, 3);
  291. queueItem = TAILQ_FIRST(&mon->queue);
  292. ck_assert_uint_eq(queueItem->data.value.hasStatus, true);
  293. ck_assert_uint_eq(queueItem->data.value.status, UA_STATUSCODE_INFOTYPE_DATAVALUE | UA_STATUSCODE_INFOBITS_OVERFLOW);
  294. /* Remove status for next test */
  295. queueItem->data.value.hasStatus = false;
  296. queueItem->data.value.status = 0;
  297. /* Modify the MonitoredItem */
  298. UA_ModifyMonitoredItemsRequest modifyMonitoredItemsRequest;
  299. UA_ModifyMonitoredItemsRequest_init(&modifyMonitoredItemsRequest);
  300. modifyMonitoredItemsRequest.subscriptionId = localSubscriptionId;
  301. modifyMonitoredItemsRequest.timestampsToReturn = UA_TIMESTAMPSTORETURN_SERVER;
  302. UA_MonitoredItemModifyRequest itemToModify;
  303. UA_MonitoredItemModifyRequest_init(&itemToModify);
  304. itemToModify.monitoredItemId = localMonitoredItemId;
  305. UA_MonitoringParameters_init(&params);
  306. itemToModify.requestedParameters = params;
  307. itemToModify.requestedParameters.queueSize = 2;
  308. itemToModify.requestedParameters.discardOldest = true;
  309. modifyMonitoredItemsRequest.itemsToModifySize = 1;
  310. modifyMonitoredItemsRequest.itemsToModify = &itemToModify;
  311. UA_ModifyMonitoredItemsResponse modifyMonitoredItemsResponse;
  312. UA_ModifyMonitoredItemsResponse_init(&modifyMonitoredItemsResponse);
  313. Service_ModifyMonitoredItems(server, &adminSession, &modifyMonitoredItemsRequest, &modifyMonitoredItemsResponse);
  314. ck_assert_uint_eq(modifyMonitoredItemsResponse.responseHeader.serviceResult, UA_STATUSCODE_GOOD);
  315. ck_assert_uint_eq(modifyMonitoredItemsResponse.resultsSize, 1);
  316. ck_assert_uint_eq(modifyMonitoredItemsResponse.results[0].statusCode, UA_STATUSCODE_GOOD);
  317. UA_MonitoredItemModifyRequest_deleteMembers(&itemToModify);
  318. UA_ModifyMonitoredItemsResponse_deleteMembers(&modifyMonitoredItemsResponse);
  319. ck_assert_uint_eq(mon->currentQueueSize, 2);
  320. ck_assert_uint_eq(mon->maxQueueSize, 2);
  321. queueItem = TAILQ_FIRST(&mon->queue);
  322. ck_assert_uint_eq(queueItem->data.value.hasStatus, true);
  323. ck_assert_uint_eq(queueItem->data.value.status, UA_STATUSCODE_INFOTYPE_DATAVALUE | UA_STATUSCODE_INFOBITS_OVERFLOW);
  324. /* Modify the MonitoredItem */
  325. UA_ModifyMonitoredItemsRequest_init(&modifyMonitoredItemsRequest);
  326. modifyMonitoredItemsRequest.subscriptionId = localSubscriptionId;
  327. modifyMonitoredItemsRequest.timestampsToReturn = UA_TIMESTAMPSTORETURN_SERVER;
  328. UA_MonitoredItemModifyRequest_init(&itemToModify);
  329. itemToModify.monitoredItemId = localMonitoredItemId;
  330. UA_MonitoringParameters_init(&params);
  331. itemToModify.requestedParameters = params;
  332. itemToModify.requestedParameters.queueSize = 1;
  333. modifyMonitoredItemsRequest.itemsToModifySize = 1;
  334. modifyMonitoredItemsRequest.itemsToModify = &itemToModify;
  335. UA_ModifyMonitoredItemsResponse_init(&modifyMonitoredItemsResponse);
  336. Service_ModifyMonitoredItems(server, &adminSession, &modifyMonitoredItemsRequest, &modifyMonitoredItemsResponse);
  337. ck_assert_uint_eq(modifyMonitoredItemsResponse.responseHeader.serviceResult, UA_STATUSCODE_GOOD);
  338. ck_assert_uint_eq(modifyMonitoredItemsResponse.resultsSize, 1);
  339. ck_assert_uint_eq(modifyMonitoredItemsResponse.results[0].statusCode, UA_STATUSCODE_GOOD);
  340. UA_MonitoredItemModifyRequest_deleteMembers(&itemToModify);
  341. UA_ModifyMonitoredItemsResponse_deleteMembers(&modifyMonitoredItemsResponse);
  342. ck_assert_uint_eq(mon->currentQueueSize, 1);
  343. ck_assert_uint_eq(mon->maxQueueSize, 1);
  344. queueItem = TAILQ_LAST(&mon->queue, QueuedValueQueue);
  345. ck_assert_uint_eq(queueItem->data.value.hasStatus, false);
  346. /* Modify the MonitoredItem */
  347. UA_ModifyMonitoredItemsRequest_init(&modifyMonitoredItemsRequest);
  348. modifyMonitoredItemsRequest.subscriptionId = localSubscriptionId;
  349. modifyMonitoredItemsRequest.timestampsToReturn = UA_TIMESTAMPSTORETURN_SERVER;
  350. UA_MonitoredItemModifyRequest_init(&itemToModify);
  351. itemToModify.monitoredItemId = localMonitoredItemId;
  352. UA_MonitoringParameters_init(&params);
  353. itemToModify.requestedParameters = params;
  354. itemToModify.requestedParameters.discardOldest = false;
  355. itemToModify.requestedParameters.queueSize = 1;
  356. modifyMonitoredItemsRequest.itemsToModifySize = 1;
  357. modifyMonitoredItemsRequest.itemsToModify = &itemToModify;
  358. UA_ModifyMonitoredItemsResponse_init(&modifyMonitoredItemsResponse);
  359. Service_ModifyMonitoredItems(server, &adminSession, &modifyMonitoredItemsRequest,
  360. &modifyMonitoredItemsResponse);
  361. ck_assert_uint_eq(modifyMonitoredItemsResponse.responseHeader.serviceResult, UA_STATUSCODE_GOOD);
  362. ck_assert_uint_eq(modifyMonitoredItemsResponse.resultsSize, 1);
  363. ck_assert_uint_eq(modifyMonitoredItemsResponse.results[0].statusCode, UA_STATUSCODE_GOOD);
  364. UA_MonitoredItemModifyRequest_deleteMembers(&itemToModify);
  365. UA_ModifyMonitoredItemsResponse_deleteMembers(&modifyMonitoredItemsResponse);
  366. UA_MonitoredItem_SampleCallback(server, mon);
  367. ck_assert_uint_eq(mon->currentQueueSize, 1);
  368. ck_assert_uint_eq(mon->maxQueueSize, 1);
  369. queueItem = TAILQ_FIRST(&mon->queue);
  370. ck_assert_uint_eq(queueItem->data.value.hasStatus, false); /* the infobit is only set if the queue is larger than one */
  371. /* Remove the subscriptions */
  372. UA_DeleteSubscriptionsRequest deleteSubscriptionsRequest;
  373. UA_DeleteSubscriptionsRequest_init(&deleteSubscriptionsRequest);
  374. UA_UInt32 removeId = localSubscriptionId;
  375. deleteSubscriptionsRequest.subscriptionIdsSize = 1;
  376. deleteSubscriptionsRequest.subscriptionIds = &removeId;
  377. UA_DeleteSubscriptionsResponse deleteSubscriptionsResponse;
  378. UA_DeleteSubscriptionsResponse_init(&deleteSubscriptionsResponse);
  379. Service_DeleteSubscriptions(server, &adminSession, &deleteSubscriptionsRequest, &deleteSubscriptionsResponse);
  380. ck_assert_uint_eq(deleteSubscriptionsResponse.responseHeader.serviceResult, UA_STATUSCODE_GOOD);
  381. ck_assert_uint_eq(deleteSubscriptionsResponse.resultsSize, 1);
  382. ck_assert_uint_eq(deleteSubscriptionsResponse.results[0], UA_STATUSCODE_GOOD);
  383. UA_DeleteSubscriptionsResponse_deleteMembers(&deleteSubscriptionsResponse);
  384. }
  385. END_TEST
  386. START_TEST(Server_setMonitoringMode) {
  387. UA_SetMonitoringModeRequest request;
  388. UA_SetMonitoringModeRequest_init(&request);
  389. request.subscriptionId = subscriptionId;
  390. request.monitoringMode = UA_MONITORINGMODE_DISABLED;
  391. request.monitoredItemIdsSize = 1;
  392. request.monitoredItemIds = &monitoredItemId;
  393. UA_SetMonitoringModeResponse response;
  394. UA_SetMonitoringModeResponse_init(&response);
  395. Service_SetMonitoringMode(server, &adminSession, &request, &response);
  396. ck_assert_uint_eq(response.responseHeader.serviceResult, UA_STATUSCODE_GOOD);
  397. ck_assert_uint_eq(response.resultsSize, 1);
  398. ck_assert_uint_eq(response.results[0], UA_STATUSCODE_GOOD);
  399. UA_SetMonitoringModeResponse_deleteMembers(&response);
  400. }
  401. END_TEST
  402. START_TEST(Server_deleteMonitoredItems) {
  403. UA_DeleteMonitoredItemsRequest request;
  404. UA_DeleteMonitoredItemsRequest_init(&request);
  405. request.subscriptionId = subscriptionId;
  406. request.monitoredItemIdsSize = 1;
  407. request.monitoredItemIds = &monitoredItemId;
  408. UA_DeleteMonitoredItemsResponse response;
  409. UA_DeleteMonitoredItemsResponse_init(&response);
  410. Service_DeleteMonitoredItems(server, &adminSession, &request, &response);
  411. ck_assert_uint_eq(response.responseHeader.serviceResult, UA_STATUSCODE_GOOD);
  412. ck_assert_uint_eq(response.resultsSize, 1);
  413. ck_assert_uint_eq(response.results[0], UA_STATUSCODE_GOOD);
  414. UA_DeleteMonitoredItemsResponse_deleteMembers(&response);
  415. }
  416. END_TEST
  417. START_TEST(Server_lifeTimeCount) {
  418. /* Create a subscription */
  419. UA_CreateSubscriptionRequest request;
  420. UA_CreateSubscriptionResponse response;
  421. UA_CreateSubscriptionRequest_init(&request);
  422. request.publishingEnabled = true;
  423. request.requestedLifetimeCount = 3;
  424. request.requestedMaxKeepAliveCount = 1;
  425. UA_CreateSubscriptionResponse_init(&response);
  426. Service_CreateSubscription(server, &adminSession, &request, &response);
  427. ck_assert_uint_eq(response.responseHeader.serviceResult, UA_STATUSCODE_GOOD);
  428. ck_assert_uint_eq(response.revisedMaxKeepAliveCount, 1);
  429. ck_assert_uint_eq(response.revisedLifetimeCount, 3);
  430. UA_CreateSubscriptionResponse_deleteMembers(&response);
  431. /* Create a second subscription */
  432. UA_CreateSubscriptionRequest_init(&request);
  433. request.publishingEnabled = true;
  434. request.requestedLifetimeCount = 4;
  435. request.requestedMaxKeepAliveCount = 2;
  436. UA_CreateSubscriptionResponse_init(&response);
  437. Service_CreateSubscription(server, &adminSession, &request, &response);
  438. ck_assert_uint_eq(response.responseHeader.serviceResult, UA_STATUSCODE_GOOD);
  439. ck_assert_uint_eq(response.revisedMaxKeepAliveCount, 2);
  440. /* revisedLifetimeCount is revised to 3*MaxKeepAliveCount == 3 */
  441. ck_assert_uint_eq(response.revisedLifetimeCount, 6);
  442. UA_Double publishingInterval = response.revisedPublishingInterval;
  443. ck_assert(publishingInterval > 0.0f);
  444. subscriptionId = response.subscriptionId;
  445. UA_CreateSubscriptionResponse_deleteMembers(&response);
  446. /* Add a MonitoredItem to the second subscription */
  447. UA_CreateMonitoredItemsRequest mrequest;
  448. UA_CreateMonitoredItemsRequest_init(&mrequest);
  449. mrequest.subscriptionId = subscriptionId;
  450. mrequest.timestampsToReturn = UA_TIMESTAMPSTORETURN_SERVER;
  451. UA_MonitoredItemCreateRequest item;
  452. UA_MonitoredItemCreateRequest_init(&item);
  453. UA_ReadValueId rvi;
  454. UA_ReadValueId_init(&rvi);
  455. rvi.nodeId = UA_NODEID_NUMERIC(0, UA_NS0ID_SERVER);
  456. rvi.attributeId = UA_ATTRIBUTEID_BROWSENAME;
  457. rvi.indexRange = UA_STRING_NULL;
  458. item.itemToMonitor = rvi;
  459. item.requestedParameters.samplingInterval = publishingInterval / 5.0;
  460. item.monitoringMode = UA_MONITORINGMODE_REPORTING;
  461. UA_MonitoringParameters params;
  462. UA_MonitoringParameters_init(&params);
  463. item.requestedParameters = params;
  464. mrequest.itemsToCreateSize = 1;
  465. mrequest.itemsToCreate = &item;
  466. UA_CreateMonitoredItemsResponse mresponse;
  467. UA_CreateMonitoredItemsResponse_init(&mresponse);
  468. Service_CreateMonitoredItems(server, &adminSession, &mrequest, &mresponse);
  469. ck_assert_uint_eq(mresponse.responseHeader.serviceResult, UA_STATUSCODE_GOOD);
  470. ck_assert_uint_eq(mresponse.resultsSize, 1);
  471. ck_assert_uint_eq(mresponse.results[0].statusCode, UA_STATUSCODE_GOOD);
  472. monitoredItemId = mresponse.results[0].monitoredItemId;
  473. ck_assert_uint_gt(monitoredItemId, 0);
  474. UA_MonitoredItemCreateRequest_deleteMembers(&item);
  475. UA_CreateMonitoredItemsResponse_deleteMembers(&mresponse);
  476. UA_Server_run_iterate(server, false);
  477. UA_UInt32 count = 0;
  478. UA_Subscription *sub;
  479. LIST_FOREACH(sub, &adminSession.serverSubscriptions, listEntry) {
  480. ck_assert_uint_eq(sub->currentLifetimeCount, 0);
  481. count++;
  482. }
  483. ck_assert_uint_eq(count, 2);
  484. UA_fakeSleep((UA_UInt32)publishingInterval + 1);
  485. UA_Server_run_iterate(server, false);
  486. count = 0;
  487. LIST_FOREACH(sub, &adminSession.serverSubscriptions, listEntry) {
  488. ck_assert_uint_eq(sub->currentLifetimeCount, 1);
  489. count++;
  490. }
  491. ck_assert_uint_eq(count, 2);
  492. /* Sleep until the publishing interval times out */
  493. UA_fakeSleep((UA_UInt32)publishingInterval + 1);
  494. UA_Server_run_iterate(server, false);
  495. count = 0;
  496. LIST_FOREACH(sub, &adminSession.serverSubscriptions, listEntry) {
  497. ck_assert_uint_eq(sub->currentLifetimeCount, 2);
  498. count++;
  499. }
  500. ck_assert_uint_eq(count, 2);
  501. /* Sleep until the publishing interval times out */
  502. UA_fakeSleep((UA_UInt32)publishingInterval + 1);
  503. UA_Server_run_iterate(server, false);
  504. count = 0;
  505. LIST_FOREACH(sub, &adminSession.serverSubscriptions, listEntry) {
  506. ck_assert_uint_eq(sub->currentLifetimeCount, 3);
  507. count++;
  508. }
  509. ck_assert_uint_eq(count, 2);
  510. /* Sleep until the publishing interval times out */
  511. UA_fakeSleep((UA_UInt32)publishingInterval + 1);
  512. UA_Server_run_iterate(server, false);
  513. count = 0;
  514. LIST_FOREACH(sub, &adminSession.serverSubscriptions, listEntry) {
  515. ck_assert_uint_eq(sub->currentLifetimeCount, 4);
  516. count++;
  517. }
  518. ck_assert_uint_eq(count, 1);
  519. /* Sleep until the publishing interval times out */
  520. UA_fakeSleep((UA_UInt32)publishingInterval + 1);
  521. UA_Server_run_iterate(server, false);
  522. count = 0;
  523. LIST_FOREACH(sub, &adminSession.serverSubscriptions, listEntry) {
  524. ck_assert_uint_eq(sub->currentLifetimeCount, 5);
  525. count++;
  526. }
  527. ck_assert_uint_eq(count, 1);
  528. /* Sleep until the publishing interval times out */
  529. UA_fakeSleep((UA_UInt32)publishingInterval + 1);
  530. UA_Server_run_iterate(server, false);
  531. count = 0;
  532. LIST_FOREACH(sub, &adminSession.serverSubscriptions, listEntry) {
  533. ck_assert_uint_eq(sub->currentLifetimeCount, 6);
  534. count++;
  535. }
  536. ck_assert_uint_eq(count, 1);
  537. /* Sleep until the publishing interval times out. The next iteration removes
  538. * the subscription. */
  539. UA_fakeSleep((UA_UInt32)publishingInterval + 1);
  540. UA_Server_run_iterate(server, false);
  541. count = 0;
  542. LIST_FOREACH(sub, &adminSession.serverSubscriptions, listEntry) {
  543. count++;
  544. }
  545. ck_assert_uint_eq(count, 0);
  546. }
  547. END_TEST
  548. #endif /* UA_ENABLE_SUBSCRIPTIONS */
  549. static Suite* testSuite_Client(void) {
  550. Suite *s = suite_create("Server Subscription");
  551. TCase *tc_server = tcase_create("Server Subscription Basic");
  552. tcase_add_checked_fixture(tc_server, setup, teardown);
  553. #ifdef UA_ENABLE_SUBSCRIPTIONS
  554. tcase_add_test(tc_server, Server_createSubscription);
  555. tcase_add_test(tc_server, Server_modifySubscription);
  556. tcase_add_test(tc_server, Server_setPublishingMode);
  557. tcase_add_test(tc_server, Server_createMonitoredItems);
  558. tcase_add_test(tc_server, Server_modifyMonitoredItems);
  559. tcase_add_test(tc_server, Server_overflow);
  560. tcase_add_test(tc_server, Server_setMonitoringMode);
  561. tcase_add_test(tc_server, Server_deleteMonitoredItems);
  562. tcase_add_test(tc_server, Server_republish);
  563. tcase_add_test(tc_server, Server_deleteSubscription);
  564. tcase_add_test(tc_server, Server_republish_invalid);
  565. tcase_add_test(tc_server, Server_publishCallback);
  566. tcase_add_test(tc_server, Server_lifeTimeCount);
  567. #endif /* UA_ENABLE_SUBSCRIPTIONS */
  568. suite_add_tcase(s, tc_server);
  569. return s;
  570. }
  571. int main(void) {
  572. Suite *s = testSuite_Client();
  573. SRunner *sr = srunner_create(s);
  574. srunner_set_fork_status(sr, CK_NOFORK);
  575. srunner_run_all(sr,CK_NORMAL);
  576. int number_failed = srunner_ntests_failed(sr);
  577. srunner_free(sr);
  578. return (number_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
  579. }