check_server_historical_data.c 42 KB

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