check_builtin.c 61 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include "ua_types.h"
  4. #include "ua_types_encoding_binary.h"
  5. #include "ua_types_generated.h"
  6. #include "ua_types_generated_encoding_binary.h"
  7. //#include "ua_transport.h"
  8. #include "ua_util.h"
  9. #include "check.h"
  10. /* copied here from encoding_binary.c */
  11. enum UA_VARIANT_ENCODINGMASKTYPE_enum {
  12. UA_VARIANT_ENCODINGMASKTYPE_TYPEID_MASK = 0x3F, // bits 0:5
  13. UA_VARIANT_ENCODINGMASKTYPE_DIMENSIONS = (0x01 << 6), // bit 6
  14. UA_VARIANT_ENCODINGMASKTYPE_ARRAY = (0x01 << 7) // bit 7
  15. };
  16. START_TEST(UA_ExtensionObject_calcSizeShallWorkOnExample) {
  17. // given
  18. UA_Byte data[3] = { 1, 2, 3 };
  19. UA_ExtensionObject extensionObject;
  20. // empty ExtensionObject, handcoded
  21. // when
  22. UA_ExtensionObject_init(&extensionObject);
  23. extensionObject.typeId.identifierType = UA_NODEIDTYPE_NUMERIC;
  24. extensionObject.typeId.identifier.numeric = 0;
  25. extensionObject.encoding = UA_EXTENSIONOBJECT_ENCODINGMASK_NOBODYISENCODED;
  26. // then
  27. ck_assert_int_eq(UA_calcSizeBinary(&extensionObject, &UA_TYPES[UA_TYPES_EXTENSIONOBJECT]), 1 + 1 + 1);
  28. // ExtensionObject with ByteString-Body
  29. // when
  30. extensionObject.encoding = UA_EXTENSIONOBJECT_ENCODINGMASK_BODYISBYTESTRING;
  31. extensionObject.body.data = data;
  32. extensionObject.body.length = 3;
  33. // then
  34. ck_assert_int_eq(UA_calcSizeBinary(&extensionObject, &UA_TYPES[UA_TYPES_EXTENSIONOBJECT]), 3 + 4 + 3);
  35. }
  36. END_TEST
  37. START_TEST(UA_DataValue_calcSizeShallWorkOnExample) {
  38. // given
  39. UA_DataValue dataValue;
  40. UA_DataValue_init(&dataValue);
  41. dataValue.status = 12;
  42. dataValue.hasStatus = UA_TRUE;
  43. dataValue.sourceTimestamp = 80;
  44. dataValue.hasSourceTimestamp = UA_TRUE;
  45. dataValue.sourcePicoseconds = 214;
  46. dataValue.hasSourcePicoseconds = UA_TRUE;
  47. int size = 0;
  48. // when
  49. size = UA_calcSizeBinary(&dataValue, &UA_TYPES[UA_TYPES_DATAVALUE]);
  50. // then
  51. // 1 (bitfield) + 4 (status) + 8 (timestamp) + 2 (picoseconds)
  52. ck_assert_int_eq(size, 15);
  53. }
  54. END_TEST
  55. START_TEST(UA_DiagnosticInfo_calcSizeShallWorkOnExample) {
  56. // given
  57. UA_DiagnosticInfo diagnosticInfo;
  58. UA_DiagnosticInfo_init(&diagnosticInfo);
  59. diagnosticInfo.symbolicId = 30;
  60. diagnosticInfo.hasSymbolicId = UA_TRUE;
  61. diagnosticInfo.namespaceUri = 25;
  62. diagnosticInfo.hasNamespaceUri = UA_TRUE;
  63. diagnosticInfo.localizedText = 22;
  64. diagnosticInfo.hasLocalizedText = UA_TRUE;
  65. UA_Byte additionalInfoData = 'd';
  66. diagnosticInfo.additionalInfo.data = &additionalInfoData; //"OPCUA";
  67. diagnosticInfo.additionalInfo.length = 1;
  68. diagnosticInfo.hasAdditionalInfo = UA_TRUE;
  69. // when & then
  70. // 1 (bitfield) + 4 (symbolic id) + 4 (namespaceuri) + 4 (localizedtext) + 5 (additionalinfo)
  71. ck_assert_int_eq(UA_calcSizeBinary(&diagnosticInfo, &UA_TYPES[UA_TYPES_DIAGNOSTICINFO]), 18);
  72. }
  73. END_TEST
  74. START_TEST(UA_String_calcSizeWithNegativLengthShallReturnEncodingSize) {
  75. // given
  76. UA_String arg = { -1, UA_NULL };
  77. // when
  78. UA_UInt32 encodingSize = UA_calcSizeBinary(&arg, &UA_TYPES[UA_TYPES_STRING]);
  79. // then
  80. ck_assert_int_eq(encodingSize, 4);
  81. }
  82. END_TEST
  83. START_TEST(UA_String_calcSizeWithNegativLengthAndValidPointerShallReturnEncodingSize) {
  84. // given
  85. UA_String arg = { -1, (UA_Byte *)"OPC" };
  86. // when
  87. UA_UInt32 encodingSize = UA_calcSizeBinary(&arg, &UA_TYPES[UA_TYPES_STRING]);
  88. // then
  89. ck_assert_int_eq(encodingSize, 4);
  90. }
  91. END_TEST
  92. START_TEST(UA_String_calcSizeWithZeroLengthShallReturnEncodingSize) {
  93. // given
  94. UA_String arg = { 0, UA_NULL };
  95. // when
  96. UA_UInt32 encodingSize = UA_calcSizeBinary(&arg, &UA_TYPES[UA_TYPES_STRING]);
  97. // then
  98. ck_assert_int_eq(encodingSize, 4);
  99. }
  100. END_TEST
  101. START_TEST(UA_String_calcSizeWithZeroLengthAndValidPointerShallReturnEncodingSize) {
  102. // given
  103. UA_String arg = { 0, (UA_Byte *)"OPC" };
  104. // when
  105. UA_UInt32 encodingSize = UA_calcSizeBinary(&arg, &UA_TYPES[UA_TYPES_STRING]);
  106. // then
  107. ck_assert_int_eq(encodingSize, 4);
  108. }
  109. END_TEST
  110. START_TEST(UA_String_calcSizeShallReturnEncodingSize) {
  111. // given
  112. UA_String arg = { 3, (UA_Byte *)"OPC" };
  113. // when
  114. UA_UInt32 encodingSize = UA_calcSizeBinary(&arg, &UA_TYPES[UA_TYPES_STRING]);
  115. // then
  116. ck_assert_int_eq(encodingSize, 4+3);
  117. }
  118. END_TEST
  119. START_TEST(UA_NodeId_calcSizeEncodingTwoByteShallReturnEncodingSize) {
  120. // given
  121. UA_NodeId arg;
  122. arg.identifierType = UA_NODEIDTYPE_NUMERIC;
  123. arg.namespaceIndex = 0;
  124. arg.identifier.numeric = 1;
  125. // when
  126. UA_UInt32 encodingSize = UA_calcSizeBinary(&arg, &UA_TYPES[UA_TYPES_NODEID]);
  127. // then
  128. ck_assert_int_eq(encodingSize, 2);
  129. }
  130. END_TEST
  131. START_TEST(UA_NodeId_calcSizeEncodingFourByteShallReturnEncodingSize) {
  132. // given
  133. UA_NodeId arg;
  134. arg.identifierType = UA_NODEIDTYPE_NUMERIC;
  135. arg.namespaceIndex = 1;
  136. arg.identifier.numeric = 1;
  137. // when
  138. UA_UInt32 encodingSize = UA_calcSizeBinary(&arg, &UA_TYPES[UA_TYPES_NODEID]);
  139. // then
  140. ck_assert_int_eq(encodingSize, 4);
  141. }
  142. END_TEST
  143. START_TEST(UA_NodeId_calcSizeEncodingStringShallReturnEncodingSize) {
  144. // given
  145. UA_NodeId arg;
  146. arg.identifierType = UA_NODEIDTYPE_STRING;
  147. arg.identifier.string.length = 3;
  148. arg.identifier.string.data = (UA_Byte *)"PLT";
  149. // when
  150. UA_UInt32 encodingSize = UA_calcSizeBinary(&arg, &UA_TYPES[UA_TYPES_NODEID]);
  151. // then
  152. ck_assert_int_eq(encodingSize, 1+2+4+3);
  153. }
  154. END_TEST
  155. START_TEST(UA_NodeId_calcSizeEncodingStringNegativLengthShallReturnEncodingSize) {
  156. // given
  157. UA_NodeId arg;
  158. arg.identifierType = UA_NODEIDTYPE_STRING;
  159. arg.identifier.string.length = -1;
  160. // when
  161. UA_UInt32 encodingSize = UA_calcSizeBinary(&arg, &UA_TYPES[UA_TYPES_NODEID]);
  162. // then
  163. ck_assert_int_eq(encodingSize, 1+2+4+0);
  164. }
  165. END_TEST
  166. START_TEST(UA_NodeId_calcSizeEncodingStringZeroLengthShallReturnEncodingSize) {
  167. // given
  168. UA_NodeId arg;
  169. arg.identifierType = UA_NODEIDTYPE_STRING;
  170. arg.identifier.string.length = 0;
  171. // when
  172. UA_UInt32 encodingSize = UA_calcSizeBinary(&arg, &UA_TYPES[UA_TYPES_NODEID]);
  173. // then
  174. ck_assert_int_eq(encodingSize, 1+2+4+0);
  175. }
  176. END_TEST
  177. START_TEST(UA_ExpandedNodeId_calcSizeEncodingStringAndServerIndexShallReturnEncodingSize) {
  178. // given
  179. UA_ExpandedNodeId arg;
  180. UA_ExpandedNodeId_init(&arg);
  181. arg.nodeId.identifierType = UA_NODEIDTYPE_STRING;
  182. arg.serverIndex = 1;
  183. arg.nodeId.identifier.string.length = 3;
  184. // when
  185. UA_UInt32 encodingSize = UA_calcSizeBinary(&arg, &UA_TYPES[UA_TYPES_EXPANDEDNODEID]);
  186. // then
  187. ck_assert_int_eq(encodingSize, 1+2+4+3+4);
  188. }
  189. END_TEST
  190. START_TEST(UA_ExpandedNodeId_calcSizeEncodingStringAndNamespaceUriShallReturnEncodingSize) {
  191. // given
  192. UA_ExpandedNodeId arg;
  193. UA_ExpandedNodeId_init(&arg);
  194. arg.nodeId.identifierType = UA_NODEIDTYPE_STRING;
  195. arg.nodeId.identifier.string.length = 3;
  196. arg.namespaceUri.length = 7;
  197. // when
  198. UA_UInt32 encodingSize = UA_calcSizeBinary(&arg, &UA_TYPES[UA_TYPES_EXPANDEDNODEID]);
  199. // then
  200. ck_assert_int_eq(encodingSize, 1+2+4+3+4+7);
  201. }
  202. END_TEST
  203. START_TEST(UA_Guid_calcSizeShallReturnEncodingSize) {
  204. // given
  205. UA_Guid arg;
  206. // when
  207. UA_UInt32 encodingSize = UA_calcSizeBinary(&arg, &UA_TYPES[UA_TYPES_GUID]);
  208. // then
  209. ck_assert_int_eq(encodingSize, 16);
  210. }
  211. END_TEST
  212. START_TEST(UA_LocalizedText_calcSizeTextOnlyShallReturnEncodingSize) {
  213. // given
  214. UA_LocalizedText arg;
  215. UA_LocalizedText_init(&arg);
  216. arg.text = (UA_String) {8, (UA_Byte *)"12345678"};
  217. // when
  218. UA_UInt32 encodingSize = UA_calcSizeBinary(&arg, &UA_TYPES[UA_TYPES_LOCALIZEDTEXT]);
  219. // then
  220. ck_assert_int_eq(encodingSize, 1+4+8);
  221. // finally
  222. UA_LocalizedText_init(&arg); // do not delete text
  223. UA_LocalizedText_deleteMembers(&arg);
  224. }
  225. END_TEST
  226. START_TEST(UA_LocalizedText_calcSizeLocaleOnlyShallReturnEncodingSize) {
  227. // given
  228. UA_LocalizedText arg;
  229. UA_LocalizedText_init(&arg);
  230. arg.locale = (UA_String) {8, (UA_Byte *)"12345678"};
  231. // when
  232. UA_UInt32 encodingSize = UA_calcSizeBinary(&arg, &UA_TYPES[UA_TYPES_LOCALIZEDTEXT]);
  233. // then
  234. ck_assert_int_eq(encodingSize, 1+4+8);
  235. UA_LocalizedText_init(&arg); // do not delete locale
  236. UA_LocalizedText_deleteMembers(&arg);
  237. }
  238. END_TEST
  239. START_TEST(UA_LocalizedText_calcSizeTextAndLocaleShallReturnEncodingSize) {
  240. // given
  241. UA_LocalizedText arg;
  242. UA_LocalizedText_init(&arg);
  243. arg.locale = (UA_String) {8, (UA_Byte *)"12345678"};
  244. arg.text = (UA_String) {8, (UA_Byte *)"12345678"};
  245. // when
  246. UA_UInt32 encodingSize = UA_calcSizeBinary(&arg, &UA_TYPES[UA_TYPES_LOCALIZEDTEXT]);
  247. // then
  248. ck_assert_int_eq(encodingSize, 1+4+8+4+8);
  249. UA_LocalizedText_init(&arg); // do not delete locale and text
  250. UA_LocalizedText_deleteMembers(&arg);
  251. }
  252. END_TEST
  253. START_TEST(UA_Variant_calcSizeFixedSizeArrayShallReturnEncodingSize) {
  254. // given
  255. UA_Variant arg;
  256. UA_Variant_init(&arg);
  257. arg.type = &UA_TYPES[UA_TYPES_INT32];
  258. #define ARRAY_LEN 8
  259. arg.arrayLength = ARRAY_LEN;
  260. UA_Int32 *data[ARRAY_LEN];
  261. arg.data = (void *)data;
  262. // when
  263. UA_UInt32 encodingSize = UA_calcSizeBinary(&arg, &UA_TYPES[UA_TYPES_VARIANT]);
  264. // then
  265. ck_assert_int_eq(encodingSize, 1+4+ARRAY_LEN*4);
  266. #undef ARRAY_LEN
  267. }
  268. END_TEST
  269. START_TEST(UA_Variant_calcSizeVariableSizeArrayShallReturnEncodingSize) {
  270. // given
  271. UA_Variant arg;
  272. UA_Variant_init(&arg);
  273. arg.type = &UA_TYPES[UA_TYPES_STRING];
  274. #define ARRAY_LEN 3
  275. arg.arrayLength = ARRAY_LEN;
  276. UA_String strings[3];
  277. strings[0] = (UA_String) {-1, UA_NULL };
  278. strings[1] = (UA_String) {3, (UA_Byte *)"PLT" };
  279. strings[2] = (UA_String) {47, UA_NULL };
  280. arg.data = (void *)strings;
  281. // when
  282. UA_UInt32 encodingSize = UA_calcSizeBinary(&arg, &UA_TYPES[UA_TYPES_VARIANT]);
  283. // then
  284. ck_assert_int_eq(encodingSize, 1+4+(4+0)+(4+3)+(4+47));
  285. #undef ARRAY_LEN
  286. }
  287. END_TEST
  288. START_TEST(UA_Byte_decodeShallCopyAndAdvancePosition) {
  289. // given
  290. UA_Byte dst;
  291. UA_Byte data[] = { 0x08 };
  292. UA_ByteString src = { 1, data };
  293. size_t pos = 0;
  294. // when
  295. UA_StatusCode retval = UA_Byte_decodeBinary(&src, &pos, &dst);
  296. // then
  297. ck_assert_int_eq(retval, UA_STATUSCODE_GOOD);
  298. ck_assert_uint_eq(pos, 1);
  299. ck_assert_uint_eq(dst, 0x08);
  300. }
  301. END_TEST
  302. START_TEST(UA_Byte_decodeShallModifyOnlyCurrentPosition) {
  303. // given
  304. UA_Byte dst[] = { 0xFF, 0xFF, 0xFF };
  305. UA_Byte data[] = { 0x08 };
  306. UA_ByteString src = { 1, data };
  307. size_t pos = 0;
  308. // when
  309. UA_StatusCode retval = UA_Byte_decodeBinary(&src, &pos, &dst[1]);
  310. // then
  311. ck_assert_int_eq(retval, UA_STATUSCODE_GOOD);
  312. ck_assert_int_eq(pos, 1);
  313. ck_assert_uint_eq(dst[0], 0xFF);
  314. ck_assert_uint_eq(dst[1], 0x08);
  315. ck_assert_uint_eq(dst[2], 0xFF);
  316. }
  317. END_TEST
  318. START_TEST(UA_Int16_decodeShallAssumeLittleEndian) {
  319. // given
  320. size_t pos = 0;
  321. UA_Byte data[] = {
  322. 0x01, 0x00, // 1
  323. 0x00, 0x01 // 256
  324. };
  325. UA_ByteString src = { 4, data };
  326. // when
  327. UA_Int16 val_01_00, val_00_01;
  328. UA_StatusCode retval = UA_Int16_decodeBinary(&src, &pos, &val_01_00);
  329. retval |= UA_Int16_decodeBinary(&src, &pos, &val_00_01);
  330. // then
  331. ck_assert_int_eq(retval, UA_STATUSCODE_GOOD);
  332. ck_assert_int_eq(val_01_00, 1);
  333. ck_assert_int_eq(val_00_01, 256);
  334. ck_assert_int_eq(pos, 4);
  335. }
  336. END_TEST
  337. START_TEST(UA_Int16_decodeShallRespectSign) {
  338. // given
  339. size_t pos = 0;
  340. UA_Byte data[] = {
  341. 0xFF, 0xFF, // -1
  342. 0x00, 0x80 // -32768
  343. };
  344. UA_ByteString src = { 4, data };
  345. // when
  346. UA_Int16 val_ff_ff, val_00_80;
  347. UA_StatusCode retval = UA_Int16_decodeBinary(&src, &pos, &val_ff_ff);
  348. retval |= UA_Int16_decodeBinary(&src, &pos, &val_00_80);
  349. // then
  350. ck_assert_int_eq(retval, UA_STATUSCODE_GOOD);
  351. ck_assert_int_eq(val_ff_ff, -1);
  352. ck_assert_int_eq(val_00_80, -32768);
  353. }
  354. END_TEST
  355. START_TEST(UA_UInt16_decodeShallNotRespectSign) {
  356. // given
  357. size_t pos = 0;
  358. UA_Byte data[] = {
  359. 0xFF, 0xFF, // (2^16)-1
  360. 0x00, 0x80 // (2^15)
  361. };
  362. UA_ByteString src = { 4, data };
  363. // when
  364. UA_UInt16 val_ff_ff, val_00_80;
  365. UA_StatusCode retval = UA_UInt16_decodeBinary(&src, &pos, &val_ff_ff);
  366. retval |= UA_UInt16_decodeBinary(&src, &pos, &val_00_80);
  367. // then
  368. ck_assert_int_eq(retval, UA_STATUSCODE_GOOD);
  369. ck_assert_int_eq(pos, 4);
  370. ck_assert_uint_eq(val_ff_ff, (0x01 << 16)-1);
  371. ck_assert_uint_eq(val_00_80, (0x01 << 15));
  372. }
  373. END_TEST
  374. START_TEST(UA_Int32_decodeShallAssumeLittleEndian) {
  375. // given
  376. size_t pos = 0;
  377. UA_Byte data[] = {
  378. 0x01, 0x00, 0x00, 0x00, // 1
  379. 0x00, 0x01, 0x00, 0x00 // 256
  380. };
  381. UA_ByteString src = { 8, data };
  382. // when
  383. UA_Int32 val_01_00, val_00_01;
  384. UA_StatusCode retval = UA_Int32_decodeBinary(&src, &pos, &val_01_00);
  385. retval |= UA_Int32_decodeBinary(&src, &pos, &val_00_01);
  386. // then
  387. ck_assert_int_eq(retval, UA_STATUSCODE_GOOD);
  388. ck_assert_int_eq(val_01_00, 1);
  389. ck_assert_int_eq(val_00_01, 256);
  390. ck_assert_int_eq(pos, 8);
  391. }
  392. END_TEST
  393. START_TEST(UA_Int32_decodeShallRespectSign) {
  394. // given
  395. size_t pos = 0;
  396. UA_Byte data[] = {
  397. 0xFF, 0xFF, 0xFF, 0xFF, // -1
  398. 0x00, 0x80, 0xFF, 0xFF // -32768
  399. };
  400. UA_ByteString src = { 8, data };
  401. // when
  402. UA_Int32 val_ff_ff, val_00_80;
  403. UA_StatusCode retval = UA_Int32_decodeBinary(&src, &pos, &val_ff_ff);
  404. retval |= UA_Int32_decodeBinary(&src, &pos, &val_00_80);
  405. // then
  406. ck_assert_int_eq(retval, UA_STATUSCODE_GOOD);
  407. ck_assert_int_eq(val_ff_ff, -1);
  408. ck_assert_int_eq(val_00_80, -32768);
  409. }
  410. END_TEST
  411. START_TEST(UA_UInt32_decodeShallNotRespectSign) {
  412. // given
  413. size_t pos = 0;
  414. UA_Byte data[] = {
  415. 0xFF, 0xFF, 0xFF, 0xFF, // (2^32)-1
  416. 0x00, 0x00, 0x00, 0x80 // (2^31)
  417. };
  418. UA_ByteString src = { 8, data };
  419. // when
  420. UA_UInt32 val_ff_ff, val_00_80;
  421. UA_StatusCode retval = UA_UInt32_decodeBinary(&src, &pos, &val_ff_ff);
  422. retval |= UA_UInt32_decodeBinary(&src, &pos, &val_00_80);
  423. // then
  424. ck_assert_int_eq(retval, UA_STATUSCODE_GOOD);
  425. ck_assert_int_eq(pos, 8);
  426. ck_assert_uint_eq(val_ff_ff, (UA_UInt32)( (0x01LL << 32 ) - 1 ));
  427. ck_assert_uint_eq(val_00_80, (UA_UInt32)(0x01 << 31));
  428. }
  429. END_TEST
  430. START_TEST(UA_UInt64_decodeShallNotRespectSign) {
  431. // given
  432. UA_ByteString rawMessage;
  433. UA_UInt64 expectedVal = 0xFF;
  434. expectedVal = expectedVal << 56;
  435. UA_Byte mem[8] = { 00, 00, 00, 00, 0x00, 0x00, 0x00, 0xFF };
  436. rawMessage.data = mem;
  437. rawMessage.length = 8;
  438. size_t pos = 0;
  439. UA_UInt64 val;
  440. // when
  441. UA_UInt64_decodeBinary(&rawMessage, &pos, &val);
  442. // then
  443. ck_assert_uint_eq(val, expectedVal);
  444. }
  445. END_TEST
  446. START_TEST(UA_Int64_decodeShallRespectSign) {
  447. // given
  448. UA_ByteString rawMessage;
  449. UA_Int64 expectedVal = 0xFF;
  450. expectedVal = expectedVal << 56;
  451. UA_Byte mem[8] = { 00, 00, 00, 00, 0x00, 0x00, 0x00, 0xFF };
  452. rawMessage.data = mem;
  453. rawMessage.length = 8;
  454. size_t pos = 0;
  455. UA_Int64 val;
  456. // when
  457. UA_Int64_decodeBinary(&rawMessage, &pos, &val);
  458. //then
  459. ck_assert_uint_eq(val, expectedVal);
  460. }
  461. END_TEST
  462. START_TEST(UA_Float_decodeShallWorkOnExample) {
  463. // given
  464. size_t pos = 0;
  465. UA_Byte data[] = { 0x00, 0x00, 0xD0, 0xC0 }; // -6.5
  466. UA_ByteString src = { 4, data };
  467. UA_Float dst;
  468. // when
  469. UA_StatusCode retval = UA_Float_decodeBinary(&src, &pos, &dst);
  470. // then
  471. ck_assert_int_eq(retval, UA_STATUSCODE_GOOD);
  472. ck_assert_int_eq(pos, 4);
  473. ck_assert(-6.5000001 < dst);
  474. ck_assert(dst < -6.49999999999);
  475. }
  476. END_TEST
  477. START_TEST(UA_Double_decodeShallGiveOne) {
  478. // given
  479. size_t pos = 0;
  480. UA_Byte data[] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xF0, 0x3F }; // 1
  481. UA_ByteString src = { 8, data }; // 1
  482. UA_Double dst;
  483. // when
  484. UA_StatusCode retval = UA_Double_decodeBinary(&src, &pos, &dst);
  485. // then
  486. ck_assert_int_eq(retval, UA_STATUSCODE_GOOD);
  487. ck_assert_int_eq(pos, 8);
  488. ck_assert(0.9999999 < dst);
  489. ck_assert(dst < 1.00000000001);
  490. }
  491. END_TEST
  492. START_TEST(UA_Double_decodeShallGiveZero) {
  493. // given
  494. size_t pos = 0;
  495. UA_Byte data[] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
  496. UA_ByteString src = { 8, data }; // 1
  497. UA_Double dst;
  498. // when
  499. UA_StatusCode retval = UA_Double_decodeBinary(&src, &pos, &dst);
  500. // then
  501. ck_assert_int_eq(retval, UA_STATUSCODE_GOOD);
  502. ck_assert_int_eq(pos, 8);
  503. ck_assert(-0.00000001 < dst);
  504. ck_assert(dst < 0.000000001);
  505. }
  506. END_TEST
  507. START_TEST(UA_Double_decodeShallGiveMinusTwo) {
  508. // given
  509. size_t pos = 0;
  510. UA_Byte data[] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xC0 }; // -2
  511. UA_ByteString src = { 8, data };
  512. UA_Double dst;
  513. // when
  514. UA_StatusCode retval = UA_Double_decodeBinary(&src, &pos, &dst);
  515. // then
  516. ck_assert_int_eq(retval, UA_STATUSCODE_GOOD);
  517. ck_assert_int_eq(pos, 8);
  518. ck_assert(-1.9999999 > dst);
  519. ck_assert(dst > -2.00000000001);
  520. }
  521. END_TEST
  522. START_TEST(UA_Double_decodeShallGive2147483648) {
  523. // given
  524. size_t pos = 0;
  525. UA_Byte data[] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xE0, 0x41 }; //2147483648
  526. UA_ByteString src = { 8, data }; // 1
  527. UA_Double dst;
  528. // when
  529. UA_StatusCode retval = UA_Double_decodeBinary(&src, &pos, &dst);
  530. // then
  531. ck_assert_int_eq(retval, UA_STATUSCODE_GOOD);
  532. ck_assert_int_eq(pos, 8);
  533. ck_assert(2147483647.9999999 <= dst);
  534. ck_assert(dst <= 2147483648.00000001);
  535. }
  536. END_TEST
  537. START_TEST(UA_String_decodeShallAllocateMemoryAndCopyString) {
  538. // given
  539. size_t pos = 0;
  540. UA_Byte data[] =
  541. { 0x08, 0x00, 0x00, 0x00, 'A', 'C', 'P', 'L', 'T', ' ', 'U', 'A', 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
  542. UA_ByteString src = { 16, data };
  543. UA_String dst;
  544. // when
  545. UA_StatusCode retval = UA_String_decodeBinary(&src, &pos, &dst);
  546. // then
  547. ck_assert_int_eq(retval, UA_STATUSCODE_GOOD);
  548. ck_assert_int_eq(dst.length, 8);
  549. ck_assert_int_eq(dst.data[3], 'L');
  550. // finally
  551. UA_String_deleteMembers(&dst);
  552. }
  553. END_TEST
  554. START_TEST(UA_String_decodeWithNegativeSizeShallNotAllocateMemoryAndNullPtr) {
  555. // given
  556. size_t pos = 0;
  557. UA_Byte data[] =
  558. { 0xFF, 0xFF, 0xFF, 0xFF, 'A', 'C', 'P', 'L', 'T', ' ', 'U', 'A', 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
  559. UA_ByteString src = { 16, data };
  560. UA_String dst;
  561. // when
  562. UA_StatusCode retval = UA_String_decodeBinary(&src, &pos, &dst);
  563. // then
  564. ck_assert_int_eq(retval, UA_STATUSCODE_GOOD);
  565. ck_assert_int_eq(dst.length, -1);
  566. ck_assert_ptr_eq(dst.data, UA_NULL);
  567. }
  568. END_TEST
  569. START_TEST(UA_String_decodeWithZeroSizeShallNotAllocateMemoryAndNullPtr) {
  570. // given
  571. size_t pos = 0;
  572. UA_Byte data[] =
  573. { 0x00, 0x00, 0x00, 0x00, 'A', 'C', 'P', 'L', 'T', ' ', 'U', 'A', 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
  574. UA_ByteString src = { 17, data };
  575. UA_String dst;
  576. // when
  577. UA_StatusCode retval = UA_String_decodeBinary(&src, &pos, &dst);
  578. // then
  579. ck_assert_int_eq(retval, UA_STATUSCODE_GOOD);
  580. ck_assert_int_eq(dst.length, 0);
  581. ck_assert_ptr_eq(dst.data, UA_NULL);
  582. }
  583. END_TEST
  584. START_TEST(UA_NodeId_decodeTwoByteShallReadTwoBytesAndSetNamespaceToZero) {
  585. // given
  586. size_t pos = 0;
  587. UA_Byte data[] = { 0 /* UA_NODEIDTYPE_TWOBYTE */, 0x10 };
  588. UA_ByteString src = { 2, data };
  589. UA_NodeId dst;
  590. // when
  591. UA_StatusCode retval = UA_NodeId_decodeBinary(&src, &pos, &dst);
  592. // then
  593. ck_assert_int_eq(retval, UA_STATUSCODE_GOOD);
  594. ck_assert_int_eq(pos, 2);
  595. ck_assert_int_eq(dst.identifierType, UA_NODEIDTYPE_NUMERIC);
  596. ck_assert_int_eq(dst.identifier.numeric, 16);
  597. ck_assert_int_eq(dst.namespaceIndex, 0);
  598. }
  599. END_TEST
  600. START_TEST(UA_NodeId_decodeFourByteShallReadFourBytesAndRespectNamespace) {
  601. // given
  602. size_t pos = 0;
  603. UA_Byte data[] = { 1 /* UA_NODEIDTYPE_FOURBYTE */, 0x01, 0x00, 0x01 };
  604. UA_ByteString src = { 4, data };
  605. UA_NodeId dst;
  606. // when
  607. UA_StatusCode retval = UA_NodeId_decodeBinary(&src, &pos, &dst);
  608. // then
  609. ck_assert_int_eq(retval, UA_STATUSCODE_GOOD);
  610. ck_assert_int_eq(pos, 4);
  611. ck_assert_int_eq(dst.identifierType, UA_NODEIDTYPE_NUMERIC);
  612. ck_assert_int_eq(dst.identifier.numeric, 256);
  613. ck_assert_int_eq(dst.namespaceIndex, 1);
  614. }
  615. END_TEST
  616. START_TEST(UA_NodeId_decodeStringShallAllocateMemory) {
  617. // given
  618. size_t pos = 0;
  619. UA_Byte data[] = { UA_NODEIDTYPE_STRING, 0x01, 0x00, 0x03, 0x00, 0x00, 0x00, 'P', 'L', 'T' };
  620. UA_ByteString src = { 10, data };
  621. UA_NodeId dst;
  622. // when
  623. UA_StatusCode retval = UA_NodeId_decodeBinary(&src, &pos, &dst);
  624. // then
  625. ck_assert_int_eq(retval, UA_STATUSCODE_GOOD);
  626. ck_assert_int_eq(pos, 10);
  627. ck_assert_int_eq(dst.identifierType, UA_NODEIDTYPE_STRING);
  628. ck_assert_int_eq(dst.namespaceIndex, 1);
  629. ck_assert_int_eq(dst.identifier.string.length, 3);
  630. ck_assert_int_eq(dst.identifier.string.data[1], 'L');
  631. // finally
  632. UA_NodeId_deleteMembers(&dst);
  633. }
  634. END_TEST
  635. START_TEST(UA_Variant_decodeWithOutArrayFlagSetShallSetVTAndAllocateMemoryForArray) {
  636. // given
  637. size_t pos = 0;
  638. UA_Byte data[] = { UA_TYPES[UA_TYPES_INT32].typeId.identifier.numeric, 0xFF, 0x00, 0x00, 0x00 };
  639. UA_ByteString src = { 5, data };
  640. UA_Variant dst;
  641. // when
  642. UA_StatusCode retval = UA_Variant_decodeBinary(&src, &pos, &dst);
  643. // then
  644. ck_assert_int_eq(retval, UA_STATUSCODE_GOOD);
  645. ck_assert_int_eq(pos, 5);
  646. //ck_assert_ptr_eq((const void *)dst.type, (const void *)&UA_TYPES[UA_TYPES_INT32]); //does not compile in gcc 4.6
  647. ck_assert_int_eq((uintptr_t)dst.type, (uintptr_t)&UA_TYPES[UA_TYPES_INT32]);
  648. ck_assert_int_eq(dst.arrayLength, -1);
  649. ck_assert_int_eq(*(UA_Int32 *)dst.data, 255);
  650. // finally
  651. UA_Variant_deleteMembers(&dst);
  652. }
  653. END_TEST
  654. START_TEST(UA_Variant_decodeWithArrayFlagSetShallSetVTAndAllocateMemoryForArray) {
  655. // given
  656. size_t pos = 0;
  657. UA_Byte data[] = { UA_TYPES[UA_TYPES_INT32].typeId.identifier.numeric | UA_VARIANT_ENCODINGMASKTYPE_ARRAY,
  658. 0x02, 0x00, 0x00, 0x00, 0xFF, 0x00, 0x00, 0x00, 0xFF, 0xFF,
  659. 0xFF, 0xFF };
  660. UA_ByteString src = { 13, data };
  661. UA_Variant dst;
  662. // when
  663. UA_StatusCode retval = UA_Variant_decodeBinary(&src, &pos, &dst);
  664. // then
  665. ck_assert_int_eq(retval, UA_STATUSCODE_GOOD);
  666. ck_assert_int_eq(pos, 1+4+2*4);
  667. //ck_assert_ptr_eq((const (void*))dst.type, (const void*)&UA_TYPES[UA_TYPES_INT32]); //does not compile in gcc 4.6
  668. ck_assert_int_eq((uintptr_t)dst.type,(uintptr_t)&UA_TYPES[UA_TYPES_INT32]);
  669. ck_assert_int_eq(dst.arrayLength, 2);
  670. ck_assert_int_eq(((UA_Int32 *)dst.data)[0], 255);
  671. ck_assert_int_eq(((UA_Int32 *)dst.data)[1], -1);
  672. // finally
  673. UA_Variant_deleteMembers(&dst);
  674. }
  675. END_TEST
  676. START_TEST(UA_Variant_decodeSingleExtensionObjectShallSetVTAndAllocateMemory){
  677. // given
  678. size_t pos = 0;
  679. UA_Variant dst;
  680. UA_NodeId tmpNodeId;
  681. UA_ByteString srcByteString;
  682. UA_NodeId_init(&tmpNodeId);
  683. tmpNodeId.identifier.numeric = 22;
  684. tmpNodeId.namespaceIndex = 2;
  685. tmpNodeId.identifierType = UA_NODEIDTYPE_NUMERIC;
  686. UA_ExtensionObject tmpExtensionObject;
  687. UA_ExtensionObject_init(&tmpExtensionObject);
  688. UA_ByteString_newMembers(&tmpExtensionObject.body,3);
  689. tmpExtensionObject.body.data[0]= 10;
  690. tmpExtensionObject.body.data[1]= 20;
  691. tmpExtensionObject.body.data[2]= 30;
  692. tmpExtensionObject.encoding = UA_EXTENSIONOBJECT_ENCODINGMASK_BODYISBYTESTRING;
  693. tmpExtensionObject.typeId = tmpNodeId;
  694. UA_Variant tmpVariant;
  695. UA_Variant_init(&tmpVariant);
  696. tmpVariant.arrayDimensions = UA_NULL;
  697. tmpVariant.arrayDimensionsSize = -1;
  698. tmpVariant.arrayLength = -1;
  699. tmpVariant.storageType = UA_VARIANT_DATA_NODELETE;
  700. tmpVariant.type = &UA_TYPES[UA_TYPES_EXTENSIONOBJECT];
  701. tmpVariant.data = &tmpExtensionObject;
  702. UA_ByteString_newMembers(&srcByteString,200);
  703. pos = 0;
  704. UA_Variant_encodeBinary(&tmpVariant,&srcByteString,&pos);
  705. // when
  706. pos = 0;
  707. UA_StatusCode retval = UA_Variant_decodeBinary(&srcByteString, &pos, &dst);
  708. // then
  709. ck_assert_int_eq(retval, UA_STATUSCODE_GOOD);
  710. ck_assert_int_eq((uintptr_t)dst.type, (uintptr_t)&UA_TYPES[UA_TYPES_EXTENSIONOBJECT]);
  711. ck_assert_int_eq(dst.arrayLength, -1);
  712. ck_assert_int_eq(((UA_ExtensionObject *)dst.data)->body.data[0], 10);
  713. ck_assert_int_eq(((UA_ExtensionObject *)dst.data)->body.data[1], 20);
  714. ck_assert_int_eq(((UA_ExtensionObject *)dst.data)->body.data[2], 30);
  715. ck_assert_int_eq(((UA_ExtensionObject *)dst.data)->body.length, 3);
  716. // finally
  717. UA_Variant_deleteMembers(&dst);
  718. UA_ByteString_deleteMembers(&srcByteString);
  719. UA_ExtensionObject_deleteMembers(&tmpExtensionObject);
  720. }
  721. END_TEST
  722. START_TEST(UA_Variant_decodeWithOutDeleteMembersShallFailInCheckMem) {
  723. // given
  724. size_t pos = 0;
  725. UA_Byte data[] = { UA_TYPES[UA_TYPES_INT32].typeId.identifier.numeric | UA_VARIANT_ENCODINGMASKTYPE_ARRAY,
  726. 0x02, 0x00, 0x00, 0x00, 0xFF, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF };
  727. UA_ByteString src = { 13, data };
  728. UA_Variant dst;
  729. // when
  730. UA_StatusCode retval = UA_Variant_decodeBinary(&src, &pos, &dst);
  731. // then
  732. ck_assert_int_eq(retval, UA_STATUSCODE_GOOD);
  733. // finally
  734. UA_Variant_deleteMembers(&dst);
  735. }
  736. END_TEST
  737. START_TEST(UA_Variant_decodeWithTooSmallSourceShallReturnWithError) {
  738. // given
  739. size_t pos = 0;
  740. UA_Byte data[] = { UA_TYPES[UA_TYPES_INT32].typeId.identifier.numeric | UA_VARIANT_ENCODINGMASKTYPE_ARRAY,
  741. 0x02, 0x00, 0x00, 0x00, 0xFF, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF };
  742. UA_ByteString src = { 4, data };
  743. UA_Variant dst;
  744. // when
  745. UA_StatusCode retval = UA_Variant_decodeBinary(&src, &pos, &dst);
  746. // then
  747. ck_assert_int_ne(retval, UA_STATUSCODE_GOOD);
  748. // finally
  749. UA_Variant_deleteMembers(&dst);
  750. }
  751. END_TEST
  752. START_TEST(UA_Byte_encode_test) {
  753. // given
  754. UA_Byte src;
  755. UA_Byte data[] = { 0x00, 0xFF };
  756. UA_ByteString dst = { 2, data };
  757. UA_Int32 retval = 0;
  758. size_t pos = 0;
  759. ck_assert_uint_eq(dst.data[1], 0xFF);
  760. src = 8;
  761. retval = UA_Byte_encodeBinary(&src, &dst, &pos);
  762. ck_assert_uint_eq(dst.data[0], 0x08);
  763. ck_assert_uint_eq(dst.data[1], 0xFF);
  764. ck_assert_int_eq(pos, 1);
  765. ck_assert_int_eq(retval, UA_STATUSCODE_GOOD);
  766. // Test2
  767. // given
  768. src = 0xFF;
  769. dst.data[1] = 0x00;
  770. pos = 0;
  771. retval = UA_Byte_encodeBinary(&src, &dst, &pos);
  772. ck_assert_int_eq(dst.data[0], 0xFF);
  773. ck_assert_int_eq(dst.data[1], 0x00);
  774. ck_assert_int_eq(pos, 1);
  775. ck_assert_int_eq(retval, UA_STATUSCODE_GOOD);
  776. }
  777. END_TEST
  778. START_TEST(UA_UInt16_encodeNegativeShallEncodeLittleEndian) {
  779. // given
  780. UA_UInt16 src;
  781. UA_Byte data[] = { 0x55, 0x55, 0x55, 0x55 };
  782. UA_ByteString dst = { 4, data };
  783. UA_StatusCode retval = 0;
  784. size_t pos = 0;
  785. // when test 1
  786. src = -1;
  787. retval = UA_UInt16_encodeBinary(&src, &dst, &pos);
  788. // then test 1
  789. ck_assert_int_eq(pos, 2);
  790. ck_assert_int_eq(dst.data[0], 0xFF);
  791. ck_assert_int_eq(dst.data[1], 0xFF);
  792. ck_assert_int_eq(retval, UA_STATUSCODE_GOOD);
  793. // when test 2
  794. src = -32768;
  795. retval = UA_UInt16_encodeBinary(&src, &dst, &pos);
  796. // then test 2
  797. ck_assert_int_eq(pos, 4);
  798. ck_assert_int_eq(dst.data[2], 0x00);
  799. ck_assert_int_eq(dst.data[3], 0x80);
  800. ck_assert_int_eq(retval, UA_STATUSCODE_GOOD);
  801. }
  802. END_TEST
  803. START_TEST(UA_UInt16_encodeShallEncodeLittleEndian) {
  804. // given
  805. UA_UInt16 src;
  806. UA_Byte data[] = { 0x55, 0x55,
  807. 0x55, 0x55 };
  808. UA_ByteString dst = { 4, data };
  809. UA_StatusCode retval = 0;
  810. size_t pos = 0;
  811. // when test 1
  812. src = 0;
  813. retval = UA_UInt16_encodeBinary(&src, &dst, &pos);
  814. // then test 1
  815. ck_assert_int_eq(pos, 2);
  816. ck_assert_int_eq(dst.data[0], 0x00);
  817. ck_assert_int_eq(dst.data[1], 0x00);
  818. ck_assert_int_eq(retval, UA_STATUSCODE_GOOD);
  819. // when test 2
  820. src = 32767;
  821. retval = UA_UInt16_encodeBinary(&src, &dst, &pos);
  822. // then test 2
  823. ck_assert_int_eq(pos, 4);
  824. ck_assert_int_eq(dst.data[2], 0xFF);
  825. ck_assert_int_eq(dst.data[3], 0x7F);
  826. ck_assert_int_eq(retval, UA_STATUSCODE_GOOD);
  827. }
  828. END_TEST
  829. START_TEST(UA_UInt32_encodeShallEncodeLittleEndian) {
  830. // given
  831. UA_UInt32 src;
  832. UA_Byte data[] = { 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55 };
  833. UA_ByteString dst = { 8, data };
  834. UA_StatusCode retval = 0;
  835. size_t pos = 0;
  836. // when test 1
  837. src = -1;
  838. retval = UA_UInt32_encodeBinary(&src, &dst, &pos);
  839. // then test 1
  840. ck_assert_int_eq(pos, 4);
  841. ck_assert_int_eq(dst.data[0], 0xFF);
  842. ck_assert_int_eq(dst.data[1], 0xFF);
  843. ck_assert_int_eq(dst.data[2], 0xFF);
  844. ck_assert_int_eq(dst.data[3], 0xFF);
  845. ck_assert_int_eq(retval, UA_STATUSCODE_GOOD);
  846. // when test 2
  847. src = 0x0101FF00;
  848. retval = UA_UInt32_encodeBinary(&src, &dst, &pos);
  849. // then test 2
  850. ck_assert_int_eq(pos, 8);
  851. ck_assert_int_eq(dst.data[4], 0x00);
  852. ck_assert_int_eq(dst.data[5], 0xFF);
  853. ck_assert_int_eq(dst.data[6], 0x01);
  854. ck_assert_int_eq(dst.data[7], 0x01);
  855. ck_assert_int_eq(retval, UA_STATUSCODE_GOOD);
  856. }
  857. END_TEST
  858. START_TEST(UA_Int32_encodeShallEncodeLittleEndian) {
  859. // given
  860. UA_Int32 src;
  861. UA_Byte data[] = { 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55 };
  862. UA_ByteString dst = { 8, data };
  863. UA_Int32 retval = 0;
  864. size_t pos = 0;
  865. // when test 1
  866. src = 1;
  867. retval = UA_Int32_encodeBinary(&src, &dst, &pos);
  868. // then test 1
  869. ck_assert_int_eq(pos, 4);
  870. ck_assert_int_eq(dst.data[0], 0x01);
  871. ck_assert_int_eq(dst.data[1], 0x00);
  872. ck_assert_int_eq(dst.data[2], 0x00);
  873. ck_assert_int_eq(dst.data[3], 0x00);
  874. ck_assert_int_eq(retval, UA_STATUSCODE_GOOD);
  875. // when test 2
  876. src = 0x7FFFFFFF;
  877. retval = UA_Int32_encodeBinary(&src, &dst, &pos);
  878. // then test 2
  879. ck_assert_int_eq(pos, 8);
  880. ck_assert_int_eq(dst.data[4], 0xFF);
  881. ck_assert_int_eq(dst.data[5], 0xFF);
  882. ck_assert_int_eq(dst.data[6], 0xFF);
  883. ck_assert_int_eq(dst.data[7], 0x7F);
  884. ck_assert_int_eq(retval, UA_STATUSCODE_GOOD);
  885. }
  886. END_TEST
  887. START_TEST(UA_Int32_encodeNegativeShallEncodeLittleEndian) {
  888. // given
  889. UA_Int32 src;
  890. UA_Byte data[] = { 0x55, 0x55, 0x55, 0x55,
  891. 0x55, 0x55, 0x55, 0x55 };
  892. UA_ByteString dst = { 8, data };
  893. UA_Int32 retval = 0;
  894. size_t pos = 0;
  895. // when test 1
  896. src = -1;
  897. retval = UA_Int32_encodeBinary(&src, &dst, &pos);
  898. // then test 1
  899. ck_assert_int_eq(pos, 4);
  900. ck_assert_int_eq(dst.data[0], 0xFF);
  901. ck_assert_int_eq(dst.data[1], 0xFF);
  902. ck_assert_int_eq(dst.data[2], 0xFF);
  903. ck_assert_int_eq(dst.data[3], 0xFF);
  904. ck_assert_int_eq(retval, UA_STATUSCODE_GOOD);
  905. }
  906. END_TEST
  907. START_TEST(UA_UInt64_encodeShallWorkOnExample) {
  908. // given
  909. UA_UInt64 src;
  910. UA_Byte data[] = { 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55,
  911. 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55 };
  912. UA_ByteString dst = { 16, data };
  913. UA_StatusCode retval = 0;
  914. size_t pos = 0;
  915. // when test 1
  916. src = -1;
  917. retval = UA_UInt64_encodeBinary(&src, &dst, &pos);
  918. // then test 1
  919. ck_assert_int_eq(pos, 8);
  920. ck_assert_int_eq(dst.data[0], 0xFF);
  921. ck_assert_int_eq(dst.data[1], 0xFF);
  922. ck_assert_int_eq(dst.data[2], 0xFF);
  923. ck_assert_int_eq(dst.data[3], 0xFF);
  924. ck_assert_int_eq(dst.data[4], 0xFF);
  925. ck_assert_int_eq(dst.data[5], 0xFF);
  926. ck_assert_int_eq(dst.data[6], 0xFF);
  927. ck_assert_int_eq(dst.data[7], 0xFF);
  928. ck_assert_int_eq(retval, UA_STATUSCODE_GOOD);
  929. // when test 2
  930. src = 0x7F0033AA44EE6611;
  931. retval = UA_UInt64_encodeBinary(&src, &dst, &pos);
  932. // then test 2
  933. ck_assert_int_eq(pos, 16);
  934. ck_assert_int_eq(dst.data[8], 0x11);
  935. ck_assert_int_eq(dst.data[9], 0x66);
  936. ck_assert_int_eq(dst.data[10], 0xEE);
  937. ck_assert_int_eq(dst.data[11], 0x44);
  938. ck_assert_int_eq(dst.data[12], 0xAA);
  939. ck_assert_int_eq(dst.data[13], 0x33);
  940. ck_assert_int_eq(dst.data[14], 0x00);
  941. ck_assert_int_eq(dst.data[15], 0x7F);
  942. ck_assert_int_eq(retval, UA_STATUSCODE_GOOD);
  943. }
  944. END_TEST
  945. START_TEST(UA_Int64_encodeShallEncodeLittleEndian) {
  946. // given
  947. UA_Int64 src;
  948. UA_Byte data[] = { 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55,
  949. 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55 };
  950. UA_ByteString dst = { 16, data };
  951. UA_Int32 retval = 0;
  952. size_t pos = 0;
  953. // when test 1
  954. src = 0x7F0033AA44EE6611;
  955. retval = UA_Int64_encodeBinary(&src, &dst, &pos);
  956. // then test 1
  957. ck_assert_int_eq(pos, 8);
  958. ck_assert_int_eq(dst.data[0], 0x11);
  959. ck_assert_int_eq(dst.data[1], 0x66);
  960. ck_assert_int_eq(dst.data[2], 0xEE);
  961. ck_assert_int_eq(dst.data[3], 0x44);
  962. ck_assert_int_eq(dst.data[4], 0xAA);
  963. ck_assert_int_eq(dst.data[5], 0x33);
  964. ck_assert_int_eq(dst.data[6], 0x00);
  965. ck_assert_int_eq(dst.data[7], 0x7F);
  966. ck_assert_int_eq(retval, UA_STATUSCODE_GOOD);
  967. }
  968. END_TEST
  969. START_TEST(UA_Int64_encodeNegativeShallEncodeLittleEndian) {
  970. // given
  971. UA_Int64 src;
  972. UA_Byte data[] = { 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55,
  973. 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55 };
  974. UA_ByteString dst = { 16, data };
  975. UA_Int32 retval = 0;
  976. size_t pos = 0;
  977. // when test 1
  978. src = -1;
  979. retval = UA_Int64_encodeBinary(&src, &dst, &pos);
  980. // then test 1
  981. ck_assert_int_eq(pos, 8);
  982. ck_assert_int_eq(dst.data[0], 0xFF);
  983. ck_assert_int_eq(dst.data[1], 0xFF);
  984. ck_assert_int_eq(dst.data[2], 0xFF);
  985. ck_assert_int_eq(dst.data[3], 0xFF);
  986. ck_assert_int_eq(dst.data[4], 0xFF);
  987. ck_assert_int_eq(dst.data[5], 0xFF);
  988. ck_assert_int_eq(dst.data[6], 0xFF);
  989. ck_assert_int_eq(dst.data[7], 0xFF);
  990. ck_assert_int_eq(retval, UA_STATUSCODE_GOOD);
  991. }
  992. END_TEST
  993. START_TEST(UA_Float_encodeShallWorkOnExample) {
  994. // given
  995. UA_Float src;
  996. UA_Byte data[] = { 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55,
  997. 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55 };
  998. UA_ByteString dst = { 16, data };
  999. UA_Int32 retval = 0;
  1000. size_t pos = 0;
  1001. // when test 1
  1002. src = -6.5;
  1003. retval = UA_Float_encodeBinary(&src, &dst, &pos);
  1004. // then test 1
  1005. ck_assert_int_eq(pos, 4);
  1006. ck_assert_int_eq(dst.data[2], 0xD0);
  1007. ck_assert_int_eq(dst.data[3], 0xC0);
  1008. ck_assert_int_eq(retval, UA_STATUSCODE_GOOD);
  1009. }
  1010. END_TEST
  1011. START_TEST(UA_Double_encodeShallWorkOnExample)
  1012. {
  1013. // given
  1014. UA_Double src;
  1015. UA_Byte data[] = { 0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,
  1016. 0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55
  1017. };
  1018. UA_ByteString dst = {16,data};
  1019. UA_Int32 retval;
  1020. size_t pos = 0;
  1021. // when test 1
  1022. src = -6.5;
  1023. retval = UA_Double_encodeBinary(&src, &dst, &pos);
  1024. // then test 1
  1025. ck_assert_int_eq(pos, 8);
  1026. ck_assert_int_eq(dst.data[6], 0x1A);
  1027. ck_assert_int_eq(dst.data[7], 0xC0);
  1028. ck_assert_int_eq(retval, UA_STATUSCODE_GOOD);
  1029. }
  1030. END_TEST
  1031. START_TEST(UA_String_encodeShallWorkOnExample) {
  1032. // given
  1033. UA_String src;
  1034. src.length = 11;
  1035. UA_Byte mem[11] = "ACPLT OPCUA";
  1036. src.data = mem;
  1037. UA_Byte data[] = { 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55,
  1038. 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55,
  1039. 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55 };
  1040. UA_ByteString dst = { 24, data };
  1041. UA_Int32 retval = 0;
  1042. size_t pos = 0;
  1043. // when
  1044. retval = UA_String_encodeBinary(&src, &dst, &pos);
  1045. // then
  1046. ck_assert_int_eq(pos, sizeof(UA_Int32)+11);
  1047. ck_assert_int_eq(dst.data[0], 11);
  1048. ck_assert_int_eq(dst.data[sizeof(UA_Int32)+0], 'A');
  1049. ck_assert_int_eq(dst.data[sizeof(UA_Int32)+1], 'C');
  1050. ck_assert_int_eq(dst.data[sizeof(UA_Int32)+2], 'P');
  1051. ck_assert_int_eq(dst.data[sizeof(UA_Int32)+3], 'L');
  1052. ck_assert_int_eq(dst.data[sizeof(UA_Int32)+4], 'T');
  1053. ck_assert_int_eq(dst.data[sizeof(UA_Int32)+5], 0x20); //Space
  1054. ck_assert_int_eq(retval, UA_STATUSCODE_GOOD);
  1055. }
  1056. END_TEST
  1057. START_TEST(UA_DataValue_encodeShallWorkOnExampleWithoutVariant) {
  1058. // given
  1059. UA_DataValue src;
  1060. UA_DataValue_init(&src);
  1061. src.serverTimestamp = 80;
  1062. src.hasServerTimestamp = UA_TRUE;
  1063. UA_Byte data[] = { 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55,
  1064. 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55,
  1065. 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55 };
  1066. UA_ByteString dst = { 24, data };
  1067. UA_Int32 retval = 0;
  1068. size_t pos = 0;
  1069. // when
  1070. retval = UA_DataValue_encodeBinary(&src, &dst, &pos);
  1071. // then
  1072. ck_assert_int_eq(pos, 9); // represents the length
  1073. ck_assert_int_eq(dst.data[0], 0x08); // encodingMask
  1074. ck_assert_int_eq(dst.data[1], 80); // 8 Byte serverTimestamp
  1075. ck_assert_int_eq(dst.data[2], 0);
  1076. ck_assert_int_eq(dst.data[3], 0);
  1077. ck_assert_int_eq(dst.data[4], 0);
  1078. ck_assert_int_eq(dst.data[5], 0);
  1079. ck_assert_int_eq(dst.data[6], 0);
  1080. ck_assert_int_eq(dst.data[7], 0);
  1081. ck_assert_int_eq(dst.data[8], 0);
  1082. ck_assert_int_eq(retval, UA_STATUSCODE_GOOD);
  1083. }
  1084. END_TEST
  1085. START_TEST(UA_DataValue_encodeShallWorkOnExampleWithVariant) {
  1086. // given
  1087. UA_DataValue src;
  1088. UA_DataValue_init(&src);
  1089. src.serverTimestamp = 80;
  1090. src.hasValue = UA_TRUE;
  1091. src.hasServerTimestamp = UA_TRUE;
  1092. src.value.type = &UA_TYPES[UA_TYPES_INT32];
  1093. src.value.arrayLength = -1; // one element (encoded as not an array)
  1094. UA_Int32 vdata = 45;
  1095. src.value.data = (void *)&vdata;
  1096. UA_Byte data[] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  1097. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  1098. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
  1099. UA_ByteString dst = { 24, data };
  1100. UA_Int32 retval = 0;
  1101. size_t pos = 0;
  1102. // when
  1103. retval = UA_DataValue_encodeBinary(&src, &dst, &pos);
  1104. // then
  1105. ck_assert_int_eq(pos, 1+(1+4)+8); // represents the length
  1106. ck_assert_int_eq(dst.data[0], 0x08 | 0x01); // encodingMask
  1107. ck_assert_int_eq(dst.data[1], 0x06); // Variant's Encoding Mask - INT32
  1108. ck_assert_int_eq(dst.data[2], 45); // the single value
  1109. ck_assert_int_eq(dst.data[3], 0);
  1110. ck_assert_int_eq(dst.data[4], 0);
  1111. ck_assert_int_eq(dst.data[5], 0);
  1112. ck_assert_int_eq(dst.data[6], 80); // the server timestamp
  1113. ck_assert_int_eq(dst.data[7], 0);
  1114. ck_assert_int_eq(retval, UA_STATUSCODE_GOOD);
  1115. }
  1116. END_TEST
  1117. START_TEST(UA_DateTime_toStructShallWorkOnExample) {
  1118. // given
  1119. UA_DateTime src = 13974671891234567 + (11644473600 * 10000000); // ua counts since 1601, unix since 1970
  1120. //1397467189... is Mon, 14 Apr 2014 09:19:49 GMT
  1121. //...1234567 are the milli-, micro- and nanoseconds
  1122. UA_DateTimeStruct dst;
  1123. // when
  1124. dst = UA_DateTime_toStruct(src);
  1125. // then
  1126. ck_assert_int_eq(dst.nanoSec, 700);
  1127. ck_assert_int_eq(dst.microSec, 456);
  1128. ck_assert_int_eq(dst.milliSec, 123);
  1129. ck_assert_int_eq(dst.sec, 49);
  1130. ck_assert_int_eq(dst.min, 19);
  1131. ck_assert_int_eq(dst.hour, 9);
  1132. ck_assert_int_eq(dst.day, 14);
  1133. ck_assert_int_eq(dst.month, 4);
  1134. ck_assert_int_eq(dst.year, 2014);
  1135. }
  1136. END_TEST
  1137. START_TEST(UA_DateTime_toStringShallWorkOnExample) {
  1138. // given
  1139. UA_DateTime src = 13974671891234567 + (11644473600 * 10000000); // ua counts since 1601, unix since 1970
  1140. //1397467189... is Mon, 14 Apr 2014 09:19:49 GMT
  1141. //...1234567 are the milli-, micro- and nanoseconds
  1142. UA_String dst;
  1143. // when
  1144. UA_DateTime_toString(src, &dst);
  1145. // then
  1146. ck_assert_int_eq(dst.data[0], '0');
  1147. ck_assert_int_eq(dst.data[1], '4');
  1148. ck_assert_int_eq(dst.data[2], '/');
  1149. ck_assert_int_eq(dst.data[3], '1');
  1150. ck_assert_int_eq(dst.data[4], '4');
  1151. UA_String_deleteMembers(&dst);
  1152. }
  1153. END_TEST
  1154. START_TEST(UA_ExtensionObject_copyShallWorkOnExample) {
  1155. // given
  1156. UA_Byte data[3] = { 1, 2, 3 };
  1157. UA_ExtensionObject value, valueCopied;
  1158. UA_ExtensionObject_init(&value);
  1159. UA_ExtensionObject_init(&valueCopied);
  1160. value.typeId = UA_TYPES[UA_TYPES_BYTE].typeId;
  1161. value.encoding = UA_EXTENSIONOBJECT_ENCODINGMASK_NOBODYISENCODED;
  1162. value.encoding = UA_EXTENSIONOBJECT_ENCODINGMASK_BODYISBYTESTRING;
  1163. value.body.data = data;
  1164. value.body.length = 3;
  1165. //when
  1166. UA_ExtensionObject_copy(&value, &valueCopied);
  1167. for(UA_Int32 i = 0;i < 3;i++)
  1168. ck_assert_int_eq(valueCopied.body.data[i], value.body.data[i]);
  1169. ck_assert_int_eq(valueCopied.encoding, value.encoding);
  1170. ck_assert_int_eq(valueCopied.typeId.identifierType, value.typeId.identifierType);
  1171. ck_assert_int_eq(valueCopied.typeId.identifier.numeric, value.typeId.identifier.numeric);
  1172. //finally
  1173. value.body.data = UA_NULL; // we cannot free the static string
  1174. UA_ExtensionObject_deleteMembers(&value);
  1175. UA_ExtensionObject_deleteMembers(&valueCopied);
  1176. }
  1177. END_TEST
  1178. START_TEST(UA_Array_copyByteArrayShallWorkOnExample) {
  1179. //given
  1180. UA_String testString;
  1181. UA_Byte *dstArray;
  1182. UA_Int32 size = 5;
  1183. UA_Int32 i = 0;
  1184. testString.data = UA_malloc(size);
  1185. testString.data[0] = 'O';
  1186. testString.data[1] = 'P';
  1187. testString.data[2] = 'C';
  1188. testString.data[3] = 'U';
  1189. testString.data[4] = 'A';
  1190. testString.length = 5;
  1191. //when
  1192. UA_Array_copy((const void *)testString.data, (void **)&dstArray, &UA_TYPES[UA_TYPES_BYTE], 5);
  1193. //then
  1194. for(i = 0;i < size;i++)
  1195. ck_assert_int_eq(testString.data[i], dstArray[i]);
  1196. //finally
  1197. UA_String_deleteMembers(&testString);
  1198. UA_free((void *)dstArray);
  1199. }
  1200. END_TEST
  1201. START_TEST(UA_Array_copyUA_StringShallWorkOnExample) {
  1202. // given
  1203. UA_Int32 i, j;
  1204. UA_String *srcArray = UA_Array_new(&UA_TYPES[UA_TYPES_STRING], 3);
  1205. UA_String *dstArray;
  1206. srcArray[0] = UA_STRING_ALLOC("open");
  1207. srcArray[1] = UA_STRING_ALLOC("62541");
  1208. srcArray[2] = UA_STRING_ALLOC("opc ua");
  1209. //when
  1210. UA_Array_copy((const void *)srcArray, (void **)&dstArray, &UA_TYPES[UA_TYPES_STRING], 3);
  1211. //then
  1212. for(i = 0;i < 3;i++) {
  1213. for(j = 0;j < 3;j++)
  1214. ck_assert_int_eq(srcArray[i].data[j], dstArray[i].data[j]);
  1215. ck_assert_int_eq(srcArray[i].length, dstArray[i].length);
  1216. }
  1217. //finally
  1218. UA_Array_delete(srcArray, &UA_TYPES[UA_TYPES_STRING], 3);
  1219. UA_Array_delete(dstArray, &UA_TYPES[UA_TYPES_STRING], 3);
  1220. }
  1221. END_TEST
  1222. START_TEST(UA_DiagnosticInfo_copyShallWorkOnExample) {
  1223. //given
  1224. UA_DiagnosticInfo value, innerValue, copiedValue;
  1225. UA_String testString = (UA_String){5, (UA_Byte*)"OPCUA"};
  1226. UA_DiagnosticInfo_init(&value);
  1227. UA_DiagnosticInfo_init(&innerValue);
  1228. value.hasInnerDiagnosticInfo = UA_TRUE;
  1229. value.innerDiagnosticInfo = &innerValue;
  1230. value.hasAdditionalInfo = UA_TRUE;
  1231. value.additionalInfo = testString;
  1232. //when
  1233. UA_DiagnosticInfo_copy(&value, &copiedValue);
  1234. //then
  1235. for(UA_Int32 i = 0;i < testString.length;i++)
  1236. ck_assert_int_eq(copiedValue.additionalInfo.data[i], value.additionalInfo.data[i]);
  1237. ck_assert_int_eq(copiedValue.additionalInfo.length, value.additionalInfo.length);
  1238. ck_assert_int_eq(copiedValue.hasInnerDiagnosticInfo, value.hasInnerDiagnosticInfo);
  1239. ck_assert_int_eq(copiedValue.innerDiagnosticInfo->locale, value.innerDiagnosticInfo->locale);
  1240. ck_assert_int_eq(copiedValue.innerStatusCode, value.innerStatusCode);
  1241. ck_assert_int_eq(copiedValue.locale, value.locale);
  1242. ck_assert_int_eq(copiedValue.localizedText, value.localizedText);
  1243. ck_assert_int_eq(copiedValue.namespaceUri, value.namespaceUri);
  1244. ck_assert_int_eq(copiedValue.symbolicId, value.symbolicId);
  1245. //finally
  1246. value.additionalInfo.data = UA_NULL; // do not delete the static string
  1247. value.innerDiagnosticInfo = UA_NULL; // do not delete the static innerdiagnosticinfo
  1248. UA_DiagnosticInfo_deleteMembers(&value);
  1249. UA_DiagnosticInfo_deleteMembers(&copiedValue);
  1250. }
  1251. END_TEST
  1252. START_TEST(UA_ApplicationDescription_copyShallWorkOnExample) {
  1253. //given
  1254. UA_StatusCode retval = UA_STATUSCODE_GOOD;
  1255. UA_String appString = (UA_String){3, (UA_Byte*)"APP"};
  1256. UA_String discString = (UA_String){4, (UA_Byte*)"DISC"};
  1257. UA_String gateWayString = (UA_String){7, (UA_Byte*)"GATEWAY"};
  1258. UA_String srcArray[3];
  1259. srcArray[0] = (UA_String){ 6, (UA_Byte*)"__open" };
  1260. srcArray[1] = (UA_String){ 6, (UA_Byte*)"_62541" };
  1261. srcArray[2] = (UA_String){ 6, (UA_Byte*)"opc ua" };
  1262. UA_ApplicationDescription value, copiedValue;
  1263. UA_ApplicationDescription_init(&value);
  1264. value.applicationUri = appString;
  1265. value.discoveryProfileUri = discString;
  1266. value.gatewayServerUri = gateWayString;
  1267. value.discoveryUrlsSize = 3;
  1268. value.discoveryUrls = srcArray;
  1269. //when
  1270. retval = UA_ApplicationDescription_copy(&value, &copiedValue);
  1271. //then
  1272. ck_assert_int_eq(retval, UA_STATUSCODE_GOOD);
  1273. for(UA_Int32 i = 0;i < appString.length;i++)
  1274. ck_assert_int_eq(copiedValue.applicationUri.data[i], value.applicationUri.data[i]);
  1275. ck_assert_int_eq(copiedValue.applicationUri.length, value.applicationUri.length);
  1276. for(UA_Int32 i = 0;i < discString.length;i++)
  1277. ck_assert_int_eq(copiedValue.discoveryProfileUri.data[i], value.discoveryProfileUri.data[i]);
  1278. ck_assert_int_eq(copiedValue.discoveryProfileUri.length, value.discoveryProfileUri.length);
  1279. for(UA_Int32 i = 0;i < gateWayString.length;i++)
  1280. ck_assert_int_eq(copiedValue.gatewayServerUri.data[i], value.gatewayServerUri.data[i]);
  1281. ck_assert_int_eq(copiedValue.gatewayServerUri.length, value.gatewayServerUri.length);
  1282. //String Array Test
  1283. for(UA_Int32 i = 0;i < 3;i++) {
  1284. for(UA_Int32 j = 0;j < 6;j++)
  1285. ck_assert_int_eq(value.discoveryUrls[i].data[j], copiedValue.discoveryUrls[i].data[j]);
  1286. ck_assert_int_eq(value.discoveryUrls[i].length, copiedValue.discoveryUrls[i].length);
  1287. }
  1288. ck_assert_int_eq(copiedValue.discoveryUrls[0].data[2], 'o');
  1289. ck_assert_int_eq(copiedValue.discoveryUrls[0].data[3], 'p');
  1290. ck_assert_int_eq(copiedValue.discoveryUrlsSize, value.discoveryUrlsSize);
  1291. //finally
  1292. // UA_ApplicationDescription_deleteMembers(&value); // do not free the members as they are statically allocated
  1293. UA_ApplicationDescription_deleteMembers(&copiedValue);
  1294. }
  1295. END_TEST
  1296. START_TEST(UA_QualifiedName_copyShallWorkOnInputExample) {
  1297. // given
  1298. UA_String srcName = (UA_String){8, (UA_Byte*)"tEsT123!"};
  1299. UA_QualifiedName src = {5, srcName};
  1300. UA_QualifiedName dst;
  1301. // when
  1302. UA_StatusCode ret = UA_QualifiedName_copy(&src, &dst);
  1303. // then
  1304. ck_assert_int_eq(ret, UA_STATUSCODE_GOOD);
  1305. ck_assert_int_eq('E', dst.name.data[1]);
  1306. ck_assert_int_eq('!', dst.name.data[7]);
  1307. ck_assert_int_eq(8, dst.name.length);
  1308. ck_assert_int_eq(5, dst.namespaceIndex);
  1309. // finally
  1310. UA_QualifiedName_deleteMembers(&dst);
  1311. }
  1312. END_TEST
  1313. START_TEST(UA_Guid_copyShallWorkOnInputExample) {
  1314. //given
  1315. const UA_Guid src = {3, 45, 1222, {8, 7, 6, 5, 4, 3, 2, 1}};
  1316. UA_Guid dst;
  1317. //when
  1318. UA_StatusCode ret = UA_Guid_copy(&src, &dst);
  1319. //then
  1320. ck_assert_int_eq(ret, UA_STATUSCODE_GOOD);
  1321. ck_assert_int_eq(src.data1, dst.data1);
  1322. ck_assert_int_eq(src.data3, dst.data3);
  1323. ck_assert_int_eq(src.data4[4], dst.data4[4]);
  1324. //finally
  1325. }
  1326. END_TEST
  1327. START_TEST(UA_LocalizedText_copycstringShallWorkOnInputExample) {
  1328. // given
  1329. char src[8] = {'t', 'e', 'X', 't', '1', '2', '3', (char)0};
  1330. const UA_LocalizedText dst = UA_LOCALIZEDTEXT("", src);
  1331. // then
  1332. ck_assert_int_eq('1', dst.text.data[4]);
  1333. ck_assert_int_eq(0, dst.locale.length);
  1334. ck_assert_int_eq(7, dst.text.length);
  1335. }
  1336. END_TEST
  1337. START_TEST(UA_DataValue_copyShallWorkOnInputExample) {
  1338. // given
  1339. UA_Variant srcVariant;
  1340. UA_Variant_init(&srcVariant);
  1341. UA_DataValue src;
  1342. UA_DataValue_init(&src);
  1343. src.hasSourceTimestamp = UA_TRUE;
  1344. src.sourceTimestamp = 4;
  1345. src.hasSourcePicoseconds = UA_TRUE;
  1346. src.sourcePicoseconds = 77;
  1347. src.hasServerPicoseconds = UA_TRUE;
  1348. src.serverPicoseconds = 8;
  1349. UA_DataValue dst;
  1350. // when
  1351. UA_StatusCode ret = UA_DataValue_copy(&src, &dst);
  1352. // then
  1353. ck_assert_int_eq(ret, UA_STATUSCODE_GOOD);
  1354. ck_assert_int_eq(4, dst.sourceTimestamp);
  1355. ck_assert_int_eq(77, dst.sourcePicoseconds);
  1356. ck_assert_int_eq(8, dst.serverPicoseconds);
  1357. }
  1358. END_TEST
  1359. START_TEST(UA_Variant_copyShallWorkOnSingleValueExample) {
  1360. //given
  1361. UA_String testString = (UA_String){5, (UA_Byte*)"OPCUA"};
  1362. UA_Variant value, copiedValue;
  1363. UA_Variant_init(&value);
  1364. UA_Variant_init(&copiedValue);
  1365. value.data = UA_malloc(sizeof(UA_String));
  1366. *((UA_String*)value.data) = testString;
  1367. value.type = &UA_TYPES[UA_TYPES_STRING];
  1368. value.arrayLength = 1;
  1369. //when
  1370. UA_Variant_copy(&value, &copiedValue);
  1371. //then
  1372. UA_String copiedString = *(UA_String*)(copiedValue.data);
  1373. for(UA_Int32 i = 0;i < 5;i++)
  1374. ck_assert_int_eq(copiedString.data[i], testString.data[i]);
  1375. ck_assert_int_eq(copiedString.length, testString.length);
  1376. ck_assert_int_eq(value.arrayDimensionsSize, copiedValue.arrayDimensionsSize);
  1377. ck_assert_int_eq(value.arrayLength, copiedValue.arrayLength);
  1378. //finally
  1379. ((UA_String*)value.data)->data = UA_NULL; // the string is statically allocated. do not free it.
  1380. UA_Variant_deleteMembers(&value);
  1381. UA_Variant_deleteMembers(&copiedValue);
  1382. }
  1383. END_TEST
  1384. START_TEST(UA_Variant_copyShallWorkOn1DArrayExample) {
  1385. // given
  1386. UA_String *srcArray = UA_Array_new(&UA_TYPES[UA_TYPES_STRING], 3);
  1387. srcArray[0] = UA_STRING_ALLOC("__open");
  1388. srcArray[1] = UA_STRING_ALLOC("_62541");
  1389. srcArray[2] = UA_STRING_ALLOC("opc ua");
  1390. UA_Int32 *dimensions;
  1391. dimensions = UA_malloc(sizeof(UA_Int32));
  1392. dimensions[0] = 3;
  1393. UA_Variant value, copiedValue;
  1394. UA_Variant_init(&value);
  1395. UA_Variant_init(&copiedValue);
  1396. value.arrayLength = 3;
  1397. value.data = (void *)srcArray;
  1398. value.arrayDimensionsSize = 1;
  1399. value.arrayDimensions = dimensions;
  1400. value.type = &UA_TYPES[UA_TYPES_STRING];
  1401. //when
  1402. UA_Variant_copy(&value, &copiedValue);
  1403. //then
  1404. UA_Int32 i1 = value.arrayDimensions[0];
  1405. UA_Int32 i2 = copiedValue.arrayDimensions[0];
  1406. ck_assert_int_eq(i1, i2);
  1407. for(UA_Int32 i = 0;i < 3;i++) {
  1408. for(UA_Int32 j = 0;j < 6;j++) {
  1409. ck_assert_int_eq(((UA_String *)value.data)[i].data[j],
  1410. ((UA_String *)copiedValue.data)[i].data[j]);
  1411. }
  1412. ck_assert_int_eq(((UA_String *)value.data)[i].length,
  1413. ((UA_String *)copiedValue.data)[i].length);
  1414. }
  1415. ck_assert_int_eq(((UA_String *)copiedValue.data)[0].data[2], 'o');
  1416. ck_assert_int_eq(((UA_String *)copiedValue.data)[0].data[3], 'p');
  1417. ck_assert_int_eq(value.arrayDimensionsSize, copiedValue.arrayDimensionsSize);
  1418. ck_assert_int_eq(value.arrayLength, copiedValue.arrayLength);
  1419. //finally
  1420. UA_Variant_deleteMembers(&value);
  1421. UA_Variant_deleteMembers(&copiedValue);
  1422. }
  1423. END_TEST
  1424. START_TEST(UA_Variant_copyShallWorkOn2DArrayExample) {
  1425. // given
  1426. UA_Int32 *srcArray = UA_Array_new(&UA_TYPES[UA_TYPES_INT32], 6);
  1427. srcArray[0] = 0;
  1428. srcArray[1] = 1;
  1429. srcArray[2] = 2;
  1430. srcArray[3] = 3;
  1431. srcArray[4] = 4;
  1432. srcArray[5] = 5;
  1433. UA_Int32 *dimensions = UA_Array_new(&UA_TYPES[UA_TYPES_INT32], 2);
  1434. UA_Int32 dim1 = 3;
  1435. UA_Int32 dim2 = 2;
  1436. dimensions[0] = dim1;
  1437. dimensions[1] = dim2;
  1438. UA_Variant value, copiedValue;
  1439. UA_Variant_init(&value);
  1440. UA_Variant_init(&copiedValue);
  1441. value.arrayLength = 6;
  1442. value.data = srcArray;
  1443. value.arrayDimensionsSize = 2;
  1444. value.arrayDimensions = dimensions;
  1445. value.type = &UA_TYPES[UA_TYPES_INT32];
  1446. //when
  1447. UA_Variant_copy(&value, &copiedValue);
  1448. //then
  1449. //1st dimension
  1450. UA_Int32 i1 = value.arrayDimensions[0];
  1451. UA_Int32 i2 = copiedValue.arrayDimensions[0];
  1452. ck_assert_int_eq(i1, i2);
  1453. ck_assert_int_eq(i1, dim1);
  1454. //2nd dimension
  1455. i1 = value.arrayDimensions[1];
  1456. i2 = copiedValue.arrayDimensions[1];
  1457. ck_assert_int_eq(i1, i2);
  1458. ck_assert_int_eq(i1, dim2);
  1459. for(UA_Int32 i = 0;i < 6;i++) {
  1460. i1 = ((UA_Int32 *)value.data)[i];
  1461. i2 = ((UA_Int32 *)copiedValue.data)[i];
  1462. ck_assert_int_eq(i1, i2);
  1463. ck_assert_int_eq(i2, i);
  1464. }
  1465. ck_assert_int_eq(value.arrayDimensionsSize, copiedValue.arrayDimensionsSize);
  1466. ck_assert_int_eq(value.arrayLength, copiedValue.arrayLength);
  1467. //finally
  1468. UA_Variant_deleteMembers(&value);
  1469. UA_Variant_deleteMembers(&copiedValue);
  1470. }
  1471. END_TEST
  1472. START_TEST(UA_ExtensionObject_encodeDecodeShallWorkOnExtensionObject) {
  1473. UA_Int32 val = 42;
  1474. UA_VariableAttributes varAttr;
  1475. UA_VariableAttributes_init(&varAttr);
  1476. varAttr.dataType = UA_TYPES[UA_TYPES_INT32].typeId;
  1477. UA_Variant_init(&varAttr.value);
  1478. varAttr.value.type = &UA_TYPES[UA_TYPES_INT32];
  1479. varAttr.value.data = &val;
  1480. varAttr.value.arrayLength = -1;
  1481. varAttr.userWriteMask = 41;
  1482. varAttr.specifiedAttributes |= UA_NODEATTRIBUTESMASK_DATATYPE;
  1483. varAttr.specifiedAttributes |= UA_NODEATTRIBUTESMASK_VALUE;
  1484. varAttr.specifiedAttributes |= UA_NODEATTRIBUTESMASK_USERWRITEMASK;
  1485. /* wrap it into a extension object attributes */
  1486. UA_ExtensionObject extensionObject;
  1487. UA_ExtensionObject_init(&extensionObject);
  1488. extensionObject.typeId = UA_TYPES[UA_TYPES_VARIABLEATTRIBUTES].typeId;
  1489. UA_Byte extensionData[50];
  1490. extensionObject.body = (UA_ByteString){.data = extensionData, .length=50};
  1491. size_t posEncode = 0;
  1492. UA_VariableAttributes_encodeBinary(&varAttr, &extensionObject.body, &posEncode);
  1493. extensionObject.body.length = posEncode;
  1494. extensionObject.encoding = UA_EXTENSIONOBJECT_ENCODINGMASK_BODYISBYTESTRING;
  1495. UA_Byte data[50];
  1496. UA_ByteString dst = {.data = data, .length=50};
  1497. posEncode = 0;
  1498. UA_ExtensionObject_encodeBinary(&extensionObject, &dst, &posEncode);
  1499. UA_ExtensionObject extensionObjectDecoded;
  1500. size_t posDecode = 0;
  1501. UA_ExtensionObject_decodeBinary(&dst, &posDecode, &extensionObjectDecoded);
  1502. ck_assert_int_eq(posEncode, posDecode);
  1503. ck_assert_int_eq(extensionObjectDecoded.body.length, extensionObject.body.length);
  1504. UA_VariableAttributes varAttrDecoded;
  1505. UA_VariableAttributes_init(&varAttrDecoded);
  1506. posDecode = 0;
  1507. UA_VariableAttributes_decodeBinary(&extensionObjectDecoded.body, &posDecode, &varAttrDecoded);
  1508. ck_assert_uint_eq(41, varAttrDecoded.userWriteMask);
  1509. ck_assert_int_eq(-1, varAttrDecoded.value.arrayLength);
  1510. // finally
  1511. UA_ExtensionObject_deleteMembers(&extensionObjectDecoded);
  1512. UA_Variant_deleteMembers(&varAttrDecoded.value);
  1513. }
  1514. END_TEST
  1515. static Suite *testSuite_builtin(void) {
  1516. Suite *s = suite_create("Built-in Data Types 62541-6 Table 1");
  1517. TCase *tc_calcSize = tcase_create("calcSize");
  1518. tcase_add_test(tc_calcSize, UA_ExtensionObject_calcSizeShallWorkOnExample);
  1519. tcase_add_test(tc_calcSize, UA_DataValue_calcSizeShallWorkOnExample);
  1520. tcase_add_test(tc_calcSize, UA_DiagnosticInfo_calcSizeShallWorkOnExample);
  1521. tcase_add_test(tc_calcSize, UA_String_calcSizeShallReturnEncodingSize);
  1522. tcase_add_test(tc_calcSize, UA_String_calcSizeWithNegativLengthShallReturnEncodingSize);
  1523. tcase_add_test(tc_calcSize, UA_String_calcSizeWithNegativLengthAndValidPointerShallReturnEncodingSize);
  1524. tcase_add_test(tc_calcSize, UA_String_calcSizeWithZeroLengthShallReturnEncodingSize);
  1525. tcase_add_test(tc_calcSize, UA_String_calcSizeWithZeroLengthAndValidPointerShallReturnEncodingSize);
  1526. tcase_add_test(tc_calcSize, UA_NodeId_calcSizeEncodingTwoByteShallReturnEncodingSize);
  1527. tcase_add_test(tc_calcSize, UA_NodeId_calcSizeEncodingFourByteShallReturnEncodingSize);
  1528. tcase_add_test(tc_calcSize, UA_NodeId_calcSizeEncodingStringShallReturnEncodingSize);
  1529. tcase_add_test(tc_calcSize, UA_NodeId_calcSizeEncodingStringNegativLengthShallReturnEncodingSize);
  1530. tcase_add_test(tc_calcSize, UA_NodeId_calcSizeEncodingStringZeroLengthShallReturnEncodingSize);
  1531. tcase_add_test(tc_calcSize, UA_ExpandedNodeId_calcSizeEncodingStringAndServerIndexShallReturnEncodingSize);
  1532. tcase_add_test(tc_calcSize, UA_ExpandedNodeId_calcSizeEncodingStringAndNamespaceUriShallReturnEncodingSize);
  1533. tcase_add_test(tc_calcSize, UA_Guid_calcSizeShallReturnEncodingSize);
  1534. tcase_add_test(tc_calcSize, UA_Guid_calcSizeShallReturnEncodingSize);
  1535. tcase_add_test(tc_calcSize, UA_LocalizedText_calcSizeTextOnlyShallReturnEncodingSize);
  1536. tcase_add_test(tc_calcSize, UA_LocalizedText_calcSizeLocaleOnlyShallReturnEncodingSize);
  1537. tcase_add_test(tc_calcSize, UA_LocalizedText_calcSizeTextAndLocaleShallReturnEncodingSize);
  1538. tcase_add_test(tc_calcSize, UA_Variant_calcSizeFixedSizeArrayShallReturnEncodingSize);
  1539. tcase_add_test(tc_calcSize, UA_Variant_calcSizeVariableSizeArrayShallReturnEncodingSize);
  1540. tcase_add_test(tc_calcSize, UA_Variant_decodeWithOutDeleteMembersShallFailInCheckMem);
  1541. suite_add_tcase(s, tc_calcSize);
  1542. TCase *tc_decode = tcase_create("decode");
  1543. tcase_add_test(tc_decode, UA_Byte_decodeShallCopyAndAdvancePosition);
  1544. tcase_add_test(tc_decode, UA_Byte_decodeShallModifyOnlyCurrentPosition);
  1545. tcase_add_test(tc_decode, UA_Int16_decodeShallAssumeLittleEndian);
  1546. tcase_add_test(tc_decode, UA_Int16_decodeShallRespectSign);
  1547. tcase_add_test(tc_decode, UA_UInt16_decodeShallNotRespectSign);
  1548. tcase_add_test(tc_decode, UA_Int32_decodeShallAssumeLittleEndian);
  1549. tcase_add_test(tc_decode, UA_Int32_decodeShallRespectSign);
  1550. tcase_add_test(tc_decode, UA_UInt32_decodeShallNotRespectSign);
  1551. tcase_add_test(tc_decode, UA_UInt64_decodeShallNotRespectSign);
  1552. tcase_add_test(tc_decode, UA_Int64_decodeShallRespectSign);
  1553. tcase_add_test(tc_decode, UA_Float_decodeShallWorkOnExample);
  1554. tcase_add_test(tc_decode, UA_Double_decodeShallGiveOne);
  1555. tcase_add_test(tc_decode, UA_Double_decodeShallGiveZero);
  1556. tcase_add_test(tc_decode, UA_Double_decodeShallGiveMinusTwo);
  1557. tcase_add_test(tc_decode, UA_Double_decodeShallGive2147483648);
  1558. tcase_add_test(tc_decode, UA_Byte_encode_test);
  1559. tcase_add_test(tc_decode, UA_String_decodeShallAllocateMemoryAndCopyString);
  1560. tcase_add_test(tc_decode, UA_String_decodeWithNegativeSizeShallNotAllocateMemoryAndNullPtr);
  1561. tcase_add_test(tc_decode, UA_String_decodeWithZeroSizeShallNotAllocateMemoryAndNullPtr);
  1562. tcase_add_test(tc_decode, UA_NodeId_decodeTwoByteShallReadTwoBytesAndSetNamespaceToZero);
  1563. tcase_add_test(tc_decode, UA_NodeId_decodeFourByteShallReadFourBytesAndRespectNamespace);
  1564. tcase_add_test(tc_decode, UA_NodeId_decodeStringShallAllocateMemory);
  1565. tcase_add_test(tc_decode, UA_Variant_decodeSingleExtensionObjectShallSetVTAndAllocateMemory);
  1566. tcase_add_test(tc_decode, UA_Variant_decodeWithOutArrayFlagSetShallSetVTAndAllocateMemoryForArray);
  1567. tcase_add_test(tc_decode, UA_Variant_decodeWithArrayFlagSetShallSetVTAndAllocateMemoryForArray);
  1568. tcase_add_test(tc_decode, UA_Variant_decodeWithOutDeleteMembersShallFailInCheckMem);
  1569. tcase_add_test(tc_decode, UA_Variant_decodeWithTooSmallSourceShallReturnWithError);
  1570. suite_add_tcase(s, tc_decode);
  1571. TCase *tc_encode = tcase_create("encode");
  1572. tcase_add_test(tc_encode, UA_Byte_encode_test);
  1573. tcase_add_test(tc_encode, UA_UInt16_encodeNegativeShallEncodeLittleEndian);
  1574. tcase_add_test(tc_encode, UA_UInt16_encodeShallEncodeLittleEndian);
  1575. tcase_add_test(tc_encode, UA_UInt32_encodeShallEncodeLittleEndian);
  1576. tcase_add_test(tc_encode, UA_Int32_encodeShallEncodeLittleEndian);
  1577. tcase_add_test(tc_encode, UA_Int32_encodeNegativeShallEncodeLittleEndian);
  1578. tcase_add_test(tc_encode, UA_UInt64_encodeShallWorkOnExample);
  1579. tcase_add_test(tc_encode, UA_Int64_encodeNegativeShallEncodeLittleEndian);
  1580. tcase_add_test(tc_encode, UA_Int64_encodeShallEncodeLittleEndian);
  1581. tcase_add_test(tc_encode, UA_Float_encodeShallWorkOnExample);
  1582. tcase_add_test(tc_encode, UA_Double_encodeShallWorkOnExample);
  1583. tcase_add_test(tc_encode, UA_String_encodeShallWorkOnExample);
  1584. tcase_add_test(tc_encode, UA_DataValue_encodeShallWorkOnExampleWithoutVariant);
  1585. tcase_add_test(tc_encode, UA_DataValue_encodeShallWorkOnExampleWithVariant);
  1586. tcase_add_test(tc_encode, UA_ExtensionObject_encodeDecodeShallWorkOnExtensionObject);
  1587. suite_add_tcase(s, tc_encode);
  1588. TCase *tc_convert = tcase_create("convert");
  1589. tcase_add_test(tc_convert, UA_DateTime_toStructShallWorkOnExample);
  1590. tcase_add_test(tc_convert, UA_DateTime_toStringShallWorkOnExample);
  1591. suite_add_tcase(s, tc_convert);
  1592. TCase *tc_copy = tcase_create("copy");
  1593. tcase_add_test(tc_copy, UA_Array_copyByteArrayShallWorkOnExample);
  1594. tcase_add_test(tc_copy, UA_Array_copyUA_StringShallWorkOnExample);
  1595. tcase_add_test(tc_copy, UA_ExtensionObject_copyShallWorkOnExample);
  1596. tcase_add_test(tc_copy, UA_Variant_copyShallWorkOnSingleValueExample);
  1597. tcase_add_test(tc_copy, UA_Variant_copyShallWorkOn1DArrayExample);
  1598. tcase_add_test(tc_copy, UA_Variant_copyShallWorkOn2DArrayExample);
  1599. tcase_add_test(tc_copy, UA_DiagnosticInfo_copyShallWorkOnExample);
  1600. tcase_add_test(tc_copy, UA_ApplicationDescription_copyShallWorkOnExample);
  1601. tcase_add_test(tc_copy, UA_QualifiedName_copyShallWorkOnInputExample);
  1602. tcase_add_test(tc_copy, UA_Guid_copyShallWorkOnInputExample);
  1603. tcase_add_test(tc_copy, UA_LocalizedText_copycstringShallWorkOnInputExample);
  1604. tcase_add_test(tc_copy, UA_DataValue_copyShallWorkOnInputExample);
  1605. suite_add_tcase(s, tc_copy);
  1606. return s;
  1607. }
  1608. int main(void) {
  1609. int number_failed = 0;
  1610. Suite *s;
  1611. SRunner *sr;
  1612. s = testSuite_builtin();
  1613. sr = srunner_create(s);
  1614. srunner_set_fork_status(sr, CK_NOFORK);
  1615. srunner_run_all(sr, CK_NORMAL);
  1616. number_failed += srunner_ntests_failed(sr);
  1617. srunner_free(sr);
  1618. return (number_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
  1619. }