check_decode.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  1. /*
  2. ============================================================================
  3. Name : check_decode.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(decodeByte_test)
  16. {
  17. UA_ByteString rawMessage;
  18. UA_Int32 position = 0;
  19. //EncodeByte
  20. UA_Byte* mem = (UA_Byte*) malloc(sizeof(UA_Byte));
  21. UA_Byte val;
  22. rawMessage.data = mem;
  23. rawMessage.length = 1;
  24. mem[0] = 0x08;
  25. position = 0;
  26. UA_Byte_decode(rawMessage.data, &position, &val);
  27. ck_assert_int_eq(val, 0x08);
  28. ck_assert_int_eq(position, 1);
  29. free(mem);
  30. }
  31. END_TEST
  32. START_TEST(decodeInt16_test_positives)
  33. {
  34. UA_Int32 p = 0;
  35. UA_Int16 val;
  36. UA_ByteString rawMessage;
  37. UA_Byte mem[] = {
  38. 0x00,0x00, // 0
  39. 0x01,0x00, // 1
  40. 0xFF,0x00, // 255
  41. 0x00,0x01, // 256
  42. };
  43. rawMessage.data = mem;
  44. rawMessage.length = sizeof(mem);
  45. ck_assert_int_eq(rawMessage.length,8);
  46. UA_Int16_decode(rawMessage.data,&p,&val);
  47. ck_assert_int_eq(val,0);
  48. UA_Int16_decode(rawMessage.data,&p,&val);
  49. ck_assert_int_eq(val,1);
  50. UA_Int16_decode(rawMessage.data,&p,&val);
  51. ck_assert_int_eq(val,255);
  52. UA_Int16_decode(rawMessage.data,&p,&val);
  53. ck_assert_int_eq(val,256);
  54. }
  55. END_TEST
  56. START_TEST(decodeInt16_test_negatives)
  57. {
  58. UA_Int32 p = 0;
  59. UA_Int16 val;
  60. UA_ByteString rawMessage;
  61. UA_Byte mem[] = {
  62. 0xFF,0xFF, // -1
  63. 0x00,0x80, // -32768
  64. };
  65. rawMessage.data = mem;
  66. rawMessage.length = sizeof(mem);
  67. ck_assert_int_eq(rawMessage.length,4);
  68. UA_Int16_decode(rawMessage.data,&p,&val);
  69. ck_assert_int_eq(val,-1);
  70. UA_Int16_decode(rawMessage.data,&p,&val);
  71. ck_assert_int_eq(val,-32768);
  72. }
  73. END_TEST
  74. START_TEST(decodeUInt16_test)
  75. {
  76. UA_ByteString rawMessage;
  77. //EncodeUInt16
  78. UA_Byte mem[2] = {0x01,0x00};
  79. rawMessage.data = mem;
  80. rawMessage.length = 2;
  81. //encodeUInt16(testUInt16, &position, &rawMessage);
  82. UA_Int32 p = 0;
  83. UA_UInt16 val;
  84. UA_UInt16_decode(rawMessage.data,&p,&val);
  85. ck_assert_int_eq(val,1);
  86. //ck_assert_int_eq(p, 2);
  87. //ck_assert_int_eq(rawMessage.data[0], 0xAB);
  88. }
  89. END_TEST
  90. START_TEST(decodeUInt32_test)
  91. {
  92. UA_ByteString rawMessage;
  93. //EncodeUInt16
  94. UA_Byte mem[4] = {0xFF,0x00,0x00,0x00};
  95. rawMessage.data = mem;
  96. rawMessage.length = 4;
  97. UA_Int32 p = 0;
  98. UA_UInt32 val;
  99. UA_UInt32_decode(rawMessage.data, &p, &val);
  100. ck_assert_uint_eq(val,255);
  101. }
  102. END_TEST
  103. START_TEST(decodeInt32_test)
  104. {
  105. UA_ByteString rawMessage;
  106. //EncodeUInt16
  107. UA_Byte mem[4] = {0x00,0xCA,0x9A,0x3B};
  108. rawMessage.data = mem;
  109. rawMessage.length = 4;
  110. UA_Int32 p = 0;
  111. UA_Int32 val;
  112. UA_Int32_decode(rawMessage.data, &p, &val);
  113. ck_assert_int_eq(val,1000000000);
  114. }
  115. END_TEST
  116. START_TEST(decodeUInt64_test)
  117. {
  118. UA_ByteString rawMessage;
  119. UA_UInt64 expectedVal = 0xFF;
  120. expectedVal = expectedVal << 56;
  121. UA_Byte mem[8] = {00,00,00,00,0x00,0x00,0x00,0xFF};
  122. rawMessage.data = mem;
  123. rawMessage.length = 8;
  124. UA_Int32 p = 0;
  125. UA_UInt64 val;
  126. UA_UInt64_decode(rawMessage.data, &p, &val);
  127. ck_assert_uint_eq(val, expectedVal);
  128. }
  129. END_TEST
  130. START_TEST(decodeInt64_test)
  131. {
  132. UA_ByteString rawMessage;
  133. UA_Int64 expectedVal = 0xFF;
  134. expectedVal = expectedVal << 56;
  135. UA_Byte mem[8] = {00,00,00,00,0x00,0x00,0x00,0xFF};
  136. rawMessage.data = mem;
  137. rawMessage.length = 8;
  138. UA_Int32 p = 0;
  139. UA_Int64 val;
  140. UA_Int64_decode(rawMessage.data, &p, &val);
  141. ck_assert_uint_eq(val, expectedVal);
  142. }
  143. END_TEST
  144. START_TEST(decodeFloat_test)
  145. {
  146. UA_Int32 pos = 0;
  147. UA_Byte buf[4] = {0x00,0x00,0xD0,0xC0};
  148. UA_Float fval;
  149. UA_Float_decode(buf, &pos, &fval);
  150. //val should be -6.5
  151. UA_Int32 val = (fval > -6.501 && fval < -6.499);
  152. ck_assert_int_gt(val,0);
  153. }
  154. END_TEST
  155. START_TEST(decodeDouble_test)
  156. {
  157. }
  158. END_TEST
  159. START_TEST(decodeUAString_test)
  160. {
  161. UA_Int32 pos = 0;
  162. UA_String string;
  163. UA_Byte binString[12] = {0x08,0x00,0x00,0x00,'A','C','P','L','T',' ','U','A'};
  164. UA_String_decode(binString, &pos, &string);
  165. ck_assert_int_eq(string.length,8);
  166. ck_assert_ptr_eq(string.data,UA_alloc_lastptr);
  167. ck_assert_int_eq(string.data[3],'L');
  168. UA_String_deleteMembers(&string);
  169. }
  170. END_TEST
  171. Suite *testSuite_decodeByte(void)
  172. {
  173. Suite *s = suite_create("encodeByte_test");
  174. TCase *tc_core = tcase_create("Core");
  175. tcase_add_test(tc_core, decodeByte_test);
  176. suite_add_tcase(s,tc_core);
  177. return s;
  178. }
  179. Suite *testSuite_decodeInt16(void)
  180. {
  181. Suite *s = suite_create("decodeInt16_test");
  182. TCase *tc_core = tcase_create("Core");
  183. tcase_add_test(tc_core, decodeInt16_test_positives);
  184. tcase_add_test(tc_core, decodeInt16_test_negatives);
  185. suite_add_tcase(s,tc_core);
  186. return s;
  187. }
  188. Suite *testSuite_decodeUInt16(void)
  189. {
  190. Suite *s = suite_create("decodeUInt16_test");
  191. TCase *tc_core = tcase_create("Core");
  192. tcase_add_test(tc_core, decodeUInt16_test);
  193. suite_add_tcase(s,tc_core);
  194. return s;
  195. }
  196. Suite*testSuite_decodeUInt32(void)
  197. {
  198. Suite *s = suite_create("decodeUInt32_test");
  199. TCase *tc_core = tcase_create("Core");
  200. tcase_add_test(tc_core, decodeUInt32_test);
  201. suite_add_tcase(s,tc_core);
  202. return s;
  203. }
  204. Suite*testSuite_decodeInt32(void)
  205. {
  206. Suite *s = suite_create("decodeInt32_test");
  207. TCase *tc_core = tcase_create("Core");
  208. tcase_add_test(tc_core, decodeInt32_test);
  209. suite_add_tcase(s,tc_core);
  210. return s;
  211. }
  212. Suite*testSuite_decodeInt64(void)
  213. {
  214. Suite *s = suite_create("decodeInt64_test");
  215. TCase *tc_core = tcase_create("Core");
  216. tcase_add_test(tc_core, decodeInt64_test);
  217. suite_add_tcase(s,tc_core);
  218. return s;
  219. }
  220. Suite*testSuite_decodeUInt64(void)
  221. {
  222. Suite *s = suite_create("decodeUInt64_test");
  223. TCase *tc_core = tcase_create("Core");
  224. tcase_add_test(tc_core, decodeUInt64_test);
  225. suite_add_tcase(s,tc_core);
  226. return s;
  227. }
  228. Suite *testSuite_decodeFloat(void)
  229. {
  230. Suite *s = suite_create("decodeFloat_test");
  231. TCase *tc_core = tcase_create("Core");
  232. tcase_add_test(tc_core, decodeFloat_test);
  233. suite_add_tcase(s,tc_core);
  234. return s;
  235. }
  236. Suite *testSuite_decodeDouble(void)
  237. {
  238. Suite *s = suite_create("decodeDouble_test");
  239. TCase *tc_core = tcase_create("Core");
  240. tcase_add_test(tc_core, decodeDouble_test);
  241. suite_add_tcase(s,tc_core);
  242. return s;
  243. }
  244. Suite * testSuite_decodeUAString(void)
  245. {
  246. Suite *s = suite_create("decodeUAString_test");
  247. TCase *tc_core = tcase_create("Core");
  248. tcase_add_test(tc_core, decodeUAString_test);
  249. suite_add_tcase(s,tc_core);
  250. return s;
  251. }
  252. int main (void)
  253. {
  254. int number_failed = 0;
  255. Suite *s = testSuite_decodeByte();
  256. SRunner *sr = srunner_create(s);
  257. srunner_run_all(sr,CK_NORMAL);
  258. number_failed += srunner_ntests_failed(sr);
  259. srunner_free(sr);
  260. s = testSuite_decodeInt16();
  261. sr = srunner_create(s);
  262. srunner_run_all(sr,CK_NORMAL);
  263. number_failed += srunner_ntests_failed(sr);
  264. srunner_free(sr);
  265. s = testSuite_decodeUInt16();
  266. sr = srunner_create(s);
  267. srunner_run_all(sr,CK_NORMAL);
  268. number_failed += srunner_ntests_failed(sr);
  269. srunner_free(sr);
  270. s = testSuite_decodeUInt32();
  271. sr = srunner_create(s);
  272. srunner_run_all(sr,CK_NORMAL);
  273. number_failed += srunner_ntests_failed(sr);
  274. srunner_free(sr);
  275. s = testSuite_decodeInt32();
  276. sr = srunner_create(s);
  277. srunner_run_all(sr,CK_NORMAL);
  278. number_failed += srunner_ntests_failed(sr);
  279. srunner_free(sr);
  280. s = testSuite_decodeUInt64();
  281. sr = srunner_create(s);
  282. srunner_run_all(sr,CK_NORMAL);
  283. number_failed += srunner_ntests_failed(sr);
  284. srunner_free(sr);
  285. s = testSuite_decodeInt64();
  286. sr = srunner_create(s);
  287. srunner_run_all(sr,CK_NORMAL);
  288. number_failed += srunner_ntests_failed(sr);
  289. srunner_free(sr);
  290. s = testSuite_decodeUAString();
  291. sr = srunner_create(s);
  292. srunner_run_all(sr,CK_NORMAL);
  293. number_failed += srunner_ntests_failed(sr);
  294. srunner_free(sr);
  295. /* <TESTSUITE_TEMPLATE>
  296. s = <TESTSUITENAME>;
  297. sr = srunner_create(s);
  298. srunner_run_all(sr,CK_NORMAL);
  299. number_failed += srunner_ntests_failed(sr);
  300. srunner_free(sr);
  301. */
  302. return (number_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
  303. }