test-gobex-packet.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. *
  4. * OBEX library with GLib integration
  5. *
  6. * Copyright (C) 2011 Intel Corporation. All rights reserved.
  7. *
  8. */
  9. #ifdef HAVE_CONFIG_H
  10. #include <config.h>
  11. #endif
  12. #include <stdint.h>
  13. #include <string.h>
  14. #include <errno.h>
  15. #include "gobex/gobex.h"
  16. #include "gobex/gobex-packet.h"
  17. #include "util.h"
  18. static uint8_t pkt_connect[] = { G_OBEX_OP_CONNECT, 0x00, 0x0c,
  19. 0x10, 0x00, 0x10, 0x00,
  20. G_OBEX_HDR_TARGET,
  21. 0x00, 0x05, 0xab, 0xcd };
  22. static uint8_t pkt_put_action[] = { G_OBEX_OP_PUT, 0x00, 0x05,
  23. G_OBEX_HDR_ACTION, 0xab };
  24. static uint8_t pkt_put_body[] = { G_OBEX_OP_PUT, 0x00, 0x0a,
  25. G_OBEX_HDR_BODY, 0x00, 0x07,
  26. 1, 2, 3, 4 };
  27. static uint8_t pkt_put[] = { G_OBEX_OP_PUT, 0x00, 0x03 };
  28. static uint8_t pkt_nval_len[] = { G_OBEX_OP_PUT, 0xab, 0xcd, 0x12 };
  29. static guint8 pkt_put_long[] = { G_OBEX_OP_PUT, 0x00, 0x32,
  30. G_OBEX_HDR_CONNECTION, 0x01, 0x02, 0x03, 0x04,
  31. G_OBEX_HDR_TYPE, 0x00, 0x0b,
  32. 'f', 'o', 'o', '/', 'b', 'a', 'r', '\0',
  33. G_OBEX_HDR_NAME, 0x00, 0x15,
  34. 0, 'f', 0, 'i', 0, 'l', 0, 'e', 0, '.', 0, 't', 0, 'x', 0, 't', 0, 0,
  35. G_OBEX_HDR_ACTION, 0xab,
  36. G_OBEX_HDR_BODY, 0x00, 0x08,
  37. 0, 1, 2, 3, 4 };
  38. static void test_pkt(void)
  39. {
  40. GObexPacket *pkt;
  41. pkt = g_obex_packet_new(G_OBEX_OP_PUT, TRUE, G_OBEX_HDR_INVALID);
  42. g_assert(pkt != NULL);
  43. g_obex_packet_free(pkt);
  44. }
  45. static void test_decode_pkt(void)
  46. {
  47. GObexPacket *pkt;
  48. GError *err = NULL;
  49. pkt = g_obex_packet_decode(pkt_put, sizeof(pkt_put), 0,
  50. G_OBEX_DATA_REF, &err);
  51. g_assert_no_error(err);
  52. g_obex_packet_free(pkt);
  53. }
  54. static void test_decode_pkt_header(void)
  55. {
  56. GObexPacket *pkt;
  57. GObexHeader *header;
  58. GError *err = NULL;
  59. gboolean ret;
  60. guint8 val;
  61. pkt = g_obex_packet_decode(pkt_put_action, sizeof(pkt_put_action),
  62. 0, G_OBEX_DATA_REF, &err);
  63. g_assert_no_error(err);
  64. header = g_obex_packet_get_header(pkt, G_OBEX_HDR_ACTION);
  65. g_assert(header != NULL);
  66. ret = g_obex_header_get_uint8(header, &val);
  67. g_assert(ret == TRUE);
  68. g_assert(val == 0xab);
  69. g_obex_packet_free(pkt);
  70. }
  71. static void test_decode_connect(void)
  72. {
  73. GObexPacket *pkt;
  74. GObexHeader *header;
  75. GError *err = NULL;
  76. gboolean ret;
  77. const guint8 *buf;
  78. guint8 target[] = { 0xab, 0xcd };
  79. gsize len;
  80. pkt = g_obex_packet_decode(pkt_connect, sizeof(pkt_connect),
  81. 4, G_OBEX_DATA_REF, &err);
  82. g_assert_no_error(err);
  83. g_assert(pkt != NULL);
  84. header = g_obex_packet_get_header(pkt, G_OBEX_HDR_TARGET);
  85. g_assert(header != NULL);
  86. ret = g_obex_header_get_bytes(header, &buf, &len);
  87. g_assert(ret == TRUE);
  88. assert_memequal(target, sizeof(target), buf, len);
  89. g_obex_packet_free(pkt);
  90. }
  91. static void test_decode_nval(void)
  92. {
  93. GObexPacket *pkt;
  94. GError *err = NULL;
  95. pkt = g_obex_packet_decode(pkt_nval_len, sizeof(pkt_nval_len), 0,
  96. G_OBEX_DATA_REF, &err);
  97. g_assert_error(err, G_OBEX_ERROR, G_OBEX_ERROR_PARSE_ERROR);
  98. g_assert(pkt == NULL);
  99. g_error_free(err);
  100. }
  101. static void test_decode_encode(void)
  102. {
  103. GObexPacket *pkt;
  104. GError *err = NULL;
  105. uint8_t buf[255];
  106. gssize len;
  107. pkt = g_obex_packet_decode(pkt_put_action, sizeof(pkt_put_action),
  108. 0, G_OBEX_DATA_REF, &err);
  109. g_assert_no_error(err);
  110. len = g_obex_packet_encode(pkt, buf, sizeof(buf));
  111. if (len < 0) {
  112. g_printerr("Encoding failed: %s\n", g_strerror(-len));
  113. g_assert_not_reached();
  114. }
  115. assert_memequal(pkt_put_action, sizeof(pkt_put_action), buf, len);
  116. g_obex_packet_free(pkt);
  117. }
  118. static gssize get_body_data(void *buf, gsize len, gpointer user_data)
  119. {
  120. uint8_t data[] = { 1, 2, 3, 4 };
  121. memcpy(buf, data, sizeof(data));
  122. return sizeof(data);
  123. }
  124. static void test_encode_on_demand(void)
  125. {
  126. GObexPacket *pkt;
  127. uint8_t buf[255];
  128. gssize len;
  129. pkt = g_obex_packet_new(G_OBEX_OP_PUT, FALSE, G_OBEX_HDR_INVALID);
  130. g_obex_packet_add_body(pkt, get_body_data, NULL);
  131. len = g_obex_packet_encode(pkt, buf, sizeof(buf));
  132. if (len < 0) {
  133. g_printerr("Encoding failed: %s\n", g_strerror(-len));
  134. g_assert_not_reached();
  135. }
  136. assert_memequal(pkt_put_body, sizeof(pkt_put_body), buf, len);
  137. g_obex_packet_free(pkt);
  138. }
  139. static gssize get_body_data_fail(void *buf, gsize len, gpointer user_data)
  140. {
  141. return -EIO;
  142. }
  143. static void test_encode_on_demand_fail(void)
  144. {
  145. GObexPacket *pkt;
  146. uint8_t buf[255];
  147. gssize len;
  148. pkt = g_obex_packet_new(G_OBEX_OP_PUT, FALSE, G_OBEX_HDR_INVALID);
  149. g_obex_packet_add_body(pkt, get_body_data_fail, NULL);
  150. len = g_obex_packet_encode(pkt, buf, sizeof(buf));
  151. g_assert_cmpint(len, ==, -EIO);
  152. g_obex_packet_free(pkt);
  153. }
  154. static void test_create_args(void)
  155. {
  156. GObexPacket *pkt;
  157. guint8 buf[255], body[] = { 0x00, 0x01, 0x02, 0x03, 0x04 };
  158. gssize len;
  159. pkt = g_obex_packet_new(G_OBEX_OP_PUT, FALSE,
  160. G_OBEX_HDR_CONNECTION, 0x01020304,
  161. G_OBEX_HDR_TYPE, "foo/bar", strlen("foo/bar") + 1,
  162. G_OBEX_HDR_NAME, "file.txt",
  163. G_OBEX_HDR_ACTION, 0xab,
  164. G_OBEX_HDR_BODY, body, sizeof(body),
  165. G_OBEX_HDR_INVALID);
  166. g_assert(pkt != NULL);
  167. len = g_obex_packet_encode(pkt, buf, sizeof(buf));
  168. g_assert(len > 0);
  169. assert_memequal(pkt_put_long, sizeof(pkt_put_long), buf, len);
  170. g_obex_packet_free(pkt);
  171. }
  172. int main(int argc, char *argv[])
  173. {
  174. g_test_init(&argc, &argv, NULL);
  175. g_test_add_func("/gobex/test_pkt", test_pkt);
  176. g_test_add_func("/gobex/test_decode_pkt", test_decode_pkt);
  177. g_test_add_func("/gobex/test_decode_pkt_header",
  178. test_decode_pkt_header);
  179. g_test_add_func("/gobex/test_decode_connect",
  180. test_decode_connect);
  181. g_test_add_func("/gobex/test_decode_nval", test_decode_nval);
  182. g_test_add_func("/gobex/test_encode_pkt", test_decode_encode);
  183. g_test_add_func("/gobex/test_encode_on_demand", test_encode_on_demand);
  184. g_test_add_func("/gobex/test_encode_on_demand_fail",
  185. test_encode_on_demand_fail);
  186. g_test_add_func("/gobex/test_create_args", test_create_args);
  187. return g_test_run();
  188. }