From: Yu Watanabe Date: Mon, 4 Sep 2023 08:38:10 +0000 (+0900) Subject: test: extract dummy icmp6 utils for tests X-Git-Tag: v255-rc1~591^2 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=refs%2Fpull%2F29052%2Fhead;p=thirdparty%2Fsystemd.git test: extract dummy icmp6 utils for tests This extracts common implementation of dummy icmp6 utils used by tests. --- diff --git a/src/libsystemd-network/fuzz-ndisc-rs.c b/src/libsystemd-network/fuzz-ndisc-rs.c index ef5567329e4..54dbfc1c76f 100644 --- a/src/libsystemd-network/fuzz-ndisc-rs.c +++ b/src/libsystemd-network/fuzz-ndisc-rs.c @@ -9,50 +9,10 @@ #include "alloc-util.h" #include "fd-util.h" #include "fuzz.h" -#include "icmp6-util.h" +#include "icmp6-util-unix.h" #include "ndisc-internal.h" #include "socket-util.h" -static int test_fd[2] = PIPE_EBADF; - -int icmp6_bind_router_solicitation(int index) { - assert_se(socketpair(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC | SOCK_NONBLOCK, 0, test_fd) >= 0); - return test_fd[0]; -} - -int icmp6_bind_router_advertisement(int index) { - return -ENOSYS; -} - -static struct in6_addr dummy_link_local = { - .s6_addr = { - 0xfe, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x12, 0x34, 0x56, 0xff, 0xfe, 0x78, 0x9a, 0xbc, - }, -}; - -int icmp6_receive( - int fd, - void *iov_base, - size_t iov_len, - struct in6_addr *ret_sender, - triple_timestamp *ret_timestamp) { - - assert_se(read(fd, iov_base, iov_len) == (ssize_t) iov_len); - - if (ret_timestamp) - triple_timestamp_get(ret_timestamp); - - if (ret_sender) - *ret_sender = dummy_link_local; - - return 0; -} - -int icmp6_send_router_solicitation(int s, const struct ether_addr *ether_addr) { - return 0; -} - int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) { struct ether_addr mac_addr = { .ether_addr_octet = {'A', 'B', 'C', '1', '2', '3'} diff --git a/src/libsystemd-network/icmp6-util-unix.c b/src/libsystemd-network/icmp6-util-unix.c new file mode 100644 index 00000000000..bbb3bc8cc6c --- /dev/null +++ b/src/libsystemd-network/icmp6-util-unix.c @@ -0,0 +1,53 @@ +/* SPDX-License-Identifier: LGPL-2.1-or-later */ + +#include +#include + +#include "fd-util.h" +#include "icmp6-util-unix.h" + +send_ra_t send_ra_function = NULL; +int test_fd[2] = PIPE_EBADF; + +static struct in6_addr dummy_link_local = { + .s6_addr = { + 0xfe, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x12, 0x34, 0x56, 0xff, 0xfe, 0x78, 0x9a, 0xbc, + }, +}; + +int icmp6_bind_router_solicitation(int ifindex) { + if (socketpair(AF_UNIX, SOCK_DGRAM | SOCK_CLOEXEC | SOCK_NONBLOCK, 0, test_fd) < 0) + return -errno; + + return test_fd[0]; +} + +int icmp6_bind_router_advertisement(int ifindex) { + return test_fd[1]; +} + +int icmp6_send_router_solicitation(int s, const struct ether_addr *ether_addr) { + if (!send_ra_function) + return 0; + + return send_ra_function(0); +} + +int icmp6_receive( + int fd, + void *iov_base, + size_t iov_len, + struct in6_addr *ret_sender, + triple_timestamp *ret_timestamp) { + + assert_se(read (fd, iov_base, iov_len) == (ssize_t) iov_len); + + if (ret_timestamp) + triple_timestamp_get(ret_timestamp); + + if (ret_sender) + *ret_sender = dummy_link_local; + + return 0; +} diff --git a/src/libsystemd-network/icmp6-util-unix.h b/src/libsystemd-network/icmp6-util-unix.h new file mode 100644 index 00000000000..a9cb05a96e3 --- /dev/null +++ b/src/libsystemd-network/icmp6-util-unix.h @@ -0,0 +1,9 @@ +/* SPDX-License-Identifier: LGPL-2.1-or-later */ +#pragma once + +#include "icmp6-util.h" + +typedef int (*send_ra_t)(uint8_t flags); + +extern send_ra_t send_ra_function; +extern int test_fd[2]; diff --git a/src/libsystemd-network/meson.build b/src/libsystemd-network/meson.build index 043d3bc2548..6f62a80cb39 100644 --- a/src/libsystemd-network/meson.build +++ b/src/libsystemd-network/meson.build @@ -82,10 +82,16 @@ executables += [ 'sources' : files('test-lldp-rx.c'), }, network_test_template + { - 'sources' : files('test-ndisc-ra.c'), + 'sources' : files( + 'test-ndisc-ra.c', + 'icmp6-util-unix.c', + ), }, network_test_template + { - 'sources' : files('test-ndisc-rs.c'), + 'sources' : files( + 'test-ndisc-rs.c', + 'icmp6-util-unix.c', + ), }, network_test_template + { 'sources' : files('test-sd-dhcp-lease.c'), @@ -106,6 +112,9 @@ executables += [ 'sources' : files('fuzz-lldp-rx.c'), }, network_fuzz_template + { - 'sources' : files('fuzz-ndisc-rs.c'), + 'sources' : files( + 'fuzz-ndisc-rs.c', + 'icmp6-util-unix.c', + ), }, ] diff --git a/src/libsystemd-network/test-ndisc-ra.c b/src/libsystemd-network/test-ndisc-ra.c index d3d96e776c1..18004f64665 100644 --- a/src/libsystemd-network/test-ndisc-ra.c +++ b/src/libsystemd-network/test-ndisc-ra.c @@ -11,7 +11,7 @@ #include "alloc-util.h" #include "hexdecoct.h" -#include "icmp6-util.h" +#include "icmp6-util-unix.h" #include "socket-util.h" #include "strv.h" #include "tests.h" @@ -52,7 +52,6 @@ static uint8_t advertisement[] = { }; static bool test_stopped; -static int test_fd[2]; static struct { struct in6_addr address; unsigned char prefixlen; @@ -208,36 +207,6 @@ TEST(radv) { assert_se(!ra); } -int icmp6_bind_router_solicitation(int ifindex) { - return -ENOSYS; -} - -int icmp6_bind_router_advertisement(int ifindex) { - assert_se(ifindex == 42); - - return test_fd[1]; -} - -int icmp6_send_router_solicitation(int s, const struct ether_addr *ether_addr) { - - return 0; -} - -int icmp6_receive( - int fd, - void *iov_base, - size_t iov_len, - struct in6_addr *ret_sender, - triple_timestamp *ret_timestamp) { - - assert_se(read (fd, iov_base, iov_len) == (ssize_t)iov_len); - - if (ret_timestamp) - triple_timestamp_get(ret_timestamp); - - return 0; -} - static int radv_recv(sd_event_source *s, int fd, uint32_t revents, void *userdata) { sd_radv *ra = userdata; unsigned char buf[168]; diff --git a/src/libsystemd-network/test-ndisc-rs.c b/src/libsystemd-network/test-ndisc-rs.c index baa9cb2696b..09e292a2ec3 100644 --- a/src/libsystemd-network/test-ndisc-rs.c +++ b/src/libsystemd-network/test-ndisc-rs.c @@ -12,7 +12,7 @@ #include "alloc-util.h" #include "fd-util.h" #include "hexdecoct.h" -#include "icmp6-util.h" +#include "icmp6-util-unix.h" #include "socket-util.h" #include "strv.h" #include "ndisc-internal.h" @@ -23,12 +23,8 @@ static struct ether_addr mac_addr = { }; static bool verbose = false; -static int test_fd[2]; static sd_ndisc *test_timeout_nd; -typedef int (*send_ra_t)(uint8_t flags); -static send_ra_t send_ra_function; - static void router_dump(sd_ndisc_router *rt) { struct in6_addr addr; uint8_t hop_limit; @@ -164,44 +160,6 @@ static void router_dump(sd_ndisc_router *rt) { } } -int icmp6_bind_router_solicitation(int ifindex) { - assert_se(ifindex == 42); - - if (socketpair(AF_UNIX, SOCK_DGRAM | SOCK_CLOEXEC | SOCK_NONBLOCK, 0, test_fd) < 0) - return -errno; - - return test_fd[0]; -} - -int icmp6_bind_router_advertisement(int ifindex) { - return -ENOSYS; -} - -static struct in6_addr dummy_link_local = { - .s6_addr = { - 0xfe, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x12, 0x34, 0x56, 0xff, 0xfe, 0x78, 0x9a, 0xbc, - }, -}; - -int icmp6_receive( - int fd, - void *iov_base, - size_t iov_len, - struct in6_addr *ret_sender, - triple_timestamp *ret_timestamp) { - - assert_se(read (fd, iov_base, iov_len) == (ssize_t)iov_len); - - if (ret_timestamp) - triple_timestamp_get(ret_timestamp); - - if (ret_sender) - *ret_sender = dummy_link_local; - - return 0; -} - static int send_ra(uint8_t flags) { uint8_t advertisement[] = { 0x86, 0x00, 0xde, 0x83, 0x40, 0xc0, 0x00, 0xb4, @@ -230,13 +188,6 @@ static int send_ra(uint8_t flags) { return 0; } -int icmp6_send_router_solicitation(int s, const struct ether_addr *ether_addr) { - if (!send_ra_function) - return 0; - - return send_ra_function(0); -} - static void test_callback(sd_ndisc *nd, sd_ndisc_event_t event, sd_ndisc_router *rt, void *userdata) { sd_event *e = userdata; static unsigned idx = 0;