check_builtin.c 53 KB

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