forked from edrosten/bluez
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathipc-tester.c
1512 lines (1316 loc) · 45.4 KB
/
ipc-tester.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
*
* BlueZ - Bluetooth protocol stack for Linux
*
* Copyright (C) 2013-2014 Intel Corporation. All rights reserved.
*
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <poll.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <sys/un.h>
#include <libgen.h>
#include <glib.h>
#include "lib/bluetooth.h"
#include "lib/mgmt.h"
#include "src/shared/tester.h"
#include "src/shared/mgmt.h"
#include "emulator/hciemu.h"
#include "hal-msg.h"
#include "ipc-common.h"
#include <cutils/properties.h>
#define WAIT_FOR_SIGNAL_TIME 2 /* in seconds */
#define EMULATOR_SIGNAL "emulator_started"
struct test_data {
struct mgmt *mgmt;
uint16_t mgmt_index;
struct hciemu *hciemu;
enum hciemu_type hciemu_type;
pid_t bluetoothd_pid;
bool setup_done;
};
struct ipc_data {
void *buffer;
size_t len;
};
struct generic_data {
struct ipc_data ipc_data;
unsigned int num_services;
int init_services[];
};
struct regmod_msg {
struct ipc_hdr header;
struct hal_cmd_register_module cmd;
} __attribute__((packed));
#define CONNECT_TIMEOUT (5 * 1000)
#define SERVICE_NAME "bluetoothd"
static char exec_dir[PATH_MAX];
static int cmd_sk = -1;
static int notif_sk = -1;
static void read_info_callback(uint8_t status, uint16_t length,
const void *param, void *user_data)
{
struct test_data *data = tester_get_data();
const struct mgmt_rp_read_info *rp = param;
char addr[18];
uint16_t manufacturer;
uint32_t supported_settings, current_settings;
tester_print("Read Info callback");
tester_print(" Status: 0x%02x", status);
if (status || !param) {
tester_pre_setup_failed();
return;
}
ba2str(&rp->bdaddr, addr);
manufacturer = btohs(rp->manufacturer);
supported_settings = btohl(rp->supported_settings);
current_settings = btohl(rp->current_settings);
tester_print(" Address: %s", addr);
tester_print(" Version: 0x%02x", rp->version);
tester_print(" Manufacturer: 0x%04x", manufacturer);
tester_print(" Supported settings: 0x%08x", supported_settings);
tester_print(" Current settings: 0x%08x", current_settings);
tester_print(" Class: 0x%02x%02x%02x",
rp->dev_class[2], rp->dev_class[1], rp->dev_class[0]);
tester_print(" Name: %s", rp->name);
tester_print(" Short name: %s", rp->short_name);
if (strcmp(hciemu_get_address(data->hciemu), addr)) {
tester_pre_setup_failed();
return;
}
tester_pre_setup_complete();
}
static void index_added_callback(uint16_t index, uint16_t length,
const void *param, void *user_data)
{
struct test_data *data = tester_get_data();
tester_print("Index Added callback");
tester_print(" Index: 0x%04x", index);
data->mgmt_index = index;
mgmt_send(data->mgmt, MGMT_OP_READ_INFO, data->mgmt_index, 0, NULL,
read_info_callback, NULL, NULL);
}
static void index_removed_callback(uint16_t index, uint16_t length,
const void *param, void *user_data)
{
struct test_data *data = tester_get_data();
tester_print("Index Removed callback");
tester_print(" Index: 0x%04x", index);
if (index != data->mgmt_index)
return;
mgmt_unregister_index(data->mgmt, data->mgmt_index);
mgmt_unref(data->mgmt);
data->mgmt = NULL;
tester_post_teardown_complete();
}
static void read_index_list_callback(uint8_t status, uint16_t length,
const void *param, void *user_data)
{
struct test_data *data = tester_get_data();
tester_print("Read Index List callback");
tester_print(" Status: 0x%02x", status);
if (status || !param) {
tester_pre_setup_failed();
return;
}
mgmt_register(data->mgmt, MGMT_EV_INDEX_ADDED, MGMT_INDEX_NONE,
index_added_callback, NULL, NULL);
mgmt_register(data->mgmt, MGMT_EV_INDEX_REMOVED, MGMT_INDEX_NONE,
index_removed_callback, NULL, NULL);
data->hciemu = hciemu_new(data->hciemu_type);
if (!data->hciemu) {
tester_warn("Failed to setup HCI emulation");
tester_pre_setup_failed();
return;
}
tester_print("New hciemu instance created");
}
static void test_pre_setup(const void *data)
{
struct test_data *test_data = tester_get_data();
if (!tester_use_debug())
fclose(stderr);
test_data->mgmt = mgmt_new_default();
if (!test_data->mgmt) {
tester_warn("Failed to setup management interface");
tester_pre_setup_failed();
return;
}
mgmt_send(test_data->mgmt, MGMT_OP_READ_INDEX_LIST, MGMT_INDEX_NONE, 0,
NULL, read_index_list_callback, NULL, NULL);
}
static void test_post_teardown(const void *data)
{
struct test_data *test_data = tester_get_data();
if (test_data->hciemu) {
hciemu_unref(test_data->hciemu);
test_data->hciemu = NULL;
}
}
static void bluetoothd_start(int hci_index)
{
char prg_name[PATH_MAX];
char index[8];
char *prg_argv[4];
snprintf(prg_name, sizeof(prg_name), "%s/%s", exec_dir, "bluetoothd");
snprintf(index, sizeof(index), "%d", hci_index);
prg_argv[0] = prg_name;
prg_argv[1] = "-i";
prg_argv[2] = index;
prg_argv[3] = NULL;
if (!tester_use_debug())
fclose(stderr);
execve(prg_argv[0], prg_argv, NULL);
}
static void emulator(int pipe, int hci_index)
{
static const char SYSTEM_SOCKET_PATH[] = "\0android_system";
char buf[1024];
struct sockaddr_un addr;
struct timeval tv;
int fd;
ssize_t len;
fd = socket(PF_LOCAL, SOCK_DGRAM | SOCK_CLOEXEC, 0);
if (fd < 0)
goto failed;
tv.tv_sec = WAIT_FOR_SIGNAL_TIME;
tv.tv_usec = 0;
setsockopt(fd, SOL_SOCKET, SO_RCVTIMEO, (char *)&tv, sizeof(tv));
memset(&addr, 0, sizeof(addr));
addr.sun_family = AF_UNIX;
memcpy(addr.sun_path, SYSTEM_SOCKET_PATH, sizeof(SYSTEM_SOCKET_PATH));
if (bind(fd, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
perror("Failed to bind system socket");
goto failed;
}
len = write(pipe, EMULATOR_SIGNAL, sizeof(EMULATOR_SIGNAL));
if (len != sizeof(EMULATOR_SIGNAL))
goto failed;
memset(buf, 0, sizeof(buf));
len = read(fd, buf, sizeof(buf));
if (len <= 0 || strcmp(buf, "ctl.start=bluetoothd"))
goto failed;
close(pipe);
close(fd);
return bluetoothd_start(hci_index);
failed:
close(pipe);
if (fd >= 0)
close(fd);
}
static int accept_connection(int sk)
{
int err;
struct pollfd pfd;
int new_sk;
memset(&pfd, 0 , sizeof(pfd));
pfd.fd = sk;
pfd.events = POLLIN;
err = poll(&pfd, 1, CONNECT_TIMEOUT);
if (err < 0) {
err = errno;
tester_warn("Failed to poll: %d (%s)", err, strerror(err));
return -errno;
}
if (err == 0) {
tester_warn("bluetoothd connect timeout");
return -errno;
}
new_sk = accept(sk, NULL, NULL);
if (new_sk < 0) {
err = errno;
tester_warn("Failed to accept socket: %d (%s)",
err, strerror(err));
return -errno;
}
return new_sk;
}
static bool init_ipc(void)
{
struct sockaddr_un addr;
int sk;
int err;
sk = socket(AF_LOCAL, SOCK_SEQPACKET, 0);
if (sk < 0) {
err = errno;
tester_warn("Failed to create socket: %d (%s)", err,
strerror(err));
return false;
}
memset(&addr, 0, sizeof(addr));
addr.sun_family = AF_UNIX;
memcpy(addr.sun_path, BLUEZ_HAL_SK_PATH, sizeof(BLUEZ_HAL_SK_PATH));
if (bind(sk, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
err = errno;
tester_warn("Failed to bind socket: %d (%s)", err,
strerror(err));
close(sk);
return false;
}
if (listen(sk, 2) < 0) {
err = errno;
tester_warn("Failed to listen on socket: %d (%s)", err,
strerror(err));
close(sk);
return false;
}
/* Start Android Bluetooth daemon service */
if (property_set("ctl.start", SERVICE_NAME) < 0) {
tester_warn("Failed to start service %s", SERVICE_NAME);
close(sk);
return false;
}
cmd_sk = accept_connection(sk);
if (cmd_sk < 0) {
close(sk);
return false;
}
notif_sk = accept_connection(sk);
if (notif_sk < 0) {
close(sk);
close(cmd_sk);
cmd_sk = -1;
return false;
}
tester_print("bluetoothd connected");
close(sk);
return true;
}
static void cleanup_ipc(void)
{
if (cmd_sk < 0)
return;
close(cmd_sk);
cmd_sk = -1;
}
static gboolean check_for_daemon(gpointer user_data)
{
int status;
struct test_data *data = user_data;
if ((waitpid(data->bluetoothd_pid, &status, WNOHANG))
!= data->bluetoothd_pid)
return true;
if (data->setup_done) {
if (WIFEXITED(status) &&
(WEXITSTATUS(status) == EXIT_SUCCESS)) {
tester_test_passed();
return false;
}
tester_test_failed();
} else {
tester_setup_failed();
test_post_teardown(data);
}
tester_warn("Unexpected Daemon shutdown with status %d", status);
return false;
}
static bool setup_module(int service_id)
{
struct ipc_hdr response;
struct ipc_hdr expected_response;
struct regmod_msg btmodule_msg = {
.header = {
.service_id = HAL_SERVICE_ID_CORE,
.opcode = HAL_OP_REGISTER_MODULE,
.len = sizeof(struct hal_cmd_register_module),
},
.cmd = {
.service_id = service_id,
.mode = HAL_MODE_DEFAULT,
.max_clients = 1,
},
};
if (write(cmd_sk, &btmodule_msg, sizeof(btmodule_msg)) < 0)
goto fail;
if (read(cmd_sk, &response, sizeof(response)) < 0)
goto fail;
expected_response = btmodule_msg.header;
expected_response.len = 0;
if (memcmp(&response, &expected_response, sizeof(response)) == 0)
return true;
fail:
tester_warn("Module registration failed.");
return false;
}
static void setup(const void *data)
{
const struct generic_data *generic_data = data;
struct test_data *test_data = tester_get_data();
int signal_fd[2];
char buf[1024];
pid_t pid;
int len;
unsigned int i;
if (pipe(signal_fd))
goto failed;
pid = fork();
if (pid < 0) {
close(signal_fd[0]);
close(signal_fd[1]);
goto failed;
}
if (pid == 0) {
if (!tester_use_debug())
fclose(stderr);
close(signal_fd[0]);
emulator(signal_fd[1], test_data->mgmt_index);
exit(0);
}
close(signal_fd[1]);
test_data->bluetoothd_pid = pid;
len = read(signal_fd[0], buf, sizeof(buf));
if (len <= 0 || (strcmp(buf, EMULATOR_SIGNAL))) {
close(signal_fd[0]);
goto failed;
}
g_idle_add_full(G_PRIORITY_DEFAULT_IDLE, check_for_daemon, test_data,
NULL);
if (!init_ipc()) {
tester_warn("Cannot initialize IPC mechanism!");
goto failed;
}
tester_print("Will init %d services.", generic_data->num_services);
for (i = 0; i < generic_data->num_services; i++)
if (!setup_module(generic_data->init_services[i])) {
cleanup_ipc();
goto failed;
}
test_data->setup_done = true;
tester_setup_complete();
return;
failed:
g_idle_remove_by_data(test_data);
tester_setup_failed();
test_post_teardown(data);
}
static void teardown(const void *data)
{
struct test_data *test_data = tester_get_data();
g_idle_remove_by_data(test_data);
cleanup_ipc();
if (test_data->bluetoothd_pid)
waitpid(test_data->bluetoothd_pid, NULL, 0);
tester_teardown_complete();
}
static void ipc_send_tc(const void *data)
{
const struct generic_data *generic_data = data;
const struct ipc_data *ipc_data = &generic_data->ipc_data;
if (ipc_data->len) {
if (write(cmd_sk, ipc_data->buffer, ipc_data->len) < 0)
tester_test_failed();
}
}
#define service_data(args...) { args }
#define gen_data(writelen, writebuf, servicelist...) \
{ \
.ipc_data = { \
.buffer = writebuf, \
.len = writelen, \
}, \
.init_services = service_data(servicelist), \
.num_services = sizeof((const int[]) \
service_data(servicelist)) / \
sizeof(int), \
}
#define test_generic(name, test, setup, teardown, buffer, writelen, \
services...) \
do { \
struct test_data *user; \
static const struct generic_data data = \
gen_data(writelen, buffer, services); \
user = g_malloc0(sizeof(struct test_data)); \
if (!user) \
break; \
user->hciemu_type = HCIEMU_TYPE_BREDRLE; \
tester_add_full(name, &data, test_pre_setup, setup, \
test, teardown, test_post_teardown, \
3, user, g_free); \
} while (0)
#define test_opcode_valid(_name, _service, _opcode, _len, _servicelist...) \
do { \
static struct ipc_hdr hdr = { \
.service_id = _service, \
.opcode = _opcode, \
.len = _len, \
}; \
\
test_generic("Opcode out of range: "_name, \
ipc_send_tc, setup, teardown, \
&hdr, \
sizeof(hdr), \
_servicelist); \
} while (0)
struct vardata {
struct ipc_hdr hdr;
uint8_t buf[IPC_MTU];
} __attribute__((packed));
#define test_datasize_valid(_name, _service, _opcode, _hlen, _addatasize, \
_servicelist...) \
do { \
static struct vardata vdata = { \
.hdr.service_id = _service, \
.hdr.opcode = _opcode, \
.hdr.len = (_hlen) + (_addatasize), \
.buf = {}, \
}; \
test_generic("Data size "_name, \
ipc_send_tc, setup, teardown, \
&vdata, \
sizeof(vdata.hdr) + (_hlen) + (_addatasize),\
_servicelist); \
} while (0)
static struct regmod_msg register_bt_msg = {
.header = {
.service_id = HAL_SERVICE_ID_CORE,
.opcode = HAL_OP_REGISTER_MODULE,
.len = sizeof(struct hal_cmd_register_module),
},
.cmd = {
.service_id = HAL_SERVICE_ID_BLUETOOTH,
},
};
static struct regmod_msg register_bt_malformed_size_msg = {
.header = {
.service_id = HAL_SERVICE_ID_CORE,
.opcode = HAL_OP_REGISTER_MODULE,
/* wrong payload size declared */
.len = sizeof(struct hal_cmd_register_module) - 1,
},
.cmd = {
.service_id = HAL_SERVICE_ID_CORE,
},
};
struct malformed_data3_struct {
struct regmod_msg valid_msg;
int redundant_data;
} __attribute__((packed));
static struct malformed_data3_struct malformed_data3_msg = {
/* valid register service message */
.valid_msg = {
.header = {
.service_id = HAL_SERVICE_ID_CORE,
.opcode = HAL_OP_REGISTER_MODULE,
.len = sizeof(struct hal_cmd_register_module),
},
.cmd = {
.service_id = HAL_SERVICE_ID_CORE,
},
},
/* plus redundant data */
. redundant_data = 666,
};
static struct ipc_hdr enable_unknown_service_hdr = {
.service_id = HAL_SERVICE_ID_MAX + 1,
.opcode = HAL_OP_REGISTER_MODULE,
.len = 0,
};
static struct ipc_hdr enable_bt_service_hdr = {
.service_id = HAL_SERVICE_ID_BLUETOOTH,
.opcode = HAL_OP_ENABLE,
.len = 0,
};
struct bt_set_adapter_prop_data {
struct ipc_hdr hdr;
struct hal_cmd_set_adapter_prop prop;
/* data placeholder for hal_cmd_set_adapter_prop.val[0] */
uint8_t buf[IPC_MTU - sizeof(struct ipc_hdr) -
sizeof(struct hal_cmd_set_adapter_prop)];
} __attribute__((packed));
#define set_name "new name"
static struct bt_set_adapter_prop_data bt_set_adapter_prop_data_overs = {
.hdr.service_id = HAL_SERVICE_ID_BLUETOOTH,
.hdr.opcode = HAL_OP_SET_ADAPTER_PROP,
.hdr.len = sizeof(struct hal_cmd_set_adapter_prop) + sizeof(set_name),
.prop.type = HAL_PROP_ADAPTER_NAME,
/* declare wrong descriptor length */
.prop.len = sizeof(set_name) + 1,
/* init prop.val[0] */
.buf = set_name,
};
static struct bt_set_adapter_prop_data bt_set_adapter_prop_data_unders = {
.hdr.service_id = HAL_SERVICE_ID_BLUETOOTH,
.hdr.opcode = HAL_OP_SET_ADAPTER_PROP,
.hdr.len = sizeof(struct hal_cmd_set_adapter_prop) + sizeof(set_name),
.prop.type = HAL_PROP_ADAPTER_NAME,
/* declare wrong descriptor length */
.prop.len = sizeof(set_name) - 1,
/* init prop.val[0] */
.buf = set_name,
};
struct bt_set_remote_prop_data {
struct ipc_hdr hdr;
struct hal_cmd_set_remote_device_prop prop;
/* data placeholder for hal_cmd_set_remote_device_prop.val[0] */
uint8_t buf[IPC_MTU - sizeof(struct ipc_hdr) -
sizeof(struct hal_cmd_set_remote_device_prop)];
} __attribute__((packed));
static struct bt_set_remote_prop_data bt_set_remote_prop_data_overs = {
.hdr.service_id = HAL_SERVICE_ID_BLUETOOTH,
.hdr.opcode = HAL_OP_SET_REMOTE_DEVICE_PROP,
.hdr.len = sizeof(struct hal_cmd_set_remote_device_prop) +
sizeof(set_name),
.prop.bdaddr = {},
.prop.type = HAL_PROP_DEVICE_NAME,
/* declare wrong descriptor length */
.prop.len = sizeof(set_name) + 1,
.buf = set_name,
};
static struct bt_set_remote_prop_data bt_set_remote_prop_data_unders = {
.hdr.service_id = HAL_SERVICE_ID_BLUETOOTH,
.hdr.opcode = HAL_OP_SET_REMOTE_DEVICE_PROP,
.hdr.len = sizeof(struct hal_cmd_set_remote_device_prop) +
sizeof(set_name),
.prop.bdaddr = {},
.prop.type = HAL_PROP_DEVICE_NAME,
/* declare wrong descriptor length */
.prop.len = sizeof(set_name) - 1,
.buf = set_name,
};
struct hidhost_set_info_data {
struct ipc_hdr hdr;
struct hal_cmd_hidhost_set_info info;
/* data placeholder for hal_cmd_hidhost_set_info.descr[0] field */
uint8_t buf[IPC_MTU - sizeof(struct ipc_hdr) -
sizeof(struct hal_cmd_hidhost_set_info)];
} __attribute__((packed));
#define set_info_data "some descriptor"
static struct hidhost_set_info_data hidhost_set_info_data_overs = {
.hdr.service_id = HAL_SERVICE_ID_HIDHOST,
.hdr.opcode = HAL_OP_HIDHOST_SET_INFO,
.hdr.len = sizeof(struct hal_cmd_hidhost_set_info) +
sizeof(set_info_data),
/* declare wrong descriptor length */
.info.descr_len = sizeof(set_info_data) + 1,
/* init .info.descr[0] */
.buf = set_info_data,
};
static struct hidhost_set_info_data hidhost_set_info_data_unders = {
.hdr.service_id = HAL_SERVICE_ID_HIDHOST,
.hdr.opcode = HAL_OP_HIDHOST_SET_INFO,
.hdr.len = sizeof(struct hal_cmd_hidhost_set_info) +
sizeof(set_info_data),
/* declare wrong descriptor length */
.info.descr_len = sizeof(set_info_data) - 1,
/* init .info.descr[0] */
.buf = set_info_data,
};
struct hidhost_set_report_data {
struct ipc_hdr hdr;
struct hal_cmd_hidhost_set_report report;
/* data placeholder for hal_cmd_hidhost_set_report.data[0] field */
uint8_t buf[IPC_MTU - sizeof(struct ipc_hdr) -
sizeof(struct hal_cmd_hidhost_set_report)];
} __attribute__((packed));
#define set_rep_data "1234567890"
static struct hidhost_set_report_data hidhost_set_report_data_overs = {
.hdr.service_id = HAL_SERVICE_ID_HIDHOST,
.hdr.opcode = HAL_OP_HIDHOST_SET_REPORT,
.hdr.len = sizeof(struct hal_cmd_hidhost_set_report) +
sizeof(set_rep_data),
/* declare wrong descriptor length */
.report.len = sizeof(set_rep_data) + 1,
/* init report.data[0] */
.buf = set_rep_data,
};
static struct hidhost_set_report_data hidhost_set_report_data_unders = {
.hdr.service_id = HAL_SERVICE_ID_HIDHOST,
.hdr.opcode = HAL_OP_HIDHOST_SET_REPORT,
.hdr.len = sizeof(struct hal_cmd_hidhost_set_report) +
sizeof(set_rep_data),
/* declare wrong descriptor length */
.report.len = sizeof(set_rep_data) - 1,
/* init report.data[0] */
.buf = set_rep_data,
};
struct hidhost_send_data_data {
struct ipc_hdr hdr;
struct hal_cmd_hidhost_send_data hiddata;
/* data placeholder for hal_cmd_hidhost_send_data.data[0] field */
uint8_t buf[IPC_MTU - sizeof(struct ipc_hdr) -
sizeof(struct hal_cmd_hidhost_send_data)];
} __attribute__((packed));
#define send_data_data "1234567890"
static struct hidhost_send_data_data hidhost_send_data_overs = {
.hdr.service_id = HAL_SERVICE_ID_HIDHOST,
.hdr.opcode = HAL_OP_HIDHOST_SEND_DATA,
.hdr.len = sizeof(struct hal_cmd_hidhost_send_data) +
sizeof(send_data_data),
/* declare wrong descriptor length */
.hiddata.len = sizeof(send_data_data) + 1,
/* init .hiddata.data[0] */
.buf = send_data_data,
};
static struct hidhost_send_data_data hidhost_send_data_unders = {
.hdr.service_id = HAL_SERVICE_ID_HIDHOST,
.hdr.opcode = HAL_OP_HIDHOST_SEND_DATA,
.hdr.len = sizeof(struct hal_cmd_hidhost_send_data) +
sizeof(send_data_data),
/* declare wrong descriptor length */
.hiddata.len = sizeof(send_data_data) - 1,
/* init .hiddata.data[0] */
.buf = send_data_data,
};
#define hfp_number "#1234567890"
struct hfp_dial_data {
struct ipc_hdr hdr;
struct hal_cmd_hf_client_dial data;
uint8_t buf[IPC_MTU - sizeof(struct ipc_hdr) -
sizeof(struct hal_cmd_hf_client_dial)];
} __attribute__((packed));
static struct hfp_dial_data hfp_dial_overs = {
.hdr.service_id = HAL_SERVICE_ID_HANDSFREE_CLIENT,
.hdr.opcode = HAL_OP_HF_CLIENT_DIAL,
.hdr.len = sizeof(struct hal_cmd_hf_client_dial) + sizeof(hfp_number),
.data.number_len = sizeof(hfp_number) + 1,
.buf = hfp_number,
};
static struct hfp_dial_data hfp_dial_unders = {
.hdr.service_id = HAL_SERVICE_ID_HANDSFREE_CLIENT,
.hdr.opcode = HAL_OP_HF_CLIENT_DIAL,
.hdr.len = sizeof(struct hal_cmd_hf_client_dial) + sizeof(hfp_number),
.data.number_len = sizeof(hfp_number) - 1,
.buf = hfp_number,
};
int main(int argc, char *argv[])
{
snprintf(exec_dir, sizeof(exec_dir), "%s", dirname(argv[0]));
tester_init(&argc, &argv);
/* check general IPC errors */
test_generic("Too small data",
ipc_send_tc, setup, teardown,
®ister_bt_msg, 1);
test_generic("Malformed data (wrong payload declared)",
ipc_send_tc, setup, teardown,
®ister_bt_malformed_size_msg,
sizeof(register_bt_malformed_size_msg),
HAL_SERVICE_ID_BLUETOOTH);
test_generic("Malformed data2 (undersized msg)",
ipc_send_tc, setup, teardown,
®ister_bt_msg,
sizeof(register_bt_msg) - 1,
HAL_SERVICE_ID_BLUETOOTH);
test_generic("Malformed data3 (oversized msg)",
ipc_send_tc, setup, teardown,
&malformed_data3_msg,
sizeof(malformed_data3_msg),
HAL_SERVICE_ID_BLUETOOTH);
test_generic("Invalid service",
ipc_send_tc, setup, teardown,
&enable_unknown_service_hdr,
sizeof(enable_unknown_service_hdr),
HAL_SERVICE_ID_BLUETOOTH);
test_generic("Enable unregistered service",
ipc_send_tc, setup, teardown,
&enable_bt_service_hdr,
sizeof(enable_bt_service_hdr));
/* check service handler's max opcode value */
test_opcode_valid("CORE", HAL_SERVICE_ID_CORE, 0x03, 0);
test_opcode_valid("BLUETOOTH", HAL_SERVICE_ID_BLUETOOTH, 0x15, 0,
HAL_SERVICE_ID_BLUETOOTH);
test_opcode_valid("SOCK", HAL_SERVICE_ID_SOCKET, 0x03, 0,
HAL_SERVICE_ID_BLUETOOTH, HAL_SERVICE_ID_SOCKET);
test_opcode_valid("HIDHOST", HAL_SERVICE_ID_HIDHOST, 0x10, 0,
HAL_SERVICE_ID_BLUETOOTH, HAL_SERVICE_ID_HIDHOST);
test_opcode_valid("PAN", HAL_SERVICE_ID_PAN, 0x05, 0,
HAL_SERVICE_ID_BLUETOOTH, HAL_SERVICE_ID_PAN);
test_opcode_valid("HANDSFREE", HAL_SERVICE_ID_HANDSFREE, 0x10, 0,
HAL_SERVICE_ID_BLUETOOTH,
HAL_SERVICE_ID_HANDSFREE);
test_opcode_valid("A2DP", HAL_SERVICE_ID_A2DP, 0x03, 0,
HAL_SERVICE_ID_BLUETOOTH, HAL_SERVICE_ID_A2DP);
test_opcode_valid("HEALTH", HAL_SERVICE_ID_HEALTH, 0x06, 0,
HAL_SERVICE_ID_BLUETOOTH,
HAL_SERVICE_ID_HEALTH);
test_opcode_valid("AVRCP", HAL_SERVICE_ID_AVRCP, 0x0b, 0,
HAL_SERVICE_ID_BLUETOOTH, HAL_SERVICE_ID_AVRCP);
test_opcode_valid("GATT", HAL_SERVICE_ID_GATT, 0x24, 0,
HAL_SERVICE_ID_BLUETOOTH, HAL_SERVICE_ID_GATT);
test_opcode_valid("HF_CLIENT", HAL_SERVICE_ID_HANDSFREE_CLIENT, 0x10, 0,
HAL_SERVICE_ID_BLUETOOTH,
HAL_SERVICE_ID_HANDSFREE_CLIENT);
test_opcode_valid("MAP_CLIENT", HAL_SERVICE_ID_MAP_CLIENT, 0x01, 0,
HAL_SERVICE_ID_BLUETOOTH,
HAL_SERVICE_ID_MAP_CLIENT);
/* check for valid data size */
test_datasize_valid("CORE Register+", HAL_SERVICE_ID_CORE,
HAL_OP_REGISTER_MODULE,
sizeof(struct hal_cmd_register_module), 1);
test_datasize_valid("CORE Register-", HAL_SERVICE_ID_CORE,
HAL_OP_REGISTER_MODULE,
sizeof(struct hal_cmd_register_module), -1);
test_datasize_valid("CORE Unregister+", HAL_SERVICE_ID_CORE,
HAL_OP_UNREGISTER_MODULE,
sizeof(struct hal_cmd_unregister_module), 1);
test_datasize_valid("CORE Unregister-", HAL_SERVICE_ID_CORE,
HAL_OP_UNREGISTER_MODULE,
sizeof(struct hal_cmd_unregister_module), -1);
/* check for valid data size for BLUETOOTH */
test_datasize_valid("BT Enable+", HAL_SERVICE_ID_BLUETOOTH,
HAL_OP_ENABLE,
0, 1,
HAL_SERVICE_ID_BLUETOOTH);
test_datasize_valid("BT Disable+", HAL_SERVICE_ID_BLUETOOTH,
HAL_OP_DISABLE,
0, 1,
HAL_SERVICE_ID_BLUETOOTH);
test_datasize_valid("BT Get Adapter Props+", HAL_SERVICE_ID_BLUETOOTH,
HAL_OP_GET_ADAPTER_PROPS,
0, 1,
HAL_SERVICE_ID_BLUETOOTH);
test_datasize_valid("BT Get Adapter Prop+", HAL_SERVICE_ID_BLUETOOTH,
HAL_OP_GET_ADAPTER_PROP,
sizeof(struct hal_cmd_get_adapter_prop), 1,
HAL_SERVICE_ID_BLUETOOTH);
test_datasize_valid("BT Get Adapter Prop-", HAL_SERVICE_ID_BLUETOOTH,
HAL_OP_GET_ADAPTER_PROP,
sizeof(struct hal_cmd_get_adapter_prop), -1,
HAL_SERVICE_ID_BLUETOOTH);
test_datasize_valid("BT Set Adapter Prop+", HAL_SERVICE_ID_BLUETOOTH,
HAL_OP_SET_ADAPTER_PROP,
sizeof(struct hal_cmd_set_adapter_prop), 1,
HAL_SERVICE_ID_BLUETOOTH);
test_datasize_valid("BT Set Adapter Prop-", HAL_SERVICE_ID_BLUETOOTH,
HAL_OP_SET_ADAPTER_PROP,
sizeof(struct hal_cmd_set_adapter_prop), -1,
HAL_SERVICE_ID_BLUETOOTH);
test_generic("Data size BT Set Adapter Prop Vardata+",
ipc_send_tc, setup, teardown,
&bt_set_adapter_prop_data_overs,
(sizeof(struct ipc_hdr) +
sizeof(struct hal_cmd_set_adapter_prop) +
sizeof(set_name)),
HAL_SERVICE_ID_BLUETOOTH);
test_generic("Data size BT Set Adapter Prop Vardata+",
ipc_send_tc, setup, teardown,
&bt_set_adapter_prop_data_unders,
(sizeof(struct ipc_hdr) +
sizeof(struct hal_cmd_set_adapter_prop) +
sizeof(set_name)),