]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/libsystemd-network/test-ipv4ll.c
io.systemd.Unit.List fix context/runtime split (#38172)
[thirdparty/systemd.git] / src / libsystemd-network / test-ipv4ll.c
CommitLineData
db9ecf05 1/* SPDX-License-Identifier: LGPL-2.1-or-later */
d9bf4f8c 2/***
810adae9 3 Copyright © 2014 Axis Communications AB. All rights reserved.
d9bf4f8c
UTL
4***/
5
8f815e8b 6#include <netinet/if_ether.h>
d9bf4f8c 7#include <stdio.h>
d9bf4f8c 8
5cdf13c7 9#include "sd-event.h"
d9bf4f8c 10#include "sd-ipv4ll.h"
07630cea 11
996d1697 12#include "arp-util.h"
3ffd4af2 13#include "fd-util.h"
5cdf13c7 14#include "in-addr-util.h"
6d7c4033 15#include "tests.h"
d9bf4f8c
UTL
16
17static bool verbose = false;
18static bool extended = false;
19static int test_fd[2];
20
21static int basic_request_handler_bind = 0;
22static int basic_request_handler_stop = 0;
e095f51d
LP
23static void* basic_request_handler_userdata = (void*) 0xCABCAB;
24
d9bf4f8c 25static void basic_request_handler(sd_ipv4ll *ll, int event, void *userdata) {
89ca10c6 26 assert_se(userdata == basic_request_handler_userdata);
d9bf4f8c 27
79893116 28 switch (event) {
be19c5b5 29 case SD_IPV4LL_EVENT_STOP:
d9bf4f8c
UTL
30 basic_request_handler_stop = 1;
31 break;
be19c5b5 32 case SD_IPV4LL_EVENT_BIND:
d9bf4f8c
UTL
33 basic_request_handler_bind = 1;
34 break;
35 default:
36 assert_se(0);
d9bf4f8c
UTL
37 }
38}
39
e1a3915b
YW
40int arp_send_packet(
41 int fd,
42 int ifindex,
ecad63f8 43 const struct in_addr *pa,
e1a3915b
YW
44 const struct ether_addr *ha,
45 bool announce) {
d9bf4f8c 46
996d1697 47 struct ether_arp ea = {};
d9bf4f8c 48
3bf47e73
ZJS
49 assert_se(fd >= 0);
50 assert_se(ifindex > 0);
ecad63f8 51 assert_se(pa);
3bf47e73 52 assert_se(ha);
d9bf4f8c 53
e1a3915b
YW
54 if (send(fd, &ea, sizeof(struct ether_arp), 0) < 0)
55 return -errno;
d9bf4f8c 56
e1a3915b 57 return 0;
d9bf4f8c
UTL
58}
59
d17ed573
YW
60int arp_update_filter(int fd, const struct in_addr *a, const struct ether_addr *eth_mac) {
61 return 0;
62}
63
ecad63f8 64int arp_network_bind_raw_socket(int ifindex, const struct in_addr *a, const struct ether_addr *eth_mac) {
3e29b889 65 if (socketpair(AF_UNIX, SOCK_DGRAM | SOCK_CLOEXEC | SOCK_NONBLOCK, 0, test_fd) < 0)
996d1697 66 return -errno;
d9bf4f8c 67
996d1697 68 return test_fd[0];
d9bf4f8c
UTL
69}
70
71static void test_public_api_setters(sd_event *e) {
129dc1b4 72 struct in_addr address = {};
38958cd6 73 uint64_t seed = 0;
d9bf4f8c
UTL
74 sd_ipv4ll *ll;
75 struct ether_addr mac_addr = {
76 .ether_addr_octet = {'A', 'B', 'C', '1', '2', '3'}};
77
78 if (verbose)
7fbae5b7 79 printf("* %s\n", __func__);
d9bf4f8c
UTL
80
81 assert_se(sd_ipv4ll_new(&ll) == 0);
82 assert_se(ll);
83
8161f608 84 ASSERT_RETURN_EXPECTED_SE(sd_ipv4ll_attach_event(NULL, NULL, 0) == -EINVAL);
d9bf4f8c 85 assert_se(sd_ipv4ll_attach_event(ll, e, 0) == 0);
8161f608 86 ASSERT_RETURN_EXPECTED_SE(sd_ipv4ll_attach_event(ll, e, 0) == -EBUSY);
d9bf4f8c 87
8161f608 88 ASSERT_RETURN_EXPECTED_SE(sd_ipv4ll_set_callback(NULL, NULL, NULL) == -EINVAL);
d9bf4f8c
UTL
89 assert_se(sd_ipv4ll_set_callback(ll, NULL, NULL) == 0);
90
8161f608 91 ASSERT_RETURN_EXPECTED_SE(sd_ipv4ll_set_address(ll, &address) == -EINVAL);
129dc1b4 92 address.s_addr |= htobe32(169U << 24 | 254U << 16);
8161f608 93 ASSERT_RETURN_EXPECTED_SE(sd_ipv4ll_set_address(ll, &address) == -EINVAL);
129dc1b4 94 address.s_addr |= htobe32(0x00FF);
8161f608 95 ASSERT_RETURN_EXPECTED_SE(sd_ipv4ll_set_address(ll, &address) == -EINVAL);
129dc1b4
TG
96 address.s_addr |= htobe32(0xF000);
97 assert_se(sd_ipv4ll_set_address(ll, &address) == 0);
98 address.s_addr |= htobe32(0x0F00);
8161f608 99 ASSERT_RETURN_EXPECTED_SE(sd_ipv4ll_set_address(ll, &address) == -EINVAL);
129dc1b4 100
8161f608 101 ASSERT_RETURN_EXPECTED_SE(sd_ipv4ll_set_address_seed(NULL, seed) == -EINVAL);
5625be76 102 assert_se(sd_ipv4ll_set_address_seed(ll, seed) == 0);
d9bf4f8c 103
8161f608
YW
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);
d9bf4f8c
UTL
107 assert_se(sd_ipv4ll_set_mac(ll, &mac_addr) == 0);
108
8161f608
YW
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);
2f8e7633 112 assert_se(sd_ipv4ll_set_ifindex(ll, 1) == 0);
d9bf4f8c
UTL
113
114 assert_se(sd_ipv4ll_ref(ll) == ll);
b45e4eb6 115 assert_se(sd_ipv4ll_unref(ll) == NULL);
d9bf4f8c
UTL
116
117 /* Cleanup */
118 assert_se(sd_ipv4ll_unref(ll) == NULL);
119}
120
59c27231 121static void test_basic_request(sd_event *e, const struct in_addr *start_address) {
d9bf4f8c
UTL
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)
7fbae5b7 129 printf("* %s\n", __func__);
d9bf4f8c
UTL
130
131 assert_se(sd_ipv4ll_new(&ll) == 0);
59c27231
AK
132 if (in4_addr_is_set(start_address))
133 assert_se(sd_ipv4ll_set_address(ll, start_address) >= 0);
8161f608 134 ASSERT_RETURN_EXPECTED_SE(sd_ipv4ll_start(ll) == -EINVAL);
d9bf4f8c
UTL
135
136 assert_se(sd_ipv4ll_attach_event(ll, e, 0) == 0);
8161f608 137 ASSERT_RETURN_EXPECTED_SE(sd_ipv4ll_start(ll) == -EINVAL);
d9bf4f8c
UTL
138
139 assert_se(sd_ipv4ll_set_mac(ll, &mac_addr) == 0);
8161f608 140 ASSERT_RETURN_EXPECTED_SE(sd_ipv4ll_start(ll) == -EINVAL);
d9bf4f8c
UTL
141
142 assert_se(sd_ipv4ll_set_callback(ll, basic_request_handler,
89ca10c6 143 basic_request_handler_userdata) == 0);
8161f608 144 ASSERT_RETURN_EXPECTED_SE(sd_ipv4ll_start(ll) == -EINVAL);
d9bf4f8c 145
2f8e7633 146 assert_se(sd_ipv4ll_set_ifindex(ll, 1) == 0);
6b8a1aa6 147 assert_se(sd_ipv4ll_start(ll) == 1);
d9bf4f8c 148
f5fbe71d 149 sd_event_run(e, UINT64_MAX);
6b8a1aa6 150 assert_se(sd_ipv4ll_start(ll) == 0);
d9bf4f8c 151
e3dca008
TG
152 assert_se(sd_ipv4ll_is_running(ll));
153
d9bf4f8c 154 /* PROBE */
f5fbe71d 155 sd_event_run(e, UINT64_MAX);
e095f51d 156 assert_se(recv(test_fd[1], &arp, sizeof(struct ether_arp), 0) == sizeof(struct ether_arp));
d9bf4f8c
UTL
157
158 if (extended) {
159 /* PROBE */
f5fbe71d 160 sd_event_run(e, UINT64_MAX);
e095f51d 161 assert_se(recv(test_fd[1], &arp, sizeof(struct ether_arp), 0) == sizeof(struct ether_arp));
d9bf4f8c
UTL
162
163 /* PROBE */
f5fbe71d 164 sd_event_run(e, UINT64_MAX);
e095f51d 165 assert_se(recv(test_fd[1], &arp, sizeof(struct ether_arp), 0) == sizeof(struct ether_arp));
d9bf4f8c 166
f5fbe71d 167 sd_event_run(e, UINT64_MAX);
d9bf4f8c 168 assert_se(basic_request_handler_bind == 1);
59c27231
AK
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 }
d9bf4f8c
UTL
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
186int main(int argc, char *argv[]) {
59c27231 187 struct in_addr start_address = {};
4afd3348 188 _cleanup_(sd_event_unrefp) sd_event *e = NULL;
d9bf4f8c 189
6d7c4033 190 test_setup_logging(LOG_DEBUG);
e3dca008 191
d9bf4f8c
UTL
192 assert_se(sd_event_new(&e) >= 0);
193
194 test_public_api_setters(e);
59c27231
AK
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);
d9bf4f8c
UTL
201
202 return 0;
203}