check_pubsub_encoding.c 82 KB

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