]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/libsystemd-network/test-ipv4ll.c
Merge pull request #5930 from larskarlitski/journal-skip
[thirdparty/systemd.git] / src / libsystemd-network / test-ipv4ll.c
1 /***
2 This file is part of systemd.
3
4 Copyright (C) 2014 Axis Communications AB. All rights reserved.
5
6 systemd is free software; you can redistribute it and/or modify it
7 under the terms of the GNU Lesser General Public License as published by
8 the Free Software Foundation; either version 2.1 of the License, or
9 (at your option) any later version.
10
11 systemd is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
15
16 You should have received a copy of the GNU Lesser General Public License
17 along with systemd; If not, see <http://www.gnu.org/licenses/>.
18 ***/
19
20 #include <errno.h>
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <sys/socket.h>
24 #include <sys/types.h>
25 #include <unistd.h>
26
27 #include "sd-ipv4ll.h"
28
29 #include "arp-util.h"
30 #include "fd-util.h"
31 #include "socket-util.h"
32 #include "util.h"
33
34 static bool verbose = false;
35 static bool extended = false;
36 static int test_fd[2];
37
38 static int basic_request_handler_bind = 0;
39 static int basic_request_handler_stop = 0;
40 static void* basic_request_handler_userdata = (void*) 0xCABCAB;
41
42 static void basic_request_handler(sd_ipv4ll *ll, int event, void *userdata) {
43 assert_se(userdata == basic_request_handler_userdata);
44
45 switch(event) {
46 case SD_IPV4LL_EVENT_STOP:
47 basic_request_handler_stop = 1;
48 break;
49 case SD_IPV4LL_EVENT_BIND:
50 basic_request_handler_bind = 1;
51 break;
52 default:
53 assert_se(0);
54 break;
55 }
56 }
57
58 static int arp_network_send_raw_socket(int fd, int ifindex,
59 const struct ether_arp *arp) {
60 assert_se(arp);
61 assert_se(ifindex > 0);
62 assert_se(fd >= 0);
63
64 if (send(fd, arp, sizeof(struct ether_arp), 0) < 0)
65 return -errno;
66
67 return 0;
68 }
69
70 int arp_send_probe(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_send_announcement(int fd, int ifindex,
83 be32_t pa, const struct ether_addr *ha) {
84 struct ether_arp ea = {};
85
86 assert_se(fd >= 0);
87 assert_se(ifindex > 0);
88 assert_se(pa != 0);
89 assert_se(ha);
90
91 return arp_network_send_raw_socket(fd, ifindex, &ea);
92 }
93
94 int arp_network_bind_raw_socket(int index, be32_t address, const struct ether_addr *eth_mac) {
95 if (socketpair(AF_UNIX, SOCK_DGRAM | SOCK_NONBLOCK, 0, test_fd) < 0)
96 return -errno;
97
98 return test_fd[0];
99 }
100
101 static void test_public_api_setters(sd_event *e) {
102 struct in_addr address = {};
103 uint64_t seed = 0;
104 sd_ipv4ll *ll;
105 struct ether_addr mac_addr = {
106 .ether_addr_octet = {'A', 'B', 'C', '1', '2', '3'}};
107
108 if (verbose)
109 printf("* %s\n", __FUNCTION__);
110
111 assert_se(sd_ipv4ll_new(&ll) == 0);
112 assert_se(ll);
113
114 assert_se(sd_ipv4ll_attach_event(NULL, NULL, 0) == -EINVAL);
115 assert_se(sd_ipv4ll_attach_event(ll, e, 0) == 0);
116 assert_se(sd_ipv4ll_attach_event(ll, e, 0) == -EBUSY);
117
118 assert_se(sd_ipv4ll_set_callback(NULL, NULL, NULL) == -EINVAL);
119 assert_se(sd_ipv4ll_set_callback(ll, NULL, NULL) == 0);
120
121 assert_se(sd_ipv4ll_set_address(ll, &address) == -EINVAL);
122 address.s_addr |= htobe32(169U << 24 | 254U << 16);
123 assert_se(sd_ipv4ll_set_address(ll, &address) == -EINVAL);
124 address.s_addr |= htobe32(0x00FF);
125 assert_se(sd_ipv4ll_set_address(ll, &address) == -EINVAL);
126 address.s_addr |= htobe32(0xF000);
127 assert_se(sd_ipv4ll_set_address(ll, &address) == 0);
128 address.s_addr |= htobe32(0x0F00);
129 assert_se(sd_ipv4ll_set_address(ll, &address) == -EINVAL);
130
131 assert_se(sd_ipv4ll_set_address_seed(NULL, seed) == -EINVAL);
132 assert_se(sd_ipv4ll_set_address_seed(ll, seed) == 0);
133
134 assert_se(sd_ipv4ll_set_mac(NULL, NULL) == -EINVAL);
135 assert_se(sd_ipv4ll_set_mac(ll, NULL) == -EINVAL);
136 assert_se(sd_ipv4ll_set_mac(ll, &mac_addr) == 0);
137
138 assert_se(sd_ipv4ll_set_ifindex(NULL, -1) == -EINVAL);
139 assert_se(sd_ipv4ll_set_ifindex(ll, -1) == -EINVAL);
140 assert_se(sd_ipv4ll_set_ifindex(ll, -99) == -EINVAL);
141 assert_se(sd_ipv4ll_set_ifindex(ll, 1) == 0);
142 assert_se(sd_ipv4ll_set_ifindex(ll, 99) == 0);
143
144 assert_se(sd_ipv4ll_ref(ll) == ll);
145 assert_se(sd_ipv4ll_unref(ll) == NULL);
146
147 /* Cleanup */
148 assert_se(sd_ipv4ll_unref(ll) == NULL);
149 }
150
151 static void test_basic_request(sd_event *e) {
152
153 sd_ipv4ll *ll;
154 struct ether_arp arp;
155 struct ether_addr mac_addr = {
156 .ether_addr_octet = {'A', 'B', 'C', '1', '2', '3'}};
157
158 if (verbose)
159 printf("* %s\n", __FUNCTION__);
160
161 assert_se(sd_ipv4ll_new(&ll) == 0);
162 assert_se(sd_ipv4ll_start(ll) == -EINVAL);
163
164 assert_se(sd_ipv4ll_attach_event(ll, e, 0) == 0);
165 assert_se(sd_ipv4ll_start(ll) == -EINVAL);
166
167 assert_se(sd_ipv4ll_set_mac(ll, &mac_addr) == 0);
168 assert_se(sd_ipv4ll_start(ll) == -EINVAL);
169
170 assert_se(sd_ipv4ll_set_callback(ll, basic_request_handler,
171 basic_request_handler_userdata) == 0);
172 assert_se(sd_ipv4ll_start(ll) == -EINVAL);
173
174 assert_se(sd_ipv4ll_set_ifindex(ll, 1) == 0);
175 assert_se(sd_ipv4ll_start(ll) == 0);
176
177 sd_event_run(e, (uint64_t) -1);
178 assert_se(sd_ipv4ll_start(ll) == -EBUSY);
179
180 assert_se(sd_ipv4ll_is_running(ll));
181
182 /* PROBE */
183 sd_event_run(e, (uint64_t) -1);
184 assert_se(recv(test_fd[1], &arp, sizeof(struct ether_arp), 0) == sizeof(struct ether_arp));
185
186 if (extended) {
187 /* PROBE */
188 sd_event_run(e, (uint64_t) -1);
189 assert_se(recv(test_fd[1], &arp, sizeof(struct ether_arp), 0) == sizeof(struct ether_arp));
190
191 /* PROBE */
192 sd_event_run(e, (uint64_t) -1);
193 assert_se(recv(test_fd[1], &arp, sizeof(struct ether_arp), 0) == sizeof(struct ether_arp));
194
195 sd_event_run(e, (uint64_t) -1);
196 assert_se(basic_request_handler_bind == 1);
197 }
198
199 sd_ipv4ll_stop(ll);
200 assert_se(basic_request_handler_stop == 1);
201
202 /* Cleanup */
203 assert_se(sd_ipv4ll_unref(ll) == NULL);
204 safe_close(test_fd[1]);
205 }
206
207 int main(int argc, char *argv[]) {
208 _cleanup_(sd_event_unrefp) sd_event *e = NULL;
209
210 log_set_max_level(LOG_DEBUG);
211 log_parse_environment();
212 log_open();
213
214 assert_se(sd_event_new(&e) >= 0);
215
216 test_public_api_setters(e);
217 test_basic_request(e);
218
219 return 0;
220 }