check_builtin.c 53 KB

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