check_builtin.c 53 KB

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