check_builtin.c 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413
  1. /*
  2. ============================================================================
  3. Name : check_stack.c
  4. Author :
  5. Copyright : Your copyright notice
  6. Description :
  7. ============================================================================
  8. */
  9. #include <stdio.h>
  10. #include <stdlib.h>
  11. #include "opcua.h"
  12. #include "check.h"
  13. START_TEST(UA_Boolean_calcSizeWithNullArgumentShallReturnStorageSize)
  14. {
  15. // given
  16. UA_Boolean* arg = UA_NULL;
  17. // when
  18. UA_Boolean storageSize = UA_Boolean_calcSize(arg);
  19. // then
  20. ck_assert_int_eq(storageSize, 1);
  21. }
  22. END_TEST
  23. START_TEST(UA_SByte_calcSizeWithNullArgumentShallReturnStorageSize)
  24. {
  25. // given
  26. UA_SByte* arg = UA_NULL;
  27. // when
  28. UA_Int32 storageSize = UA_SByte_calcSize(arg);
  29. // then
  30. ck_assert_int_ge(storageSize, 1);
  31. }
  32. END_TEST
  33. START_TEST(UA_Byte_calcSizeWithNullArgumentShallReturnStorageSize)
  34. {
  35. // given
  36. UA_Byte* arg = UA_NULL;
  37. // when
  38. UA_Int32 storageSize = UA_Byte_calcSize(arg);
  39. // then
  40. ck_assert_int_ge(storageSize, 1);
  41. }
  42. END_TEST
  43. START_TEST(UA_Int16_calcSizeWithNullArgumentShallReturnStorageSize)
  44. {
  45. // given
  46. UA_Int16* arg = UA_NULL;
  47. // when
  48. UA_Int32 storageSize = UA_Int16_calcSize(arg);
  49. // then
  50. ck_assert_int_ge(storageSize, 2);
  51. }
  52. END_TEST
  53. START_TEST(UA_UInt16_calcSizeWithNullArgumentShallReturnStorageSize)
  54. {
  55. // given
  56. UA_UInt16* arg = UA_NULL;
  57. // when
  58. UA_Int32 storageSize = UA_UInt16_calcSize(arg);
  59. // then
  60. ck_assert_int_ge(storageSize, 2);
  61. }
  62. END_TEST
  63. START_TEST(UA_Int32_calcSizeWithNullArgumentShallReturnStorageSize)
  64. {
  65. // given
  66. UA_Int32* arg = UA_NULL;
  67. // when
  68. UA_Int32 storageSize = UA_Int32_calcSize(arg);
  69. // then
  70. ck_assert_int_ge(storageSize, 4);
  71. }
  72. END_TEST
  73. START_TEST(UA_UInt32_calcSizeWithNullArgumentShallReturnStorageSize)
  74. {
  75. // given
  76. UA_UInt32* arg = UA_NULL;
  77. // when
  78. UA_Int32 storageSize = UA_UInt32_calcSize(arg);
  79. // then
  80. ck_assert_int_ge(storageSize, 4);
  81. }
  82. END_TEST
  83. START_TEST(UA_Int64_calcSizeWithNullArgumentShallReturnStorageSize)
  84. {
  85. // given
  86. UA_Int64* arg = UA_NULL;
  87. // when
  88. UA_Int32 storageSize = UA_Int64_calcSize(arg);
  89. // then
  90. ck_assert_int_ge(storageSize, 8);
  91. }
  92. END_TEST
  93. START_TEST(UA_UInt64_calcSizeWithNullArgumentShallReturnStorageSize)
  94. {
  95. // given
  96. UA_UInt64* arg = UA_NULL;
  97. // when
  98. UA_Int32 storageSize = UA_UInt64_calcSize(arg);
  99. // then
  100. ck_assert_int_ge(storageSize, 8);
  101. }
  102. END_TEST
  103. START_TEST(UA_Float_calcSizeWithNullArgumentShallReturnStorageSize)
  104. {
  105. // given
  106. UA_Float* arg = UA_NULL;
  107. // when
  108. UA_Int32 storageSize = UA_Float_calcSize(arg);
  109. // then
  110. ck_assert_int_ge(storageSize, 4);
  111. }
  112. END_TEST
  113. START_TEST(UA_Double_calcSizeWithNullArgumentShallReturnStorageSize)
  114. {
  115. // given
  116. UA_Double* arg = UA_NULL;
  117. // when
  118. UA_Int32 storageSize = UA_Double_calcSize(arg);
  119. // then
  120. ck_assert_int_ge(storageSize, 8);
  121. }
  122. END_TEST
  123. START_TEST(UA_String_calcSizeWithNullArgumentShallReturnStorageSize)
  124. {
  125. // given
  126. UA_String* arg = UA_NULL;
  127. // when
  128. UA_Int32 storageSize = UA_String_calcSize(arg);
  129. // then
  130. ck_assert_int_eq(storageSize, sizeof(UA_String));
  131. ck_assert_int_ge(storageSize, UA_Int32_calcSize(UA_NULL) + sizeof(UA_NULL));
  132. }
  133. END_TEST
  134. START_TEST(UA_DateTime_calcSizeWithNullArgumentShallReturnStorageSize)
  135. {
  136. // given
  137. UA_DateTime* arg = UA_NULL;
  138. // when
  139. UA_Int32 storageSize = UA_DateTime_calcSize(arg);
  140. // then
  141. ck_assert_int_ge(storageSize, 8);
  142. }
  143. END_TEST
  144. START_TEST(UA_Guid_calcSizeWithNullArgumentShallReturnStorageSize)
  145. {
  146. // given
  147. UA_Guid* arg = UA_NULL;
  148. // when
  149. UA_Int32 storageSize = UA_Guid_calcSize(arg);
  150. // then
  151. ck_assert_int_ge(storageSize, 16);
  152. }
  153. END_TEST
  154. START_TEST(UA_ByteString_calcSizeWithNullArgumentShallReturnStorageSize)
  155. {
  156. // given
  157. UA_ByteString* arg = UA_NULL;
  158. // when
  159. UA_Int32 storageSize = UA_ByteString_calcSize(arg);
  160. // then
  161. ck_assert_int_eq(storageSize, sizeof(UA_ByteString));
  162. ck_assert_int_ge(storageSize, UA_Int32_calcSize(UA_NULL)+sizeof(UA_NULL));
  163. }
  164. END_TEST
  165. START_TEST(UA_XmlElement_calcSizeWithNullArgumentShallReturnStorageSize)
  166. {
  167. // given
  168. UA_XmlElement* arg = UA_NULL;
  169. // when
  170. UA_Int32 storageSize = UA_XmlElement_calcSize(arg);
  171. // then
  172. ck_assert_int_eq(storageSize, sizeof(UA_XmlElement));
  173. ck_assert_int_ge(storageSize, UA_Int32_calcSize(UA_NULL)+sizeof(UA_NULL));
  174. }
  175. END_TEST
  176. START_TEST(UA_NodeId_calcSizeWithNullArgumentShallReturnStorageSize)
  177. {
  178. // given
  179. UA_NodeId* arg = UA_NULL;
  180. // when
  181. UA_Int32 storageSize = UA_NodeId_calcSize(arg);
  182. // then
  183. ck_assert_int_eq(storageSize, sizeof(UA_NodeId));
  184. }
  185. END_TEST
  186. START_TEST(UA_ExpandedNodeId_calcSizeShallWorkOnExample)
  187. {
  188. //ToDo: Function needs to be filled
  189. UA_Int32 valreal = 300;
  190. UA_Int32 valcalc = 0;
  191. ck_assert_int_eq(valcalc,valreal);
  192. }
  193. END_TEST
  194. START_TEST(UA_ExpandedNodeId_calcSizeWithNullArgumentShallReturnStorageSize)
  195. {
  196. // given
  197. UA_ExpandedNodeId* arg = UA_NULL;
  198. // when
  199. UA_Int32 storageSize = UA_ExpandedNodeId_calcSize(arg);
  200. // then
  201. ck_assert_int_eq(storageSize, sizeof(UA_ExpandedNodeId));
  202. }
  203. END_TEST
  204. START_TEST(UA_StatusCode_calcSizeWithNullArgumentShallReturnStorageSize)
  205. {
  206. // given
  207. UA_StatusCode* arg = UA_NULL;
  208. // when
  209. UA_Int32 storageSize = UA_StatusCode_calcSize(arg);
  210. // then
  211. ck_assert_int_eq(storageSize, sizeof(UA_StatusCode));
  212. }
  213. END_TEST
  214. START_TEST(UA_QualifiedName_calcSizeWithNullArgumentShallReturnStorageSize)
  215. {
  216. // given
  217. UA_QualifiedName* arg = UA_NULL;
  218. // when
  219. UA_Int32 storageSize = UA_QualifiedName_calcSize(arg);
  220. // then
  221. ck_assert_int_eq(storageSize, sizeof(UA_QualifiedName));
  222. }
  223. END_TEST
  224. START_TEST(UA_LocalizedText_calcSizeWithNullArgumentShallReturnStorageSize)
  225. {
  226. // given
  227. UA_LocalizedText* arg = UA_NULL;
  228. // when
  229. UA_Int32 storageSize = UA_LocalizedText_calcSize(arg);
  230. // then
  231. ck_assert_int_eq(storageSize, sizeof(UA_LocalizedText));
  232. }
  233. END_TEST
  234. START_TEST(UA_ExtensionObject_calcSizeWithNullArgumentShallReturnStorageSize)
  235. {
  236. // given
  237. UA_ExtensionObject* arg = UA_NULL;
  238. // when
  239. UA_Int32 storageSize = UA_ExtensionObject_calcSize(arg);
  240. // then
  241. ck_assert_int_eq(storageSize, sizeof(UA_ExtensionObject));
  242. }
  243. END_TEST
  244. START_TEST(UA_ExtensionObject_calcSizeShallWorkOnExample)
  245. {
  246. // given
  247. UA_Byte data[3] = {1,2,3};
  248. UA_ExtensionObject extensionObject;
  249. // empty ExtensionObject, handcoded
  250. // when
  251. extensionObject.typeId.encodingByte = UA_NODEIDTYPE_TWOBYTE;
  252. extensionObject.typeId.identifier.numeric = 0;
  253. extensionObject.encoding = UA_EXTENSIONOBJECT_ENCODINGMASKTYPE_NOBODYISENCODED;
  254. // then
  255. ck_assert_int_eq(UA_ExtensionObject_calcSize(&extensionObject), 1 + 1 + 1);
  256. // ExtensionObject with ByteString-Body
  257. // when
  258. extensionObject.encoding = UA_EXTENSIONOBJECT_ENCODINGMASKTYPE_BODYISBYTESTRING;
  259. extensionObject.body.data = data;
  260. extensionObject.body.length = 3;
  261. // then
  262. ck_assert_int_eq(UA_ExtensionObject_calcSize(&extensionObject), 3 + 4 + 3);
  263. }
  264. END_TEST
  265. START_TEST(UA_DataValue_calcSizeShallWorkOnExample)
  266. {
  267. // given
  268. UA_DataValue dataValue;
  269. dataValue.encodingMask = UA_DATAVALUE_STATUSCODE | UA_DATAVALUE_SOURCETIMESTAMP | UA_DATAVALUE_SOURCEPICOSECONDS;
  270. dataValue.status = 12;
  271. UA_DateTime dateTime;
  272. dateTime = 80;
  273. dataValue.sourceTimestamp = dateTime;
  274. UA_DateTime sourceTime;
  275. sourceTime = 214;
  276. dataValue.sourcePicoseconds = sourceTime;
  277. int size = 0;
  278. // when
  279. size = UA_DataValue_calcSize(&dataValue);
  280. // then
  281. ck_assert_int_eq(size, 21);
  282. }
  283. END_TEST
  284. START_TEST(UA_DataValue_calcSizeWithNullArgumentShallReturnStorageSize)
  285. {
  286. // given
  287. UA_DataValue* arg = UA_NULL;
  288. // when
  289. UA_Int32 storageSize = UA_DataValue_calcSize(arg);
  290. // then
  291. ck_assert_int_eq(storageSize, sizeof(UA_DataValue));
  292. }
  293. END_TEST
  294. START_TEST(UA_Variant_calcSizeWithNullArgumentShallReturnStorageSize)
  295. {
  296. // given
  297. UA_Variant* arg = UA_NULL;
  298. // when
  299. UA_Int32 storageSize = UA_Variant_calcSize(arg);
  300. // then
  301. ck_assert_int_eq(storageSize, sizeof(UA_Variant));
  302. }
  303. END_TEST
  304. START_TEST(UA_DiagnosticInfo_calcSizeShallWorkOnExample)
  305. {
  306. // given
  307. UA_DiagnosticInfo diagnosticInfo;
  308. diagnosticInfo.encodingMask = 0x01 | 0x02 | 0x04 | 0x08 | 0x10;
  309. diagnosticInfo.symbolicId = 30;
  310. diagnosticInfo.namespaceUri = 25;
  311. diagnosticInfo.localizedText = 22;
  312. UA_Byte additionalInfoData = 'd';
  313. diagnosticInfo.additionalInfo.data = &additionalInfoData;//"OPCUA";
  314. diagnosticInfo.additionalInfo.length = 5;
  315. // when & then
  316. ck_assert_int_eq(UA_DiagnosticInfo_calcSize(&diagnosticInfo),26);
  317. }
  318. END_TEST
  319. START_TEST(UA_DiagnosticInfo_calcSizeWithNullArgumentShallReturnStorageSize)
  320. {
  321. // given
  322. UA_DiagnosticInfo* arg = UA_NULL;
  323. // when
  324. UA_Int32 storageSize = UA_DiagnosticInfo_calcSize(arg);
  325. // then
  326. ck_assert_int_eq(storageSize, sizeof(UA_DiagnosticInfo));
  327. }
  328. END_TEST
  329. START_TEST(UA_String_calcSizeWithNegativLengthShallReturnEncodingSize)
  330. {
  331. // given
  332. UA_String arg = {-1, UA_NULL};
  333. // when
  334. UA_Int32 encodingSize = UA_String_calcSize(&arg);
  335. // then
  336. ck_assert_int_eq(encodingSize, 4);
  337. }
  338. END_TEST
  339. START_TEST(UA_String_calcSizeWithNegativLengthAndValidPointerShallReturnEncodingSize)
  340. {
  341. // given
  342. UA_String arg = {-1, (UA_Byte*) "OPC"};
  343. // when
  344. UA_Int32 encodingSize = UA_String_calcSize(&arg);
  345. // then
  346. ck_assert_int_eq(encodingSize, 4);
  347. }
  348. END_TEST
  349. START_TEST(UA_String_calcSizeWithZeroLengthShallReturnEncodingSize)
  350. {
  351. // given
  352. UA_String arg = {0, UA_NULL};
  353. // when
  354. UA_Int32 encodingSize = UA_String_calcSize(&arg);
  355. // then
  356. ck_assert_int_eq(encodingSize, 4);
  357. }
  358. END_TEST
  359. START_TEST(UA_String_calcSizeWithZeroLengthAndValidPointerShallReturnEncodingSize)
  360. {
  361. // given
  362. UA_String arg = {0, (UA_Byte*) "OPC"};
  363. // when
  364. UA_Int32 encodingSize = UA_String_calcSize(&arg);
  365. // then
  366. ck_assert_int_eq(encodingSize, 4);
  367. }
  368. END_TEST
  369. START_TEST(UA_String_calcSizeShallReturnEncodingSize)
  370. {
  371. // given
  372. UA_String arg = {3, (UA_Byte*) "OPC"};
  373. // when
  374. UA_Int32 encodingSize = UA_String_calcSize(&arg);
  375. // then
  376. ck_assert_int_eq(encodingSize, 4+3);
  377. }
  378. END_TEST
  379. START_TEST(UA_NodeId_calcSizeEncodingTwoByteShallReturnEncodingSize)
  380. {
  381. // given
  382. UA_NodeId arg;
  383. arg.encodingByte = UA_NODEIDTYPE_TWOBYTE;
  384. // when
  385. UA_Int32 encodingSize = UA_NodeId_calcSize(&arg);
  386. // then
  387. ck_assert_int_eq(encodingSize, 2);
  388. }
  389. END_TEST
  390. START_TEST(UA_NodeId_calcSizeEncodingFourByteShallReturnEncodingSize)
  391. {
  392. // given
  393. UA_NodeId arg;
  394. arg.encodingByte = UA_NODEIDTYPE_FOURBYTE;
  395. // when
  396. UA_Int32 encodingSize = UA_NodeId_calcSize(&arg);
  397. // then
  398. ck_assert_int_eq(encodingSize, 4);
  399. }
  400. END_TEST
  401. START_TEST(UA_NodeId_calcSizeEncodingStringShallReturnEncodingSize)
  402. {
  403. // given
  404. UA_NodeId arg;
  405. arg.encodingByte = UA_NODEIDTYPE_STRING;
  406. arg.identifier.string.length = 3;
  407. arg.identifier.string.data = (UA_Byte*) "PLT";
  408. // when
  409. UA_Int32 encodingSize = UA_NodeId_calcSize(&arg);
  410. // then
  411. ck_assert_int_eq(encodingSize, 1+2+4+3);
  412. }
  413. END_TEST
  414. START_TEST(UA_NodeId_calcSizeEncodingStringNegativLengthShallReturnEncodingSize)
  415. {
  416. // given
  417. UA_NodeId arg;
  418. arg.encodingByte = UA_NODEIDTYPE_STRING;
  419. arg.identifier.string.length = -1;
  420. // when
  421. UA_Int32 encodingSize = UA_NodeId_calcSize(&arg);
  422. // then
  423. ck_assert_int_eq(encodingSize, 1+2+4+0);
  424. }
  425. END_TEST
  426. START_TEST(UA_NodeId_calcSizeEncodingStringZeroLengthShallReturnEncodingSize)
  427. {
  428. // given
  429. UA_NodeId arg;
  430. arg.encodingByte = UA_NODEIDTYPE_STRING;
  431. arg.identifier.string.length = 0;
  432. // when
  433. UA_Int32 encodingSize = UA_NodeId_calcSize(&arg);
  434. // then
  435. ck_assert_int_eq(encodingSize, 1+2+4+0);
  436. }
  437. END_TEST
  438. START_TEST(UA_ExpandedNodeId_calcSizeEncodingStringAndServerIndexShallReturnEncodingSize)
  439. {
  440. // given
  441. UA_ExpandedNodeId arg;
  442. arg.nodeId.encodingByte = UA_NODEIDTYPE_STRING | UA_NODEIDTYPE_SERVERINDEX_FLAG;
  443. arg.nodeId.identifier.string.length = 3;
  444. // when
  445. UA_Int32 encodingSize = UA_ExpandedNodeId_calcSize(&arg);
  446. // then
  447. ck_assert_int_eq(encodingSize, 1+2+4+3+4);
  448. }
  449. END_TEST
  450. START_TEST(UA_ExpandedNodeId_calcSizeEncodingStringAndNamespaceUriShallReturnEncodingSize)
  451. {
  452. // given
  453. UA_ExpandedNodeId arg;
  454. arg.nodeId.encodingByte = UA_NODEIDTYPE_STRING | UA_NODEIDTYPE_NAMESPACE_URI_FLAG;
  455. arg.nodeId.identifier.string.length = 3;
  456. arg.namespaceUri.length = 7;
  457. // when
  458. UA_Int32 encodingSize = UA_ExpandedNodeId_calcSize(&arg);
  459. // then
  460. ck_assert_int_eq(encodingSize, 1+2+4+3+4+7);
  461. }
  462. END_TEST
  463. START_TEST(UA_Guid_calcSizeShallReturnEncodingSize)
  464. {
  465. // given
  466. UA_Guid arg;
  467. // when
  468. UA_Int32 encodingSize = UA_Guid_calcSize(&arg);
  469. // then
  470. ck_assert_int_eq(encodingSize, 16);
  471. }
  472. END_TEST
  473. START_TEST(UA_LocalizedText_calcSizeTextOnlyShallReturnEncodingSize)
  474. {
  475. // given
  476. UA_LocalizedText arg;
  477. arg.encodingMask = UA_LOCALIZEDTEXT_ENCODINGMASKTYPE_TEXT;
  478. arg.text.length = 42;
  479. // when
  480. UA_Int32 encodingSize = UA_LocalizedText_calcSize(&arg);
  481. // then
  482. ck_assert_int_eq(encodingSize, 1+4+42);
  483. }
  484. END_TEST
  485. START_TEST(UA_LocalizedText_calcSizeLocaleOnlyShallReturnEncodingSize)
  486. {
  487. // given
  488. UA_LocalizedText arg;
  489. arg.encodingMask = UA_LOCALIZEDTEXT_ENCODINGMASKTYPE_LOCALE;
  490. arg.locale.length = 11;
  491. // when
  492. UA_Int32 encodingSize = UA_LocalizedText_calcSize(&arg);
  493. // then
  494. ck_assert_int_eq(encodingSize, 1+4+11);
  495. }
  496. END_TEST
  497. START_TEST(UA_LocalizedText_calcSizeTextAndLocaleShallReturnEncodingSize)
  498. {
  499. // given
  500. UA_LocalizedText arg;
  501. arg.encodingMask = UA_LOCALIZEDTEXT_ENCODINGMASKTYPE_LOCALE | UA_LOCALIZEDTEXT_ENCODINGMASKTYPE_TEXT;
  502. arg.text.length = 47;
  503. arg.locale.length = 11;
  504. // when
  505. UA_Int32 encodingSize = UA_LocalizedText_calcSize(&arg);
  506. // then
  507. ck_assert_int_eq(encodingSize, 1+4+11+4+47);
  508. }
  509. END_TEST
  510. START_TEST(UA_Variant_calcSizeFixedSizeArrayShallReturnEncodingSize)
  511. {
  512. // given
  513. UA_Variant arg;
  514. arg.encodingMask = UA_INT32_NS0 | UA_VARIANT_ENCODINGMASKTYPE_ARRAY;
  515. arg.vt = &UA_[UA_INT32];
  516. #define ARRAY_LEN 8
  517. arg.arrayLength = ARRAY_LEN;
  518. UA_Int32* data[ARRAY_LEN];
  519. arg.data = (void**) &data;
  520. // when
  521. UA_Int32 encodingSize = UA_Variant_calcSize(&arg);
  522. // then
  523. ck_assert_int_eq(encodingSize, 1+4+ARRAY_LEN*4);
  524. #undef ARRAY_LEN
  525. }
  526. END_TEST
  527. START_TEST(UA_Variant_calcSizeVariableSizeArrayShallReturnEncodingSize)
  528. {
  529. // given
  530. UA_Variant arg;
  531. arg.encodingMask = UA_STRING_NS0 | UA_VARIANT_ENCODINGMASKTYPE_ARRAY;
  532. arg.vt = &UA_[UA_STRING];
  533. #define ARRAY_LEN 3
  534. arg.arrayLength = ARRAY_LEN;
  535. UA_String s1 = {-1, UA_NULL };
  536. UA_String s2 = {3, (UA_Byte*) "PLT" };
  537. UA_String s3 = {47, UA_NULL };
  538. UA_String* data[ARRAY_LEN] = { &s1, &s2, &s3 };
  539. arg.data = (void**) &data;
  540. // when
  541. UA_Int32 encodingSize = UA_Variant_calcSize(&arg);
  542. // then
  543. ck_assert_int_eq(encodingSize, 1+4+(4+0)+(4+3)+(4+47));
  544. #undef ARRAY_LEN
  545. }
  546. END_TEST
  547. START_TEST(UA_Variant_calcSizeVariableSizeArrayWithNullPtrWillReturnWrongButLargeEnoughEncodingSize)
  548. {
  549. // given
  550. UA_Variant arg;
  551. arg.encodingMask = UA_STRING_NS0 | UA_VARIANT_ENCODINGMASKTYPE_ARRAY;
  552. arg.vt = &UA_[UA_STRING];
  553. #define ARRAY_LEN 6
  554. arg.arrayLength = ARRAY_LEN;
  555. UA_String s1 = {-1, UA_NULL };
  556. UA_String s2 = {3, (UA_Byte*) "PLT" };
  557. UA_String s3 = {47, UA_NULL };
  558. UA_String* data[ARRAY_LEN] = { &s1, &s2, &s3 }; // will be filled with null-ptrs
  559. arg.data = (void**) &data;
  560. // when
  561. UA_Int32 encodingSize = UA_Variant_calcSize(&arg);
  562. // then
  563. ck_assert_int_ge(encodingSize, 1+4+(4+0)+(4+3)+(4+47)+(ARRAY_LEN-3)*(4+0));
  564. #undef ARRAY_LEN
  565. }
  566. END_TEST
  567. START_TEST(UA_Byte_decodeShallCopyAndAdvancePosition)
  568. {
  569. // given
  570. UA_Byte dst;
  571. UA_Byte src[] = { 0x08 };
  572. UA_Int32 pos = 0;
  573. // when
  574. UA_Int32 retval = UA_Byte_decode(src, &pos, &dst);
  575. // then
  576. ck_assert_int_eq(retval, UA_SUCCESS);
  577. ck_assert_uint_eq(pos, 1);
  578. ck_assert_uint_eq(dst, 0x08);
  579. }
  580. END_TEST
  581. START_TEST(UA_Byte_decodeShallModifyOnlyCurrentPosition)
  582. {
  583. // given
  584. UA_Byte dst[] = { 0xFF, 0xFF, 0xFF };
  585. UA_Byte src[] = { 0x08 };
  586. UA_Int32 pos = 0;
  587. // when
  588. UA_Int32 retval = UA_Byte_decode(src, &pos, &dst[1]);
  589. // then
  590. ck_assert_int_eq(retval, UA_SUCCESS);
  591. ck_assert_int_eq(pos, 1);
  592. ck_assert_uint_eq(dst[0], 0xFF);
  593. ck_assert_uint_eq(dst[1], 0x08);
  594. ck_assert_uint_eq(dst[2], 0xFF);
  595. }
  596. END_TEST
  597. START_TEST(UA_Int16_decodeShallAssumeLittleEndian)
  598. {
  599. // given
  600. UA_Int32 pos = 0;
  601. UA_Byte src[] = {
  602. 0x01,0x00, // 1
  603. 0x00,0x01, // 256
  604. };
  605. // when
  606. UA_Int16 val_01_00, val_00_01;
  607. UA_Int32 retval = UA_Int16_decode(src,&pos,&val_01_00);
  608. retval |= UA_Int16_decode(src,&pos,&val_00_01);
  609. // then
  610. ck_assert_int_eq(retval,UA_SUCCESS);
  611. ck_assert_int_eq(val_01_00,1);
  612. ck_assert_int_eq(val_00_01,256);
  613. ck_assert_int_eq(pos,4);
  614. }
  615. END_TEST
  616. START_TEST(UA_Int16_decodeShallRespectSign)
  617. {
  618. // given
  619. UA_Int32 pos = 0;
  620. UA_Byte src[] = {
  621. 0xFF,0xFF, // -1
  622. 0x00,0x80, // -32768
  623. };
  624. // when
  625. UA_Int16 val_ff_ff, val_00_80;
  626. UA_Int32 retval = UA_Int16_decode(src,&pos,&val_ff_ff);
  627. retval |= UA_Int16_decode(src,&pos,&val_00_80);
  628. // then
  629. ck_assert_int_eq(retval,UA_SUCCESS);
  630. ck_assert_int_eq(val_ff_ff,-1);
  631. ck_assert_int_eq(val_00_80,-32768);
  632. }
  633. END_TEST
  634. START_TEST(UA_UInt16_decodeShallNotRespectSign)
  635. {
  636. // given
  637. UA_Int32 pos = 0;
  638. UA_Byte src[] = {
  639. 0xFF,0xFF, // (2^16)-1
  640. 0x00,0x80, // (2^15)
  641. };
  642. // when
  643. UA_UInt16 val_ff_ff, val_00_80;
  644. UA_Int32 retval = UA_UInt16_decode(src,&pos,&val_ff_ff);
  645. retval |= UA_UInt16_decode(src,&pos,&val_00_80);
  646. // then
  647. ck_assert_int_eq(retval,UA_SUCCESS);
  648. ck_assert_int_eq(pos,4);
  649. ck_assert_uint_eq(val_ff_ff, (0x01 << 16)-1);
  650. ck_assert_uint_eq(val_00_80, (0x01 << 15));
  651. }
  652. END_TEST
  653. START_TEST(UA_Int32_decodeShallAssumeLittleEndian)
  654. {
  655. // given
  656. UA_Int32 pos = 0;
  657. UA_Byte src[] = {
  658. 0x01,0x00,0x00,0x00, // 1
  659. 0x00,0x01,0x00,0x00 // 256
  660. };
  661. // when
  662. UA_Int32 val_01_00, val_00_01;
  663. UA_Int32 retval = UA_Int32_decode(src,&pos,&val_01_00);
  664. retval |= UA_Int32_decode(src,&pos,&val_00_01);
  665. // then
  666. ck_assert_int_eq(retval,UA_SUCCESS);
  667. ck_assert_int_eq(val_01_00,1);
  668. ck_assert_int_eq(val_00_01,256);
  669. ck_assert_int_eq(pos,8);
  670. }
  671. END_TEST
  672. START_TEST(UA_Int32_decodeShallRespectSign)
  673. {
  674. // given
  675. UA_Int32 pos = 0;
  676. UA_Byte src[] = {
  677. 0xFF,0xFF,0xFF,0xFF, // -1
  678. 0x00,0x80,0xFF,0xFF // -32768
  679. };
  680. // when
  681. UA_Int32 val_ff_ff, val_00_80;
  682. UA_Int32 retval = UA_Int32_decode(src,&pos,&val_ff_ff);
  683. retval |= UA_Int32_decode(src,&pos,&val_00_80);
  684. // then
  685. ck_assert_int_eq(retval,UA_SUCCESS);
  686. ck_assert_int_eq(val_ff_ff,-1);
  687. ck_assert_int_eq(val_00_80,-32768);
  688. }
  689. END_TEST
  690. START_TEST(UA_UInt32_decodeShallNotRespectSign)
  691. {
  692. // given
  693. UA_Int32 pos = 0;
  694. UA_Byte src[] = {
  695. 0xFF,0xFF,0xFF,0xFF, // (2^32)-1
  696. 0x00,0x00,0x00,0x80 // (2^31)
  697. };
  698. // when
  699. UA_UInt32 val_ff_ff, val_00_80;
  700. UA_Int32 retval = UA_UInt32_decode(src,&pos,&val_ff_ff);
  701. retval |= UA_UInt32_decode(src,&pos,&val_00_80);
  702. // then
  703. ck_assert_int_eq(retval,UA_SUCCESS);
  704. ck_assert_int_eq(pos,8);
  705. ck_assert_uint_eq(val_ff_ff, (UA_UInt32) ( (0x01LL << 32 ) - 1 ));
  706. ck_assert_uint_eq(val_00_80, (UA_UInt32) (0x01 << 31));
  707. }
  708. END_TEST
  709. START_TEST(UA_UInt64_decodeShallNotRespectSign)
  710. {
  711. // given
  712. UA_ByteString rawMessage;
  713. UA_UInt64 expectedVal = 0xFF;
  714. expectedVal = expectedVal << 56;
  715. UA_Byte mem[8] = {00,00,00,00,0x00,0x00,0x00,0xFF};
  716. rawMessage.data = mem;
  717. rawMessage.length = 8;
  718. UA_Int32 p = 0;
  719. UA_UInt64 val;
  720. // when
  721. UA_UInt64_decode(rawMessage.data, &p, &val);
  722. // then
  723. ck_assert_uint_eq(val, expectedVal);
  724. }
  725. END_TEST
  726. START_TEST(UA_Int64_decodeShallRespectSign)
  727. {
  728. // given
  729. UA_ByteString rawMessage;
  730. UA_Int64 expectedVal = 0xFF;
  731. expectedVal = expectedVal << 56;
  732. UA_Byte mem[8] = {00,00,00,00,0x00,0x00,0x00,0xFF};
  733. rawMessage.data = mem;
  734. rawMessage.length = 8;
  735. UA_Int32 p = 0;
  736. UA_Int64 val;
  737. // when
  738. UA_Int64_decode(rawMessage.data, &p, &val);
  739. //then
  740. ck_assert_uint_eq(val, expectedVal);
  741. }
  742. END_TEST
  743. START_TEST(UA_Float_decodeShallWorkOnExample)
  744. {
  745. // given
  746. UA_Int32 pos = 0;
  747. UA_Byte src[] = { 0x00,0x00,0xD0,0xC0 }; // -6.5
  748. UA_Float dst;
  749. // when
  750. UA_Int32 retval = UA_Float_decode(src,&pos,&dst);
  751. // then
  752. ck_assert_int_eq(retval,UA_SUCCESS);
  753. ck_assert_int_eq(pos,4);
  754. ck_assert(-6.5000001 < dst);
  755. ck_assert(dst < -6.49999999999);
  756. }
  757. END_TEST
  758. START_TEST(UA_Double_decodeShallGiveOne)
  759. {
  760. // given
  761. UA_Int32 pos = 0;
  762. UA_Byte src[] = { 0x00,0x00,0x00,0x00,0x00,0x00,0xF0,0x3F }; // 1
  763. UA_Double dst;
  764. // when
  765. UA_Int32 retval = UA_Double_decode(src,&pos,&dst);
  766. // then
  767. ck_assert_int_eq(retval,UA_SUCCESS);
  768. ck_assert_int_eq(pos,8);
  769. printf("UA_Double_decodeShallGiveOne %f\n",dst);
  770. ck_assert(0.9999999 < dst);
  771. ck_assert(dst < 1.00000000001);
  772. }
  773. END_TEST
  774. START_TEST(UA_Double_decodeShallGiveZero)
  775. {
  776. // given
  777. UA_Int32 pos = 0;
  778. UA_Byte src[] = { 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 }; // 1
  779. UA_Double dst;
  780. // when
  781. UA_Int32 retval = UA_Double_decode(src,&pos,&dst);
  782. // then
  783. ck_assert_int_eq(retval,UA_SUCCESS);
  784. ck_assert_int_eq(pos,8);
  785. printf("UA_Double_decodeShallGiveZero %f\n",dst);
  786. ck_assert(-0.00000001 < dst);
  787. ck_assert(dst < 0.000000001);
  788. }
  789. END_TEST
  790. START_TEST(UA_Double_decodeShallGiveMinusTwo)
  791. {
  792. // given
  793. UA_Int32 pos = 0;
  794. UA_Byte src[] = { 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xC0 }; // -2
  795. UA_Double dst;
  796. // when
  797. UA_Int32 retval = UA_Double_decode(src,&pos,&dst);
  798. // then
  799. ck_assert_int_eq(retval,UA_SUCCESS);
  800. ck_assert_int_eq(pos,8);
  801. ck_assert(-1.9999999 > dst);
  802. ck_assert(dst > -2.00000000001);
  803. }
  804. END_TEST
  805. START_TEST(UA_ByteString_decodeShallAllocateMemoryAndCopyByteString)
  806. {
  807. // given
  808. UA_ByteString rawMessage;
  809. UA_Int32 position = 0;
  810. UA_Byte* mem = (UA_Byte*) malloc(sizeof(UA_Byte));
  811. UA_Byte val;
  812. rawMessage.data = mem;
  813. rawMessage.length = 1;
  814. mem[0] = 0x08;
  815. position = 0;
  816. // when
  817. UA_Byte_decode(rawMessage.data, &position, &val);
  818. // then
  819. ck_assert_int_eq(val, 0x08);
  820. ck_assert_int_eq(position, 1);
  821. // finally
  822. free(mem);
  823. }
  824. END_TEST
  825. START_TEST(UA_String_decodeShallAllocateMemoryAndCopyString)
  826. {
  827. // given
  828. UA_Int32 pos = 0;
  829. UA_Byte src[] = {0x08,0x00,0x00,0x00,'A','C','P','L','T',' ','U','A',0xFF,0xFF,0xFF,0xFF,0xFF};
  830. UA_String dst;
  831. // when
  832. UA_Int32 retval = UA_String_decode(src, &pos, &dst);
  833. // then
  834. ck_assert_int_eq(retval,UA_SUCCESS);
  835. ck_assert_int_eq(dst.length,8);
  836. ck_assert_ptr_eq(dst.data,UA_alloc_lastptr);
  837. ck_assert_int_eq(dst.data[3],'L');
  838. // finally
  839. UA_String_deleteMembers(&dst);
  840. }
  841. END_TEST
  842. START_TEST(UA_String_decodeWithNegativeSizeShallNotAllocateMemoryAndNullPtr)
  843. {
  844. // given
  845. UA_Int32 pos = 0;
  846. UA_Byte src[] = {0xFF,0xFF,0xFF,0xFF,'A','C','P','L','T',' ','U','A',0xFF,0xFF,0xFF,0xFF,0xFF};
  847. UA_String dst;
  848. // when
  849. UA_Int32 retval = UA_String_decode(src, &pos, &dst);
  850. // then
  851. ck_assert_int_eq(retval,UA_SUCCESS);
  852. ck_assert_int_eq(dst.length,-1);
  853. ck_assert_ptr_eq(dst.data,UA_NULL);
  854. }
  855. END_TEST
  856. START_TEST(UA_String_decodeWithZeroSizeShallNotAllocateMemoryAndNullPtr)
  857. {
  858. // given
  859. UA_Int32 pos = 0;
  860. UA_Byte src[] = {0x00,0x00,0x00,0x00,'A','C','P','L','T',' ','U','A',0xFF,0xFF,0xFF,0xFF,0xFF};
  861. UA_String dst = { 2, (UA_Byte*) "XX" };
  862. // when
  863. UA_Int32 retval = UA_String_decode(src, &pos, &dst);
  864. // then
  865. ck_assert_int_eq(retval,UA_SUCCESS);
  866. ck_assert_int_eq(dst.length,0);
  867. ck_assert_ptr_eq(dst.data,UA_NULL);
  868. }
  869. END_TEST
  870. START_TEST(UA_NodeId_decodeTwoByteShallReadTwoBytesAndSetNamespaceToZero)
  871. {
  872. // given
  873. UA_Int32 pos = 0;
  874. UA_Byte src[] = { UA_NODEIDTYPE_TWOBYTE, 0x10 };
  875. UA_NodeId dst;
  876. // when
  877. UA_Int32 retval = UA_NodeId_decode(src, &pos, &dst);
  878. // then
  879. ck_assert_int_eq(retval,UA_SUCCESS);
  880. ck_assert_int_eq(pos,2);
  881. ck_assert_int_eq(dst.encodingByte, UA_NODEIDTYPE_TWOBYTE);
  882. ck_assert_int_eq(dst.identifier.numeric,16);
  883. ck_assert_int_eq(dst.namespace,0);
  884. }
  885. END_TEST
  886. START_TEST(UA_NodeId_decodeFourByteShallReadFourBytesAndRespectNamespace)
  887. {
  888. // given
  889. UA_Int32 pos = 0;
  890. UA_Byte src[] = { UA_NODEIDTYPE_FOURBYTE, 0x01, 0x00, 0x01 };
  891. UA_NodeId dst;
  892. // when
  893. UA_Int32 retval = UA_NodeId_decode(src, &pos, &dst);
  894. // then
  895. ck_assert_int_eq(retval,UA_SUCCESS);
  896. ck_assert_int_eq(pos,4);
  897. ck_assert_int_eq(dst.encodingByte, UA_NODEIDTYPE_FOURBYTE);
  898. ck_assert_int_eq(dst.identifier.numeric,256);
  899. ck_assert_int_eq(dst.namespace,1);
  900. }
  901. END_TEST
  902. START_TEST(UA_NodeId_decodeStringShallAllocateMemory)
  903. {
  904. // given
  905. UA_Int32 pos = 0;
  906. UA_Byte src[] = { UA_NODEIDTYPE_STRING, 0x01, 0x00, 0x03, 0x00, 0x00, 0x00, 'P', 'L', 'T' };
  907. UA_NodeId dst;
  908. // when
  909. UA_Int32 retval = UA_NodeId_decode(src, &pos, &dst);
  910. // then
  911. ck_assert_int_eq(retval,UA_SUCCESS);
  912. ck_assert_int_eq(pos,10);
  913. ck_assert_int_eq(dst.encodingByte, UA_NODEIDTYPE_STRING);
  914. ck_assert_int_eq(dst.namespace,1);
  915. ck_assert_int_eq(dst.identifier.string.length,3);
  916. ck_assert_ptr_eq(dst.identifier.string.data,UA_alloc_lastptr);
  917. ck_assert_int_eq(dst.identifier.string.data[1],'L');
  918. // finally
  919. UA_NodeId_deleteMembers(&dst);
  920. }
  921. END_TEST
  922. START_TEST(UA_Variant_decodeWithOutArrayFlagSetShallSetVTAndAllocateMemoryForArray)
  923. {
  924. // given
  925. UA_Int32 pos = 0;
  926. UA_Byte src[] = { UA_INT32_NS0, 0xFF, 0x00, 0x00, 0x00};
  927. UA_Variant dst;
  928. // when
  929. UA_Int32 retval = UA_Variant_decode(src, &pos, &dst);
  930. // then
  931. ck_assert_int_eq(retval,UA_SUCCESS);
  932. ck_assert_int_eq(pos,5);
  933. ck_assert_uint_eq(dst.encodingMask, UA_INT32_NS0);
  934. ck_assert_ptr_eq(dst.vt, &UA_[UA_INT32]);
  935. ck_assert_int_eq(dst.arrayLength,1);
  936. ck_assert_ptr_ne(dst.data,UA_NULL);
  937. ck_assert_ptr_eq(dst.data[0],UA_alloc_lastptr);
  938. ck_assert_int_eq(*(UA_Int32*)dst.data[0],255);
  939. // finally
  940. UA_Variant_deleteMembers(&dst);
  941. }
  942. END_TEST
  943. START_TEST(UA_Variant_decodeWithArrayFlagSetShallSetVTAndAllocateMemoryForArray)
  944. {
  945. // given
  946. UA_Int32 pos = 0;
  947. UA_Byte src[] = { UA_INT32_NS0 | UA_VARIANT_ENCODINGMASKTYPE_ARRAY, 0x02, 0x00, 0x00, 0x00, 0xFF, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF};
  948. UA_Variant dst;
  949. // when
  950. UA_Int32 retval = UA_Variant_decode(src, &pos, &dst);
  951. // then
  952. ck_assert_int_eq(retval,UA_SUCCESS);
  953. ck_assert_int_eq(pos,1+4+2*4);
  954. ck_assert_uint_eq(dst.encodingMask & UA_VARIANT_ENCODINGMASKTYPE_TYPEID_MASK, UA_INT32_NS0);
  955. ck_assert_uint_eq(dst.encodingMask & UA_VARIANT_ENCODINGMASKTYPE_ARRAY, UA_VARIANT_ENCODINGMASKTYPE_ARRAY);
  956. ck_assert_ptr_eq(dst.vt, &UA_[UA_INT32]);
  957. ck_assert_int_eq(dst.arrayLength,2);
  958. ck_assert_ptr_ne(dst.data,UA_NULL);
  959. ck_assert_ptr_eq(dst.data[1],UA_alloc_lastptr);
  960. ck_assert_int_eq(*((UA_Int32*)dst.data[0]),255);
  961. ck_assert_int_eq(*((UA_Int32*)dst.data[1]),-1);
  962. // finally
  963. UA_Variant_deleteMembers(&dst);
  964. }
  965. END_TEST
  966. START_TEST(UA_Variant_decodeWithOutDeleteMembersShallFailInCheckMem)
  967. {
  968. // given
  969. UA_Int32 pos = 0;
  970. UA_Byte src[] = { UA_INT32_NS0 | UA_VARIANT_ENCODINGMASKTYPE_ARRAY, 0x02, 0x00, 0x00, 0x00, 0xFF, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF};
  971. UA_Variant dst;
  972. // when
  973. UA_Int32 retval = UA_Variant_decode(src, &pos, &dst);
  974. // then
  975. ck_assert_int_eq(retval,UA_SUCCESS);
  976. // finally - unfortunately we cannot express that not freeing three chunks is what we expect
  977. // UA_Variant_deleteMembers(&dst);
  978. }
  979. END_TEST
  980. START_TEST(UA_Byte_encodeShallWorkOnExample)
  981. {
  982. // given
  983. UA_Byte src;
  984. UA_Byte dst[2] = { 0x00, 0xFF };
  985. UA_Int32 retval, pos = 0;
  986. // when
  987. ck_assert_uint_eq(dst[1], 0xFF);
  988. src = 8;
  989. retval = UA_Byte_encode(&src, &pos, dst);
  990. // then
  991. ck_assert_uint_eq(dst[0], 0x08);
  992. ck_assert_uint_eq(dst[1], 0xFF);
  993. ck_assert_int_eq(pos, 1);
  994. ck_assert_int_eq(retval, UA_SUCCESS);
  995. // Test2
  996. // given
  997. src = 0xFF;
  998. dst[1] = 0x00;
  999. pos = 0;
  1000. // when
  1001. retval = UA_Byte_encode(&src, &pos, dst);
  1002. // then
  1003. ck_assert_int_eq(dst[0], 0xFF);
  1004. ck_assert_int_eq(dst[1], 0x00);
  1005. ck_assert_int_eq(pos, 1);
  1006. ck_assert_int_eq(retval, UA_SUCCESS);
  1007. }
  1008. END_TEST
  1009. START_TEST(UA_ByteString_encodeShallWorkOnExample)
  1010. {
  1011. // given
  1012. UA_ByteString rawMessage;
  1013. UA_Int32 position = 0;
  1014. UA_Byte *mem = malloc(sizeof(UA_Byte));
  1015. rawMessage.data = mem;
  1016. UA_Byte testByte = 0x08;
  1017. rawMessage.length = 1;
  1018. position = 0;
  1019. // when
  1020. UA_Byte_encode(&(testByte), &position, rawMessage.data);
  1021. // then
  1022. ck_assert_int_eq(rawMessage.data[0], 0x08);
  1023. ck_assert_int_eq(rawMessage.length, 1);
  1024. ck_assert_int_eq(position, 1);
  1025. // finally
  1026. free(mem);
  1027. }
  1028. END_TEST
  1029. START_TEST(UA_Int16_encodeShallWorkOnExample)
  1030. {
  1031. // given
  1032. UA_ByteString rawMessage;
  1033. UA_Int32 position = 0;
  1034. UA_Byte *mem = malloc(sizeof(UA_UInt16));
  1035. rawMessage.data = mem;
  1036. UA_UInt16 testUInt16 = 1;
  1037. rawMessage.length = 2;
  1038. position = 0;
  1039. // when
  1040. UA_UInt16_encode(&testUInt16, &position, rawMessage.data);
  1041. // then
  1042. ck_assert_int_eq(position, 2);
  1043. UA_Int32 p = 0;
  1044. UA_UInt16 val;
  1045. UA_UInt16_decode(rawMessage.data, &p, &val);
  1046. ck_assert_int_eq(val,testUInt16);
  1047. //ck_assert_int_eq(rawMessage.data[0], 0xAB);
  1048. // finally
  1049. free(mem);
  1050. }
  1051. END_TEST
  1052. START_TEST(UA_UInt16_encodeShallWorkOnExample)
  1053. {
  1054. // given
  1055. UA_ByteString rawMessage;
  1056. UA_Int32 position = 0;
  1057. UA_Byte *mem = (UA_Byte*) malloc(sizeof(UA_UInt16));
  1058. rawMessage.data = mem;
  1059. UA_UInt16 testUInt16 = 1;
  1060. rawMessage.length = 2;
  1061. position = 0;
  1062. // when
  1063. UA_UInt16_encode(&testUInt16, &position, rawMessage.data);
  1064. // then
  1065. ck_assert_int_eq(position, 2);
  1066. UA_Int32 p = 0;
  1067. UA_UInt16 val;
  1068. UA_UInt16_decode(rawMessage.data, &p, &val);
  1069. ck_assert_int_eq(val,testUInt16);
  1070. //ck_assert_int_eq(rawMessage.data[0], 0xAB);
  1071. // finally
  1072. free(mem);
  1073. }
  1074. END_TEST
  1075. START_TEST(UA_UInt32_encodeShallWorkOnExample)
  1076. {
  1077. // given
  1078. UA_ByteString rawMessage;
  1079. UA_UInt32 value = 0x0101FF00;
  1080. rawMessage.data = (UA_Byte*) malloc(2 * sizeof(UA_UInt32));
  1081. rawMessage.length = 8;
  1082. UA_Int32 p = 4;
  1083. // when
  1084. UA_UInt32_encode(&value,&p,rawMessage.data);
  1085. // then
  1086. ck_assert_uint_eq(rawMessage.data[4],0x00);
  1087. ck_assert_uint_eq(rawMessage.data[5],0xFF);
  1088. ck_assert_uint_eq(rawMessage.data[6],0x01);
  1089. ck_assert_uint_eq(rawMessage.data[7],0x01);
  1090. ck_assert_int_eq(p,8);
  1091. // finally
  1092. free(rawMessage.data);
  1093. }
  1094. END_TEST
  1095. START_TEST(UA_Int32_encodeShallEncodeLittleEndian)
  1096. {
  1097. // given
  1098. UA_Int32 value = 0x01020304;
  1099. UA_Byte buf[4];
  1100. UA_Int32 p = 0;
  1101. // when
  1102. UA_Int32_encode(&value,&p,buf);
  1103. // then
  1104. ck_assert_int_eq(p,4);
  1105. ck_assert_uint_eq(buf[0],0x04);
  1106. ck_assert_uint_eq(buf[1],0x03);
  1107. ck_assert_uint_eq(buf[2],0x02);
  1108. ck_assert_uint_eq(buf[3],0x01);
  1109. }
  1110. END_TEST
  1111. START_TEST(UA_Int32_encodeNegativeShallEncodeLittleEndian)
  1112. {
  1113. // given
  1114. UA_Int32 value = -1;
  1115. UA_Byte buf[4];
  1116. UA_Int32 p = 0;
  1117. // when
  1118. UA_Int32_encode(&value,&p,buf);
  1119. // then
  1120. ck_assert_int_eq(p,4);
  1121. ck_assert_uint_eq(buf[0],0xFF);
  1122. ck_assert_uint_eq(buf[1],0xFF);
  1123. ck_assert_uint_eq(buf[2],0xFF);
  1124. ck_assert_uint_eq(buf[3],0xFF);
  1125. }
  1126. END_TEST
  1127. START_TEST(UA_UInt64_encodeShallWorkOnExample)
  1128. {
  1129. // given
  1130. UA_ByteString rawMessage;
  1131. UA_UInt64 value = 0x0101FF00FF00FF00;
  1132. rawMessage.data = (UA_Byte*) malloc(sizeof(UA_UInt64));
  1133. rawMessage.length = 8;
  1134. UA_Int32 p = 0;
  1135. // when
  1136. UA_UInt64_encode(&value, &p,rawMessage.data);
  1137. // then
  1138. ck_assert_uint_eq((UA_Byte)rawMessage.data[0],0x00);
  1139. ck_assert_uint_eq((UA_Byte)rawMessage.data[1],0xFF);
  1140. ck_assert_uint_eq((UA_Byte)rawMessage.data[2],0x00);
  1141. ck_assert_uint_eq((UA_Byte)rawMessage.data[3],0xFF);
  1142. ck_assert_uint_eq((UA_Byte)rawMessage.data[4],0x00);
  1143. ck_assert_uint_eq((UA_Byte)rawMessage.data[5],0xFF);
  1144. ck_assert_uint_eq((UA_Byte)rawMessage.data[6],0x01);
  1145. ck_assert_uint_eq((UA_Byte)rawMessage.data[7],0x01);
  1146. // finally
  1147. free(rawMessage.data);
  1148. }
  1149. END_TEST
  1150. START_TEST(UA_Int64_encodeShallWorkOnExample)
  1151. {
  1152. // given
  1153. UA_ByteString rawMessage;
  1154. UA_UInt64 value = 0x0101FF00FF00FF00;
  1155. rawMessage.data = (UA_Byte*) malloc(sizeof(UA_UInt64));
  1156. rawMessage.length = 8;
  1157. UA_Int32 p = 0;
  1158. // when
  1159. UA_UInt64_encode(&value, &p,rawMessage.data);
  1160. // then
  1161. ck_assert_uint_eq(rawMessage.data[0],0x00);
  1162. ck_assert_uint_eq(rawMessage.data[1],0xFF);
  1163. ck_assert_uint_eq(rawMessage.data[2],0x00);
  1164. ck_assert_uint_eq(rawMessage.data[3],0xFF);
  1165. ck_assert_uint_eq(rawMessage.data[4],0x00);
  1166. ck_assert_uint_eq(rawMessage.data[5],0xFF);
  1167. ck_assert_uint_eq(rawMessage.data[6],0x01);
  1168. ck_assert_uint_eq(rawMessage.data[7],0x01);
  1169. // finally
  1170. free(rawMessage.data);
  1171. }
  1172. END_TEST
  1173. START_TEST(UA_Float_encodeShallWorkOnExample)
  1174. {
  1175. // given
  1176. UA_Float value = -6.5;
  1177. UA_Int32 pos = 0;
  1178. UA_Byte* buf = (UA_Byte*)malloc(sizeof(UA_Float));
  1179. // when
  1180. UA_Float_encode(&value,&pos,buf);
  1181. // then
  1182. ck_assert_uint_eq(buf[2],0xD0);
  1183. ck_assert_uint_eq(buf[3],0xC0);
  1184. // finally
  1185. free(buf);
  1186. }
  1187. END_TEST
  1188. /*START_TEST(encodeDouble_test)
  1189. {
  1190. UA_Double value = -6.5;
  1191. UA_Int32 pos = 0;
  1192. UA_Byte* buf = (char*)malloc(sizeof(UA_Double));
  1193. UA_Double_encode(&value,&pos,buf);
  1194. ck_assert_uint_eq(buf[6],0xD0);
  1195. ck_assert_uint_eq(buf[7],0xC0);
  1196. free(buf);
  1197. }
  1198. END_TEST*/
  1199. START_TEST(UA_String_encodeShallWorkOnExample)
  1200. {
  1201. // given
  1202. UA_Int32 pos = 0;
  1203. UA_String string;
  1204. UA_Int32 l = 11;
  1205. UA_Byte mem[11] = "ACPLT OPCUA";
  1206. UA_Byte *dstBuf = (UA_Byte*) malloc(sizeof(UA_Int32)+l);
  1207. string.data = mem;
  1208. string.length = 11;
  1209. // when
  1210. UA_String_encode(&string, &pos, dstBuf);
  1211. // then
  1212. ck_assert_int_eq(dstBuf[0],11);
  1213. ck_assert_int_eq(dstBuf[0+sizeof(UA_Int32)],'A');
  1214. // finally
  1215. free(dstBuf);
  1216. }
  1217. END_TEST
  1218. START_TEST(UA_DataValue_encodeShallWorkOnExampleWithoutVariant)
  1219. {
  1220. // given
  1221. UA_DataValue dataValue;
  1222. UA_Int32 pos = 0;
  1223. UA_Byte* buf = (UA_Byte*) malloc(15);
  1224. UA_DateTime dateTime;
  1225. dateTime = 80;
  1226. dataValue.serverTimestamp = dateTime;
  1227. dataValue.encodingMask = UA_DATAVALUE_SERVERTIMPSTAMP; //Only the sourcePicoseconds
  1228. // when
  1229. UA_DataValue_encode(&dataValue, &pos, buf);
  1230. //then
  1231. ck_assert_int_eq(pos, 9);// represents the length
  1232. ck_assert_uint_eq(buf[0], 0x08); // encodingMask
  1233. ck_assert_uint_eq(buf[1], 80); // 8 Byte serverTimestamp
  1234. ck_assert_uint_eq(buf[2], 0);
  1235. ck_assert_uint_eq(buf[3], 0);
  1236. ck_assert_uint_eq(buf[4], 0);
  1237. ck_assert_uint_eq(buf[5], 0);
  1238. ck_assert_uint_eq(buf[6], 0);
  1239. ck_assert_uint_eq(buf[7], 0);
  1240. ck_assert_uint_eq(buf[8], 0);
  1241. // finally
  1242. free(buf);
  1243. }
  1244. END_TEST
  1245. START_TEST(UA_DataValue_encodeShallWorkOnExampleWithVariant)
  1246. {
  1247. // given
  1248. UA_DataValue dataValue;
  1249. UA_Int32 pos = 0, retval;
  1250. UA_Byte* buf = (UA_Byte*) malloc(15);
  1251. dataValue.encodingMask = UA_DATAVALUE_VARIANT | UA_DATAVALUE_SERVERTIMPSTAMP; //Variant & SourvePicoseconds
  1252. dataValue.value.vt = &UA_[UA_INT32];
  1253. dataValue.value.arrayLength = 0;
  1254. dataValue.value.encodingMask = UA_INT32_NS0;
  1255. UA_DateTime serverTime = 80;
  1256. dataValue.serverTimestamp = serverTime;
  1257. UA_Int32 data = 45;
  1258. UA_Int32* pdata = &data;
  1259. dataValue.value.data = (void**) &pdata;
  1260. pos = 0;
  1261. // when
  1262. retval = UA_DataValue_encode(&dataValue, &pos, buf);
  1263. // then
  1264. ck_assert_int_eq(retval, UA_SUCCESS);
  1265. ck_assert_int_eq(pos, 1+(1+4)+8);// represents the length
  1266. ck_assert_uint_eq(buf[0], 0x08 | 0x01); // encodingMask
  1267. ck_assert_uint_eq(buf[1], 0x06); // Variant's Encoding Mask - INT32
  1268. ck_assert_uint_eq(buf[2], 45); // the single value
  1269. ck_assert_uint_eq(buf[3], 0);
  1270. ck_assert_uint_eq(buf[4], 0);
  1271. ck_assert_uint_eq(buf[5], 0);
  1272. ck_assert_uint_eq(buf[6], 80); // the server timestamp
  1273. ck_assert_uint_eq(buf[7], 0);
  1274. // finally
  1275. free(buf);
  1276. }
  1277. END_TEST
  1278. Suite *testSuite_builtin(void)
  1279. {
  1280. Suite *s = suite_create("Built-in Data Types 62541-6 Table 1");
  1281. TCase *tc_calcSize = tcase_create("calcSize");
  1282. tcase_add_test(tc_calcSize, UA_Boolean_calcSizeWithNullArgumentShallReturnStorageSize);
  1283. tcase_add_test(tc_calcSize, UA_SByte_calcSizeWithNullArgumentShallReturnStorageSize);
  1284. tcase_add_test(tc_calcSize, UA_Byte_calcSizeWithNullArgumentShallReturnStorageSize);
  1285. tcase_add_test(tc_calcSize, UA_Int16_calcSizeWithNullArgumentShallReturnStorageSize);
  1286. tcase_add_test(tc_calcSize, UA_UInt16_calcSizeWithNullArgumentShallReturnStorageSize);
  1287. tcase_add_test(tc_calcSize, UA_Int32_calcSizeWithNullArgumentShallReturnStorageSize);
  1288. tcase_add_test(tc_calcSize, UA_UInt32_calcSizeWithNullArgumentShallReturnStorageSize);
  1289. tcase_add_test(tc_calcSize, UA_Int64_calcSizeWithNullArgumentShallReturnStorageSize);
  1290. tcase_add_test(tc_calcSize, UA_UInt64_calcSizeWithNullArgumentShallReturnStorageSize);
  1291. tcase_add_test(tc_calcSize, UA_Float_calcSizeWithNullArgumentShallReturnStorageSize);
  1292. tcase_add_test(tc_calcSize, UA_Double_calcSizeWithNullArgumentShallReturnStorageSize);
  1293. tcase_add_test(tc_calcSize, UA_String_calcSizeWithNullArgumentShallReturnStorageSize);
  1294. tcase_add_test(tc_calcSize, UA_DateTime_calcSizeWithNullArgumentShallReturnStorageSize);
  1295. tcase_add_test(tc_calcSize, UA_Guid_calcSizeWithNullArgumentShallReturnStorageSize);
  1296. tcase_add_test(tc_calcSize, UA_ByteString_calcSizeWithNullArgumentShallReturnStorageSize);
  1297. tcase_add_test(tc_calcSize, UA_XmlElement_calcSizeWithNullArgumentShallReturnStorageSize);
  1298. tcase_add_test(tc_calcSize, UA_NodeId_calcSizeWithNullArgumentShallReturnStorageSize);
  1299. tcase_add_test(tc_calcSize, UA_ExpandedNodeId_calcSizeWithNullArgumentShallReturnStorageSize);
  1300. tcase_add_test(tc_calcSize, UA_StatusCode_calcSizeWithNullArgumentShallReturnStorageSize);
  1301. tcase_add_test(tc_calcSize, UA_QualifiedName_calcSizeWithNullArgumentShallReturnStorageSize);
  1302. tcase_add_test(tc_calcSize, UA_LocalizedText_calcSizeWithNullArgumentShallReturnStorageSize);
  1303. tcase_add_test(tc_calcSize, UA_ExtensionObject_calcSizeShallWorkOnExample);
  1304. tcase_add_test(tc_calcSize, UA_ExtensionObject_calcSizeWithNullArgumentShallReturnStorageSize);
  1305. tcase_add_test(tc_calcSize, UA_DataValue_calcSizeShallWorkOnExample);
  1306. tcase_add_test(tc_calcSize, UA_DataValue_calcSizeWithNullArgumentShallReturnStorageSize);
  1307. tcase_add_test(tc_calcSize, UA_Variant_calcSizeWithNullArgumentShallReturnStorageSize);
  1308. tcase_add_test(tc_calcSize, UA_DiagnosticInfo_calcSizeShallWorkOnExample);
  1309. tcase_add_test(tc_calcSize, UA_DiagnosticInfo_calcSizeWithNullArgumentShallReturnStorageSize);
  1310. tcase_add_test(tc_calcSize, UA_String_calcSizeShallReturnEncodingSize);
  1311. tcase_add_test(tc_calcSize, UA_String_calcSizeWithNegativLengthShallReturnEncodingSize);
  1312. tcase_add_test(tc_calcSize, UA_String_calcSizeWithNegativLengthAndValidPointerShallReturnEncodingSize);
  1313. tcase_add_test(tc_calcSize, UA_String_calcSizeWithZeroLengthShallReturnEncodingSize);
  1314. tcase_add_test(tc_calcSize, UA_String_calcSizeWithZeroLengthAndValidPointerShallReturnEncodingSize);
  1315. tcase_add_test(tc_calcSize, UA_NodeId_calcSizeEncodingTwoByteShallReturnEncodingSize);
  1316. tcase_add_test(tc_calcSize, UA_NodeId_calcSizeEncodingFourByteShallReturnEncodingSize);
  1317. tcase_add_test(tc_calcSize, UA_NodeId_calcSizeEncodingStringShallReturnEncodingSize);
  1318. tcase_add_test(tc_calcSize, UA_NodeId_calcSizeEncodingStringNegativLengthShallReturnEncodingSize);
  1319. tcase_add_test(tc_calcSize, UA_NodeId_calcSizeEncodingStringZeroLengthShallReturnEncodingSize);
  1320. tcase_add_test(tc_calcSize, UA_ExpandedNodeId_calcSizeShallWorkOnExample);
  1321. tcase_add_test(tc_calcSize, UA_ExpandedNodeId_calcSizeEncodingStringAndServerIndexShallReturnEncodingSize);
  1322. tcase_add_test(tc_calcSize, UA_ExpandedNodeId_calcSizeEncodingStringAndNamespaceUriShallReturnEncodingSize);
  1323. tcase_add_test(tc_calcSize, UA_Guid_calcSizeShallReturnEncodingSize);
  1324. tcase_add_test(tc_calcSize, UA_Guid_calcSizeShallReturnEncodingSize);
  1325. tcase_add_test(tc_calcSize, UA_LocalizedText_calcSizeTextOnlyShallReturnEncodingSize);
  1326. tcase_add_test(tc_calcSize, UA_LocalizedText_calcSizeLocaleOnlyShallReturnEncodingSize);
  1327. tcase_add_test(tc_calcSize, UA_LocalizedText_calcSizeTextAndLocaleShallReturnEncodingSize);
  1328. tcase_add_test(tc_calcSize, UA_Variant_calcSizeFixedSizeArrayShallReturnEncodingSize);
  1329. tcase_add_test(tc_calcSize, UA_Variant_calcSizeVariableSizeArrayShallReturnEncodingSize);
  1330. tcase_add_test(tc_calcSize, UA_Variant_calcSizeVariableSizeArrayWithNullPtrWillReturnWrongButLargeEnoughEncodingSize);
  1331. suite_add_tcase(s,tc_calcSize);
  1332. TCase *tc_decode = tcase_create("decode");
  1333. tcase_add_test(tc_decode, UA_Byte_decodeShallCopyAndAdvancePosition);
  1334. tcase_add_test(tc_decode, UA_Byte_decodeShallModifyOnlyCurrentPosition);
  1335. tcase_add_test(tc_decode, UA_Int16_decodeShallAssumeLittleEndian);
  1336. tcase_add_test(tc_decode, UA_Int16_decodeShallRespectSign);
  1337. tcase_add_test(tc_decode, UA_UInt16_decodeShallNotRespectSign);
  1338. tcase_add_test(tc_decode, UA_Int32_decodeShallAssumeLittleEndian);
  1339. tcase_add_test(tc_decode, UA_Int32_decodeShallRespectSign);
  1340. tcase_add_test(tc_decode, UA_UInt32_decodeShallNotRespectSign);
  1341. tcase_add_test(tc_decode, UA_UInt64_decodeShallNotRespectSign);
  1342. tcase_add_test(tc_decode, UA_Int64_decodeShallRespectSign);
  1343. tcase_add_test(tc_decode, UA_Float_decodeShallWorkOnExample);
  1344. tcase_add_test(tc_decode, UA_Double_decodeShallGiveOne);
  1345. tcase_add_test(tc_decode, UA_Double_decodeShallGiveZero);
  1346. tcase_add_test(tc_decode, UA_Double_decodeShallGiveMinusTwo);
  1347. tcase_add_test(tc_decode, UA_ByteString_decodeShallAllocateMemoryAndCopyByteString);
  1348. tcase_add_test(tc_decode, UA_String_decodeShallAllocateMemoryAndCopyString);
  1349. tcase_add_test(tc_decode, UA_String_decodeWithNegativeSizeShallNotAllocateMemoryAndNullPtr);
  1350. tcase_add_test(tc_decode, UA_String_decodeWithZeroSizeShallNotAllocateMemoryAndNullPtr);
  1351. tcase_add_test(tc_decode, UA_NodeId_decodeTwoByteShallReadTwoBytesAndSetNamespaceToZero);
  1352. tcase_add_test(tc_decode, UA_NodeId_decodeFourByteShallReadFourBytesAndRespectNamespace);
  1353. tcase_add_test(tc_decode, UA_NodeId_decodeStringShallAllocateMemory);
  1354. tcase_add_test(tc_decode, UA_Variant_decodeWithOutArrayFlagSetShallSetVTAndAllocateMemoryForArray);
  1355. tcase_add_test(tc_decode, UA_Variant_decodeWithArrayFlagSetShallSetVTAndAllocateMemoryForArray);
  1356. tcase_add_test(tc_decode, UA_Variant_decodeWithOutDeleteMembersShallFailInCheckMem);
  1357. suite_add_tcase(s,tc_decode);
  1358. TCase *tc_encode = tcase_create("encode");
  1359. tcase_add_test(tc_encode, UA_Byte_encodeShallWorkOnExample);
  1360. tcase_add_test(tc_encode, UA_ByteString_encodeShallWorkOnExample);
  1361. tcase_add_test(tc_encode, UA_Int16_encodeShallWorkOnExample);
  1362. tcase_add_test(tc_encode, UA_UInt16_encodeShallWorkOnExample);
  1363. tcase_add_test(tc_encode, UA_UInt32_encodeShallWorkOnExample);
  1364. tcase_add_test(tc_encode, UA_Int32_encodeShallEncodeLittleEndian);
  1365. tcase_add_test(tc_encode, UA_Int32_encodeNegativeShallEncodeLittleEndian);
  1366. tcase_add_test(tc_encode, UA_UInt64_encodeShallWorkOnExample);
  1367. tcase_add_test(tc_encode, UA_Int64_encodeShallWorkOnExample);
  1368. tcase_add_test(tc_encode, UA_Float_encodeShallWorkOnExample);
  1369. tcase_add_test(tc_encode, UA_String_encodeShallWorkOnExample);
  1370. tcase_add_test(tc_encode, UA_DataValue_encodeShallWorkOnExampleWithoutVariant);
  1371. tcase_add_test(tc_encode, UA_DataValue_encodeShallWorkOnExampleWithVariant);
  1372. suite_add_tcase(s,tc_encode);
  1373. return s;
  1374. }
  1375. int main (void)
  1376. {
  1377. int number_failed = 0;
  1378. Suite* s;
  1379. SRunner* sr;
  1380. s = testSuite_builtin();
  1381. sr = srunner_create(s);
  1382. srunner_run_all(sr,CK_NORMAL);
  1383. number_failed += srunner_ntests_failed(sr);
  1384. srunner_free(sr);
  1385. return (number_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
  1386. }