check_server_historical_data.c 43 KB

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