check_pubsub_encoding.c 82 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381
  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) 2017 - 2018 Fraunhofer IOSB (Author: Tino Bischoff)
  6. */
  7. #include <open62541/client.h>
  8. #include <open62541/types.h>
  9. #include "ua_pubsub_networkmessage.h"
  10. #include "check.h"
  11. START_TEST(UA_PubSub_EnDecode_ShallWorkOn1DS1ValueVariantKeyFrame) {
  12. UA_NetworkMessage m;
  13. memset(&m, 0, sizeof(UA_NetworkMessage));
  14. m.version = 1;
  15. m.networkMessageType = UA_NETWORKMESSAGE_DATASET;
  16. UA_DataSetMessage dmkf;
  17. memset(&dmkf, 0, sizeof(UA_DataSetMessage));
  18. dmkf.header.dataSetMessageValid = true;
  19. dmkf.header.fieldEncoding = UA_FIELDENCODING_VARIANT;
  20. dmkf.header.dataSetMessageType = UA_DATASETMESSAGE_DATAKEYFRAME;
  21. dmkf.data.keyFrameData.fieldCount = 1;
  22. dmkf.data.keyFrameData.dataSetFields =
  23. (UA_DataValue*)UA_Array_new(dmkf.data.keyFrameData.fieldCount, &UA_TYPES[UA_TYPES_DATAVALUE]);
  24. UA_DataValue_init(&dmkf.data.keyFrameData.dataSetFields[0]);
  25. UA_Int32 iv = 27;
  26. UA_Variant_setScalarCopy(&dmkf.data.keyFrameData.dataSetFields[0].value, &iv, &UA_TYPES[UA_TYPES_INT32]);
  27. dmkf.data.keyFrameData.dataSetFields[0].hasValue = true;
  28. m.payload.dataSetPayload.dataSetMessages = &dmkf;
  29. UA_StatusCode rv = UA_STATUSCODE_UNCERTAININITIALVALUE;
  30. UA_ByteString buffer;
  31. size_t msgSize = UA_NetworkMessage_calcSizeBinary(&m);
  32. rv = UA_ByteString_allocBuffer(&buffer, msgSize);
  33. ck_assert_int_eq(rv, UA_STATUSCODE_GOOD);
  34. //if (rv == UA_STATUSCODE_GOOD) {
  35. UA_Byte *bufPos = buffer.data;
  36. memset(bufPos, 0, msgSize);
  37. const UA_Byte *bufEnd = &(buffer.data[buffer.length]);
  38. rv = UA_NetworkMessage_encodeBinary(&m, &bufPos, bufEnd);
  39. ck_assert_int_eq(rv, UA_STATUSCODE_GOOD);
  40. UA_NetworkMessage m2;
  41. memset(&m2, 0, sizeof(UA_NetworkMessage));
  42. size_t offset = 0;
  43. rv = UA_NetworkMessage_decodeBinary(&buffer, &offset, &m2);
  44. ck_assert_int_eq(rv, UA_STATUSCODE_GOOD);
  45. ck_assert(m.version == m2.version);
  46. ck_assert(m.networkMessageType == m2.networkMessageType);
  47. ck_assert(m.timestampEnabled == m2.timestampEnabled);
  48. ck_assert(m.dataSetClassIdEnabled == m2.dataSetClassIdEnabled);
  49. ck_assert(m.groupHeaderEnabled == m2.groupHeaderEnabled);
  50. ck_assert(m.picosecondsEnabled == m2.picosecondsEnabled);
  51. ck_assert(m.promotedFieldsEnabled == m2.promotedFieldsEnabled);
  52. ck_assert(m.publisherIdEnabled == m2.publisherIdEnabled);
  53. ck_assert(m.securityEnabled == m2.securityEnabled);
  54. ck_assert(m.chunkMessage == m2.chunkMessage);
  55. ck_assert(m.payloadHeaderEnabled == m2.payloadHeaderEnabled);
  56. ck_assert(m.payload.dataSetPayload.dataSetMessages[0].header.dataSetMessageValid == m2.payload.dataSetPayload.dataSetMessages[0].header.dataSetMessageValid);
  57. ck_assert(m.payload.dataSetPayload.dataSetMessages[0].header.fieldEncoding == m2.payload.dataSetPayload.dataSetMessages[0].header.fieldEncoding);
  58. ck_assert_int_eq(m2.payload.dataSetPayload.dataSetMessages[0].data.keyFrameData.fieldCount, 1);
  59. ck_assert(m.payload.dataSetPayload.dataSetMessages[0].data.keyFrameData.dataSetFields[0].hasValue == m2.payload.dataSetPayload.dataSetMessages[0].data.keyFrameData.dataSetFields[0].hasValue);
  60. ck_assert_int_eq((uintptr_t)m2.payload.dataSetPayload.dataSetMessages[0].data.keyFrameData.dataSetFields[0].value.type, (uintptr_t)&UA_TYPES[UA_TYPES_INT32]);
  61. ck_assert_int_eq(*(UA_Int32 *)m2.payload.dataSetPayload.dataSetMessages[0].data.keyFrameData.dataSetFields[0].value.data, iv);
  62. ck_assert(m.payload.dataSetPayload.dataSetMessages[0].data.keyFrameData.dataSetFields[0].hasSourceTimestamp == m2.payload.dataSetPayload.dataSetMessages[0].data.keyFrameData.dataSetFields[0].hasSourceTimestamp);
  63. //}
  64. UA_DataValue_deleteMembers(&dmkf.data.keyFrameData.dataSetFields[0]);
  65. UA_NetworkMessage_deleteMembers(&m2);
  66. UA_ByteString_deleteMembers(&buffer);
  67. UA_Array_delete(dmkf.data.keyFrameData.dataSetFields, dmkf.data.keyFrameData.fieldCount, &UA_TYPES[UA_TYPES_DATAVALUE]);
  68. }
  69. END_TEST
  70. START_TEST(UA_PubSub_EnDecode_ShallWorkOn1DS1ValueDataValueKeyFrame) {
  71. UA_NetworkMessage m;
  72. memset(&m, 0, sizeof(UA_NetworkMessage));
  73. m.version = 1;
  74. m.networkMessageType = UA_NETWORKMESSAGE_DATASET;
  75. UA_DataSetMessage dmkf;
  76. memset(&dmkf, 0, sizeof(UA_DataSetMessage));
  77. dmkf.header.dataSetMessageValid = true;
  78. dmkf.header.fieldEncoding = UA_FIELDENCODING_DATAVALUE;
  79. dmkf.header.dataSetMessageType = UA_DATASETMESSAGE_DATAKEYFRAME;
  80. dmkf.data.keyFrameData.fieldCount = 1;
  81. dmkf.data.keyFrameData.dataSetFields =
  82. (UA_DataValue*)UA_Array_new(dmkf.data.keyFrameData.fieldCount, &UA_TYPES[UA_TYPES_DATAVALUE]);
  83. UA_DataValue_init(&dmkf.data.keyFrameData.dataSetFields[0]);
  84. UA_Int32 iv = 27;
  85. UA_Variant_setScalarCopy(&dmkf.data.keyFrameData.dataSetFields[0].value, &iv, &UA_TYPES[UA_TYPES_INT32]);
  86. dmkf.data.keyFrameData.dataSetFields[0].hasValue = true;
  87. m.payload.dataSetPayload.dataSetMessages = &dmkf;
  88. UA_StatusCode rv = UA_STATUSCODE_UNCERTAININITIALVALUE;
  89. UA_ByteString buffer;
  90. size_t msgSize = UA_NetworkMessage_calcSizeBinary(&m);
  91. rv = UA_ByteString_allocBuffer(&buffer, msgSize);
  92. ck_assert_int_eq(rv, UA_STATUSCODE_GOOD);
  93. //if (rv == UA_STATUSCODE_GOOD) {
  94. UA_Byte *bufPos = buffer.data;
  95. memset(bufPos, 0, msgSize);
  96. const UA_Byte *bufEnd = &(buffer.data[buffer.length]);
  97. rv = UA_NetworkMessage_encodeBinary(&m, &bufPos, bufEnd);
  98. ck_assert_int_eq(rv, UA_STATUSCODE_GOOD);
  99. UA_NetworkMessage m2;
  100. memset(&m2, 0, sizeof(UA_NetworkMessage));
  101. size_t offset = 0;
  102. rv = UA_NetworkMessage_decodeBinary(&buffer, &offset, &m2);
  103. ck_assert_int_eq(rv, UA_STATUSCODE_GOOD);
  104. ck_assert(m.version == m2.version);
  105. ck_assert(m.networkMessageType == m2.networkMessageType);
  106. ck_assert(m.timestampEnabled == m2.timestampEnabled);
  107. ck_assert(m.payloadHeaderEnabled == m2.payloadHeaderEnabled);
  108. ck_assert(m.payload.dataSetPayload.dataSetMessages[0].header.dataSetMessageValid == m2.payload.dataSetPayload.dataSetMessages[0].header.dataSetMessageValid);
  109. ck_assert(m.payload.dataSetPayload.dataSetMessages[0].header.fieldEncoding == m2.payload.dataSetPayload.dataSetMessages[0].header.fieldEncoding);
  110. ck_assert_int_eq(m2.payload.dataSetPayload.dataSetMessages[0].data.keyFrameData.fieldCount, 1);
  111. ck_assert(m.payload.dataSetPayload.dataSetMessages[0].data.keyFrameData.dataSetFields[0].hasValue == m2.payload.dataSetPayload.dataSetMessages[0].data.keyFrameData.dataSetFields[0].hasValue);
  112. ck_assert_int_eq((uintptr_t)m2.payload.dataSetPayload.dataSetMessages[0].data.keyFrameData.dataSetFields[0].value.type, (uintptr_t)&UA_TYPES[UA_TYPES_INT32]);
  113. ck_assert_int_eq(*(UA_Int32 *)m2.payload.dataSetPayload.dataSetMessages[0].data.keyFrameData.dataSetFields[0].value.data, iv);
  114. ck_assert(m.payload.dataSetPayload.dataSetMessages[0].data.keyFrameData.dataSetFields[0].hasSourceTimestamp == m2.payload.dataSetPayload.dataSetMessages[0].data.keyFrameData.dataSetFields[0].hasSourceTimestamp);
  115. ck_assert(m.dataSetClassIdEnabled == m2.dataSetClassIdEnabled);
  116. ck_assert(m.groupHeaderEnabled == m2.groupHeaderEnabled);
  117. ck_assert(m.picosecondsEnabled == m2.picosecondsEnabled);
  118. ck_assert(m.promotedFieldsEnabled == m2.promotedFieldsEnabled);
  119. ck_assert(m.publisherIdEnabled == m2.publisherIdEnabled);
  120. ck_assert(m.securityEnabled == m2.securityEnabled);
  121. ck_assert(m.chunkMessage == m2.chunkMessage);
  122. //}
  123. UA_DataValue_deleteMembers(&dmkf.data.keyFrameData.dataSetFields[0]);
  124. UA_NetworkMessage_deleteMembers(&m2);
  125. UA_ByteString_deleteMembers(&buffer);
  126. UA_Array_delete(dmkf.data.keyFrameData.dataSetFields, dmkf.data.keyFrameData.fieldCount, &UA_TYPES[UA_TYPES_DATAVALUE]);
  127. }
  128. END_TEST
  129. START_TEST(UA_PubSub_EnDecode_ShallWorkOn1DS2ValuesVariantKeyFrame) {
  130. UA_NetworkMessage m;
  131. memset(&m, 0, sizeof(UA_NetworkMessage));
  132. m.version = 1;
  133. m.networkMessageType = UA_NETWORKMESSAGE_DATASET;
  134. UA_DataSetMessage dmkf;
  135. memset(&dmkf, 0, sizeof(UA_DataSetMessage));
  136. dmkf.header.dataSetMessageValid = true;
  137. dmkf.header.fieldEncoding = UA_FIELDENCODING_DATAVALUE;
  138. dmkf.header.dataSetMessageType = UA_DATASETMESSAGE_DATAKEYFRAME;
  139. UA_UInt16 anzFields = 2;
  140. dmkf.data.keyFrameData.fieldCount = anzFields;
  141. dmkf.data.keyFrameData.dataSetFields =
  142. (UA_DataValue*)UA_Array_new(dmkf.data.keyFrameData.fieldCount, &UA_TYPES[UA_TYPES_DATAVALUE]);
  143. UA_DataValue_init(&dmkf.data.keyFrameData.dataSetFields[0]);
  144. UA_DataValue_init(&dmkf.data.keyFrameData.dataSetFields[1]);
  145. UA_Double dv = 231.3;
  146. UA_Variant_setScalarCopy(&dmkf.data.keyFrameData.dataSetFields[0].value, &dv, &UA_TYPES[UA_TYPES_DOUBLE]);
  147. dmkf.data.keyFrameData.dataSetFields[0].hasValue = true;
  148. UA_UInt64 uiv = 98412;
  149. UA_Variant_setScalarCopy(&dmkf.data.keyFrameData.dataSetFields[1].value, &uiv, &UA_TYPES[UA_TYPES_UINT64]);
  150. dmkf.data.keyFrameData.dataSetFields[1].hasValue = true;
  151. m.payload.dataSetPayload.dataSetMessages = &dmkf;
  152. UA_StatusCode rv = UA_STATUSCODE_UNCERTAININITIALVALUE;
  153. UA_ByteString buffer;
  154. size_t msgSize = UA_NetworkMessage_calcSizeBinary(&m);
  155. rv = UA_ByteString_allocBuffer(&buffer, msgSize);
  156. ck_assert_int_eq(rv, UA_STATUSCODE_GOOD);
  157. UA_Byte *bufPos = buffer.data;
  158. memset(bufPos, 0, msgSize);
  159. const UA_Byte *bufEnd = &(buffer.data[buffer.length]);
  160. rv = UA_NetworkMessage_encodeBinary(&m, &bufPos, bufEnd);
  161. ck_assert_int_eq(rv, UA_STATUSCODE_GOOD);
  162. UA_NetworkMessage m2;
  163. memset(&m2, 0, sizeof(UA_NetworkMessage));
  164. size_t offset = 0;
  165. rv = UA_NetworkMessage_decodeBinary(&buffer, &offset, &m2);
  166. ck_assert_int_eq(rv, UA_STATUSCODE_GOOD);
  167. ck_assert(m.version == m2.version);
  168. ck_assert(m.networkMessageType == m2.networkMessageType);
  169. ck_assert(m.timestampEnabled == m2.timestampEnabled);
  170. ck_assert(m.payloadHeaderEnabled == m2.payloadHeaderEnabled);
  171. ck_assert(m.payload.dataSetPayload.dataSetMessages[0].header.dataSetMessageValid == m2.payload.dataSetPayload.dataSetMessages[0].header.dataSetMessageValid);
  172. ck_assert(m.payload.dataSetPayload.dataSetMessages[0].header.fieldEncoding == m2.payload.dataSetPayload.dataSetMessages[0].header.fieldEncoding);
  173. ck_assert_int_eq(m2.payload.dataSetPayload.dataSetMessages[0].data.keyFrameData.fieldCount, anzFields);
  174. ck_assert(m.payload.dataSetPayload.dataSetMessages[0].data.keyFrameData.dataSetFields[0].hasValue == m2.payload.dataSetPayload.dataSetMessages[0].data.keyFrameData.dataSetFields[0].hasValue);
  175. ck_assert_int_eq((uintptr_t)m2.payload.dataSetPayload.dataSetMessages[0].data.keyFrameData.dataSetFields[0].value.type, (uintptr_t)&UA_TYPES[UA_TYPES_DOUBLE]);
  176. ck_assert(*(UA_Double*)m2.payload.dataSetPayload.dataSetMessages[0].data.keyFrameData.dataSetFields[0].value.data == dv);
  177. ck_assert(m.payload.dataSetPayload.dataSetMessages[0].data.keyFrameData.dataSetFields[1].hasValue == m2.payload.dataSetPayload.dataSetMessages[0].data.keyFrameData.dataSetFields[0].hasValue);
  178. ck_assert_int_eq((uintptr_t)m2.payload.dataSetPayload.dataSetMessages[0].data.keyFrameData.dataSetFields[1].value.type, (uintptr_t)&UA_TYPES[UA_TYPES_UINT64]);
  179. ck_assert_uint_eq(*(UA_UInt64 *)m2.payload.dataSetPayload.dataSetMessages[0].data.keyFrameData.dataSetFields[1].value.data, uiv);
  180. ck_assert(m.payload.dataSetPayload.dataSetMessages[0].data.keyFrameData.dataSetFields[0].hasSourceTimestamp == m2.payload.dataSetPayload.dataSetMessages[0].data.keyFrameData.dataSetFields[0].hasSourceTimestamp);
  181. ck_assert(m.payload.dataSetPayload.dataSetMessages[0].data.keyFrameData.dataSetFields[1].hasSourceTimestamp == m2.payload.dataSetPayload.dataSetMessages[0].data.keyFrameData.dataSetFields[0].hasSourceTimestamp);
  182. ck_assert(m.dataSetClassIdEnabled == m2.dataSetClassIdEnabled);
  183. ck_assert(m.groupHeaderEnabled == m2.groupHeaderEnabled);
  184. ck_assert(m.picosecondsEnabled == m2.picosecondsEnabled);
  185. ck_assert(m.promotedFieldsEnabled == m2.promotedFieldsEnabled);
  186. ck_assert(m.publisherIdEnabled == m2.publisherIdEnabled);
  187. ck_assert(m.securityEnabled == m2.securityEnabled);
  188. ck_assert(m.chunkMessage == m2.chunkMessage);
  189. UA_DataValue_deleteMembers(&dmkf.data.keyFrameData.dataSetFields[0]);
  190. UA_DataValue_deleteMembers(&dmkf.data.keyFrameData.dataSetFields[1]);
  191. UA_NetworkMessage_deleteMembers(&m2);
  192. UA_ByteString_deleteMembers(&buffer);
  193. UA_Array_delete(dmkf.data.keyFrameData.dataSetFields, dmkf.data.keyFrameData.fieldCount, &UA_TYPES[UA_TYPES_DATAVALUE]);
  194. }
  195. END_TEST
  196. START_TEST(UA_PubSub_EnDecode_ShallWorkOn1DS2ValuesDataValueKeyFrame) {
  197. UA_NetworkMessage m;
  198. memset(&m, 0, sizeof(UA_NetworkMessage));
  199. m.version = 1;
  200. m.networkMessageType = UA_NETWORKMESSAGE_DATASET;
  201. UA_DataSetMessage dmkf;
  202. memset(&dmkf, 0, sizeof(UA_DataSetMessage));
  203. dmkf.header.dataSetMessageValid = true;
  204. dmkf.header.fieldEncoding = UA_FIELDENCODING_DATAVALUE;
  205. dmkf.header.dataSetMessageType = UA_DATASETMESSAGE_DATAKEYFRAME;
  206. UA_UInt16 anzFields = 2;
  207. dmkf.data.keyFrameData.fieldCount = anzFields;
  208. dmkf.data.keyFrameData.dataSetFields =
  209. (UA_DataValue*)UA_Array_new(dmkf.data.keyFrameData.fieldCount, &UA_TYPES[UA_TYPES_DATAVALUE]);
  210. UA_DataValue_init(&dmkf.data.keyFrameData.dataSetFields[0]);
  211. UA_DataValue_init(&dmkf.data.keyFrameData.dataSetFields[1]);
  212. UA_Double dv = 231.3;
  213. UA_Variant_setScalarCopy(&dmkf.data.keyFrameData.dataSetFields[0].value, &dv, &UA_TYPES[UA_TYPES_DOUBLE]);
  214. dmkf.data.keyFrameData.dataSetFields[0].hasValue = true;
  215. UA_UInt64 uiv = 98412;
  216. UA_Variant_setScalarCopy(&dmkf.data.keyFrameData.dataSetFields[1].value, &uiv, &UA_TYPES[UA_TYPES_UINT64]);
  217. dmkf.data.keyFrameData.dataSetFields[1].hasValue = true;
  218. m.payload.dataSetPayload.dataSetMessages = &dmkf;
  219. UA_StatusCode rv = UA_STATUSCODE_UNCERTAININITIALVALUE;
  220. UA_ByteString buffer;
  221. size_t msgSize = UA_NetworkMessage_calcSizeBinary(&m);
  222. rv = UA_ByteString_allocBuffer(&buffer, msgSize);
  223. ck_assert_int_eq(rv, UA_STATUSCODE_GOOD);
  224. UA_Byte *bufPos = buffer.data;
  225. memset(bufPos, 0, msgSize);
  226. const UA_Byte *bufEnd = &(buffer.data[buffer.length]);
  227. rv = UA_NetworkMessage_encodeBinary(&m, &bufPos, bufEnd);
  228. ck_assert_int_eq(rv, UA_STATUSCODE_GOOD);
  229. UA_NetworkMessage m2;
  230. memset(&m2, 0, sizeof(UA_NetworkMessage));
  231. size_t offset = 0;
  232. rv = UA_NetworkMessage_decodeBinary(&buffer, &offset, &m2);
  233. ck_assert_int_eq(rv, UA_STATUSCODE_GOOD);
  234. ck_assert(m.version == m2.version);
  235. ck_assert(m.networkMessageType == m2.networkMessageType);
  236. ck_assert(m.timestampEnabled == m2.timestampEnabled);
  237. ck_assert(m.payloadHeaderEnabled == m2.payloadHeaderEnabled);
  238. ck_assert(m.payload.dataSetPayload.dataSetMessages[0].header.dataSetMessageValid == m2.payload.dataSetPayload.dataSetMessages[0].header.dataSetMessageValid);
  239. ck_assert(m.payload.dataSetPayload.dataSetMessages[0].header.fieldEncoding == m2.payload.dataSetPayload.dataSetMessages[0].header.fieldEncoding);
  240. ck_assert_int_eq(m2.payload.dataSetPayload.dataSetMessages[0].data.keyFrameData.fieldCount, anzFields);
  241. ck_assert(m.payload.dataSetPayload.dataSetMessages[0].data.keyFrameData.dataSetFields[0].hasValue == m2.payload.dataSetPayload.dataSetMessages[0].data.keyFrameData.dataSetFields[0].hasValue);
  242. ck_assert_int_eq((uintptr_t)m2.payload.dataSetPayload.dataSetMessages[0].data.keyFrameData.dataSetFields[0].value.type, (uintptr_t)&UA_TYPES[UA_TYPES_DOUBLE]);
  243. ck_assert(*(UA_Double*)m2.payload.dataSetPayload.dataSetMessages[0].data.keyFrameData.dataSetFields[0].value.data == dv);
  244. ck_assert(m.payload.dataSetPayload.dataSetMessages[0].data.keyFrameData.dataSetFields[1].hasValue == m2.payload.dataSetPayload.dataSetMessages[0].data.keyFrameData.dataSetFields[0].hasValue);
  245. ck_assert_int_eq((uintptr_t)m2.payload.dataSetPayload.dataSetMessages[0].data.keyFrameData.dataSetFields[1].value.type, (uintptr_t)&UA_TYPES[UA_TYPES_UINT64]);
  246. ck_assert_uint_eq(*(UA_UInt64 *)m2.payload.dataSetPayload.dataSetMessages[0].data.keyFrameData.dataSetFields[1].value.data, uiv);
  247. ck_assert(m.payload.dataSetPayload.dataSetMessages[0].data.keyFrameData.dataSetFields[0].hasSourceTimestamp == m2.payload.dataSetPayload.dataSetMessages[0].data.keyFrameData.dataSetFields[0].hasSourceTimestamp);
  248. ck_assert(m.payload.dataSetPayload.dataSetMessages[0].data.keyFrameData.dataSetFields[1].hasSourceTimestamp == m2.payload.dataSetPayload.dataSetMessages[0].data.keyFrameData.dataSetFields[0].hasSourceTimestamp);
  249. ck_assert(m.dataSetClassIdEnabled == m2.dataSetClassIdEnabled);
  250. ck_assert(m.groupHeaderEnabled == m2.groupHeaderEnabled);
  251. ck_assert(m.picosecondsEnabled == m2.picosecondsEnabled);
  252. ck_assert(m.promotedFieldsEnabled == m2.promotedFieldsEnabled);
  253. ck_assert(m.publisherIdEnabled == m2.publisherIdEnabled);
  254. ck_assert(m.securityEnabled == m2.securityEnabled);
  255. ck_assert(m.chunkMessage == m2.chunkMessage);
  256. UA_DataValue_deleteMembers(&dmkf.data.keyFrameData.dataSetFields[0]);
  257. UA_DataValue_deleteMembers(&dmkf.data.keyFrameData.dataSetFields[1]);
  258. UA_NetworkMessage_deleteMembers(&m2);
  259. UA_ByteString_deleteMembers(&buffer);
  260. UA_Array_delete(dmkf.data.keyFrameData.dataSetFields, dmkf.data.keyFrameData.fieldCount, &UA_TYPES[UA_TYPES_DATAVALUE]);
  261. }
  262. END_TEST
  263. START_TEST(UA_PubSub_EnDecode_ShallWorkOn1DS1ValueVariantDeltaFrame) {
  264. UA_NetworkMessage m;
  265. memset(&m, 0, sizeof(UA_NetworkMessage));
  266. m.version = 1;
  267. m.networkMessageType = UA_NETWORKMESSAGE_DATASET;
  268. UA_DataSetMessage dmdf;
  269. memset(&dmdf, 0, sizeof(UA_DataSetMessage));
  270. dmdf.header.dataSetMessageValid = true;
  271. dmdf.header.fieldEncoding = UA_FIELDENCODING_VARIANT;
  272. dmdf.header.dataSetMessageType = UA_DATASETMESSAGE_DATADELTAFRAME;
  273. dmdf.data.deltaFrameData.fieldCount = 1;
  274. size_t memsize = sizeof(UA_DataSetMessage_DeltaFrameField) * dmdf.data.deltaFrameData.fieldCount;
  275. dmdf.data.deltaFrameData.deltaFrameFields = (UA_DataSetMessage_DeltaFrameField*)UA_malloc(memsize);
  276. UA_UInt16 fieldIndex = 1;
  277. dmdf.data.deltaFrameData.deltaFrameFields[0].fieldIndex = fieldIndex;
  278. UA_DataValue_init(&dmdf.data.deltaFrameData.deltaFrameFields[0].fieldValue);
  279. UA_Int32 iv = 58;
  280. UA_Variant_setScalarCopy(&dmdf.data.deltaFrameData.deltaFrameFields[0].fieldValue.value, &iv, &UA_TYPES[UA_TYPES_INT32]);
  281. dmdf.data.deltaFrameData.deltaFrameFields[0].fieldValue.hasValue = true;
  282. m.payload.dataSetPayload.dataSetMessages = &dmdf;
  283. UA_StatusCode rv = UA_STATUSCODE_UNCERTAININITIALVALUE;
  284. UA_ByteString buffer;
  285. size_t msgSize = UA_NetworkMessage_calcSizeBinary(&m);
  286. rv = UA_ByteString_allocBuffer(&buffer, msgSize);
  287. ck_assert_int_eq(rv, UA_STATUSCODE_GOOD);
  288. UA_Byte *bufPos = buffer.data;
  289. memset(bufPos, 0, msgSize);
  290. const UA_Byte *bufEnd = &(buffer.data[buffer.length]);
  291. rv = UA_NetworkMessage_encodeBinary(&m, &bufPos, bufEnd);
  292. ck_assert_int_eq(rv, UA_STATUSCODE_GOOD);
  293. UA_NetworkMessage m2;
  294. memset(&m2, 0, sizeof(UA_NetworkMessage));
  295. size_t offset = 0;
  296. rv = UA_NetworkMessage_decodeBinary(&buffer, &offset, &m2);
  297. ck_assert_int_eq(rv, UA_STATUSCODE_GOOD);
  298. ck_assert(m.version == m2.version);
  299. ck_assert(m.networkMessageType == m2.networkMessageType);
  300. ck_assert(m.timestampEnabled == m2.timestampEnabled);
  301. ck_assert(m.payloadHeaderEnabled == m2.payloadHeaderEnabled);
  302. ck_assert(m.payload.dataSetPayload.dataSetMessages[0].header.dataSetMessageValid == m2.payload.dataSetPayload.dataSetMessages[0].header.dataSetMessageValid);
  303. ck_assert(m.payload.dataSetPayload.dataSetMessages[0].header.fieldEncoding == m2.payload.dataSetPayload.dataSetMessages[0].header.fieldEncoding);
  304. ck_assert_int_eq(m2.payload.dataSetPayload.dataSetMessages[0].data.deltaFrameData.fieldCount, 1);
  305. ck_assert_int_eq(m.payload.dataSetPayload.dataSetMessages[0].data.deltaFrameData.deltaFrameFields[0].fieldIndex, fieldIndex);
  306. ck_assert(m.payload.dataSetPayload.dataSetMessages[0].data.deltaFrameData.deltaFrameFields[0].fieldValue.hasValue == m2.payload.dataSetPayload.dataSetMessages[0].data.deltaFrameData.deltaFrameFields[0].fieldValue.hasValue);
  307. ck_assert_int_eq((uintptr_t)m2.payload.dataSetPayload.dataSetMessages[0].data.deltaFrameData.deltaFrameFields[0].fieldValue.value.type, (uintptr_t)&UA_TYPES[UA_TYPES_INT32]);
  308. ck_assert_int_eq(*(UA_Int32 *)m2.payload.dataSetPayload.dataSetMessages[0].data.deltaFrameData.deltaFrameFields[0].fieldValue.value.data, iv);
  309. ck_assert(m.payload.dataSetPayload.dataSetMessages[0].data.deltaFrameData.deltaFrameFields[0].fieldValue.hasSourceTimestamp == m2.payload.dataSetPayload.dataSetMessages[0].data.deltaFrameData.deltaFrameFields[0].fieldValue.hasSourceTimestamp);
  310. ck_assert(m.dataSetClassIdEnabled == m2.dataSetClassIdEnabled);
  311. ck_assert(m.groupHeaderEnabled == m2.groupHeaderEnabled);
  312. ck_assert(m.picosecondsEnabled == m2.picosecondsEnabled);
  313. ck_assert(m.promotedFieldsEnabled == m2.promotedFieldsEnabled);
  314. ck_assert(m.publisherIdEnabled == m2.publisherIdEnabled);
  315. ck_assert(m.securityEnabled == m2.securityEnabled);
  316. ck_assert(m.chunkMessage == m2.chunkMessage);
  317. UA_DataValue_deleteMembers(&dmdf.data.deltaFrameData.deltaFrameFields[0].fieldValue);
  318. UA_NetworkMessage_deleteMembers(&m2);
  319. UA_ByteString_deleteMembers(&buffer);
  320. UA_free(dmdf.data.deltaFrameData.deltaFrameFields);
  321. }
  322. END_TEST
  323. START_TEST(UA_PubSub_EnDecode_ShallWorkOn1DS1ValueDataValueDeltaFrame) {
  324. UA_NetworkMessage m;
  325. memset(&m, 0, sizeof(UA_NetworkMessage));
  326. m.version = 1;
  327. m.networkMessageType = UA_NETWORKMESSAGE_DATASET;
  328. UA_DataSetMessage dmdf;
  329. memset(&dmdf, 0, sizeof(UA_DataSetMessage));
  330. dmdf.header.dataSetMessageValid = true;
  331. dmdf.header.fieldEncoding = UA_FIELDENCODING_DATAVALUE;
  332. dmdf.header.dataSetMessageType = UA_DATASETMESSAGE_DATADELTAFRAME;
  333. dmdf.data.deltaFrameData.fieldCount = 1;
  334. size_t memsize = sizeof(UA_DataSetMessage_DeltaFrameField) * dmdf.data.deltaFrameData.fieldCount;
  335. dmdf.data.deltaFrameData.deltaFrameFields = (UA_DataSetMessage_DeltaFrameField*)UA_malloc(memsize);
  336. UA_UInt16 fieldIndex = 1;
  337. dmdf.data.deltaFrameData.deltaFrameFields[0].fieldIndex = fieldIndex;
  338. UA_DataValue_init(&dmdf.data.deltaFrameData.deltaFrameFields[0].fieldValue);
  339. UA_Int32 iv = 197;
  340. UA_Variant_setScalarCopy(&dmdf.data.deltaFrameData.deltaFrameFields[0].fieldValue.value, &iv, &UA_TYPES[UA_TYPES_INT32]);
  341. dmdf.data.deltaFrameData.deltaFrameFields[0].fieldValue.hasValue = true;
  342. m.payload.dataSetPayload.dataSetMessages = &dmdf;
  343. UA_StatusCode rv = UA_STATUSCODE_UNCERTAININITIALVALUE;
  344. UA_ByteString buffer;
  345. size_t msgSize = UA_NetworkMessage_calcSizeBinary(&m);
  346. rv = UA_ByteString_allocBuffer(&buffer, msgSize);
  347. ck_assert_int_eq(rv, UA_STATUSCODE_GOOD);
  348. UA_Byte *bufPos = buffer.data;
  349. memset(bufPos, 0, msgSize);
  350. const UA_Byte *bufEnd = &(buffer.data[buffer.length]);
  351. rv = UA_NetworkMessage_encodeBinary(&m, &bufPos, bufEnd);
  352. ck_assert_int_eq(rv, UA_STATUSCODE_GOOD);
  353. UA_NetworkMessage m2;
  354. memset(&m2, 0, sizeof(UA_NetworkMessage));
  355. size_t offset = 0;
  356. rv = UA_NetworkMessage_decodeBinary(&buffer, &offset, &m2);
  357. ck_assert_int_eq(rv, UA_STATUSCODE_GOOD);
  358. ck_assert(m.version == m2.version);
  359. ck_assert(m.networkMessageType == m2.networkMessageType);
  360. ck_assert(m.timestampEnabled == m2.timestampEnabled);
  361. ck_assert(m.payloadHeaderEnabled == m2.payloadHeaderEnabled);
  362. ck_assert(m.payload.dataSetPayload.dataSetMessages[0].header.dataSetMessageValid == m2.payload.dataSetPayload.dataSetMessages[0].header.dataSetMessageValid);
  363. ck_assert(m.payload.dataSetPayload.dataSetMessages[0].header.fieldEncoding == m2.payload.dataSetPayload.dataSetMessages[0].header.fieldEncoding);
  364. ck_assert_int_eq(m2.payload.dataSetPayload.dataSetMessages[0].data.deltaFrameData.fieldCount, 1);
  365. ck_assert_int_eq(m.payload.dataSetPayload.dataSetMessages[0].data.deltaFrameData.deltaFrameFields[0].fieldIndex, fieldIndex);
  366. ck_assert(m.payload.dataSetPayload.dataSetMessages[0].data.deltaFrameData.deltaFrameFields[0].fieldValue.hasValue == m2.payload.dataSetPayload.dataSetMessages[0].data.deltaFrameData.deltaFrameFields[0].fieldValue.hasValue);
  367. ck_assert_int_eq((uintptr_t)m2.payload.dataSetPayload.dataSetMessages[0].data.deltaFrameData.deltaFrameFields[0].fieldValue.value.type, (uintptr_t)&UA_TYPES[UA_TYPES_INT32]);
  368. ck_assert_int_eq(*(UA_Int32 *)m2.payload.dataSetPayload.dataSetMessages[0].data.deltaFrameData.deltaFrameFields[0].fieldValue.value.data, iv);
  369. ck_assert(m.payload.dataSetPayload.dataSetMessages[0].data.deltaFrameData.deltaFrameFields[0].fieldValue.hasSourceTimestamp == m2.payload.dataSetPayload.dataSetMessages[0].data.deltaFrameData.deltaFrameFields[0].fieldValue.hasSourceTimestamp);
  370. ck_assert(m.dataSetClassIdEnabled == m2.dataSetClassIdEnabled);
  371. ck_assert(m.groupHeaderEnabled == m2.groupHeaderEnabled);
  372. ck_assert(m.picosecondsEnabled == m2.picosecondsEnabled);
  373. ck_assert(m.promotedFieldsEnabled == m2.promotedFieldsEnabled);
  374. ck_assert(m.publisherIdEnabled == m2.publisherIdEnabled);
  375. ck_assert(m.securityEnabled == m2.securityEnabled);
  376. ck_assert(m.chunkMessage == m2.chunkMessage);
  377. UA_DataValue_deleteMembers(&dmdf.data.deltaFrameData.deltaFrameFields[0].fieldValue);
  378. UA_NetworkMessage_deleteMembers(&m2);
  379. UA_ByteString_deleteMembers(&buffer);
  380. UA_free(dmdf.data.deltaFrameData.deltaFrameFields);
  381. }
  382. END_TEST
  383. START_TEST(UA_PubSub_Encode_WithBufferTooSmallShallReturnError) {
  384. UA_NetworkMessage m;
  385. memset(&m, 0, sizeof(UA_NetworkMessage));
  386. m.version = 1;
  387. m.networkMessageType = UA_NETWORKMESSAGE_DATASET;
  388. UA_DataSetMessage dmkf;
  389. memset(&dmkf, 0, sizeof(UA_DataSetMessage));
  390. dmkf.header.dataSetMessageValid = true;
  391. dmkf.header.fieldEncoding = UA_FIELDENCODING_DATAVALUE;
  392. dmkf.header.dataSetMessageType = UA_DATASETMESSAGE_DATAKEYFRAME;
  393. dmkf.data.keyFrameData.fieldCount = 1;
  394. dmkf.data.keyFrameData.dataSetFields =
  395. (UA_DataValue*)UA_Array_new(dmkf.data.keyFrameData.fieldCount, &UA_TYPES[UA_TYPES_DATAVALUE]);
  396. UA_DataValue_init(&dmkf.data.keyFrameData.dataSetFields[0]);
  397. UA_Int32 iv = 27;
  398. UA_Variant_setScalarCopy(&dmkf.data.keyFrameData.dataSetFields[0].value, &iv, &UA_TYPES[UA_TYPES_INT32]);
  399. dmkf.data.keyFrameData.dataSetFields[0].hasValue = true;
  400. m.payload.dataSetPayload.dataSetMessages = &dmkf;
  401. UA_StatusCode rv = UA_STATUSCODE_UNCERTAININITIALVALUE;
  402. UA_ByteString buffer;
  403. size_t msgSize = UA_NetworkMessage_calcSizeBinary(&m);
  404. // to generate an error we make the buffer too small
  405. msgSize -= 5;
  406. rv = UA_ByteString_allocBuffer(&buffer, msgSize);
  407. ck_assert_int_eq(rv, UA_STATUSCODE_GOOD);
  408. UA_Byte *bufPos = buffer.data;
  409. memset(bufPos, 0, msgSize);
  410. const UA_Byte *bufEnd = &(buffer.data[buffer.length]);
  411. rv = UA_NetworkMessage_encodeBinary(&m, &bufPos, bufEnd);
  412. ck_assert_int_ne(rv, UA_STATUSCODE_GOOD);
  413. UA_DataValue_deleteMembers(&dmkf.data.keyFrameData.dataSetFields[0]);
  414. UA_ByteString_deleteMembers(&buffer);
  415. UA_Array_delete(dmkf.data.keyFrameData.dataSetFields, dmkf.data.keyFrameData.fieldCount, &UA_TYPES[UA_TYPES_DATAVALUE]);
  416. }
  417. END_TEST
  418. START_TEST(UA_PubSub_Decode_WithBufferTooSmallShallReturnError) {
  419. UA_NetworkMessage m;
  420. memset(&m, 0, sizeof(UA_NetworkMessage));
  421. m.version = 1;
  422. m.networkMessageType = UA_NETWORKMESSAGE_DATASET;
  423. UA_DataSetMessage dmkf;
  424. memset(&dmkf, 0, sizeof(UA_DataSetMessage));
  425. dmkf.header.dataSetMessageValid = true;
  426. dmkf.header.fieldEncoding = UA_FIELDENCODING_DATAVALUE;
  427. dmkf.header.dataSetMessageType = UA_DATASETMESSAGE_DATAKEYFRAME;
  428. dmkf.data.keyFrameData.fieldCount = 1;
  429. dmkf.data.keyFrameData.dataSetFields =
  430. (UA_DataValue*)UA_Array_new(dmkf.data.keyFrameData.fieldCount, &UA_TYPES[UA_TYPES_DATAVALUE]);
  431. UA_DataValue_init(&dmkf.data.keyFrameData.dataSetFields[0]);
  432. UA_Int32 iv = 27;
  433. UA_Variant_setScalarCopy(&dmkf.data.keyFrameData.dataSetFields[0].value, &iv, &UA_TYPES[UA_TYPES_INT32]);
  434. dmkf.data.keyFrameData.dataSetFields[0].hasValue = true;
  435. m.payload.dataSetPayload.dataSetMessages = &dmkf;
  436. UA_StatusCode rv = UA_STATUSCODE_UNCERTAININITIALVALUE;
  437. UA_ByteString buffer;
  438. size_t msgSize = UA_NetworkMessage_calcSizeBinary(&m);
  439. rv = UA_ByteString_allocBuffer(&buffer, msgSize);
  440. ck_assert_int_eq(rv, UA_STATUSCODE_GOOD);
  441. UA_Byte *bufPos = buffer.data;
  442. memset(bufPos, 0, msgSize);
  443. const UA_Byte *bufEnd = &(buffer.data[buffer.length]);
  444. rv = UA_NetworkMessage_encodeBinary(&m, &bufPos, bufEnd);
  445. ck_assert_int_eq(rv, UA_STATUSCODE_GOOD);
  446. UA_NetworkMessage m2;
  447. memset(&m2, 0, sizeof(UA_NetworkMessage));
  448. size_t offset = 0;
  449. UA_ByteString buffer2;
  450. UA_ByteString_init(&buffer2);
  451. size_t shortLength = buffer.length - 4;
  452. UA_ByteString_allocBuffer(&buffer2, shortLength);
  453. for (size_t i = 0; i < shortLength; i++)
  454. {
  455. buffer2.data[i] = buffer.data[i];
  456. }
  457. rv = UA_NetworkMessage_decodeBinary(&buffer2, &offset, &m2);
  458. ck_assert_int_ne(rv, UA_STATUSCODE_GOOD);
  459. UA_DataValue_deleteMembers(&dmkf.data.keyFrameData.dataSetFields[0]);
  460. UA_NetworkMessage_deleteMembers(&m2);
  461. UA_ByteString_deleteMembers(&buffer);
  462. UA_ByteString_deleteMembers(&buffer2);
  463. UA_Array_delete(dmkf.data.keyFrameData.dataSetFields, dmkf.data.keyFrameData.fieldCount, &UA_TYPES[UA_TYPES_DATAVALUE]);
  464. }
  465. END_TEST
  466. START_TEST(UA_PubSub_EnDecode_ShallWorkOn1DS2ValuesVariantDeltaFrame) {
  467. UA_NetworkMessage m;
  468. memset(&m, 0, sizeof(UA_NetworkMessage));
  469. m.version = 1;
  470. m.networkMessageType = UA_NETWORKMESSAGE_DATASET;
  471. UA_DataSetMessage dmdf;
  472. memset(&dmdf, 0, sizeof(UA_DataSetMessage));
  473. dmdf.header.dataSetMessageValid = true;
  474. dmdf.header.fieldEncoding = UA_FIELDENCODING_VARIANT;
  475. dmdf.header.dataSetMessageType = UA_DATASETMESSAGE_DATADELTAFRAME;
  476. dmdf.data.deltaFrameData.fieldCount = 2;
  477. size_t memsize = sizeof(UA_DataSetMessage_DeltaFrameField) * dmdf.data.deltaFrameData.fieldCount;
  478. dmdf.data.deltaFrameData.deltaFrameFields = (UA_DataSetMessage_DeltaFrameField*)UA_malloc(memsize);
  479. UA_UInt16 fieldIndex1 = 1;
  480. dmdf.data.deltaFrameData.deltaFrameFields[0].fieldIndex = fieldIndex1;
  481. UA_DataValue_init(&dmdf.data.deltaFrameData.deltaFrameFields[0].fieldValue);
  482. UA_UInt16 fieldIndex2 = 3;
  483. dmdf.data.deltaFrameData.deltaFrameFields[1].fieldIndex = fieldIndex2;
  484. UA_DataValue_init(&dmdf.data.deltaFrameData.deltaFrameFields[1].fieldValue);
  485. UA_Int32 iv = 58;
  486. UA_Variant_setScalar(&dmdf.data.deltaFrameData.deltaFrameFields[0].fieldValue.value, &iv, &UA_TYPES[UA_TYPES_INT32]);
  487. dmdf.data.deltaFrameData.deltaFrameFields[0].fieldValue.hasValue = true;
  488. UA_String testString = (UA_String) { 5, (UA_Byte*)"OPCUA" };
  489. UA_Variant value;
  490. UA_Variant_init(&value);
  491. value.data = UA_malloc(sizeof(UA_String));
  492. *((UA_String*)value.data) = testString;
  493. value.type = &UA_TYPES[UA_TYPES_STRING];
  494. dmdf.data.deltaFrameData.deltaFrameFields[1].fieldValue.value = value;
  495. dmdf.data.deltaFrameData.deltaFrameFields[1].fieldValue.hasValue = true;
  496. m.payload.dataSetPayload.dataSetMessages = &dmdf;
  497. UA_StatusCode rv = UA_STATUSCODE_UNCERTAININITIALVALUE;
  498. UA_ByteString buffer;
  499. size_t msgSize = UA_NetworkMessage_calcSizeBinary(&m);
  500. rv = UA_ByteString_allocBuffer(&buffer, msgSize);
  501. ck_assert_int_eq(rv, UA_STATUSCODE_GOOD);
  502. UA_Byte *bufPos = buffer.data;
  503. memset(bufPos, 0, msgSize);
  504. const UA_Byte *bufEnd = &(buffer.data[buffer.length]);
  505. rv = UA_NetworkMessage_encodeBinary(&m, &bufPos, bufEnd);
  506. ck_assert_int_eq(rv, UA_STATUSCODE_GOOD);
  507. UA_NetworkMessage m2;
  508. memset(&m2, 0, sizeof(UA_NetworkMessage));
  509. size_t offset = 0;
  510. rv = UA_NetworkMessage_decodeBinary(&buffer, &offset, &m2);
  511. ck_assert_int_eq(rv, UA_STATUSCODE_GOOD);
  512. ck_assert(m.version == m2.version);
  513. ck_assert(m.networkMessageType == m2.networkMessageType);
  514. ck_assert(m.timestampEnabled == m2.timestampEnabled);
  515. ck_assert(m.payloadHeaderEnabled == m2.payloadHeaderEnabled);
  516. ck_assert(m.payload.dataSetPayload.dataSetMessages[0].header.dataSetMessageValid == m2.payload.dataSetPayload.dataSetMessages[0].header.dataSetMessageValid);
  517. ck_assert(m.payload.dataSetPayload.dataSetMessages[0].header.fieldEncoding == m2.payload.dataSetPayload.dataSetMessages[0].header.fieldEncoding);
  518. ck_assert_int_eq(m2.payload.dataSetPayload.dataSetMessages[0].data.deltaFrameData.fieldCount, 2);
  519. ck_assert_int_eq(m.payload.dataSetPayload.dataSetMessages[0].data.deltaFrameData.deltaFrameFields[0].fieldIndex, fieldIndex1);
  520. ck_assert(m.payload.dataSetPayload.dataSetMessages[0].data.deltaFrameData.deltaFrameFields[0].fieldValue.hasValue == m2.payload.dataSetPayload.dataSetMessages[0].data.deltaFrameData.deltaFrameFields[0].fieldValue.hasValue);
  521. ck_assert_int_eq((uintptr_t)m2.payload.dataSetPayload.dataSetMessages[0].data.deltaFrameData.deltaFrameFields[0].fieldValue.value.type, (uintptr_t)&UA_TYPES[UA_TYPES_INT32]);
  522. ck_assert_int_eq(*(UA_Int32 *)m2.payload.dataSetPayload.dataSetMessages[0].data.deltaFrameData.deltaFrameFields[0].fieldValue.value.data, iv);
  523. ck_assert(m.payload.dataSetPayload.dataSetMessages[0].data.deltaFrameData.deltaFrameFields[0].fieldValue.hasSourceTimestamp == m2.payload.dataSetPayload.dataSetMessages[0].data.deltaFrameData.deltaFrameFields[0].fieldValue.hasSourceTimestamp);
  524. ck_assert_int_eq(m.payload.dataSetPayload.dataSetMessages[0].data.deltaFrameData.deltaFrameFields[1].fieldIndex, fieldIndex2);
  525. ck_assert(m.payload.dataSetPayload.dataSetMessages[0].data.deltaFrameData.deltaFrameFields[1].fieldValue.hasValue == m2.payload.dataSetPayload.dataSetMessages[0].data.deltaFrameData.deltaFrameFields[1].fieldValue.hasValue);
  526. // compare string value
  527. UA_String decodedString = *(UA_String*)(m2.payload.dataSetPayload.dataSetMessages[0].data.deltaFrameData.deltaFrameFields[1].fieldValue.value.data);
  528. for (UA_Int32 i = 0; i < 5; i++)
  529. {
  530. ck_assert_int_eq(decodedString.data[i], testString.data[i]);
  531. }
  532. ck_assert_int_eq(decodedString.length, testString.length);
  533. ck_assert(m.payload.dataSetPayload.dataSetMessages[0].data.deltaFrameData.deltaFrameFields[1].fieldValue.hasSourceTimestamp == m2.payload.dataSetPayload.dataSetMessages[0].data.deltaFrameData.deltaFrameFields[1].fieldValue.hasSourceTimestamp);
  534. ck_assert(m.dataSetClassIdEnabled == m2.dataSetClassIdEnabled);
  535. ck_assert(m.groupHeaderEnabled == m2.groupHeaderEnabled);
  536. ck_assert(m.picosecondsEnabled == m2.picosecondsEnabled);
  537. ck_assert(m.promotedFieldsEnabled == m2.promotedFieldsEnabled);
  538. ck_assert(m.publisherIdEnabled == m2.publisherIdEnabled);
  539. ck_assert(m.securityEnabled == m2.securityEnabled);
  540. ck_assert(m.chunkMessage == m2.chunkMessage);
  541. ((UA_String*)value.data)->data = NULL; // the string is statically allocated. do not free it.
  542. UA_Variant_deleteMembers(&value);
  543. UA_NetworkMessage_deleteMembers(&m2);
  544. UA_ByteString_deleteMembers(&buffer);
  545. UA_free(dmdf.data.deltaFrameData.deltaFrameFields);
  546. }
  547. END_TEST
  548. START_TEST(UA_PubSub_EnDecode_ShallWorkOn1DS2ValuesDataValueDeltaFrame) {
  549. UA_NetworkMessage m;
  550. memset(&m, 0, sizeof(UA_NetworkMessage));
  551. m.version = 1;
  552. m.networkMessageType = UA_NETWORKMESSAGE_DATASET;
  553. UA_DataSetMessage dmdf;
  554. memset(&dmdf, 0, sizeof(UA_DataSetMessage));
  555. dmdf.header.dataSetMessageValid = true;
  556. dmdf.header.fieldEncoding = UA_FIELDENCODING_DATAVALUE;
  557. dmdf.header.dataSetMessageType = UA_DATASETMESSAGE_DATADELTAFRAME;
  558. dmdf.data.deltaFrameData.fieldCount = 2;
  559. size_t memsize = sizeof(UA_DataSetMessage_DeltaFrameField) * dmdf.data.deltaFrameData.fieldCount;
  560. dmdf.data.deltaFrameData.deltaFrameFields = (UA_DataSetMessage_DeltaFrameField*)UA_malloc(memsize);
  561. UA_UInt16 fieldIndex1 = 1;
  562. dmdf.data.deltaFrameData.deltaFrameFields[0].fieldIndex = fieldIndex1;
  563. UA_DataValue_init(&dmdf.data.deltaFrameData.deltaFrameFields[0].fieldValue);
  564. UA_UInt16 fieldIndex2 = 3;
  565. dmdf.data.deltaFrameData.deltaFrameFields[1].fieldIndex = fieldIndex2;
  566. UA_DataValue_init(&dmdf.data.deltaFrameData.deltaFrameFields[1].fieldValue);
  567. UA_Int32 iv = 193;
  568. UA_Variant_setScalarCopy(&dmdf.data.deltaFrameData.deltaFrameFields[0].fieldValue.value, &iv, &UA_TYPES[UA_TYPES_INT32]);
  569. dmdf.data.deltaFrameData.deltaFrameFields[0].fieldValue.hasValue = true;
  570. dmdf.data.deltaFrameData.deltaFrameFields[0].fieldValue.sourceTimestamp = UA_DateTime_now();
  571. dmdf.data.deltaFrameData.deltaFrameFields[0].fieldValue.hasSourceTimestamp = true;
  572. UA_String testString = (UA_String) { 5, (UA_Byte*)"OPCUA" };
  573. UA_Variant value;
  574. UA_Variant_init(&value);
  575. value.data = UA_malloc(sizeof(UA_String));
  576. *((UA_String*)value.data) = testString;
  577. value.type = &UA_TYPES[UA_TYPES_STRING];
  578. dmdf.data.deltaFrameData.deltaFrameFields[1].fieldValue.value = value;
  579. dmdf.data.deltaFrameData.deltaFrameFields[1].fieldValue.hasValue = true;
  580. m.payload.dataSetPayload.dataSetMessages = &dmdf;
  581. UA_StatusCode rv = UA_STATUSCODE_UNCERTAININITIALVALUE;
  582. UA_ByteString buffer;
  583. size_t msgSize = UA_NetworkMessage_calcSizeBinary(&m);
  584. rv = UA_ByteString_allocBuffer(&buffer, msgSize);
  585. ck_assert_int_eq(rv, UA_STATUSCODE_GOOD);
  586. UA_Byte *bufPos = buffer.data;
  587. memset(bufPos, 0, msgSize);
  588. const UA_Byte *bufEnd = &(buffer.data[buffer.length]);
  589. rv = UA_NetworkMessage_encodeBinary(&m, &bufPos, bufEnd);
  590. ck_assert_int_eq(rv, UA_STATUSCODE_GOOD);
  591. UA_NetworkMessage m2;
  592. memset(&m2, 0, sizeof(UA_NetworkMessage));
  593. size_t offset = 0;
  594. rv = UA_NetworkMessage_decodeBinary(&buffer, &offset, &m2);
  595. ck_assert_int_eq(rv, UA_STATUSCODE_GOOD);
  596. ck_assert(m.version == m2.version);
  597. ck_assert(m.networkMessageType == m2.networkMessageType);
  598. ck_assert(m.timestampEnabled == m2.timestampEnabled);
  599. ck_assert(m.payloadHeaderEnabled == m2.payloadHeaderEnabled);
  600. ck_assert(m.payload.dataSetPayload.dataSetMessages[0].header.dataSetMessageValid == m2.payload.dataSetPayload.dataSetMessages[0].header.dataSetMessageValid);
  601. ck_assert(m.payload.dataSetPayload.dataSetMessages[0].header.fieldEncoding == m2.payload.dataSetPayload.dataSetMessages[0].header.fieldEncoding);
  602. ck_assert_int_eq(m2.payload.dataSetPayload.dataSetMessages[0].data.deltaFrameData.fieldCount, 2);
  603. ck_assert_int_eq(m.payload.dataSetPayload.dataSetMessages[0].data.deltaFrameData.deltaFrameFields[0].fieldIndex, fieldIndex1);
  604. ck_assert(m.payload.dataSetPayload.dataSetMessages[0].data.deltaFrameData.deltaFrameFields[0].fieldValue.hasValue == m2.payload.dataSetPayload.dataSetMessages[0].data.deltaFrameData.deltaFrameFields[0].fieldValue.hasValue);
  605. ck_assert_int_eq((uintptr_t)m2.payload.dataSetPayload.dataSetMessages[0].data.deltaFrameData.deltaFrameFields[0].fieldValue.value.type, (uintptr_t)&UA_TYPES[UA_TYPES_INT32]);
  606. ck_assert_int_eq(*(UA_Int32 *)m2.payload.dataSetPayload.dataSetMessages[0].data.deltaFrameData.deltaFrameFields[0].fieldValue.value.data, iv);
  607. ck_assert(m.payload.dataSetPayload.dataSetMessages[0].data.deltaFrameData.deltaFrameFields[0].fieldValue.hasSourceTimestamp == m2.payload.dataSetPayload.dataSetMessages[0].data.deltaFrameData.deltaFrameFields[0].fieldValue.hasSourceTimestamp);
  608. ck_assert_int_eq(m.payload.dataSetPayload.dataSetMessages[0].data.deltaFrameData.deltaFrameFields[1].fieldIndex, fieldIndex2);
  609. ck_assert(m.payload.dataSetPayload.dataSetMessages[0].data.deltaFrameData.deltaFrameFields[1].fieldValue.hasValue == m2.payload.dataSetPayload.dataSetMessages[0].data.deltaFrameData.deltaFrameFields[1].fieldValue.hasValue);
  610. // compare string value
  611. UA_String decodedString = *(UA_String*)(m2.payload.dataSetPayload.dataSetMessages[0].data.deltaFrameData.deltaFrameFields[1].fieldValue.value.data);
  612. for (UA_Int32 i = 0; i < 5; i++)
  613. {
  614. ck_assert_int_eq(decodedString.data[i], testString.data[i]);
  615. }
  616. ck_assert_int_eq(decodedString.length, testString.length);
  617. ck_assert(m.payload.dataSetPayload.dataSetMessages[0].data.deltaFrameData.deltaFrameFields[1].fieldValue.hasSourceTimestamp == m2.payload.dataSetPayload.dataSetMessages[0].data.deltaFrameData.deltaFrameFields[1].fieldValue.hasSourceTimestamp);
  618. ck_assert(m.dataSetClassIdEnabled == m2.dataSetClassIdEnabled);
  619. ck_assert(m.groupHeaderEnabled == m2.groupHeaderEnabled);
  620. ck_assert(m.picosecondsEnabled == m2.picosecondsEnabled);
  621. ck_assert(m.promotedFieldsEnabled == m2.promotedFieldsEnabled);
  622. ck_assert(m.publisherIdEnabled == m2.publisherIdEnabled);
  623. ck_assert(m.securityEnabled == m2.securityEnabled);
  624. ck_assert(m.chunkMessage == m2.chunkMessage);
  625. ((UA_String*)value.data)->data = NULL; // the string is statically allocated. do not free it.
  626. UA_Variant_deleteMembers(&value);
  627. UA_DataValue_deleteMembers(&dmdf.data.deltaFrameData.deltaFrameFields[0].fieldValue);
  628. UA_NetworkMessage_deleteMembers(&m2);
  629. UA_ByteString_deleteMembers(&buffer);
  630. UA_free(dmdf.data.deltaFrameData.deltaFrameFields);
  631. }
  632. END_TEST
  633. START_TEST(UA_PubSub_EnDecode_ShallWorkOn1DS2ValuesVariantKeyFrameGroupHeader) {
  634. UA_NetworkMessage m;
  635. memset(&m, 0, sizeof(UA_NetworkMessage));
  636. m.version = 1;
  637. m.networkMessageType = UA_NETWORKMESSAGE_DATASET;
  638. m.groupHeaderEnabled = true;
  639. m.groupHeader.writerGroupIdEnabled = true;
  640. UA_UInt16 writerGroupId = 17;
  641. m.groupHeader.writerGroupId = writerGroupId;
  642. m.groupHeader.groupVersionEnabled = true;
  643. UA_UInt32 groupVersion = 573354747; // das sollte irgendwann 2018 sein
  644. m.groupHeader.groupVersion = groupVersion;
  645. UA_DataSetMessage dmkf;
  646. memset(&dmkf, 0, sizeof(UA_DataSetMessage));
  647. dmkf.header.dataSetMessageValid = true;
  648. dmkf.header.fieldEncoding = UA_FIELDENCODING_DATAVALUE;
  649. dmkf.header.dataSetMessageType = UA_DATASETMESSAGE_DATAKEYFRAME;
  650. UA_UInt16 anzFields = 2;
  651. dmkf.data.keyFrameData.fieldCount = anzFields;
  652. dmkf.data.keyFrameData.dataSetFields =
  653. (UA_DataValue*)UA_Array_new(dmkf.data.keyFrameData.fieldCount, &UA_TYPES[UA_TYPES_DATAVALUE]);
  654. UA_DataValue_init(&dmkf.data.keyFrameData.dataSetFields[0]);
  655. UA_DataValue_init(&dmkf.data.keyFrameData.dataSetFields[1]);
  656. UA_Double dv = 231.3;
  657. UA_Variant_setScalarCopy(&dmkf.data.keyFrameData.dataSetFields[0].value, &dv, &UA_TYPES[UA_TYPES_DOUBLE]);
  658. dmkf.data.keyFrameData.dataSetFields[0].hasValue = true;
  659. UA_UInt64 uiv = 98412;
  660. UA_Variant_setScalarCopy(&dmkf.data.keyFrameData.dataSetFields[1].value, &uiv, &UA_TYPES[UA_TYPES_UINT64]);
  661. dmkf.data.keyFrameData.dataSetFields[1].hasValue = true;
  662. m.payload.dataSetPayload.dataSetMessages = &dmkf;
  663. UA_StatusCode rv = UA_STATUSCODE_UNCERTAININITIALVALUE;
  664. UA_ByteString buffer;
  665. size_t msgSize = UA_NetworkMessage_calcSizeBinary(&m);
  666. rv = UA_ByteString_allocBuffer(&buffer, msgSize);
  667. ck_assert_int_eq(rv, UA_STATUSCODE_GOOD);
  668. UA_Byte *bufPos = buffer.data;
  669. memset(bufPos, 0, msgSize);
  670. const UA_Byte *bufEnd = &(buffer.data[buffer.length]);
  671. rv = UA_NetworkMessage_encodeBinary(&m, &bufPos, bufEnd);
  672. ck_assert_int_eq(rv, UA_STATUSCODE_GOOD);
  673. UA_NetworkMessage m2;
  674. memset(&m2, 0, sizeof(UA_NetworkMessage));
  675. size_t offset = 0;
  676. rv = UA_NetworkMessage_decodeBinary(&buffer, &offset, &m2);
  677. ck_assert_int_eq(rv, UA_STATUSCODE_GOOD);
  678. ck_assert(m.version == m2.version);
  679. ck_assert(m.networkMessageType == m2.networkMessageType);
  680. ck_assert(m.timestampEnabled == m2.timestampEnabled);
  681. ck_assert(m.dataSetClassIdEnabled == m2.dataSetClassIdEnabled);
  682. ck_assert(m.groupHeaderEnabled == m2.groupHeaderEnabled);
  683. ck_assert(m.groupHeader.writerGroupIdEnabled == m2.groupHeader.writerGroupIdEnabled);
  684. ck_assert(m.groupHeader.groupVersionEnabled == m2.groupHeader.groupVersionEnabled);
  685. ck_assert(m.groupHeader.networkMessageNumberEnabled == m2.groupHeader.networkMessageNumberEnabled);
  686. ck_assert(m.groupHeader.sequenceNumberEnabled == m2.groupHeader.sequenceNumberEnabled);
  687. ck_assert_uint_eq(m2.groupHeader.writerGroupId, writerGroupId);
  688. ck_assert_uint_eq(m2.groupHeader.groupVersion, groupVersion);
  689. ck_assert(m.picosecondsEnabled == m2.picosecondsEnabled);
  690. ck_assert(m.promotedFieldsEnabled == m2.promotedFieldsEnabled);
  691. ck_assert(m.publisherIdEnabled == m2.publisherIdEnabled);
  692. ck_assert(m.securityEnabled == m2.securityEnabled);
  693. ck_assert(m.chunkMessage == m2.chunkMessage);
  694. ck_assert(m.payloadHeaderEnabled == m2.payloadHeaderEnabled);
  695. ck_assert(m.payload.dataSetPayload.dataSetMessages[0].header.dataSetMessageValid == m2.payload.dataSetPayload.dataSetMessages[0].header.dataSetMessageValid);
  696. ck_assert(m.payload.dataSetPayload.dataSetMessages[0].header.fieldEncoding == m2.payload.dataSetPayload.dataSetMessages[0].header.fieldEncoding);
  697. ck_assert_int_eq(m2.payload.dataSetPayload.dataSetMessages[0].data.keyFrameData.fieldCount, anzFields);
  698. ck_assert(m.payload.dataSetPayload.dataSetMessages[0].data.keyFrameData.dataSetFields[0].hasValue == m2.payload.dataSetPayload.dataSetMessages[0].data.keyFrameData.dataSetFields[0].hasValue);
  699. ck_assert_uint_eq((uintptr_t)m2.payload.dataSetPayload.dataSetMessages[0].data.keyFrameData.dataSetFields[0].value.type, (uintptr_t)&UA_TYPES[UA_TYPES_DOUBLE]);
  700. ck_assert(*(UA_Double*)m2.payload.dataSetPayload.dataSetMessages[0].data.keyFrameData.dataSetFields[0].value.data == dv);
  701. ck_assert(m.payload.dataSetPayload.dataSetMessages[0].data.keyFrameData.dataSetFields[1].hasValue == m2.payload.dataSetPayload.dataSetMessages[0].data.keyFrameData.dataSetFields[0].hasValue);
  702. ck_assert_uint_eq((uintptr_t)m2.payload.dataSetPayload.dataSetMessages[0].data.keyFrameData.dataSetFields[1].value.type, (uintptr_t)&UA_TYPES[UA_TYPES_UINT64]);
  703. ck_assert_uint_eq(*(UA_UInt64 *)m2.payload.dataSetPayload.dataSetMessages[0].data.keyFrameData.dataSetFields[1].value.data, uiv);
  704. ck_assert(m.payload.dataSetPayload.dataSetMessages[0].data.keyFrameData.dataSetFields[0].hasSourceTimestamp == m2.payload.dataSetPayload.dataSetMessages[0].data.keyFrameData.dataSetFields[0].hasSourceTimestamp);
  705. ck_assert(m.payload.dataSetPayload.dataSetMessages[0].data.keyFrameData.dataSetFields[1].hasSourceTimestamp == m2.payload.dataSetPayload.dataSetMessages[0].data.keyFrameData.dataSetFields[0].hasSourceTimestamp);
  706. UA_DataValue_deleteMembers(&dmkf.data.keyFrameData.dataSetFields[0]);
  707. UA_DataValue_deleteMembers(&dmkf.data.keyFrameData.dataSetFields[1]);
  708. UA_NetworkMessage_deleteMembers(&m2);
  709. UA_ByteString_deleteMembers(&buffer);
  710. UA_Array_delete(dmkf.data.keyFrameData.dataSetFields, dmkf.data.keyFrameData.fieldCount, &UA_TYPES[UA_TYPES_DATAVALUE]);
  711. }
  712. END_TEST
  713. START_TEST(UA_PubSub_EnDecode_ShallWorkOn1DS2ValuesVariantDeltaFramePublDSCID) {
  714. UA_NetworkMessage m;
  715. memset(&m, 0, sizeof(UA_NetworkMessage));
  716. m.version = 1;
  717. m.networkMessageType = UA_NETWORKMESSAGE_DATASET;
  718. m.publisherIdEnabled = true;
  719. m.publisherIdType = UA_PUBLISHERDATATYPE_UINT32;
  720. UA_UInt32 publisherId = 1469;
  721. m.publisherId.publisherIdUInt32 = publisherId;
  722. m.dataSetClassIdEnabled = true;
  723. UA_Guid dataSetClassId = UA_Guid_random();
  724. m.dataSetClassId = dataSetClassId;
  725. UA_DataSetMessage dmdf;
  726. memset(&dmdf, 0, sizeof(UA_DataSetMessage));
  727. dmdf.header.dataSetMessageValid = true;
  728. dmdf.header.fieldEncoding = UA_FIELDENCODING_VARIANT;
  729. dmdf.header.dataSetMessageType = UA_DATASETMESSAGE_DATADELTAFRAME;
  730. dmdf.data.deltaFrameData.fieldCount = 2;
  731. size_t memsize = sizeof(UA_DataSetMessage_DeltaFrameField) * dmdf.data.deltaFrameData.fieldCount;
  732. dmdf.data.deltaFrameData.deltaFrameFields = (UA_DataSetMessage_DeltaFrameField*)UA_malloc(memsize);
  733. UA_UInt16 fieldIndex1 = 1;
  734. dmdf.data.deltaFrameData.deltaFrameFields[0].fieldIndex = fieldIndex1;
  735. UA_DataValue_init(&dmdf.data.deltaFrameData.deltaFrameFields[0].fieldValue);
  736. UA_UInt16 fieldIndex2 = 3;
  737. dmdf.data.deltaFrameData.deltaFrameFields[1].fieldIndex = fieldIndex2;
  738. UA_DataValue_init(&dmdf.data.deltaFrameData.deltaFrameFields[1].fieldValue);
  739. UA_Int32 iv = 58;
  740. UA_Variant_setScalarCopy(&dmdf.data.deltaFrameData.deltaFrameFields[0].fieldValue.value, &iv, &UA_TYPES[UA_TYPES_INT32]);
  741. dmdf.data.deltaFrameData.deltaFrameFields[0].fieldValue.hasValue = true;
  742. UA_String testString = (UA_String) { 5, (UA_Byte*)"OPCUA" };
  743. UA_Variant value;
  744. UA_Variant_init(&value);
  745. value.data = UA_malloc(sizeof(UA_String));
  746. *((UA_String*)value.data) = testString;
  747. value.type = &UA_TYPES[UA_TYPES_STRING];
  748. dmdf.data.deltaFrameData.deltaFrameFields[1].fieldValue.value = value;
  749. dmdf.data.deltaFrameData.deltaFrameFields[1].fieldValue.hasValue = true;
  750. m.payload.dataSetPayload.dataSetMessages = &dmdf;
  751. UA_StatusCode rv = UA_STATUSCODE_UNCERTAININITIALVALUE;
  752. UA_ByteString buffer;
  753. size_t msgSize = UA_NetworkMessage_calcSizeBinary(&m);
  754. rv = UA_ByteString_allocBuffer(&buffer, msgSize);
  755. ck_assert_int_eq(rv, UA_STATUSCODE_GOOD);
  756. UA_Byte *bufPos = buffer.data;
  757. memset(bufPos, 0, msgSize);
  758. const UA_Byte *bufEnd = &(buffer.data[buffer.length]);
  759. rv = UA_NetworkMessage_encodeBinary(&m, &bufPos, bufEnd);
  760. ck_assert_int_eq(rv, UA_STATUSCODE_GOOD);
  761. UA_NetworkMessage m2;
  762. memset(&m2, 0, sizeof(UA_NetworkMessage));
  763. size_t offset = 0;
  764. rv = UA_NetworkMessage_decodeBinary(&buffer, &offset, &m2);
  765. ck_assert_int_eq(rv, UA_STATUSCODE_GOOD);
  766. ck_assert(m.version == m2.version);
  767. ck_assert(m.networkMessageType == m2.networkMessageType);
  768. ck_assert(m.timestampEnabled == m2.timestampEnabled);
  769. ck_assert(m.dataSetClassIdEnabled == m2.dataSetClassIdEnabled);
  770. ck_assert(UA_Guid_equal(&m.dataSetClassId, &m2.dataSetClassId) == true);
  771. ck_assert(m.groupHeaderEnabled == m2.groupHeaderEnabled);
  772. ck_assert(m.picosecondsEnabled == m2.picosecondsEnabled);
  773. ck_assert(m.promotedFieldsEnabled == m2.promotedFieldsEnabled);
  774. ck_assert(m.publisherIdEnabled == m2.publisherIdEnabled);
  775. ck_assert_int_eq(m2.publisherIdType, m.publisherIdType);
  776. ck_assert_uint_eq(m2.publisherId.publisherIdUInt32, publisherId);
  777. ck_assert(m.securityEnabled == m2.securityEnabled);
  778. ck_assert(m.chunkMessage == m2.chunkMessage);
  779. ck_assert(m.payloadHeaderEnabled == m2.payloadHeaderEnabled);
  780. ck_assert(m.payload.dataSetPayload.dataSetMessages[0].header.dataSetMessageValid == m2.payload.dataSetPayload.dataSetMessages[0].header.dataSetMessageValid);
  781. ck_assert(m.payload.dataSetPayload.dataSetMessages[0].header.fieldEncoding == m2.payload.dataSetPayload.dataSetMessages[0].header.fieldEncoding);
  782. ck_assert_int_eq(m2.payload.dataSetPayload.dataSetMessages[0].data.deltaFrameData.fieldCount, 2);
  783. ck_assert_int_eq(m.payload.dataSetPayload.dataSetMessages[0].data.deltaFrameData.deltaFrameFields[0].fieldIndex, fieldIndex1);
  784. ck_assert(m.payload.dataSetPayload.dataSetMessages[0].data.deltaFrameData.deltaFrameFields[0].fieldValue.hasValue == m2.payload.dataSetPayload.dataSetMessages[0].data.deltaFrameData.deltaFrameFields[0].fieldValue.hasValue);
  785. ck_assert_int_eq((uintptr_t)m2.payload.dataSetPayload.dataSetMessages[0].data.deltaFrameData.deltaFrameFields[0].fieldValue.value.type, (uintptr_t)&UA_TYPES[UA_TYPES_INT32]);
  786. ck_assert_int_eq(*(UA_Int32 *)m2.payload.dataSetPayload.dataSetMessages[0].data.deltaFrameData.deltaFrameFields[0].fieldValue.value.data, iv);
  787. ck_assert(m.payload.dataSetPayload.dataSetMessages[0].data.deltaFrameData.deltaFrameFields[0].fieldValue.hasSourceTimestamp == m2.payload.dataSetPayload.dataSetMessages[0].data.deltaFrameData.deltaFrameFields[0].fieldValue.hasSourceTimestamp);
  788. ck_assert_int_eq(m.payload.dataSetPayload.dataSetMessages[0].data.deltaFrameData.deltaFrameFields[1].fieldIndex, fieldIndex2);
  789. ck_assert(m.payload.dataSetPayload.dataSetMessages[0].data.deltaFrameData.deltaFrameFields[1].fieldValue.hasValue == m2.payload.dataSetPayload.dataSetMessages[0].data.deltaFrameData.deltaFrameFields[1].fieldValue.hasValue);
  790. // compare string value
  791. UA_String decodedString = *(UA_String*)(m2.payload.dataSetPayload.dataSetMessages[0].data.deltaFrameData.deltaFrameFields[1].fieldValue.value.data);
  792. for (UA_Int32 i = 0; i < 5; i++)
  793. {
  794. ck_assert_int_eq(decodedString.data[i], testString.data[i]);
  795. }
  796. ck_assert_int_eq(decodedString.length, testString.length);
  797. ck_assert(m.payload.dataSetPayload.dataSetMessages[0].data.deltaFrameData.deltaFrameFields[1].fieldValue.hasSourceTimestamp == m2.payload.dataSetPayload.dataSetMessages[0].data.deltaFrameData.deltaFrameFields[1].fieldValue.hasSourceTimestamp);
  798. ((UA_String*)value.data)->data = NULL; // the string is statically allocated. do not free it.
  799. UA_Variant_deleteMembers(&value);
  800. UA_DataValue_deleteMembers(&dmdf.data.deltaFrameData.deltaFrameFields[0].fieldValue);
  801. UA_NetworkMessage_deleteMembers(&m2);
  802. UA_ByteString_deleteMembers(&buffer);
  803. UA_free(dmdf.data.deltaFrameData.deltaFrameFields);
  804. }
  805. END_TEST
  806. START_TEST(UA_PubSub_EnDecode_ShallWorkOn1DS2ValuesDataValueKeyFramePH) {
  807. UA_NetworkMessage m;
  808. memset(&m, 0, sizeof(UA_NetworkMessage));
  809. m.version = 1;
  810. m.networkMessageType = UA_NETWORKMESSAGE_DATASET;
  811. m.payloadHeaderEnabled = true;
  812. m.payloadHeader.dataSetPayloadHeader.count = 1;
  813. m.payloadHeader.dataSetPayloadHeader.dataSetWriterIds = (UA_UInt16 *)UA_Array_new(m.payloadHeader.dataSetPayloadHeader.count, &UA_TYPES[UA_TYPES_UINT16]);
  814. UA_UInt16 dataSetWriterId = 1698;
  815. m.payloadHeader.dataSetPayloadHeader.dataSetWriterIds[0] = dataSetWriterId;
  816. UA_DataSetMessage dmkf;
  817. memset(&dmkf, 0, sizeof(UA_DataSetMessage));
  818. dmkf.header.dataSetMessageValid = true;
  819. dmkf.header.fieldEncoding = UA_FIELDENCODING_DATAVALUE;
  820. dmkf.header.dataSetMessageType = UA_DATASETMESSAGE_DATAKEYFRAME;
  821. UA_UInt16 anzFields = 2;
  822. dmkf.data.keyFrameData.fieldCount = anzFields;
  823. dmkf.data.keyFrameData.dataSetFields =
  824. (UA_DataValue*)UA_Array_new(dmkf.data.keyFrameData.fieldCount, &UA_TYPES[UA_TYPES_DATAVALUE]);
  825. UA_DataValue_init(&dmkf.data.keyFrameData.dataSetFields[0]);
  826. UA_DataValue_init(&dmkf.data.keyFrameData.dataSetFields[1]);
  827. UA_Double dv = 231.3;
  828. UA_Variant_setScalarCopy(&dmkf.data.keyFrameData.dataSetFields[0].value, &dv, &UA_TYPES[UA_TYPES_DOUBLE]);
  829. dmkf.data.keyFrameData.dataSetFields[0].hasValue = true;
  830. UA_UInt64 uiv = 98412;
  831. UA_Variant_setScalarCopy(&dmkf.data.keyFrameData.dataSetFields[1].value, &uiv, &UA_TYPES[UA_TYPES_UINT64]);
  832. dmkf.data.keyFrameData.dataSetFields[1].hasValue = true;
  833. m.payload.dataSetPayload.dataSetMessages = &dmkf;
  834. UA_StatusCode rv = UA_STATUSCODE_UNCERTAININITIALVALUE;
  835. UA_ByteString buffer;
  836. size_t msgSize = UA_NetworkMessage_calcSizeBinary(&m);
  837. rv = UA_ByteString_allocBuffer(&buffer, msgSize);
  838. ck_assert_int_eq(rv, UA_STATUSCODE_GOOD);
  839. UA_Byte *bufPos = buffer.data;
  840. memset(bufPos, 0, msgSize);
  841. const UA_Byte *bufEnd = &(buffer.data[buffer.length]);
  842. rv = UA_NetworkMessage_encodeBinary(&m, &bufPos, bufEnd);
  843. ck_assert_int_eq(rv, UA_STATUSCODE_GOOD);
  844. UA_NetworkMessage m2;
  845. memset(&m2, 0, sizeof(UA_NetworkMessage));
  846. size_t offset = 0;
  847. rv = UA_NetworkMessage_decodeBinary(&buffer, &offset, &m2);
  848. ck_assert_int_eq(rv, UA_STATUSCODE_GOOD);
  849. ck_assert(m.version == m2.version);
  850. ck_assert(m.networkMessageType == m2.networkMessageType);
  851. ck_assert(m.timestampEnabled == m2.timestampEnabled);
  852. ck_assert(m.payloadHeaderEnabled == m2.payloadHeaderEnabled);
  853. ck_assert_uint_eq(m.payloadHeader.dataSetPayloadHeader.count, 1);
  854. ck_assert_uint_eq(m.payloadHeader.dataSetPayloadHeader.dataSetWriterIds[0], dataSetWriterId);
  855. ck_assert(m.payload.dataSetPayload.dataSetMessages[0].header.dataSetMessageValid == m2.payload.dataSetPayload.dataSetMessages[0].header.dataSetMessageValid);
  856. ck_assert(m.payload.dataSetPayload.dataSetMessages[0].header.fieldEncoding == m2.payload.dataSetPayload.dataSetMessages[0].header.fieldEncoding);
  857. ck_assert_int_eq(m2.payload.dataSetPayload.dataSetMessages[0].data.keyFrameData.fieldCount, anzFields);
  858. ck_assert(m.payload.dataSetPayload.dataSetMessages[0].data.keyFrameData.dataSetFields[0].hasValue == m2.payload.dataSetPayload.dataSetMessages[0].data.keyFrameData.dataSetFields[0].hasValue);
  859. ck_assert_int_eq((uintptr_t)m2.payload.dataSetPayload.dataSetMessages[0].data.keyFrameData.dataSetFields[0].value.type, (uintptr_t)&UA_TYPES[UA_TYPES_DOUBLE]);
  860. ck_assert(*(UA_Double*)m2.payload.dataSetPayload.dataSetMessages[0].data.keyFrameData.dataSetFields[0].value.data == dv);
  861. ck_assert(m.payload.dataSetPayload.dataSetMessages[0].data.keyFrameData.dataSetFields[1].hasValue == m2.payload.dataSetPayload.dataSetMessages[0].data.keyFrameData.dataSetFields[0].hasValue);
  862. ck_assert_int_eq((uintptr_t)m2.payload.dataSetPayload.dataSetMessages[0].data.keyFrameData.dataSetFields[1].value.type, (uintptr_t)&UA_TYPES[UA_TYPES_UINT64]);
  863. ck_assert_uint_eq(*(UA_UInt64 *)m2.payload.dataSetPayload.dataSetMessages[0].data.keyFrameData.dataSetFields[1].value.data, uiv);
  864. ck_assert(m.payload.dataSetPayload.dataSetMessages[0].data.keyFrameData.dataSetFields[0].hasSourceTimestamp == m2.payload.dataSetPayload.dataSetMessages[0].data.keyFrameData.dataSetFields[0].hasSourceTimestamp);
  865. ck_assert(m.payload.dataSetPayload.dataSetMessages[0].data.keyFrameData.dataSetFields[1].hasSourceTimestamp == m2.payload.dataSetPayload.dataSetMessages[0].data.keyFrameData.dataSetFields[0].hasSourceTimestamp);
  866. ck_assert(m.dataSetClassIdEnabled == m2.dataSetClassIdEnabled);
  867. ck_assert(m.groupHeaderEnabled == m2.groupHeaderEnabled);
  868. ck_assert(m.picosecondsEnabled == m2.picosecondsEnabled);
  869. ck_assert(m.promotedFieldsEnabled == m2.promotedFieldsEnabled);
  870. ck_assert(m.publisherIdEnabled == m2.publisherIdEnabled);
  871. ck_assert(m.securityEnabled == m2.securityEnabled);
  872. ck_assert(m.chunkMessage == m2.chunkMessage);
  873. UA_Array_delete(m.payloadHeader.dataSetPayloadHeader.dataSetWriterIds, m.payloadHeader.dataSetPayloadHeader.count, &UA_TYPES[UA_TYPES_UINT16]);
  874. UA_DataValue_deleteMembers(&dmkf.data.keyFrameData.dataSetFields[0]);
  875. UA_DataValue_deleteMembers(&dmkf.data.keyFrameData.dataSetFields[1]);
  876. UA_NetworkMessage_deleteMembers(&m2);
  877. UA_ByteString_deleteMembers(&buffer);
  878. UA_Array_delete(dmkf.data.keyFrameData.dataSetFields, dmkf.data.keyFrameData.fieldCount, &UA_TYPES[UA_TYPES_DATAVALUE]);
  879. }
  880. END_TEST
  881. START_TEST(UA_PubSub_EnDecode_ShallWorkOn1DS2ValuesVariantKeyFrameTSProm) {
  882. UA_NetworkMessage m;
  883. memset(&m, 0, sizeof(UA_NetworkMessage));
  884. m.version = 1;
  885. m.networkMessageType = UA_NETWORKMESSAGE_DATASET;
  886. m.timestampEnabled = true;
  887. UA_DateTime ts = UA_DateTime_now();
  888. m.timestamp = ts;
  889. m.promotedFieldsEnabled = true;
  890. m.promotedFieldsSize = 1;
  891. m.promotedFields = (UA_Variant*)UA_Array_new(m.promotedFieldsSize, &UA_TYPES[UA_TYPES_VARIANT]);
  892. UA_DataSetMessage dmkf;
  893. memset(&dmkf, 0, sizeof(UA_DataSetMessage));
  894. dmkf.header.dataSetMessageValid = true;
  895. dmkf.header.fieldEncoding = UA_FIELDENCODING_DATAVALUE;
  896. dmkf.header.dataSetMessageType = UA_DATASETMESSAGE_DATAKEYFRAME;
  897. UA_UInt16 anzFields = 2;
  898. dmkf.data.keyFrameData.fieldCount = anzFields;
  899. dmkf.data.keyFrameData.dataSetFields =
  900. (UA_DataValue*)UA_Array_new(dmkf.data.keyFrameData.fieldCount, &UA_TYPES[UA_TYPES_DATAVALUE]);
  901. UA_DataValue_init(&dmkf.data.keyFrameData.dataSetFields[0]);
  902. UA_DataValue_init(&dmkf.data.keyFrameData.dataSetFields[1]);
  903. UA_Double dv = 231.3;
  904. UA_Variant_setScalarCopy(&dmkf.data.keyFrameData.dataSetFields[0].value, &dv, &UA_TYPES[UA_TYPES_DOUBLE]);
  905. dmkf.data.keyFrameData.dataSetFields[0].hasValue = true;
  906. UA_Variant_copy(&dmkf.data.keyFrameData.dataSetFields[0].value, &m.promotedFields[0]);
  907. UA_UInt64 uiv = 98412;
  908. UA_Variant_setScalarCopy(&dmkf.data.keyFrameData.dataSetFields[1].value, &uiv, &UA_TYPES[UA_TYPES_UINT64]);
  909. dmkf.data.keyFrameData.dataSetFields[1].hasValue = true;
  910. m.payload.dataSetPayload.dataSetMessages = &dmkf;
  911. UA_StatusCode rv = UA_STATUSCODE_UNCERTAININITIALVALUE;
  912. UA_ByteString buffer;
  913. size_t msgSize = UA_NetworkMessage_calcSizeBinary(&m);
  914. rv = UA_ByteString_allocBuffer(&buffer, msgSize);
  915. ck_assert_int_eq(rv, UA_STATUSCODE_GOOD);
  916. UA_Byte *bufPos = buffer.data;
  917. memset(bufPos, 0, msgSize);
  918. const UA_Byte *bufEnd = &(buffer.data[buffer.length]);
  919. rv = UA_NetworkMessage_encodeBinary(&m, &bufPos, bufEnd);
  920. ck_assert_int_eq(rv, UA_STATUSCODE_GOOD);
  921. UA_NetworkMessage m2;
  922. memset(&m2, 0, sizeof(UA_NetworkMessage));
  923. size_t offset = 0;
  924. rv = UA_NetworkMessage_decodeBinary(&buffer, &offset, &m2);
  925. ck_assert_int_eq(rv, UA_STATUSCODE_GOOD);
  926. ck_assert(m.version == m2.version);
  927. ck_assert(m.networkMessageType == m2.networkMessageType);
  928. ck_assert(m.timestampEnabled == m2.timestampEnabled);
  929. ck_assert_int_eq(m.timestamp, ts);
  930. ck_assert(m.groupHeaderEnabled == m2.groupHeaderEnabled);
  931. ck_assert(m.picosecondsEnabled == m2.picosecondsEnabled);
  932. ck_assert(m.promotedFieldsEnabled == m2.promotedFieldsEnabled);
  933. ck_assert_uint_eq(m2.promotedFieldsSize, 1);
  934. ck_assert(*((UA_Double*)m2.promotedFields[0].data) == dv);
  935. ck_assert(m.publisherIdEnabled == m2.publisherIdEnabled);
  936. ck_assert(m.securityEnabled == m2.securityEnabled);
  937. ck_assert(m.chunkMessage == m2.chunkMessage);
  938. ck_assert(m.payloadHeaderEnabled == m2.payloadHeaderEnabled);
  939. ck_assert(m.payload.dataSetPayload.dataSetMessages[0].header.dataSetMessageValid == m2.payload.dataSetPayload.dataSetMessages[0].header.dataSetMessageValid);
  940. ck_assert(m.payload.dataSetPayload.dataSetMessages[0].header.fieldEncoding == m2.payload.dataSetPayload.dataSetMessages[0].header.fieldEncoding);
  941. ck_assert_int_eq(m2.payload.dataSetPayload.dataSetMessages[0].data.keyFrameData.fieldCount, anzFields);
  942. ck_assert(m.payload.dataSetPayload.dataSetMessages[0].data.keyFrameData.dataSetFields[0].hasValue == m2.payload.dataSetPayload.dataSetMessages[0].data.keyFrameData.dataSetFields[0].hasValue);
  943. ck_assert_int_eq((uintptr_t)m2.payload.dataSetPayload.dataSetMessages[0].data.keyFrameData.dataSetFields[0].value.type, (uintptr_t)&UA_TYPES[UA_TYPES_DOUBLE]);
  944. ck_assert(*(UA_Double*)m2.payload.dataSetPayload.dataSetMessages[0].data.keyFrameData.dataSetFields[0].value.data == dv);
  945. ck_assert(m.payload.dataSetPayload.dataSetMessages[0].data.keyFrameData.dataSetFields[1].hasValue == m2.payload.dataSetPayload.dataSetMessages[0].data.keyFrameData.dataSetFields[0].hasValue);
  946. ck_assert_int_eq((uintptr_t)m2.payload.dataSetPayload.dataSetMessages[0].data.keyFrameData.dataSetFields[1].value.type, (uintptr_t)&UA_TYPES[UA_TYPES_UINT64]);
  947. ck_assert_uint_eq(*(UA_UInt64 *)m2.payload.dataSetPayload.dataSetMessages[0].data.keyFrameData.dataSetFields[1].value.data, uiv);
  948. ck_assert(m.payload.dataSetPayload.dataSetMessages[0].data.keyFrameData.dataSetFields[0].hasSourceTimestamp == m2.payload.dataSetPayload.dataSetMessages[0].data.keyFrameData.dataSetFields[0].hasSourceTimestamp);
  949. ck_assert(m.payload.dataSetPayload.dataSetMessages[0].data.keyFrameData.dataSetFields[1].hasSourceTimestamp == m2.payload.dataSetPayload.dataSetMessages[0].data.keyFrameData.dataSetFields[0].hasSourceTimestamp);
  950. ck_assert(m.dataSetClassIdEnabled == m2.dataSetClassIdEnabled);
  951. UA_Array_delete(m.promotedFields, m.promotedFieldsSize, &UA_TYPES[UA_TYPES_VARIANT]);
  952. UA_DataValue_deleteMembers(&dmkf.data.keyFrameData.dataSetFields[0]);
  953. UA_DataValue_deleteMembers(&dmkf.data.keyFrameData.dataSetFields[1]);
  954. UA_NetworkMessage_deleteMembers(&m2);
  955. UA_ByteString_deleteMembers(&buffer);
  956. UA_Array_delete(dmkf.data.keyFrameData.dataSetFields, dmkf.data.keyFrameData.fieldCount, &UA_TYPES[UA_TYPES_DATAVALUE]);
  957. }
  958. END_TEST
  959. START_TEST(UA_PubSub_EnDecode_ShallWorkOn1DS2ValuesDataValueDeltaFrameGHProm2) {
  960. UA_NetworkMessage m;
  961. memset(&m, 0, sizeof(UA_NetworkMessage));
  962. m.version = 1;
  963. m.networkMessageType = UA_NETWORKMESSAGE_DATASET;
  964. m.groupHeaderEnabled = true;
  965. m.groupHeader.networkMessageNumberEnabled = true;
  966. UA_UInt16 networkMessageNumber = 2175;
  967. m.groupHeader.networkMessageNumber = networkMessageNumber;
  968. m.promotedFieldsEnabled = true;
  969. m.promotedFieldsSize = 2;
  970. m.promotedFields = (UA_Variant*)UA_Array_new(m.promotedFieldsSize, &UA_TYPES[UA_TYPES_VARIANT]);
  971. UA_DataSetMessage dmdf;
  972. memset(&dmdf, 0, sizeof(UA_DataSetMessage));
  973. dmdf.header.dataSetMessageValid = true;
  974. dmdf.header.fieldEncoding = UA_FIELDENCODING_DATAVALUE;
  975. dmdf.header.dataSetMessageType = UA_DATASETMESSAGE_DATADELTAFRAME;
  976. dmdf.data.deltaFrameData.fieldCount = 2;
  977. size_t memsize = sizeof(UA_DataSetMessage_DeltaFrameField) * dmdf.data.deltaFrameData.fieldCount;
  978. dmdf.data.deltaFrameData.deltaFrameFields = (UA_DataSetMessage_DeltaFrameField*)UA_malloc(memsize);
  979. UA_UInt16 fieldIndex1 = 1;
  980. dmdf.data.deltaFrameData.deltaFrameFields[0].fieldIndex = fieldIndex1;
  981. UA_DataValue_init(&dmdf.data.deltaFrameData.deltaFrameFields[0].fieldValue);
  982. UA_UInt16 fieldIndex2 = 3;
  983. dmdf.data.deltaFrameData.deltaFrameFields[1].fieldIndex = fieldIndex2;
  984. UA_DataValue_init(&dmdf.data.deltaFrameData.deltaFrameFields[1].fieldValue);
  985. UA_Int32 iv = 1573;
  986. UA_Variant_setScalarCopy(&dmdf.data.deltaFrameData.deltaFrameFields[0].fieldValue.value, &iv, &UA_TYPES[UA_TYPES_INT32]);
  987. dmdf.data.deltaFrameData.deltaFrameFields[0].fieldValue.hasValue = true;
  988. dmdf.data.deltaFrameData.deltaFrameFields[0].fieldValue.sourceTimestamp = UA_DateTime_now();
  989. dmdf.data.deltaFrameData.deltaFrameFields[0].fieldValue.hasSourceTimestamp = true;
  990. UA_Variant_copy(&dmdf.data.deltaFrameData.deltaFrameFields[0].fieldValue.value, &m.promotedFields[0]);
  991. UA_Float fv = 197.34f;
  992. UA_Variant_setScalarCopy(&m.promotedFields[1], &fv, &UA_TYPES[UA_TYPES_FLOAT]);
  993. UA_String testString = (UA_String) { 5, (UA_Byte*)"OPC24" };
  994. UA_Variant value;
  995. UA_Variant_init(&value);
  996. value.data = UA_malloc(sizeof(UA_String));
  997. *((UA_String*)value.data) = testString;
  998. value.type = &UA_TYPES[UA_TYPES_STRING];
  999. dmdf.data.deltaFrameData.deltaFrameFields[1].fieldValue.value = value;
  1000. dmdf.data.deltaFrameData.deltaFrameFields[1].fieldValue.hasValue = true;
  1001. m.payload.dataSetPayload.dataSetMessages = &dmdf;
  1002. UA_StatusCode rv = UA_STATUSCODE_UNCERTAININITIALVALUE;
  1003. UA_ByteString buffer;
  1004. size_t msgSize = UA_NetworkMessage_calcSizeBinary(&m);
  1005. rv = UA_ByteString_allocBuffer(&buffer, msgSize);
  1006. ck_assert_int_eq(rv, UA_STATUSCODE_GOOD);
  1007. UA_Byte *bufPos = buffer.data;
  1008. memset(bufPos, 0, msgSize);
  1009. const UA_Byte *bufEnd = &(buffer.data[buffer.length]);
  1010. rv = UA_NetworkMessage_encodeBinary(&m, &bufPos, bufEnd);
  1011. ck_assert_int_eq(rv, UA_STATUSCODE_GOOD);
  1012. UA_NetworkMessage m2;
  1013. memset(&m2, 0, sizeof(UA_NetworkMessage));
  1014. size_t offset = 0;
  1015. rv = UA_NetworkMessage_decodeBinary(&buffer, &offset, &m2);
  1016. ck_assert_int_eq(rv, UA_STATUSCODE_GOOD);
  1017. ck_assert(m.version == m2.version);
  1018. ck_assert(m.networkMessageType == m2.networkMessageType);
  1019. ck_assert(m.timestampEnabled == m2.timestampEnabled);
  1020. ck_assert(m.dataSetClassIdEnabled == m2.dataSetClassIdEnabled);
  1021. ck_assert(m.groupHeaderEnabled == m2.groupHeaderEnabled);
  1022. ck_assert(m.groupHeader.writerGroupIdEnabled == m2.groupHeader.writerGroupIdEnabled);
  1023. ck_assert(m.groupHeader.groupVersionEnabled == m2.groupHeader.groupVersionEnabled);
  1024. ck_assert(m.groupHeader.networkMessageNumberEnabled == m2.groupHeader.networkMessageNumberEnabled);
  1025. ck_assert_uint_eq(m2.groupHeader.networkMessageNumber, networkMessageNumber);
  1026. ck_assert(m.groupHeader.sequenceNumberEnabled == m2.groupHeader.sequenceNumberEnabled);
  1027. ck_assert(m.picosecondsEnabled == m2.picosecondsEnabled);
  1028. ck_assert(m.promotedFieldsEnabled == m2.promotedFieldsEnabled);
  1029. ck_assert_uint_eq(m2.promotedFieldsSize, 2);
  1030. ck_assert_int_eq(*((UA_Int32*)m2.promotedFields[0].data), iv);
  1031. ck_assert(*((UA_Float*)m2.promotedFields[1].data) == fv);
  1032. ck_assert(m.publisherIdEnabled == m2.publisherIdEnabled);
  1033. ck_assert(m.securityEnabled == m2.securityEnabled);
  1034. ck_assert(m.chunkMessage == m2.chunkMessage);
  1035. ck_assert(m.payloadHeaderEnabled == m2.payloadHeaderEnabled);
  1036. ck_assert(m.payload.dataSetPayload.dataSetMessages[0].header.dataSetMessageValid == m2.payload.dataSetPayload.dataSetMessages[0].header.dataSetMessageValid);
  1037. ck_assert(m.payload.dataSetPayload.dataSetMessages[0].header.fieldEncoding == m2.payload.dataSetPayload.dataSetMessages[0].header.fieldEncoding);
  1038. ck_assert_int_eq(m2.payload.dataSetPayload.dataSetMessages[0].data.deltaFrameData.fieldCount, 2);
  1039. ck_assert_int_eq(m.payload.dataSetPayload.dataSetMessages[0].data.deltaFrameData.deltaFrameFields[0].fieldIndex, fieldIndex1);
  1040. ck_assert(m.payload.dataSetPayload.dataSetMessages[0].data.deltaFrameData.deltaFrameFields[0].fieldValue.hasValue == m2.payload.dataSetPayload.dataSetMessages[0].data.deltaFrameData.deltaFrameFields[0].fieldValue.hasValue);
  1041. ck_assert_int_eq((uintptr_t)m2.payload.dataSetPayload.dataSetMessages[0].data.deltaFrameData.deltaFrameFields[0].fieldValue.value.type, (uintptr_t)&UA_TYPES[UA_TYPES_INT32]);
  1042. ck_assert_int_eq(*(UA_Int32 *)m2.payload.dataSetPayload.dataSetMessages[0].data.deltaFrameData.deltaFrameFields[0].fieldValue.value.data, iv);
  1043. ck_assert(m.payload.dataSetPayload.dataSetMessages[0].data.deltaFrameData.deltaFrameFields[0].fieldValue.hasSourceTimestamp == m2.payload.dataSetPayload.dataSetMessages[0].data.deltaFrameData.deltaFrameFields[0].fieldValue.hasSourceTimestamp);
  1044. ck_assert_int_eq(m.payload.dataSetPayload.dataSetMessages[0].data.deltaFrameData.deltaFrameFields[1].fieldIndex, fieldIndex2);
  1045. ck_assert(m.payload.dataSetPayload.dataSetMessages[0].data.deltaFrameData.deltaFrameFields[1].fieldValue.hasValue == m2.payload.dataSetPayload.dataSetMessages[0].data.deltaFrameData.deltaFrameFields[1].fieldValue.hasValue);
  1046. // compare string value
  1047. UA_String decodedString = *(UA_String*)(m2.payload.dataSetPayload.dataSetMessages[0].data.deltaFrameData.deltaFrameFields[1].fieldValue.value.data);
  1048. for (UA_Int32 i = 0; i < 5; i++)
  1049. {
  1050. ck_assert_int_eq(decodedString.data[i], testString.data[i]);
  1051. }
  1052. ck_assert_int_eq(decodedString.length, testString.length);
  1053. ck_assert(m.payload.dataSetPayload.dataSetMessages[0].data.deltaFrameData.deltaFrameFields[1].fieldValue.hasSourceTimestamp == m2.payload.dataSetPayload.dataSetMessages[0].data.deltaFrameData.deltaFrameFields[1].fieldValue.hasSourceTimestamp);
  1054. UA_Array_delete(m.promotedFields, m.promotedFieldsSize, &UA_TYPES[UA_TYPES_VARIANT]);
  1055. ((UA_String*)value.data)->data = NULL; // the string is statically allocated. do not free it.
  1056. UA_Variant_deleteMembers(&value);
  1057. UA_DataValue_deleteMembers(&dmdf.data.deltaFrameData.deltaFrameFields[0].fieldValue);
  1058. UA_NetworkMessage_deleteMembers(&m2);
  1059. UA_ByteString_deleteMembers(&buffer);
  1060. UA_free(dmdf.data.deltaFrameData.deltaFrameFields);
  1061. }
  1062. END_TEST
  1063. START_TEST(UA_PubSub_EnDecode_ShallWorkOn2DSVariant) {
  1064. UA_NetworkMessage m;
  1065. memset(&m, 0, sizeof(UA_NetworkMessage));
  1066. m.version = 1;
  1067. m.networkMessageType = UA_NETWORKMESSAGE_DATASET;
  1068. m.payloadHeaderEnabled = true;
  1069. m.payloadHeader.dataSetPayloadHeader.count = 2;
  1070. UA_UInt16 dsWriter1 = 4;
  1071. UA_UInt16 dsWriter2 = 7;
  1072. m.payloadHeader.dataSetPayloadHeader.dataSetWriterIds = (UA_UInt16 *)UA_Array_new(m.payloadHeader.dataSetPayloadHeader.count, &UA_TYPES[UA_TYPES_UINT16]);
  1073. m.payloadHeader.dataSetPayloadHeader.dataSetWriterIds[0] = dsWriter1;
  1074. m.payloadHeader.dataSetPayloadHeader.dataSetWriterIds[1] = dsWriter2;
  1075. size_t memsize = m.payloadHeader.dataSetPayloadHeader.count * sizeof(UA_DataSetMessage);
  1076. m.payload.dataSetPayload.dataSetMessages = (UA_DataSetMessage*)UA_malloc(memsize);
  1077. memset(m.payload.dataSetPayload.dataSetMessages, 0, memsize);
  1078. //UA_DataSetMessage dmkf;
  1079. //memset(&dmkf, 0, sizeof(UA_DataSetMessage));
  1080. m.payload.dataSetPayload.dataSetMessages[0].header.dataSetMessageValid = true;
  1081. m.payload.dataSetPayload.dataSetMessages[0].header.fieldEncoding = UA_FIELDENCODING_VARIANT;
  1082. m.payload.dataSetPayload.dataSetMessages[0].header.dataSetMessageType = UA_DATASETMESSAGE_DATAKEYFRAME;
  1083. UA_UInt16 fieldCountDS1 = 1;
  1084. m.payload.dataSetPayload.dataSetMessages[0].data.keyFrameData.fieldCount = fieldCountDS1;
  1085. m.payload.dataSetPayload.dataSetMessages[0].data.keyFrameData.dataSetFields =
  1086. (UA_DataValue*)UA_Array_new(m.payload.dataSetPayload.dataSetMessages[0].data.keyFrameData.fieldCount, &UA_TYPES[UA_TYPES_DATAVALUE]);
  1087. UA_DataValue_init(&m.payload.dataSetPayload.dataSetMessages[0].data.keyFrameData.dataSetFields[0]);
  1088. UA_UInt32 iv = 27;
  1089. UA_Variant_setScalarCopy(&m.payload.dataSetPayload.dataSetMessages[0].data.keyFrameData.dataSetFields[0].value, &iv, &UA_TYPES[UA_TYPES_UINT32]);
  1090. m.payload.dataSetPayload.dataSetMessages[0].data.keyFrameData.dataSetFields[0].hasValue = true;
  1091. m.payload.dataSetPayload.dataSetMessages[1].header.dataSetMessageValid = true;
  1092. m.payload.dataSetPayload.dataSetMessages[1].header.fieldEncoding = UA_FIELDENCODING_DATAVALUE;
  1093. m.payload.dataSetPayload.dataSetMessages[1].header.dataSetMessageType = UA_DATASETMESSAGE_DATADELTAFRAME;
  1094. UA_UInt16 fieldCountDS2 = 2;
  1095. m.payload.dataSetPayload.dataSetMessages[1].data.deltaFrameData.fieldCount = fieldCountDS2;
  1096. memsize = sizeof(UA_DataSetMessage_DeltaFrameField) * m.payload.dataSetPayload.dataSetMessages[1].data.deltaFrameData.fieldCount;
  1097. m.payload.dataSetPayload.dataSetMessages[1].data.deltaFrameData.deltaFrameFields = (UA_DataSetMessage_DeltaFrameField*)UA_malloc(memsize);
  1098. UA_Guid gv = UA_Guid_random();
  1099. UA_UInt16 fieldIndex1 = 2;
  1100. m.payload.dataSetPayload.dataSetMessages[1].data.deltaFrameData.deltaFrameFields[0].fieldIndex = fieldIndex1;
  1101. UA_DataValue_init(&m.payload.dataSetPayload.dataSetMessages[1].data.deltaFrameData.deltaFrameFields[0].fieldValue);
  1102. UA_Variant_setScalar(&m.payload.dataSetPayload.dataSetMessages[1].data.deltaFrameData.deltaFrameFields[0].fieldValue.value, &gv, &UA_TYPES[UA_TYPES_GUID]);
  1103. m.payload.dataSetPayload.dataSetMessages[1].data.deltaFrameData.deltaFrameFields[0].fieldValue.hasValue = true;
  1104. UA_UInt16 fieldIndex2 = 5;
  1105. m.payload.dataSetPayload.dataSetMessages[1].data.deltaFrameData.deltaFrameFields[1].fieldIndex = fieldIndex2;
  1106. UA_DataValue_init(&m.payload.dataSetPayload.dataSetMessages[1].data.deltaFrameData.deltaFrameFields[1].fieldValue);
  1107. UA_Int64 iv64 = 152478978534;
  1108. UA_Variant_setScalar(&m.payload.dataSetPayload.dataSetMessages[1].data.deltaFrameData.deltaFrameFields[1].fieldValue.value, &iv64, &UA_TYPES[UA_TYPES_INT64]);
  1109. m.payload.dataSetPayload.dataSetMessages[1].data.deltaFrameData.deltaFrameFields[1].fieldValue.hasValue = true;
  1110. UA_StatusCode rv = UA_STATUSCODE_UNCERTAININITIALVALUE;
  1111. UA_ByteString buffer;
  1112. size_t msgSize = UA_NetworkMessage_calcSizeBinary(&m);
  1113. rv = UA_ByteString_allocBuffer(&buffer, msgSize);
  1114. ck_assert_int_eq(rv, UA_STATUSCODE_GOOD);
  1115. UA_Byte *bufPos = buffer.data;
  1116. memset(bufPos, 0, msgSize);
  1117. const UA_Byte *bufEnd = &(buffer.data[buffer.length]);
  1118. rv = UA_NetworkMessage_encodeBinary(&m, &bufPos, bufEnd);
  1119. ck_assert_int_eq(rv, UA_STATUSCODE_GOOD);
  1120. UA_NetworkMessage m2;
  1121. memset(&m2, 0, sizeof(UA_NetworkMessage));
  1122. size_t offset = 0;
  1123. rv = UA_NetworkMessage_decodeBinary(&buffer, &offset, &m2);
  1124. ck_assert_int_eq(rv, UA_STATUSCODE_GOOD);
  1125. ck_assert(m.version == m2.version);
  1126. ck_assert(m.networkMessageType == m2.networkMessageType);
  1127. ck_assert(m.timestampEnabled == m2.timestampEnabled);
  1128. ck_assert(m.dataSetClassIdEnabled == m2.dataSetClassIdEnabled);
  1129. ck_assert(m.groupHeaderEnabled == m2.groupHeaderEnabled);
  1130. ck_assert(m.picosecondsEnabled == m2.picosecondsEnabled);
  1131. ck_assert(m.promotedFieldsEnabled == m2.promotedFieldsEnabled);
  1132. ck_assert(m.publisherIdEnabled == m2.publisherIdEnabled);
  1133. ck_assert(m.securityEnabled == m2.securityEnabled);
  1134. ck_assert(m.chunkMessage == m2.chunkMessage);
  1135. ck_assert(m.payloadHeaderEnabled == m2.payloadHeaderEnabled);
  1136. ck_assert_uint_eq(m2.payloadHeader.dataSetPayloadHeader.dataSetWriterIds[0], dsWriter1);
  1137. ck_assert_uint_eq(m2.payloadHeader.dataSetPayloadHeader.dataSetWriterIds[1], dsWriter2);
  1138. ck_assert(m.payload.dataSetPayload.dataSetMessages[0].header.dataSetMessageValid == m2.payload.dataSetPayload.dataSetMessages[0].header.dataSetMessageValid);
  1139. ck_assert(m.payload.dataSetPayload.dataSetMessages[0].header.fieldEncoding == m2.payload.dataSetPayload.dataSetMessages[0].header.fieldEncoding);
  1140. ck_assert_int_eq(m2.payload.dataSetPayload.dataSetMessages[0].data.keyFrameData.fieldCount, fieldCountDS1);
  1141. ck_assert(m.payload.dataSetPayload.dataSetMessages[0].data.keyFrameData.dataSetFields[0].hasValue == m2.payload.dataSetPayload.dataSetMessages[0].data.keyFrameData.dataSetFields[0].hasValue);
  1142. ck_assert_uint_eq((uintptr_t)m2.payload.dataSetPayload.dataSetMessages[0].data.keyFrameData.dataSetFields[0].value.type, (uintptr_t)&UA_TYPES[UA_TYPES_UINT32]);
  1143. ck_assert_uint_eq(*(UA_UInt32 *)m2.payload.dataSetPayload.dataSetMessages[0].data.keyFrameData.dataSetFields[0].value.data, iv);
  1144. ck_assert(m.payload.dataSetPayload.dataSetMessages[0].data.keyFrameData.dataSetFields[0].hasSourceTimestamp == m2.payload.dataSetPayload.dataSetMessages[0].data.keyFrameData.dataSetFields[0].hasSourceTimestamp);
  1145. ck_assert(m.payload.dataSetPayload.dataSetMessages[1].header.dataSetMessageValid == m2.payload.dataSetPayload.dataSetMessages[1].header.dataSetMessageValid);
  1146. ck_assert(m.payload.dataSetPayload.dataSetMessages[1].header.fieldEncoding == m2.payload.dataSetPayload.dataSetMessages[1].header.fieldEncoding);
  1147. ck_assert_int_eq(m2.payload.dataSetPayload.dataSetMessages[1].data.deltaFrameData.fieldCount, fieldCountDS2);
  1148. ck_assert(m.payload.dataSetPayload.dataSetMessages[1].data.deltaFrameData.deltaFrameFields[0].fieldValue.hasValue == m2.payload.dataSetPayload.dataSetMessages[1].data.deltaFrameData.deltaFrameFields[0].fieldValue.hasValue);
  1149. ck_assert_uint_eq(m2.payload.dataSetPayload.dataSetMessages[1].data.deltaFrameData.deltaFrameFields[0].fieldIndex, fieldIndex1);
  1150. ck_assert_uint_eq((uintptr_t)m2.payload.dataSetPayload.dataSetMessages[1].data.deltaFrameData.deltaFrameFields[0].fieldValue.value.type, (uintptr_t)&UA_TYPES[UA_TYPES_GUID]);
  1151. ck_assert(UA_Guid_equal((UA_Guid*)m2.payload.dataSetPayload.dataSetMessages[1].data.deltaFrameData.deltaFrameFields[0].fieldValue.value.data, &gv) == true);
  1152. ck_assert(m.payload.dataSetPayload.dataSetMessages[1].data.deltaFrameData.deltaFrameFields[0].fieldValue.hasSourceTimestamp == m2.payload.dataSetPayload.dataSetMessages[1].data.deltaFrameData.deltaFrameFields[0].fieldValue.hasSourceTimestamp);
  1153. ck_assert(m.payload.dataSetPayload.dataSetMessages[1].data.deltaFrameData.deltaFrameFields[1].fieldValue.hasValue == m2.payload.dataSetPayload.dataSetMessages[1].data.deltaFrameData.deltaFrameFields[1].fieldValue.hasValue);
  1154. ck_assert_uint_eq(m2.payload.dataSetPayload.dataSetMessages[1].data.deltaFrameData.deltaFrameFields[1].fieldIndex, fieldIndex2);
  1155. ck_assert_uint_eq((uintptr_t)m2.payload.dataSetPayload.dataSetMessages[1].data.deltaFrameData.deltaFrameFields[1].fieldValue.value.type, (uintptr_t)&UA_TYPES[UA_TYPES_INT64]);
  1156. ck_assert_int_eq(*(UA_Int64 *)m2.payload.dataSetPayload.dataSetMessages[1].data.deltaFrameData.deltaFrameFields[1].fieldValue.value.data, iv64);
  1157. ck_assert(m.payload.dataSetPayload.dataSetMessages[1].data.deltaFrameData.deltaFrameFields[1].fieldValue.hasSourceTimestamp == m2.payload.dataSetPayload.dataSetMessages[1].data.deltaFrameData.deltaFrameFields[1].fieldValue.hasSourceTimestamp);
  1158. UA_Array_delete(m.payloadHeader.dataSetPayloadHeader.dataSetWriterIds, m.payloadHeader.dataSetPayloadHeader.count, &UA_TYPES[UA_TYPES_UINT16]);
  1159. UA_Array_delete(m.payload.dataSetPayload.dataSetMessages[0].data.keyFrameData.dataSetFields, m.payload.dataSetPayload.dataSetMessages[0].data.keyFrameData.fieldCount, &UA_TYPES[UA_TYPES_DATAVALUE]);
  1160. UA_free(m.payload.dataSetPayload.dataSetMessages[1].data.deltaFrameData.deltaFrameFields);
  1161. UA_NetworkMessage_deleteMembers(&m2);
  1162. UA_ByteString_deleteMembers(&buffer);
  1163. UA_free(m.payload.dataSetPayload.dataSetMessages);
  1164. //UA_Array_delete(dmkf.data.keyFrameData.dataSetFields, dmkf.data.keyFrameData.fieldCount, &UA_TYPES[UA_TYPES_DATAVALUE]);
  1165. }
  1166. END_TEST
  1167. int main(void) {
  1168. TCase *tc_encode = tcase_create("encode");
  1169. tcase_add_test(tc_encode, UA_PubSub_Encode_WithBufferTooSmallShallReturnError);
  1170. TCase *tc_decode = tcase_create("decode");
  1171. tcase_add_test(tc_decode, UA_PubSub_Decode_WithBufferTooSmallShallReturnError);
  1172. TCase *tc_ende1 = tcase_create("encode_decode1DS");
  1173. tcase_add_test(tc_ende1, UA_PubSub_EnDecode_ShallWorkOn1DS1ValueVariantKeyFrame);
  1174. tcase_add_test(tc_ende1, UA_PubSub_EnDecode_ShallWorkOn1DS1ValueDataValueKeyFrame);
  1175. tcase_add_test(tc_ende1, UA_PubSub_EnDecode_ShallWorkOn1DS2ValuesVariantKeyFrame);
  1176. tcase_add_test(tc_ende1, UA_PubSub_EnDecode_ShallWorkOn1DS2ValuesDataValueKeyFrame);
  1177. tcase_add_test(tc_ende1, UA_PubSub_EnDecode_ShallWorkOn1DS1ValueVariantDeltaFrame);
  1178. tcase_add_test(tc_ende1, UA_PubSub_EnDecode_ShallWorkOn1DS1ValueDataValueDeltaFrame);
  1179. tcase_add_test(tc_ende1, UA_PubSub_EnDecode_ShallWorkOn1DS2ValuesVariantDeltaFrame);
  1180. tcase_add_test(tc_ende1, UA_PubSub_EnDecode_ShallWorkOn1DS2ValuesDataValueDeltaFrame);
  1181. tcase_add_test(tc_ende1, UA_PubSub_EnDecode_ShallWorkOn1DS2ValuesVariantKeyFrameGroupHeader);
  1182. tcase_add_test(tc_ende1, UA_PubSub_EnDecode_ShallWorkOn1DS2ValuesVariantDeltaFramePublDSCID);
  1183. tcase_add_test(tc_ende1, UA_PubSub_EnDecode_ShallWorkOn1DS2ValuesDataValueKeyFramePH);
  1184. tcase_add_test(tc_ende1, UA_PubSub_EnDecode_ShallWorkOn1DS2ValuesVariantKeyFrameTSProm);
  1185. tcase_add_test(tc_ende1, UA_PubSub_EnDecode_ShallWorkOn1DS2ValuesDataValueDeltaFrameGHProm2);
  1186. TCase *tc_ende2 = tcase_create("encode_decode2DS");
  1187. tcase_add_test(tc_ende2, UA_PubSub_EnDecode_ShallWorkOn2DSVariant);
  1188. Suite *s = suite_create("PubSub NetworkMessage");
  1189. suite_add_tcase(s, tc_encode);
  1190. suite_add_tcase(s, tc_decode);
  1191. suite_add_tcase(s, tc_ende1);
  1192. suite_add_tcase(s, tc_ende2);
  1193. SRunner *sr = srunner_create(s);
  1194. srunner_set_fork_status(sr, CK_NOFORK);
  1195. srunner_run_all(sr,CK_NORMAL);
  1196. int number_failed = srunner_ntests_failed(sr);
  1197. srunner_free(sr);
  1198. return (number_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
  1199. }