test-gobex-apparam.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. *
  4. * OBEX library with GLib integration
  5. *
  6. * Copyright (C) 2012 Intel Corporation.
  7. *
  8. */
  9. #ifdef HAVE_CONFIG_H
  10. #include <config.h>
  11. #endif
  12. #include <stdint.h>
  13. #include <string.h>
  14. #include "gobex/gobex.h"
  15. #include "gobex/gobex-apparam.h"
  16. #include "util.h"
  17. #define TAG_U8 0x00
  18. #define TAG_U16 0x01
  19. #define TAG_U32 0x02
  20. #define TAG_U64 0x03
  21. #define TAG_STRING 0x04
  22. #define TAG_BYTES 0x05
  23. static uint8_t tag_nval_short[] = { TAG_U8 };
  24. static uint8_t tag_nval_data[] = { TAG_U8, 0x01 };
  25. static uint8_t tag_nval2_short[] = { TAG_U8, 0x01, 0x1, TAG_U16 };
  26. static uint8_t tag_nval2_data[] = { TAG_U8, 0x01, 0x1, TAG_U16, 0x02 };
  27. static uint8_t tag_uint8[] = { TAG_U8, 0x01, 0x01 };
  28. static uint8_t tag_uint16[] = { TAG_U16, 0x02, 0x01, 0x02 };
  29. static uint8_t tag_uint32[] = { TAG_U32, 0x04, 0x01, 0x02, 0x03, 0x04 };
  30. static uint8_t tag_uint64[] = { TAG_U64, 0x08, 0x01, 0x02, 0x03, 0x04,
  31. 0x05, 0x06, 0x07, 0x08 };
  32. static uint8_t tag_string[] = { TAG_STRING, 0x04, 'A', 'B', 'C', '\0' };
  33. static uint8_t tag_bytes[257] = { TAG_BYTES, 0xFF };
  34. static uint8_t tag_multi[] = { TAG_U8, 0x01, 0x01,
  35. TAG_U16, 0x02, 0x01, 0x02,
  36. TAG_U32, 0x04, 0x01, 0x02, 0x03, 0x04,
  37. TAG_U64, 0x08, 0x01, 0x02, 0x03, 0x04,
  38. 0x05, 0x06, 0x07, 0x08,
  39. TAG_STRING, 0x04, 'A', 'B', 'C', '\0' };
  40. static GObexApparam *parse_and_decode(const void *data, gsize size)
  41. {
  42. GObexApparam *apparam;
  43. guint8 encoded[1024];
  44. gsize len;
  45. apparam = g_obex_apparam_decode(data, size);
  46. g_assert(apparam != NULL);
  47. len = g_obex_apparam_encode(apparam, encoded, sizeof(encoded));
  48. assert_memequal(data, size, encoded, len);
  49. return apparam;
  50. }
  51. static void test_apparam_nval_short(void)
  52. {
  53. GObexApparam *apparam;
  54. apparam = g_obex_apparam_decode(tag_nval_short,
  55. sizeof(tag_nval_short));
  56. g_assert(apparam == NULL);
  57. }
  58. static void test_apparam_nval_data(void)
  59. {
  60. GObexApparam *apparam;
  61. apparam = g_obex_apparam_decode(tag_nval_data,
  62. sizeof(tag_nval_data));
  63. g_assert(apparam == NULL);
  64. }
  65. static void test_apparam_nval2_short(void)
  66. {
  67. GObexApparam *apparam;
  68. apparam = g_obex_apparam_decode(tag_nval2_short,
  69. sizeof(tag_nval2_short));
  70. g_assert(apparam == NULL);
  71. }
  72. static void test_apparam_nval2_data(void)
  73. {
  74. GObexApparam *apparam;
  75. apparam = g_obex_apparam_decode(tag_nval2_data,
  76. sizeof(tag_nval2_data));
  77. g_assert(apparam == NULL);
  78. }
  79. static void test_apparam_get_uint8(void)
  80. {
  81. GObexApparam *apparam;
  82. guint8 data;
  83. gboolean ret;
  84. apparam = parse_and_decode(tag_uint8, sizeof(tag_uint8));
  85. ret = g_obex_apparam_get_uint8(apparam, TAG_U8, &data);
  86. g_assert(ret == TRUE);
  87. g_assert(data == 0x01);
  88. g_obex_apparam_free(apparam);
  89. }
  90. static void test_apparam_get_uint16(void)
  91. {
  92. GObexApparam *apparam;
  93. uint16_t data;
  94. gboolean ret;
  95. apparam = parse_and_decode(tag_uint16, sizeof(tag_uint16));
  96. ret = g_obex_apparam_get_uint16(apparam, TAG_U16, &data);
  97. g_assert(ret == TRUE);
  98. g_assert(data == 0x0102);
  99. g_obex_apparam_free(apparam);
  100. }
  101. static void test_apparam_get_uint32(void)
  102. {
  103. GObexApparam *apparam;
  104. uint32_t data;
  105. gboolean ret;
  106. apparam = parse_and_decode(tag_uint32, sizeof(tag_uint32));
  107. ret = g_obex_apparam_get_uint32(apparam, TAG_U32, &data);
  108. g_assert(ret == TRUE);
  109. g_assert(data == 0x01020304);
  110. g_obex_apparam_free(apparam);
  111. }
  112. static void test_apparam_get_uint64(void)
  113. {
  114. GObexApparam *apparam;
  115. uint64_t data;
  116. gboolean ret;
  117. apparam = parse_and_decode(tag_uint64, sizeof(tag_uint64));
  118. ret = g_obex_apparam_get_uint64(apparam, TAG_U64, &data);
  119. g_assert(ret == TRUE);
  120. g_assert(data == 0x0102030405060708);
  121. g_obex_apparam_free(apparam);
  122. }
  123. static void test_apparam_get_string(void)
  124. {
  125. GObexApparam *apparam;
  126. char *string;
  127. apparam = parse_and_decode(tag_string, sizeof(tag_string));
  128. string = g_obex_apparam_get_string(apparam, TAG_STRING);
  129. g_assert(string != NULL);
  130. g_assert_cmpstr(string, ==, "ABC");
  131. g_free(string);
  132. g_obex_apparam_free(apparam);
  133. }
  134. static void test_apparam_get_bytes(void)
  135. {
  136. GObexApparam *apparam;
  137. const uint8_t *data;
  138. gsize len;
  139. gboolean ret;
  140. apparam = parse_and_decode(tag_bytes, sizeof(tag_bytes));
  141. ret = g_obex_apparam_get_bytes(apparam, TAG_BYTES, &data, &len);
  142. g_assert(ret == TRUE);
  143. assert_memequal(tag_bytes + 2, sizeof(tag_bytes) - 2, data, len);
  144. g_obex_apparam_free(apparam);
  145. }
  146. static void test_apparam_get_multi(void)
  147. {
  148. GObexApparam *apparam;
  149. char *string;
  150. uint8_t data8;
  151. uint16_t data16;
  152. uint32_t data32;
  153. uint64_t data64;
  154. gboolean ret;
  155. apparam = g_obex_apparam_decode(tag_multi, sizeof(tag_multi));
  156. g_assert(apparam != NULL);
  157. ret = g_obex_apparam_get_uint8(apparam, TAG_U8, &data8);
  158. g_assert(ret == TRUE);
  159. g_assert(data8 == 0x01);
  160. ret = g_obex_apparam_get_uint16(apparam, TAG_U16, &data16);
  161. g_assert(ret == TRUE);
  162. g_assert(data16 == 0x0102);
  163. ret = g_obex_apparam_get_uint32(apparam, TAG_U32, &data32);
  164. g_assert(ret == TRUE);
  165. g_assert(data32 == 0x01020304);
  166. ret = g_obex_apparam_get_uint64(apparam, TAG_U64, &data64);
  167. g_assert(ret == TRUE);
  168. g_assert(data64 == 0x0102030405060708);
  169. string = g_obex_apparam_get_string(apparam, TAG_STRING);
  170. g_assert(string != NULL);
  171. g_assert_cmpstr(string, ==, "ABC");
  172. g_free(string);
  173. g_obex_apparam_free(apparam);
  174. }
  175. static void test_apparam_set_uint8(void)
  176. {
  177. GObexApparam *apparam;
  178. guint8 buf[1024];
  179. gsize len;
  180. apparam = g_obex_apparam_set_uint8(NULL, TAG_U8, 0x01);
  181. g_assert(apparam != NULL);
  182. len = g_obex_apparam_encode(apparam, buf, sizeof(buf));
  183. assert_memequal(tag_uint8, sizeof(tag_uint8), buf, len);
  184. g_obex_apparam_free(apparam);
  185. }
  186. static void test_apparam_set_uint16(void)
  187. {
  188. GObexApparam *apparam;
  189. guint8 buf[1024];
  190. gsize len;
  191. apparam = g_obex_apparam_set_uint16(NULL, TAG_U16, 0x0102);
  192. g_assert(apparam != NULL);
  193. len = g_obex_apparam_encode(apparam, buf, sizeof(buf));
  194. assert_memequal(tag_uint16, sizeof(tag_uint16), buf, len);
  195. g_obex_apparam_free(apparam);
  196. }
  197. static void test_apparam_set_uint32(void)
  198. {
  199. GObexApparam *apparam;
  200. guint8 buf[1024];
  201. gsize len;
  202. apparam = g_obex_apparam_set_uint32(NULL, TAG_U32, 0x01020304);
  203. g_assert(apparam != NULL);
  204. len = g_obex_apparam_encode(apparam, buf, sizeof(buf));
  205. assert_memequal(tag_uint32, sizeof(tag_uint32), buf, len);
  206. g_obex_apparam_free(apparam);
  207. }
  208. static void test_apparam_set_uint64(void)
  209. {
  210. GObexApparam *apparam;
  211. guint8 buf[1024];
  212. gsize len;
  213. apparam = g_obex_apparam_set_uint64(NULL, TAG_U64, 0x0102030405060708);
  214. g_assert(apparam != NULL);
  215. len = g_obex_apparam_encode(apparam, buf, sizeof(buf));
  216. assert_memequal(tag_uint64, sizeof(tag_uint64), buf, len);
  217. g_obex_apparam_free(apparam);
  218. }
  219. static void test_apparam_set_string(void)
  220. {
  221. GObexApparam *apparam;
  222. guint8 buf[1024];
  223. gsize len;
  224. apparam = g_obex_apparam_set_string(NULL, TAG_STRING, "ABC");
  225. g_assert(apparam != NULL);
  226. len = g_obex_apparam_encode(apparam, buf, sizeof(buf));
  227. assert_memequal(tag_string, sizeof(tag_string), buf, len);
  228. g_obex_apparam_free(apparam);
  229. }
  230. static void test_apparam_set_bytes(void)
  231. {
  232. GObexApparam *apparam;
  233. guint8 buf[1024];
  234. gsize len;
  235. apparam = g_obex_apparam_set_bytes(NULL, TAG_BYTES, tag_bytes + 2, 255);
  236. g_assert(apparam != NULL);
  237. len = g_obex_apparam_encode(apparam, buf, sizeof(buf));
  238. assert_memequal(tag_bytes, sizeof(tag_bytes), buf, len);
  239. g_obex_apparam_free(apparam);
  240. }
  241. static void test_apparam_set_multi(void)
  242. {
  243. GObexApparam *apparam;
  244. guint8 buf[1024];
  245. gsize len;
  246. apparam = g_obex_apparam_set_uint8(NULL, TAG_U8, 0x01);
  247. g_assert(apparam != NULL);
  248. apparam = g_obex_apparam_set_uint16(apparam, TAG_U16, 0x0102);
  249. g_assert(apparam != NULL);
  250. apparam = g_obex_apparam_set_uint32(apparam, TAG_U32, 0x01020304);
  251. g_assert(apparam != NULL);
  252. apparam = g_obex_apparam_set_uint64(apparam, TAG_U64,
  253. 0x0102030405060708);
  254. g_assert(apparam != NULL);
  255. apparam = g_obex_apparam_set_string(apparam, TAG_STRING, "ABC");
  256. g_assert(apparam != NULL);
  257. len = g_obex_apparam_encode(apparam, buf, sizeof(buf));
  258. g_assert_cmpuint(len, ==, sizeof(tag_multi));
  259. g_obex_apparam_free(apparam);
  260. }
  261. int main(int argc, char *argv[])
  262. {
  263. g_test_init(&argc, &argv, NULL);
  264. g_test_add_func("/gobex/test_apparam_nval_short",
  265. test_apparam_nval_short);
  266. g_test_add_func("/gobex/test_apparam_nval_data",
  267. test_apparam_nval_data);
  268. g_test_add_func("/gobex/test_apparam_nval2_short",
  269. test_apparam_nval2_short);
  270. g_test_add_func("/gobex/test_apparam_nval2_data",
  271. test_apparam_nval2_data);
  272. g_test_add_func("/gobex/test_apparam_get_uint8",
  273. test_apparam_get_uint8);
  274. g_test_add_func("/gobex/test_apparam_get_uint16",
  275. test_apparam_get_uint16);
  276. g_test_add_func("/gobex/test_apparam_get_uint32",
  277. test_apparam_get_uint32);
  278. g_test_add_func("/gobex/test_apparam_get_uint64",
  279. test_apparam_get_uint64);
  280. g_test_add_func("/gobex/test_apparam_get_string",
  281. test_apparam_get_string);
  282. g_test_add_func("/gobex/test_apparam_get_bytes",
  283. test_apparam_get_bytes);
  284. g_test_add_func("/gobex/test_apparam_get_multi",
  285. test_apparam_get_multi);
  286. g_test_add_func("/gobex/test_apparam_set_uint8",
  287. test_apparam_set_uint8);
  288. g_test_add_func("/gobex/test_apparam_set_uint16",
  289. test_apparam_set_uint16);
  290. g_test_add_func("/gobex/test_apparam_set_uint32",
  291. test_apparam_set_uint32);
  292. g_test_add_func("/gobex/test_apparam_set_uint64",
  293. test_apparam_set_uint64);
  294. g_test_add_func("/gobex/test_apparam_set_string",
  295. test_apparam_set_string);
  296. g_test_add_func("/gobex/test_apparam_set_bytes",
  297. test_apparam_set_bytes);
  298. g_test_add_func("/gobex/test_apparam_set_multi",
  299. test_apparam_set_multi);
  300. return g_test_run();
  301. }