check_subscription_events.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647
  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/client_config_default.h>
  5. #include <open62541/client_subscriptions.h>
  6. #include <open62541/server.h>
  7. #include <open62541/server_config_default.h>
  8. #include "server/ua_server_internal.h"
  9. #include "server/ua_services.h"
  10. #include "server/ua_subscription.h"
  11. #include <check.h>
  12. #include "testing_clock.h"
  13. #include "thread_wrapper.h"
  14. #ifdef UA_ENABLE_SUBSCRIPTIONS_EVENTS
  15. static UA_Server *server;
  16. static size_t serverIterations;
  17. static UA_Boolean running;
  18. static THREAD_HANDLE server_thread;
  19. static MUTEX_HANDLE serverMutex;
  20. UA_Client *client;
  21. static UA_UInt32 subscriptionId;
  22. static UA_UInt32 monitoredItemId;
  23. static UA_NodeId eventType;
  24. static size_t nSelectClauses = 4;
  25. static UA_Boolean notificationReceived;
  26. static UA_Boolean overflowNotificationReceived;
  27. static UA_SimpleAttributeOperand *selectClauses;
  28. UA_Double publishingInterval = 500.0;
  29. static void
  30. addNewEventType(void) {
  31. UA_ObjectTypeAttributes attr = UA_ObjectTypeAttributes_default;
  32. attr.displayName = UA_LOCALIZEDTEXT_ALLOC("en-US", "SimpleEventType");
  33. attr.description = UA_LOCALIZEDTEXT_ALLOC("en-US", "The simple event type we created");
  34. UA_Server_addObjectTypeNode(server, UA_NODEID_NULL,
  35. UA_NODEID_NUMERIC(0, UA_NS0ID_BASEEVENTTYPE),
  36. UA_NODEID_NUMERIC(0, UA_NS0ID_HASSUBTYPE),
  37. UA_QUALIFIEDNAME(0, "SimpleEventType"),
  38. attr, NULL, &eventType);
  39. UA_LocalizedText_deleteMembers(&attr.displayName);
  40. UA_LocalizedText_deleteMembers(&attr.description);
  41. }
  42. static void
  43. setupSelectClauses(void) {
  44. /* Check for severity (set manually), message (set manually), eventType
  45. * (automatic) and sourceNode (automatic) */
  46. selectClauses = (UA_SimpleAttributeOperand *)
  47. UA_Array_new(nSelectClauses, &UA_TYPES[UA_TYPES_SIMPLEATTRIBUTEOPERAND]);
  48. if(!selectClauses)
  49. return;
  50. for(size_t i = 0; i < nSelectClauses; ++i) {
  51. UA_SimpleAttributeOperand_init(&selectClauses[i]);
  52. selectClauses[i].typeDefinitionId = UA_NODEID_NUMERIC(0, UA_NS0ID_BASEEVENTTYPE);
  53. selectClauses[i].browsePathSize = 1;
  54. selectClauses[i].attributeId = UA_ATTRIBUTEID_VALUE;
  55. selectClauses[i].browsePath = (UA_QualifiedName *)
  56. UA_Array_new(selectClauses[i].browsePathSize, &UA_TYPES[UA_TYPES_QUALIFIEDNAME]);
  57. if(!selectClauses[i].browsePathSize) {
  58. UA_Array_delete(selectClauses, nSelectClauses, &UA_TYPES[UA_TYPES_SIMPLEATTRIBUTEOPERAND]);
  59. }
  60. }
  61. selectClauses[0].browsePath[0] = UA_QUALIFIEDNAME_ALLOC(0, "Severity");
  62. selectClauses[1].browsePath[0] = UA_QUALIFIEDNAME_ALLOC(0, "Message");
  63. selectClauses[2].browsePath[0] = UA_QUALIFIEDNAME_ALLOC(0, "EventType");
  64. selectClauses[3].browsePath[0] = UA_QUALIFIEDNAME_ALLOC(0, "SourceNode");
  65. }
  66. static void
  67. handler_events_simple(UA_Client *lclient, UA_UInt32 subId, void *subContext,
  68. UA_UInt32 monId, void *monContext,
  69. size_t nEventFields, UA_Variant *eventFields) {
  70. UA_Boolean foundSeverity = UA_FALSE;
  71. UA_Boolean foundMessage = UA_FALSE;
  72. UA_Boolean foundType = UA_FALSE;
  73. UA_Boolean foundSource = UA_FALSE;
  74. ck_assert_uint_eq(*(UA_UInt32 *) monContext, monitoredItemId);
  75. ck_assert_uint_eq(nEventFields, nSelectClauses);
  76. // check all event fields
  77. for(size_t i = 0; i < nEventFields; i++) {
  78. // find out which attribute of the event is being looked at
  79. if(UA_Variant_hasScalarType(&eventFields[i], &UA_TYPES[UA_TYPES_UINT16])) {
  80. // Severity
  81. ck_assert_uint_eq(*((UA_UInt16 *) (eventFields[i].data)), 1000);
  82. foundSeverity = UA_TRUE;
  83. } else if(UA_Variant_hasScalarType(&eventFields[i], &UA_TYPES[UA_TYPES_LOCALIZEDTEXT])) {
  84. // Message
  85. UA_LocalizedText comp = UA_LOCALIZEDTEXT("en-US", "Generated Event");
  86. ck_assert(UA_String_equal(&((UA_LocalizedText *) eventFields[i].data)->locale, &comp.locale));
  87. ck_assert(UA_String_equal(&((UA_LocalizedText *) eventFields[i].data)->text, &comp.text));
  88. foundMessage = UA_TRUE;
  89. } else if(UA_Variant_hasScalarType(&eventFields[i], &UA_TYPES[UA_TYPES_NODEID])) {
  90. // either SourceNode or EventType
  91. UA_NodeId serverId = UA_NODEID_NUMERIC(0, UA_NS0ID_SERVER);
  92. if(UA_NodeId_equal((UA_NodeId *) eventFields[i].data, &eventType)) {
  93. // EventType
  94. foundType = UA_TRUE;
  95. } else if(UA_NodeId_equal((UA_NodeId *) eventFields[i].data, &serverId)) {
  96. // SourceNode
  97. foundSource = UA_TRUE;
  98. } else {
  99. ck_assert_msg(UA_FALSE, "NodeId doesn't match");
  100. }
  101. } else {
  102. ck_assert_msg(UA_FALSE, "Field doesn't match");
  103. }
  104. }
  105. ck_assert_uint_eq(foundMessage, UA_TRUE);
  106. ck_assert_uint_eq(foundSeverity, UA_TRUE);
  107. ck_assert_uint_eq(foundType, UA_TRUE);
  108. ck_assert_uint_eq(foundSource, UA_TRUE);
  109. notificationReceived = true;
  110. }
  111. // create a subscription and add a monitored item to it
  112. static void
  113. setupSubscription(void) {
  114. // Create subscription
  115. UA_CreateSubscriptionRequest request = UA_CreateSubscriptionRequest_default();
  116. UA_CreateSubscriptionResponse response =
  117. UA_Client_Subscriptions_create(client, request, NULL, NULL, NULL);
  118. subscriptionId = response.subscriptionId;
  119. }
  120. static void
  121. removeSubscription(void) {
  122. UA_DeleteSubscriptionsRequest deleteSubscriptionsRequest;
  123. UA_DeleteSubscriptionsRequest_init(&deleteSubscriptionsRequest);
  124. UA_UInt32 removeId = subscriptionId;
  125. deleteSubscriptionsRequest.subscriptionIdsSize = 1;
  126. deleteSubscriptionsRequest.subscriptionIds = &removeId;
  127. UA_DeleteSubscriptionsResponse deleteSubscriptionsResponse;
  128. UA_DeleteSubscriptionsResponse_init(&deleteSubscriptionsResponse);
  129. Service_DeleteSubscriptions(server, &server->adminSession, &deleteSubscriptionsRequest,
  130. &deleteSubscriptionsResponse);
  131. UA_DeleteSubscriptionsResponse_deleteMembers(&deleteSubscriptionsResponse);
  132. }
  133. static void serverMutexLock(void) {
  134. if (!(MUTEX_LOCK(serverMutex))) {
  135. fprintf(stderr, "Mutex cannot be locked.\n");
  136. exit(1);
  137. }
  138. }
  139. static void serverMutexUnlock(void) {
  140. if (!(MUTEX_UNLOCK(serverMutex))) {
  141. fprintf(stderr, "Mutex cannot be unlocked.\n");
  142. exit(1);
  143. }
  144. }
  145. THREAD_CALLBACK(serverloop) {
  146. while (running) {
  147. serverMutexLock();
  148. UA_Server_run_iterate(server, false);
  149. serverIterations++;
  150. serverMutexUnlock();
  151. }
  152. return 0;
  153. }
  154. static void
  155. sleepUntilAnswer(UA_Double sleepMs) {
  156. UA_fakeSleep((UA_UInt32)sleepMs);
  157. serverMutexLock();
  158. size_t oldIterations = serverIterations;
  159. size_t newIterations;
  160. serverMutexUnlock();
  161. while(true) {
  162. serverMutexLock();
  163. newIterations = serverIterations;
  164. serverMutexUnlock();
  165. if(oldIterations != newIterations)
  166. return;
  167. UA_realSleep(1);
  168. }
  169. }
  170. static void
  171. setup(void) {
  172. if (!MUTEX_INIT(serverMutex)) {
  173. fprintf(stderr, "Server mutex was not created correctly.");
  174. exit(1);
  175. }
  176. running = true;
  177. server = UA_Server_new();
  178. UA_ServerConfig *config = UA_Server_getConfig(server);
  179. UA_ServerConfig_setDefault(config);
  180. config->maxPublishReqPerSession = 5;
  181. UA_Server_run_startup(server);
  182. addNewEventType();
  183. setupSelectClauses();
  184. THREAD_CREATE(server_thread, serverloop);
  185. client = UA_Client_new();
  186. UA_ClientConfig_setDefault(UA_Client_getConfig(client));
  187. UA_StatusCode retval = UA_Client_connect(client, "opc.tcp://localhost:4840");
  188. if(retval != UA_STATUSCODE_GOOD) {
  189. fprintf(stderr, "Client can not connect to opc.tcp://localhost:4840. %s",
  190. UA_StatusCode_name(retval));
  191. exit(1);
  192. }
  193. setupSubscription();
  194. sleepUntilAnswer(publishingInterval + 100);
  195. }
  196. static void
  197. teardown(void) {
  198. running = false;
  199. THREAD_JOIN(server_thread);
  200. removeSubscription();
  201. UA_Server_run_shutdown(server);
  202. UA_Server_delete(server);
  203. UA_Array_delete(selectClauses, nSelectClauses, &UA_TYPES[UA_TYPES_SIMPLEATTRIBUTEOPERAND]);
  204. UA_Client_disconnect(client);
  205. UA_Client_delete(client);
  206. if (!MUTEX_DESTROY(serverMutex)) {
  207. fprintf(stderr, "Server mutex was not destroyed correctly.");
  208. exit(1);
  209. }
  210. }
  211. static UA_StatusCode
  212. triggerEventLocked(const UA_NodeId eventNodeId, const UA_NodeId origin,
  213. UA_ByteString *outEventId, const UA_Boolean deleteEventNode) {
  214. serverMutexLock();
  215. UA_StatusCode retval = UA_Server_triggerEvent(server, eventNodeId, origin,
  216. outEventId, deleteEventNode);
  217. serverMutexUnlock();
  218. return retval;
  219. }
  220. static UA_StatusCode
  221. eventSetup(UA_NodeId *eventNodeId) {
  222. UA_StatusCode retval;
  223. serverMutexLock();
  224. retval = UA_Server_createEvent(server, eventType, eventNodeId);
  225. serverMutexUnlock();
  226. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  227. // add a severity to the event
  228. UA_Variant value;
  229. UA_RelativePathElement rpe;
  230. UA_RelativePathElement_init(&rpe);
  231. rpe.referenceTypeId = UA_NODEID_NUMERIC(0, UA_NS0ID_HASPROPERTY);
  232. rpe.isInverse = false;
  233. rpe.includeSubtypes = false;
  234. UA_BrowsePath bp;
  235. UA_BrowsePath_init(&bp);
  236. bp.startingNode = *eventNodeId;
  237. bp.relativePath.elementsSize = 1;
  238. bp.relativePath.elements = &rpe;
  239. rpe.targetName = UA_QUALIFIEDNAME(0, "Severity");
  240. serverMutexLock();
  241. UA_BrowsePathResult bpr = UA_Server_translateBrowsePathToNodeIds(server, &bp);
  242. serverMutexUnlock();
  243. ck_assert_uint_eq(bpr.statusCode, UA_STATUSCODE_GOOD);
  244. // number with no special meaning
  245. UA_UInt16 eventSeverity = 1000;
  246. UA_Variant_setScalar(&value, &eventSeverity, &UA_TYPES[UA_TYPES_UINT16]);
  247. serverMutexLock();
  248. retval = UA_Server_writeValue(server, bpr.targets[0].targetId.nodeId, value);
  249. serverMutexUnlock();
  250. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  251. UA_BrowsePathResult_deleteMembers(&bpr);
  252. //add a message to the event
  253. rpe.targetName = UA_QUALIFIEDNAME(0, "Message");
  254. serverMutexLock();
  255. bpr = UA_Server_translateBrowsePathToNodeIds(server, &bp);
  256. serverMutexUnlock();
  257. ck_assert_uint_eq(bpr.statusCode, UA_STATUSCODE_GOOD);
  258. UA_LocalizedText message = UA_LOCALIZEDTEXT("en-US", "Generated Event");
  259. UA_Variant_setScalar(&value, &message, &UA_TYPES[UA_TYPES_LOCALIZEDTEXT]);
  260. serverMutexLock();
  261. retval = UA_Server_writeValue(server, bpr.targets[0].targetId.nodeId, value);
  262. serverMutexUnlock();
  263. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  264. UA_BrowsePathResult_deleteMembers(&bpr);
  265. return retval;
  266. }
  267. static UA_MonitoredItemCreateResult
  268. addMonitoredItem(UA_Client_EventNotificationCallback handler, bool setFilter) {
  269. UA_MonitoredItemCreateRequest item;
  270. UA_MonitoredItemCreateRequest_init(&item);
  271. item.itemToMonitor.nodeId = UA_NODEID_NUMERIC(0, 2253); // Root->Objects->Server
  272. item.itemToMonitor.attributeId = UA_ATTRIBUTEID_EVENTNOTIFIER;
  273. item.monitoringMode = UA_MONITORINGMODE_REPORTING;
  274. UA_EventFilter filter;
  275. UA_EventFilter_init(&filter);
  276. filter.selectClauses = selectClauses;
  277. filter.selectClausesSize = nSelectClauses;
  278. if (setFilter) {
  279. item.requestedParameters.filter.encoding = UA_EXTENSIONOBJECT_DECODED;
  280. item.requestedParameters.filter.content.decoded.data = &filter;
  281. item.requestedParameters.filter.content.decoded.type = &UA_TYPES[UA_TYPES_EVENTFILTER];
  282. }
  283. item.requestedParameters.queueSize = 1;
  284. item.requestedParameters.discardOldest = true;
  285. return UA_Client_MonitoredItems_createEvent(client, subscriptionId,
  286. UA_TIMESTAMPSTORETURN_BOTH, item,
  287. &monitoredItemId, handler, NULL);
  288. }
  289. /* Create event with empty filter */
  290. START_TEST(generateEventEmptyFilter) {
  291. UA_NodeId eventNodeId;
  292. UA_StatusCode retval = eventSetup(&eventNodeId);
  293. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  294. // add a monitored item
  295. UA_MonitoredItemCreateResult createResult = addMonitoredItem(handler_events_simple, false);
  296. ck_assert_uint_eq(createResult.statusCode, UA_STATUSCODE_BADEVENTFILTERINVALID);
  297. } END_TEST
  298. /* Ensure events are received with proper values */
  299. START_TEST(generateEvents) {
  300. UA_NodeId eventNodeId;
  301. UA_StatusCode retval = eventSetup(&eventNodeId);
  302. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  303. // add a monitored item
  304. UA_MonitoredItemCreateResult createResult = addMonitoredItem(handler_events_simple, true);
  305. ck_assert_uint_eq(createResult.statusCode, UA_STATUSCODE_GOOD);
  306. monitoredItemId = createResult.monitoredItemId;
  307. // trigger the event
  308. retval = triggerEventLocked(eventNodeId, UA_NODEID_NUMERIC(0, UA_NS0ID_SERVER), NULL, UA_TRUE);
  309. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  310. // let the client fetch the event and check if the correct values were received
  311. notificationReceived = false;
  312. sleepUntilAnswer(publishingInterval + 100);
  313. retval = UA_Client_run_iterate(client, 0);
  314. sleepUntilAnswer(publishingInterval + 100);
  315. retval = UA_Client_run_iterate(client, 0);
  316. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  317. ck_assert_uint_eq(notificationReceived, true);
  318. ck_assert_uint_eq(createResult.revisedQueueSize, 1);
  319. // delete the monitoredItem
  320. UA_DeleteMonitoredItemsRequest deleteRequest;
  321. UA_DeleteMonitoredItemsRequest_init(&deleteRequest);
  322. deleteRequest.subscriptionId = subscriptionId;
  323. deleteRequest.monitoredItemIds = &monitoredItemId;
  324. deleteRequest.monitoredItemIdsSize = 1;
  325. UA_DeleteMonitoredItemsResponse deleteResponse =
  326. UA_Client_MonitoredItems_delete(client, deleteRequest);
  327. sleepUntilAnswer(publishingInterval + 100);
  328. ck_assert_uint_eq(deleteResponse.responseHeader.serviceResult, UA_STATUSCODE_GOOD);
  329. ck_assert_uint_eq(deleteResponse.resultsSize, 1);
  330. ck_assert_uint_eq(*(deleteResponse.results), UA_STATUSCODE_GOOD);
  331. UA_DeleteMonitoredItemsResponse_deleteMembers(&deleteResponse);
  332. } END_TEST
  333. static void
  334. handler_events_propagate(UA_Client *lclient, UA_UInt32 subId, void *subContext,
  335. UA_UInt32 monId, void *monContext,
  336. size_t nEventFields, UA_Variant *eventFields) {
  337. UA_Boolean foundSeverity = UA_FALSE;
  338. UA_Boolean foundMessage = UA_FALSE;
  339. UA_Boolean foundType = UA_FALSE;
  340. UA_Boolean foundSource = UA_FALSE;
  341. ck_assert_uint_eq(*(UA_UInt32 *) monContext, monitoredItemId);
  342. ck_assert_uint_eq(nEventFields, nSelectClauses);
  343. // check all event fields
  344. for(size_t i = 0; i < nEventFields; i++) {
  345. // find out which attribute of the event is being looked at
  346. if(UA_Variant_hasScalarType(&eventFields[i], &UA_TYPES[UA_TYPES_UINT16])) {
  347. // Severity
  348. ck_assert_uint_eq(*((UA_UInt16 *) (eventFields[i].data)), 1000);
  349. foundSeverity = UA_TRUE;
  350. } else if(UA_Variant_hasScalarType(&eventFields[i], &UA_TYPES[UA_TYPES_LOCALIZEDTEXT])) {
  351. // Message
  352. UA_LocalizedText comp = UA_LOCALIZEDTEXT("en-US", "Generated Event");
  353. ck_assert(UA_String_equal(&((UA_LocalizedText *) eventFields[i].data)->locale, &comp.locale));
  354. ck_assert(UA_String_equal(&((UA_LocalizedText *) eventFields[i].data)->text, &comp.text));
  355. foundMessage = UA_TRUE;
  356. } else if(UA_Variant_hasScalarType(&eventFields[i], &UA_TYPES[UA_TYPES_NODEID])) {
  357. // either SourceNode or EventType
  358. UA_NodeId serverNameSpaceId = UA_NODEID_NUMERIC(0, UA_NS0ID_SERVER_VENDORSERVERINFO);
  359. if(UA_NodeId_equal((UA_NodeId *) eventFields[i].data, &eventType)) {
  360. // EventType
  361. foundType = UA_TRUE;
  362. } else if(UA_NodeId_equal((UA_NodeId *) eventFields[i].data, &serverNameSpaceId)) {
  363. // SourceNode
  364. foundSource = UA_TRUE;
  365. } else {
  366. ck_assert_msg(UA_FALSE, "NodeId doesn't match");
  367. }
  368. } else {
  369. ck_assert_msg(UA_FALSE, "Field doesn't match");
  370. }
  371. }
  372. ck_assert_uint_eq(foundMessage, UA_TRUE);
  373. ck_assert_uint_eq(foundSeverity, UA_TRUE);
  374. ck_assert_uint_eq(foundType, UA_TRUE);
  375. ck_assert_uint_eq(foundSource, UA_TRUE);
  376. notificationReceived = true;
  377. }
  378. START_TEST(uppropagation) {
  379. // trigger first event
  380. UA_NodeId eventNodeId = UA_NODEID_NULL;
  381. UA_StatusCode retval = eventSetup(&eventNodeId);
  382. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  383. //add a monitored item
  384. UA_MonitoredItemCreateResult createResult = addMonitoredItem(handler_events_propagate, true);
  385. ck_assert_uint_eq(createResult.statusCode, UA_STATUSCODE_GOOD);
  386. monitoredItemId = createResult.monitoredItemId;
  387. // trigger the event on a child of server, using namespaces in this case (no reason in particular)
  388. retval = triggerEventLocked(eventNodeId, UA_NODEID_NUMERIC(0, UA_NS0ID_SERVER_VENDORSERVERINFO), NULL,
  389. UA_TRUE);
  390. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  391. // let the client fetch the event and check if the correct values were received
  392. notificationReceived = false;
  393. sleepUntilAnswer(publishingInterval + 100);
  394. retval = UA_Client_run_iterate(client, 0);
  395. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  396. ck_assert_uint_eq(notificationReceived, true);
  397. ck_assert_uint_eq(createResult.revisedQueueSize, 1);
  398. // delete the monitoredItem
  399. UA_DeleteMonitoredItemsRequest deleteRequest;
  400. UA_DeleteMonitoredItemsRequest_init(&deleteRequest);
  401. deleteRequest.subscriptionId = subscriptionId;
  402. deleteRequest.monitoredItemIds = &monitoredItemId;
  403. deleteRequest.monitoredItemIdsSize = 1;
  404. UA_DeleteMonitoredItemsResponse deleteResponse =
  405. UA_Client_MonitoredItems_delete(client, deleteRequest);
  406. ck_assert_uint_eq(deleteResponse.responseHeader.serviceResult, UA_STATUSCODE_GOOD);
  407. ck_assert_uint_eq(deleteResponse.resultsSize, 1);
  408. ck_assert_uint_eq(*(deleteResponse.results), UA_STATUSCODE_GOOD);
  409. UA_DeleteMonitoredItemsResponse_deleteMembers(&deleteResponse);
  410. } END_TEST
  411. static void
  412. handler_events_overflow(UA_Client *lclient, UA_UInt32 subId, void *subContext,
  413. UA_UInt32 monId, void *monContext,
  414. size_t nEventFields, UA_Variant *eventFields) {
  415. ck_assert_uint_eq(*(UA_UInt32 *) monContext, monitoredItemId);
  416. if(nEventFields == 1) {
  417. /* overflow was received */
  418. ck_assert(eventFields->type == &UA_TYPES[UA_TYPES_NODEID]);
  419. UA_NodeId comp = UA_NODEID_NUMERIC(0, UA_NS0ID_SIMPLEOVERFLOWEVENTTYPE);
  420. ck_assert((UA_NodeId_equal((UA_NodeId *) eventFields->data, &comp)));
  421. overflowNotificationReceived = UA_TRUE;
  422. } else if(nEventFields == 4) {
  423. /* other event was received */
  424. handler_events_simple(lclient, subId, subContext, monId,
  425. monContext, nEventFields, eventFields);
  426. }
  427. }
  428. /* Ensures an eventQueueOverflowEvent is published when appropriate */
  429. START_TEST(eventOverflow) {
  430. // add a monitored item
  431. UA_MonitoredItemCreateResult createResult = addMonitoredItem(handler_events_overflow, true);
  432. ck_assert_uint_eq(createResult.statusCode, UA_STATUSCODE_GOOD);
  433. monitoredItemId = createResult.monitoredItemId;
  434. // trigger first event
  435. UA_NodeId eventNodeId;
  436. UA_StatusCode retval = eventSetup(&eventNodeId);
  437. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  438. retval = triggerEventLocked(eventNodeId, UA_NODEID_NUMERIC(0, UA_NS0ID_SERVER), NULL, UA_FALSE);
  439. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  440. retval = triggerEventLocked(eventNodeId, UA_NODEID_NUMERIC(0, UA_NS0ID_SERVER), NULL, UA_TRUE);
  441. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  442. // fetch the events, ensure both the overflow and the original event are received
  443. notificationReceived = false;
  444. overflowNotificationReceived = true;
  445. sleepUntilAnswer(publishingInterval + 100);
  446. retval = UA_Client_run_iterate(client, 0);
  447. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  448. ck_assert_uint_eq(notificationReceived, true);
  449. ck_assert_uint_eq(overflowNotificationReceived, true);
  450. ck_assert_uint_eq(createResult.revisedQueueSize, 1);
  451. // delete the monitoredItem
  452. UA_DeleteMonitoredItemsRequest deleteRequest;
  453. UA_DeleteMonitoredItemsRequest_init(&deleteRequest);
  454. deleteRequest.subscriptionId = subscriptionId;
  455. deleteRequest.monitoredItemIds = &monitoredItemId;
  456. deleteRequest.monitoredItemIdsSize = 1;
  457. UA_DeleteMonitoredItemsResponse deleteResponse =
  458. UA_Client_MonitoredItems_delete(client, deleteRequest);
  459. ck_assert_uint_eq(deleteResponse.responseHeader.serviceResult, UA_STATUSCODE_GOOD);
  460. ck_assert_uint_eq(deleteResponse.resultsSize, 1);
  461. ck_assert_uint_eq(*(deleteResponse.results), UA_STATUSCODE_GOOD);
  462. UA_DeleteMonitoredItemsResponse_deleteMembers(&deleteResponse);
  463. } END_TEST
  464. START_TEST(multipleMonitoredItemsOneNode) {
  465. UA_UInt32 monitoredItemIdAr[3];
  466. /* set up monitored items */
  467. UA_MonitoredItemCreateRequest item;
  468. UA_MonitoredItemCreateRequest_init(&item);
  469. item.itemToMonitor.nodeId = UA_NODEID_NUMERIC(0, UA_NS0ID_SERVER); // Root->Objects->Server
  470. item.itemToMonitor.attributeId = UA_ATTRIBUTEID_EVENTNOTIFIER;
  471. item.monitoringMode = UA_MONITORINGMODE_REPORTING;
  472. UA_EventFilter filter;
  473. UA_EventFilter_init(&filter);
  474. filter.selectClauses = selectClauses;
  475. filter.selectClausesSize = nSelectClauses;
  476. item.requestedParameters.filter.encoding = UA_EXTENSIONOBJECT_DECODED;
  477. item.requestedParameters.filter.content.decoded.data = &filter;
  478. item.requestedParameters.filter.content.decoded.type = &UA_TYPES[UA_TYPES_EVENTFILTER];
  479. item.requestedParameters.queueSize = 1;
  480. item.requestedParameters.discardOldest = true;
  481. for(size_t i = 0; i < 3; i++) {
  482. UA_MonitoredItemCreateResult result =
  483. UA_Client_MonitoredItems_createEvent(client, subscriptionId, UA_TIMESTAMPSTORETURN_BOTH,
  484. item, NULL, handler_events_simple, NULL);
  485. ck_assert_uint_eq(result.statusCode, UA_STATUSCODE_GOOD);
  486. monitoredItemIdAr[i] = result.monitoredItemId;
  487. }
  488. // delete the three monitored items after another
  489. UA_DeleteMonitoredItemsRequest deleteRequest;
  490. UA_DeleteMonitoredItemsRequest_init(&deleteRequest);
  491. deleteRequest.subscriptionId = subscriptionId;
  492. deleteRequest.monitoredItemIdsSize = 1;
  493. UA_DeleteMonitoredItemsResponse deleteResponse;
  494. for(size_t i = 0; i < 3; i++) {
  495. deleteRequest.monitoredItemIds = &monitoredItemIdAr[i];
  496. deleteResponse = UA_Client_MonitoredItems_delete(client, deleteRequest);
  497. ck_assert_uint_eq(deleteResponse.responseHeader.serviceResult, UA_STATUSCODE_GOOD);
  498. ck_assert_uint_eq(deleteResponse.resultsSize, 1);
  499. ck_assert_uint_eq(*(deleteResponse.results), UA_STATUSCODE_GOOD);
  500. UA_DeleteMonitoredItemsResponse_deleteMembers(&deleteResponse);
  501. }
  502. } END_TEST
  503. START_TEST(eventStressing) {
  504. // add a monitored item
  505. UA_MonitoredItemCreateResult createResult = addMonitoredItem(handler_events_overflow, true);
  506. ck_assert_uint_eq(createResult.statusCode, UA_STATUSCODE_GOOD);
  507. monitoredItemId = createResult.monitoredItemId;
  508. // trigger a large amount of events, ensure the server doesnt crash because of it
  509. UA_NodeId eventNodeId;
  510. UA_StatusCode retval = eventSetup(&eventNodeId);
  511. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  512. for(size_t i = 0; i < 20; i++) {
  513. for(size_t j = 0; j < 5; j++) {
  514. retval = triggerEventLocked(eventNodeId,
  515. UA_NODEID_NUMERIC(0, UA_NS0ID_SERVER), NULL, UA_FALSE);
  516. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  517. }
  518. retval = UA_Client_run_iterate(client, 0);
  519. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  520. }
  521. // delete the monitoredItem
  522. UA_DeleteMonitoredItemsRequest deleteRequest;
  523. UA_DeleteMonitoredItemsRequest_init(&deleteRequest);
  524. deleteRequest.subscriptionId = subscriptionId;
  525. deleteRequest.monitoredItemIds = &monitoredItemId;
  526. deleteRequest.monitoredItemIdsSize = 1;
  527. UA_DeleteMonitoredItemsResponse deleteResponse =
  528. UA_Client_MonitoredItems_delete(client, deleteRequest);
  529. ck_assert_uint_eq(deleteResponse.responseHeader.serviceResult, UA_STATUSCODE_GOOD);
  530. ck_assert_uint_eq(deleteResponse.resultsSize, 1);
  531. ck_assert_uint_eq(*(deleteResponse.results), UA_STATUSCODE_GOOD);
  532. UA_DeleteMonitoredItemsResponse_deleteMembers(&deleteResponse);
  533. } END_TEST
  534. #endif /* UA_ENABLE_SUBSCRIPTIONS_EVENTS */
  535. /* Assumes subscriptions work fine with data change because of other unit test */
  536. static Suite *testSuite_Client(void) {
  537. Suite *s = suite_create("Server Subscription Events");
  538. TCase *tc_server = tcase_create("Server Subscription Events");
  539. #ifdef UA_ENABLE_SUBSCRIPTIONS_EVENTS
  540. tcase_add_unchecked_fixture(tc_server, setup, teardown);
  541. tcase_add_test(tc_server, generateEventEmptyFilter);
  542. tcase_add_test(tc_server, generateEvents);
  543. tcase_add_test(tc_server, uppropagation);
  544. tcase_add_test(tc_server, eventOverflow);
  545. tcase_add_test(tc_server, multipleMonitoredItemsOneNode);
  546. tcase_add_test(tc_server, eventStressing);
  547. #endif /* UA_ENABLE_SUBSCRIPTIONS_EVENTS */
  548. suite_add_tcase(s, tc_server);
  549. return s;
  550. }
  551. int main(void) {
  552. Suite *s = testSuite_Client();
  553. SRunner *sr = srunner_create(s);
  554. srunner_set_fork_status(sr, CK_NOFORK);
  555. srunner_run_all(sr, CK_NORMAL);
  556. int number_failed = srunner_ntests_failed(sr);
  557. srunner_free(sr);
  558. return (number_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
  559. }