3 * OBEX library with GLib integration
5 * Copyright (C) 2011 Intel Corporation. All rights reserved.
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
22 #include <sys/types.h>
23 #include <sys/socket.h>
34 GQuark test_error_quark(void)
36 return g_quark_from_static_string("test-error-quark");
39 static void dump_bytes(const uint8_t *buf, size_t buf_len)
43 for (i = 0; i < buf_len; i++)
44 g_printerr("%02x ", buf[i]);
49 void dump_bufs(const void *mem1, size_t len1, const void *mem2, size_t len2)
51 g_printerr("\nExpected: ");
52 dump_bytes(mem1, len1);
54 dump_bytes(mem2, len2);
57 void assert_memequal(const void *mem1, size_t len1,
58 const void *mem2, size_t len2)
60 if (len1 == len2 && memcmp(mem1, mem2, len1) == 0)
63 dump_bufs(mem1, len1, mem2, len2);
68 GObex *create_gobex(int fd, GObexTransportType transport_type,
69 gboolean close_on_unref)
74 io = g_io_channel_unix_new(fd);
77 g_io_channel_set_close_on_unref(io, close_on_unref);
79 obex = g_obex_new(io, transport_type, -1, -1);
80 g_io_channel_unref(io);
85 void create_endpoints(GObex **obex, GIOChannel **io, int sock_type)
87 GObexTransportType transport_type;
90 if (socketpair(AF_UNIX, sock_type | SOCK_NONBLOCK, 0, sv) < 0) {
91 g_printerr("socketpair: %s", strerror(errno));
95 if (sock_type == SOCK_STREAM)
96 transport_type = G_OBEX_TRANSPORT_STREAM;
98 transport_type = G_OBEX_TRANSPORT_PACKET;
100 *obex = create_gobex(sv[0], transport_type, TRUE);
101 g_assert(*obex != NULL);
108 *io = g_io_channel_unix_new(sv[1]);
109 g_assert(*io != NULL);
111 g_io_channel_set_encoding(*io, NULL, NULL);
112 g_io_channel_set_buffered(*io, FALSE);
113 g_io_channel_set_close_on_unref(*io, TRUE);
116 gboolean test_timeout(gpointer user_data)
118 struct test_data *d = user_data;
120 if (!g_main_loop_is_running(d->mainloop))
123 d->err = g_error_new(TEST_ERROR, TEST_ERROR_TIMEOUT, "Timed out");
125 g_main_loop_quit(d->mainloop);
130 gboolean test_io_cb(GIOChannel *io, GIOCondition cond, gpointer user_data)
132 struct test_data *d = user_data;
134 gsize bytes_written, rbytes, send_buf_len, expect_len;
136 const char *send_buf, *expect;
138 expect = d->recv[d->count].data;
139 expect_len = d->recv[d->count].len;
140 send_buf = d->send[d->count].data;
141 send_buf_len = d->send[d->count].len;
145 if (!(cond & G_IO_IN))
148 status = g_io_channel_read_chars(io, buf, sizeof(buf), &rbytes, NULL);
149 if (status != G_IO_STATUS_NORMAL) {
150 g_print("io_cb count %u\n", d->count);
151 g_set_error(&d->err, TEST_ERROR, TEST_ERROR_UNEXPECTED,
152 "Reading data failed with status %d", status);
156 if (rbytes < expect_len) {
157 g_print("io_cb count %u\n", d->count);
158 dump_bufs(expect, expect_len, buf, rbytes);
159 g_set_error(&d->err, TEST_ERROR, TEST_ERROR_UNEXPECTED,
160 "Not enough data from socket");
164 if (memcmp(buf, expect, expect_len) != 0) {
165 g_print("io_cb count %u\n", d->count);
166 dump_bufs(expect, expect_len, buf, rbytes);
167 g_set_error(&d->err, TEST_ERROR, TEST_ERROR_UNEXPECTED,
168 "Received data is not correct");
173 if ((gssize) send_buf_len < 0)
176 g_io_channel_write_chars(io, send_buf, send_buf_len, &bytes_written,
178 if (bytes_written != send_buf_len) {
179 g_print("io_cb count %u\n", d->count);
180 g_set_error(&d->err, TEST_ERROR, TEST_ERROR_UNEXPECTED,
181 "Unable to write to socket");
185 if (d->recv[d->count].len < 0 || (gssize) expect_len < 0)
186 return test_io_cb(io, G_IO_OUT, user_data);
191 g_main_loop_quit(d->mainloop);