check_builtin.c 41 KB

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