check_builtin.c 55 KB

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