check_services_subscriptions.c 33 KB

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