| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
/*** |
| 3 |
Copyright © 2014 Axis Communications AB. All rights reserved. |
| 4 |
***/ |
| 5 |
|
| 6 |
#include <netinet/if_ether.h> |
| 7 |
#include <stdio.h> |
| 8 |
|
| 9 |
#include "sd-event.h" |
| 10 |
#include "sd-ipv4ll.h" |
| 11 |
|
| 12 |
#include "arp-util.h" |
| 13 |
#include "fd-util.h" |
| 14 |
#include "in-addr-util.h" |
| 15 |
#include "tests.h" |
| 16 |
|
| 17 |
static bool verbose = false; |
| 18 |
static bool extended = false; |
| 19 |
static int test_fd[2]; |
| 20 |
|
| 21 |
static int basic_request_handler_bind = 0; |
| 22 |
static int basic_request_handler_stop = 0; |
| 23 |
static void* basic_request_handler_userdata = (void*) 0xCABCAB; |
| 24 |
|
| 25 |
static void basic_request_handler(sd_ipv4ll *ll, int event, void *userdata) { |
| 26 |
assert_se(userdata == basic_request_handler_userdata); |
| 27 |
|
| 28 |
switch (event) { |
| 29 |
case SD_IPV4LL_EVENT_STOP: |
| 30 |
basic_request_handler_stop = 1; |
| 31 |
break; |
| 32 |
case SD_IPV4LL_EVENT_BIND: |
| 33 |
basic_request_handler_bind = 1; |
| 34 |
break; |
| 35 |
default: |
| 36 |
assert_se(0); |
| 37 |
} |
| 38 |
} |
| 39 |
|
| 40 |
int arp_send_packet( |
| 41 |
int fd, |
| 42 |
int ifindex, |
| 43 |
const struct in_addr *pa, |
| 44 |
const struct ether_addr *ha, |
| 45 |
bool announce) { |
| 46 |
|
| 47 |
struct ether_arp ea = {}; |
| 48 |
|
| 49 |
assert_se(fd >= 0); |
| 50 |
assert_se(ifindex > 0); |
| 51 |
assert_se(pa); |
| 52 |
assert_se(ha); |
| 53 |
|
| 54 |
if (send(fd, &ea, sizeof(struct ether_arp), 0) < 0) |
| 55 |
return -errno; |
| 56 |
|
| 57 |
return 0; |
| 58 |
} |
| 59 |
|
| 60 |
int arp_update_filter(int fd, const struct in_addr *a, const struct ether_addr *eth_mac) { |
| 61 |
return 0; |
| 62 |
} |
| 63 |
|
| 64 |
int arp_network_bind_raw_socket(int ifindex, const struct in_addr *a, const struct ether_addr *eth_mac) { |
| 65 |
if (socketpair(AF_UNIX, SOCK_DGRAM | SOCK_CLOEXEC | SOCK_NONBLOCK, 0, test_fd) < 0) |
| 66 |
return -errno; |
| 67 |
|
| 68 |
return test_fd[0]; |
| 69 |
} |
| 70 |
|
| 71 |
static void test_public_api_setters(sd_event *e) { |
| 72 |
struct in_addr address = {}; |
| 73 |
uint64_t seed = 0; |
| 74 |
sd_ipv4ll *ll; |
| 75 |
struct ether_addr mac_addr = { |
| 76 |
.ether_addr_octet = {'A', 'B', 'C', '1', '2', '3'}}; |
| 77 |
|
| 78 |
if (verbose) |
| 79 |
printf("* %s\n", __func__); |
| 80 |
|
| 81 |
assert_se(sd_ipv4ll_new(&ll) == 0); |
| 82 |
assert_se(ll); |
| 83 |
|
| 84 |
ASSERT_RETURN_EXPECTED_SE(sd_ipv4ll_attach_event(NULL, NULL, 0) == -EINVAL); |
| 85 |
assert_se(sd_ipv4ll_attach_event(ll, e, 0) == 0); |
| 86 |
ASSERT_RETURN_EXPECTED_SE(sd_ipv4ll_attach_event(ll, e, 0) == -EBUSY); |
| 87 |
|
| 88 |
ASSERT_RETURN_EXPECTED_SE(sd_ipv4ll_set_callback(NULL, NULL, NULL) == -EINVAL); |
| 89 |
assert_se(sd_ipv4ll_set_callback(ll, NULL, NULL) == 0); |
| 90 |
|
| 91 |
ASSERT_RETURN_EXPECTED_SE(sd_ipv4ll_set_address(ll, &address) == -EINVAL); |
| 92 |
address.s_addr |= htobe32(169U << 24 | 254U << 16); |
| 93 |
ASSERT_RETURN_EXPECTED_SE(sd_ipv4ll_set_address(ll, &address) == -EINVAL); |
| 94 |
address.s_addr |= htobe32(0x00FF); |
| 95 |
ASSERT_RETURN_EXPECTED_SE(sd_ipv4ll_set_address(ll, &address) == -EINVAL); |
| 96 |
address.s_addr |= htobe32(0xF000); |
| 97 |
assert_se(sd_ipv4ll_set_address(ll, &address) == 0); |
| 98 |
address.s_addr |= htobe32(0x0F00); |
| 99 |
ASSERT_RETURN_EXPECTED_SE(sd_ipv4ll_set_address(ll, &address) == -EINVAL); |
| 100 |
|
| 101 |
ASSERT_RETURN_EXPECTED_SE(sd_ipv4ll_set_address_seed(NULL, seed) == -EINVAL); |
| 102 |
assert_se(sd_ipv4ll_set_address_seed(ll, seed) == 0); |
| 103 |
|
| 104 |
ASSERT_RETURN_EXPECTED_SE(sd_ipv4ll_set_mac(NULL, NULL) == -EINVAL); |
| 105 |
|
| 106 |
ASSERT_RETURN_EXPECTED_SE(sd_ipv4ll_set_mac(ll, NULL) == -EINVAL); |
| 107 |
assert_se(sd_ipv4ll_set_mac(ll, &mac_addr) == 0); |
| 108 |
|
| 109 |
ASSERT_RETURN_EXPECTED_SE(sd_ipv4ll_set_ifindex(NULL, -1) == -EINVAL); |
| 110 |
ASSERT_RETURN_EXPECTED_SE(sd_ipv4ll_set_ifindex(ll, -1) == -EINVAL); |
| 111 |
ASSERT_RETURN_EXPECTED_SE(sd_ipv4ll_set_ifindex(ll, -99) == -EINVAL); |
| 112 |
assert_se(sd_ipv4ll_set_ifindex(ll, 1) == 0); |
| 113 |
|
| 114 |
assert_se(sd_ipv4ll_ref(ll) == ll); |
| 115 |
assert_se(sd_ipv4ll_unref(ll) == NULL); |
| 116 |
|
| 117 |
/* Cleanup */ |
| 118 |
assert_se(sd_ipv4ll_unref(ll) == NULL); |
| 119 |
} |
| 120 |
|
| 121 |
static void test_basic_request(sd_event *e, const struct in_addr *start_address) { |
| 122 |
|
| 123 |
sd_ipv4ll *ll; |
| 124 |
struct ether_arp arp; |
| 125 |
struct ether_addr mac_addr = { |
| 126 |
.ether_addr_octet = {'A', 'B', 'C', '1', '2', '3'}}; |
| 127 |
|
| 128 |
if (verbose) |
| 129 |
printf("* %s\n", __func__); |
| 130 |
|
| 131 |
assert_se(sd_ipv4ll_new(&ll) == 0); |
| 132 |
if (in4_addr_is_set(start_address)) |
| 133 |
assert_se(sd_ipv4ll_set_address(ll, start_address) >= 0); |
| 134 |
ASSERT_RETURN_EXPECTED_SE(sd_ipv4ll_start(ll) == -EINVAL); |
| 135 |
|
| 136 |
assert_se(sd_ipv4ll_attach_event(ll, e, 0) == 0); |
| 137 |
ASSERT_RETURN_EXPECTED_SE(sd_ipv4ll_start(ll) == -EINVAL); |
| 138 |
|
| 139 |
assert_se(sd_ipv4ll_set_mac(ll, &mac_addr) == 0); |
| 140 |
ASSERT_RETURN_EXPECTED_SE(sd_ipv4ll_start(ll) == -EINVAL); |
| 141 |
|
| 142 |
assert_se(sd_ipv4ll_set_callback(ll, basic_request_handler, |
| 143 |
basic_request_handler_userdata) == 0); |
| 144 |
ASSERT_RETURN_EXPECTED_SE(sd_ipv4ll_start(ll) == -EINVAL); |
| 145 |
|
| 146 |
assert_se(sd_ipv4ll_set_ifindex(ll, 1) == 0); |
| 147 |
assert_se(sd_ipv4ll_start(ll) == 1); |
| 148 |
|
| 149 |
sd_event_run(e, UINT64_MAX); |
| 150 |
assert_se(sd_ipv4ll_start(ll) == 0); |
| 151 |
|
| 152 |
assert_se(sd_ipv4ll_is_running(ll)); |
| 153 |
|
| 154 |
/* PROBE */ |
| 155 |
sd_event_run(e, UINT64_MAX); |
| 156 |
assert_se(recv(test_fd[1], &arp, sizeof(struct ether_arp), 0) == sizeof(struct ether_arp)); |
| 157 |
|
| 158 |
if (extended) { |
| 159 |
/* PROBE */ |
| 160 |
sd_event_run(e, UINT64_MAX); |
| 161 |
assert_se(recv(test_fd[1], &arp, sizeof(struct ether_arp), 0) == sizeof(struct ether_arp)); |
| 162 |
|
| 163 |
/* PROBE */ |
| 164 |
sd_event_run(e, UINT64_MAX); |
| 165 |
assert_se(recv(test_fd[1], &arp, sizeof(struct ether_arp), 0) == sizeof(struct ether_arp)); |
| 166 |
|
| 167 |
sd_event_run(e, UINT64_MAX); |
| 168 |
assert_se(basic_request_handler_bind == 1); |
| 169 |
|
| 170 |
if (in4_addr_is_set(start_address)) { |
| 171 |
struct in_addr address; |
| 172 |
|
| 173 |
assert_se(sd_ipv4ll_get_address(ll, &address) >= 0); |
| 174 |
assert_se(start_address->s_addr == address.s_addr); |
| 175 |
} |
| 176 |
} |
| 177 |
|
| 178 |
sd_ipv4ll_stop(ll); |
| 179 |
assert_se(basic_request_handler_stop == 1); |
| 180 |
|
| 181 |
/* Cleanup */ |
| 182 |
assert_se(sd_ipv4ll_unref(ll) == NULL); |
| 183 |
safe_close(test_fd[1]); |
| 184 |
} |
| 185 |
|
| 186 |
int main(int argc, char *argv[]) { |
| 187 |
struct in_addr start_address = {}; |
| 188 |
_cleanup_(sd_event_unrefp) sd_event *e = NULL; |
| 189 |
|
| 190 |
test_setup_logging(LOG_DEBUG); |
| 191 |
|
| 192 |
assert_se(sd_event_new(&e) >= 0); |
| 193 |
|
| 194 |
test_public_api_setters(e); |
| 195 |
test_basic_request(e, &start_address); |
| 196 |
|
| 197 |
basic_request_handler_bind = 0; |
| 198 |
basic_request_handler_stop = 0; |
| 199 |
start_address.s_addr = htobe32(169U << 24 | 254U << 16 | 1U << 8 | 2U); |
| 200 |
test_basic_request(e, &start_address); |
| 201 |
|
| 202 |
return 0; |
| 203 |
} |