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
26 #include <sys/types.h>
27 #include <sys/socket.h>
36 #include "gobex/gobex.h"
40 GQuark test_error_quark(void)
42 return g_quark_from_static_string("test-error-quark");
45 static void dump_bytes(const uint8_t *buf, size_t buf_len)
49 for (i = 0; i < buf_len; i++)
50 g_printerr("%02x ", buf[i]);
55 void dump_bufs(const void *mem1, size_t len1, const void *mem2, size_t len2)
57 g_printerr("\nExpected: ");
58 dump_bytes(mem1, len1);
60 dump_bytes(mem2, len2);
63 void assert_memequal(const void *mem1, size_t len1,
64 const void *mem2, size_t len2)
66 if (len1 == len2 && memcmp(mem1, mem2, len1) == 0)
69 dump_bufs(mem1, len1, mem2, len2);
74 GObex *create_gobex(int fd, GObexTransportType transport_type,
75 gboolean close_on_unref)
80 io = g_io_channel_unix_new(fd);
83 g_io_channel_set_close_on_unref(io, close_on_unref);
85 obex = g_obex_new(io, transport_type, -1, -1);
86 g_io_channel_unref(io);
91 void create_endpoints(GObex **obex, GIOChannel **io, int sock_type)
93 GObexTransportType transport_type;
96 if (socketpair(AF_UNIX, sock_type | SOCK_NONBLOCK, 0, sv) < 0) {
97 g_printerr("socketpair: %s", strerror(errno));
101 if (sock_type == SOCK_STREAM)
102 transport_type = G_OBEX_TRANSPORT_STREAM;
104 transport_type = G_OBEX_TRANSPORT_PACKET;
106 *obex = create_gobex(sv[0], transport_type, TRUE);
107 g_assert(*obex != NULL);
114 *io = g_io_channel_unix_new(sv[1]);
115 g_assert(*io != NULL);
117 g_io_channel_set_encoding(*io, NULL, NULL);
118 g_io_channel_set_buffered(*io, FALSE);
119 g_io_channel_set_close_on_unref(*io, TRUE);
122 gboolean test_timeout(gpointer user_data)
124 struct test_data *d = user_data;
126 if (!g_main_loop_is_running(d->mainloop))
129 d->err = g_error_new(TEST_ERROR, TEST_ERROR_TIMEOUT, "Timed out");
131 g_main_loop_quit(d->mainloop);
136 gboolean test_io_cb(GIOChannel *io, GIOCondition cond, gpointer user_data)
138 struct test_data *d = user_data;
140 gsize bytes_written, rbytes, send_buf_len, expect_len;
142 const char *send_buf, *expect;
144 expect = d->recv[d->count].data;
145 expect_len = d->recv[d->count].len;
146 send_buf = d->send[d->count].data;
147 send_buf_len = d->send[d->count].len;
151 if (!(cond & G_IO_IN))
154 status = g_io_channel_read_chars(io, buf, sizeof(buf), &rbytes, NULL);
155 if (status != G_IO_STATUS_NORMAL) {
156 g_print("io_cb count %u\n", d->count);
157 g_set_error(&d->err, TEST_ERROR, TEST_ERROR_UNEXPECTED,
158 "Reading data failed with status %d", status);
162 if (rbytes < expect_len) {
163 g_print("io_cb count %u\n", d->count);
164 dump_bufs(expect, expect_len, buf, rbytes);
165 g_set_error(&d->err, TEST_ERROR, TEST_ERROR_UNEXPECTED,
166 "Not enough data from socket");
170 if (memcmp(buf, expect, expect_len) != 0) {
171 g_print("io_cb count %u\n", d->count);
172 dump_bufs(expect, expect_len, buf, rbytes);
173 g_set_error(&d->err, TEST_ERROR, TEST_ERROR_UNEXPECTED,
174 "Received data is not correct");
179 if ((gssize) send_buf_len < 0)
182 g_io_channel_write_chars(io, send_buf, send_buf_len, &bytes_written,
184 if (bytes_written != send_buf_len) {
185 g_print("io_cb count %u\n", d->count);
186 g_set_error(&d->err, TEST_ERROR, TEST_ERROR_UNEXPECTED,
187 "Unable to write to socket");
191 if (d->recv[d->count].len < 0 || (gssize) expect_len < 0)
192 return test_io_cb(io, G_IO_OUT, user_data);
197 g_main_loop_quit(d->mainloop);
198 d->io_completed = TRUE;