]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/libsystemd-network/test-ipv4ll.c
man: fix markup and grammar for FOU{Source,Destination}Port=
[thirdparty/systemd.git] / src / libsystemd-network / test-ipv4ll.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2 /***
3 Copyright © 2014 Axis Communications AB. All rights reserved.
4 ***/
5
6 #include <errno.h>
7 #include <stdio.h>
8 #include <stdlib.h>
9 #include <sys/socket.h>
10 #include <sys/types.h>
11 #include <unistd.h>
12
13 #include "sd-ipv4ll.h"
14
15 #include "arp-util.h"
16 #include "fd-util.h"
17 #include "socket-util.h"
18 #include "tests.h"
19 #include "util.h"
20
21 static bool verbose = false;
22 static bool extended = false;
23 static int test_fd[2];
24
25 static int basic_request_handler_bind = 0;
26 static int basic_request_handler_stop = 0;
27 static void* basic_request_handler_userdata = (void*) 0xCABCAB;
28
29 static void basic_request_handler(sd_ipv4ll *ll, int event, void *userdata) {
30 assert_se(userdata == basic_request_handler_userdata);
31
32 switch(event) {
33 case SD_IPV4LL_EVENT_STOP:
34 basic_request_handler_stop = 1;
35 break;
36 case SD_IPV4LL_EVENT_BIND:
37 basic_request_handler_bind = 1;
38 break;
39 default:
40 assert_se(0);
41 break;
42 }
43 }
44
45 static int arp_network_send_raw_socket(int fd, int ifindex,
46 const struct ether_arp *arp) {
47 assert_se(arp);
48 assert_se(ifindex > 0);
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
57 int arp_send_probe(int fd, int ifindex,
58 be32_t pa, const struct ether_addr *ha) {
59 struct ether_arp ea = {};
60
61 assert_se(fd >= 0);
62 assert_se(ifindex > 0);
63 assert_se(pa != 0);
64 assert_se(ha);
65
66 return arp_network_send_raw_socket(fd, ifindex, &ea);
67 }
68
69 int arp_send_announcement(int fd, int ifindex,
70 be32_t pa, const struct ether_addr *ha) {
71 struct ether_arp ea = {};
72
73 assert_se(fd >= 0);
74 assert_se(ifindex > 0);
75 assert_se(pa != 0);
76 assert_se(ha);
77
78 return arp_network_send_raw_socket(fd, ifindex, &ea);
79 }
80
81 int arp_network_bind_raw_socket(int index, be32_t address, const struct ether_addr *eth_mac) {
82 if (socketpair(AF_UNIX, SOCK_DGRAM | SOCK_CLOEXEC | SOCK_NONBLOCK, 0, test_fd) < 0)
83 return -errno;
84
85 return test_fd[0];
86 }
87
88 static void test_public_api_setters(sd_event *e) {
89 struct in_addr address = {};
90 uint64_t seed = 0;
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
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
118 assert_se(sd_ipv4ll_set_address_seed(NULL, seed) == -EINVAL);
119 assert_se(sd_ipv4ll_set_address_seed(ll, seed) == 0);
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
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);
130
131 assert_se(sd_ipv4ll_ref(ll) == ll);
132 assert_se(sd_ipv4ll_unref(ll) == NULL);
133
134 /* Cleanup */
135 assert_se(sd_ipv4ll_unref(ll) == NULL);
136 }
137
138 static 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,
158 basic_request_handler_userdata) == 0);
159 assert_se(sd_ipv4ll_start(ll) == -EINVAL);
160
161 assert_se(sd_ipv4ll_set_ifindex(ll, 1) == 0);
162 assert_se(sd_ipv4ll_start(ll) == 0);
163
164 sd_event_run(e, (uint64_t) -1);
165 assert_se(sd_ipv4ll_start(ll) == -EBUSY);
166
167 assert_se(sd_ipv4ll_is_running(ll));
168
169 /* PROBE */
170 sd_event_run(e, (uint64_t) -1);
171 assert_se(recv(test_fd[1], &arp, sizeof(struct ether_arp), 0) == sizeof(struct ether_arp));
172
173 if (extended) {
174 /* PROBE */
175 sd_event_run(e, (uint64_t) -1);
176 assert_se(recv(test_fd[1], &arp, sizeof(struct ether_arp), 0) == sizeof(struct ether_arp));
177
178 /* PROBE */
179 sd_event_run(e, (uint64_t) -1);
180 assert_se(recv(test_fd[1], &arp, sizeof(struct ether_arp), 0) == sizeof(struct ether_arp));
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
194 int main(int argc, char *argv[]) {
195 _cleanup_(sd_event_unrefp) sd_event *e = NULL;
196
197 test_setup_logging(LOG_DEBUG);
198
199 assert_se(sd_event_new(&e) >= 0);
200
201 test_public_api_setters(e);
202 test_basic_request(e);
203
204 return 0;
205 }