1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 This file is part of systemd.
5 Copyright (C) 2014 Axis Communications AB. All rights reserved.
7 systemd is free software; you can redistribute it and/or modify it
8 under the terms of the GNU Lesser General Public License as published by
9 the Free Software Foundation; either version 2.1 of the License, or
10 (at your option) any later version.
12 systemd is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Lesser General Public License for more details.
17 You should have received a copy of the GNU Lesser General Public License
18 along with systemd; If not, see <http://www.gnu.org/licenses/>.
24 #include <sys/socket.h>
25 #include <sys/types.h>
28 #include "sd-ipv4ll.h"
32 #include "socket-util.h"
35 static bool verbose
= false;
36 static bool extended
= false;
37 static int test_fd
[2];
39 static int basic_request_handler_bind
= 0;
40 static int basic_request_handler_stop
= 0;
41 static void* basic_request_handler_userdata
= (void*) 0xCABCAB;
43 static void basic_request_handler(sd_ipv4ll
*ll
, int event
, void *userdata
) {
44 assert_se(userdata
== basic_request_handler_userdata
);
47 case SD_IPV4LL_EVENT_STOP
:
48 basic_request_handler_stop
= 1;
50 case SD_IPV4LL_EVENT_BIND
:
51 basic_request_handler_bind
= 1;
59 static int arp_network_send_raw_socket(int fd
, int ifindex
,
60 const struct ether_arp
*arp
) {
62 assert_se(ifindex
> 0);
65 if (send(fd
, arp
, sizeof(struct ether_arp
), 0) < 0)
71 int arp_send_probe(int fd
, int ifindex
,
72 be32_t pa
, const struct ether_addr
*ha
) {
73 struct ether_arp ea
= {};
76 assert_se(ifindex
> 0);
80 return arp_network_send_raw_socket(fd
, ifindex
, &ea
);
83 int arp_send_announcement(int fd
, int ifindex
,
84 be32_t pa
, const struct ether_addr
*ha
) {
85 struct ether_arp ea
= {};
88 assert_se(ifindex
> 0);
92 return arp_network_send_raw_socket(fd
, ifindex
, &ea
);
95 int arp_network_bind_raw_socket(int index
, be32_t address
, const struct ether_addr
*eth_mac
) {
96 if (socketpair(AF_UNIX
, SOCK_DGRAM
| SOCK_NONBLOCK
, 0, test_fd
) < 0)
102 static void test_public_api_setters(sd_event
*e
) {
103 struct in_addr address
= {};
106 struct ether_addr mac_addr
= {
107 .ether_addr_octet
= {'A', 'B', 'C', '1', '2', '3'}};
110 printf("* %s\n", __FUNCTION__
);
112 assert_se(sd_ipv4ll_new(&ll
) == 0);
115 assert_se(sd_ipv4ll_attach_event(NULL
, NULL
, 0) == -EINVAL
);
116 assert_se(sd_ipv4ll_attach_event(ll
, e
, 0) == 0);
117 assert_se(sd_ipv4ll_attach_event(ll
, e
, 0) == -EBUSY
);
119 assert_se(sd_ipv4ll_set_callback(NULL
, NULL
, NULL
) == -EINVAL
);
120 assert_se(sd_ipv4ll_set_callback(ll
, NULL
, NULL
) == 0);
122 assert_se(sd_ipv4ll_set_address(ll
, &address
) == -EINVAL
);
123 address
.s_addr
|= htobe32(169U << 24 | 254U << 16);
124 assert_se(sd_ipv4ll_set_address(ll
, &address
) == -EINVAL
);
125 address
.s_addr
|= htobe32(0x00FF);
126 assert_se(sd_ipv4ll_set_address(ll
, &address
) == -EINVAL
);
127 address
.s_addr
|= htobe32(0xF000);
128 assert_se(sd_ipv4ll_set_address(ll
, &address
) == 0);
129 address
.s_addr
|= htobe32(0x0F00);
130 assert_se(sd_ipv4ll_set_address(ll
, &address
) == -EINVAL
);
132 assert_se(sd_ipv4ll_set_address_seed(NULL
, seed
) == -EINVAL
);
133 assert_se(sd_ipv4ll_set_address_seed(ll
, seed
) == 0);
135 assert_se(sd_ipv4ll_set_mac(NULL
, NULL
) == -EINVAL
);
136 assert_se(sd_ipv4ll_set_mac(ll
, NULL
) == -EINVAL
);
137 assert_se(sd_ipv4ll_set_mac(ll
, &mac_addr
) == 0);
139 assert_se(sd_ipv4ll_set_ifindex(NULL
, -1) == -EINVAL
);
140 assert_se(sd_ipv4ll_set_ifindex(ll
, -1) == -EINVAL
);
141 assert_se(sd_ipv4ll_set_ifindex(ll
, -99) == -EINVAL
);
142 assert_se(sd_ipv4ll_set_ifindex(ll
, 1) == 0);
143 assert_se(sd_ipv4ll_set_ifindex(ll
, 99) == 0);
145 assert_se(sd_ipv4ll_ref(ll
) == ll
);
146 assert_se(sd_ipv4ll_unref(ll
) == NULL
);
149 assert_se(sd_ipv4ll_unref(ll
) == NULL
);
152 static void test_basic_request(sd_event
*e
) {
155 struct ether_arp arp
;
156 struct ether_addr mac_addr
= {
157 .ether_addr_octet
= {'A', 'B', 'C', '1', '2', '3'}};
160 printf("* %s\n", __FUNCTION__
);
162 assert_se(sd_ipv4ll_new(&ll
) == 0);
163 assert_se(sd_ipv4ll_start(ll
) == -EINVAL
);
165 assert_se(sd_ipv4ll_attach_event(ll
, e
, 0) == 0);
166 assert_se(sd_ipv4ll_start(ll
) == -EINVAL
);
168 assert_se(sd_ipv4ll_set_mac(ll
, &mac_addr
) == 0);
169 assert_se(sd_ipv4ll_start(ll
) == -EINVAL
);
171 assert_se(sd_ipv4ll_set_callback(ll
, basic_request_handler
,
172 basic_request_handler_userdata
) == 0);
173 assert_se(sd_ipv4ll_start(ll
) == -EINVAL
);
175 assert_se(sd_ipv4ll_set_ifindex(ll
, 1) == 0);
176 assert_se(sd_ipv4ll_start(ll
) == 0);
178 sd_event_run(e
, (uint64_t) -1);
179 assert_se(sd_ipv4ll_start(ll
) == -EBUSY
);
181 assert_se(sd_ipv4ll_is_running(ll
));
184 sd_event_run(e
, (uint64_t) -1);
185 assert_se(recv(test_fd
[1], &arp
, sizeof(struct ether_arp
), 0) == sizeof(struct ether_arp
));
189 sd_event_run(e
, (uint64_t) -1);
190 assert_se(recv(test_fd
[1], &arp
, sizeof(struct ether_arp
), 0) == sizeof(struct ether_arp
));
193 sd_event_run(e
, (uint64_t) -1);
194 assert_se(recv(test_fd
[1], &arp
, sizeof(struct ether_arp
), 0) == sizeof(struct ether_arp
));
196 sd_event_run(e
, (uint64_t) -1);
197 assert_se(basic_request_handler_bind
== 1);
201 assert_se(basic_request_handler_stop
== 1);
204 assert_se(sd_ipv4ll_unref(ll
) == NULL
);
205 safe_close(test_fd
[1]);
208 int main(int argc
, char *argv
[]) {
209 _cleanup_(sd_event_unrefp
) sd_event
*e
= NULL
;
211 log_set_max_level(LOG_DEBUG
);
212 log_parse_environment();
215 assert_se(sd_event_new(&e
) >= 0);
217 test_public_api_setters(e
);
218 test_basic_request(e
);