check_subscription_events.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699
  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. UA_LOCK(server->serviceMutex);
  130. Service_DeleteSubscriptions(server, &server->adminSession, &deleteSubscriptionsRequest,
  131. &deleteSubscriptionsResponse);
  132. UA_UNLOCK(server->serviceMutex);
  133. UA_DeleteSubscriptionsResponse_deleteMembers(&deleteSubscriptionsResponse);
  134. }
  135. static void serverMutexLock(void) {
  136. if (!(MUTEX_LOCK(serverMutex))) {
  137. fprintf(stderr, "Mutex cannot be locked.\n");
  138. exit(1);
  139. }
  140. }
  141. static void serverMutexUnlock(void) {
  142. if (!(MUTEX_UNLOCK(serverMutex))) {
  143. fprintf(stderr, "Mutex cannot be unlocked.\n");
  144. exit(1);
  145. }
  146. }
  147. THREAD_CALLBACK(serverloop) {
  148. while (running) {
  149. serverMutexLock();
  150. UA_Server_run_iterate(server, false);
  151. serverIterations++;
  152. serverMutexUnlock();
  153. }
  154. return 0;
  155. }
  156. static void
  157. sleepUntilAnswer(UA_Double sleepMs) {
  158. UA_fakeSleep((UA_UInt32)sleepMs);
  159. serverMutexLock();
  160. size_t oldIterations = serverIterations;
  161. size_t newIterations;
  162. serverMutexUnlock();
  163. while(true) {
  164. serverMutexLock();
  165. newIterations = serverIterations;
  166. serverMutexUnlock();
  167. if(oldIterations != newIterations)
  168. return;
  169. UA_realSleep(1);
  170. }
  171. }
  172. static void
  173. setup(void) {
  174. if (!MUTEX_INIT(serverMutex)) {
  175. fprintf(stderr, "Server mutex was not created correctly.");
  176. exit(1);
  177. }
  178. running = true;
  179. server = UA_Server_new();
  180. UA_ServerConfig *config = UA_Server_getConfig(server);
  181. UA_ServerConfig_setDefault(config);
  182. config->maxPublishReqPerSession = 5;
  183. UA_Server_run_startup(server);
  184. addNewEventType();
  185. setupSelectClauses();
  186. THREAD_CREATE(server_thread, serverloop);
  187. client = UA_Client_new();
  188. UA_ClientConfig_setDefault(UA_Client_getConfig(client));
  189. UA_StatusCode retval = UA_Client_connect(client, "opc.tcp://localhost:4840");
  190. if(retval != UA_STATUSCODE_GOOD) {
  191. fprintf(stderr, "Client can not connect to opc.tcp://localhost:4840. %s",
  192. UA_StatusCode_name(retval));
  193. exit(1);
  194. }
  195. setupSubscription();
  196. sleepUntilAnswer(publishingInterval + 100);
  197. }
  198. static void
  199. teardown(void) {
  200. running = false;
  201. THREAD_JOIN(server_thread);
  202. removeSubscription();
  203. UA_Server_run_shutdown(server);
  204. UA_Server_delete(server);
  205. UA_Array_delete(selectClauses, nSelectClauses, &UA_TYPES[UA_TYPES_SIMPLEATTRIBUTEOPERAND]);
  206. UA_Client_disconnect(client);
  207. UA_Client_delete(client);
  208. if (!MUTEX_DESTROY(serverMutex)) {
  209. fprintf(stderr, "Server mutex was not destroyed correctly.");
  210. exit(1);
  211. }
  212. }
  213. static UA_StatusCode
  214. triggerEventLocked(const UA_NodeId eventNodeId, const UA_NodeId origin,
  215. UA_ByteString *outEventId, const UA_Boolean deleteEventNode) {
  216. serverMutexLock();
  217. UA_StatusCode retval = UA_Server_triggerEvent(server, eventNodeId, origin,
  218. outEventId, deleteEventNode);
  219. serverMutexUnlock();
  220. return retval;
  221. }
  222. static UA_StatusCode
  223. eventSetup(UA_NodeId *eventNodeId) {
  224. UA_StatusCode retval;
  225. serverMutexLock();
  226. retval = UA_Server_createEvent(server, eventType, eventNodeId);
  227. serverMutexUnlock();
  228. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  229. // add a severity to the event
  230. UA_Variant value;
  231. UA_RelativePathElement rpe;
  232. UA_RelativePathElement_init(&rpe);
  233. rpe.referenceTypeId = UA_NODEID_NUMERIC(0, UA_NS0ID_HASPROPERTY);
  234. rpe.isInverse = false;
  235. rpe.includeSubtypes = false;
  236. UA_BrowsePath bp;
  237. UA_BrowsePath_init(&bp);
  238. bp.startingNode = *eventNodeId;
  239. bp.relativePath.elementsSize = 1;
  240. bp.relativePath.elements = &rpe;
  241. rpe.targetName = UA_QUALIFIEDNAME(0, "Severity");
  242. serverMutexLock();
  243. UA_BrowsePathResult bpr = UA_Server_translateBrowsePathToNodeIds(server, &bp);
  244. serverMutexUnlock();
  245. ck_assert_uint_eq(bpr.statusCode, UA_STATUSCODE_GOOD);
  246. // number with no special meaning
  247. UA_UInt16 eventSeverity = 1000;
  248. UA_Variant_setScalar(&value, &eventSeverity, &UA_TYPES[UA_TYPES_UINT16]);
  249. serverMutexLock();
  250. retval = UA_Server_writeValue(server, bpr.targets[0].targetId.nodeId, value);
  251. serverMutexUnlock();
  252. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  253. UA_BrowsePathResult_deleteMembers(&bpr);
  254. //add a message to the event
  255. rpe.targetName = UA_QUALIFIEDNAME(0, "Message");
  256. serverMutexLock();
  257. bpr = UA_Server_translateBrowsePathToNodeIds(server, &bp);
  258. serverMutexUnlock();
  259. ck_assert_uint_eq(bpr.statusCode, UA_STATUSCODE_GOOD);
  260. UA_LocalizedText message = UA_LOCALIZEDTEXT("en-US", "Generated Event");
  261. UA_Variant_setScalar(&value, &message, &UA_TYPES[UA_TYPES_LOCALIZEDTEXT]);
  262. serverMutexLock();
  263. retval = UA_Server_writeValue(server, bpr.targets[0].targetId.nodeId, value);
  264. serverMutexUnlock();
  265. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  266. UA_BrowsePathResult_deleteMembers(&bpr);
  267. return retval;
  268. }
  269. static UA_MonitoredItemCreateResult
  270. addMonitoredItem(UA_Client_EventNotificationCallback handler, bool setFilter) {
  271. UA_MonitoredItemCreateRequest item;
  272. UA_MonitoredItemCreateRequest_init(&item);
  273. item.itemToMonitor.nodeId = UA_NODEID_NUMERIC(0, 2253); // Root->Objects->Server
  274. item.itemToMonitor.attributeId = UA_ATTRIBUTEID_EVENTNOTIFIER;
  275. item.monitoringMode = UA_MONITORINGMODE_REPORTING;
  276. UA_EventFilter filter;
  277. UA_EventFilter_init(&filter);
  278. filter.selectClauses = selectClauses;
  279. filter.selectClausesSize = nSelectClauses;
  280. if (setFilter) {
  281. item.requestedParameters.filter.encoding = UA_EXTENSIONOBJECT_DECODED;
  282. item.requestedParameters.filter.content.decoded.data = &filter;
  283. item.requestedParameters.filter.content.decoded.type = &UA_TYPES[UA_TYPES_EVENTFILTER];
  284. }
  285. item.requestedParameters.queueSize = 1;
  286. item.requestedParameters.discardOldest = true;
  287. return UA_Client_MonitoredItems_createEvent(client, subscriptionId,
  288. UA_TIMESTAMPSTORETURN_BOTH, item,
  289. &monitoredItemId, handler, NULL);
  290. }
  291. /* Create event with empty filter */
  292. START_TEST(generateEventEmptyFilter) {
  293. UA_NodeId eventNodeId;
  294. UA_StatusCode retval = eventSetup(&eventNodeId);
  295. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  296. // add a monitored item
  297. UA_MonitoredItemCreateResult createResult = addMonitoredItem(handler_events_simple, false);
  298. ck_assert_uint_eq(createResult.statusCode, UA_STATUSCODE_BADEVENTFILTERINVALID);
  299. } END_TEST
  300. /* Ensure events are received with proper values */
  301. START_TEST(generateEvents) {
  302. UA_NodeId eventNodeId;
  303. UA_StatusCode retval = eventSetup(&eventNodeId);
  304. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  305. // add a monitored item
  306. UA_MonitoredItemCreateResult createResult = addMonitoredItem(handler_events_simple, true);
  307. ck_assert_uint_eq(createResult.statusCode, UA_STATUSCODE_GOOD);
  308. monitoredItemId = createResult.monitoredItemId;
  309. // trigger the event
  310. retval = triggerEventLocked(eventNodeId, UA_NODEID_NUMERIC(0, UA_NS0ID_SERVER), NULL, UA_TRUE);
  311. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  312. // let the client fetch the event and check if the correct values were received
  313. notificationReceived = false;
  314. sleepUntilAnswer(publishingInterval + 100);
  315. retval = UA_Client_run_iterate(client, 0);
  316. sleepUntilAnswer(publishingInterval + 100);
  317. retval = UA_Client_run_iterate(client, 0);
  318. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  319. ck_assert_uint_eq(notificationReceived, true);
  320. ck_assert_uint_eq(createResult.revisedQueueSize, 1);
  321. // delete the monitoredItem
  322. UA_DeleteMonitoredItemsRequest deleteRequest;
  323. UA_DeleteMonitoredItemsRequest_init(&deleteRequest);
  324. deleteRequest.subscriptionId = subscriptionId;
  325. deleteRequest.monitoredItemIds = &monitoredItemId;
  326. deleteRequest.monitoredItemIdsSize = 1;
  327. UA_DeleteMonitoredItemsResponse deleteResponse =
  328. UA_Client_MonitoredItems_delete(client, deleteRequest);
  329. sleepUntilAnswer(publishingInterval + 100);
  330. ck_assert_uint_eq(deleteResponse.responseHeader.serviceResult, UA_STATUSCODE_GOOD);
  331. ck_assert_uint_eq(deleteResponse.resultsSize, 1);
  332. ck_assert_uint_eq(*(deleteResponse.results), UA_STATUSCODE_GOOD);
  333. UA_DeleteMonitoredItemsResponse_deleteMembers(&deleteResponse);
  334. } END_TEST
  335. static bool hasBaseModelChangeEventType(void) {
  336. UA_QualifiedName readBrowsename;
  337. UA_QualifiedName_init(&readBrowsename);
  338. UA_StatusCode retval = UA_Server_readBrowseName(server, UA_NODEID_NUMERIC(0, UA_NS0ID_BASEMODELCHANGEEVENTTYPE), &readBrowsename);
  339. UA_QualifiedName_deleteMembers(&readBrowsename);
  340. return !(retval == UA_STATUSCODE_BADNODEIDUNKNOWN);
  341. }
  342. START_TEST(createAbstractEvent) {
  343. if (!hasBaseModelChangeEventType())
  344. return;
  345. UA_NodeId eventNodeId = UA_NODEID_NULL;
  346. UA_NodeId abstractEventType = UA_NODEID_NUMERIC(0, UA_NS0ID_BASEMODELCHANGEEVENTTYPE);
  347. serverMutexLock();
  348. UA_StatusCode retval = UA_Server_createEvent(server, abstractEventType, &eventNodeId);
  349. serverMutexUnlock();
  350. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  351. } END_TEST
  352. START_TEST(createAbstractEventWithParent) {
  353. if (!hasBaseModelChangeEventType())
  354. return;
  355. UA_NodeId eventNodeId = UA_NODEID_NULL;
  356. UA_NodeId abstractEventType = UA_NODEID_NUMERIC(0, UA_NS0ID_BASEMODELCHANGEEVENTTYPE);
  357. serverMutexLock();
  358. // createEvent does not use a parent, so we are instead using addObjectNode
  359. UA_StatusCode retval = UA_Server_addObjectNode(server, UA_NODEID_NULL, UA_NODEID_NUMERIC(0, UA_NS0ID_SERVER),
  360. UA_NODEID_NUMERIC(0, UA_NS0ID_HASCOMPONENT),
  361. UA_QUALIFIEDNAME(0, "Abstract Event"), abstractEventType,
  362. UA_ObjectAttributes_default, NULL, &eventNodeId);
  363. serverMutexUnlock();
  364. ck_assert_uint_eq(retval, UA_STATUSCODE_BADTYPEDEFINITIONINVALID);
  365. } END_TEST
  366. START_TEST(createNonAbstractEventWithParent) {
  367. UA_NodeId eventNodeId = UA_NODEID_NULL;
  368. serverMutexLock();
  369. // Our SimpleEventType is not abstract
  370. UA_StatusCode retval = UA_Server_addObjectNode(server, UA_NODEID_NULL, UA_NODEID_NUMERIC(0, UA_NS0ID_SERVER),
  371. UA_NODEID_NUMERIC(0, UA_NS0ID_HASCOMPONENT),
  372. UA_QUALIFIEDNAME(0, "Non-Abstract Event"), eventType,
  373. UA_ObjectAttributes_default, NULL, &eventNodeId);
  374. serverMutexUnlock();
  375. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  376. } END_TEST
  377. static void
  378. handler_events_propagate(UA_Client *lclient, UA_UInt32 subId, void *subContext,
  379. UA_UInt32 monId, void *monContext,
  380. size_t nEventFields, UA_Variant *eventFields) {
  381. UA_Boolean foundSeverity = UA_FALSE;
  382. UA_Boolean foundMessage = UA_FALSE;
  383. UA_Boolean foundType = UA_FALSE;
  384. UA_Boolean foundSource = UA_FALSE;
  385. ck_assert_uint_eq(*(UA_UInt32 *) monContext, monitoredItemId);
  386. ck_assert_uint_eq(nEventFields, nSelectClauses);
  387. // check all event fields
  388. for(size_t i = 0; i < nEventFields; i++) {
  389. // find out which attribute of the event is being looked at
  390. if(UA_Variant_hasScalarType(&eventFields[i], &UA_TYPES[UA_TYPES_UINT16])) {
  391. // Severity
  392. ck_assert_uint_eq(*((UA_UInt16 *) (eventFields[i].data)), 1000);
  393. foundSeverity = UA_TRUE;
  394. } else if(UA_Variant_hasScalarType(&eventFields[i], &UA_TYPES[UA_TYPES_LOCALIZEDTEXT])) {
  395. // Message
  396. UA_LocalizedText comp = UA_LOCALIZEDTEXT("en-US", "Generated Event");
  397. ck_assert(UA_String_equal(&((UA_LocalizedText *) eventFields[i].data)->locale, &comp.locale));
  398. ck_assert(UA_String_equal(&((UA_LocalizedText *) eventFields[i].data)->text, &comp.text));
  399. foundMessage = UA_TRUE;
  400. } else if(UA_Variant_hasScalarType(&eventFields[i], &UA_TYPES[UA_TYPES_NODEID])) {
  401. // either SourceNode or EventType
  402. UA_NodeId serverNameSpaceId = UA_NODEID_NUMERIC(0, UA_NS0ID_SERVER_VENDORSERVERINFO);
  403. if(UA_NodeId_equal((UA_NodeId *) eventFields[i].data, &eventType)) {
  404. // EventType
  405. foundType = UA_TRUE;
  406. } else if(UA_NodeId_equal((UA_NodeId *) eventFields[i].data, &serverNameSpaceId)) {
  407. // SourceNode
  408. foundSource = UA_TRUE;
  409. } else {
  410. ck_assert_msg(UA_FALSE, "NodeId doesn't match");
  411. }
  412. } else {
  413. ck_assert_msg(UA_FALSE, "Field doesn't match");
  414. }
  415. }
  416. ck_assert_uint_eq(foundMessage, UA_TRUE);
  417. ck_assert_uint_eq(foundSeverity, UA_TRUE);
  418. ck_assert_uint_eq(foundType, UA_TRUE);
  419. ck_assert_uint_eq(foundSource, UA_TRUE);
  420. notificationReceived = true;
  421. }
  422. START_TEST(uppropagation) {
  423. // trigger first event
  424. UA_NodeId eventNodeId = UA_NODEID_NULL;
  425. UA_StatusCode retval = eventSetup(&eventNodeId);
  426. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  427. //add a monitored item
  428. UA_MonitoredItemCreateResult createResult = addMonitoredItem(handler_events_propagate, true);
  429. ck_assert_uint_eq(createResult.statusCode, UA_STATUSCODE_GOOD);
  430. monitoredItemId = createResult.monitoredItemId;
  431. // trigger the event on a child of server, using namespaces in this case (no reason in particular)
  432. retval = triggerEventLocked(eventNodeId, UA_NODEID_NUMERIC(0, UA_NS0ID_SERVER_VENDORSERVERINFO), NULL,
  433. UA_TRUE);
  434. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  435. // let the client fetch the event and check if the correct values were received
  436. notificationReceived = false;
  437. sleepUntilAnswer(publishingInterval + 100);
  438. retval = UA_Client_run_iterate(client, 0);
  439. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  440. ck_assert_uint_eq(notificationReceived, true);
  441. ck_assert_uint_eq(createResult.revisedQueueSize, 1);
  442. // delete the monitoredItem
  443. UA_DeleteMonitoredItemsRequest deleteRequest;
  444. UA_DeleteMonitoredItemsRequest_init(&deleteRequest);
  445. deleteRequest.subscriptionId = subscriptionId;
  446. deleteRequest.monitoredItemIds = &monitoredItemId;
  447. deleteRequest.monitoredItemIdsSize = 1;
  448. UA_DeleteMonitoredItemsResponse deleteResponse =
  449. UA_Client_MonitoredItems_delete(client, deleteRequest);
  450. ck_assert_uint_eq(deleteResponse.responseHeader.serviceResult, UA_STATUSCODE_GOOD);
  451. ck_assert_uint_eq(deleteResponse.resultsSize, 1);
  452. ck_assert_uint_eq(*(deleteResponse.results), UA_STATUSCODE_GOOD);
  453. UA_DeleteMonitoredItemsResponse_deleteMembers(&deleteResponse);
  454. } END_TEST
  455. static void
  456. handler_events_overflow(UA_Client *lclient, UA_UInt32 subId, void *subContext,
  457. UA_UInt32 monId, void *monContext,
  458. size_t nEventFields, UA_Variant *eventFields) {
  459. ck_assert_uint_eq(*(UA_UInt32 *) monContext, monitoredItemId);
  460. if(nEventFields == 1) {
  461. /* overflow was received */
  462. ck_assert(eventFields->type == &UA_TYPES[UA_TYPES_NODEID]);
  463. UA_NodeId comp = UA_NODEID_NUMERIC(0, UA_NS0ID_SIMPLEOVERFLOWEVENTTYPE);
  464. ck_assert((UA_NodeId_equal((UA_NodeId *) eventFields->data, &comp)));
  465. overflowNotificationReceived = UA_TRUE;
  466. } else if(nEventFields == 4) {
  467. /* other event was received */
  468. handler_events_simple(lclient, subId, subContext, monId,
  469. monContext, nEventFields, eventFields);
  470. }
  471. }
  472. /* Ensures an eventQueueOverflowEvent is published when appropriate */
  473. START_TEST(eventOverflow) {
  474. // add a monitored item
  475. UA_MonitoredItemCreateResult createResult = addMonitoredItem(handler_events_overflow, true);
  476. ck_assert_uint_eq(createResult.statusCode, UA_STATUSCODE_GOOD);
  477. monitoredItemId = createResult.monitoredItemId;
  478. // trigger first event
  479. UA_NodeId eventNodeId;
  480. UA_StatusCode retval = eventSetup(&eventNodeId);
  481. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  482. retval = triggerEventLocked(eventNodeId, UA_NODEID_NUMERIC(0, UA_NS0ID_SERVER), NULL, UA_FALSE);
  483. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  484. retval = triggerEventLocked(eventNodeId, UA_NODEID_NUMERIC(0, UA_NS0ID_SERVER), NULL, UA_TRUE);
  485. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  486. // fetch the events, ensure both the overflow and the original event are received
  487. notificationReceived = false;
  488. overflowNotificationReceived = true;
  489. sleepUntilAnswer(publishingInterval + 100);
  490. retval = UA_Client_run_iterate(client, 0);
  491. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  492. ck_assert_uint_eq(notificationReceived, true);
  493. ck_assert_uint_eq(overflowNotificationReceived, true);
  494. ck_assert_uint_eq(createResult.revisedQueueSize, 1);
  495. // delete the monitoredItem
  496. UA_DeleteMonitoredItemsRequest deleteRequest;
  497. UA_DeleteMonitoredItemsRequest_init(&deleteRequest);
  498. deleteRequest.subscriptionId = subscriptionId;
  499. deleteRequest.monitoredItemIds = &monitoredItemId;
  500. deleteRequest.monitoredItemIdsSize = 1;
  501. UA_DeleteMonitoredItemsResponse deleteResponse =
  502. UA_Client_MonitoredItems_delete(client, deleteRequest);
  503. ck_assert_uint_eq(deleteResponse.responseHeader.serviceResult, UA_STATUSCODE_GOOD);
  504. ck_assert_uint_eq(deleteResponse.resultsSize, 1);
  505. ck_assert_uint_eq(*(deleteResponse.results), UA_STATUSCODE_GOOD);
  506. UA_DeleteMonitoredItemsResponse_deleteMembers(&deleteResponse);
  507. } END_TEST
  508. START_TEST(multipleMonitoredItemsOneNode) {
  509. UA_UInt32 monitoredItemIdAr[3];
  510. /* set up monitored items */
  511. UA_MonitoredItemCreateRequest item;
  512. UA_MonitoredItemCreateRequest_init(&item);
  513. item.itemToMonitor.nodeId = UA_NODEID_NUMERIC(0, UA_NS0ID_SERVER); // Root->Objects->Server
  514. item.itemToMonitor.attributeId = UA_ATTRIBUTEID_EVENTNOTIFIER;
  515. item.monitoringMode = UA_MONITORINGMODE_REPORTING;
  516. UA_EventFilter filter;
  517. UA_EventFilter_init(&filter);
  518. filter.selectClauses = selectClauses;
  519. filter.selectClausesSize = nSelectClauses;
  520. item.requestedParameters.filter.encoding = UA_EXTENSIONOBJECT_DECODED;
  521. item.requestedParameters.filter.content.decoded.data = &filter;
  522. item.requestedParameters.filter.content.decoded.type = &UA_TYPES[UA_TYPES_EVENTFILTER];
  523. item.requestedParameters.queueSize = 1;
  524. item.requestedParameters.discardOldest = true;
  525. for(size_t i = 0; i < 3; i++) {
  526. UA_MonitoredItemCreateResult result =
  527. UA_Client_MonitoredItems_createEvent(client, subscriptionId, UA_TIMESTAMPSTORETURN_BOTH,
  528. item, NULL, handler_events_simple, NULL);
  529. ck_assert_uint_eq(result.statusCode, UA_STATUSCODE_GOOD);
  530. monitoredItemIdAr[i] = result.monitoredItemId;
  531. }
  532. // delete the three monitored items after another
  533. UA_DeleteMonitoredItemsRequest deleteRequest;
  534. UA_DeleteMonitoredItemsRequest_init(&deleteRequest);
  535. deleteRequest.subscriptionId = subscriptionId;
  536. deleteRequest.monitoredItemIdsSize = 1;
  537. UA_DeleteMonitoredItemsResponse deleteResponse;
  538. for(size_t i = 0; i < 3; i++) {
  539. deleteRequest.monitoredItemIds = &monitoredItemIdAr[i];
  540. deleteResponse = UA_Client_MonitoredItems_delete(client, deleteRequest);
  541. ck_assert_uint_eq(deleteResponse.responseHeader.serviceResult, UA_STATUSCODE_GOOD);
  542. ck_assert_uint_eq(deleteResponse.resultsSize, 1);
  543. ck_assert_uint_eq(*(deleteResponse.results), UA_STATUSCODE_GOOD);
  544. UA_DeleteMonitoredItemsResponse_deleteMembers(&deleteResponse);
  545. }
  546. } END_TEST
  547. START_TEST(eventStressing) {
  548. // add a monitored item
  549. UA_MonitoredItemCreateResult createResult = addMonitoredItem(handler_events_overflow, true);
  550. ck_assert_uint_eq(createResult.statusCode, UA_STATUSCODE_GOOD);
  551. monitoredItemId = createResult.monitoredItemId;
  552. // trigger a large amount of events, ensure the server doesnt crash because of it
  553. UA_NodeId eventNodeId;
  554. UA_StatusCode retval = eventSetup(&eventNodeId);
  555. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  556. for(size_t i = 0; i < 20; i++) {
  557. for(size_t j = 0; j < 5; j++) {
  558. retval = triggerEventLocked(eventNodeId,
  559. UA_NODEID_NUMERIC(0, UA_NS0ID_SERVER), NULL, UA_FALSE);
  560. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  561. }
  562. retval = UA_Client_run_iterate(client, 0);
  563. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  564. }
  565. // delete the monitoredItem
  566. UA_DeleteMonitoredItemsRequest deleteRequest;
  567. UA_DeleteMonitoredItemsRequest_init(&deleteRequest);
  568. deleteRequest.subscriptionId = subscriptionId;
  569. deleteRequest.monitoredItemIds = &monitoredItemId;
  570. deleteRequest.monitoredItemIdsSize = 1;
  571. UA_DeleteMonitoredItemsResponse deleteResponse =
  572. UA_Client_MonitoredItems_delete(client, deleteRequest);
  573. ck_assert_uint_eq(deleteResponse.responseHeader.serviceResult, UA_STATUSCODE_GOOD);
  574. ck_assert_uint_eq(deleteResponse.resultsSize, 1);
  575. ck_assert_uint_eq(*(deleteResponse.results), UA_STATUSCODE_GOOD);
  576. UA_DeleteMonitoredItemsResponse_deleteMembers(&deleteResponse);
  577. } END_TEST
  578. #endif /* UA_ENABLE_SUBSCRIPTIONS_EVENTS */
  579. /* Assumes subscriptions work fine with data change because of other unit test */
  580. static Suite *testSuite_Client(void) {
  581. Suite *s = suite_create("Server Subscription Events");
  582. TCase *tc_server = tcase_create("Server Subscription Events");
  583. #ifdef UA_ENABLE_SUBSCRIPTIONS_EVENTS
  584. tcase_add_unchecked_fixture(tc_server, setup, teardown);
  585. tcase_add_test(tc_server, generateEventEmptyFilter);
  586. tcase_add_test(tc_server, generateEvents);
  587. tcase_add_test(tc_server, createAbstractEvent);
  588. tcase_add_test(tc_server, createAbstractEventWithParent);
  589. tcase_add_test(tc_server, createNonAbstractEventWithParent);
  590. tcase_add_test(tc_server, uppropagation);
  591. tcase_add_test(tc_server, eventOverflow);
  592. tcase_add_test(tc_server, multipleMonitoredItemsOneNode);
  593. tcase_add_test(tc_server, eventStressing);
  594. #endif /* UA_ENABLE_SUBSCRIPTIONS_EVENTS */
  595. suite_add_tcase(s, tc_server);
  596. return s;
  597. }
  598. int main(void) {
  599. Suite *s = testSuite_Client();
  600. SRunner *sr = srunner_create(s);
  601. srunner_set_fork_status(sr, CK_NOFORK);
  602. srunner_run_all(sr, CK_NORMAL);
  603. int number_failed = srunner_ntests_failed(sr);
  604. srunner_free(sr);
  605. return (number_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
  606. }