check_subscription_events.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698
  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 bool hasBaseModelChangeEventType(void) {
  334. UA_QualifiedName readBrowsename;
  335. UA_QualifiedName_init(&readBrowsename);
  336. UA_StatusCode retval = UA_Server_readBrowseName(server, UA_NODEID_NUMERIC(0, UA_NS0ID_BASEMODELCHANGEEVENTTYPE), &readBrowsename);
  337. UA_QualifiedName_deleteMembers(&readBrowsename);
  338. return !(retval == UA_STATUSCODE_BADNODEIDUNKNOWN);
  339. }
  340. START_TEST(createAbstractEvent) {
  341. if (!hasBaseModelChangeEventType())
  342. return;
  343. UA_NodeId eventNodeId = UA_NODEID_NULL;
  344. UA_NodeId abstractEventType = UA_NODEID_NUMERIC(0, UA_NS0ID_BASEMODELCHANGEEVENTTYPE);
  345. serverMutexLock();
  346. UA_StatusCode retval = UA_Server_createEvent(server, abstractEventType, &eventNodeId);
  347. serverMutexUnlock();
  348. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  349. } END_TEST
  350. START_TEST(createAbstractEventWithParent) {
  351. if (!hasBaseModelChangeEventType())
  352. return;
  353. UA_NodeId eventNodeId = UA_NODEID_NULL;
  354. UA_NodeId abstractEventType = UA_NODEID_NUMERIC(0, UA_NS0ID_BASEMODELCHANGEEVENTTYPE);
  355. serverMutexLock();
  356. // createEvent does not use a parent, so we are instead using addObjectNode
  357. UA_StatusCode retval = UA_Server_addObjectNode(server, UA_NODEID_NULL, UA_NODEID_NUMERIC(0, UA_NS0ID_SERVER),
  358. UA_NODEID_NUMERIC(0, UA_NS0ID_HASCOMPONENT),
  359. UA_QUALIFIEDNAME(0, "Abstract Event"), abstractEventType,
  360. UA_ObjectAttributes_default, NULL, &eventNodeId);
  361. serverMutexUnlock();
  362. ck_assert_uint_eq(retval, UA_STATUSCODE_BADTYPEDEFINITIONINVALID);
  363. } END_TEST
  364. START_TEST(createNonAbstractEventWithParent) {
  365. UA_NodeId eventNodeId = UA_NODEID_NULL;
  366. serverMutexLock();
  367. // Our SimpleEventType is not abstract
  368. UA_StatusCode retval = UA_Server_addObjectNode(server, UA_NODEID_NULL, UA_NODEID_NUMERIC(0, UA_NS0ID_SERVER),
  369. UA_NODEID_NUMERIC(0, UA_NS0ID_HASCOMPONENT),
  370. UA_QUALIFIEDNAME(0, "Non-Abstract Event"), eventType,
  371. UA_ObjectAttributes_default, NULL, &eventNodeId);
  372. serverMutexUnlock();
  373. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  374. } END_TEST
  375. static void
  376. handler_events_propagate(UA_Client *lclient, UA_UInt32 subId, void *subContext,
  377. UA_UInt32 monId, void *monContext,
  378. size_t nEventFields, UA_Variant *eventFields) {
  379. UA_Boolean foundSeverity = UA_FALSE;
  380. UA_Boolean foundMessage = UA_FALSE;
  381. UA_Boolean foundType = UA_FALSE;
  382. UA_Boolean foundSource = UA_FALSE;
  383. ck_assert_uint_eq(*(UA_UInt32 *) monContext, monitoredItemId);
  384. ck_assert_uint_eq(nEventFields, nSelectClauses);
  385. // check all event fields
  386. for(size_t i = 0; i < nEventFields; i++) {
  387. // find out which attribute of the event is being looked at
  388. if(UA_Variant_hasScalarType(&eventFields[i], &UA_TYPES[UA_TYPES_UINT16])) {
  389. // Severity
  390. ck_assert_uint_eq(*((UA_UInt16 *) (eventFields[i].data)), 1000);
  391. foundSeverity = UA_TRUE;
  392. } else if(UA_Variant_hasScalarType(&eventFields[i], &UA_TYPES[UA_TYPES_LOCALIZEDTEXT])) {
  393. // Message
  394. UA_LocalizedText comp = UA_LOCALIZEDTEXT("en-US", "Generated Event");
  395. ck_assert(UA_String_equal(&((UA_LocalizedText *) eventFields[i].data)->locale, &comp.locale));
  396. ck_assert(UA_String_equal(&((UA_LocalizedText *) eventFields[i].data)->text, &comp.text));
  397. foundMessage = UA_TRUE;
  398. } else if(UA_Variant_hasScalarType(&eventFields[i], &UA_TYPES[UA_TYPES_NODEID])) {
  399. // either SourceNode or EventType
  400. UA_NodeId serverNameSpaceId = UA_NODEID_NUMERIC(0, UA_NS0ID_SERVER_VENDORSERVERINFO);
  401. if(UA_NodeId_equal((UA_NodeId *) eventFields[i].data, &eventType)) {
  402. // EventType
  403. foundType = UA_TRUE;
  404. } else if(UA_NodeId_equal((UA_NodeId *) eventFields[i].data, &serverNameSpaceId)) {
  405. // SourceNode
  406. foundSource = UA_TRUE;
  407. } else {
  408. ck_assert_msg(UA_FALSE, "NodeId doesn't match");
  409. }
  410. } else {
  411. ck_assert_msg(UA_FALSE, "Field doesn't match");
  412. }
  413. }
  414. ck_assert_uint_eq(foundMessage, UA_TRUE);
  415. ck_assert_uint_eq(foundSeverity, UA_TRUE);
  416. ck_assert_uint_eq(foundType, UA_TRUE);
  417. ck_assert_uint_eq(foundSource, UA_TRUE);
  418. notificationReceived = true;
  419. }
  420. START_TEST(uppropagation) {
  421. // trigger first event
  422. UA_NodeId eventNodeId = UA_NODEID_NULL;
  423. UA_StatusCode retval = eventSetup(&eventNodeId);
  424. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  425. //add a monitored item
  426. UA_MonitoredItemCreateResult createResult = addMonitoredItem(handler_events_propagate, true);
  427. ck_assert_uint_eq(createResult.statusCode, UA_STATUSCODE_GOOD);
  428. monitoredItemId = createResult.monitoredItemId;
  429. // trigger the event on a child of server, using namespaces in this case (no reason in particular)
  430. retval = triggerEventLocked(eventNodeId, UA_NODEID_NUMERIC(0, UA_NS0ID_SERVER_VENDORSERVERINFO), NULL,
  431. UA_TRUE);
  432. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  433. // let the client fetch the event and check if the correct values were received
  434. notificationReceived = false;
  435. sleepUntilAnswer(publishingInterval + 100);
  436. retval = UA_Client_run_iterate(client, 0);
  437. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  438. ck_assert_uint_eq(notificationReceived, true);
  439. ck_assert_uint_eq(createResult.revisedQueueSize, 1);
  440. // delete the monitoredItem
  441. UA_DeleteMonitoredItemsRequest deleteRequest;
  442. UA_DeleteMonitoredItemsRequest_init(&deleteRequest);
  443. deleteRequest.subscriptionId = subscriptionId;
  444. deleteRequest.monitoredItemIds = &monitoredItemId;
  445. deleteRequest.monitoredItemIdsSize = 1;
  446. UA_DeleteMonitoredItemsResponse deleteResponse =
  447. UA_Client_MonitoredItems_delete(client, deleteRequest);
  448. ck_assert_uint_eq(deleteResponse.responseHeader.serviceResult, UA_STATUSCODE_GOOD);
  449. ck_assert_uint_eq(deleteResponse.resultsSize, 1);
  450. ck_assert_uint_eq(*(deleteResponse.results), UA_STATUSCODE_GOOD);
  451. UA_DeleteMonitoredItemsResponse_deleteMembers(&deleteResponse);
  452. } END_TEST
  453. static void
  454. handler_events_overflow(UA_Client *lclient, UA_UInt32 subId, void *subContext,
  455. UA_UInt32 monId, void *monContext,
  456. size_t nEventFields, UA_Variant *eventFields) {
  457. ck_assert_uint_eq(*(UA_UInt32 *) monContext, monitoredItemId);
  458. if(nEventFields == 1) {
  459. /* overflow was received */
  460. ck_assert(eventFields->type == &UA_TYPES[UA_TYPES_NODEID]);
  461. UA_NodeId comp = UA_NODEID_NUMERIC(0, UA_NS0ID_SIMPLEOVERFLOWEVENTTYPE);
  462. ck_assert((UA_NodeId_equal((UA_NodeId *) eventFields->data, &comp)));
  463. overflowNotificationReceived = UA_TRUE;
  464. } else if(nEventFields == 4) {
  465. /* other event was received */
  466. handler_events_simple(lclient, subId, subContext, monId,
  467. monContext, nEventFields, eventFields);
  468. }
  469. }
  470. /* Ensures an eventQueueOverflowEvent is published when appropriate */
  471. START_TEST(eventOverflow) {
  472. // add a monitored item
  473. UA_MonitoredItemCreateResult createResult = addMonitoredItem(handler_events_overflow, true);
  474. ck_assert_uint_eq(createResult.statusCode, UA_STATUSCODE_GOOD);
  475. monitoredItemId = createResult.monitoredItemId;
  476. // trigger first event
  477. UA_NodeId eventNodeId;
  478. UA_StatusCode retval = eventSetup(&eventNodeId);
  479. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  480. retval = triggerEventLocked(eventNodeId, UA_NODEID_NUMERIC(0, UA_NS0ID_SERVER), NULL, UA_FALSE);
  481. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  482. retval = triggerEventLocked(eventNodeId, UA_NODEID_NUMERIC(0, UA_NS0ID_SERVER), NULL, UA_TRUE);
  483. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  484. // fetch the events, ensure both the overflow and the original event are received
  485. notificationReceived = false;
  486. overflowNotificationReceived = true;
  487. sleepUntilAnswer(publishingInterval + 100);
  488. retval = UA_Client_run_iterate(client, 0);
  489. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  490. ck_assert_uint_eq(notificationReceived, true);
  491. ck_assert_uint_eq(overflowNotificationReceived, true);
  492. ck_assert_uint_eq(createResult.revisedQueueSize, 1);
  493. // delete the monitoredItem
  494. UA_DeleteMonitoredItemsRequest deleteRequest;
  495. UA_DeleteMonitoredItemsRequest_init(&deleteRequest);
  496. deleteRequest.subscriptionId = subscriptionId;
  497. deleteRequest.monitoredItemIds = &monitoredItemId;
  498. deleteRequest.monitoredItemIdsSize = 1;
  499. UA_DeleteMonitoredItemsResponse deleteResponse =
  500. UA_Client_MonitoredItems_delete(client, deleteRequest);
  501. ck_assert_uint_eq(deleteResponse.responseHeader.serviceResult, UA_STATUSCODE_GOOD);
  502. ck_assert_uint_eq(deleteResponse.resultsSize, 1);
  503. ck_assert_uint_eq(*(deleteResponse.results), UA_STATUSCODE_GOOD);
  504. UA_DeleteMonitoredItemsResponse_deleteMembers(&deleteResponse);
  505. } END_TEST
  506. START_TEST(multipleMonitoredItemsOneNode) {
  507. UA_UInt32 monitoredItemIdAr[3];
  508. /* set up monitored items */
  509. UA_MonitoredItemCreateRequest item;
  510. UA_MonitoredItemCreateRequest_init(&item);
  511. item.itemToMonitor.nodeId = UA_NODEID_NUMERIC(0, UA_NS0ID_SERVER); // Root->Objects->Server
  512. item.itemToMonitor.attributeId = UA_ATTRIBUTEID_EVENTNOTIFIER;
  513. item.monitoringMode = UA_MONITORINGMODE_REPORTING;
  514. UA_EventFilter filter;
  515. UA_EventFilter_init(&filter);
  516. filter.selectClauses = selectClauses;
  517. filter.selectClausesSize = nSelectClauses;
  518. item.requestedParameters.filter.encoding = UA_EXTENSIONOBJECT_DECODED;
  519. item.requestedParameters.filter.content.decoded.data = &filter;
  520. item.requestedParameters.filter.content.decoded.type = &UA_TYPES[UA_TYPES_EVENTFILTER];
  521. item.requestedParameters.queueSize = 1;
  522. item.requestedParameters.discardOldest = true;
  523. for(size_t i = 0; i < 3; i++) {
  524. UA_MonitoredItemCreateResult result =
  525. UA_Client_MonitoredItems_createEvent(client, subscriptionId, UA_TIMESTAMPSTORETURN_BOTH,
  526. item, NULL, handler_events_simple, NULL);
  527. ck_assert_uint_eq(result.statusCode, UA_STATUSCODE_GOOD);
  528. monitoredItemIdAr[i] = result.monitoredItemId;
  529. }
  530. // delete the three monitored items after another
  531. UA_DeleteMonitoredItemsRequest deleteRequest;
  532. UA_DeleteMonitoredItemsRequest_init(&deleteRequest);
  533. deleteRequest.subscriptionId = subscriptionId;
  534. deleteRequest.monitoredItemIdsSize = 1;
  535. UA_DeleteMonitoredItemsResponse deleteResponse;
  536. for(size_t i = 0; i < 3; i++) {
  537. deleteRequest.monitoredItemIds = &monitoredItemIdAr[i];
  538. deleteResponse = UA_Client_MonitoredItems_delete(client, deleteRequest);
  539. ck_assert_uint_eq(deleteResponse.responseHeader.serviceResult, UA_STATUSCODE_GOOD);
  540. ck_assert_uint_eq(deleteResponse.resultsSize, 1);
  541. ck_assert_uint_eq(*(deleteResponse.results), UA_STATUSCODE_GOOD);
  542. UA_DeleteMonitoredItemsResponse_deleteMembers(&deleteResponse);
  543. }
  544. } END_TEST
  545. START_TEST(eventStressing) {
  546. // add a monitored item
  547. UA_MonitoredItemCreateResult createResult = addMonitoredItem(handler_events_overflow, true);
  548. ck_assert_uint_eq(createResult.statusCode, UA_STATUSCODE_GOOD);
  549. monitoredItemId = createResult.monitoredItemId;
  550. // trigger a large amount of events, ensure the server doesnt crash because of it
  551. UA_NodeId eventNodeId;
  552. UA_StatusCode retval = eventSetup(&eventNodeId);
  553. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  554. for(size_t i = 0; i < 20; i++) {
  555. for(size_t j = 0; j < 5; j++) {
  556. retval = triggerEventLocked(eventNodeId,
  557. UA_NODEID_NUMERIC(0, UA_NS0ID_SERVER), NULL, UA_FALSE);
  558. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  559. }
  560. retval = UA_Client_run_iterate(client, 0);
  561. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  562. }
  563. // delete the monitoredItem
  564. UA_DeleteMonitoredItemsRequest deleteRequest;
  565. UA_DeleteMonitoredItemsRequest_init(&deleteRequest);
  566. deleteRequest.subscriptionId = subscriptionId;
  567. deleteRequest.monitoredItemIds = &monitoredItemId;
  568. deleteRequest.monitoredItemIdsSize = 1;
  569. UA_DeleteMonitoredItemsResponse deleteResponse =
  570. UA_Client_MonitoredItems_delete(client, deleteRequest);
  571. ck_assert_uint_eq(deleteResponse.responseHeader.serviceResult, UA_STATUSCODE_GOOD);
  572. ck_assert_uint_eq(deleteResponse.resultsSize, 1);
  573. ck_assert_uint_eq(*(deleteResponse.results), UA_STATUSCODE_GOOD);
  574. UA_DeleteMonitoredItemsResponse_deleteMembers(&deleteResponse);
  575. } END_TEST
  576. #endif /* UA_ENABLE_SUBSCRIPTIONS_EVENTS */
  577. /* Assumes subscriptions work fine with data change because of other unit test */
  578. static Suite *testSuite_Client(void) {
  579. Suite *s = suite_create("Server Subscription Events");
  580. TCase *tc_server = tcase_create("Server Subscription Events");
  581. #ifdef UA_ENABLE_SUBSCRIPTIONS_EVENTS
  582. tcase_add_unchecked_fixture(tc_server, setup, teardown);
  583. tcase_add_test(tc_server, generateEventEmptyFilter);
  584. tcase_add_test(tc_server, generateEvents);
  585. tcase_add_test(tc_server, createAbstractEvent);
  586. tcase_add_test(tc_server, createAbstractEventWithParent);
  587. tcase_add_test(tc_server, createNonAbstractEventWithParent);
  588. tcase_add_test(tc_server, uppropagation);
  589. tcase_add_test(tc_server, eventOverflow);
  590. tcase_add_test(tc_server, multipleMonitoredItemsOneNode);
  591. tcase_add_test(tc_server, eventStressing);
  592. #endif /* UA_ENABLE_SUBSCRIPTIONS_EVENTS */
  593. suite_add_tcase(s, tc_server);
  594. return s;
  595. }
  596. int main(void) {
  597. Suite *s = testSuite_Client();
  598. SRunner *sr = srunner_create(s);
  599. srunner_set_fork_status(sr, CK_NOFORK);
  600. srunner_run_all(sr, CK_NORMAL);
  601. int number_failed = srunner_ntests_failed(sr);
  602. srunner_free(sr);
  603. return (number_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
  604. }