check_services_subscriptions.c 35 KB

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