check_builtin.c 58 KB

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