tizen beta release
[profile/ivi/obexd.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 #include <sys/types.h>
23 #include <sys/socket.h>
24 #include <unistd.h>
25 #include <stdlib.h>
26 #include <stdint.h>
27 #include <string.h>
28 #include <errno.h>
29
30 #include <glib.h>
31
32 #include "util.h"
33
34 GQuark test_error_quark(void)
35 {
36         return g_quark_from_static_string("test-error-quark");
37 }
38
39 static void dump_bytes(const uint8_t *buf, size_t buf_len)
40 {
41         size_t i;
42
43         for (i = 0; i < buf_len; i++)
44                 g_printerr("%02x ", buf[i]);
45
46         g_printerr("\n");
47 }
48
49 void dump_bufs(const void *mem1, size_t len1, const void *mem2, size_t len2)
50 {
51         g_printerr("\nExpected: ");
52         dump_bytes(mem1, len1);
53         g_printerr("Got:      ");
54         dump_bytes(mem2, len2);
55 }
56
57 void assert_memequal(const void *mem1, size_t len1,
58                                                 const void *mem2, size_t len2)
59 {
60         if (len1 == len2 && memcmp(mem1, mem2, len1) == 0)
61                 return;
62
63         dump_bufs(mem1, len1, mem2, len2);
64
65         g_assert(0);
66 }
67
68 GObex *create_gobex(int fd, GObexTransportType transport_type,
69                                                 gboolean close_on_unref)
70 {
71         GIOChannel *io;
72         GObex *obex;
73
74         io = g_io_channel_unix_new(fd);
75         g_assert(io != NULL);
76
77         g_io_channel_set_close_on_unref(io, close_on_unref);
78
79         obex = g_obex_new(io, transport_type, -1, -1);
80         g_io_channel_unref(io);
81
82         return obex;
83 }
84
85 void create_endpoints(GObex **obex, GIOChannel **io, int sock_type)
86 {
87         GObexTransportType transport_type;
88         int sv[2];
89
90         if (socketpair(AF_UNIX, sock_type | SOCK_NONBLOCK, 0, sv) < 0) {
91                 g_printerr("socketpair: %s", strerror(errno));
92                 abort();
93         }
94
95         if (sock_type == SOCK_STREAM)
96                 transport_type = G_OBEX_TRANSPORT_STREAM;
97         else
98                 transport_type = G_OBEX_TRANSPORT_PACKET;
99
100         *obex = create_gobex(sv[0], transport_type, TRUE);
101         g_assert(*obex != NULL);
102
103         if (io == NULL) {
104                 close(sv[1]);
105                 return;
106         }
107
108         *io = g_io_channel_unix_new(sv[1]);
109         g_assert(*io != NULL);
110
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);
114 }
115
116 gboolean test_timeout(gpointer user_data)
117 {
118         struct test_data *d = user_data;
119
120         if (!g_main_loop_is_running(d->mainloop))
121                 return FALSE;
122
123         d->err = g_error_new(TEST_ERROR, TEST_ERROR_TIMEOUT, "Timed out");
124
125         g_main_loop_quit(d->mainloop);
126
127         return FALSE;
128 }
129
130 gboolean test_io_cb(GIOChannel *io, GIOCondition cond, gpointer user_data)
131 {
132         struct test_data *d = user_data;
133         GIOStatus status;
134         gsize bytes_written, rbytes, send_buf_len, expect_len;
135         char buf[65535];
136         const char *send_buf, *expect;
137
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;
142
143         d->count++;
144
145         if (!(cond & G_IO_IN))
146                 goto send;
147
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);
153                 goto failed;
154         }
155
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");
161                 goto failed;
162         }
163
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");
169                 goto failed;
170         }
171
172 send:
173         if ((gssize) send_buf_len < 0)
174                 goto failed;
175
176         g_io_channel_write_chars(io, send_buf, send_buf_len, &bytes_written,
177                                                                         NULL);
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");
182                 goto failed;
183         }
184
185         if (d->recv[d->count].len < 0 || (gssize) expect_len < 0)
186                 return test_io_cb(io, G_IO_OUT, user_data);
187
188         return TRUE;
189
190 failed:
191         g_main_loop_quit(d->mainloop);
192         return FALSE;
193 }