util.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. // SPDX-License-Identifier: GPL-2.0-only
  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 <sys/types.h>
  13. #include <sys/socket.h>
  14. #include <unistd.h>
  15. #include <stdlib.h>
  16. #include <stdint.h>
  17. #include <string.h>
  18. #include <errno.h>
  19. #include <glib.h>
  20. #include "gobex/gobex.h"
  21. #include "util.h"
  22. GQuark test_error_quark(void)
  23. {
  24. return g_quark_from_static_string("test-error-quark");
  25. }
  26. static void dump_bytes(const uint8_t *buf, size_t buf_len)
  27. {
  28. size_t i;
  29. for (i = 0; i < buf_len; i++)
  30. g_printerr("%02x ", buf[i]);
  31. g_printerr("\n");
  32. }
  33. void dump_bufs(const void *mem1, size_t len1, const void *mem2, size_t len2)
  34. {
  35. g_printerr("\nExpected: ");
  36. dump_bytes(mem1, len1);
  37. g_printerr("Got: ");
  38. dump_bytes(mem2, len2);
  39. }
  40. void assert_memequal(const void *mem1, size_t len1,
  41. const void *mem2, size_t len2)
  42. {
  43. if (len1 == len2 && memcmp(mem1, mem2, len1) == 0)
  44. return;
  45. dump_bufs(mem1, len1, mem2, len2);
  46. g_assert(0);
  47. }
  48. GObex *create_gobex(int fd, GObexTransportType transport_type,
  49. gboolean close_on_unref)
  50. {
  51. GIOChannel *io;
  52. GObex *obex;
  53. io = g_io_channel_unix_new(fd);
  54. g_assert(io != NULL);
  55. g_io_channel_set_close_on_unref(io, close_on_unref);
  56. obex = g_obex_new(io, transport_type, -1, -1);
  57. g_io_channel_unref(io);
  58. return obex;
  59. }
  60. void create_endpoints(GObex **obex, GIOChannel **io, int sock_type)
  61. {
  62. GObexTransportType transport_type;
  63. int sv[2];
  64. if (socketpair(AF_UNIX, sock_type | SOCK_NONBLOCK, 0, sv) < 0) {
  65. g_printerr("socketpair: %s", strerror(errno));
  66. abort();
  67. }
  68. if (sock_type == SOCK_STREAM)
  69. transport_type = G_OBEX_TRANSPORT_STREAM;
  70. else
  71. transport_type = G_OBEX_TRANSPORT_PACKET;
  72. *obex = create_gobex(sv[0], transport_type, TRUE);
  73. g_assert(*obex != NULL);
  74. if (io == NULL) {
  75. close(sv[1]);
  76. return;
  77. }
  78. *io = g_io_channel_unix_new(sv[1]);
  79. g_assert(*io != NULL);
  80. g_io_channel_set_encoding(*io, NULL, NULL);
  81. g_io_channel_set_buffered(*io, FALSE);
  82. g_io_channel_set_close_on_unref(*io, TRUE);
  83. }
  84. gboolean test_timeout(gpointer user_data)
  85. {
  86. struct test_data *d = user_data;
  87. if (!g_main_loop_is_running(d->mainloop))
  88. return FALSE;
  89. d->err = g_error_new(TEST_ERROR, TEST_ERROR_TIMEOUT, "Timed out");
  90. d->timer_id = 0;
  91. g_main_loop_quit(d->mainloop);
  92. return FALSE;
  93. }
  94. gboolean test_io_cb(GIOChannel *io, GIOCondition cond, gpointer user_data)
  95. {
  96. struct test_data *d = user_data;
  97. GIOStatus status;
  98. gsize bytes_written, rbytes, send_buf_len, expect_len;
  99. char buf[65535];
  100. const char *send_buf, *expect;
  101. expect = d->recv[d->count].data;
  102. expect_len = d->recv[d->count].len;
  103. send_buf = d->send[d->count].data;
  104. send_buf_len = d->send[d->count].len;
  105. d->count++;
  106. if (!(cond & G_IO_IN))
  107. goto send;
  108. status = g_io_channel_read_chars(io, buf, sizeof(buf), &rbytes, NULL);
  109. if (status != G_IO_STATUS_NORMAL) {
  110. g_print("io_cb count %u\n", d->count);
  111. g_set_error(&d->err, TEST_ERROR, TEST_ERROR_UNEXPECTED,
  112. "Reading data failed with status %d", status);
  113. goto failed;
  114. }
  115. if (rbytes < expect_len) {
  116. g_print("io_cb count %u\n", d->count);
  117. dump_bufs(expect, expect_len, buf, rbytes);
  118. g_set_error(&d->err, TEST_ERROR, TEST_ERROR_UNEXPECTED,
  119. "Not enough data from socket");
  120. goto failed;
  121. }
  122. if (memcmp(buf, expect, expect_len) != 0) {
  123. g_print("io_cb count %u\n", d->count);
  124. dump_bufs(expect, expect_len, buf, rbytes);
  125. g_set_error(&d->err, TEST_ERROR, TEST_ERROR_UNEXPECTED,
  126. "Received data is not correct");
  127. goto failed;
  128. }
  129. send:
  130. if ((gssize) send_buf_len < 0)
  131. goto failed;
  132. g_io_channel_write_chars(io, send_buf, send_buf_len, &bytes_written,
  133. NULL);
  134. if (bytes_written != send_buf_len) {
  135. g_print("io_cb count %u\n", d->count);
  136. g_set_error(&d->err, TEST_ERROR, TEST_ERROR_UNEXPECTED,
  137. "Unable to write to socket");
  138. goto failed;
  139. }
  140. if (d->count >= TEST_BUF_MAX) {
  141. g_print("io_cb count %u\n", d->count);
  142. goto failed;
  143. }
  144. if (d->recv[d->count].len < 0 || (gssize) expect_len < 0)
  145. return test_io_cb(io, G_IO_OUT, user_data);
  146. return TRUE;
  147. failed:
  148. d->io_id = 0;
  149. g_main_loop_quit(d->mainloop);
  150. return FALSE;
  151. }