tizen 2.3.1 release
[framework/connectivity/bluez.git] / unit / util.c
1 /*
2  *
3  *  OBEX library with GLib integration
4  *
5  *  Copyright (C) 2011  Intel Corporation. All rights reserved.
6  *
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.
10  *
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.
15  *
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
19  *
20  */
21
22 #ifdef HAVE_CONFIG_H
23 #include "config.h"
24 #endif
25
26 #include <sys/types.h>
27 #include <sys/socket.h>
28 #include <unistd.h>
29 #include <stdlib.h>
30 #include <stdint.h>
31 #include <string.h>
32 #include <errno.h>
33
34 #include <glib.h>
35
36 #include "gobex/gobex.h"
37
38 #include "util.h"
39
40 GQuark test_error_quark(void)
41 {
42         return g_quark_from_static_string("test-error-quark");
43 }
44
45 static void dump_bytes(const uint8_t *buf, size_t buf_len)
46 {
47         size_t i;
48
49         for (i = 0; i < buf_len; i++)
50                 g_printerr("%02x ", buf[i]);
51
52         g_printerr("\n");
53 }
54
55 void dump_bufs(const void *mem1, size_t len1, const void *mem2, size_t len2)
56 {
57         g_printerr("\nExpected: ");
58         dump_bytes(mem1, len1);
59         g_printerr("Got:      ");
60         dump_bytes(mem2, len2);
61 }
62
63 void assert_memequal(const void *mem1, size_t len1,
64                                                 const void *mem2, size_t len2)
65 {
66         if (len1 == len2 && memcmp(mem1, mem2, len1) == 0)
67                 return;
68
69         dump_bufs(mem1, len1, mem2, len2);
70
71         g_assert(0);
72 }
73
74 GObex *create_gobex(int fd, GObexTransportType transport_type,
75                                                 gboolean close_on_unref)
76 {
77         GIOChannel *io;
78         GObex *obex;
79
80         io = g_io_channel_unix_new(fd);
81         g_assert(io != NULL);
82
83         g_io_channel_set_close_on_unref(io, close_on_unref);
84
85         obex = g_obex_new(io, transport_type, -1, -1);
86         g_io_channel_unref(io);
87
88         return obex;
89 }
90
91 void create_endpoints(GObex **obex, GIOChannel **io, int sock_type)
92 {
93         GObexTransportType transport_type;
94         int sv[2];
95
96         if (socketpair(AF_UNIX, sock_type | SOCK_NONBLOCK, 0, sv) < 0) {
97                 g_printerr("socketpair: %s", strerror(errno));
98                 abort();
99         }
100
101         if (sock_type == SOCK_STREAM)
102                 transport_type = G_OBEX_TRANSPORT_STREAM;
103         else
104                 transport_type = G_OBEX_TRANSPORT_PACKET;
105
106         *obex = create_gobex(sv[0], transport_type, TRUE);
107         g_assert(*obex != NULL);
108
109         if (io == NULL) {
110                 close(sv[1]);
111                 return;
112         }
113
114         *io = g_io_channel_unix_new(sv[1]);
115         g_assert(*io != NULL);
116
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);
120 }
121
122 gboolean test_timeout(gpointer user_data)
123 {
124         struct test_data *d = user_data;
125
126         if (!g_main_loop_is_running(d->mainloop))
127                 return FALSE;
128
129         d->err = g_error_new(TEST_ERROR, TEST_ERROR_TIMEOUT, "Timed out");
130
131         g_main_loop_quit(d->mainloop);
132
133         return FALSE;
134 }
135
136 gboolean test_io_cb(GIOChannel *io, GIOCondition cond, gpointer user_data)
137 {
138         struct test_data *d = user_data;
139         GIOStatus status;
140         gsize bytes_written, rbytes, send_buf_len, expect_len;
141         char buf[65535];
142         const char *send_buf, *expect;
143
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;
148
149         d->count++;
150
151         if (!(cond & G_IO_IN))
152                 goto send;
153
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);
159                 goto failed;
160         }
161
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");
167                 goto failed;
168         }
169
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");
175                 goto failed;
176         }
177
178 send:
179         if ((gssize) send_buf_len < 0)
180                 goto failed;
181
182         g_io_channel_write_chars(io, send_buf, send_buf_len, &bytes_written,
183                                                                         NULL);
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");
188                 goto failed;
189         }
190
191         if (d->recv[d->count].len < 0 || (gssize) expect_len < 0)
192                 return test_io_cb(io, G_IO_OUT, user_data);
193
194         return TRUE;
195
196 failed:
197         g_main_loop_quit(d->mainloop);
198         d->io_completed = TRUE;
199         return FALSE;
200 }