]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/libsystemd-network/test-ipv4ll.c
769abc2c14eb089346293af58a3b1db0b1c43e5a
[thirdparty/systemd.git] / src / libsystemd-network / test-ipv4ll.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2 /***
3 Copyright (C) 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 "util.h"
19
20 static bool verbose = false;
21 static bool extended = false;
22 static int test_fd[2];
23
24 static int basic_request_handler_bind = 0;
25 static int basic_request_handler_stop = 0;
26 static void* basic_request_handler_userdata = (void*) 0xCABCAB;
27
28 static void basic_request_handler(sd_ipv4ll *ll, int event, void *userdata) {
29 assert_se(userdata == basic_request_handler_userdata);
30
31 switch(event) {
32 case SD_IPV4LL_EVENT_STOP:
33 basic_request_handler_stop = 1;
34 break;
35 case SD_IPV4LL_EVENT_BIND:
36 basic_request_handler_bind = 1;
37 break;
38 default:
39 assert_se(0);
40 break;
41 }
42 }
43
44 static int arp_network_send_raw_socket(int fd, int ifindex,
45 const struct ether_arp *arp) {
46 assert_se(arp);
47 assert_se(ifindex > 0);
48 assert_se(fd >= 0);
49
50 if (send(fd, arp, sizeof(struct ether_arp), 0) < 0)
51 return -errno;
52
53 return 0;
54 }
55
56 int arp_send_probe(int fd, int ifindex,
57 be32_t pa, const struct ether_addr *ha) {
58 struct ether_arp ea = {};
59
60 assert_se(fd >= 0);
61 assert_se(ifindex > 0);
62 assert_se(pa != 0);
63 assert_se(ha);
64
65 return arp_network_send_raw_socket(fd, ifindex, &ea);
66 }
67
68 int arp_send_announcement(int fd, int ifindex,
69 be32_t pa, const struct ether_addr *ha) {
70 struct ether_arp ea = {};
71
72 assert_se(fd >= 0);
73 assert_se(ifindex > 0);
74 assert_se(pa != 0);
75 assert_se(ha);
76
77 return arp_network_send_raw_socket(fd, ifindex, &ea);
78 }
79
80 int arp_network_bind_raw_socket(int index, be32_t address, const struct ether_addr *eth_mac) {
81 if (socketpair(AF_UNIX, SOCK_DGRAM | SOCK_NONBLOCK, 0, test_fd) < 0)
82 return -errno;
83
84 return test_fd[0];
85 }
86
87 static void test_public_api_setters(sd_event *e) {
88 struct in_addr address = {};
89 uint64_t seed = 0;
90 sd_ipv4ll *ll;
91 struct ether_addr mac_addr = {
92 .ether_addr_octet = {'A', 'B', 'C', '1', '2', '3'}};
93
94 if (verbose)
95 printf("* %s\n", __FUNCTION__);
96
97 assert_se(sd_ipv4ll_new(&ll) == 0);
98 assert_se(ll);
99
100 assert_se(sd_ipv4ll_attach_event(NULL, NULL, 0) == -EINVAL);
101 assert_se(sd_ipv4ll_attach_event(ll, e, 0) == 0);
102 assert_se(sd_ipv4ll_attach_event(ll, e, 0) == -EBUSY);
103
104 assert_se(sd_ipv4ll_set_callback(NULL, NULL, NULL) == -EINVAL);
105 assert_se(sd_ipv4ll_set_callback(ll, NULL, NULL) == 0);
106
107 assert_se(sd_ipv4ll_set_address(ll, &address) == -EINVAL);
108 address.s_addr |= htobe32(169U << 24 | 254U << 16);
109 assert_se(sd_ipv4ll_set_address(ll, &address) == -EINVAL);
110 address.s_addr |= htobe32(0x00FF);
111 assert_se(sd_ipv4ll_set_address(ll, &address) == -EINVAL);
112 address.s_addr |= htobe32(0xF000);
113 assert_se(sd_ipv4ll_set_address(ll, &address) == 0);
114 address.s_addr |= htobe32(0x0F00);
115 assert_se(sd_ipv4ll_set_address(ll, &address) == -EINVAL);
116
117 assert_se(sd_ipv4ll_set_address_seed(NULL, seed) == -EINVAL);
118 assert_se(sd_ipv4ll_set_address_seed(ll, seed) == 0);
119
120 assert_se(sd_ipv4ll_set_mac(NULL, NULL) == -EINVAL);
121 assert_se(sd_ipv4ll_set_mac(ll, NULL) == -EINVAL);
122 assert_se(sd_ipv4ll_set_mac(ll, &mac_addr) == 0);
123
124 assert_se(sd_ipv4ll_set_ifindex(NULL, -1) == -EINVAL);
125 assert_se(sd_ipv4ll_set_ifindex(ll, -1) == -EINVAL);
126 assert_se(sd_ipv4ll_set_ifindex(ll, -99) == -EINVAL);
127 assert_se(sd_ipv4ll_set_ifindex(ll, 1) == 0);
128 assert_se(sd_ipv4ll_set_ifindex(ll, 99) == 0);
129
130 assert_se(sd_ipv4ll_ref(ll) == ll);
131 assert_se(sd_ipv4ll_unref(ll) == NULL);
132
133 /* Cleanup */
134 assert_se(sd_ipv4ll_unref(ll) == NULL);
135 }
136
137 static 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,
157 basic_request_handler_userdata) == 0);
158 assert_se(sd_ipv4ll_start(ll) == -EINVAL);
159
160 assert_se(sd_ipv4ll_set_ifindex(ll, 1) == 0);
161 assert_se(sd_ipv4ll_start(ll) == 0);
162
163 sd_event_run(e, (uint64_t) -1);
164 assert_se(sd_ipv4ll_start(ll) == -EBUSY);
165
166 assert_se(sd_ipv4ll_is_running(ll));
167
168 /* PROBE */
169 sd_event_run(e, (uint64_t) -1);
170 assert_se(recv(test_fd[1], &arp, sizeof(struct ether_arp), 0) == sizeof(struct ether_arp));
171
172 if (extended) {
173 /* PROBE */
174 sd_event_run(e, (uint64_t) -1);
175 assert_se(recv(test_fd[1], &arp, sizeof(struct ether_arp), 0) == sizeof(struct ether_arp));
176
177 /* PROBE */
178 sd_event_run(e, (uint64_t) -1);
179 assert_se(recv(test_fd[1], &arp, sizeof(struct ether_arp), 0) == sizeof(struct ether_arp));
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
193 int main(int argc, char *argv[]) {
194 _cleanup_(sd_event_unrefp) sd_event *e = NULL;
195
196 log_set_max_level(LOG_DEBUG);
197 log_parse_environment();
198 log_open();
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 }