check_pubsub_encoding_json.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  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 (c) 2018 Fraunhofer IOSB (Author: Lukas Meling)
  6. */
  7. #include <open62541/client.h>
  8. #include <open62541/types.h>
  9. #include <open62541/util.h>
  10. #include "ua_pubsub_networkmessage.h"
  11. #include <check.h>
  12. START_TEST(UA_PubSub_EncodeAllOptionalFields) {
  13. UA_NetworkMessage m;
  14. memset(&m, 0, sizeof(UA_NetworkMessage));
  15. m.version = 1;
  16. m.networkMessageType = UA_NETWORKMESSAGE_DATASET;
  17. m.payloadHeaderEnabled = true;
  18. m.payloadHeader.dataSetPayloadHeader.count = 1;
  19. UA_UInt16 dsWriter1 = 12345;
  20. m.payloadHeader.dataSetPayloadHeader.dataSetWriterIds = (UA_UInt16 *)UA_Array_new(m.payloadHeader.dataSetPayloadHeader.count, &UA_TYPES[UA_TYPES_UINT16]);
  21. m.payloadHeader.dataSetPayloadHeader.dataSetWriterIds[0] = dsWriter1;
  22. size_t memsize = m.payloadHeader.dataSetPayloadHeader.count * sizeof(UA_DataSetMessage);
  23. m.payload.dataSetPayload.dataSetMessages = (UA_DataSetMessage*)UA_malloc(memsize);
  24. memset(m.payload.dataSetPayload.dataSetMessages, 0, memsize);
  25. /* enable messageId */
  26. m.messageIdEnabled = true;
  27. m.messageId = UA_STRING_ALLOC("ABCDEFGH");
  28. /* enable publisherId */
  29. m.publisherIdEnabled = true;
  30. m.publisherIdType = UA_PUBLISHERDATATYPE_UINT16;
  31. m.publisherId.publisherIdUInt16 = 65535;
  32. /* enable dataSetClassId */
  33. m.dataSetClassIdEnabled = true;
  34. m.dataSetClassId.data1 = 1;
  35. m.dataSetClassId.data2 = 2;
  36. m.dataSetClassId.data3 = 3;
  37. /* DatasetMessage */
  38. m.payload.dataSetPayload.dataSetMessages[0].header.dataSetMessageValid = true;
  39. m.payload.dataSetPayload.dataSetMessages[0].header.fieldEncoding = UA_FIELDENCODING_VARIANT;
  40. m.payload.dataSetPayload.dataSetMessages[0].header.dataSetMessageType = UA_DATASETMESSAGE_DATAKEYFRAME;
  41. UA_UInt16 fieldCountDS1 = 1;
  42. m.payload.dataSetPayload.dataSetMessages[0].data.keyFrameData.fieldCount = fieldCountDS1;
  43. m.payload.dataSetPayload.dataSetMessages[0].data.keyFrameData.dataSetFields =
  44. (UA_DataValue*)UA_Array_new(m.payload.dataSetPayload.dataSetMessages[0].data.keyFrameData.fieldCount, &UA_TYPES[UA_TYPES_DATAVALUE]);
  45. UA_DataValue_init(&m.payload.dataSetPayload.dataSetMessages[0].data.keyFrameData.dataSetFields[0]);
  46. /* enable DataSetMessageSequenceNr */
  47. m.payload.dataSetPayload.dataSetMessages[0].header.dataSetMessageSequenceNrEnabled = true;
  48. m.payload.dataSetPayload.dataSetMessages[0].header.dataSetMessageSequenceNr = 4711;
  49. /* enable metaDataVersion */
  50. m.payload.dataSetPayload.dataSetMessages[0].header.configVersionMajorVersionEnabled = true;
  51. m.payload.dataSetPayload.dataSetMessages[0].header.configVersionMinorVersionEnabled = true;
  52. m.payload.dataSetPayload.dataSetMessages[0].header.configVersionMajorVersion = 42;
  53. m.payload.dataSetPayload.dataSetMessages[0].header.configVersionMinorVersion = 7;
  54. /* enable timestamp */
  55. m.payload.dataSetPayload.dataSetMessages[0].header.timestampEnabled = true;
  56. m.payload.dataSetPayload.dataSetMessages[0].header.timestamp = 11111111111111;
  57. /* enable status */
  58. m.payload.dataSetPayload.dataSetMessages[0].header.statusEnabled = true;
  59. m.payload.dataSetPayload.dataSetMessages[0].header.status = 12345;
  60. /* Set fieldnames */
  61. m.payload.dataSetPayload.dataSetMessages[0].data.keyFrameData.fieldNames =
  62. (UA_String*)UA_Array_new(m.payload.dataSetPayload.dataSetMessages[0].data.keyFrameData.fieldCount, &UA_TYPES[UA_TYPES_STRING]);
  63. m.payload.dataSetPayload.dataSetMessages[0].data.keyFrameData.fieldNames[0] = UA_STRING_ALLOC("Field1");
  64. UA_UInt32 iv = 27;
  65. UA_Variant_setScalarCopy(&m.payload.dataSetPayload.dataSetMessages[0].data.keyFrameData.dataSetFields[0].value, &iv, &UA_TYPES[UA_TYPES_UINT32]);
  66. m.payload.dataSetPayload.dataSetMessages[0].data.keyFrameData.dataSetFields[0].hasValue = true;
  67. size_t size = UA_NetworkMessage_calcSizeJson(&m, NULL, 0, NULL, 0, true);
  68. ck_assert_int_eq(size, 342);
  69. UA_ByteString buffer;
  70. UA_StatusCode rv = UA_ByteString_allocBuffer(&buffer, size+1);
  71. ck_assert_int_eq(rv, UA_STATUSCODE_GOOD);
  72. UA_Byte *bufPos = buffer.data;
  73. memset(bufPos, 0, size+1);
  74. const UA_Byte *bufEnd = &(buffer.data[buffer.length]);
  75. rv = UA_NetworkMessage_encodeJson(&m, &bufPos, &bufEnd, NULL, 0, NULL, 0, true);
  76. *bufPos = 0;
  77. // then
  78. ck_assert_int_eq(rv, UA_STATUSCODE_GOOD);
  79. char* result = "{\"MessageId\":\"ABCDEFGH\",\"MessageType\":\"ua-data\",\"PublisherId\":65535,\"DataSetClassId\":\"00000001-0002-0003-0000-000000000000\",\"Messages\":[{\"DataSetWriterId\":12345,\"SequenceNumber\":4711,\"MetaDataVersion\":{\"MajorVersion\":42,\"MinorVersion\":7},\"Timestamp\":\"1601-01-13T20:38:32.1111111Z\",\"Status\":2764857,\"Payload\":{\"Field1\":{\"Type\":7,\"Body\":27}}}]}";
  80. ck_assert_str_eq(result, (char*)buffer.data);
  81. UA_ByteString_deleteMembers(&buffer);
  82. UA_NetworkMessage_deleteMembers(&m);
  83. }
  84. END_TEST
  85. START_TEST(UA_PubSub_EnDecode) {
  86. UA_NetworkMessage m;
  87. memset(&m, 0, sizeof(UA_NetworkMessage));
  88. m.version = 1;
  89. m.networkMessageType = UA_NETWORKMESSAGE_DATASET;
  90. m.payloadHeaderEnabled = true;
  91. m.payloadHeader.dataSetPayloadHeader.count = 2;
  92. UA_UInt16 dsWriter1 = 4;
  93. UA_UInt16 dsWriter2 = 7;
  94. m.payloadHeader.dataSetPayloadHeader.dataSetWriterIds = (UA_UInt16 *)UA_Array_new(m.payloadHeader.dataSetPayloadHeader.count, &UA_TYPES[UA_TYPES_UINT16]);
  95. m.payloadHeader.dataSetPayloadHeader.dataSetWriterIds[0] = dsWriter1;
  96. m.payloadHeader.dataSetPayloadHeader.dataSetWriterIds[1] = dsWriter2;
  97. size_t memsize = m.payloadHeader.dataSetPayloadHeader.count * sizeof(UA_DataSetMessage);
  98. m.payload.dataSetPayload.dataSetMessages = (UA_DataSetMessage*)UA_malloc(memsize);
  99. memset(m.payload.dataSetPayload.dataSetMessages, 0, memsize);
  100. m.payload.dataSetPayload.dataSetMessages[0].header.dataSetMessageValid = true;
  101. m.payload.dataSetPayload.dataSetMessages[0].header.fieldEncoding = UA_FIELDENCODING_VARIANT;
  102. m.payload.dataSetPayload.dataSetMessages[0].header.dataSetMessageType = UA_DATASETMESSAGE_DATAKEYFRAME;
  103. UA_UInt16 fieldCountDS1 = 1;
  104. m.payload.dataSetPayload.dataSetMessages[0].data.keyFrameData.fieldCount = fieldCountDS1;
  105. m.payload.dataSetPayload.dataSetMessages[0].data.keyFrameData.dataSetFields =
  106. (UA_DataValue*)UA_Array_new(m.payload.dataSetPayload.dataSetMessages[0].data.keyFrameData.fieldCount, &UA_TYPES[UA_TYPES_DATAVALUE]);
  107. UA_DataValue_init(&m.payload.dataSetPayload.dataSetMessages[0].data.keyFrameData.dataSetFields[0]);
  108. /* Set fieldnames */
  109. m.payload.dataSetPayload.dataSetMessages[0].data.keyFrameData.fieldNames =
  110. (UA_String*)UA_Array_new(m.payload.dataSetPayload.dataSetMessages[0].data.keyFrameData.fieldCount, &UA_TYPES[UA_TYPES_STRING]);
  111. m.payload.dataSetPayload.dataSetMessages[0].data.keyFrameData.fieldNames[0] = UA_STRING_ALLOC("Field1");
  112. UA_UInt32 iv = 27;
  113. UA_Variant_setScalarCopy(&m.payload.dataSetPayload.dataSetMessages[0].data.keyFrameData.dataSetFields[0].value, &iv, &UA_TYPES[UA_TYPES_UINT32]);
  114. m.payload.dataSetPayload.dataSetMessages[0].data.keyFrameData.dataSetFields[0].hasValue = true;
  115. m.payload.dataSetPayload.dataSetMessages[1].header.dataSetMessageValid = true;
  116. m.payload.dataSetPayload.dataSetMessages[1].header.fieldEncoding = UA_FIELDENCODING_DATAVALUE;
  117. m.payload.dataSetPayload.dataSetMessages[1].header.dataSetMessageType = UA_DATASETMESSAGE_DATAKEYFRAME;
  118. UA_UInt16 fieldCountDS2 = 2;
  119. m.payload.dataSetPayload.dataSetMessages[1].data.keyFrameData.fieldCount = fieldCountDS2;
  120. memsize = sizeof(UA_DataSetMessage_DeltaFrameField) * m.payload.dataSetPayload.dataSetMessages[1].data.deltaFrameData.fieldCount;
  121. m.payload.dataSetPayload.dataSetMessages[1].data.deltaFrameData.deltaFrameFields = (UA_DataSetMessage_DeltaFrameField*)UA_malloc(memsize);
  122. /* Set fieldnames */
  123. m.payload.dataSetPayload.dataSetMessages[1].data.keyFrameData.fieldNames =
  124. (UA_String*)UA_Array_new(m.payload.dataSetPayload.dataSetMessages[1].data.deltaFrameData.fieldCount, &UA_TYPES[UA_TYPES_STRING]);
  125. m.payload.dataSetPayload.dataSetMessages[1].data.keyFrameData.fieldNames[0] = UA_STRING_ALLOC("Field2.1");
  126. m.payload.dataSetPayload.dataSetMessages[1].data.keyFrameData.fieldNames[1] = UA_STRING_ALLOC("Field2.2");
  127. UA_Guid gv = UA_Guid_random();
  128. UA_DataValue_init(&m.payload.dataSetPayload.dataSetMessages[1].data.keyFrameData.dataSetFields[0]);
  129. UA_Variant_setScalarCopy(&m.payload.dataSetPayload.dataSetMessages[1].data.keyFrameData.dataSetFields[0].value, &gv, &UA_TYPES[UA_TYPES_GUID]);
  130. m.payload.dataSetPayload.dataSetMessages[1].data.keyFrameData.dataSetFields[0].hasValue = true;
  131. UA_DataValue_init(&m.payload.dataSetPayload.dataSetMessages[1].data.keyFrameData.dataSetFields[1]);
  132. UA_Int64 iv64 = 152478978534;
  133. UA_Variant_setScalarCopy(&m.payload.dataSetPayload.dataSetMessages[1].data.keyFrameData.dataSetFields[1].value, &iv64, &UA_TYPES[UA_TYPES_INT64]);
  134. m.payload.dataSetPayload.dataSetMessages[1].data.keyFrameData.dataSetFields[1].hasValue = true;
  135. size_t size = UA_NetworkMessage_calcSizeJson(&m, NULL, 0, NULL, 0, true);
  136. UA_ByteString buffer;
  137. UA_StatusCode rv = UA_ByteString_allocBuffer(&buffer, size);
  138. ck_assert_int_eq(rv, UA_STATUSCODE_GOOD);
  139. UA_Byte *bufPos = buffer.data;
  140. memset(bufPos, 0, size);
  141. const UA_Byte *bufEnd = &(buffer.data[buffer.length]);
  142. rv = UA_NetworkMessage_encodeJson(&m, &bufPos, &bufEnd, NULL, 0, NULL, 0, true);
  143. //*bufPos = 0;
  144. // then
  145. ck_assert_int_eq(rv, UA_STATUSCODE_GOOD);
  146. UA_NetworkMessage m2;
  147. memset(&m2, 0, sizeof(UA_NetworkMessage));
  148. rv = UA_NetworkMessage_decodeJson(&m2, &buffer);
  149. ck_assert_int_eq(rv, UA_STATUSCODE_GOOD);
  150. ck_assert(m.networkMessageType == m2.networkMessageType);
  151. ck_assert(m.timestampEnabled == m2.timestampEnabled);
  152. ck_assert(m.dataSetClassIdEnabled == m2.dataSetClassIdEnabled);
  153. ck_assert(m.groupHeaderEnabled == m2.groupHeaderEnabled);
  154. ck_assert(m.picosecondsEnabled == m2.picosecondsEnabled);
  155. ck_assert(m.promotedFieldsEnabled == m2.promotedFieldsEnabled);
  156. ck_assert(m.publisherIdEnabled == m2.publisherIdEnabled);
  157. ck_assert(m.securityEnabled == m2.securityEnabled);
  158. ck_assert(m.chunkMessage == m2.chunkMessage);
  159. ck_assert(m.payloadHeaderEnabled == m2.payloadHeaderEnabled);
  160. ck_assert_uint_eq(m2.payloadHeader.dataSetPayloadHeader.dataSetWriterIds[0], dsWriter1);
  161. ck_assert_uint_eq(m2.payloadHeader.dataSetPayloadHeader.dataSetWriterIds[1], dsWriter2);
  162. ck_assert(m.payload.dataSetPayload.dataSetMessages[0].header.dataSetMessageValid == m2.payload.dataSetPayload.dataSetMessages[0].header.dataSetMessageValid);
  163. ck_assert(m.payload.dataSetPayload.dataSetMessages[0].header.fieldEncoding == m2.payload.dataSetPayload.dataSetMessages[0].header.fieldEncoding);
  164. ck_assert_int_eq(m2.payload.dataSetPayload.dataSetMessages[0].data.keyFrameData.fieldCount, fieldCountDS1);
  165. ck_assert(m.payload.dataSetPayload.dataSetMessages[0].data.keyFrameData.dataSetFields[0].hasValue == m2.payload.dataSetPayload.dataSetMessages[0].data.keyFrameData.dataSetFields[0].hasValue);
  166. ck_assert_uint_eq((uintptr_t)m2.payload.dataSetPayload.dataSetMessages[0].data.keyFrameData.dataSetFields[0].value.type, (uintptr_t)&UA_TYPES[UA_TYPES_UINT32]);
  167. ck_assert_uint_eq(*(UA_UInt32 *)m2.payload.dataSetPayload.dataSetMessages[0].data.keyFrameData.dataSetFields[0].value.data, iv);
  168. ck_assert(m.payload.dataSetPayload.dataSetMessages[0].data.keyFrameData.dataSetFields[0].hasSourceTimestamp == m2.payload.dataSetPayload.dataSetMessages[0].data.keyFrameData.dataSetFields[0].hasSourceTimestamp);
  169. ck_assert(m.payload.dataSetPayload.dataSetMessages[1].header.dataSetMessageValid == m2.payload.dataSetPayload.dataSetMessages[1].header.dataSetMessageValid);
  170. ck_assert(m.payload.dataSetPayload.dataSetMessages[1].data.deltaFrameData.deltaFrameFields[0].fieldValue.hasSourceTimestamp == m2.payload.dataSetPayload.dataSetMessages[1].data.deltaFrameData.deltaFrameFields[0].fieldValue.hasSourceTimestamp);
  171. UA_ByteString_deleteMembers(&buffer);
  172. UA_NetworkMessage_deleteMembers(&m);
  173. UA_NetworkMessage_deleteMembers(&m2);
  174. }
  175. END_TEST
  176. START_TEST(UA_NetworkMessage_oneMessage_twoFields_json_decode) {
  177. // given
  178. UA_NetworkMessage out;
  179. UA_ByteString buf = UA_STRING("{\"MessageId\":\"5ED82C10-50BB-CD07-0120-22521081E8EE\",\"MessageType\":\"ua-data\",\"Messages\":[{\"DataSetWriterId\":62541,\"MetaDataVersion\":{\"MajorVersion\":1478393530,\"MinorVersion\":12345},\"SequenceNumber\":4711,\"Payload\":{\"Test\":{\"Type\":5,\"Body\":42},\"Server localtime\":{\"Type\":13,\"Body\":\"2018-06-05T05:58:36.000Z\"}}}]}");
  180. // when
  181. UA_StatusCode retval = UA_NetworkMessage_decodeJson(&out, &buf);
  182. // then
  183. ck_assert_int_eq(retval, UA_STATUSCODE_GOOD);
  184. //NetworkMessage
  185. ck_assert_int_eq(out.chunkMessage, false);
  186. ck_assert_int_eq(out.dataSetClassIdEnabled, false);
  187. ck_assert_int_eq(out.groupHeaderEnabled, false);
  188. ck_assert_int_eq(out.networkMessageType, UA_NETWORKMESSAGE_DATASET);
  189. ck_assert_int_eq(out.picosecondsEnabled, false);
  190. ck_assert_int_eq(out.promotedFieldsEnabled, false);
  191. ck_assert_int_eq(out.securityEnabled, false);
  192. ck_assert_int_eq(out.timestampEnabled, false);
  193. ck_assert_int_eq(out.publisherIdEnabled, false);
  194. ck_assert_int_eq(out.payloadHeaderEnabled, true);
  195. ck_assert_int_eq(out.payloadHeader.dataSetPayloadHeader.count, 1);
  196. ck_assert_int_eq(out.payloadHeader.dataSetPayloadHeader.dataSetWriterIds[0], 62541);
  197. //dataSetMessage
  198. ck_assert_int_eq(out.payload.dataSetPayload.dataSetMessages[0].header.dataSetMessageSequenceNrEnabled, true);
  199. ck_assert_int_eq(out.payload.dataSetPayload.dataSetMessages[0].header.dataSetMessageSequenceNr, 4711);
  200. ck_assert_int_eq(out.payload.dataSetPayload.dataSetMessages[0].header.dataSetMessageType, UA_DATASETMESSAGE_DATAKEYFRAME);
  201. ck_assert_int_eq(out.payload.dataSetPayload.dataSetMessages[0].header.fieldEncoding, UA_FIELDENCODING_VARIANT);
  202. ck_assert_int_eq(out.payload.dataSetPayload.dataSetMessages[0].header.picoSecondsIncluded, false);
  203. ck_assert_int_eq(out.payload.dataSetPayload.dataSetMessages[0].header.configVersionMinorVersionEnabled, true);
  204. ck_assert_int_eq(out.payload.dataSetPayload.dataSetMessages[0].header.configVersionMajorVersionEnabled, true);
  205. ck_assert_int_eq(out.payload.dataSetPayload.dataSetMessages[0].header.configVersionMinorVersion, 12345);
  206. ck_assert_int_eq(out.payload.dataSetPayload.dataSetMessages[0].header.configVersionMajorVersion, 1478393530);
  207. ck_assert_int_eq(out.payload.dataSetPayload.dataSetMessages[0].header.dataSetMessageSequenceNr, 4711);
  208. //ck_assert_int_eq(out.payload.dataSetPayload.dataSetMessages[0].header.dataSetWriterId, 62541);
  209. ck_assert_int_eq(out.payload.dataSetPayload.dataSetMessages[0].data.keyFrameData.dataSetFields[0].hasValue, 1);
  210. ck_assert_int_eq(*((UA_UInt16*)out.payload.dataSetPayload.dataSetMessages[0].data.keyFrameData.dataSetFields[0].value.data), 42);
  211. ck_assert_int_eq(out.payload.dataSetPayload.dataSetMessages[0].data.keyFrameData.dataSetFields[1].hasValue, 1);
  212. UA_DateTime *dt = (UA_DateTime*)out.payload.dataSetPayload.dataSetMessages[0].data.keyFrameData.dataSetFields[1].value.data;
  213. UA_DateTimeStruct dts = UA_DateTime_toStruct(*dt);
  214. ck_assert_int_eq(dts.year, 2018);
  215. ck_assert_int_eq(dts.month, 6);
  216. ck_assert_int_eq(dts.day, 5);
  217. ck_assert_int_eq(dts.hour, 5);
  218. ck_assert_int_eq(dts.min, 58);
  219. ck_assert_int_eq(dts.sec, 36);
  220. ck_assert_int_eq(dts.milliSec, 0);
  221. ck_assert_int_eq(dts.microSec, 0);
  222. ck_assert_int_eq(dts.nanoSec, 0);
  223. UA_NetworkMessage_deleteMembers(&out);
  224. }
  225. END_TEST
  226. START_TEST(UA_NetworkMessage_json_decode) {
  227. // given
  228. UA_NetworkMessage out;
  229. memset(&out,0,sizeof(UA_NetworkMessage));
  230. UA_ByteString buf = UA_STRING("{\"MessageId\":\"5ED82C10-50BB-CD07-0120-22521081E8EE\",\"MessageType\":\"ua-data\",\"Messages\":[{\"MetaDataVersion\":{\"MajorVersion\": 47, \"MinorVersion\": 47},\"DataSetWriterId\":62541,\"Status\":22,\"SequenceNumber\":4711,\"Payload\":{\"Test\":{\"Type\":5,\"Body\":42},\"Server localtime\":{\"Type\":1,\"Body\":true}}}]}");
  231. // when
  232. UA_StatusCode retval = UA_NetworkMessage_decodeJson(&out, &buf);
  233. // then
  234. ck_assert_int_eq(retval, UA_STATUSCODE_GOOD);
  235. //NetworkMessage
  236. ck_assert_int_eq(out.chunkMessage, false);
  237. ck_assert_int_eq(out.dataSetClassIdEnabled, false);
  238. ck_assert_int_eq(out.groupHeaderEnabled, false);
  239. ck_assert_int_eq(out.networkMessageType, UA_NETWORKMESSAGE_DATASET);
  240. ck_assert_int_eq(out.picosecondsEnabled, false);
  241. ck_assert_int_eq(out.promotedFieldsEnabled, false);
  242. ck_assert_int_eq(out.securityEnabled, false);
  243. ck_assert_int_eq(out.timestampEnabled, false);
  244. ck_assert_int_eq(out.publisherIdEnabled, false);
  245. ck_assert_int_eq(out.payloadHeaderEnabled, true);
  246. ck_assert_int_eq(out.payloadHeader.dataSetPayloadHeader.count, 1);
  247. ck_assert_int_eq(out.payloadHeader.dataSetPayloadHeader.dataSetWriterIds[0], 62541);
  248. //dataSetMessage
  249. ck_assert_int_eq(out.payload.dataSetPayload.dataSetMessages[0].header.dataSetMessageSequenceNrEnabled, true);
  250. ck_assert_int_eq(out.payload.dataSetPayload.dataSetMessages[0].header.dataSetMessageSequenceNr, 4711);
  251. ck_assert_int_eq(out.payload.dataSetPayload.dataSetMessages[0].header.dataSetMessageType, UA_DATASETMESSAGE_DATAKEYFRAME);
  252. ck_assert_int_eq(out.payload.dataSetPayload.dataSetMessages[0].header.fieldEncoding, UA_FIELDENCODING_VARIANT);
  253. ck_assert_int_eq(out.payload.dataSetPayload.dataSetMessages[0].header.picoSecondsIncluded, false);
  254. ck_assert_int_eq(out.payload.dataSetPayload.dataSetMessages[0].header.statusEnabled, true);
  255. ck_assert_int_eq(out.payload.dataSetPayload.dataSetMessages[0].header.status, 22);
  256. ck_assert_int_eq(out.payload.dataSetPayload.dataSetMessages[0].header.configVersionMinorVersionEnabled, true);
  257. ck_assert_int_eq(out.payload.dataSetPayload.dataSetMessages[0].header.configVersionMajorVersionEnabled, true);
  258. ck_assert_int_eq(out.payload.dataSetPayload.dataSetMessages[0].header.configVersionMinorVersion, 47);
  259. ck_assert_int_eq(out.payload.dataSetPayload.dataSetMessages[0].header.configVersionMajorVersion, 47);
  260. //dataSetFields
  261. ck_assert_int_eq(out.payload.dataSetPayload.dataSetMessages[0].data.keyFrameData.dataSetFields[0].hasValue, true);
  262. ck_assert_int_eq(*((UA_UInt16*)out.payload.dataSetPayload.dataSetMessages[0].data.keyFrameData.dataSetFields[0].value.data), 42);
  263. ck_assert_int_eq(out.payload.dataSetPayload.dataSetMessages[0].data.keyFrameData.dataSetFields[1].hasValue, true);
  264. ck_assert_int_eq(*((UA_Boolean*)out.payload.dataSetPayload.dataSetMessages[0].data.keyFrameData.dataSetFields[1].value.data), 1);
  265. UA_NetworkMessage_deleteMembers(&out);
  266. }
  267. END_TEST
  268. START_TEST(UA_Networkmessage_DataSetFieldsNull_json_decode) {
  269. // given
  270. UA_NetworkMessage out;
  271. memset(&out, 0, sizeof(UA_NetworkMessage));
  272. UA_ByteString buf = UA_STRING("{ \"MessageId\": \"32235546-05d9-4fd7-97df-ea3ff3408574\", "
  273. "\"MessageType\": \"ua-data\", \"PublisherId\": \"MQTT-Localhost\", "
  274. "\"DataSetClassId\": \"00000005-cab9-4470-8f8a-2c1ead207e0e\", \"Messages\": "
  275. "[ { \"DataSetWriterId\": 1, \"SequenceNumber\": 224, \"MetaDataVersion\": "
  276. "{ \"MajorVersion\": 1, \"MinorVersion\": 1 },\"Payload\":null}]}");
  277. // when
  278. UA_StatusCode retval = UA_NetworkMessage_decodeJson(&out, &buf);
  279. // then
  280. ck_assert_int_eq(retval, UA_STATUSCODE_GOOD);
  281. ck_assert_int_eq(out.dataSetClassId.data1, 5);
  282. ck_assert_int_eq(out.payload.dataSetPayload.dataSetMessages->header.dataSetMessageSequenceNr, 224);
  283. ck_assert_ptr_eq(out.payload.dataSetPayload.dataSetMessages->data.keyFrameData.dataSetFields, NULL);
  284. UA_NetworkMessage_deleteMembers(&out);
  285. }
  286. END_TEST
  287. START_TEST(UA_NetworkMessage_fieldNames_json_decode) {
  288. // given
  289. UA_NetworkMessage out;
  290. UA_ByteString buf = UA_STRING("{\"MessageId\":\"5ED82C10-50BB-CD07-0120-22521081E8EE\","
  291. "\"MessageType\":\"ua-data\",\"Messages\":"
  292. "[{\"DataSetWriterId\":62541,\"MetaDataVersion\":"
  293. "{\"MajorVersion\":1478393530,\"MinorVersion\":12345},"
  294. "\"SequenceNumber\":4711,\"Payload\":{\"Test\":{\"Type\":5,\"Body\":42},\"Test2\":"
  295. "{\"Type\":13,\"Body\":\"2018-06-05T05:58:36.000Z\"}}}]}");
  296. // when
  297. UA_StatusCode retval = UA_NetworkMessage_decodeJson(&out, &buf);
  298. // then
  299. ck_assert_int_eq(retval, UA_STATUSCODE_GOOD);
  300. //NetworkMessage
  301. ck_assert_int_eq(out.payload.dataSetPayload.dataSetMessages[0].data.keyFrameData.fieldNames[0].data[0], 'T');
  302. ck_assert_int_eq(out.payload.dataSetPayload.dataSetMessages[0].data.keyFrameData.fieldNames[0].data[1], 'e');
  303. ck_assert_int_eq(out.payload.dataSetPayload.dataSetMessages[0].data.keyFrameData.fieldNames[0].data[2], 's');
  304. ck_assert_int_eq(out.payload.dataSetPayload.dataSetMessages[0].data.keyFrameData.fieldNames[0].data[3], 't');
  305. ck_assert_int_eq(out.payload.dataSetPayload.dataSetMessages[0].data.keyFrameData.fieldNames[1].data[0], 'T');
  306. ck_assert_int_eq(out.payload.dataSetPayload.dataSetMessages[0].data.keyFrameData.fieldNames[1].data[1], 'e');
  307. ck_assert_int_eq(out.payload.dataSetPayload.dataSetMessages[0].data.keyFrameData.fieldNames[1].data[2], 's');
  308. ck_assert_int_eq(out.payload.dataSetPayload.dataSetMessages[0].data.keyFrameData.fieldNames[1].data[3], 't');
  309. ck_assert_int_eq(out.payload.dataSetPayload.dataSetMessages[0].data.keyFrameData.fieldNames[1].data[4], '2');
  310. UA_NetworkMessage_deleteMembers(&out);
  311. }
  312. END_TEST
  313. static Suite *testSuite_networkmessage(void) {
  314. Suite *s = suite_create("Built-in Data Types 62541-6 Json");
  315. TCase *tc_json_networkmessage = tcase_create("networkmessage_json");
  316. tcase_add_test(tc_json_networkmessage, UA_PubSub_EncodeAllOptionalFields);
  317. tcase_add_test(tc_json_networkmessage, UA_PubSub_EnDecode);
  318. tcase_add_test(tc_json_networkmessage, UA_NetworkMessage_oneMessage_twoFields_json_decode);
  319. tcase_add_test(tc_json_networkmessage, UA_NetworkMessage_json_decode);
  320. tcase_add_test(tc_json_networkmessage, UA_Networkmessage_DataSetFieldsNull_json_decode);
  321. tcase_add_test(tc_json_networkmessage, UA_NetworkMessage_fieldNames_json_decode);
  322. suite_add_tcase(s, tc_json_networkmessage);
  323. return s;
  324. }
  325. int main(void) {
  326. int number_failed = 0;
  327. Suite *s;
  328. SRunner *sr;
  329. s = testSuite_networkmessage();
  330. sr = srunner_create(s);
  331. srunner_set_fork_status(sr, CK_NOFORK);
  332. srunner_run_all(sr, CK_NORMAL);
  333. number_failed += srunner_ntests_failed(sr);
  334. srunner_free(sr);
  335. return (number_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
  336. }