check_encode.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430
  1. /*
  2. ============================================================================
  3. Name : check_encode.c
  4. Author :
  5. Version :
  6. Copyright : Your copyright notice
  7. Description :
  8. ============================================================================
  9. */
  10. #include <stdio.h>
  11. #include <stdlib.h>
  12. #include "opcua.h"
  13. #include "opcua_transportLayer.h"
  14. #include "check.h"
  15. START_TEST(encodeByte_test)
  16. {
  17. UA_ByteString rawMessage;
  18. UA_Int32 position = 0;
  19. //EncodeByte
  20. UA_Byte *mem = malloc(sizeof(UA_Byte));
  21. rawMessage.data = mem;
  22. UA_Byte testByte = 0x08;
  23. rawMessage.length = 1;
  24. position = 0;
  25. UA_Byte_encode(&(testByte), &position, rawMessage.data);
  26. ck_assert_int_eq(rawMessage.data[0], 0x08);
  27. ck_assert_int_eq(rawMessage.length, 1);
  28. ck_assert_int_eq(position, 1);
  29. free(mem);
  30. }
  31. END_TEST
  32. START_TEST(encodeInt16_test)
  33. {
  34. UA_ByteString rawMessage;
  35. UA_Int32 position = 0;
  36. //EncodeUInt16
  37. UA_Byte *mem = malloc(sizeof(UA_UInt16));
  38. rawMessage.data = mem;
  39. UA_UInt16 testUInt16 = 1;
  40. rawMessage.length = 2;
  41. position = 0;
  42. UA_UInt16_encode(&testUInt16, &position, rawMessage.data);
  43. ck_assert_int_eq(position, 2);
  44. UA_Int32 p = 0;
  45. UA_UInt16 val;
  46. UA_UInt16_decode(rawMessage.data, &p, &val);
  47. ck_assert_int_eq(val,testUInt16);
  48. //ck_assert_int_eq(rawMessage.data[0], 0xAB);
  49. free(mem);
  50. }
  51. END_TEST
  52. START_TEST(encodeUInt16_test)
  53. {
  54. UA_ByteString rawMessage;
  55. UA_Int32 position = 0;
  56. //EncodeUInt16
  57. UA_Byte *mem = (UA_Byte*) malloc(sizeof(UA_UInt16));
  58. rawMessage.data = mem;
  59. UA_UInt16 testUInt16 = 1;
  60. rawMessage.length = 2;
  61. position = 0;
  62. UA_UInt16_encode(&testUInt16, &position, rawMessage.data);
  63. ck_assert_int_eq(position, 2);
  64. UA_Int32 p = 0;
  65. UA_UInt16 val;
  66. UA_UInt16_decode(rawMessage.data, &p, &val);
  67. ck_assert_int_eq(val,testUInt16);
  68. //ck_assert_int_eq(rawMessage.data[0], 0xAB);
  69. free(mem);
  70. }
  71. END_TEST
  72. START_TEST(encodeUInt32_test)
  73. {
  74. UA_ByteString rawMessage;
  75. UA_UInt32 value = 0x0101FF00;
  76. //EncodeUInt16
  77. rawMessage.data = (UA_Byte*) malloc(2 * sizeof(UA_UInt32));
  78. rawMessage.length = 8;
  79. UA_Int32 p = 4;
  80. UA_UInt32_encode(&value,&p,rawMessage.data);
  81. ck_assert_uint_eq(rawMessage.data[4],0x00);
  82. ck_assert_uint_eq(rawMessage.data[5],0xFF);
  83. ck_assert_uint_eq(rawMessage.data[6],0x01);
  84. ck_assert_uint_eq(rawMessage.data[7],0x01);
  85. ck_assert_int_eq(p,8);
  86. free(rawMessage.data);
  87. }
  88. END_TEST
  89. START_TEST(encodeInt32_test)
  90. {
  91. }
  92. END_TEST
  93. START_TEST(encodeUInt64_test)
  94. {
  95. UA_ByteString rawMessage;
  96. UA_UInt64 value = 0x0101FF00FF00FF00;
  97. //EncodeUInt16
  98. rawMessage.data = (UA_Byte*) malloc(sizeof(UA_UInt64));
  99. rawMessage.length = 8;
  100. UA_Int32 p = 0;
  101. UA_UInt64_encode(&value, &p,rawMessage.data);
  102. ck_assert_uint_eq((UA_Byte)rawMessage.data[0],0x00);
  103. ck_assert_uint_eq((UA_Byte)rawMessage.data[1],0xFF);
  104. ck_assert_uint_eq((UA_Byte)rawMessage.data[2],0x00);
  105. ck_assert_uint_eq((UA_Byte)rawMessage.data[3],0xFF);
  106. ck_assert_uint_eq((UA_Byte)rawMessage.data[4],0x00);
  107. ck_assert_uint_eq((UA_Byte)rawMessage.data[5],0xFF);
  108. ck_assert_uint_eq((UA_Byte)rawMessage.data[6],0x01);
  109. ck_assert_uint_eq((UA_Byte)rawMessage.data[7],0x01);
  110. free(rawMessage.data);
  111. }
  112. END_TEST
  113. START_TEST(encodeInt64_test)
  114. {
  115. UA_ByteString rawMessage;
  116. UA_UInt64 value = 0x0101FF00FF00FF00;
  117. //EncodeUInt16
  118. rawMessage.data = (UA_Byte*) malloc(sizeof(UA_UInt64));
  119. rawMessage.length = 8;
  120. UA_Int32 p = 0;
  121. UA_UInt64_encode(&value, &p,rawMessage.data);
  122. ck_assert_uint_eq(rawMessage.data[0],0x00);
  123. ck_assert_uint_eq(rawMessage.data[1],0xFF);
  124. ck_assert_uint_eq(rawMessage.data[2],0x00);
  125. ck_assert_uint_eq(rawMessage.data[3],0xFF);
  126. ck_assert_uint_eq(rawMessage.data[4],0x00);
  127. ck_assert_uint_eq(rawMessage.data[5],0xFF);
  128. ck_assert_uint_eq(rawMessage.data[6],0x01);
  129. ck_assert_uint_eq(rawMessage.data[7],0x01);
  130. free(rawMessage.data);
  131. }
  132. END_TEST
  133. START_TEST(encodeFloat_test)
  134. {
  135. UA_Float value = -6.5;
  136. UA_Int32 pos = 0;
  137. UA_Byte* buf = (UA_Byte*)malloc(sizeof(UA_Float));
  138. UA_Float_encode(&value,&pos,buf);
  139. ck_assert_uint_eq(buf[2],0xD0);
  140. ck_assert_uint_eq(buf[3],0xC0);
  141. free(buf);
  142. }
  143. END_TEST
  144. /*START_TEST(encodeDouble_test)
  145. {
  146. UA_Double value = -6.5;
  147. UA_Int32 pos = 0;
  148. UA_Byte* buf = (char*)malloc(sizeof(UA_Double));
  149. UA_Double_encode(&value,&pos,buf);
  150. ck_assert_uint_eq(buf[6],0xD0);
  151. ck_assert_uint_eq(buf[7],0xC0);
  152. free(buf);
  153. }
  154. END_TEST*/
  155. START_TEST(encodeUAString_test)
  156. {
  157. UA_Int32 pos = 0;
  158. UA_String string;
  159. UA_Int32 l = 11;
  160. UA_Byte mem[11] = "ACPLT OPCUA";
  161. UA_Byte *dstBuf = (UA_Byte*) malloc(sizeof(UA_Int32)+l);
  162. string.data = mem;
  163. string.length = 11;
  164. UA_String_encode(&string, &pos, dstBuf);
  165. ck_assert_int_eq(dstBuf[0],11);
  166. ck_assert_int_eq(dstBuf[0+sizeof(UA_Int32)],'A');
  167. free(dstBuf);
  168. }
  169. END_TEST
  170. START_TEST(encodeDataValue_test)
  171. {
  172. UA_DataValue dataValue;
  173. UA_Int32 pos = 0, retval;
  174. UA_Byte* buf = (UA_Byte*) malloc(15);
  175. UA_DateTime dateTime;
  176. dateTime = 80;
  177. dataValue.serverTimestamp = dateTime;
  178. //--without Variant
  179. dataValue.encodingMask = UA_DATAVALUE_SERVERTIMPSTAMP; //Only the sourcePicoseconds
  180. UA_DataValue_encode(&dataValue, &pos, buf);
  181. ck_assert_int_eq(pos, 9);// represents the length
  182. ck_assert_uint_eq(buf[0], 0x08); // encodingMask
  183. ck_assert_uint_eq(buf[1], 80); // 8 Byte serverTimestamp
  184. ck_assert_uint_eq(buf[2], 0);
  185. ck_assert_uint_eq(buf[3], 0);
  186. ck_assert_uint_eq(buf[4], 0);
  187. ck_assert_uint_eq(buf[5], 0);
  188. ck_assert_uint_eq(buf[6], 0);
  189. ck_assert_uint_eq(buf[7], 0);
  190. ck_assert_uint_eq(buf[8], 0);
  191. //TestCase for a DataValue with a Variant!
  192. dataValue.encodingMask = UA_DATAVALUE_VARIANT | UA_DATAVALUE_SERVERTIMPSTAMP; //Variant & SourvePicoseconds
  193. dataValue.value.vt = &UA_[UA_INT32];
  194. dataValue.value.arrayLength = 0;
  195. dataValue.value.encodingMask = UA_INT32_NS0;
  196. UA_Int32 data = 45;
  197. UA_Int32* pdata = &data;
  198. dataValue.value.data = (void**) &pdata;
  199. pos = 0;
  200. retval = UA_DataValue_encode(&dataValue, &pos, buf);
  201. ck_assert_int_eq(retval, UA_SUCCESS);
  202. ck_assert_int_eq(pos, 1+(1+4)+8);// represents the length
  203. ck_assert_uint_eq(buf[0], 0x08 | 0x01); // encodingMask
  204. ck_assert_uint_eq(buf[1], 0x06); // Variant's Encoding Mask - INT32
  205. ck_assert_uint_eq(buf[2], 45); // the single value
  206. ck_assert_uint_eq(buf[3], 0);
  207. ck_assert_uint_eq(buf[4], 0);
  208. ck_assert_uint_eq(buf[5], 0);
  209. ck_assert_uint_eq(buf[6], 80); // the server timestamp
  210. ck_assert_uint_eq(buf[7], 0);
  211. free(buf);
  212. }
  213. END_TEST
  214. Suite *testSuite_encodeByte(void)
  215. {
  216. Suite *s = suite_create("encodeByte_test");
  217. TCase *tc_core = tcase_create("Core");
  218. tcase_add_test(tc_core, encodeByte_test);
  219. suite_add_tcase(s,tc_core);
  220. return s;
  221. }
  222. Suite*testSuite_encodeInt16(void)
  223. {
  224. Suite *s = suite_create("encodeInt16_test");
  225. TCase *tc_core = tcase_create("Core");
  226. tcase_add_test(tc_core, encodeInt16_test);
  227. suite_add_tcase(s,tc_core);
  228. return s;
  229. }
  230. Suite*testSuite_encodeUInt16(void)
  231. {
  232. Suite *s = suite_create("encodeUInt16_test");
  233. TCase *tc_core = tcase_create("Core");
  234. tcase_add_test(tc_core, encodeUInt16_test);
  235. suite_add_tcase(s,tc_core);
  236. return s;
  237. }
  238. Suite*testSuite_encodeUInt32(void)
  239. {
  240. Suite *s = suite_create("encodeUInt32_test");
  241. TCase *tc_core = tcase_create("Core");
  242. tcase_add_test(tc_core, encodeUInt32_test);
  243. suite_add_tcase(s,tc_core);
  244. return s;
  245. }
  246. Suite*testSuite_encodeInt32(void)
  247. {
  248. Suite *s = suite_create("encodeInt32_test");
  249. TCase *tc_core = tcase_create("Core");
  250. tcase_add_test(tc_core, encodeInt32_test);
  251. suite_add_tcase(s,tc_core);
  252. return s;
  253. }
  254. Suite*testSuite_encodeUInt64(void)
  255. {
  256. Suite *s = suite_create("encodeUInt64_test");
  257. TCase *tc_core = tcase_create("Core");
  258. tcase_add_test(tc_core, encodeUInt64_test);
  259. suite_add_tcase(s,tc_core);
  260. return s;
  261. }
  262. Suite*testSuite_encodeInt64(void)
  263. {
  264. Suite *s = suite_create("encodeInt64_test");
  265. TCase *tc_core = tcase_create("Core");
  266. tcase_add_test(tc_core, encodeInt64_test);
  267. suite_add_tcase(s,tc_core);
  268. return s;
  269. }
  270. Suite *testSuite_encodeFloat(void)
  271. {
  272. Suite *s = suite_create("encodeFloat_test");
  273. TCase *tc_core = tcase_create("Core");
  274. tcase_add_test(tc_core, encodeFloat_test);
  275. suite_add_tcase(s,tc_core);
  276. return s;
  277. }
  278. /*Suite *testSuite_encodeDouble(void)
  279. {
  280. Suite *s = suite_create("encodeDouble_test");
  281. TCase *tc_core = tcase_create("Core");
  282. tcase_add_test(tc_core, encodeDouble_test);
  283. suite_add_tcase(s,tc_core);
  284. return s;
  285. }*/
  286. Suite * testSuite_encodeUAString(void)
  287. {
  288. Suite *s = suite_create("encodeUAString_test");
  289. TCase *tc_core = tcase_create("Core");
  290. tcase_add_test(tc_core, encodeUAString_test);
  291. suite_add_tcase(s,tc_core);
  292. return s;
  293. }
  294. Suite* testSuite_encodeDataValue()
  295. {
  296. Suite *s = suite_create("encodeDataValue");
  297. TCase *tc_core = tcase_create("Core");
  298. tcase_add_test(tc_core, encodeDataValue_test);
  299. suite_add_tcase(s,tc_core);
  300. return s;
  301. }
  302. int main (void)
  303. {
  304. int number_failed = 0;
  305. Suite *s = testSuite_encodeByte();
  306. SRunner *sr = srunner_create(s);
  307. srunner_run_all(sr,CK_NORMAL);
  308. number_failed += srunner_ntests_failed(sr);
  309. srunner_free(sr);
  310. s = testSuite_encodeInt16();
  311. sr = srunner_create(s);
  312. srunner_run_all(sr,CK_NORMAL);
  313. number_failed += srunner_ntests_failed(sr);
  314. srunner_free(sr);
  315. s = testSuite_encodeUInt16();
  316. sr = srunner_create(s);
  317. srunner_run_all(sr,CK_NORMAL);
  318. number_failed += srunner_ntests_failed(sr);
  319. srunner_free(sr);
  320. s = testSuite_encodeUInt32();
  321. sr = srunner_create(s);
  322. srunner_run_all(sr,CK_NORMAL);
  323. number_failed += srunner_ntests_failed(sr);
  324. srunner_free(sr);
  325. s = testSuite_encodeInt32();
  326. sr = srunner_create(s);
  327. srunner_run_all(sr,CK_NORMAL);
  328. number_failed += srunner_ntests_failed(sr);
  329. srunner_free(sr);
  330. s = testSuite_encodeUInt64();
  331. sr = srunner_create(s);
  332. srunner_run_all(sr,CK_NORMAL);
  333. number_failed += srunner_ntests_failed(sr);
  334. srunner_free(sr);
  335. s = testSuite_encodeInt64();
  336. sr = srunner_create(s);
  337. srunner_run_all(sr,CK_NORMAL);
  338. number_failed += srunner_ntests_failed(sr);
  339. srunner_free(sr);
  340. s = testSuite_encodeFloat();
  341. sr = srunner_create(s);
  342. srunner_run_all(sr,CK_NORMAL);
  343. number_failed += srunner_ntests_failed(sr);
  344. srunner_free(sr);
  345. /* s = testSuite_encodeDouble();
  346. sr = srunner_create(s);
  347. srunner_run_all(sr,CK_NORMAL);
  348. number_failed += srunner_ntests_failed(sr);
  349. srunner_free(sr);*/
  350. s = testSuite_encodeUAString();
  351. sr = srunner_create(s);
  352. srunner_run_all(sr,CK_NORMAL);
  353. number_failed += srunner_ntests_failed(sr);
  354. srunner_free(sr);
  355. s = testSuite_encodeDataValue();
  356. sr = srunner_create(s);
  357. srunner_run_all(sr,CK_NORMAL);
  358. number_failed += srunner_ntests_failed(sr);
  359. srunner_free(sr);
  360. /* <TESTSUITE_TEMPLATE>
  361. s = <TESTSUITENAME>;
  362. sr = srunner_create(s);
  363. srunner_run_all(sr,CK_NORMAL);
  364. number_failed += srunner_ntests_failed(sr);
  365. srunner_free(sr);
  366. */
  367. return (number_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
  368. }