check_services_subscriptions.c 33 KB

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