check_builtin.c 56 KB

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