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