1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 This file is part of systemd.
5 Copyright (C) 2014 Axis Communications AB. All rights reserved.
11 #include <sys/socket.h>
12 #include <sys/types.h>
15 #include "sd-ipv4ll.h"
19 #include "socket-util.h"
22 static bool verbose
= false;
23 static bool extended
= false;
24 static int test_fd
[2];
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;
30 static void basic_request_handler(sd_ipv4ll
*ll
, int event
, void *userdata
) {
31 assert_se(userdata
== basic_request_handler_userdata
);
34 case SD_IPV4LL_EVENT_STOP
:
35 basic_request_handler_stop
= 1;
37 case SD_IPV4LL_EVENT_BIND
:
38 basic_request_handler_bind
= 1;
46 static int arp_network_send_raw_socket(int fd
, int ifindex
,
47 const struct ether_arp
*arp
) {
49 assert_se(ifindex
> 0);
52 if (send(fd
, arp
, sizeof(struct ether_arp
), 0) < 0)
58 int arp_send_probe(int fd
, int ifindex
,
59 be32_t pa
, const struct ether_addr
*ha
) {
60 struct ether_arp ea
= {};
63 assert_se(ifindex
> 0);
67 return arp_network_send_raw_socket(fd
, ifindex
, &ea
);
70 int arp_send_announcement(int fd
, int ifindex
,
71 be32_t pa
, const struct ether_addr
*ha
) {
72 struct ether_arp ea
= {};
75 assert_se(ifindex
> 0);
79 return arp_network_send_raw_socket(fd
, ifindex
, &ea
);
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_NONBLOCK
, 0, test_fd
) < 0)
89 static void test_public_api_setters(sd_event
*e
) {
90 struct in_addr address
= {};
93 struct ether_addr mac_addr
= {
94 .ether_addr_octet
= {'A', 'B', 'C', '1', '2', '3'}};
97 printf("* %s\n", __FUNCTION__
);
99 assert_se(sd_ipv4ll_new(&ll
) == 0);
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
);
106 assert_se(sd_ipv4ll_set_callback(NULL
, NULL
, NULL
) == -EINVAL
);
107 assert_se(sd_ipv4ll_set_callback(ll
, NULL
, NULL
) == 0);
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
);
119 assert_se(sd_ipv4ll_set_address_seed(NULL
, seed
) == -EINVAL
);
120 assert_se(sd_ipv4ll_set_address_seed(ll
, seed
) == 0);
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);
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);
132 assert_se(sd_ipv4ll_ref(ll
) == ll
);
133 assert_se(sd_ipv4ll_unref(ll
) == NULL
);
136 assert_se(sd_ipv4ll_unref(ll
) == NULL
);
139 static void test_basic_request(sd_event
*e
) {
142 struct ether_arp arp
;
143 struct ether_addr mac_addr
= {
144 .ether_addr_octet
= {'A', 'B', 'C', '1', '2', '3'}};
147 printf("* %s\n", __FUNCTION__
);
149 assert_se(sd_ipv4ll_new(&ll
) == 0);
150 assert_se(sd_ipv4ll_start(ll
) == -EINVAL
);
152 assert_se(sd_ipv4ll_attach_event(ll
, e
, 0) == 0);
153 assert_se(sd_ipv4ll_start(ll
) == -EINVAL
);
155 assert_se(sd_ipv4ll_set_mac(ll
, &mac_addr
) == 0);
156 assert_se(sd_ipv4ll_start(ll
) == -EINVAL
);
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
);
162 assert_se(sd_ipv4ll_set_ifindex(ll
, 1) == 0);
163 assert_se(sd_ipv4ll_start(ll
) == 0);
165 sd_event_run(e
, (uint64_t) -1);
166 assert_se(sd_ipv4ll_start(ll
) == -EBUSY
);
168 assert_se(sd_ipv4ll_is_running(ll
));
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
));
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
));
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
));
183 sd_event_run(e
, (uint64_t) -1);
184 assert_se(basic_request_handler_bind
== 1);
188 assert_se(basic_request_handler_stop
== 1);
191 assert_se(sd_ipv4ll_unref(ll
) == NULL
);
192 safe_close(test_fd
[1]);
195 int main(int argc
, char *argv
[]) {
196 _cleanup_(sd_event_unrefp
) sd_event
*e
= NULL
;
198 log_set_max_level(LOG_DEBUG
);
199 log_parse_environment();
202 assert_se(sd_event_new(&e
) >= 0);
204 test_public_api_setters(e
);
205 test_basic_request(e
);