]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/libsystemd-network/test-ipv4ll.c
sd-ipv4acd,sd-ipv4ll: introduce _get_ifindex() and _get_ifname()
[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
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);
d9bf4f8c
UTL
129
130 assert_se(sd_ipv4ll_ref(ll) == ll);
b45e4eb6 131 assert_se(sd_ipv4ll_unref(ll) == NULL);
d9bf4f8c
UTL
132
133 /* Cleanup */
134 assert_se(sd_ipv4ll_unref(ll) == NULL);
135}
136
137static void test_basic_request(sd_event *e) {
138
139 sd_ipv4ll *ll;
140 struct ether_arp arp;
141 struct ether_addr mac_addr = {
142 .ether_addr_octet = {'A', 'B', 'C', '1', '2', '3'}};
143
144 if (verbose)
145 printf("* %s\n", __FUNCTION__);
146
147 assert_se(sd_ipv4ll_new(&ll) == 0);
148 assert_se(sd_ipv4ll_start(ll) == -EINVAL);
149
150 assert_se(sd_ipv4ll_attach_event(ll, e, 0) == 0);
151 assert_se(sd_ipv4ll_start(ll) == -EINVAL);
152
153 assert_se(sd_ipv4ll_set_mac(ll, &mac_addr) == 0);
154 assert_se(sd_ipv4ll_start(ll) == -EINVAL);
155
156 assert_se(sd_ipv4ll_set_callback(ll, basic_request_handler,
89ca10c6 157 basic_request_handler_userdata) == 0);
d9bf4f8c
UTL
158 assert_se(sd_ipv4ll_start(ll) == -EINVAL);
159
2f8e7633 160 assert_se(sd_ipv4ll_set_ifindex(ll, 1) == 0);
6b8a1aa6 161 assert_se(sd_ipv4ll_start(ll) == 1);
d9bf4f8c
UTL
162
163 sd_event_run(e, (uint64_t) -1);
6b8a1aa6 164 assert_se(sd_ipv4ll_start(ll) == 0);
d9bf4f8c 165
e3dca008
TG
166 assert_se(sd_ipv4ll_is_running(ll));
167
d9bf4f8c
UTL
168 /* PROBE */
169 sd_event_run(e, (uint64_t) -1);
e095f51d 170 assert_se(recv(test_fd[1], &arp, sizeof(struct ether_arp), 0) == sizeof(struct ether_arp));
d9bf4f8c
UTL
171
172 if (extended) {
173 /* PROBE */
174 sd_event_run(e, (uint64_t) -1);
e095f51d 175 assert_se(recv(test_fd[1], &arp, sizeof(struct ether_arp), 0) == sizeof(struct ether_arp));
d9bf4f8c
UTL
176
177 /* PROBE */
178 sd_event_run(e, (uint64_t) -1);
e095f51d 179 assert_se(recv(test_fd[1], &arp, sizeof(struct ether_arp), 0) == sizeof(struct ether_arp));
d9bf4f8c
UTL
180
181 sd_event_run(e, (uint64_t) -1);
182 assert_se(basic_request_handler_bind == 1);
183 }
184
185 sd_ipv4ll_stop(ll);
186 assert_se(basic_request_handler_stop == 1);
187
188 /* Cleanup */
189 assert_se(sd_ipv4ll_unref(ll) == NULL);
190 safe_close(test_fd[1]);
191}
192
193int main(int argc, char *argv[]) {
4afd3348 194 _cleanup_(sd_event_unrefp) sd_event *e = NULL;
d9bf4f8c 195
6d7c4033 196 test_setup_logging(LOG_DEBUG);
e3dca008 197
d9bf4f8c
UTL
198 assert_se(sd_event_new(&e) >= 0);
199
200 test_public_api_setters(e);
d9bf4f8c
UTL
201 test_basic_request(e);
202
203 return 0;
204}