check_builtin.c 53 KB

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