/src/adhd/cras/fuzz/cras_hfp_slc.cc
Line | Count | Source (jump to first uncovered line) |
1 | | /* Copyright 2020 The ChromiumOS Authors |
2 | | * Use of this source code is governed by a BSD-style license that can be |
3 | | * found in the LICENSE file. |
4 | | */ |
5 | | |
6 | | #include "cras/src/server/cras_hfp_slc.h" |
7 | | |
8 | | #include <fuzzer/FuzzedDataProvider.h> |
9 | | #include <stddef.h> |
10 | | #include <stdint.h> |
11 | | |
12 | | #include "cras/common/check.h" |
13 | | #include "cras/src/server/cras_bt_device.h" |
14 | | #include "cras/src/server/cras_bt_log.h" |
15 | | #include "cras/src/server/cras_iodev_list.h" |
16 | | #include "cras/src/server/cras_mix.h" |
17 | | #include "cras/src/server/cras_observer.h" |
18 | | #include "cras/src/server/cras_system_state.h" |
19 | | #include "cras_shm.h" |
20 | | |
21 | 3 | int disconnect_cb(struct hfp_slc_handle*) { |
22 | 3 | return 0; |
23 | 3 | } |
24 | | |
25 | 1.27k | extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) { |
26 | 1.27k | FuzzedDataProvider data_provider(data, size); |
27 | 1.27k | int ag_supported_features = data_provider.ConsumeIntegral<int>(); |
28 | 1.27k | std::string command = data_provider.ConsumeRemainingBytesAsString(); |
29 | 1.27k | int fd = open("/dev/null", O_RDWR); |
30 | | |
31 | 1.27k | struct cras_bt_device* bt_dev = cras_bt_device_create(NULL, ""); |
32 | 1.27k | struct hfp_slc_handle* handle = |
33 | 1.27k | hfp_slc_create(fd, ag_supported_features, bt_dev, NULL, &disconnect_cb); |
34 | 1.27k | if (!handle) { |
35 | 0 | return 0; |
36 | 0 | } |
37 | | |
38 | 1.27k | handle_at_command_for_test(handle, command.c_str()); |
39 | | |
40 | 1.27k | hfp_slc_destroy(handle); |
41 | 1.27k | cras_bt_device_remove(bt_dev); |
42 | 1.27k | return 0; |
43 | 1.27k | } |
44 | | |
45 | 4 | extern "C" int LLVMFuzzerInitialize(int* argc, char*** argv) { |
46 | 4 | char* shm_name; |
47 | 4 | if (asprintf(&shm_name, "/cras-%d", getpid()) < 0) { |
48 | 0 | exit(-ENOMEM); |
49 | 0 | } |
50 | 4 | struct cras_server_state* exp_state = |
51 | 4 | (struct cras_server_state*)calloc(1, sizeof(*exp_state)); |
52 | 4 | if (!exp_state) { |
53 | 0 | exit(-1); |
54 | 0 | } |
55 | 4 | int rw_shm_fd = open("/dev/null", O_RDWR); |
56 | 4 | int ro_shm_fd = open("/dev/null", O_RDONLY); |
57 | 4 | cras_system_state_init("/tmp", shm_name, rw_shm_fd, ro_shm_fd, exp_state, |
58 | 4 | sizeof(*exp_state)); |
59 | 4 | free(shm_name); |
60 | 4 | cras_observer_server_init(); |
61 | 4 | cras_mix_init(); |
62 | 4 | cras_iodev_list_init(); |
63 | 4 | btlog = cras_bt_event_log_init(); |
64 | 4 | return 0; |
65 | 4 | } |