]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/libsystemd-network/fuzz-lldp-rx.c
tree-wide: introduce PIPE_EBADF macro
[thirdparty/systemd.git] / src / libsystemd-network / fuzz-lldp-rx.c
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include <errno.h>
4 #include <unistd.h>
5
6 #include "sd-event.h"
7 #include "sd-lldp-rx.h"
8
9 #include "fd-util.h"
10 #include "fuzz.h"
11 #include "lldp-network.h"
12
13 static int test_fd[2] = PIPE_EBADF;
14
15 int lldp_network_bind_raw_socket(int ifindex) {
16 if (socketpair(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC | SOCK_NONBLOCK, 0, test_fd) < 0)
17 return -errno;
18
19 return test_fd[0];
20 }
21
22 int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
23 _cleanup_(sd_event_unrefp) sd_event *e = NULL;
24 _cleanup_(sd_lldp_rx_unrefp) sd_lldp_rx *lldp_rx = NULL;
25
26 if (outside_size_range(size, 0, 2048))
27 return 0;
28
29 assert_se(sd_event_new(&e) == 0);
30 assert_se(sd_lldp_rx_new(&lldp_rx) >= 0);
31 assert_se(sd_lldp_rx_set_ifindex(lldp_rx, 42) >= 0);
32 assert_se(sd_lldp_rx_attach_event(lldp_rx, e, 0) >= 0);
33 assert_se(sd_lldp_rx_start(lldp_rx) >= 0);
34
35 assert_se(write(test_fd[1], data, size) == (ssize_t) size);
36 assert_se(sd_event_run(e, 0) >= 0);
37
38 assert_se(sd_lldp_rx_stop(lldp_rx) >= 0);
39 assert_se(sd_lldp_rx_detach_event(lldp_rx) >= 0);
40 test_fd[1] = safe_close(test_fd[1]);
41
42 return 0;
43 }