check_builtin.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867
  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_calcSizeWithNullArgumentShallReturnStorageSize)
  187. {
  188. // given
  189. UA_ExpandedNodeId* arg = UA_NULL;
  190. // when
  191. UA_Int32 storageSize = UA_ExpandedNodeId_calcSize(arg);
  192. // then
  193. ck_assert_int_eq(storageSize, sizeof(UA_ExpandedNodeId));
  194. }
  195. END_TEST
  196. START_TEST(UA_StatusCode_calcSizeWithNullArgumentShallReturnStorageSize)
  197. {
  198. // given
  199. UA_StatusCode* arg = UA_NULL;
  200. // when
  201. UA_Int32 storageSize = UA_StatusCode_calcSize(arg);
  202. // then
  203. ck_assert_int_eq(storageSize, sizeof(UA_StatusCode));
  204. }
  205. END_TEST
  206. START_TEST(UA_QualifiedName_calcSizeWithNullArgumentShallReturnStorageSize)
  207. {
  208. // given
  209. UA_QualifiedName* arg = UA_NULL;
  210. // when
  211. UA_Int32 storageSize = UA_QualifiedName_calcSize(arg);
  212. // then
  213. ck_assert_int_eq(storageSize, sizeof(UA_QualifiedName));
  214. }
  215. END_TEST
  216. START_TEST(UA_LocalizedText_calcSizeWithNullArgumentShallReturnStorageSize)
  217. {
  218. // given
  219. UA_LocalizedText* arg = UA_NULL;
  220. // when
  221. UA_Int32 storageSize = UA_LocalizedText_calcSize(arg);
  222. // then
  223. ck_assert_int_eq(storageSize, sizeof(UA_LocalizedText));
  224. }
  225. END_TEST
  226. START_TEST(UA_ExtensionObject_calcSizeWithNullArgumentShallReturnStorageSize)
  227. {
  228. // given
  229. UA_ExtensionObject* arg = UA_NULL;
  230. // when
  231. UA_Int32 storageSize = UA_ExtensionObject_calcSize(arg);
  232. // then
  233. ck_assert_int_eq(storageSize, sizeof(UA_ExtensionObject));
  234. }
  235. END_TEST
  236. START_TEST(UA_DataValue_calcSizeWithNullArgumentShallReturnStorageSize)
  237. {
  238. // given
  239. UA_DataValue* arg = UA_NULL;
  240. // when
  241. UA_Int32 storageSize = UA_DataValue_calcSize(arg);
  242. // then
  243. ck_assert_int_eq(storageSize, sizeof(UA_DataValue));
  244. }
  245. END_TEST
  246. START_TEST(UA_Variant_calcSizeWithNullArgumentShallReturnStorageSize)
  247. {
  248. // given
  249. UA_Variant* arg = UA_NULL;
  250. // when
  251. UA_Int32 storageSize = UA_Variant_calcSize(arg);
  252. // then
  253. ck_assert_int_eq(storageSize, sizeof(UA_Variant));
  254. }
  255. END_TEST
  256. START_TEST(UA_DiagnosticInfo_calcSizeWithNullArgumentShallReturnStorageSize)
  257. {
  258. // given
  259. UA_DiagnosticInfo* arg = UA_NULL;
  260. // when
  261. UA_Int32 storageSize = UA_DiagnosticInfo_calcSize(arg);
  262. // then
  263. ck_assert_int_eq(storageSize, sizeof(UA_DiagnosticInfo));
  264. }
  265. END_TEST
  266. START_TEST(UA_String_calcSizeWithNegativLengthShallReturnEncodingSize)
  267. {
  268. // given
  269. UA_String arg = {-1, UA_NULL};
  270. // when
  271. UA_Int32 encodingSize = UA_String_calcSize(&arg);
  272. // then
  273. ck_assert_int_eq(encodingSize, 4);
  274. }
  275. END_TEST
  276. START_TEST(UA_String_calcSizeWithNegativLengthAndValidPointerShallReturnEncodingSize)
  277. {
  278. // given
  279. UA_String arg = {-1, (UA_Byte*) "OPC"};
  280. // when
  281. UA_Int32 encodingSize = UA_String_calcSize(&arg);
  282. // then
  283. ck_assert_int_eq(encodingSize, 4);
  284. }
  285. END_TEST
  286. START_TEST(UA_String_calcSizeWithZeroLengthShallReturnEncodingSize)
  287. {
  288. // given
  289. UA_String arg = {0, UA_NULL};
  290. // when
  291. UA_Int32 encodingSize = UA_String_calcSize(&arg);
  292. // then
  293. ck_assert_int_eq(encodingSize, 4);
  294. }
  295. END_TEST
  296. START_TEST(UA_String_calcSizeWithZeroLengthAndValidPointerShallReturnEncodingSize)
  297. {
  298. // given
  299. UA_String arg = {0, (UA_Byte*) "OPC"};
  300. // when
  301. UA_Int32 encodingSize = UA_String_calcSize(&arg);
  302. // then
  303. ck_assert_int_eq(encodingSize, 4);
  304. }
  305. END_TEST
  306. START_TEST(UA_String_calcSizeShallReturnEncodingSize)
  307. {
  308. // given
  309. UA_String arg = {3, (UA_Byte*) "OPC"};
  310. // when
  311. UA_Int32 encodingSize = UA_String_calcSize(&arg);
  312. // then
  313. ck_assert_int_eq(encodingSize, 4+3);
  314. }
  315. END_TEST
  316. START_TEST(UA_NodeId_calcSizeEncodingTwoByteShallReturnEncodingSize)
  317. {
  318. // given
  319. UA_NodeId arg;
  320. arg.encodingByte = UA_NODEIDTYPE_TWOBYTE;
  321. // when
  322. UA_Int32 encodingSize = UA_NodeId_calcSize(&arg);
  323. // then
  324. ck_assert_int_eq(encodingSize, 2);
  325. }
  326. END_TEST
  327. START_TEST(UA_NodeId_calcSizeEncodingFourByteShallReturnEncodingSize)
  328. {
  329. // given
  330. UA_NodeId arg;
  331. arg.encodingByte = UA_NODEIDTYPE_FOURBYTE;
  332. // when
  333. UA_Int32 encodingSize = UA_NodeId_calcSize(&arg);
  334. // then
  335. ck_assert_int_eq(encodingSize, 4);
  336. }
  337. END_TEST
  338. START_TEST(UA_NodeId_calcSizeEncodingStringShallReturnEncodingSize)
  339. {
  340. // given
  341. UA_NodeId arg;
  342. arg.encodingByte = UA_NODEIDTYPE_STRING;
  343. arg.identifier.string.length = 3;
  344. arg.identifier.string.data = (UA_Byte*) "PLT";
  345. // when
  346. UA_Int32 encodingSize = UA_NodeId_calcSize(&arg);
  347. // then
  348. ck_assert_int_eq(encodingSize, 1+2+4+3);
  349. }
  350. END_TEST
  351. START_TEST(UA_NodeId_calcSizeEncodingStringNegativLengthShallReturnEncodingSize)
  352. {
  353. // given
  354. UA_NodeId arg;
  355. arg.encodingByte = UA_NODEIDTYPE_STRING;
  356. arg.identifier.string.length = -1;
  357. // when
  358. UA_Int32 encodingSize = UA_NodeId_calcSize(&arg);
  359. // then
  360. ck_assert_int_eq(encodingSize, 1+2+4+0);
  361. }
  362. END_TEST
  363. START_TEST(UA_NodeId_calcSizeEncodingStringZeroLengthShallReturnEncodingSize)
  364. {
  365. // given
  366. UA_NodeId arg;
  367. arg.encodingByte = UA_NODEIDTYPE_STRING;
  368. arg.identifier.string.length = 0;
  369. // when
  370. UA_Int32 encodingSize = UA_NodeId_calcSize(&arg);
  371. // then
  372. ck_assert_int_eq(encodingSize, 1+2+4+0);
  373. }
  374. END_TEST
  375. START_TEST(UA_ExpandedNodeId_calcSizeEncodingStringAndServerIndexShallReturnEncodingSize)
  376. {
  377. // given
  378. UA_ExpandedNodeId arg;
  379. arg.nodeId.encodingByte = UA_NODEIDTYPE_STRING | UA_NODEIDTYPE_SERVERINDEX_FLAG;
  380. arg.nodeId.identifier.string.length = 3;
  381. // when
  382. UA_Int32 encodingSize = UA_ExpandedNodeId_calcSize(&arg);
  383. // then
  384. ck_assert_int_eq(encodingSize, 1+2+4+3+4);
  385. }
  386. END_TEST
  387. START_TEST(UA_ExpandedNodeId_calcSizeEncodingStringAndNamespaceUriShallReturnEncodingSize)
  388. {
  389. // given
  390. UA_ExpandedNodeId arg;
  391. arg.nodeId.encodingByte = UA_NODEIDTYPE_STRING | UA_NODEIDTYPE_NAMESPACE_URI_FLAG;
  392. arg.nodeId.identifier.string.length = 3;
  393. arg.namespaceUri.length = 7;
  394. // when
  395. UA_Int32 encodingSize = UA_ExpandedNodeId_calcSize(&arg);
  396. // then
  397. ck_assert_int_eq(encodingSize, 1+2+4+3+4+7);
  398. }
  399. END_TEST
  400. START_TEST(UA_Guid_calcSizeShallReturnEncodingSize)
  401. {
  402. // given
  403. UA_Guid arg;
  404. // when
  405. UA_Int32 encodingSize = UA_Guid_calcSize(&arg);
  406. // then
  407. ck_assert_int_eq(encodingSize, 16);
  408. }
  409. END_TEST
  410. START_TEST(UA_LocalizedText_calcSizeTextOnlyShallReturnEncodingSize)
  411. {
  412. // given
  413. UA_LocalizedText arg;
  414. arg.encodingMask = UA_LOCALIZEDTEXT_ENCODINGMASKTYPE_TEXT;
  415. arg.text.length = 42;
  416. // when
  417. UA_Int32 encodingSize = UA_LocalizedText_calcSize(&arg);
  418. // then
  419. ck_assert_int_eq(encodingSize, 1+4+42);
  420. }
  421. END_TEST
  422. START_TEST(UA_LocalizedText_calcSizeLocaleOnlyShallReturnEncodingSize)
  423. {
  424. // given
  425. UA_LocalizedText arg;
  426. arg.encodingMask = UA_LOCALIZEDTEXT_ENCODINGMASKTYPE_LOCALE;
  427. arg.locale.length = 11;
  428. // when
  429. UA_Int32 encodingSize = UA_LocalizedText_calcSize(&arg);
  430. // then
  431. ck_assert_int_eq(encodingSize, 1+4+11);
  432. }
  433. END_TEST
  434. START_TEST(UA_LocalizedText_calcSizeTextAndLocaleShallReturnEncodingSize)
  435. {
  436. // given
  437. UA_LocalizedText arg;
  438. arg.encodingMask = UA_LOCALIZEDTEXT_ENCODINGMASKTYPE_LOCALE | UA_LOCALIZEDTEXT_ENCODINGMASKTYPE_TEXT;
  439. arg.text.length = 47;
  440. arg.locale.length = 11;
  441. // when
  442. UA_Int32 encodingSize = UA_LocalizedText_calcSize(&arg);
  443. // then
  444. ck_assert_int_eq(encodingSize, 1+4+11+4+47);
  445. }
  446. END_TEST
  447. START_TEST(UA_Variant_calcSizeFixedSizeArrayShallReturnEncodingSize)
  448. {
  449. // given
  450. UA_Variant arg;
  451. arg.encodingMask = UA_INT32_NS0 | UA_VARIANT_ENCODINGMASKTYPE_ARRAY;
  452. arg.vt = &UA_[UA_INT32];
  453. #define ARRAY_LEN 8
  454. arg.arrayLength = ARRAY_LEN;
  455. UA_Int32* data[ARRAY_LEN];
  456. arg.data = (void**) &data;
  457. // when
  458. UA_Int32 encodingSize = UA_Variant_calcSize(&arg);
  459. // then
  460. ck_assert_int_eq(encodingSize, 1+4+ARRAY_LEN*4);
  461. #undef ARRAY_LEN
  462. }
  463. END_TEST
  464. START_TEST(UA_Variant_calcSizeVariableSizeArrayShallReturnEncodingSize)
  465. {
  466. // given
  467. UA_Variant arg;
  468. arg.encodingMask = UA_STRING_NS0 | UA_VARIANT_ENCODINGMASKTYPE_ARRAY;
  469. arg.vt = &UA_[UA_STRING];
  470. #define ARRAY_LEN 3
  471. arg.arrayLength = ARRAY_LEN;
  472. UA_String s1 = {-1, UA_NULL };
  473. UA_String s2 = {3, (UA_Byte*) "PLT" };
  474. UA_String s3 = {47, UA_NULL };
  475. UA_String* data[ARRAY_LEN] = { &s1, &s2, &s3 };
  476. arg.data = (void**) &data;
  477. // when
  478. UA_Int32 encodingSize = UA_Variant_calcSize(&arg);
  479. // then
  480. ck_assert_int_eq(encodingSize, 1+4+(4+0)+(4+3)+(4+47));
  481. #undef ARRAY_LEN
  482. }
  483. END_TEST
  484. START_TEST(UA_Variant_calcSizeVariableSizeArrayWithNullPtrWillReturnWrongButLargeEnoughEncodingSize)
  485. {
  486. // given
  487. UA_Variant arg;
  488. arg.encodingMask = UA_STRING_NS0 | UA_VARIANT_ENCODINGMASKTYPE_ARRAY;
  489. arg.vt = &UA_[UA_STRING];
  490. #define ARRAY_LEN 6
  491. arg.arrayLength = ARRAY_LEN;
  492. UA_String s1 = {-1, UA_NULL };
  493. UA_String s2 = {3, (UA_Byte*) "PLT" };
  494. UA_String s3 = {47, UA_NULL };
  495. UA_String* data[ARRAY_LEN] = { &s1, &s2, &s3 }; // will be filled with null-ptrs
  496. arg.data = (void**) &data;
  497. // when
  498. UA_Int32 encodingSize = UA_Variant_calcSize(&arg);
  499. // then
  500. ck_assert_int_ge(encodingSize, 1+4+(4+0)+(4+3)+(4+47)+(ARRAY_LEN-3)*(4+0));
  501. #undef ARRAY_LEN
  502. }
  503. END_TEST
  504. START_TEST(UA_Byte_decodeShallCopyAndAdvancePosition)
  505. {
  506. // given
  507. UA_Byte dst;
  508. UA_Byte src[] = { 0x08 };
  509. UA_Int32 pos = 0;
  510. // when
  511. UA_Int32 retval = UA_Byte_decode(src, &pos, &dst);
  512. // then
  513. ck_assert_int_eq(retval, UA_SUCCESS);
  514. ck_assert_uint_eq(pos, 1);
  515. ck_assert_uint_eq(dst, 0x08);
  516. }
  517. END_TEST
  518. START_TEST(UA_Byte_decodeShallModifyOnlyCurrentPosition)
  519. {
  520. // given
  521. UA_Byte dst[] = { 0xFF, 0xFF, 0xFF };
  522. UA_Byte src[] = { 0x08 };
  523. UA_Int32 pos = 0;
  524. // when
  525. UA_Int32 retval = UA_Byte_decode(src, &pos, &dst[1]);
  526. // then
  527. ck_assert_int_eq(retval, UA_SUCCESS);
  528. ck_assert_int_eq(pos, 1);
  529. ck_assert_uint_eq(dst[0], 0xFF);
  530. ck_assert_uint_eq(dst[1], 0x08);
  531. ck_assert_uint_eq(dst[2], 0xFF);
  532. }
  533. END_TEST
  534. START_TEST(UA_Int16_decodeShallAssumeLittleEndian)
  535. {
  536. // given
  537. UA_Int32 pos = 0;
  538. UA_Byte src[] = {
  539. 0x01,0x00, // 1
  540. 0x00,0x01, // 256
  541. };
  542. // when
  543. UA_Int16 val_01_00, val_00_01;
  544. UA_Int32 retval = UA_Int16_decode(src,&pos,&val_01_00);
  545. retval |= UA_Int16_decode(src,&pos,&val_00_01);
  546. // then
  547. ck_assert_int_eq(retval,UA_SUCCESS);
  548. ck_assert_int_eq(val_01_00,1);
  549. ck_assert_int_eq(val_00_01,256);
  550. ck_assert_int_eq(pos,4);
  551. }
  552. END_TEST
  553. START_TEST(UA_Int16_decodeShallRespectSign)
  554. {
  555. // given
  556. UA_Int32 pos = 0;
  557. UA_Byte src[] = {
  558. 0xFF,0xFF, // -1
  559. 0x00,0x80, // -32768
  560. };
  561. // when
  562. UA_Int16 val_ff_ff, val_00_80;
  563. UA_Int32 retval = UA_Int16_decode(src,&pos,&val_ff_ff);
  564. retval |= UA_Int16_decode(src,&pos,&val_00_80);
  565. // then
  566. ck_assert_int_eq(retval,UA_SUCCESS);
  567. ck_assert_int_eq(val_ff_ff,-1);
  568. ck_assert_int_eq(val_00_80,-32768);
  569. }
  570. END_TEST
  571. START_TEST(UA_UInt16_decodeShallNotRespectSign)
  572. {
  573. // given
  574. UA_Int32 pos = 0;
  575. UA_Byte src[] = {
  576. 0xFF,0xFF, // -1
  577. 0x00,0x80, // -32768
  578. };
  579. // when
  580. UA_UInt16 val_ff_ff, val_00_80;
  581. UA_Int32 retval = UA_UInt16_decode(src,&pos,&val_ff_ff);
  582. retval |= UA_UInt16_decode(src,&pos,&val_00_80);
  583. // then
  584. ck_assert_int_eq(retval,UA_SUCCESS);
  585. ck_assert_int_eq(pos,4);
  586. ck_assert_uint_eq(val_ff_ff, (0x01 << 16)-1);
  587. ck_assert_uint_eq(val_00_80, (0x01 << 15));
  588. }
  589. END_TEST
  590. START_TEST(UA_String_decodeShallAllocateMemoryAndCopyString)
  591. {
  592. // given
  593. UA_Int32 pos = 0;
  594. UA_Byte src[] = {0x08,0x00,0x00,0x00,'A','C','P','L','T',' ','U','A',0xFF,0xFF,0xFF,0xFF,0xFF};
  595. UA_String dst;
  596. // when
  597. UA_Int32 retval = UA_String_decode(src, &pos, &dst);
  598. // then
  599. ck_assert_int_eq(retval,UA_SUCCESS);
  600. ck_assert_int_eq(dst.length,8);
  601. ck_assert_ptr_eq(dst.data,UA_alloc_lastptr);
  602. ck_assert_int_eq(dst.data[3],'L');
  603. // finally
  604. UA_String_deleteMembers(&dst);
  605. }
  606. END_TEST
  607. START_TEST(UA_String_decodeWithNegativeSizeShallNotAllocateMemoryAndNullPtr)
  608. {
  609. // given
  610. UA_Int32 pos = 0;
  611. UA_Byte src[] = {0xFF,0xFF,0xFF,0xFF,'A','C','P','L','T',' ','U','A',0xFF,0xFF,0xFF,0xFF,0xFF};
  612. UA_String dst;
  613. // when
  614. UA_Int32 retval = UA_String_decode(src, &pos, &dst);
  615. // then
  616. ck_assert_int_eq(retval,UA_SUCCESS);
  617. ck_assert_int_eq(dst.length,-1);
  618. ck_assert_ptr_eq(dst.data,UA_NULL);
  619. }
  620. END_TEST
  621. START_TEST(UA_String_decodeWithZeroSizeShallNotAllocateMemoryAndNullPtr)
  622. {
  623. // given
  624. UA_Int32 pos = 0;
  625. UA_Byte src[] = {0x00,0x00,0x00,0x00,'A','C','P','L','T',' ','U','A',0xFF,0xFF,0xFF,0xFF,0xFF};
  626. UA_String dst = { 2, (UA_Byte*) "XX" };
  627. // when
  628. UA_Int32 retval = UA_String_decode(src, &pos, &dst);
  629. // then
  630. ck_assert_int_eq(retval,UA_SUCCESS);
  631. ck_assert_int_eq(dst.length,0);
  632. ck_assert_ptr_eq(dst.data,UA_NULL);
  633. }
  634. END_TEST
  635. START_TEST(UA_NodeId_decodeTwoByteShallReadTwoBytesAndSetNamespaceToZero)
  636. {
  637. // given
  638. UA_Int32 pos = 0;
  639. UA_Byte src[] = { UA_NODEIDTYPE_TWOBYTE, 0x10 };
  640. UA_NodeId dst;
  641. // when
  642. UA_Int32 retval = UA_NodeId_decode(src, &pos, &dst);
  643. // then
  644. ck_assert_int_eq(retval,UA_SUCCESS);
  645. ck_assert_int_eq(pos,2);
  646. ck_assert_int_eq(dst.encodingByte, UA_NODEIDTYPE_TWOBYTE);
  647. ck_assert_int_eq(dst.identifier.numeric,16);
  648. ck_assert_int_eq(dst.namespace,0);
  649. }
  650. END_TEST
  651. START_TEST(UA_NodeId_decodeFourByteShallReadFourBytesAndRespectNamespace)
  652. {
  653. // given
  654. UA_Int32 pos = 0;
  655. UA_Byte src[] = { UA_NODEIDTYPE_FOURBYTE, 0x01, 0x00, 0x01 };
  656. UA_NodeId dst;
  657. // when
  658. UA_Int32 retval = UA_NodeId_decode(src, &pos, &dst);
  659. // then
  660. ck_assert_int_eq(retval,UA_SUCCESS);
  661. ck_assert_int_eq(pos,4);
  662. ck_assert_int_eq(dst.encodingByte, UA_NODEIDTYPE_FOURBYTE);
  663. ck_assert_int_eq(dst.identifier.numeric,256);
  664. ck_assert_int_eq(dst.namespace,1);
  665. }
  666. END_TEST
  667. START_TEST(UA_NodeId_decodeStringShallAllocateMemory)
  668. {
  669. // given
  670. UA_Int32 pos = 0;
  671. UA_Byte src[] = { UA_NODEIDTYPE_STRING, 0x01, 0x00, 0x03, 0x00, 0x00, 0x00, 'P', 'L', 'T' };
  672. UA_NodeId dst;
  673. // when
  674. UA_Int32 retval = UA_NodeId_decode(src, &pos, &dst);
  675. // then
  676. ck_assert_int_eq(retval,UA_SUCCESS);
  677. ck_assert_int_eq(pos,10);
  678. ck_assert_int_eq(dst.encodingByte, UA_NODEIDTYPE_STRING);
  679. ck_assert_int_eq(dst.namespace,1);
  680. ck_assert_int_eq(dst.identifier.string.length,3);
  681. ck_assert_ptr_eq(dst.identifier.string.data,UA_alloc_lastptr);
  682. ck_assert_int_eq(dst.identifier.string.data[1],'L');
  683. // finally
  684. UA_NodeId_deleteMembers(&dst);
  685. }
  686. END_TEST
  687. START_TEST(UA_Variant_decodeWithOutArrayFlagSetShallSetVTAndAllocateMemoryForArray)
  688. {
  689. // given
  690. UA_Int32 pos = 0;
  691. UA_Byte src[] = { UA_INT32_NS0, 0xFF, 0x00, 0x00, 0x00};
  692. UA_Variant dst;
  693. // when
  694. UA_Int32 retval = UA_Variant_decode(src, &pos, &dst);
  695. // then
  696. ck_assert_int_eq(retval,UA_SUCCESS);
  697. ck_assert_int_eq(pos,5);
  698. ck_assert_uint_eq(dst.encodingMask, UA_INT32_NS0);
  699. ck_assert_ptr_eq(dst.vt, &UA_[UA_INT32]);
  700. ck_assert_int_eq(dst.arrayLength,1);
  701. ck_assert_ptr_ne(dst.data,UA_NULL);
  702. ck_assert_ptr_eq(dst.data[0],UA_alloc_lastptr);
  703. ck_assert_int_eq(*(UA_Int32*)dst.data[0],255);
  704. // finally
  705. UA_Variant_deleteMembers(&dst);
  706. }
  707. END_TEST
  708. START_TEST(UA_Variant_decodeWithArrayFlagSetShallSetVTAndAllocateMemoryForArray)
  709. {
  710. // given
  711. UA_Int32 pos = 0;
  712. UA_Byte src[] = { UA_INT32_NS0 | UA_VARIANT_ENCODINGMASKTYPE_ARRAY, 0x02, 0x00, 0x00, 0x00, 0xFF, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF};
  713. UA_Variant dst;
  714. // when
  715. UA_Int32 retval = UA_Variant_decode(src, &pos, &dst);
  716. // then
  717. ck_assert_int_eq(retval,UA_SUCCESS);
  718. ck_assert_int_eq(pos,1+4+2*4);
  719. ck_assert_uint_eq(dst.encodingMask & UA_VARIANT_ENCODINGMASKTYPE_TYPEID_MASK, UA_INT32_NS0);
  720. ck_assert_uint_eq(dst.encodingMask & UA_VARIANT_ENCODINGMASKTYPE_ARRAY, UA_VARIANT_ENCODINGMASKTYPE_ARRAY);
  721. ck_assert_ptr_eq(dst.vt, &UA_[UA_INT32]);
  722. ck_assert_int_eq(dst.arrayLength,2);
  723. ck_assert_ptr_ne(dst.data,UA_NULL);
  724. ck_assert_ptr_eq(dst.data[1],UA_alloc_lastptr);
  725. ck_assert_int_eq(*((UA_Int32*)dst.data[0]),255);
  726. ck_assert_int_eq(*((UA_Int32*)dst.data[1]),-1);
  727. // finally
  728. UA_Variant_deleteMembers(&dst);
  729. }
  730. END_TEST
  731. START_TEST(UA_Variant_decodeWithOutDeleteMembersShallFailInCheckMem)
  732. {
  733. // given
  734. UA_Int32 pos = 0;
  735. UA_Byte src[] = { UA_INT32_NS0 | UA_VARIANT_ENCODINGMASKTYPE_ARRAY, 0x02, 0x00, 0x00, 0x00, 0xFF, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF};
  736. UA_Variant dst;
  737. // when
  738. UA_Int32 retval = UA_Variant_decode(src, &pos, &dst);
  739. // then
  740. ck_assert_int_eq(retval,UA_SUCCESS);
  741. // finally - unfortunately we cannot express that not freeing three chunks is what we expect
  742. // UA_Variant_deleteMembers(&dst);
  743. }
  744. END_TEST
  745. START_TEST(UA_Byte_encode_test)
  746. {
  747. UA_Byte src;
  748. UA_Byte dst[2] = { 0x00, 0xFF };
  749. UA_Int32 retval, pos = 0;
  750. ck_assert_uint_eq(dst[1], 0xFF);
  751. src = 8;
  752. retval = UA_Byte_encode(&src, &pos, dst);
  753. ck_assert_uint_eq(dst[0], 0x08);
  754. ck_assert_uint_eq(dst[1], 0xFF);
  755. ck_assert_int_eq(pos, 1);
  756. ck_assert_int_eq(retval, UA_SUCCESS);
  757. src = 0xFF;
  758. dst[1] = 0x00;
  759. pos = 0;
  760. retval = UA_Byte_encode(&src, &pos, dst);
  761. ck_assert_int_eq(dst[0], 0xFF);
  762. ck_assert_int_eq(dst[1], 0x00);
  763. ck_assert_int_eq(pos, 1);
  764. ck_assert_int_eq(retval, UA_SUCCESS);
  765. }
  766. END_TEST
  767. Suite *testSuite_builtin(void)
  768. {
  769. Suite *s = suite_create("Built-in Data Types 62541-6 Table 1");
  770. TCase *tc_calcSize = tcase_create("calcSize");
  771. tcase_add_test(tc_calcSize, UA_Boolean_calcSizeWithNullArgumentShallReturnStorageSize);
  772. tcase_add_test(tc_calcSize, UA_SByte_calcSizeWithNullArgumentShallReturnStorageSize);
  773. tcase_add_test(tc_calcSize, UA_Byte_calcSizeWithNullArgumentShallReturnStorageSize);
  774. tcase_add_test(tc_calcSize, UA_Int16_calcSizeWithNullArgumentShallReturnStorageSize);
  775. tcase_add_test(tc_calcSize, UA_UInt16_calcSizeWithNullArgumentShallReturnStorageSize);
  776. tcase_add_test(tc_calcSize, UA_Int32_calcSizeWithNullArgumentShallReturnStorageSize);
  777. tcase_add_test(tc_calcSize, UA_UInt32_calcSizeWithNullArgumentShallReturnStorageSize);
  778. tcase_add_test(tc_calcSize, UA_Int64_calcSizeWithNullArgumentShallReturnStorageSize);
  779. tcase_add_test(tc_calcSize, UA_UInt64_calcSizeWithNullArgumentShallReturnStorageSize);
  780. tcase_add_test(tc_calcSize, UA_Float_calcSizeWithNullArgumentShallReturnStorageSize);
  781. tcase_add_test(tc_calcSize, UA_Double_calcSizeWithNullArgumentShallReturnStorageSize);
  782. tcase_add_test(tc_calcSize, UA_String_calcSizeWithNullArgumentShallReturnStorageSize);
  783. tcase_add_test(tc_calcSize, UA_DateTime_calcSizeWithNullArgumentShallReturnStorageSize);
  784. tcase_add_test(tc_calcSize, UA_Guid_calcSizeWithNullArgumentShallReturnStorageSize);
  785. tcase_add_test(tc_calcSize, UA_ByteString_calcSizeWithNullArgumentShallReturnStorageSize);
  786. tcase_add_test(tc_calcSize, UA_XmlElement_calcSizeWithNullArgumentShallReturnStorageSize);
  787. tcase_add_test(tc_calcSize, UA_NodeId_calcSizeWithNullArgumentShallReturnStorageSize);
  788. tcase_add_test(tc_calcSize, UA_ExpandedNodeId_calcSizeWithNullArgumentShallReturnStorageSize);
  789. tcase_add_test(tc_calcSize, UA_StatusCode_calcSizeWithNullArgumentShallReturnStorageSize);
  790. tcase_add_test(tc_calcSize, UA_QualifiedName_calcSizeWithNullArgumentShallReturnStorageSize);
  791. tcase_add_test(tc_calcSize, UA_LocalizedText_calcSizeWithNullArgumentShallReturnStorageSize);
  792. tcase_add_test(tc_calcSize, UA_ExtensionObject_calcSizeWithNullArgumentShallReturnStorageSize);
  793. tcase_add_test(tc_calcSize, UA_DataValue_calcSizeWithNullArgumentShallReturnStorageSize);
  794. tcase_add_test(tc_calcSize, UA_Variant_calcSizeWithNullArgumentShallReturnStorageSize);
  795. tcase_add_test(tc_calcSize, UA_DiagnosticInfo_calcSizeWithNullArgumentShallReturnStorageSize);
  796. tcase_add_test(tc_calcSize, UA_String_calcSizeShallReturnEncodingSize);
  797. tcase_add_test(tc_calcSize, UA_String_calcSizeWithNegativLengthShallReturnEncodingSize);
  798. tcase_add_test(tc_calcSize, UA_String_calcSizeWithNegativLengthAndValidPointerShallReturnEncodingSize);
  799. tcase_add_test(tc_calcSize, UA_String_calcSizeWithZeroLengthShallReturnEncodingSize);
  800. tcase_add_test(tc_calcSize, UA_String_calcSizeWithZeroLengthAndValidPointerShallReturnEncodingSize);
  801. tcase_add_test(tc_calcSize, UA_NodeId_calcSizeEncodingTwoByteShallReturnEncodingSize);
  802. tcase_add_test(tc_calcSize, UA_NodeId_calcSizeEncodingFourByteShallReturnEncodingSize);
  803. tcase_add_test(tc_calcSize, UA_NodeId_calcSizeEncodingStringShallReturnEncodingSize);
  804. tcase_add_test(tc_calcSize, UA_NodeId_calcSizeEncodingStringNegativLengthShallReturnEncodingSize);
  805. tcase_add_test(tc_calcSize, UA_NodeId_calcSizeEncodingStringZeroLengthShallReturnEncodingSize);
  806. tcase_add_test(tc_calcSize, UA_ExpandedNodeId_calcSizeEncodingStringAndServerIndexShallReturnEncodingSize);
  807. tcase_add_test(tc_calcSize, UA_ExpandedNodeId_calcSizeEncodingStringAndNamespaceUriShallReturnEncodingSize);
  808. tcase_add_test(tc_calcSize, UA_Guid_calcSizeShallReturnEncodingSize);
  809. tcase_add_test(tc_calcSize, UA_Guid_calcSizeShallReturnEncodingSize);
  810. tcase_add_test(tc_calcSize, UA_LocalizedText_calcSizeTextOnlyShallReturnEncodingSize);
  811. tcase_add_test(tc_calcSize, UA_LocalizedText_calcSizeLocaleOnlyShallReturnEncodingSize);
  812. tcase_add_test(tc_calcSize, UA_LocalizedText_calcSizeTextAndLocaleShallReturnEncodingSize);
  813. tcase_add_test(tc_calcSize, UA_Variant_calcSizeFixedSizeArrayShallReturnEncodingSize);
  814. tcase_add_test(tc_calcSize, UA_Variant_calcSizeVariableSizeArrayShallReturnEncodingSize);
  815. tcase_add_test(tc_calcSize, UA_Variant_calcSizeVariableSizeArrayWithNullPtrWillReturnWrongButLargeEnoughEncodingSize);
  816. tcase_add_test(tc_calcSize, UA_Variant_decodeWithOutDeleteMembersShallFailInCheckMem);
  817. suite_add_tcase(s,tc_calcSize);
  818. TCase *tc_decode = tcase_create("decode");
  819. tcase_add_test(tc_decode, UA_Byte_decodeShallCopyAndAdvancePosition);
  820. tcase_add_test(tc_decode, UA_Byte_decodeShallModifyOnlyCurrentPosition);
  821. tcase_add_test(tc_decode, UA_Int16_decodeShallAssumeLittleEndian);
  822. tcase_add_test(tc_decode, UA_Int16_decodeShallRespectSign);
  823. tcase_add_test(tc_decode, UA_UInt16_decodeShallNotRespectSign);
  824. tcase_add_test(tc_decode, UA_String_decodeShallAllocateMemoryAndCopyString);
  825. tcase_add_test(tc_decode, UA_String_decodeWithNegativeSizeShallNotAllocateMemoryAndNullPtr);
  826. tcase_add_test(tc_decode, UA_String_decodeWithZeroSizeShallNotAllocateMemoryAndNullPtr);
  827. tcase_add_test(tc_decode, UA_NodeId_decodeTwoByteShallReadTwoBytesAndSetNamespaceToZero);
  828. tcase_add_test(tc_decode, UA_NodeId_decodeFourByteShallReadFourBytesAndRespectNamespace);
  829. tcase_add_test(tc_decode, UA_NodeId_decodeStringShallAllocateMemory);
  830. tcase_add_test(tc_decode, UA_Variant_decodeWithOutArrayFlagSetShallSetVTAndAllocateMemoryForArray);
  831. tcase_add_test(tc_decode, UA_Variant_decodeWithArrayFlagSetShallSetVTAndAllocateMemoryForArray);
  832. suite_add_tcase(s,tc_decode);
  833. return s;
  834. }
  835. int main (void)
  836. {
  837. int number_failed = 0;
  838. Suite* s;
  839. SRunner* sr;
  840. s = testSuite_builtin();
  841. sr = srunner_create(s);
  842. srunner_run_all(sr,CK_NORMAL);
  843. number_failed += srunner_ntests_failed(sr);
  844. srunner_free(sr);
  845. return (number_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
  846. }