check_server_historical_data.c 42 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022
  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. *
  5. * Copyright 2018 (c) basysKom GmbH <opensource@basyskom.com> (Author: Peter Rustler)
  6. */
  7. #include "ua_types.h"
  8. #include "ua_server.h"
  9. #include "server/ua_server_internal.h"
  10. #include "ua_client.h"
  11. #include "client/ua_client_internal.h"
  12. #include "ua_client_highlevel.h"
  13. #include "ua_config_default.h"
  14. #include "ua_network_tcp.h"
  15. #include "check.h"
  16. #include "testing_clock.h"
  17. #include "testing_networklayers.h"
  18. #include "thread_wrapper.h"
  19. #include "ua_plugin_historydatabase.h"
  20. #include "ua_historydatabase_default.h"
  21. #include "ua_plugin_history_data_gathering.h"
  22. #include "ua_historydatabackend_memory.h"
  23. #include "ua_historydatagathering_default.h"
  24. #ifdef UA_ENABLE_HISTORIZING
  25. #include "historical_read_test_data.h"
  26. #endif
  27. #include <stddef.h>
  28. static UA_Server *server;
  29. static UA_ServerConfig *config;
  30. #ifdef UA_ENABLE_HISTORIZING
  31. static UA_HistoryDataGathering *gathering;
  32. #endif
  33. static UA_Boolean running;
  34. static THREAD_HANDLE server_thread;
  35. static MUTEX_HANDLE serverMutex;
  36. static UA_Client *client;
  37. static UA_NodeId parentNodeId;
  38. static UA_NodeId parentReferenceNodeId;
  39. static UA_NodeId outNodeId;
  40. static UA_DateTime *testDataSorted;
  41. static void serverMutexLock(void) {
  42. if (!(MUTEX_LOCK(serverMutex))) {
  43. fprintf(stderr, "Mutex cannot be locked.\n");
  44. exit(1);
  45. }
  46. }
  47. static void serverMutexUnlock(void) {
  48. if (!(MUTEX_UNLOCK(serverMutex))) {
  49. fprintf(stderr, "Mutex cannot be unlocked.\n");
  50. exit(1);
  51. }
  52. }
  53. THREAD_CALLBACK(serverloop)
  54. {
  55. while(running) {
  56. serverMutexLock();
  57. UA_Server_run_iterate(server, false);
  58. serverMutexUnlock();
  59. }
  60. return 0;
  61. }
  62. static void
  63. setup(void)
  64. {
  65. if (!(MUTEX_INIT(serverMutex))) {
  66. fprintf(stderr, "Server mutex was not created correctly.\n");
  67. exit(1);
  68. }
  69. running = true;
  70. config = UA_ServerConfig_new_default();
  71. #ifdef UA_ENABLE_HISTORIZING
  72. gathering = (UA_HistoryDataGathering*)UA_calloc(1, sizeof(UA_HistoryDataGathering));
  73. *gathering = UA_HistoryDataGathering_Default(1);
  74. config->historyDatabase = UA_HistoryDatabase_default(*gathering);
  75. #endif
  76. server = UA_Server_new(config);
  77. UA_StatusCode retval = UA_Server_run_startup(server);
  78. if (retval != UA_STATUSCODE_GOOD)
  79. {
  80. fprintf(stderr, "Error while calling Server_run_startup. %s\n", UA_StatusCode_name(retval));
  81. exit(1);
  82. }
  83. THREAD_CREATE(server_thread, serverloop);
  84. /* Define the attribute of the uint32 variable node */
  85. UA_VariableAttributes attr = UA_VariableAttributes_default;
  86. UA_UInt32 myUint32 = 40;
  87. UA_Variant_setScalar(&attr.value, &myUint32, &UA_TYPES[UA_TYPES_UINT32]);
  88. attr.description = UA_LOCALIZEDTEXT("en-US","the answer");
  89. attr.displayName = UA_LOCALIZEDTEXT("en-US","the answer");
  90. attr.dataType = UA_TYPES[UA_TYPES_UINT32].typeId;
  91. attr.accessLevel = UA_ACCESSLEVELMASK_READ | UA_ACCESSLEVELMASK_WRITE | UA_ACCESSLEVELMASK_HISTORYREAD | UA_ACCESSLEVELMASK_HISTORYWRITE;
  92. attr.historizing = true;
  93. /* Add the variable node to the information model */
  94. UA_NodeId uint32NodeId = UA_NODEID_STRING(1, "the.answer");
  95. UA_QualifiedName uint32Name = UA_QUALIFIEDNAME(1, "the answer");
  96. parentNodeId = UA_NODEID_NUMERIC(0, UA_NS0ID_OBJECTSFOLDER);
  97. parentReferenceNodeId = UA_NODEID_NUMERIC(0, UA_NS0ID_ORGANIZES);
  98. UA_NodeId_init(&outNodeId);
  99. retval = UA_Server_addVariableNode(server,
  100. uint32NodeId,
  101. parentNodeId,
  102. parentReferenceNodeId,
  103. uint32Name,
  104. UA_NODEID_NUMERIC(0, UA_NS0ID_BASEDATAVARIABLETYPE),
  105. attr,
  106. NULL,
  107. &outNodeId);
  108. if (retval != UA_STATUSCODE_GOOD)
  109. {
  110. fprintf(stderr, "Error adding variable node. %s\n", UA_StatusCode_name(retval));
  111. exit(1);
  112. }
  113. client = UA_Client_new(UA_ClientConfig_default);
  114. retval = UA_Client_connect(client, "opc.tcp://localhost:4840");
  115. if (retval != UA_STATUSCODE_GOOD)
  116. {
  117. fprintf(stderr, "Client can not connect to opc.tcp://localhost:4840. %s\n", UA_StatusCode_name(retval));
  118. exit(1);
  119. }
  120. UA_Client_recv = client->connection.recv;
  121. client->connection.recv = UA_Client_recvTesting;
  122. }
  123. static void
  124. teardown(void)
  125. {
  126. /* cleanup */
  127. UA_Client_disconnect(client);
  128. UA_Client_delete(client);
  129. running = false;
  130. THREAD_JOIN(server_thread);
  131. UA_NodeId_deleteMembers(&parentNodeId);
  132. UA_NodeId_deleteMembers(&parentReferenceNodeId);
  133. UA_NodeId_deleteMembers(&outNodeId);
  134. UA_Server_run_shutdown(server);
  135. UA_Server_delete(server);
  136. UA_ServerConfig_delete(config);
  137. #ifdef UA_ENABLE_HISTORIZING
  138. UA_free(gathering);
  139. #endif
  140. if (!MUTEX_DESTROY(serverMutex)) {
  141. fprintf(stderr, "Server mutex was not destroyed correctly.\n");
  142. exit(1);
  143. }
  144. }
  145. #ifdef UA_ENABLE_HISTORIZING
  146. #include <stdio.h>
  147. #include "ua_session.h"
  148. static UA_StatusCode
  149. setUInt32(UA_Client *thisClient, UA_NodeId node, UA_UInt32 value)
  150. {
  151. UA_Variant variant;
  152. UA_Variant_setScalar(&variant, &value, &UA_TYPES[UA_TYPES_UINT32]);
  153. return UA_Client_writeValueAttribute(thisClient, node, &variant);
  154. }
  155. static UA_DateTime* sortDateTimes(UA_DateTime *data) {
  156. size_t count = 0;
  157. while(data[count++]);
  158. UA_DateTime* ret;
  159. if (UA_Array_copy(data, count, (void**)&ret, &UA_TYPES[UA_TYPES_DATETIME]) != UA_STATUSCODE_GOOD)
  160. return NULL;
  161. --count;
  162. // sort it
  163. for (size_t i = 1; i < count; i++) {
  164. for (size_t j = 0; j < count - i; j++) {
  165. if (ret[j] > ret[j+1]) {
  166. UA_DateTime tmp = ret[j];
  167. ret[j] = ret[j+1];
  168. ret[j+1] = tmp;
  169. }
  170. }
  171. }
  172. return ret;
  173. }
  174. static void
  175. printTimestamp(UA_DateTime timestamp)
  176. {
  177. if (timestamp == TIMESTAMP_FIRST) {
  178. fprintf(stderr, "FIRST,");
  179. } else if (timestamp == TIMESTAMP_LAST) {
  180. fprintf(stderr, "LAST,");
  181. } else {
  182. fprintf(stderr, "%3lld,", timestamp / UA_DATETIME_SEC);
  183. }
  184. }
  185. static void
  186. printResult(UA_DataValue * value)
  187. {
  188. if (value->status != UA_STATUSCODE_GOOD)
  189. fprintf(stderr, "%s:", UA_StatusCode_name(value->status));
  190. printTimestamp(value->sourceTimestamp);
  191. }
  192. static UA_Boolean
  193. resultIsEqual(const UA_DataValue * result, const testTuple * tuple, size_t index)
  194. {
  195. switch (tuple->result[index]) {
  196. case TIMESTAMP_FIRST:
  197. if (result->status != UA_STATUSCODE_BADBOUNDNOTFOUND
  198. || !UA_Variant_isEmpty(&result->value))
  199. return false;
  200. /* we do not test timestamp if TIMESTAMP_UNSPECIFIED is given for start.
  201. * See OPC UA Part 11, Version 1.03, Page 5-6, Table 1, Mark b for details.*/
  202. if (tuple->start != TIMESTAMP_UNSPECIFIED
  203. && tuple->start != result->sourceTimestamp)
  204. return false;
  205. break;
  206. case TIMESTAMP_LAST:
  207. if (result->status != UA_STATUSCODE_BADBOUNDNOTFOUND
  208. || !UA_Variant_isEmpty(&result->value))
  209. return false;
  210. /* we do not test timestamp if TIMESTAMP_UNSPECIFIED is given for end.
  211. * See OPC UA Part 11, Version 1.03, Page 5-6, Table 1, Mark a for details.*/
  212. if (tuple->end != TIMESTAMP_UNSPECIFIED
  213. && tuple->end != result->sourceTimestamp)
  214. return false;
  215. break;
  216. default:
  217. if (result->sourceTimestamp != tuple->result[index]
  218. || result->value.type != &UA_TYPES[UA_TYPES_INT64]
  219. || *((UA_Int64*)result->value.data) != tuple->result[index])
  220. return false;
  221. }
  222. return true;
  223. }
  224. static UA_Boolean
  225. fillHistoricalDataBackend(UA_HistoryDataBackend backend)
  226. {
  227. int i = 0;
  228. UA_DateTime currentDateTime = testData[i];
  229. fprintf(stderr, "Adding to historical data backend: ");
  230. while (currentDateTime) {
  231. fprintf(stderr, "%lld, ", currentDateTime / UA_DATETIME_SEC);
  232. UA_DataValue value;
  233. UA_DataValue_init(&value);
  234. value.hasValue = true;
  235. UA_Int64 d = currentDateTime;
  236. UA_Variant_setScalarCopy(&value.value, &d, &UA_TYPES[UA_TYPES_INT64]);
  237. value.hasSourceTimestamp = true;
  238. value.sourceTimestamp = currentDateTime;
  239. value.hasServerTimestamp = true;
  240. value.serverTimestamp = currentDateTime;
  241. value.hasStatus = true;
  242. value.status = UA_STATUSCODE_GOOD;
  243. if (backend.serverSetHistoryData(server, backend.context, NULL, NULL, &outNodeId, UA_FALSE, &value) != UA_STATUSCODE_GOOD) {
  244. fprintf(stderr, "\n");
  245. return false;
  246. }
  247. UA_DataValue_deleteMembers(&value);
  248. currentDateTime = testData[++i];
  249. }
  250. fprintf(stderr, "\n");
  251. return true;
  252. }
  253. void
  254. Service_HistoryRead(UA_Server *server, UA_Session *session,
  255. const UA_HistoryReadRequest *request,
  256. UA_HistoryReadResponse *response);
  257. static void
  258. requestHistory(UA_DateTime start,
  259. UA_DateTime end,
  260. UA_HistoryReadResponse * response,
  261. UA_UInt32 numValuesPerNode,
  262. UA_Boolean returnBounds,
  263. UA_ByteString *continuationPoint)
  264. {
  265. UA_ReadRawModifiedDetails *details = UA_ReadRawModifiedDetails_new();
  266. details->startTime = start;
  267. details->endTime = end;
  268. details->isReadModified = false;
  269. details->numValuesPerNode = numValuesPerNode;
  270. details->returnBounds = returnBounds;
  271. UA_HistoryReadValueId *valueId = UA_HistoryReadValueId_new();
  272. UA_NodeId_copy(&outNodeId, &valueId->nodeId);
  273. if (continuationPoint)
  274. UA_ByteString_copy(continuationPoint, &valueId->continuationPoint);
  275. UA_HistoryReadRequest request;
  276. UA_HistoryReadRequest_init(&request);
  277. request.historyReadDetails.encoding = UA_EXTENSIONOBJECT_DECODED;
  278. request.historyReadDetails.content.decoded.type = &UA_TYPES[UA_TYPES_READRAWMODIFIEDDETAILS];
  279. request.historyReadDetails.content.decoded.data = details;
  280. request.timestampsToReturn = UA_TIMESTAMPSTORETURN_BOTH;
  281. request.nodesToReadSize = 1;
  282. request.nodesToRead = valueId;
  283. Service_HistoryRead(server, &server->adminSession, &request, response);
  284. UA_HistoryReadRequest_deleteMembers(&request);
  285. }
  286. static UA_UInt32
  287. testHistoricalDataBackend(size_t maxResponseSize)
  288. {
  289. const UA_HistorizingNodeIdSettings* setting = gathering->getHistorizingSetting(server, gathering->context, &outNodeId);
  290. UA_HistorizingNodeIdSettings newSetting = *setting;
  291. newSetting.maxHistoryDataResponseSize = maxResponseSize;
  292. gathering->updateNodeIdSetting(server, gathering->context, &outNodeId, newSetting);
  293. UA_UInt32 retval = 0;
  294. size_t i = 0;
  295. testTuple *current = &testRequests[i];
  296. fprintf(stderr, "Testing with maxResponseSize of %lu\n", maxResponseSize);
  297. fprintf(stderr, "Start | End | numValuesPerNode | returnBounds |ContPoint| {Expected}{Result} Result\n");
  298. fprintf(stderr, "------+------+------------------+--------------+---------+----------------\n");
  299. size_t j;
  300. while (current->start || current->end) {
  301. j = 0;
  302. if (current->start == TIMESTAMP_UNSPECIFIED) {
  303. fprintf(stderr, "UNSPEC|");
  304. } else {
  305. fprintf(stderr, " %3lld |", current->start / UA_DATETIME_SEC);
  306. }
  307. if (current->end == TIMESTAMP_UNSPECIFIED) {
  308. fprintf(stderr, "UNSPEC|");
  309. } else {
  310. fprintf(stderr, " %3lld |", current->end / UA_DATETIME_SEC);
  311. }
  312. fprintf(stderr, " %2u | %s | %s | {", current->numValuesPerNode, (current->returnBounds ? "Yes" : " No"), (current->returnContinuationPoint ? "Yes" : " No"));
  313. while (current->result[j]) {
  314. printTimestamp(current->result[j]);
  315. ++j;
  316. }
  317. fprintf(stderr, "}");
  318. UA_DataValue *result = NULL;
  319. size_t resultSize = 0;
  320. UA_ByteString continuous;
  321. UA_ByteString_init(&continuous);
  322. UA_Boolean readOk = true;
  323. size_t reseivedValues = 0;
  324. fprintf(stderr, "{");
  325. size_t counter = 0;
  326. do {
  327. UA_HistoryReadResponse response;
  328. UA_HistoryReadResponse_init(&response);
  329. UA_UInt32 numValuesPerNode = current->numValuesPerNode;
  330. if (numValuesPerNode > 0 && numValuesPerNode + (UA_UInt32)reseivedValues > current->numValuesPerNode)
  331. numValuesPerNode = current->numValuesPerNode - (UA_UInt32)reseivedValues;
  332. requestHistory(current->start,
  333. current->end,
  334. &response,
  335. numValuesPerNode,
  336. current->returnBounds,
  337. &continuous);
  338. ++counter;
  339. if(response.resultsSize != 1) {
  340. fprintf(stderr, "ResultError:Size %lu %s", response.resultsSize, UA_StatusCode_name(response.responseHeader.serviceResult));
  341. readOk = false;
  342. UA_HistoryReadResponse_deleteMembers(&response);
  343. break;
  344. }
  345. UA_StatusCode stat = response.results[0].statusCode;
  346. if (stat == UA_STATUSCODE_BADBOUNDNOTSUPPORTED && current->returnBounds) {
  347. fprintf(stderr, "%s", UA_StatusCode_name(stat));
  348. UA_HistoryReadResponse_deleteMembers(&response);
  349. break;
  350. }
  351. if(response.results[0].historyData.encoding != UA_EXTENSIONOBJECT_DECODED
  352. || response.results[0].historyData.content.decoded.type != &UA_TYPES[UA_TYPES_HISTORYDATA]) {
  353. fprintf(stderr, "ResultError:HistoryData");
  354. readOk = false;
  355. UA_HistoryReadResponse_deleteMembers(&response);
  356. break;
  357. }
  358. UA_HistoryData * data = (UA_HistoryData *)response.results[0].historyData.content.decoded.data;
  359. resultSize = data->dataValuesSize;
  360. result = data->dataValues;
  361. if (resultSize == 0 && continuous.length > 0) {
  362. fprintf(stderr, "continuousResultEmpty");
  363. readOk = false;
  364. UA_HistoryReadResponse_deleteMembers(&response);
  365. break;
  366. }
  367. if (resultSize > maxResponseSize) {
  368. fprintf(stderr, "resultToBig");
  369. readOk = false;
  370. UA_HistoryReadResponse_deleteMembers(&response);
  371. break;
  372. }
  373. if (stat != UA_STATUSCODE_GOOD) {
  374. fprintf(stderr, "%s", UA_StatusCode_name(stat));
  375. } else {
  376. for (size_t k = 0; k < resultSize; ++k)
  377. printResult(&result[k]);
  378. }
  379. if (stat == UA_STATUSCODE_GOOD && j >= resultSize + reseivedValues) {
  380. for (size_t l = 0; l < resultSize; ++l) {
  381. /* See OPC UA Part 11, Version 1.03, Page 5-6, Table 1, Mark a for details.*/
  382. if (current->result[l + reseivedValues] == TIMESTAMP_LAST && current->end == TIMESTAMP_UNSPECIFIED) {
  383. // This test will work on not continous read, only
  384. if (reseivedValues == 0 && !(l > 0 && result[l].sourceTimestamp == result[l-1].sourceTimestamp + UA_DATETIME_SEC))
  385. readOk = false;
  386. }
  387. /* See OPC UA Part 11, Version 1.03, Page 5-6, Table 1, Mark b for details.*/
  388. if (current->result[l + reseivedValues] == TIMESTAMP_FIRST && current->start == TIMESTAMP_UNSPECIFIED) {
  389. // This test will work on not continous read, only
  390. if (reseivedValues == 0 && !(l > 0 && result[l].sourceTimestamp == result[l-1].sourceTimestamp - UA_DATETIME_SEC))
  391. readOk = false;
  392. }
  393. if (!resultIsEqual(&result[l], current, l + reseivedValues))
  394. readOk = false;
  395. }
  396. if (response.results[0].continuationPoint.length > 0)
  397. fprintf(stderr, "C,");
  398. reseivedValues += resultSize;
  399. if (reseivedValues == j) {
  400. if (current->returnContinuationPoint && response.results[0].continuationPoint.length == 0) {
  401. readOk = false;
  402. fprintf(stderr, "missingContinuationPoint");
  403. }
  404. if (!current->returnContinuationPoint && response.results[0].continuationPoint.length > 0) {
  405. readOk = false;
  406. fprintf(stderr, "unexpectedContinuationPoint");
  407. }
  408. UA_HistoryReadResponse_deleteMembers(&response);
  409. break;
  410. }
  411. UA_ByteString_deleteMembers(&continuous);
  412. UA_ByteString_copy(&response.results[0].continuationPoint, &continuous);
  413. } else {
  414. readOk = false;
  415. UA_HistoryReadResponse_deleteMembers(&response);
  416. break;
  417. }
  418. UA_HistoryReadResponse_deleteMembers(&response);
  419. } while (continuous.length > 0);
  420. if (j != reseivedValues) {
  421. readOk = false;
  422. }
  423. UA_ByteString_deleteMembers(&continuous);
  424. if (!readOk) {
  425. fprintf(stderr, "} Fail (%lu requests)\n", counter);
  426. ++retval;
  427. } else {
  428. fprintf(stderr, "} OK (%lu requests)\n", counter);
  429. }
  430. current = &testRequests[++i];
  431. }
  432. return retval;
  433. }
  434. void
  435. Service_HistoryUpdate(UA_Server *server, UA_Session *session,
  436. const UA_HistoryUpdateRequest *request,
  437. UA_HistoryUpdateResponse *response);
  438. static UA_StatusCode
  439. deleteHistory(UA_DateTime start,
  440. UA_DateTime end)
  441. {
  442. UA_DeleteRawModifiedDetails *details = UA_DeleteRawModifiedDetails_new();
  443. details->startTime = start;
  444. details->endTime = end;
  445. details->isDeleteModified = false;
  446. UA_NodeId_copy(&outNodeId, &details->nodeId);
  447. UA_HistoryUpdateRequest request;
  448. UA_HistoryUpdateRequest_init(&request);
  449. request.historyUpdateDetailsSize = 1;
  450. request.historyUpdateDetails = UA_ExtensionObject_new();
  451. UA_ExtensionObject_init(request.historyUpdateDetails);
  452. request.historyUpdateDetails[0].encoding = UA_EXTENSIONOBJECT_DECODED;
  453. request.historyUpdateDetails[0].content.decoded.type = &UA_TYPES[UA_TYPES_DELETERAWMODIFIEDDETAILS];
  454. request.historyUpdateDetails[0].content.decoded.data = details;
  455. UA_HistoryUpdateResponse response;
  456. UA_HistoryUpdateResponse_init(&response);
  457. Service_HistoryUpdate(server, &server->adminSession, &request, &response);
  458. UA_HistoryUpdateRequest_deleteMembers(&request);
  459. UA_StatusCode ret = UA_STATUSCODE_GOOD;
  460. if (response.responseHeader.serviceResult != UA_STATUSCODE_GOOD)
  461. ret = response.responseHeader.serviceResult;
  462. else if (response.resultsSize != 1)
  463. ret = UA_STATUSCODE_BADUNEXPECTEDERROR;
  464. else if (response.results[0].statusCode != UA_STATUSCODE_GOOD)
  465. ret = response.results[0].statusCode;
  466. else if (response.results[0].operationResultsSize != 0)
  467. ret = UA_STATUSCODE_BADUNEXPECTEDERROR;
  468. UA_HistoryUpdateResponse_deleteMembers(&response);
  469. return ret;
  470. }
  471. static UA_StatusCode
  472. updateHistory(UA_PerformUpdateType updateType, UA_DateTime *updateData, UA_StatusCode ** operationResults, size_t *operationResultsSize)
  473. {
  474. UA_UpdateDataDetails *details = UA_UpdateDataDetails_new();
  475. details->performInsertReplace = updateType;
  476. UA_NodeId_copy(&outNodeId, &details->nodeId);
  477. int updateDataSize = -1;
  478. while(updateData[++updateDataSize]);
  479. fprintf(stderr, "updateHistory for %d values.\n", updateDataSize);
  480. details->updateValuesSize = (size_t)updateDataSize;
  481. details->updateValues = (UA_DataValue*)UA_Array_new(details->updateValuesSize, &UA_TYPES[UA_TYPES_DATAVALUE]);
  482. for (size_t i = 0; i < details->updateValuesSize; ++i) {
  483. UA_DataValue_init(&details->updateValues[i]);
  484. details->updateValues[i].hasValue = true;
  485. UA_Int64 d = updateType;
  486. UA_Variant_setScalarCopy(&details->updateValues[i].value, &d, &UA_TYPES[UA_TYPES_INT64]);
  487. details->updateValues[i].hasSourceTimestamp = true;
  488. details->updateValues[i].sourceTimestamp = updateData[i];
  489. details->updateValues[i].hasServerTimestamp = true;
  490. details->updateValues[i].serverTimestamp = updateData[i];
  491. details->updateValues[i].hasStatus = true;
  492. details->updateValues[i].status = UA_STATUSCODE_GOOD;
  493. }
  494. UA_HistoryUpdateRequest request;
  495. UA_HistoryUpdateRequest_init(&request);
  496. request.historyUpdateDetailsSize = 1;
  497. request.historyUpdateDetails = UA_ExtensionObject_new();
  498. UA_ExtensionObject_init(request.historyUpdateDetails);
  499. request.historyUpdateDetails[0].encoding = UA_EXTENSIONOBJECT_DECODED;
  500. request.historyUpdateDetails[0].content.decoded.type = &UA_TYPES[UA_TYPES_UPDATEDATADETAILS];
  501. request.historyUpdateDetails[0].content.decoded.data = details;
  502. UA_HistoryUpdateResponse response;
  503. UA_HistoryUpdateResponse_init(&response);
  504. Service_HistoryUpdate(server, &server->adminSession, &request, &response);
  505. UA_HistoryUpdateRequest_deleteMembers(&request);
  506. UA_StatusCode ret = UA_STATUSCODE_GOOD;
  507. if (response.responseHeader.serviceResult != UA_STATUSCODE_GOOD)
  508. ret = response.responseHeader.serviceResult;
  509. else if (response.resultsSize != 1)
  510. ret = UA_STATUSCODE_BADUNEXPECTEDERROR;
  511. else if (response.results[0].statusCode != UA_STATUSCODE_GOOD)
  512. ret = response.results[0].statusCode;
  513. else if (response.results[0].operationResultsSize != (size_t)updateDataSize)
  514. ret = UA_STATUSCODE_BADUNEXPECTEDERROR;
  515. else {
  516. if (operationResults) {
  517. *operationResultsSize = response.results[0].operationResultsSize;
  518. ret = UA_Array_copy(response.results[0].operationResults, *operationResultsSize, (void**)operationResults, &UA_TYPES[UA_TYPES_STATUSCODE]);
  519. } else {
  520. for (size_t i = 0; i < response.results[0].operationResultsSize; ++i) {
  521. if (response.results[0].operationResults[i] != UA_STATUSCODE_GOOD) {
  522. ret = response.results[0].operationResults[i];
  523. break;
  524. }
  525. }
  526. }
  527. }
  528. UA_HistoryUpdateResponse_deleteMembers(&response);
  529. return ret;
  530. }
  531. static void
  532. testResult(UA_DateTime *resultData, UA_HistoryData * historyData) {
  533. // request
  534. UA_HistoryReadResponse localResponse;
  535. UA_HistoryReadResponse_init(&localResponse);
  536. requestHistory(TIMESTAMP_FIRST, TIMESTAMP_LAST, &localResponse, 0, false, NULL);
  537. // test the response
  538. ck_assert_str_eq(UA_StatusCode_name(localResponse.responseHeader.serviceResult), UA_StatusCode_name(UA_STATUSCODE_GOOD));
  539. ck_assert_uint_eq(localResponse.resultsSize, 1);
  540. ck_assert_str_eq(UA_StatusCode_name(localResponse.results[0].statusCode), UA_StatusCode_name(UA_STATUSCODE_GOOD));
  541. ck_assert_uint_eq(localResponse.results[0].historyData.encoding, UA_EXTENSIONOBJECT_DECODED);
  542. ck_assert(localResponse.results[0].historyData.content.decoded.type == &UA_TYPES[UA_TYPES_HISTORYDATA]);
  543. UA_HistoryData * data = (UA_HistoryData *)localResponse.results[0].historyData.content.decoded.data;
  544. if (historyData)
  545. UA_HistoryData_copy(data, historyData);
  546. for (size_t j = 0; j < data->dataValuesSize; ++j) {
  547. ck_assert(resultData[j] != 0);
  548. ck_assert_uint_eq(data->dataValues[j].hasSourceTimestamp, true);
  549. ck_assert_uint_eq(data->dataValues[j].sourceTimestamp, resultData[j]);
  550. }
  551. UA_HistoryReadResponse_deleteMembers(&localResponse);
  552. }
  553. START_TEST(Server_HistorizingUpdateDelete)
  554. {
  555. UA_HistoryDataBackend backend = UA_HistoryDataBackend_Memory(1, 1);
  556. UA_HistorizingNodeIdSettings setting;
  557. setting.historizingBackend = backend;
  558. setting.maxHistoryDataResponseSize = 1000;
  559. setting.historizingUpdateStrategy = UA_HISTORIZINGUPDATESTRATEGY_USER;
  560. serverMutexLock();
  561. UA_StatusCode ret = gathering->registerNodeId(server, gathering->context, &outNodeId, setting);
  562. serverMutexUnlock();
  563. ck_assert_str_eq(UA_StatusCode_name(ret), UA_StatusCode_name(UA_STATUSCODE_GOOD));
  564. // fill backend
  565. ck_assert_uint_eq(fillHistoricalDataBackend(backend), true);
  566. // delete some values
  567. ck_assert_str_eq(UA_StatusCode_name(deleteHistory(DELETE_START_TIME, DELETE_STOP_TIME)),
  568. UA_StatusCode_name(UA_STATUSCODE_GOOD));
  569. testResult(testDataAfterDelete, NULL);
  570. UA_HistoryDataBackend_Memory_deleteMembers(&setting.historizingBackend);
  571. }
  572. END_TEST
  573. START_TEST(Server_HistorizingUpdateInsert)
  574. {
  575. UA_HistoryDataBackend backend = UA_HistoryDataBackend_Memory(1, 1);
  576. UA_HistorizingNodeIdSettings setting;
  577. setting.historizingBackend = backend;
  578. setting.maxHistoryDataResponseSize = 1000;
  579. setting.historizingUpdateStrategy = UA_HISTORIZINGUPDATESTRATEGY_USER;
  580. serverMutexLock();
  581. UA_StatusCode ret = gathering->registerNodeId(server, gathering->context, &outNodeId, setting);
  582. serverMutexUnlock();
  583. ck_assert_str_eq(UA_StatusCode_name(ret), UA_StatusCode_name(UA_STATUSCODE_GOOD));
  584. // fill backend with insert
  585. ck_assert_str_eq(UA_StatusCode_name(updateHistory(UA_PERFORMUPDATETYPE_INSERT, testData, NULL, NULL))
  586. , UA_StatusCode_name(UA_STATUSCODE_GOOD));
  587. UA_HistoryData data;
  588. UA_HistoryData_init(&data);
  589. testResult(testDataSorted, &data);
  590. for (size_t i = 0; i < data.dataValuesSize; ++i) {
  591. ck_assert_uint_eq(data.dataValues[i].hasValue, true);
  592. ck_assert(data.dataValues[i].value.type == &UA_TYPES[UA_TYPES_INT64]);
  593. ck_assert_uint_eq(*((UA_Int64*)data.dataValues[i].value.data), UA_PERFORMUPDATETYPE_INSERT);
  594. }
  595. UA_HistoryData_deleteMembers(&data);
  596. UA_HistoryDataBackend_Memory_deleteMembers(&setting.historizingBackend);
  597. }
  598. END_TEST
  599. START_TEST(Server_HistorizingUpdateReplace)
  600. {
  601. UA_HistoryDataBackend backend = UA_HistoryDataBackend_Memory(1, 1);
  602. UA_HistorizingNodeIdSettings setting;
  603. setting.historizingBackend = backend;
  604. setting.maxHistoryDataResponseSize = 1000;
  605. setting.historizingUpdateStrategy = UA_HISTORIZINGUPDATESTRATEGY_USER;
  606. serverMutexLock();
  607. UA_StatusCode ret = gathering->registerNodeId(server, gathering->context, &outNodeId, setting);
  608. serverMutexUnlock();
  609. ck_assert_str_eq(UA_StatusCode_name(ret), UA_StatusCode_name(UA_STATUSCODE_GOOD));
  610. // fill backend with insert
  611. ck_assert_str_eq(UA_StatusCode_name(updateHistory(UA_PERFORMUPDATETYPE_INSERT, testData, NULL, NULL))
  612. , UA_StatusCode_name(UA_STATUSCODE_GOOD));
  613. // replace all
  614. ck_assert_str_eq(UA_StatusCode_name(updateHistory(UA_PERFORMUPDATETYPE_REPLACE, testData, NULL, NULL))
  615. , UA_StatusCode_name(UA_STATUSCODE_GOOD));
  616. UA_HistoryData data;
  617. UA_HistoryData_init(&data);
  618. testResult(testDataSorted, &data);
  619. for (size_t i = 0; i < data.dataValuesSize; ++i) {
  620. ck_assert_uint_eq(data.dataValues[i].hasValue, true);
  621. ck_assert(data.dataValues[i].value.type == &UA_TYPES[UA_TYPES_INT64]);
  622. ck_assert_uint_eq(*((UA_Int64*)data.dataValues[i].value.data), UA_PERFORMUPDATETYPE_REPLACE);
  623. }
  624. UA_HistoryData_deleteMembers(&data);
  625. UA_HistoryDataBackend_Memory_deleteMembers(&setting.historizingBackend);
  626. }
  627. END_TEST
  628. START_TEST(Server_HistorizingUpdateUpdate)
  629. {
  630. UA_HistoryDataBackend backend = UA_HistoryDataBackend_Memory(1, 1);
  631. UA_HistorizingNodeIdSettings setting;
  632. setting.historizingBackend = backend;
  633. setting.maxHistoryDataResponseSize = 1000;
  634. setting.historizingUpdateStrategy = UA_HISTORIZINGUPDATESTRATEGY_USER;
  635. serverMutexLock();
  636. UA_StatusCode ret = gathering->registerNodeId(server, gathering->context, &outNodeId, setting);
  637. serverMutexUnlock();
  638. ck_assert_str_eq(UA_StatusCode_name(ret), UA_StatusCode_name(UA_STATUSCODE_GOOD));
  639. // fill backend with insert
  640. ck_assert_str_eq(UA_StatusCode_name(updateHistory(UA_PERFORMUPDATETYPE_INSERT, testData, NULL, NULL))
  641. , UA_StatusCode_name(UA_STATUSCODE_GOOD));
  642. testResult(testDataSorted, NULL);
  643. // delete some values
  644. ck_assert_str_eq(UA_StatusCode_name(deleteHistory(DELETE_START_TIME, DELETE_STOP_TIME)),
  645. UA_StatusCode_name(UA_STATUSCODE_GOOD));
  646. testResult(testDataAfterDelete, NULL);
  647. // update all and insert some
  648. UA_StatusCode *result;
  649. size_t resultSize = 0;
  650. ck_assert_str_eq(UA_StatusCode_name(updateHistory(UA_PERFORMUPDATETYPE_UPDATE, testDataSorted, &result, &resultSize))
  651. , UA_StatusCode_name(UA_STATUSCODE_GOOD));
  652. for (size_t i = 0; i < resultSize; ++i) {
  653. ck_assert_str_eq(UA_StatusCode_name(result[i]), UA_StatusCode_name(testDataUpdateResult[i]));
  654. }
  655. UA_Array_delete(result, resultSize, &UA_TYPES[UA_TYPES_STATUSCODE]);
  656. UA_HistoryData data;
  657. UA_HistoryData_init(&data);
  658. testResult(testDataSorted, &data);
  659. for (size_t i = 0; i < data.dataValuesSize; ++i) {
  660. ck_assert_uint_eq(data.dataValues[i].hasValue, true);
  661. ck_assert(data.dataValues[i].value.type == &UA_TYPES[UA_TYPES_INT64]);
  662. ck_assert_uint_eq(*((UA_Int64*)data.dataValues[i].value.data), UA_PERFORMUPDATETYPE_UPDATE);
  663. }
  664. UA_HistoryData_deleteMembers(&data);
  665. UA_HistoryDataBackend_Memory_deleteMembers(&setting.historizingBackend);
  666. }
  667. END_TEST
  668. START_TEST(Server_HistorizingStrategyUser)
  669. {
  670. // set a data backend
  671. UA_HistorizingNodeIdSettings setting;
  672. setting.historizingBackend = UA_HistoryDataBackend_Memory(3, 100);
  673. setting.maxHistoryDataResponseSize = 100;
  674. setting.historizingUpdateStrategy = UA_HISTORIZINGUPDATESTRATEGY_USER;
  675. UA_StatusCode retval = gathering->registerNodeId(server, gathering->context, &outNodeId, setting);
  676. ck_assert_str_eq(UA_StatusCode_name(retval), UA_StatusCode_name(UA_STATUSCODE_GOOD));
  677. // fill the data
  678. UA_DateTime start = UA_DateTime_now();
  679. UA_DateTime end = start + (10 * UA_DATETIME_SEC);
  680. for (UA_UInt32 i = 0; i < 10; ++i) {
  681. UA_DataValue value;
  682. UA_DataValue_init(&value);
  683. value.hasValue = true;
  684. value.hasStatus = true;
  685. value.status = UA_STATUSCODE_GOOD;
  686. UA_Variant_setScalarCopy(&value.value, &i, &UA_TYPES[UA_TYPES_UINT32]);
  687. value.hasSourceTimestamp = true;
  688. value.sourceTimestamp = start + (i * UA_DATETIME_SEC);
  689. value.hasServerTimestamp = true;
  690. value.serverTimestamp = value.sourceTimestamp;
  691. retval = setting.historizingBackend.serverSetHistoryData(server,
  692. setting.historizingBackend.context,
  693. NULL,
  694. NULL,
  695. &outNodeId,
  696. UA_FALSE,
  697. &value);
  698. ck_assert_str_eq(UA_StatusCode_name(retval), UA_StatusCode_name(UA_STATUSCODE_GOOD));
  699. UA_DataValue_deleteMembers(&value);
  700. }
  701. // request
  702. UA_HistoryReadResponse response;
  703. UA_HistoryReadResponse_init(&response);
  704. requestHistory(start, end, &response, 0, false, NULL);
  705. // test the response
  706. ck_assert_str_eq(UA_StatusCode_name(response.responseHeader.serviceResult), UA_StatusCode_name(UA_STATUSCODE_GOOD));
  707. ck_assert_uint_eq(response.resultsSize, 1);
  708. for (size_t i = 0; i < response.resultsSize; ++i) {
  709. ck_assert_str_eq(UA_StatusCode_name(response.results[i].statusCode), UA_StatusCode_name(UA_STATUSCODE_GOOD));
  710. ck_assert_uint_eq(response.results[i].historyData.encoding, UA_EXTENSIONOBJECT_DECODED);
  711. ck_assert(response.results[i].historyData.content.decoded.type == &UA_TYPES[UA_TYPES_HISTORYDATA]);
  712. UA_HistoryData * data = (UA_HistoryData *)response.results[i].historyData.content.decoded.data;
  713. ck_assert_uint_eq(data->dataValuesSize, 10);
  714. for (size_t j = 0; j < data->dataValuesSize; ++j) {
  715. ck_assert_uint_eq(data->dataValues[j].hasSourceTimestamp, true);
  716. ck_assert_uint_eq(data->dataValues[j].sourceTimestamp, start + (j * UA_DATETIME_SEC));
  717. ck_assert_uint_eq(data->dataValues[j].hasStatus, true);
  718. ck_assert_str_eq(UA_StatusCode_name(data->dataValues[j].status), UA_StatusCode_name(UA_STATUSCODE_GOOD));
  719. ck_assert_uint_eq(data->dataValues[j].hasValue, true);
  720. ck_assert(data->dataValues[j].value.type == &UA_TYPES[UA_TYPES_UINT32]);
  721. UA_UInt32 * value = (UA_UInt32 *)data->dataValues[j].value.data;
  722. ck_assert_uint_eq(*value, j);
  723. }
  724. }
  725. UA_HistoryReadResponse_deleteMembers(&response);
  726. UA_HistoryDataBackend_Memory_deleteMembers(&setting.historizingBackend);
  727. }
  728. END_TEST
  729. START_TEST(Server_HistorizingStrategyPoll)
  730. {
  731. // init to a defined value
  732. UA_StatusCode retval = setUInt32(client, outNodeId, 43);
  733. ck_assert_str_eq(UA_StatusCode_name(retval), UA_StatusCode_name(UA_STATUSCODE_GOOD));
  734. // set a data backend
  735. UA_HistorizingNodeIdSettings setting;
  736. setting.historizingBackend = UA_HistoryDataBackend_Memory(3, 100);
  737. setting.maxHistoryDataResponseSize = 100;
  738. setting.pollingInterval = 100;
  739. setting.historizingUpdateStrategy = UA_HISTORIZINGUPDATESTRATEGY_POLL;
  740. serverMutexLock();
  741. retval = gathering->registerNodeId(server, gathering->context, &outNodeId, setting);
  742. serverMutexUnlock();
  743. ck_assert_str_eq(UA_StatusCode_name(retval), UA_StatusCode_name(UA_STATUSCODE_GOOD));
  744. // fill the data
  745. UA_DateTime start = UA_DateTime_now();
  746. serverMutexLock();
  747. retval = gathering->startPoll(server, gathering->context, &outNodeId);
  748. serverMutexUnlock();
  749. ck_assert_str_eq(UA_StatusCode_name(retval), UA_StatusCode_name(UA_STATUSCODE_GOOD));
  750. ck_assert_str_eq(UA_StatusCode_name(retval), UA_StatusCode_name(UA_STATUSCODE_GOOD));
  751. for (size_t k = 0; k < 10; ++k) {
  752. UA_fakeSleep(50);
  753. UA_realSleep(50);
  754. if (k == 5) {
  755. serverMutexLock();
  756. gathering->stopPoll(server, gathering->context, &outNodeId);
  757. serverMutexUnlock();
  758. }
  759. setUInt32(client, outNodeId, (unsigned int)k);
  760. }
  761. ck_assert_str_eq(UA_StatusCode_name(retval), UA_StatusCode_name(UA_STATUSCODE_GOOD));
  762. UA_DateTime end = UA_DateTime_now();
  763. // request
  764. UA_HistoryReadResponse response;
  765. UA_HistoryReadResponse_init(&response);
  766. requestHistory(start, end, &response, 0, false, NULL);
  767. // test the response
  768. ck_assert_str_eq(UA_StatusCode_name(response.responseHeader.serviceResult), UA_StatusCode_name(UA_STATUSCODE_GOOD));
  769. ck_assert_uint_eq(response.resultsSize, 1);
  770. for (size_t i = 0; i < response.resultsSize; ++i) {
  771. ck_assert_str_eq(UA_StatusCode_name(response.results[i].statusCode), UA_StatusCode_name(UA_STATUSCODE_GOOD));
  772. ck_assert_uint_eq(response.results[i].historyData.encoding, UA_EXTENSIONOBJECT_DECODED);
  773. ck_assert(response.results[i].historyData.content.decoded.type == &UA_TYPES[UA_TYPES_HISTORYDATA]);
  774. UA_HistoryData * data = (UA_HistoryData *)response.results[i].historyData.content.decoded.data;
  775. ck_assert(data->dataValuesSize > 1);
  776. for (size_t j = 0; j < data->dataValuesSize; ++j) {
  777. ck_assert_uint_eq(data->dataValues[j].hasSourceTimestamp, true);
  778. ck_assert(data->dataValues[j].sourceTimestamp >= start);
  779. ck_assert(data->dataValues[j].sourceTimestamp < end);
  780. ck_assert_uint_eq(data->dataValues[j].hasValue, true);
  781. ck_assert(data->dataValues[j].value.type == &UA_TYPES[UA_TYPES_UINT32]);
  782. UA_UInt32 * value = (UA_UInt32 *)data->dataValues[j].value.data;
  783. // first need to be 43
  784. if (j == 0) {
  785. ck_assert(*value == 43);
  786. } else {
  787. ck_assert(*value < 5);
  788. }
  789. }
  790. }
  791. UA_HistoryReadResponse_deleteMembers(&response);
  792. UA_HistoryDataBackend_Memory_deleteMembers(&setting.historizingBackend);
  793. }
  794. END_TEST
  795. START_TEST(Server_HistorizingStrategyValueSet)
  796. {
  797. // init to a defined value
  798. UA_StatusCode retval = setUInt32(client, outNodeId, 43);
  799. ck_assert_str_eq(UA_StatusCode_name(retval), UA_StatusCode_name(UA_STATUSCODE_GOOD));
  800. // set a data backend
  801. UA_HistorizingNodeIdSettings setting;
  802. setting.historizingBackend = UA_HistoryDataBackend_Memory(3, 100);
  803. setting.maxHistoryDataResponseSize = 100;
  804. setting.historizingUpdateStrategy = UA_HISTORIZINGUPDATESTRATEGY_VALUESET;
  805. serverMutexLock();
  806. retval = gathering->registerNodeId(server, gathering->context, &outNodeId, setting);
  807. serverMutexUnlock();
  808. ck_assert_str_eq(UA_StatusCode_name(retval), UA_StatusCode_name(UA_STATUSCODE_GOOD));
  809. // fill the data
  810. UA_fakeSleep(100);
  811. UA_DateTime start = UA_DateTime_now();
  812. UA_fakeSleep(100);
  813. for (UA_UInt32 i = 0; i < 10; ++i) {
  814. retval = setUInt32(client, outNodeId, i);
  815. ck_assert_str_eq(UA_StatusCode_name(retval), UA_StatusCode_name(UA_STATUSCODE_GOOD));
  816. UA_fakeSleep(100);
  817. }
  818. UA_DateTime end = UA_DateTime_now();
  819. // request
  820. UA_HistoryReadResponse response;
  821. UA_HistoryReadResponse_init(&response);
  822. requestHistory(start, end, &response, 0, false, NULL);
  823. // test the response
  824. ck_assert_str_eq(UA_StatusCode_name(response.responseHeader.serviceResult), UA_StatusCode_name(UA_STATUSCODE_GOOD));
  825. ck_assert_uint_eq(response.resultsSize, 1);
  826. for (size_t i = 0; i < response.resultsSize; ++i) {
  827. ck_assert_str_eq(UA_StatusCode_name(response.results[i].statusCode), UA_StatusCode_name(UA_STATUSCODE_GOOD));
  828. ck_assert_uint_eq(response.results[i].historyData.encoding, UA_EXTENSIONOBJECT_DECODED);
  829. ck_assert(response.results[i].historyData.content.decoded.type == &UA_TYPES[UA_TYPES_HISTORYDATA]);
  830. UA_HistoryData * data = (UA_HistoryData *)response.results[i].historyData.content.decoded.data;
  831. ck_assert(data->dataValuesSize > 0);
  832. for (size_t j = 0; j < data->dataValuesSize; ++j) {
  833. ck_assert(data->dataValues[j].sourceTimestamp >= start && data->dataValues[j].sourceTimestamp < end);
  834. ck_assert_uint_eq(data->dataValues[j].hasSourceTimestamp, true);
  835. ck_assert_str_eq(UA_StatusCode_name(data->dataValues[j].status), UA_StatusCode_name(UA_STATUSCODE_GOOD));
  836. ck_assert_uint_eq(data->dataValues[j].hasValue, true);
  837. ck_assert(data->dataValues[j].value.type == &UA_TYPES[UA_TYPES_UINT32]);
  838. UA_UInt32 * value = (UA_UInt32 *)data->dataValues[j].value.data;
  839. ck_assert_uint_eq(*value, j);
  840. }
  841. }
  842. UA_HistoryReadResponse_deleteMembers(&response);
  843. UA_HistoryDataBackend_Memory_deleteMembers(&setting.historizingBackend);
  844. }
  845. END_TEST
  846. START_TEST(Server_HistorizingBackendMemory)
  847. {
  848. UA_HistoryDataBackend backend = UA_HistoryDataBackend_Memory(1, 1);
  849. UA_HistorizingNodeIdSettings setting;
  850. setting.historizingBackend = backend;
  851. setting.maxHistoryDataResponseSize = 1000;
  852. setting.historizingUpdateStrategy = UA_HISTORIZINGUPDATESTRATEGY_USER;
  853. serverMutexLock();
  854. UA_StatusCode ret = gathering->registerNodeId(server, gathering->context, &outNodeId, setting);
  855. serverMutexUnlock();
  856. ck_assert_str_eq(UA_StatusCode_name(ret), UA_StatusCode_name(UA_STATUSCODE_GOOD));
  857. // empty backend should not crash
  858. UA_UInt32 retval = testHistoricalDataBackend(100);
  859. fprintf(stderr, "%d tests expected failed.\n", retval);
  860. // fill backend
  861. ck_assert_uint_eq(fillHistoricalDataBackend(backend), true);
  862. // read all in one
  863. retval = testHistoricalDataBackend(100);
  864. fprintf(stderr, "%d tests failed.\n", retval);
  865. ck_assert_uint_eq(retval, 0);
  866. // read continuous one at one request
  867. retval = testHistoricalDataBackend(1);
  868. fprintf(stderr, "%d tests failed.\n", retval);
  869. ck_assert_uint_eq(retval, 0);
  870. // read continuous two at one request
  871. retval = testHistoricalDataBackend(2);
  872. fprintf(stderr, "%d tests failed.\n", retval);
  873. ck_assert_uint_eq(retval, 0);
  874. UA_HistoryDataBackend_Memory_deleteMembers(&setting.historizingBackend);
  875. }
  876. END_TEST
  877. #endif /*UA_ENABLE_HISTORIZING*/
  878. static Suite* testSuite_Client(void)
  879. {
  880. Suite *s = suite_create("Server Historical Data");
  881. TCase *tc_server = tcase_create("Server Historical Data Basic");
  882. tcase_add_checked_fixture(tc_server, setup, teardown);
  883. #ifdef UA_ENABLE_HISTORIZING
  884. tcase_add_test(tc_server, Server_HistorizingStrategyPoll);
  885. tcase_add_test(tc_server, Server_HistorizingStrategyUser);
  886. tcase_add_test(tc_server, Server_HistorizingStrategyValueSet);
  887. tcase_add_test(tc_server, Server_HistorizingBackendMemory);
  888. tcase_add_test(tc_server, Server_HistorizingUpdateDelete);
  889. tcase_add_test(tc_server, Server_HistorizingUpdateInsert);
  890. tcase_add_test(tc_server, Server_HistorizingUpdateReplace);
  891. tcase_add_test(tc_server, Server_HistorizingUpdateUpdate);
  892. #endif /* UA_ENABLE_HISTORIZING */
  893. suite_add_tcase(s, tc_server);
  894. return s;
  895. }
  896. int main(void)
  897. {
  898. #ifdef UA_ENABLE_HISTORIZING
  899. testDataSorted = sortDateTimes(testData);
  900. #endif /* UA_ENABLE_HISTORIZING */
  901. Suite *s = testSuite_Client();
  902. SRunner *sr = srunner_create(s);
  903. srunner_set_fork_status(sr, CK_NOFORK);
  904. srunner_run_all(sr,CK_NORMAL);
  905. int number_failed = srunner_ntests_failed(sr);
  906. srunner_free(sr);
  907. #ifdef UA_ENABLE_HISTORIZING
  908. UA_free(testDataSorted);
  909. #endif /* UA_ENABLE_HISTORIZING */
  910. return (number_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
  911. }