]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/libsystemd-network/test-ipv4ll.c
Merge pull request #7388 from keszybz/doc-tweak
[thirdparty/systemd.git] / src / libsystemd-network / test-ipv4ll.c
CommitLineData
d9bf4f8c
UTL
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
d9bf4f8c
UTL
20#include <errno.h>
21#include <stdio.h>
07630cea 22#include <stdlib.h>
d9bf4f8c 23#include <sys/socket.h>
07630cea 24#include <sys/types.h>
d9bf4f8c
UTL
25#include <unistd.h>
26
d9bf4f8c 27#include "sd-ipv4ll.h"
07630cea 28
996d1697 29#include "arp-util.h"
3ffd4af2 30#include "fd-util.h"
07630cea
LP
31#include "socket-util.h"
32#include "util.h"
d9bf4f8c
UTL
33
34static bool verbose = false;
35static bool extended = false;
36static int test_fd[2];
37
38static int basic_request_handler_bind = 0;
39static int basic_request_handler_stop = 0;
e095f51d
LP
40static void* basic_request_handler_userdata = (void*) 0xCABCAB;
41
d9bf4f8c 42static void basic_request_handler(sd_ipv4ll *ll, int event, void *userdata) {
89ca10c6 43 assert_se(userdata == basic_request_handler_userdata);
d9bf4f8c
UTL
44
45 switch(event) {
be19c5b5 46 case SD_IPV4LL_EVENT_STOP:
d9bf4f8c
UTL
47 basic_request_handler_stop = 1;
48 break;
be19c5b5 49 case SD_IPV4LL_EVENT_BIND:
d9bf4f8c
UTL
50 basic_request_handler_bind = 1;
51 break;
52 default:
53 assert_se(0);
54 break;
55 }
56}
57
996d1697
TG
58static int arp_network_send_raw_socket(int fd, int ifindex,
59 const struct ether_arp *arp) {
d9bf4f8c 60 assert_se(arp);
996d1697 61 assert_se(ifindex > 0);
d9bf4f8c
UTL
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
996d1697
TG
70int arp_send_probe(int fd, int ifindex,
71 be32_t pa, const struct ether_addr *ha) {
72 struct ether_arp ea = {};
d9bf4f8c 73
3bf47e73
ZJS
74 assert_se(fd >= 0);
75 assert_se(ifindex > 0);
76 assert_se(pa != 0);
77 assert_se(ha);
d9bf4f8c 78
996d1697 79 return arp_network_send_raw_socket(fd, ifindex, &ea);
d9bf4f8c
UTL
80}
81
996d1697
TG
82int arp_send_announcement(int fd, int ifindex,
83 be32_t pa, const struct ether_addr *ha) {
84 struct ether_arp ea = {};
d9bf4f8c 85
3bf47e73
ZJS
86 assert_se(fd >= 0);
87 assert_se(ifindex > 0);
88 assert_se(pa != 0);
89 assert_se(ha);
d9bf4f8c 90
996d1697 91 return arp_network_send_raw_socket(fd, ifindex, &ea);
d9bf4f8c
UTL
92}
93
996d1697
TG
94int 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;
d9bf4f8c 97
996d1697 98 return test_fd[0];
d9bf4f8c
UTL
99}
100
101static void test_public_api_setters(sd_event *e) {
129dc1b4 102 struct in_addr address = {};
38958cd6 103 uint64_t seed = 0;
d9bf4f8c
UTL
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
129dc1b4
TG
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
5625be76
RC
131 assert_se(sd_ipv4ll_set_address_seed(NULL, seed) == -EINVAL);
132 assert_se(sd_ipv4ll_set_address_seed(ll, seed) == 0);
d9bf4f8c
UTL
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
2f8e7633
LP
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);
d9bf4f8c
UTL
143
144 assert_se(sd_ipv4ll_ref(ll) == ll);
b45e4eb6 145 assert_se(sd_ipv4ll_unref(ll) == NULL);
d9bf4f8c
UTL
146
147 /* Cleanup */
148 assert_se(sd_ipv4ll_unref(ll) == NULL);
149}
150
151static 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,
89ca10c6 171 basic_request_handler_userdata) == 0);
d9bf4f8c
UTL
172 assert_se(sd_ipv4ll_start(ll) == -EINVAL);
173
2f8e7633 174 assert_se(sd_ipv4ll_set_ifindex(ll, 1) == 0);
d9bf4f8c
UTL
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
e3dca008
TG
180 assert_se(sd_ipv4ll_is_running(ll));
181
d9bf4f8c
UTL
182 /* PROBE */
183 sd_event_run(e, (uint64_t) -1);
e095f51d 184 assert_se(recv(test_fd[1], &arp, sizeof(struct ether_arp), 0) == sizeof(struct ether_arp));
d9bf4f8c
UTL
185
186 if (extended) {
187 /* PROBE */
188 sd_event_run(e, (uint64_t) -1);
e095f51d 189 assert_se(recv(test_fd[1], &arp, sizeof(struct ether_arp), 0) == sizeof(struct ether_arp));
d9bf4f8c
UTL
190
191 /* PROBE */
192 sd_event_run(e, (uint64_t) -1);
e095f51d 193 assert_se(recv(test_fd[1], &arp, sizeof(struct ether_arp), 0) == sizeof(struct ether_arp));
d9bf4f8c
UTL
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
207int main(int argc, char *argv[]) {
4afd3348 208 _cleanup_(sd_event_unrefp) sd_event *e = NULL;
d9bf4f8c 209
e3dca008
TG
210 log_set_max_level(LOG_DEBUG);
211 log_parse_environment();
212 log_open();
213
d9bf4f8c
UTL
214 assert_se(sd_event_new(&e) >= 0);
215
216 test_public_api_setters(e);
d9bf4f8c
UTL
217 test_basic_request(e);
218
219 return 0;
220}