check_builtin.c 55 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770
  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.storage.data.arrayLength = ARRAY_LEN;
  259. UA_Int32 *data[ARRAY_LEN];
  260. arg.storage.data.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.storage.data.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.storage.data.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_IDS[UA_TYPES_INT32], 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.storage.data.arrayLength, 1);
  633. ck_assert_int_eq(*(UA_Int32 *)dst.storage.data.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_IDS[UA_TYPES_INT32] | 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.storage.data.arrayLength, 2);
  654. ck_assert_int_eq(((UA_Int32 *)dst.storage.data.dataPtr)[0], 255);
  655. ck_assert_int_eq(((UA_Int32 *)dst.storage.data.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_IDS[UA_TYPES_INT32] | 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_IDS[UA_TYPES_INT32] | 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.storage.data.arrayLength = 1; // one element (encoded as not an array)
  1031. UA_Int32 vdata = 45;
  1032. src.value.storage.data.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], ' ');
  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.identifierType = UA_NODEIDTYPE_NUMERIC;
  1098. value.typeId.identifier.numeric = UA_TYPES_IDS[UA_TYPES_BYTE];
  1099. value.encoding = UA_EXTENSIONOBJECT_ENCODINGMASK_NOBODYISENCODED;
  1100. value.encoding = UA_EXTENSIONOBJECT_ENCODINGMASK_BODYISBYTESTRING;
  1101. value.body.data = data;
  1102. value.body.length = 3;
  1103. //when
  1104. UA_ExtensionObject_copy(&value, &valueCopied);
  1105. for(UA_Int32 i = 0;i < 3;i++)
  1106. ck_assert_int_eq(valueCopied.body.data[i], value.body.data[i]);
  1107. ck_assert_int_eq(valueCopied.encoding, value.encoding);
  1108. ck_assert_int_eq(valueCopied.typeId.identifierType, value.typeId.identifierType);
  1109. ck_assert_int_eq(valueCopied.typeId.identifier.numeric, value.typeId.identifier.numeric);
  1110. //finally
  1111. value.body.data = UA_NULL; // we cannot free the static string
  1112. UA_ExtensionObject_deleteMembers(&value);
  1113. UA_ExtensionObject_deleteMembers(&valueCopied);
  1114. }
  1115. END_TEST
  1116. START_TEST(UA_Array_copyByteArrayShallWorkOnExample) {
  1117. //given
  1118. UA_String testString;
  1119. UA_Byte *dstArray;
  1120. UA_Int32 size = 5;
  1121. UA_Int32 i = 0;
  1122. testString.data = UA_malloc(size);
  1123. testString.data[0] = 'O';
  1124. testString.data[1] = 'P';
  1125. testString.data[2] = 'C';
  1126. testString.data[3] = 'U';
  1127. testString.data[4] = 'A';
  1128. testString.length = 5;
  1129. //when
  1130. UA_Array_copy((const void *)testString.data, 5, (void **)&dstArray, &UA_TYPES[UA_TYPES_BYTE]);
  1131. //then
  1132. for(i = 0;i < size;i++)
  1133. ck_assert_int_eq(testString.data[i], dstArray[i]);
  1134. //finally
  1135. UA_String_deleteMembers(&testString);
  1136. UA_free((void *)dstArray);
  1137. }
  1138. END_TEST
  1139. START_TEST(UA_Array_copyUA_StringShallWorkOnExample) {
  1140. // given
  1141. UA_Int32 i, j;
  1142. UA_String *srcArray;
  1143. UA_Array_new((void**)&srcArray, 3, &UA_TYPES[UA_TYPES_STRING]);
  1144. UA_String *dstArray;
  1145. UA_String_copycstring("open", &srcArray[0]);
  1146. UA_String_copycstring("62541", &srcArray[1]);
  1147. UA_String_copycstring("opc ua", &srcArray[2]);
  1148. //when
  1149. UA_Array_copy((const void *)srcArray, 3, (void **)&dstArray, &UA_TYPES[UA_TYPES_STRING]);
  1150. //then
  1151. for(i = 0;i < 3;i++) {
  1152. for(j = 0;j < 3;j++)
  1153. ck_assert_int_eq(srcArray[i].data[j], dstArray[i].data[j]);
  1154. ck_assert_int_eq(srcArray[i].length, dstArray[i].length);
  1155. }
  1156. //finally
  1157. UA_Array_delete(srcArray, 3, &UA_TYPES[UA_TYPES_STRING]);
  1158. UA_Array_delete(dstArray, 3, &UA_TYPES[UA_TYPES_STRING]);
  1159. }
  1160. END_TEST
  1161. START_TEST(UA_DiagnosticInfo_copyShallWorkOnExample) {
  1162. //given
  1163. UA_DiagnosticInfo value, innerValue, copiedValue;
  1164. UA_String testString = (UA_String){5, (UA_Byte*)"OPCUA"};
  1165. UA_DiagnosticInfo_init(&value);
  1166. UA_DiagnosticInfo_init(&innerValue);
  1167. value.hasInnerDiagnosticInfo = UA_TRUE;
  1168. value.innerDiagnosticInfo = &innerValue;
  1169. value.additionalInfo = testString;
  1170. //when
  1171. UA_DiagnosticInfo_copy(&value, &copiedValue);
  1172. //then
  1173. for(UA_Int32 i = 0;i < testString.length;i++)
  1174. ck_assert_int_eq(copiedValue.additionalInfo.data[i], value.additionalInfo.data[i]);
  1175. ck_assert_int_eq(copiedValue.additionalInfo.length, value.additionalInfo.length);
  1176. ck_assert_int_eq(copiedValue.hasInnerDiagnosticInfo, value.hasInnerDiagnosticInfo);
  1177. ck_assert_int_eq(copiedValue.innerDiagnosticInfo->locale, value.innerDiagnosticInfo->locale);
  1178. ck_assert_int_eq(copiedValue.innerStatusCode, value.innerStatusCode);
  1179. ck_assert_int_eq(copiedValue.locale, value.locale);
  1180. ck_assert_int_eq(copiedValue.localizedText, value.localizedText);
  1181. ck_assert_int_eq(copiedValue.namespaceUri, value.namespaceUri);
  1182. ck_assert_int_eq(copiedValue.symbolicId, value.symbolicId);
  1183. //finally
  1184. value.additionalInfo.data = UA_NULL; // do not delete the static string
  1185. value.innerDiagnosticInfo = UA_NULL; // do not delete the static innerdiagnosticinfo
  1186. UA_DiagnosticInfo_deleteMembers(&value);
  1187. UA_DiagnosticInfo_deleteMembers(&copiedValue);
  1188. }
  1189. END_TEST
  1190. START_TEST(UA_ApplicationDescription_copyShallWorkOnExample) {
  1191. //given
  1192. UA_StatusCode retval = UA_STATUSCODE_GOOD;
  1193. UA_String appString = (UA_String){3, (UA_Byte*)"APP"};
  1194. UA_String discString = (UA_String){4, (UA_Byte*)"DISC"};
  1195. UA_String gateWayString = (UA_String){7, (UA_Byte*)"GATEWAY"};
  1196. UA_String srcArray[3];
  1197. srcArray[0] = (UA_String){ 6, (UA_Byte*)"__open" };
  1198. srcArray[1] = (UA_String){ 6, (UA_Byte*)"_62541" };
  1199. srcArray[2] = (UA_String){ 6, (UA_Byte*)"opc ua" };
  1200. UA_ApplicationDescription value, copiedValue;
  1201. UA_ApplicationDescription_init(&value);
  1202. value.applicationUri = appString;
  1203. value.discoveryProfileUri = discString;
  1204. value.gatewayServerUri = gateWayString;
  1205. value.discoveryUrlsSize = 3;
  1206. value.discoveryUrls = srcArray;
  1207. //when
  1208. retval = UA_ApplicationDescription_copy(&value, &copiedValue);
  1209. //then
  1210. ck_assert_int_eq(retval, UA_STATUSCODE_GOOD);
  1211. for(UA_Int32 i = 0;i < appString.length;i++)
  1212. ck_assert_int_eq(copiedValue.applicationUri.data[i], value.applicationUri.data[i]);
  1213. ck_assert_int_eq(copiedValue.applicationUri.length, value.applicationUri.length);
  1214. for(UA_Int32 i = 0;i < discString.length;i++)
  1215. ck_assert_int_eq(copiedValue.discoveryProfileUri.data[i], value.discoveryProfileUri.data[i]);
  1216. ck_assert_int_eq(copiedValue.discoveryProfileUri.length, value.discoveryProfileUri.length);
  1217. for(UA_Int32 i = 0;i < gateWayString.length;i++)
  1218. ck_assert_int_eq(copiedValue.gatewayServerUri.data[i], value.gatewayServerUri.data[i]);
  1219. ck_assert_int_eq(copiedValue.gatewayServerUri.length, value.gatewayServerUri.length);
  1220. //String Array Test
  1221. for(UA_Int32 i = 0;i < 3;i++) {
  1222. for(UA_Int32 j = 0;j < 6;j++)
  1223. ck_assert_int_eq(value.discoveryUrls[i].data[j], copiedValue.discoveryUrls[i].data[j]);
  1224. ck_assert_int_eq(value.discoveryUrls[i].length, copiedValue.discoveryUrls[i].length);
  1225. }
  1226. ck_assert_int_eq(copiedValue.discoveryUrls[0].data[2], 'o');
  1227. ck_assert_int_eq(copiedValue.discoveryUrls[0].data[3], 'p');
  1228. ck_assert_int_eq(copiedValue.discoveryUrlsSize, value.discoveryUrlsSize);
  1229. //finally
  1230. // UA_ApplicationDescription_deleteMembers(&value); // do not free the members as they are statically allocated
  1231. UA_ApplicationDescription_deleteMembers(&copiedValue);
  1232. }
  1233. END_TEST
  1234. START_TEST(UA_QualifiedName_copyShallWorkOnInputExample) {
  1235. // given
  1236. UA_String srcName = (UA_String){8, (UA_Byte*)"tEsT123!"};
  1237. UA_QualifiedName src = {5, srcName};
  1238. UA_QualifiedName dst;
  1239. // when
  1240. UA_StatusCode ret = UA_QualifiedName_copy(&src, &dst);
  1241. // then
  1242. ck_assert_int_eq(ret, UA_STATUSCODE_GOOD);
  1243. ck_assert_int_eq('E', dst.name.data[1]);
  1244. ck_assert_int_eq('!', dst.name.data[7]);
  1245. ck_assert_int_eq(8, dst.name.length);
  1246. ck_assert_int_eq(5, dst.namespaceIndex);
  1247. // finally
  1248. UA_QualifiedName_deleteMembers(&dst);
  1249. }
  1250. END_TEST
  1251. START_TEST(UA_Guid_copyShallWorkOnInputExample) {
  1252. //given
  1253. const UA_Guid src = {3, 45, 1222, {8, 7, 6, 5, 4, 3, 2, 1}};
  1254. UA_Guid dst;
  1255. //when
  1256. UA_StatusCode ret = UA_Guid_copy(&src, &dst);
  1257. //then
  1258. ck_assert_int_eq(ret, UA_STATUSCODE_GOOD);
  1259. ck_assert_int_eq(src.data1, dst.data1);
  1260. ck_assert_int_eq(src.data3, dst.data3);
  1261. ck_assert_int_eq(src.data4[4], dst.data4[4]);
  1262. //finally
  1263. }
  1264. END_TEST
  1265. START_TEST(UA_LocalizedText_copycstringShallWorkOnInputExample) {
  1266. // given
  1267. const char src[8] = {'t', 'e', 'X', 't', '1', '2', '3', (char)0};
  1268. UA_LocalizedText dst;
  1269. // when
  1270. UA_StatusCode ret = UA_LocalizedText_copycstring(src, &dst);
  1271. // then
  1272. ck_assert_int_eq(ret, UA_STATUSCODE_GOOD);
  1273. ck_assert_int_eq('e', dst.locale.data[0]);
  1274. ck_assert_int_eq('n', dst.locale.data[1]);
  1275. ck_assert_int_eq('1', dst.text.data[4]);
  1276. ck_assert_int_eq(2, dst.locale.length);
  1277. ck_assert_int_eq(7, dst.text.length);
  1278. // finally
  1279. UA_LocalizedText_deleteMembers(&dst);
  1280. }
  1281. END_TEST
  1282. START_TEST(UA_DataValue_copyShallWorkOnInputExample) {
  1283. // given
  1284. UA_Variant srcVariant;
  1285. UA_Variant_init(&srcVariant);
  1286. UA_DataValue src;
  1287. UA_DataValue_init(&src);
  1288. src.hasSourceTimestamp = UA_TRUE;
  1289. src.sourceTimestamp = 4;
  1290. src.hasSourcePicoseconds = UA_TRUE;
  1291. src.sourcePicoseconds = 77;
  1292. src.hasServerPicoseconds = UA_TRUE;
  1293. src.serverPicoseconds = 8;
  1294. UA_DataValue dst;
  1295. // when
  1296. UA_StatusCode ret = UA_DataValue_copy(&src, &dst);
  1297. // then
  1298. ck_assert_int_eq(ret, UA_STATUSCODE_GOOD);
  1299. ck_assert_int_eq(4, dst.sourceTimestamp);
  1300. ck_assert_int_eq(77, dst.sourcePicoseconds);
  1301. ck_assert_int_eq(8, dst.serverPicoseconds);
  1302. }
  1303. END_TEST
  1304. START_TEST(UA_Variant_copyShallWorkOnSingleValueExample) {
  1305. //given
  1306. UA_String testString = (UA_String){5, (UA_Byte*)"OPCUA"};
  1307. UA_Variant value, copiedValue;
  1308. UA_Variant_init(&value);
  1309. UA_Variant_init(&copiedValue);
  1310. value.storage.data.dataPtr = UA_malloc(sizeof(UA_String));
  1311. *((UA_String*)value.storage.data.dataPtr) = testString;
  1312. value.type = &UA_TYPES[UA_TYPES_STRING];
  1313. value.storage.data.arrayLength = 1;
  1314. //when
  1315. UA_Variant_copy(&value, &copiedValue);
  1316. //then
  1317. UA_String copiedString = *(UA_String*)(copiedValue.storage.data.dataPtr);
  1318. for(UA_Int32 i = 0;i < 5;i++)
  1319. ck_assert_int_eq(copiedString.data[i], testString.data[i]);
  1320. ck_assert_int_eq(copiedString.length, testString.length);
  1321. ck_assert_int_eq(value.storage.data.arrayDimensionsSize, copiedValue.storage.data.arrayDimensionsSize);
  1322. ck_assert_int_eq(value.storage.data.arrayLength, copiedValue.storage.data.arrayLength);
  1323. //finally
  1324. ((UA_String*)value.storage.data.dataPtr)->data = UA_NULL; // the string is statically allocated. do not free it.
  1325. UA_Variant_deleteMembers(&value);
  1326. UA_Variant_deleteMembers(&copiedValue);
  1327. }
  1328. END_TEST
  1329. START_TEST(UA_Variant_copyShallWorkOn1DArrayExample) {
  1330. // given
  1331. UA_String *srcArray;
  1332. UA_Array_new((void**)&srcArray, 3, &UA_TYPES[UA_TYPES_STRING]);
  1333. UA_String_copycstring("__open", &srcArray[0]);
  1334. UA_String_copycstring("_62541", &srcArray[1]);
  1335. UA_String_copycstring("opc ua", &srcArray[2]);
  1336. UA_Int32 *dimensions;
  1337. dimensions = UA_malloc(sizeof(UA_Int32));
  1338. dimensions[0] = 3;
  1339. UA_Variant value, copiedValue;
  1340. UA_Variant_init(&value);
  1341. UA_Variant_init(&copiedValue);
  1342. value.storage.data.arrayLength = 3;
  1343. value.storage.data.dataPtr = (void *)srcArray;
  1344. value.storage.data.arrayDimensionsSize = 1;
  1345. value.storage.data.arrayDimensions = dimensions;
  1346. value.type = &UA_TYPES[UA_TYPES_STRING];
  1347. //when
  1348. UA_Variant_copy(&value, &copiedValue);
  1349. //then
  1350. UA_Int32 i1 = value.storage.data.arrayDimensions[0];
  1351. UA_Int32 i2 = copiedValue.storage.data.arrayDimensions[0];
  1352. ck_assert_int_eq(i1, i2);
  1353. for(UA_Int32 i = 0;i < 3;i++) {
  1354. for(UA_Int32 j = 0;j < 6;j++) {
  1355. ck_assert_int_eq(((UA_String *)value.storage.data.dataPtr)[i].data[j],
  1356. ((UA_String *)copiedValue.storage.data.dataPtr)[i].data[j]);
  1357. }
  1358. ck_assert_int_eq(((UA_String *)value.storage.data.dataPtr)[i].length,
  1359. ((UA_String *)copiedValue.storage.data.dataPtr)[i].length);
  1360. }
  1361. ck_assert_int_eq(((UA_String *)copiedValue.storage.data.dataPtr)[0].data[2], 'o');
  1362. ck_assert_int_eq(((UA_String *)copiedValue.storage.data.dataPtr)[0].data[3], 'p');
  1363. ck_assert_int_eq(value.storage.data.arrayDimensionsSize, copiedValue.storage.data.arrayDimensionsSize);
  1364. ck_assert_int_eq(value.storage.data.arrayLength, copiedValue.storage.data.arrayLength);
  1365. //finally
  1366. UA_Variant_deleteMembers(&value);
  1367. UA_Variant_deleteMembers(&copiedValue);
  1368. }
  1369. END_TEST
  1370. START_TEST(UA_Variant_copyShallWorkOn2DArrayExample) {
  1371. // given
  1372. UA_Int32 *srcArray;
  1373. UA_Array_new((void**)&srcArray, 6, &UA_TYPES[UA_TYPES_INT32]);
  1374. srcArray[0] = 0;
  1375. srcArray[1] = 1;
  1376. srcArray[2] = 2;
  1377. srcArray[3] = 3;
  1378. srcArray[4] = 4;
  1379. srcArray[5] = 5;
  1380. UA_Int32 *dimensions;
  1381. UA_Array_new((void**)&dimensions, 2, &UA_TYPES[UA_TYPES_INT32]);
  1382. UA_Int32 dim1 = 3;
  1383. UA_Int32 dim2 = 2;
  1384. dimensions[0] = dim1;
  1385. dimensions[1] = dim2;
  1386. UA_Variant value, copiedValue;
  1387. UA_Variant_init(&value);
  1388. UA_Variant_init(&copiedValue);
  1389. value.storage.data.arrayLength = 6;
  1390. value.storage.data.dataPtr = srcArray;
  1391. value.storage.data.arrayDimensionsSize = 2;
  1392. value.storage.data.arrayDimensions = dimensions;
  1393. value.type = &UA_TYPES[UA_TYPES_INT32];
  1394. //when
  1395. UA_Variant_copy(&value, &copiedValue);
  1396. //then
  1397. //1st dimension
  1398. UA_Int32 i1 = value.storage.data.arrayDimensions[0];
  1399. UA_Int32 i2 = copiedValue.storage.data.arrayDimensions[0];
  1400. ck_assert_int_eq(i1, i2);
  1401. ck_assert_int_eq(i1, dim1);
  1402. //2nd dimension
  1403. i1 = value.storage.data.arrayDimensions[1];
  1404. i2 = copiedValue.storage.data.arrayDimensions[1];
  1405. ck_assert_int_eq(i1, i2);
  1406. ck_assert_int_eq(i1, dim2);
  1407. for(UA_Int32 i = 0;i < 6;i++) {
  1408. i1 = ((UA_Int32 *)value.storage.data.dataPtr)[i];
  1409. i2 = ((UA_Int32 *)copiedValue.storage.data.dataPtr)[i];
  1410. ck_assert_int_eq(i1, i2);
  1411. ck_assert_int_eq(i2, i);
  1412. }
  1413. ck_assert_int_eq(value.storage.data.arrayDimensionsSize, copiedValue.storage.data.arrayDimensionsSize);
  1414. ck_assert_int_eq(value.storage.data.arrayLength, copiedValue.storage.data.arrayLength);
  1415. //finally
  1416. UA_Variant_deleteMembers(&value);
  1417. UA_Variant_deleteMembers(&copiedValue);
  1418. }
  1419. END_TEST
  1420. START_TEST(UA_ExtensionObject_encodeDecodeShallWorkOnExtensionObject) {
  1421. UA_Int32 val = 42;
  1422. UA_VariableAttributes varAttr;
  1423. UA_VariableAttributes_init(&varAttr);
  1424. varAttr.dataType.identifierType = UA_NODEIDTYPE_NUMERIC;
  1425. varAttr.dataType.identifier.numeric = UA_TYPES_IDS[UA_TYPES_INT32];
  1426. UA_Variant_init(&varAttr.value);
  1427. varAttr.value.type = &UA_TYPES[UA_TYPES_INT32];
  1428. UA_NODEID_ASSIGN(varAttr.value.typeId, 0, UA_TYPES_IDS[UA_TYPES_INT32]);
  1429. varAttr.value.storage.data.dataPtr = &val;
  1430. varAttr.value.storage.data.arrayLength = 1;
  1431. varAttr.userWriteMask = 41;
  1432. varAttr.specifiedAttributes |= UA_NODEATTRIBUTESMASK_DATATYPE;
  1433. varAttr.specifiedAttributes |= UA_NODEATTRIBUTESMASK_VALUE;
  1434. varAttr.specifiedAttributes |= UA_NODEATTRIBUTESMASK_USERWRITEMASK;
  1435. /* wrap it into a extension object attributes */
  1436. UA_ExtensionObject extensionObject;
  1437. UA_ExtensionObject_init(&extensionObject);
  1438. extensionObject.typeId.identifier.numeric = UA_TYPES_IDS[UA_TYPES_VARIABLEATTRIBUTES];
  1439. UA_Byte extensionData[50];
  1440. extensionObject.body = (UA_ByteString){.data = extensionData, .length=UA_VariableAttributes_calcSizeBinary(&varAttr)};
  1441. size_t posEncode = 0;
  1442. UA_VariableAttributes_encodeBinary(&varAttr, &extensionObject.body, &posEncode);
  1443. extensionObject.encoding = UA_EXTENSIONOBJECT_ENCODINGMASK_BODYISBYTESTRING;
  1444. ck_assert_int_eq(posEncode, UA_VariableAttributes_calcSizeBinary(&varAttr));
  1445. UA_Byte data[50];
  1446. UA_ByteString dst = {.data = data, .length=50};
  1447. posEncode = 0;
  1448. UA_ExtensionObject_encodeBinary(&extensionObject, &dst, &posEncode);
  1449. UA_ExtensionObject extensionObjectDecoded;
  1450. size_t posDecode = 0;
  1451. UA_ExtensionObject_decodeBinary(&dst, &posDecode, &extensionObjectDecoded);
  1452. ck_assert_int_eq(posEncode, posDecode);
  1453. ck_assert_int_eq(extensionObjectDecoded.body.length, extensionObject.body.length);
  1454. UA_VariableAttributes varAttrDecoded;
  1455. UA_VariableAttributes_init(&varAttrDecoded);
  1456. posDecode = 0;
  1457. UA_VariableAttributes_decodeBinary(&extensionObjectDecoded.body, &posDecode, &varAttrDecoded);
  1458. ck_assert_uint_eq(41, varAttrDecoded.userWriteMask);
  1459. ck_assert_int_eq(1, varAttrDecoded.value.storage.data.arrayLength);
  1460. // finally
  1461. UA_ExtensionObject_deleteMembers(&extensionObjectDecoded);
  1462. UA_Variant_deleteMembers(&varAttrDecoded.value);
  1463. }
  1464. END_TEST
  1465. static Suite *testSuite_builtin(void) {
  1466. Suite *s = suite_create("Built-in Data Types 62541-6 Table 1");
  1467. TCase *tc_calcSize = tcase_create("calcSize");
  1468. tcase_add_test(tc_calcSize, UA_ExtensionObject_calcSizeShallWorkOnExample);
  1469. tcase_add_test(tc_calcSize, UA_DataValue_calcSizeShallWorkOnExample);
  1470. tcase_add_test(tc_calcSize, UA_DiagnosticInfo_calcSizeShallWorkOnExample);
  1471. tcase_add_test(tc_calcSize, UA_String_calcSizeShallReturnEncodingSize);
  1472. tcase_add_test(tc_calcSize, UA_String_calcSizeWithNegativLengthShallReturnEncodingSize);
  1473. tcase_add_test(tc_calcSize, UA_String_calcSizeWithNegativLengthAndValidPointerShallReturnEncodingSize);
  1474. tcase_add_test(tc_calcSize, UA_String_calcSizeWithZeroLengthShallReturnEncodingSize);
  1475. tcase_add_test(tc_calcSize, UA_String_calcSizeWithZeroLengthAndValidPointerShallReturnEncodingSize);
  1476. tcase_add_test(tc_calcSize, UA_NodeId_calcSizeEncodingTwoByteShallReturnEncodingSize);
  1477. tcase_add_test(tc_calcSize, UA_NodeId_calcSizeEncodingFourByteShallReturnEncodingSize);
  1478. tcase_add_test(tc_calcSize, UA_NodeId_calcSizeEncodingStringShallReturnEncodingSize);
  1479. tcase_add_test(tc_calcSize, UA_NodeId_calcSizeEncodingStringNegativLengthShallReturnEncodingSize);
  1480. tcase_add_test(tc_calcSize, UA_NodeId_calcSizeEncodingStringZeroLengthShallReturnEncodingSize);
  1481. tcase_add_test(tc_calcSize, UA_ExpandedNodeId_calcSizeEncodingStringAndServerIndexShallReturnEncodingSize);
  1482. tcase_add_test(tc_calcSize, UA_ExpandedNodeId_calcSizeEncodingStringAndNamespaceUriShallReturnEncodingSize);
  1483. tcase_add_test(tc_calcSize, UA_Guid_calcSizeShallReturnEncodingSize);
  1484. tcase_add_test(tc_calcSize, UA_Guid_calcSizeShallReturnEncodingSize);
  1485. tcase_add_test(tc_calcSize, UA_LocalizedText_calcSizeTextOnlyShallReturnEncodingSize);
  1486. tcase_add_test(tc_calcSize, UA_LocalizedText_calcSizeLocaleOnlyShallReturnEncodingSize);
  1487. tcase_add_test(tc_calcSize, UA_LocalizedText_calcSizeTextAndLocaleShallReturnEncodingSize);
  1488. tcase_add_test(tc_calcSize, UA_Variant_calcSizeFixedSizeArrayShallReturnEncodingSize);
  1489. tcase_add_test(tc_calcSize, UA_Variant_calcSizeVariableSizeArrayShallReturnEncodingSize);
  1490. tcase_add_test(tc_calcSize, UA_Variant_decodeWithOutDeleteMembersShallFailInCheckMem);
  1491. suite_add_tcase(s, tc_calcSize);
  1492. TCase *tc_decode = tcase_create("decode");
  1493. tcase_add_test(tc_decode, UA_Byte_decodeShallCopyAndAdvancePosition);
  1494. tcase_add_test(tc_decode, UA_Byte_decodeShallModifyOnlyCurrentPosition);
  1495. tcase_add_test(tc_decode, UA_Int16_decodeShallAssumeLittleEndian);
  1496. tcase_add_test(tc_decode, UA_Int16_decodeShallRespectSign);
  1497. tcase_add_test(tc_decode, UA_UInt16_decodeShallNotRespectSign);
  1498. tcase_add_test(tc_decode, UA_Int32_decodeShallAssumeLittleEndian);
  1499. tcase_add_test(tc_decode, UA_Int32_decodeShallRespectSign);
  1500. tcase_add_test(tc_decode, UA_UInt32_decodeShallNotRespectSign);
  1501. tcase_add_test(tc_decode, UA_UInt64_decodeShallNotRespectSign);
  1502. tcase_add_test(tc_decode, UA_Int64_decodeShallRespectSign);
  1503. tcase_add_test(tc_decode, UA_Float_decodeShallWorkOnExample);
  1504. tcase_add_test(tc_decode, UA_Double_decodeShallGiveOne);
  1505. tcase_add_test(tc_decode, UA_Double_decodeShallGiveZero);
  1506. tcase_add_test(tc_decode, UA_Double_decodeShallGiveMinusTwo);
  1507. tcase_add_test(tc_decode, UA_Byte_encode_test);
  1508. tcase_add_test(tc_decode, UA_String_decodeShallAllocateMemoryAndCopyString);
  1509. tcase_add_test(tc_decode, UA_String_decodeWithNegativeSizeShallNotAllocateMemoryAndNullPtr);
  1510. tcase_add_test(tc_decode, UA_String_decodeWithZeroSizeShallNotAllocateMemoryAndNullPtr);
  1511. tcase_add_test(tc_decode, UA_NodeId_decodeTwoByteShallReadTwoBytesAndSetNamespaceToZero);
  1512. tcase_add_test(tc_decode, UA_NodeId_decodeFourByteShallReadFourBytesAndRespectNamespace);
  1513. tcase_add_test(tc_decode, UA_NodeId_decodeStringShallAllocateMemory);
  1514. tcase_add_test(tc_decode, UA_Variant_decodeWithOutArrayFlagSetShallSetVTAndAllocateMemoryForArray);
  1515. tcase_add_test(tc_decode, UA_Variant_decodeWithArrayFlagSetShallSetVTAndAllocateMemoryForArray);
  1516. tcase_add_test(tc_decode, UA_Variant_decodeWithOutDeleteMembersShallFailInCheckMem);
  1517. tcase_add_test(tc_decode, UA_Variant_decodeWithTooSmallSourceShallReturnWithError);
  1518. suite_add_tcase(s, tc_decode);
  1519. TCase *tc_encode = tcase_create("encode");
  1520. tcase_add_test(tc_encode, UA_Byte_encode_test);
  1521. tcase_add_test(tc_encode, UA_UInt16_encodeNegativeShallEncodeLittleEndian);
  1522. tcase_add_test(tc_encode, UA_UInt16_encodeShallEncodeLittleEndian);
  1523. tcase_add_test(tc_encode, UA_UInt32_encodeShallEncodeLittleEndian);
  1524. tcase_add_test(tc_encode, UA_Int32_encodeShallEncodeLittleEndian);
  1525. tcase_add_test(tc_encode, UA_Int32_encodeNegativeShallEncodeLittleEndian);
  1526. tcase_add_test(tc_encode, UA_UInt64_encodeShallWorkOnExample);
  1527. tcase_add_test(tc_encode, UA_Int64_encodeNegativeShallEncodeLittleEndian);
  1528. tcase_add_test(tc_encode, UA_Int64_encodeShallEncodeLittleEndian);
  1529. tcase_add_test(tc_encode, UA_Float_encodeShallWorkOnExample);
  1530. //tcase_add_test(tc_encode, UA_Double_encodeShallWorkOnExample);
  1531. tcase_add_test(tc_encode, UA_String_encodeShallWorkOnExample);
  1532. tcase_add_test(tc_encode, UA_DataValue_encodeShallWorkOnExampleWithoutVariant);
  1533. tcase_add_test(tc_encode, UA_DataValue_encodeShallWorkOnExampleWithVariant);
  1534. tcase_add_test(tc_encode, UA_ExtensionObject_encodeDecodeShallWorkOnExtensionObject);
  1535. suite_add_tcase(s, tc_encode);
  1536. TCase *tc_convert = tcase_create("convert");
  1537. tcase_add_test(tc_convert, UA_DateTime_toStructShallWorkOnExample);
  1538. tcase_add_test(tc_convert, UA_DateTime_toStringShallWorkOnExample);
  1539. suite_add_tcase(s, tc_convert);
  1540. TCase *tc_copy = tcase_create("copy");
  1541. tcase_add_test(tc_copy, UA_Array_copyByteArrayShallWorkOnExample);
  1542. tcase_add_test(tc_copy, UA_Array_copyUA_StringShallWorkOnExample);
  1543. tcase_add_test(tc_copy, UA_ExtensionObject_copyShallWorkOnExample);
  1544. tcase_add_test(tc_copy, UA_Variant_copyShallWorkOnSingleValueExample);
  1545. tcase_add_test(tc_copy, UA_Variant_copyShallWorkOn1DArrayExample);
  1546. tcase_add_test(tc_copy, UA_Variant_copyShallWorkOn2DArrayExample);
  1547. tcase_add_test(tc_copy, UA_DiagnosticInfo_copyShallWorkOnExample);
  1548. tcase_add_test(tc_copy, UA_ApplicationDescription_copyShallWorkOnExample);
  1549. tcase_add_test(tc_copy, UA_QualifiedName_copyShallWorkOnInputExample);
  1550. tcase_add_test(tc_copy, UA_Guid_copyShallWorkOnInputExample);
  1551. tcase_add_test(tc_copy, UA_LocalizedText_copycstringShallWorkOnInputExample);
  1552. tcase_add_test(tc_copy, UA_DataValue_copyShallWorkOnInputExample);
  1553. suite_add_tcase(s, tc_copy);
  1554. return s;
  1555. }
  1556. int main(void) {
  1557. int number_failed = 0;
  1558. Suite *s;
  1559. SRunner *sr;
  1560. s = testSuite_builtin();
  1561. sr = srunner_create(s);
  1562. //srunner_set_fork_status(sr, CK_NOFORK);
  1563. srunner_run_all(sr, CK_NORMAL);
  1564. number_failed += srunner_ntests_failed(sr);
  1565. srunner_free(sr);
  1566. return (number_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
  1567. }