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