check_builtin.c 61 KB

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