check_builtin.c 54 KB

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