check_server_historical_data.c 43 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055
  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. UA_LOCK(server->serviceMutex);
  275. Service_HistoryRead(server, &server->adminSession, &request, response);
  276. UA_UNLOCK(server->serviceMutex);
  277. UA_HistoryReadRequest_deleteMembers(&request);
  278. }
  279. static UA_UInt32
  280. testHistoricalDataBackend(size_t maxResponseSize)
  281. {
  282. const UA_HistorizingNodeIdSettings* setting = gathering->getHistorizingSetting(server, gathering->context, &outNodeId);
  283. UA_HistorizingNodeIdSettings newSetting = *setting;
  284. newSetting.maxHistoryDataResponseSize = maxResponseSize;
  285. gathering->updateNodeIdSetting(server, gathering->context, &outNodeId, newSetting);
  286. UA_UInt32 retval = 0;
  287. size_t i = 0;
  288. testTuple *current = &testRequests[i];
  289. fprintf(stderr, "Testing with maxResponseSize of %lu\n", maxResponseSize);
  290. fprintf(stderr, "Start | End | numValuesPerNode | returnBounds |ContPoint| {Expected}{Result} Result\n");
  291. fprintf(stderr, "------+------+------------------+--------------+---------+----------------\n");
  292. size_t j;
  293. while (current->start || current->end) {
  294. j = 0;
  295. if (current->start == TIMESTAMP_UNSPECIFIED) {
  296. fprintf(stderr, "UNSPEC|");
  297. } else {
  298. fprintf(stderr, " %3lld |", current->start / UA_DATETIME_SEC);
  299. }
  300. if (current->end == TIMESTAMP_UNSPECIFIED) {
  301. fprintf(stderr, "UNSPEC|");
  302. } else {
  303. fprintf(stderr, " %3lld |", current->end / UA_DATETIME_SEC);
  304. }
  305. fprintf(stderr, " %2u | %s | %s | {", current->numValuesPerNode, (current->returnBounds ? "Yes" : " No"), (current->returnContinuationPoint ? "Yes" : " No"));
  306. while (current->result[j]) {
  307. printTimestamp(current->result[j]);
  308. ++j;
  309. }
  310. fprintf(stderr, "}");
  311. UA_DataValue *result = NULL;
  312. size_t resultSize = 0;
  313. UA_ByteString continuous;
  314. UA_ByteString_init(&continuous);
  315. UA_Boolean readOk = true;
  316. size_t reseivedValues = 0;
  317. fprintf(stderr, "{");
  318. size_t counter = 0;
  319. do {
  320. UA_HistoryReadResponse response;
  321. UA_HistoryReadResponse_init(&response);
  322. UA_UInt32 numValuesPerNode = current->numValuesPerNode;
  323. if (numValuesPerNode > 0 && numValuesPerNode + (UA_UInt32)reseivedValues > current->numValuesPerNode)
  324. numValuesPerNode = current->numValuesPerNode - (UA_UInt32)reseivedValues;
  325. requestHistory(current->start,
  326. current->end,
  327. &response,
  328. numValuesPerNode,
  329. current->returnBounds,
  330. &continuous);
  331. ++counter;
  332. if(response.resultsSize != 1) {
  333. fprintf(stderr, "ResultError:Size %lu %s", response.resultsSize, UA_StatusCode_name(response.responseHeader.serviceResult));
  334. readOk = false;
  335. UA_HistoryReadResponse_deleteMembers(&response);
  336. break;
  337. }
  338. UA_StatusCode stat = response.results[0].statusCode;
  339. if (stat == UA_STATUSCODE_BADBOUNDNOTSUPPORTED && current->returnBounds) {
  340. fprintf(stderr, "%s", UA_StatusCode_name(stat));
  341. UA_HistoryReadResponse_deleteMembers(&response);
  342. break;
  343. }
  344. if(response.results[0].historyData.encoding != UA_EXTENSIONOBJECT_DECODED
  345. || response.results[0].historyData.content.decoded.type != &UA_TYPES[UA_TYPES_HISTORYDATA]) {
  346. fprintf(stderr, "ResultError:HistoryData");
  347. readOk = false;
  348. UA_HistoryReadResponse_deleteMembers(&response);
  349. break;
  350. }
  351. UA_HistoryData * data = (UA_HistoryData *)response.results[0].historyData.content.decoded.data;
  352. resultSize = data->dataValuesSize;
  353. result = data->dataValues;
  354. if (resultSize == 0 && continuous.length > 0) {
  355. fprintf(stderr, "continuousResultEmpty");
  356. readOk = false;
  357. UA_HistoryReadResponse_deleteMembers(&response);
  358. break;
  359. }
  360. if (resultSize > maxResponseSize) {
  361. fprintf(stderr, "resultToBig");
  362. readOk = false;
  363. UA_HistoryReadResponse_deleteMembers(&response);
  364. break;
  365. }
  366. if (stat != UA_STATUSCODE_GOOD) {
  367. fprintf(stderr, "%s", UA_StatusCode_name(stat));
  368. } else {
  369. for (size_t k = 0; k < resultSize; ++k)
  370. printResult(&result[k]);
  371. }
  372. if (stat == UA_STATUSCODE_GOOD && j >= resultSize + reseivedValues) {
  373. for (size_t l = 0; l < resultSize; ++l) {
  374. /* See OPC UA Part 11, Version 1.03, Page 5-6, Table 1, Mark a for details.*/
  375. if (current->result[l + reseivedValues] == TIMESTAMP_LAST && current->end == TIMESTAMP_UNSPECIFIED) {
  376. // This test will work on not continous read, only
  377. if (reseivedValues == 0 && !(l > 0 && result[l].sourceTimestamp == result[l-1].sourceTimestamp + UA_DATETIME_SEC))
  378. readOk = false;
  379. }
  380. /* See OPC UA Part 11, Version 1.03, Page 5-6, Table 1, Mark b for details.*/
  381. if (current->result[l + reseivedValues] == TIMESTAMP_FIRST && current->start == TIMESTAMP_UNSPECIFIED) {
  382. // This test will work on not continous read, only
  383. if (reseivedValues == 0 && !(l > 0 && result[l].sourceTimestamp == result[l-1].sourceTimestamp - UA_DATETIME_SEC))
  384. readOk = false;
  385. }
  386. if (!resultIsEqual(&result[l], current, l + reseivedValues))
  387. readOk = false;
  388. }
  389. if (response.results[0].continuationPoint.length > 0)
  390. fprintf(stderr, "C,");
  391. reseivedValues += resultSize;
  392. if (reseivedValues == j) {
  393. if (current->returnContinuationPoint && response.results[0].continuationPoint.length == 0) {
  394. readOk = false;
  395. fprintf(stderr, "missingContinuationPoint");
  396. }
  397. if (!current->returnContinuationPoint && response.results[0].continuationPoint.length > 0) {
  398. readOk = false;
  399. fprintf(stderr, "unexpectedContinuationPoint");
  400. }
  401. UA_HistoryReadResponse_deleteMembers(&response);
  402. break;
  403. }
  404. UA_ByteString_deleteMembers(&continuous);
  405. UA_ByteString_copy(&response.results[0].continuationPoint, &continuous);
  406. } else {
  407. readOk = false;
  408. UA_HistoryReadResponse_deleteMembers(&response);
  409. break;
  410. }
  411. UA_HistoryReadResponse_deleteMembers(&response);
  412. } while (continuous.length > 0);
  413. if (j != reseivedValues) {
  414. readOk = false;
  415. }
  416. UA_ByteString_deleteMembers(&continuous);
  417. if (!readOk) {
  418. fprintf(stderr, "} Fail (%lu requests)\n", counter);
  419. ++retval;
  420. } else {
  421. fprintf(stderr, "} OK (%lu requests)\n", counter);
  422. }
  423. current = &testRequests[++i];
  424. }
  425. return retval;
  426. }
  427. void
  428. Service_HistoryUpdate(UA_Server *server, UA_Session *session,
  429. const UA_HistoryUpdateRequest *request,
  430. UA_HistoryUpdateResponse *response);
  431. static UA_StatusCode
  432. deleteHistory(UA_DateTime start,
  433. UA_DateTime end)
  434. {
  435. UA_DeleteRawModifiedDetails *details = UA_DeleteRawModifiedDetails_new();
  436. details->startTime = start;
  437. details->endTime = end;
  438. details->isDeleteModified = false;
  439. UA_NodeId_copy(&outNodeId, &details->nodeId);
  440. UA_HistoryUpdateRequest request;
  441. UA_HistoryUpdateRequest_init(&request);
  442. request.historyUpdateDetailsSize = 1;
  443. request.historyUpdateDetails = UA_ExtensionObject_new();
  444. UA_ExtensionObject_init(request.historyUpdateDetails);
  445. request.historyUpdateDetails[0].encoding = UA_EXTENSIONOBJECT_DECODED;
  446. request.historyUpdateDetails[0].content.decoded.type = &UA_TYPES[UA_TYPES_DELETERAWMODIFIEDDETAILS];
  447. request.historyUpdateDetails[0].content.decoded.data = details;
  448. UA_HistoryUpdateResponse response;
  449. UA_HistoryUpdateResponse_init(&response);
  450. UA_LOCK(server->serviceMutex);
  451. Service_HistoryUpdate(server, &server->adminSession, &request, &response);
  452. UA_UNLOCK(server->serviceMutex);
  453. UA_HistoryUpdateRequest_deleteMembers(&request);
  454. UA_StatusCode ret = UA_STATUSCODE_GOOD;
  455. if (response.responseHeader.serviceResult != UA_STATUSCODE_GOOD)
  456. ret = response.responseHeader.serviceResult;
  457. else if (response.resultsSize != 1)
  458. ret = UA_STATUSCODE_BADUNEXPECTEDERROR;
  459. else if (response.results[0].statusCode != UA_STATUSCODE_GOOD)
  460. ret = response.results[0].statusCode;
  461. else if (response.results[0].operationResultsSize != 0)
  462. ret = UA_STATUSCODE_BADUNEXPECTEDERROR;
  463. UA_HistoryUpdateResponse_deleteMembers(&response);
  464. return ret;
  465. }
  466. static UA_StatusCode
  467. updateHistory(UA_PerformUpdateType updateType, UA_DateTime *updateData, UA_StatusCode ** operationResults, size_t *operationResultsSize)
  468. {
  469. UA_UpdateDataDetails *details = UA_UpdateDataDetails_new();
  470. details->performInsertReplace = updateType;
  471. UA_NodeId_copy(&outNodeId, &details->nodeId);
  472. int updateDataSize = -1;
  473. while(updateData[++updateDataSize]);
  474. fprintf(stderr, "updateHistory for %d values.\n", updateDataSize);
  475. details->updateValuesSize = (size_t)updateDataSize;
  476. details->updateValues = (UA_DataValue*)UA_Array_new(details->updateValuesSize, &UA_TYPES[UA_TYPES_DATAVALUE]);
  477. for (size_t i = 0; i < details->updateValuesSize; ++i) {
  478. UA_DataValue_init(&details->updateValues[i]);
  479. details->updateValues[i].hasValue = true;
  480. UA_Int64 d = updateType;
  481. UA_Variant_setScalarCopy(&details->updateValues[i].value, &d, &UA_TYPES[UA_TYPES_INT64]);
  482. details->updateValues[i].hasSourceTimestamp = true;
  483. details->updateValues[i].sourceTimestamp = updateData[i];
  484. details->updateValues[i].hasServerTimestamp = true;
  485. details->updateValues[i].serverTimestamp = updateData[i];
  486. details->updateValues[i].hasStatus = true;
  487. details->updateValues[i].status = UA_STATUSCODE_GOOD;
  488. }
  489. UA_HistoryUpdateRequest request;
  490. UA_HistoryUpdateRequest_init(&request);
  491. request.historyUpdateDetailsSize = 1;
  492. request.historyUpdateDetails = UA_ExtensionObject_new();
  493. UA_ExtensionObject_init(request.historyUpdateDetails);
  494. request.historyUpdateDetails[0].encoding = UA_EXTENSIONOBJECT_DECODED;
  495. request.historyUpdateDetails[0].content.decoded.type = &UA_TYPES[UA_TYPES_UPDATEDATADETAILS];
  496. request.historyUpdateDetails[0].content.decoded.data = details;
  497. UA_HistoryUpdateResponse response;
  498. UA_HistoryUpdateResponse_init(&response);
  499. UA_LOCK(server->serviceMutex);
  500. Service_HistoryUpdate(server, &server->adminSession, &request, &response);
  501. UA_UNLOCK(server->serviceMutex);
  502. UA_HistoryUpdateRequest_deleteMembers(&request);
  503. UA_StatusCode ret = UA_STATUSCODE_GOOD;
  504. if (response.responseHeader.serviceResult != UA_STATUSCODE_GOOD)
  505. ret = response.responseHeader.serviceResult;
  506. else if (response.resultsSize != 1)
  507. ret = UA_STATUSCODE_BADUNEXPECTEDERROR;
  508. else if (response.results[0].statusCode != UA_STATUSCODE_GOOD)
  509. ret = response.results[0].statusCode;
  510. else if (response.results[0].operationResultsSize != (size_t)updateDataSize)
  511. ret = UA_STATUSCODE_BADUNEXPECTEDERROR;
  512. else {
  513. if (operationResults) {
  514. *operationResultsSize = response.results[0].operationResultsSize;
  515. ret = UA_Array_copy(response.results[0].operationResults, *operationResultsSize, (void**)operationResults, &UA_TYPES[UA_TYPES_STATUSCODE]);
  516. } else {
  517. for (size_t i = 0; i < response.results[0].operationResultsSize; ++i) {
  518. if (response.results[0].operationResults[i] != UA_STATUSCODE_GOOD) {
  519. ret = response.results[0].operationResults[i];
  520. break;
  521. }
  522. }
  523. }
  524. }
  525. UA_HistoryUpdateResponse_deleteMembers(&response);
  526. return ret;
  527. }
  528. static void
  529. testResult(UA_DateTime *resultData, UA_HistoryData * historyData) {
  530. // request
  531. UA_HistoryReadResponse localResponse;
  532. UA_HistoryReadResponse_init(&localResponse);
  533. requestHistory(TIMESTAMP_FIRST, TIMESTAMP_LAST, &localResponse, 0, false, NULL);
  534. // test the response
  535. ck_assert_str_eq(UA_StatusCode_name(localResponse.responseHeader.serviceResult), UA_StatusCode_name(UA_STATUSCODE_GOOD));
  536. ck_assert_uint_eq(localResponse.resultsSize, 1);
  537. ck_assert_str_eq(UA_StatusCode_name(localResponse.results[0].statusCode), UA_StatusCode_name(UA_STATUSCODE_GOOD));
  538. ck_assert_uint_eq(localResponse.results[0].historyData.encoding, UA_EXTENSIONOBJECT_DECODED);
  539. ck_assert(localResponse.results[0].historyData.content.decoded.type == &UA_TYPES[UA_TYPES_HISTORYDATA]);
  540. UA_HistoryData * data = (UA_HistoryData *)localResponse.results[0].historyData.content.decoded.data;
  541. if (historyData)
  542. UA_HistoryData_copy(data, historyData);
  543. for (size_t j = 0; j < data->dataValuesSize; ++j) {
  544. ck_assert(resultData[j] != 0);
  545. ck_assert_uint_eq(data->dataValues[j].hasSourceTimestamp, true);
  546. ck_assert_uint_eq(data->dataValues[j].sourceTimestamp, resultData[j]);
  547. }
  548. UA_HistoryReadResponse_deleteMembers(&localResponse);
  549. }
  550. START_TEST(Server_HistorizingUpdateDelete)
  551. {
  552. UA_HistoryDataBackend backend = UA_HistoryDataBackend_Memory(1, 1);
  553. UA_HistorizingNodeIdSettings setting;
  554. setting.historizingBackend = backend;
  555. setting.maxHistoryDataResponseSize = 1000;
  556. setting.historizingUpdateStrategy = UA_HISTORIZINGUPDATESTRATEGY_USER;
  557. serverMutexLock();
  558. UA_StatusCode ret = gathering->registerNodeId(server, gathering->context, &outNodeId, setting);
  559. serverMutexUnlock();
  560. ck_assert_str_eq(UA_StatusCode_name(ret), UA_StatusCode_name(UA_STATUSCODE_GOOD));
  561. // fill backend
  562. ck_assert_uint_eq(fillHistoricalDataBackend(backend), true);
  563. // delete some values
  564. ck_assert_str_eq(UA_StatusCode_name(deleteHistory(DELETE_START_TIME, DELETE_STOP_TIME)),
  565. UA_StatusCode_name(UA_STATUSCODE_GOOD));
  566. testResult(testDataAfterDelete, NULL);
  567. UA_HistoryDataBackend_Memory_deleteMembers(&setting.historizingBackend);
  568. }
  569. END_TEST
  570. START_TEST(Server_HistorizingUpdateInsert)
  571. {
  572. UA_HistoryDataBackend backend = UA_HistoryDataBackend_Memory(1, 1);
  573. UA_HistorizingNodeIdSettings setting;
  574. setting.historizingBackend = backend;
  575. setting.maxHistoryDataResponseSize = 1000;
  576. setting.historizingUpdateStrategy = UA_HISTORIZINGUPDATESTRATEGY_USER;
  577. serverMutexLock();
  578. UA_StatusCode ret = gathering->registerNodeId(server, gathering->context, &outNodeId, setting);
  579. serverMutexUnlock();
  580. ck_assert_str_eq(UA_StatusCode_name(ret), UA_StatusCode_name(UA_STATUSCODE_GOOD));
  581. // fill backend with insert
  582. ck_assert_str_eq(UA_StatusCode_name(updateHistory(UA_PERFORMUPDATETYPE_INSERT, testData, NULL, NULL))
  583. , UA_StatusCode_name(UA_STATUSCODE_GOOD));
  584. UA_HistoryData data;
  585. UA_HistoryData_init(&data);
  586. testResult(testDataSorted, &data);
  587. for (size_t i = 0; i < data.dataValuesSize; ++i) {
  588. ck_assert_uint_eq(data.dataValues[i].hasValue, true);
  589. ck_assert(data.dataValues[i].value.type == &UA_TYPES[UA_TYPES_INT64]);
  590. ck_assert_uint_eq(*((UA_Int64*)data.dataValues[i].value.data), UA_PERFORMUPDATETYPE_INSERT);
  591. }
  592. UA_HistoryData_deleteMembers(&data);
  593. UA_HistoryDataBackend_Memory_deleteMembers(&setting.historizingBackend);
  594. }
  595. END_TEST
  596. START_TEST(Server_HistorizingUpdateReplace)
  597. {
  598. UA_HistoryDataBackend backend = UA_HistoryDataBackend_Memory(1, 1);
  599. UA_HistorizingNodeIdSettings setting;
  600. setting.historizingBackend = backend;
  601. setting.maxHistoryDataResponseSize = 1000;
  602. setting.historizingUpdateStrategy = UA_HISTORIZINGUPDATESTRATEGY_USER;
  603. serverMutexLock();
  604. UA_StatusCode ret = gathering->registerNodeId(server, gathering->context, &outNodeId, setting);
  605. serverMutexUnlock();
  606. ck_assert_str_eq(UA_StatusCode_name(ret), UA_StatusCode_name(UA_STATUSCODE_GOOD));
  607. // fill backend with insert
  608. ck_assert_str_eq(UA_StatusCode_name(updateHistory(UA_PERFORMUPDATETYPE_INSERT, testData, NULL, NULL))
  609. , UA_StatusCode_name(UA_STATUSCODE_GOOD));
  610. // replace all
  611. ck_assert_str_eq(UA_StatusCode_name(updateHistory(UA_PERFORMUPDATETYPE_REPLACE, testData, NULL, NULL))
  612. , UA_StatusCode_name(UA_STATUSCODE_GOOD));
  613. UA_HistoryData data;
  614. UA_HistoryData_init(&data);
  615. testResult(testDataSorted, &data);
  616. for (size_t i = 0; i < data.dataValuesSize; ++i) {
  617. ck_assert_uint_eq(data.dataValues[i].hasValue, true);
  618. ck_assert(data.dataValues[i].value.type == &UA_TYPES[UA_TYPES_INT64]);
  619. ck_assert_uint_eq(*((UA_Int64*)data.dataValues[i].value.data), UA_PERFORMUPDATETYPE_REPLACE);
  620. }
  621. UA_HistoryData_deleteMembers(&data);
  622. UA_HistoryDataBackend_Memory_deleteMembers(&setting.historizingBackend);
  623. }
  624. END_TEST
  625. START_TEST(Server_HistorizingUpdateUpdate)
  626. {
  627. UA_HistoryDataBackend backend = UA_HistoryDataBackend_Memory(1, 1);
  628. UA_HistorizingNodeIdSettings setting;
  629. setting.historizingBackend = backend;
  630. setting.maxHistoryDataResponseSize = 1000;
  631. setting.historizingUpdateStrategy = UA_HISTORIZINGUPDATESTRATEGY_USER;
  632. serverMutexLock();
  633. UA_StatusCode ret = gathering->registerNodeId(server, gathering->context, &outNodeId, setting);
  634. serverMutexUnlock();
  635. ck_assert_str_eq(UA_StatusCode_name(ret), UA_StatusCode_name(UA_STATUSCODE_GOOD));
  636. // fill backend with insert
  637. ck_assert_str_eq(UA_StatusCode_name(updateHistory(UA_PERFORMUPDATETYPE_INSERT, testData, NULL, NULL))
  638. , UA_StatusCode_name(UA_STATUSCODE_GOOD));
  639. testResult(testDataSorted, NULL);
  640. // delete some values
  641. ck_assert_str_eq(UA_StatusCode_name(deleteHistory(DELETE_START_TIME, DELETE_STOP_TIME)),
  642. UA_StatusCode_name(UA_STATUSCODE_GOOD));
  643. testResult(testDataAfterDelete, NULL);
  644. // update all and insert some
  645. UA_StatusCode *result;
  646. size_t resultSize = 0;
  647. ck_assert_str_eq(UA_StatusCode_name(updateHistory(UA_PERFORMUPDATETYPE_UPDATE, testDataSorted, &result, &resultSize))
  648. , UA_StatusCode_name(UA_STATUSCODE_GOOD));
  649. for (size_t i = 0; i < resultSize; ++i) {
  650. ck_assert_str_eq(UA_StatusCode_name(result[i]), UA_StatusCode_name(testDataUpdateResult[i]));
  651. }
  652. UA_Array_delete(result, resultSize, &UA_TYPES[UA_TYPES_STATUSCODE]);
  653. UA_HistoryData data;
  654. UA_HistoryData_init(&data);
  655. testResult(testDataSorted, &data);
  656. for (size_t i = 0; i < data.dataValuesSize; ++i) {
  657. ck_assert_uint_eq(data.dataValues[i].hasValue, true);
  658. ck_assert(data.dataValues[i].value.type == &UA_TYPES[UA_TYPES_INT64]);
  659. ck_assert_uint_eq(*((UA_Int64*)data.dataValues[i].value.data), UA_PERFORMUPDATETYPE_UPDATE);
  660. }
  661. UA_HistoryData_deleteMembers(&data);
  662. UA_HistoryDataBackend_Memory_deleteMembers(&setting.historizingBackend);
  663. }
  664. END_TEST
  665. START_TEST(Server_HistorizingStrategyUser)
  666. {
  667. // set a data backend
  668. UA_HistorizingNodeIdSettings setting;
  669. setting.historizingBackend = UA_HistoryDataBackend_Memory(3, 100);
  670. setting.maxHistoryDataResponseSize = 100;
  671. setting.historizingUpdateStrategy = UA_HISTORIZINGUPDATESTRATEGY_USER;
  672. UA_StatusCode retval = gathering->registerNodeId(server, gathering->context, &outNodeId, setting);
  673. ck_assert_str_eq(UA_StatusCode_name(retval), UA_StatusCode_name(UA_STATUSCODE_GOOD));
  674. // fill the data
  675. UA_DateTime start = UA_DateTime_now();
  676. UA_DateTime end = start + (10 * UA_DATETIME_SEC);
  677. for (UA_UInt32 i = 0; i < 10; ++i) {
  678. UA_DataValue value;
  679. UA_DataValue_init(&value);
  680. value.hasValue = true;
  681. value.hasStatus = true;
  682. value.status = UA_STATUSCODE_GOOD;
  683. UA_Variant_setScalarCopy(&value.value, &i, &UA_TYPES[UA_TYPES_UINT32]);
  684. value.hasSourceTimestamp = true;
  685. value.sourceTimestamp = start + (i * UA_DATETIME_SEC);
  686. value.hasServerTimestamp = true;
  687. value.serverTimestamp = value.sourceTimestamp;
  688. retval = setting.historizingBackend.serverSetHistoryData(server,
  689. setting.historizingBackend.context,
  690. NULL,
  691. NULL,
  692. &outNodeId,
  693. UA_FALSE,
  694. &value);
  695. ck_assert_str_eq(UA_StatusCode_name(retval), UA_StatusCode_name(UA_STATUSCODE_GOOD));
  696. UA_DataValue_deleteMembers(&value);
  697. }
  698. // request
  699. UA_HistoryReadResponse response;
  700. UA_HistoryReadResponse_init(&response);
  701. requestHistory(start, end, &response, 0, false, NULL);
  702. // test the response
  703. ck_assert_str_eq(UA_StatusCode_name(response.responseHeader.serviceResult), UA_StatusCode_name(UA_STATUSCODE_GOOD));
  704. ck_assert_uint_eq(response.resultsSize, 1);
  705. for (size_t i = 0; i < response.resultsSize; ++i) {
  706. ck_assert_str_eq(UA_StatusCode_name(response.results[i].statusCode), UA_StatusCode_name(UA_STATUSCODE_GOOD));
  707. ck_assert_uint_eq(response.results[i].historyData.encoding, UA_EXTENSIONOBJECT_DECODED);
  708. ck_assert(response.results[i].historyData.content.decoded.type == &UA_TYPES[UA_TYPES_HISTORYDATA]);
  709. UA_HistoryData * data = (UA_HistoryData *)response.results[i].historyData.content.decoded.data;
  710. ck_assert_uint_eq(data->dataValuesSize, 10);
  711. for (size_t j = 0; j < data->dataValuesSize; ++j) {
  712. ck_assert_uint_eq(data->dataValues[j].hasSourceTimestamp, true);
  713. ck_assert_uint_eq(data->dataValues[j].sourceTimestamp, start + (j * UA_DATETIME_SEC));
  714. ck_assert_uint_eq(data->dataValues[j].hasStatus, true);
  715. ck_assert_str_eq(UA_StatusCode_name(data->dataValues[j].status), UA_StatusCode_name(UA_STATUSCODE_GOOD));
  716. ck_assert_uint_eq(data->dataValues[j].hasValue, true);
  717. ck_assert(data->dataValues[j].value.type == &UA_TYPES[UA_TYPES_UINT32]);
  718. UA_UInt32 * value = (UA_UInt32 *)data->dataValues[j].value.data;
  719. ck_assert_uint_eq(*value, j);
  720. }
  721. }
  722. UA_HistoryReadResponse_deleteMembers(&response);
  723. UA_HistoryDataBackend_Memory_deleteMembers(&setting.historizingBackend);
  724. }
  725. END_TEST
  726. START_TEST(Server_HistorizingStrategyPoll)
  727. {
  728. // init to a defined value
  729. UA_StatusCode retval = setUInt32(client, outNodeId, 43);
  730. ck_assert_str_eq(UA_StatusCode_name(retval), UA_StatusCode_name(UA_STATUSCODE_GOOD));
  731. // set a data backend
  732. UA_HistorizingNodeIdSettings setting;
  733. setting.historizingBackend = UA_HistoryDataBackend_Memory(3, 100);
  734. setting.maxHistoryDataResponseSize = 100;
  735. setting.pollingInterval = 100;
  736. setting.historizingUpdateStrategy = UA_HISTORIZINGUPDATESTRATEGY_POLL;
  737. serverMutexLock();
  738. retval = gathering->registerNodeId(server, gathering->context, &outNodeId, setting);
  739. serverMutexUnlock();
  740. ck_assert_str_eq(UA_StatusCode_name(retval), UA_StatusCode_name(UA_STATUSCODE_GOOD));
  741. // fill the data
  742. UA_DateTime start = UA_DateTime_now();
  743. serverMutexLock();
  744. retval = gathering->startPoll(server, gathering->context, &outNodeId);
  745. serverMutexUnlock();
  746. ck_assert_str_eq(UA_StatusCode_name(retval), UA_StatusCode_name(UA_STATUSCODE_GOOD));
  747. ck_assert_str_eq(UA_StatusCode_name(retval), UA_StatusCode_name(UA_STATUSCODE_GOOD));
  748. for (size_t k = 0; k < 10; ++k) {
  749. UA_fakeSleep(50);
  750. UA_realSleep(50);
  751. if (k == 5) {
  752. serverMutexLock();
  753. gathering->stopPoll(server, gathering->context, &outNodeId);
  754. serverMutexUnlock();
  755. }
  756. setUInt32(client, outNodeId, (unsigned int)k);
  757. }
  758. ck_assert_str_eq(UA_StatusCode_name(retval), UA_StatusCode_name(UA_STATUSCODE_GOOD));
  759. UA_DateTime end = UA_DateTime_now();
  760. // request
  761. UA_HistoryReadResponse response;
  762. UA_HistoryReadResponse_init(&response);
  763. requestHistory(start, end, &response, 0, false, NULL);
  764. // test the response
  765. ck_assert_str_eq(UA_StatusCode_name(response.responseHeader.serviceResult), UA_StatusCode_name(UA_STATUSCODE_GOOD));
  766. ck_assert_uint_eq(response.resultsSize, 1);
  767. for (size_t i = 0; i < response.resultsSize; ++i) {
  768. ck_assert_str_eq(UA_StatusCode_name(response.results[i].statusCode), UA_StatusCode_name(UA_STATUSCODE_GOOD));
  769. ck_assert_uint_eq(response.results[i].historyData.encoding, UA_EXTENSIONOBJECT_DECODED);
  770. ck_assert(response.results[i].historyData.content.decoded.type == &UA_TYPES[UA_TYPES_HISTORYDATA]);
  771. UA_HistoryData * data = (UA_HistoryData *)response.results[i].historyData.content.decoded.data;
  772. ck_assert(data->dataValuesSize > 1);
  773. for (size_t j = 0; j < data->dataValuesSize; ++j) {
  774. ck_assert_uint_eq(data->dataValues[j].hasSourceTimestamp, true);
  775. ck_assert(data->dataValues[j].sourceTimestamp >= start);
  776. ck_assert(data->dataValues[j].sourceTimestamp < end);
  777. ck_assert_uint_eq(data->dataValues[j].hasValue, true);
  778. ck_assert(data->dataValues[j].value.type == &UA_TYPES[UA_TYPES_UINT32]);
  779. UA_UInt32 * value = (UA_UInt32 *)data->dataValues[j].value.data;
  780. // first need to be 43
  781. if (j == 0) {
  782. ck_assert(*value == 43);
  783. } else {
  784. ck_assert(*value < 5);
  785. }
  786. }
  787. }
  788. UA_HistoryReadResponse_deleteMembers(&response);
  789. UA_HistoryDataBackend_Memory_deleteMembers(&setting.historizingBackend);
  790. }
  791. END_TEST
  792. START_TEST(Server_HistorizingStrategyValueSet)
  793. {
  794. // init to a defined value
  795. UA_StatusCode retval = setUInt32(client, outNodeId, 43);
  796. ck_assert_str_eq(UA_StatusCode_name(retval), UA_StatusCode_name(UA_STATUSCODE_GOOD));
  797. // set a data backend
  798. UA_HistorizingNodeIdSettings setting;
  799. setting.historizingBackend = UA_HistoryDataBackend_Memory(3, 100);
  800. setting.maxHistoryDataResponseSize = 100;
  801. setting.historizingUpdateStrategy = UA_HISTORIZINGUPDATESTRATEGY_VALUESET;
  802. serverMutexLock();
  803. retval = gathering->registerNodeId(server, gathering->context, &outNodeId, setting);
  804. serverMutexUnlock();
  805. ck_assert_str_eq(UA_StatusCode_name(retval), UA_StatusCode_name(UA_STATUSCODE_GOOD));
  806. // fill the data
  807. UA_fakeSleep(100);
  808. UA_DateTime start = UA_DateTime_now();
  809. UA_fakeSleep(100);
  810. for (UA_UInt32 i = 0; i < 10; ++i) {
  811. retval = setUInt32(client, outNodeId, i);
  812. ck_assert_str_eq(UA_StatusCode_name(retval), UA_StatusCode_name(UA_STATUSCODE_GOOD));
  813. UA_fakeSleep(100);
  814. }
  815. UA_DateTime end = UA_DateTime_now();
  816. // request
  817. UA_HistoryReadResponse response;
  818. UA_HistoryReadResponse_init(&response);
  819. requestHistory(start, end, &response, 0, false, NULL);
  820. // test the response
  821. ck_assert_str_eq(UA_StatusCode_name(response.responseHeader.serviceResult), UA_StatusCode_name(UA_STATUSCODE_GOOD));
  822. ck_assert_uint_eq(response.resultsSize, 1);
  823. for (size_t i = 0; i < response.resultsSize; ++i) {
  824. ck_assert_str_eq(UA_StatusCode_name(response.results[i].statusCode), UA_StatusCode_name(UA_STATUSCODE_GOOD));
  825. ck_assert_uint_eq(response.results[i].historyData.encoding, UA_EXTENSIONOBJECT_DECODED);
  826. ck_assert(response.results[i].historyData.content.decoded.type == &UA_TYPES[UA_TYPES_HISTORYDATA]);
  827. UA_HistoryData * data = (UA_HistoryData *)response.results[i].historyData.content.decoded.data;
  828. ck_assert(data->dataValuesSize > 0);
  829. for (size_t j = 0; j < data->dataValuesSize; ++j) {
  830. ck_assert(data->dataValues[j].sourceTimestamp >= start && data->dataValues[j].sourceTimestamp < end);
  831. ck_assert_uint_eq(data->dataValues[j].hasSourceTimestamp, true);
  832. ck_assert_str_eq(UA_StatusCode_name(data->dataValues[j].status), UA_StatusCode_name(UA_STATUSCODE_GOOD));
  833. ck_assert_uint_eq(data->dataValues[j].hasValue, true);
  834. ck_assert(data->dataValues[j].value.type == &UA_TYPES[UA_TYPES_UINT32]);
  835. UA_UInt32 * value = (UA_UInt32 *)data->dataValues[j].value.data;
  836. ck_assert_uint_eq(*value, j);
  837. }
  838. }
  839. UA_HistoryReadResponse_deleteMembers(&response);
  840. UA_HistoryDataBackend_Memory_deleteMembers(&setting.historizingBackend);
  841. }
  842. END_TEST
  843. START_TEST(Server_HistorizingBackendMemory)
  844. {
  845. UA_HistoryDataBackend backend = UA_HistoryDataBackend_Memory(1, 1);
  846. UA_HistorizingNodeIdSettings setting;
  847. setting.historizingBackend = backend;
  848. setting.maxHistoryDataResponseSize = 1000;
  849. setting.historizingUpdateStrategy = UA_HISTORIZINGUPDATESTRATEGY_USER;
  850. serverMutexLock();
  851. UA_StatusCode ret = gathering->registerNodeId(server, gathering->context, &outNodeId, setting);
  852. serverMutexUnlock();
  853. ck_assert_str_eq(UA_StatusCode_name(ret), UA_StatusCode_name(UA_STATUSCODE_GOOD));
  854. // empty backend should not crash
  855. UA_UInt32 retval = testHistoricalDataBackend(100);
  856. fprintf(stderr, "%d tests expected failed.\n", retval);
  857. // fill backend
  858. ck_assert_uint_eq(fillHistoricalDataBackend(backend), true);
  859. // read all in one
  860. retval = testHistoricalDataBackend(100);
  861. fprintf(stderr, "%d tests failed.\n", retval);
  862. ck_assert_uint_eq(retval, 0);
  863. // read continuous one at one request
  864. retval = testHistoricalDataBackend(1);
  865. fprintf(stderr, "%d tests failed.\n", retval);
  866. ck_assert_uint_eq(retval, 0);
  867. // read continuous two at one request
  868. retval = testHistoricalDataBackend(2);
  869. fprintf(stderr, "%d tests failed.\n", retval);
  870. ck_assert_uint_eq(retval, 0);
  871. UA_HistoryDataBackend_Memory_deleteMembers(&setting.historizingBackend);
  872. }
  873. END_TEST
  874. START_TEST(Server_HistorizingRandomIndexBackend)
  875. {
  876. UA_HistoryDataBackend backend = UA_HistoryDataBackend_randomindextest(testData);
  877. UA_HistorizingNodeIdSettings setting;
  878. setting.historizingBackend = backend;
  879. setting.maxHistoryDataResponseSize = 1000;
  880. setting.historizingUpdateStrategy = UA_HISTORIZINGUPDATESTRATEGY_USER;
  881. serverMutexLock();
  882. UA_StatusCode ret = gathering->registerNodeId(server, gathering->context, &outNodeId, setting);
  883. serverMutexUnlock();
  884. ck_assert_str_eq(UA_StatusCode_name(ret), UA_StatusCode_name(UA_STATUSCODE_GOOD));
  885. // read all in one
  886. UA_UInt32 retval = testHistoricalDataBackend(100);
  887. fprintf(stderr, "%d tests failed.\n", retval);
  888. ck_assert_uint_eq(retval, 0);
  889. // read continuous one at one request
  890. retval = testHistoricalDataBackend(1);
  891. fprintf(stderr, "%d tests failed.\n", retval);
  892. ck_assert_uint_eq(retval, 0);
  893. // read continuous two at one request
  894. retval = testHistoricalDataBackend(2);
  895. fprintf(stderr, "%d tests failed.\n", retval);
  896. ck_assert_uint_eq(retval, 0);
  897. UA_HistoryDataBackend_randomindextest_deleteMembers(&backend);
  898. }
  899. END_TEST
  900. #endif /*UA_ENABLE_HISTORIZING*/
  901. static Suite* testSuite_Client(void)
  902. {
  903. Suite *s = suite_create("Server Historical Data");
  904. TCase *tc_server = tcase_create("Server Historical Data Basic");
  905. tcase_add_checked_fixture(tc_server, setup, teardown);
  906. #ifdef UA_ENABLE_HISTORIZING
  907. tcase_add_test(tc_server, Server_HistorizingStrategyPoll);
  908. tcase_add_test(tc_server, Server_HistorizingStrategyUser);
  909. tcase_add_test(tc_server, Server_HistorizingStrategyValueSet);
  910. tcase_add_test(tc_server, Server_HistorizingBackendMemory);
  911. tcase_add_test(tc_server, Server_HistorizingRandomIndexBackend);
  912. tcase_add_test(tc_server, Server_HistorizingUpdateDelete);
  913. tcase_add_test(tc_server, Server_HistorizingUpdateInsert);
  914. tcase_add_test(tc_server, Server_HistorizingUpdateReplace);
  915. tcase_add_test(tc_server, Server_HistorizingUpdateUpdate);
  916. #endif /* UA_ENABLE_HISTORIZING */
  917. suite_add_tcase(s, tc_server);
  918. return s;
  919. }
  920. int main(void)
  921. {
  922. #ifdef UA_ENABLE_HISTORIZING
  923. testDataSorted = sortDateTimes(testData);
  924. #endif /* UA_ENABLE_HISTORIZING */
  925. Suite *s = testSuite_Client();
  926. SRunner *sr = srunner_create(s);
  927. srunner_set_fork_status(sr, CK_NOFORK);
  928. srunner_run_all(sr,CK_NORMAL);
  929. int number_failed = srunner_ntests_failed(sr);
  930. srunner_free(sr);
  931. #ifdef UA_ENABLE_HISTORIZING
  932. UA_free(testDataSorted);
  933. #endif /* UA_ENABLE_HISTORIZING */
  934. return (number_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
  935. }