Enable check-TESTS using %check section in bluez
[platform/upstream/bluez.git] / unit / test-hog.c
1 /*
2  *
3  *  BlueZ - Bluetooth protocol stack for Linux
4  *
5  *  Copyright (C) 2015  Intel Corporation. All rights reserved.
6  *
7  *
8  *  This program is free software; you can redistribute it and/or modify
9  *  it under the terms of the GNU General Public License as published by
10  *  the Free Software Foundation; either version 2 of the License, or
11  *  (at your option) any later version.
12  *
13  *  This program is distributed in the hope that it will be useful,
14  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  *  GNU General Public License for more details.
17  *
18  *  You should have received a copy of the GNU General Public License
19  *  along with this program; if not, write to the Free Software
20  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
21  *
22  */
23
24 #ifdef HAVE_CONFIG_H
25 #include <config.h>
26 #endif
27
28 #include <unistd.h>
29 #include <string.h>
30 #include <sys/socket.h>
31 #include <fcntl.h>
32
33 #include <glib.h>
34
35 #include "lib/bluetooth.h"
36 #include "lib/uuid.h"
37
38 #include "src/shared/util.h"
39 #include "src/shared/tester.h"
40 #include "src/shared/queue.h"
41 #include "src/shared/att.h"
42 #include "src/shared/gatt-db.h"
43
44 #include "attrib/gattrib.h"
45
46 #include "profiles/input/hog-lib.h"
47
48 struct test_pdu {
49         bool valid;
50         const uint8_t *data;
51         size_t size;
52 };
53
54 struct test_data {
55         char *test_name;
56         struct test_pdu *pdu_list;
57 };
58
59 struct context {
60         GAttrib *attrib;
61         struct bt_hog *hog;
62         guint source;
63         guint process;
64         int fd;
65         unsigned int pdu_offset;
66         const struct test_data *data;
67 };
68
69 #define data(args...) ((const unsigned char[]) { args })
70
71 #define raw_pdu(args...)    \
72 {      \
73         .valid = true,          \
74         .data = data(args), \
75         .size = sizeof(data(args)),\
76 }
77
78 #define false_pdu()     \
79 {                                               \
80                 .valid = false, \
81 }
82
83 #define define_test(name, function, args...)      \
84         do {    \
85                 const struct test_pdu pdus[] = {                        \
86                         args, { }                                       \
87                 };              \
88                 static struct test_data data;      \
89                 data.test_name = g_strdup(name);   \
90                 data.pdu_list = g_memdup(pdus, sizeof(pdus));           \
91                 tester_add(name, &data, NULL, function, NULL);     \
92         } while (0)
93
94 static void test_debug(const char *str, void *user_data)
95 {
96         const char *prefix = user_data;
97
98         tester_debug("%s%s", prefix, str);
99 }
100
101 static gboolean context_quit(gpointer user_data)
102 {
103         struct context *context = user_data;
104
105         if (context->process > 0)
106                 g_source_remove(context->process);
107
108         if (context->source > 0)
109                 g_source_remove(context->source);
110
111         bt_hog_unref(context->hog);
112
113         g_attrib_unref(context->attrib);
114
115         g_free(context);
116
117         tester_test_passed();
118
119         return FALSE;
120 }
121
122 static gboolean send_pdu(gpointer user_data)
123 {
124         struct context *context = user_data;
125         const struct test_pdu *pdu;
126         ssize_t len;
127
128         pdu = &context->data->pdu_list[context->pdu_offset++];
129
130         len = write(context->fd, pdu->data, pdu->size);
131
132         util_hexdump('<', pdu->data, len, test_debug, "hog: ");
133
134         g_assert_cmpint(len, ==, pdu->size);
135
136         context->process = 0;
137
138         if (!context->data->pdu_list[context->pdu_offset].valid)
139 #ifdef TIZEN_FEATURE_BLUEZ_MODIFY
140                 g_idle_add(context_quit, context);
141 #else
142                 context_quit(context);
143 #endif
144
145         return FALSE;
146 }
147
148 static gboolean test_handler(GIOChannel *channel, GIOCondition cond,
149                                                         gpointer user_data)
150 {
151         struct context *context = user_data;
152         unsigned char buf[512];
153         const struct test_pdu *pdu;
154         ssize_t len;
155         int fd;
156
157         pdu = &context->data->pdu_list[context->pdu_offset++];
158
159         if (cond & (G_IO_NVAL | G_IO_ERR | G_IO_HUP)) {
160                 context->source = 0;
161                 g_print("%s: cond %x\n", __func__, cond);
162                 return FALSE;
163         }
164
165         fd = g_io_channel_unix_get_fd(channel);
166
167         len = read(fd, buf, sizeof(buf));
168
169         g_assert(len > 0);
170
171         util_hexdump('>', buf, len, test_debug, "hog: ");
172
173         g_assert_cmpint(len, ==, pdu->size);
174
175         g_assert(memcmp(buf, pdu->data, pdu->size) == 0);
176
177         context->process = g_idle_add(send_pdu, context);
178
179         return TRUE;
180 }
181
182 static struct context *create_context(gconstpointer data)
183 {
184         struct context *context;
185         GIOChannel *channel, *att_io;
186         int err, sv[2], fd;
187         char name[] = "bluez-hog";
188         uint16_t vendor = 0x0002;
189         uint16_t product = 0x0001;
190         uint16_t version = 0x0001;
191
192         context = g_new0(struct context, 1);
193         err = socketpair(AF_UNIX, SOCK_SEQPACKET | SOCK_CLOEXEC, 0, sv);
194         g_assert(err == 0);
195
196         att_io = g_io_channel_unix_new(sv[0]);
197
198         g_io_channel_set_close_on_unref(att_io, TRUE);
199
200         context->attrib = g_attrib_new(att_io, 23, false);
201         g_assert(context->attrib);
202
203         g_io_channel_unref(att_io);
204
205         fd = open("/dev/null", O_WRONLY | O_CLOEXEC);
206         g_assert(fd > 0);
207
208         context->hog = bt_hog_new(fd, name, vendor, product, version, NULL);
209         g_assert(context->hog);
210
211         channel = g_io_channel_unix_new(sv[1]);
212
213         g_io_channel_set_close_on_unref(channel, TRUE);
214         g_io_channel_set_encoding(channel, NULL, NULL);
215         g_io_channel_set_buffered(channel, FALSE);
216
217         context->source = g_io_add_watch(channel,
218                                 G_IO_IN | G_IO_HUP | G_IO_ERR | G_IO_NVAL,
219                                 test_handler, context);
220         g_assert(context->source > 0);
221
222         g_io_channel_unref(channel);
223
224         context->fd = sv[1];
225         context->data = data;
226
227         return context;
228 }
229
230 static void test_hog(gconstpointer data)
231 {
232         struct context *context = create_context(data);
233
234         g_assert(bt_hog_attach(context->hog, context->attrib));
235 }
236
237 int main(int argc, char *argv[])
238 {
239         tester_init(&argc, &argv);
240
241         define_test("/TP/HGRF/RH/BV-01-I", test_hog,
242                 raw_pdu(0x10, 0x01, 0x00, 0xff, 0xff, 0x00, 0x28),
243                 raw_pdu(0x11, 0x06, 0x01, 0x00, 0x04, 0x00, 0x12,
244                         0x18, 0x05, 0x00, 0x08, 0x00, 0x12, 0x18),
245                 raw_pdu(0x10, 0x09, 0x00, 0xff, 0xff, 0x00, 0x28),
246                 raw_pdu(0x01, 0x10, 0x09, 0x00, 0x0a),
247                 raw_pdu(0x08, 0x01, 0x00, 0x04, 0x00, 0x03, 0x28),
248                 raw_pdu(0x09, 0x07, 0x03, 0x00, 0x02, 0x04, 0x00,
249                         0x4b, 0x2a),
250                 raw_pdu(0x08, 0x01, 0x00, 0x04, 0x00, 0x02, 0x28),
251                 raw_pdu(0x01, 0x08, 0x01, 0x00, 0x0a),
252                 raw_pdu(0x08, 0x05, 0x00, 0x08, 0x00, 0x02, 0x28),
253                 raw_pdu(0x01, 0x08, 0x05, 0x00, 0x0a),
254                 raw_pdu(0x08, 0x05, 0x00, 0x08, 0x00, 0x03, 0x28),
255                 raw_pdu(0x09, 0x07, 0x07, 0x00, 0x02, 0x08, 0x00,
256                         0x4b, 0x2a),
257                 raw_pdu(0x0a, 0x04, 0x00),
258                 raw_pdu(0x0b, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06,
259                         0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d,
260                         0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14,
261                         0x15, 0x16),
262                 raw_pdu(0x0a, 0x08, 0x00),
263                 raw_pdu(0x0b, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06,
264                         0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d,
265                         0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14,
266                         0x15, 0x16),
267                 raw_pdu(0x0c, 0x04, 0x00, 0x16, 0x00),
268                 raw_pdu(0x0d, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06,
269                         0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d,
270                         0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14,
271                         0x15, 0x16),
272                 raw_pdu(0x0c, 0x08, 0x00, 0x16, 0x00),
273                 raw_pdu(0x0d, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06,
274                         0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d,
275                         0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14,
276                         0x15, 0x16),
277                 raw_pdu(0x0c, 0x04, 0x00, 0x2c, 0x00),
278                 raw_pdu(0x0d, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06,
279                         0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d,
280                         0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13),
281                 raw_pdu(0x0c, 0x08, 0x00, 0x2c, 0x00),
282                 raw_pdu(0x0d, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06,
283                         0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d,
284                         0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13));
285
286         define_test("/TP/HGRF/RH/BV-08-I", test_hog,
287                 raw_pdu(0x10, 0x01, 0x00, 0xff, 0xff, 0x00, 0x28),
288                 raw_pdu(0x11, 0x06, 0x01, 0x00, 0x05, 0x00, 0x12,
289                         0x18, 0x06, 0x00, 0x0a, 0x00, 0x12, 0x18),
290                 raw_pdu(0x10, 0x0b, 0x00, 0xff, 0xff, 0x00, 0x28),
291                 raw_pdu(0x01, 0x10, 0x0b, 0x00, 0x0a),
292                 raw_pdu(0x08, 0x01, 0x00, 0x05, 0x00, 0x03, 0x28),
293                 raw_pdu(0x09, 0x07, 0x03, 0x00, 0x0a, 0x04, 0x00,
294                         0x4d, 0x2a),
295                 raw_pdu(0x08, 0x01, 0x00, 0x05, 0x00, 0x02, 0x28),
296                 raw_pdu(0x01, 0x08, 0x01, 0x00, 0x0a),
297                 raw_pdu(0x08, 0x06, 0x00, 0x0a, 0x00, 0x02, 0x28),
298                 raw_pdu(0x01, 0x08, 0x06, 0x00, 0x0a),
299                 raw_pdu(0x08, 0x06, 0x00, 0x0a, 0x00, 0x03, 0x28),
300                 raw_pdu(0x09, 0x07, 0x08, 0x00, 0x0a, 0x09, 0x00,
301                         0x4d, 0x2a),
302                 raw_pdu(0x08, 0x04, 0x00, 0x05, 0x00, 0x03, 0x28),
303                 raw_pdu(0x01, 0x08, 0x04, 0x00, 0x0a),
304                 raw_pdu(0x08, 0x09, 0x00, 0x0a, 0x00, 0x03, 0x28),
305                 raw_pdu(0x01, 0x08, 0x09, 0x00, 0x0a),
306                 raw_pdu(0x0a, 0x04, 0x00),
307                 raw_pdu(0x0b, 0xee, 0xee, 0xff, 0xff),
308                 raw_pdu(0x04, 0x05, 0x00, 0x05, 0x00),
309                 raw_pdu(0x05, 0x01, 0x05, 0x00, 0x08, 0x29),
310                 raw_pdu(0x0a, 0x09, 0x00),
311                 raw_pdu(0x0b, 0xff, 0xff, 0xee, 0xee),
312                 raw_pdu(0x04, 0x0a, 0x00, 0x0a, 0x00),
313                 raw_pdu(0x05, 0x01, 0x0a, 0x00, 0x08, 0x29),
314                 raw_pdu(0x0a, 0x05, 0x00),
315                 raw_pdu(0x0b, 0x01, 0x03),
316                 raw_pdu(0x0a, 0x0a, 0x00),
317                 raw_pdu(0x0b, 0x02, 0x03));
318
319         define_test("/TP/HGRF/RH/BV-09-I", test_hog,
320                 raw_pdu(0x10, 0x01, 0x00, 0xff, 0xff, 0x00, 0x28),
321                 raw_pdu(0x11, 0x06, 0x01, 0x00, 0x04, 0x00, 0x12,
322                         0x18, 0x05, 0x00, 0x08, 0x00, 0x12, 0x18),
323                 raw_pdu(0x10, 0x09, 0x00, 0xff, 0xff, 0x00, 0x28),
324                 raw_pdu(0x01, 0x10, 0x09, 0x00, 0x0a),
325                 raw_pdu(0x08, 0x01, 0x00, 0x04, 0x00, 0x03, 0x28),
326                 raw_pdu(0x09, 0x07, 0x03, 0x00, 0x02, 0x04, 0x00,
327                         0x4a, 0x2a),
328                 raw_pdu(0x08, 0x01, 0x00, 0x04, 0x00, 0x02, 0x28),
329                 raw_pdu(0x01, 0x08, 0x01, 0x00, 0x0a),
330                 raw_pdu(0x08, 0x05, 0x00, 0x08, 0x00, 0x02, 0x28),
331                 raw_pdu(0x01, 0x08, 0x05, 0x00, 0x0a),
332                 raw_pdu(0x08, 0x05, 0x00, 0x08, 0x00, 0x03, 0x28),
333                 raw_pdu(0x09, 0x07, 0x07, 0x00, 0x02, 0x08, 0x00,
334                         0x4a, 0x2a),
335                 raw_pdu(0x0a, 0x04, 0x00),
336                 raw_pdu(0x0b, 0x01, 0x11, 0x00, 0x01),
337                 raw_pdu(0x0a, 0x08, 0x00),
338                 raw_pdu(0x0b, 0x01, 0x11, 0x00, 0x01));
339
340         define_test("/TP/HGRF/RH/BV-06-I", test_hog,
341                 raw_pdu(0x10, 0x01, 0x00, 0xff, 0xff, 0x00, 0x28),
342                 raw_pdu(0x11, 0x06, 0x01, 0x00, 0x05, 0x00, 0x12,
343                         0x18, 0x06, 0x00, 0x0a, 0x00, 0x12, 0x18),
344                 raw_pdu(0x10, 0x0b, 0x00, 0xff, 0xff, 0x00, 0x28),
345                 raw_pdu(0x01, 0x10, 0x0b, 0x00, 0x0a),
346                 raw_pdu(0x08, 0x01, 0x00, 0x05, 0x00, 0x03, 0x28),
347                 raw_pdu(0x09, 0x07, 0x03, 0x00, 0x0a, 0x04, 0x00,
348                         0x4d, 0x2a),
349                 raw_pdu(0x08, 0x01, 0x00, 0x05, 0x00, 0x02, 0x28),
350                 raw_pdu(0x01, 0x08, 0x01, 0x00, 0x0a),
351                 raw_pdu(0x08, 0x06, 0x00, 0x0a, 0x00, 0x02, 0x28),
352                 raw_pdu(0x01, 0x08, 0x06, 0x00, 0x0a),
353                 raw_pdu(0x08, 0x06, 0x00, 0x0a, 0x00, 0x03, 0x28),
354                 raw_pdu(0x09, 0x07, 0x08, 0x00, 0x0a, 0x09, 0x00,
355                         0x4d, 0x2a),
356                 raw_pdu(0x08, 0x04, 0x00, 0x05, 0x00, 0x03, 0x28),
357                 raw_pdu(0x01, 0x08, 0x05, 0x00, 0x0a),
358                 raw_pdu(0x08, 0x09, 0x00, 0x0a, 0x00, 0x03, 0x28),
359                 raw_pdu(0x01, 0x08, 0x09, 0x00, 0x0a),
360                 raw_pdu(0x0a, 0x04, 0x00),
361                 raw_pdu(0x0b, 0xee, 0xee, 0xff, 0xff),
362                 raw_pdu(0x04, 0x05, 0x00, 0x05, 0x00),
363                 raw_pdu(0x05, 0x01, 0x05, 0x00, 0x08, 0x29),
364                 raw_pdu(0x0a, 0x09, 0x00),
365                 raw_pdu(0x0b, 0xff, 0xff, 0xee, 0xee),
366                 raw_pdu(0x04, 0x0a, 0x00, 0x0a, 0x00),
367                 raw_pdu(0x05, 0x01, 0x0a, 0x00, 0x08, 0x29),
368                 raw_pdu(0x0a, 0x05, 0x00),
369                 raw_pdu(0x0b, 0x01, 0x02),
370                 raw_pdu(0x0a, 0x0a, 0x00),
371                 raw_pdu(0x0b, 0x02, 0x02));
372
373         define_test("/TP/HGCF/RH/BV-01-I", test_hog,
374                 raw_pdu(0x10, 0x01, 0x00, 0xff, 0xff, 0x00, 0x28),
375                 raw_pdu(0x11, 0x06, 0x01, 0x00, 0x06, 0x00, 0x12,
376                         0x18, 0x07, 0x00, 0x0c, 0x00, 0x12, 0x18),
377                 raw_pdu(0x10, 0x0d, 0x00, 0xff, 0xff, 0x00, 0x28),
378                 raw_pdu(0x01, 0x10, 0x0d, 0x00, 0x0a),
379                 raw_pdu(0x08, 0x01, 0x00, 0x06, 0x00, 0x03, 0x28),
380                 raw_pdu(0x09, 0x07, 0x03, 0x00, 0x1a, 0x04, 0x00,
381                         0x4d, 0x2a),
382                 raw_pdu(0x08, 0x01, 0x00, 0x06, 0x00, 0x02, 0x28),
383                 raw_pdu(0x01, 0x08, 0x01, 0x00, 0x0a),
384                 raw_pdu(0x08, 0x07, 0x00, 0x0c, 0x00, 0x02, 0x28),
385                 raw_pdu(0x01, 0x08, 0x07, 0x00, 0x0a),
386                 raw_pdu(0x08, 0x07, 0x00, 0x0c, 0x00, 0x03, 0x28),
387                 raw_pdu(0x09, 0x07, 0x09, 0x00, 0x1a, 0x0a, 0x00,
388                         0x4d, 0x2a),
389                 raw_pdu(0x08, 0x04, 0x00, 0x06, 0x00, 0x03, 0x28),
390                 raw_pdu(0x01, 0x08, 0x04, 0x00, 0x0a),
391                 raw_pdu(0x08, 0x0a, 0x00, 0x0c, 0x00, 0x03, 0x28),
392                 raw_pdu(0x01, 0x08, 0x0a, 0x00, 0x0a),
393                 raw_pdu(0x0a, 0x04, 0x00),
394                 raw_pdu(0x0b, 0xed, 0x00),
395                 raw_pdu(0x04, 0x05, 0x00, 0x06, 0x00),
396                 raw_pdu(0x05, 0x01, 0x05, 0x00, 0x02, 0x29,
397                         0x06, 0x00, 0x08, 0x29),
398                 raw_pdu(0x0a, 0x0a, 0x00),
399                 raw_pdu(0x0b, 0xed, 0x00),
400                 raw_pdu(0x04, 0x0b, 0x00, 0x0c, 0x00),
401                 raw_pdu(0x05, 0x01, 0x0b, 0x00, 0x02, 0x29,
402                         0x0c, 0x00, 0x08, 0x29),
403                 raw_pdu(0x0a, 0x06, 0x00),
404                 raw_pdu(0x0b, 0x01, 0x01),
405                 raw_pdu(0x0a, 0x0c, 0x00),
406                 raw_pdu(0x0b, 0x02, 0x01),
407                 raw_pdu(0x0a, 0x05, 0x00),
408                 raw_pdu(0x0b, 0x00, 0x00),
409                 raw_pdu(0x0a, 0x0b, 0x00),
410                 raw_pdu(0x0b, 0x00, 0x00),
411                 raw_pdu(0x12, 0x05, 0x00, 0x01, 0x00),
412                 raw_pdu(0x13),
413                 raw_pdu(0x12, 0x0b, 0x00, 0x01, 0x00),
414                 raw_pdu(0x13));
415
416         define_test("/TP/HGRF/RH/BV-02-I", test_hog,
417                 raw_pdu(0x10, 0x01, 0x00, 0xff, 0xff, 0x00, 0x28),
418                 raw_pdu(0x11, 0x06, 0x01, 0x00, 0x05, 0x00, 0x12,
419                         0x18, 0x06, 0x00, 0x0a, 0x00, 0x12, 0x18),
420                 raw_pdu(0x10, 0x0b, 0x00, 0xff, 0xff, 0x00, 0x28),
421                 raw_pdu(0x01, 0x10, 0x0b, 0x00, 0x0a),
422                 raw_pdu(0x08, 0x01, 0x00, 0x05, 0x00, 0x03, 0x28),
423                 raw_pdu(0x09, 0x07, 0x03, 0x00, 0x02, 0x04, 0x00,
424                         0x4b, 0x2a),
425                 raw_pdu(0x08, 0x01, 0x00, 0x05, 0x00, 0x02, 0x28),
426                 raw_pdu(0x01, 0x08, 0x01, 0x00, 0x0a),
427                 raw_pdu(0x08, 0x06, 0x00, 0x0a, 0x00, 0x02, 0x28),
428                 raw_pdu(0x01, 0x08, 0x06, 0x00, 0x0a),
429                 raw_pdu(0x08, 0x06, 0x00, 0x0a, 0x00, 0x03, 0x28),
430                 raw_pdu(0x09, 0x07, 0x08, 0x00, 0x02, 0x09, 0x00,
431                         0x4b, 0x2a),
432                 raw_pdu(0x08, 0x04, 0x00, 0x05, 0x00, 0x03, 0x28),
433                 raw_pdu(0x01, 0x08, 0x04, 0x00, 0x0a),
434                 raw_pdu(0x08, 0x09, 0x00, 0x0a, 0x00, 0x03, 0x28),
435                 raw_pdu(0x01, 0x08, 0x09, 0x00, 0x0a),
436                 raw_pdu(0x0a, 0x04, 0x00),
437                 raw_pdu(0x0b, 0x01, 0x02, 0x03),
438                 raw_pdu(0x04, 0x05, 0x00, 0x05, 0x00),
439                 raw_pdu(0x05, 0x01, 0x05, 0x00, 0x07, 0x29),
440                 raw_pdu(0x0a, 0x09, 0x00),
441                 raw_pdu(0x0b, 0x01, 0x02, 0x03),
442                 raw_pdu(0x04, 0x0a, 0x00, 0x0a, 0x00),
443                 raw_pdu(0x05, 0x01, 0x0a, 0x00, 0x07, 0x29),
444                 raw_pdu(0x0a, 0x05, 0x00),
445                 raw_pdu(0x0b, 0x19, 0x2a),
446                 raw_pdu(0x0a, 0x0a, 0x00),
447                 raw_pdu(0x0b, 0x19, 0x2a));
448
449         return tester_run();
450 }