check_subscription_events.c 25 KB

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