check_builtin.c 55 KB

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