1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 Copyright © 2014 Axis Communications AB. All rights reserved.
9 #include <sys/socket.h>
10 #include <sys/types.h>
13 #include "sd-ipv4ll.h"
17 #include "socket-util.h"
21 static bool verbose
= false;
22 static bool extended
= false;
23 static int test_fd
[2];
25 static int basic_request_handler_bind
= 0;
26 static int basic_request_handler_stop
= 0;
27 static void* basic_request_handler_userdata
= (void*) 0xCABCAB;
29 static void basic_request_handler(sd_ipv4ll
*ll
, int event
, void *userdata
) {
30 assert_se(userdata
== basic_request_handler_userdata
);
33 case SD_IPV4LL_EVENT_STOP
:
34 basic_request_handler_stop
= 1;
36 case SD_IPV4LL_EVENT_BIND
:
37 basic_request_handler_bind
= 1;
45 static int arp_network_send_raw_socket(int fd
, int ifindex
,
46 const struct ether_arp
*arp
) {
48 assert_se(ifindex
> 0);
51 if (send(fd
, arp
, sizeof(struct ether_arp
), 0) < 0)
57 int arp_send_probe(int fd
, int ifindex
,
58 be32_t pa
, const struct ether_addr
*ha
) {
59 struct ether_arp ea
= {};
62 assert_se(ifindex
> 0);
66 return arp_network_send_raw_socket(fd
, ifindex
, &ea
);
69 int arp_send_announcement(int fd
, int ifindex
,
70 be32_t pa
, const struct ether_addr
*ha
) {
71 struct ether_arp ea
= {};
74 assert_se(ifindex
> 0);
78 return arp_network_send_raw_socket(fd
, ifindex
, &ea
);
81 int arp_network_bind_raw_socket(int index
, be32_t address
, const struct ether_addr
*eth_mac
) {
82 if (socketpair(AF_UNIX
, SOCK_DGRAM
| SOCK_CLOEXEC
| SOCK_NONBLOCK
, 0, test_fd
) < 0)
88 static void test_public_api_setters(sd_event
*e
) {
89 struct in_addr address
= {};
92 struct ether_addr mac_addr
= {
93 .ether_addr_octet
= {'A', 'B', 'C', '1', '2', '3'}};
96 printf("* %s\n", __FUNCTION__
);
98 assert_se(sd_ipv4ll_new(&ll
) == 0);
101 assert_se(sd_ipv4ll_attach_event(NULL
, NULL
, 0) == -EINVAL
);
102 assert_se(sd_ipv4ll_attach_event(ll
, e
, 0) == 0);
103 assert_se(sd_ipv4ll_attach_event(ll
, e
, 0) == -EBUSY
);
105 assert_se(sd_ipv4ll_set_callback(NULL
, NULL
, NULL
) == -EINVAL
);
106 assert_se(sd_ipv4ll_set_callback(ll
, NULL
, NULL
) == 0);
108 assert_se(sd_ipv4ll_set_address(ll
, &address
) == -EINVAL
);
109 address
.s_addr
|= htobe32(169U << 24 | 254U << 16);
110 assert_se(sd_ipv4ll_set_address(ll
, &address
) == -EINVAL
);
111 address
.s_addr
|= htobe32(0x00FF);
112 assert_se(sd_ipv4ll_set_address(ll
, &address
) == -EINVAL
);
113 address
.s_addr
|= htobe32(0xF000);
114 assert_se(sd_ipv4ll_set_address(ll
, &address
) == 0);
115 address
.s_addr
|= htobe32(0x0F00);
116 assert_se(sd_ipv4ll_set_address(ll
, &address
) == -EINVAL
);
118 assert_se(sd_ipv4ll_set_address_seed(NULL
, seed
) == -EINVAL
);
119 assert_se(sd_ipv4ll_set_address_seed(ll
, seed
) == 0);
121 assert_se(sd_ipv4ll_set_mac(NULL
, NULL
) == -EINVAL
);
122 assert_se(sd_ipv4ll_set_mac(ll
, NULL
) == -EINVAL
);
123 assert_se(sd_ipv4ll_set_mac(ll
, &mac_addr
) == 0);
125 assert_se(sd_ipv4ll_set_ifindex(NULL
, -1) == -EINVAL
);
126 assert_se(sd_ipv4ll_set_ifindex(ll
, -1) == -EINVAL
);
127 assert_se(sd_ipv4ll_set_ifindex(ll
, -99) == -EINVAL
);
128 assert_se(sd_ipv4ll_set_ifindex(ll
, 1) == 0);
129 assert_se(sd_ipv4ll_set_ifindex(ll
, 99) == 0);
131 assert_se(sd_ipv4ll_ref(ll
) == ll
);
132 assert_se(sd_ipv4ll_unref(ll
) == NULL
);
135 assert_se(sd_ipv4ll_unref(ll
) == NULL
);
138 static void test_basic_request(sd_event
*e
) {
141 struct ether_arp arp
;
142 struct ether_addr mac_addr
= {
143 .ether_addr_octet
= {'A', 'B', 'C', '1', '2', '3'}};
146 printf("* %s\n", __FUNCTION__
);
148 assert_se(sd_ipv4ll_new(&ll
) == 0);
149 assert_se(sd_ipv4ll_start(ll
) == -EINVAL
);
151 assert_se(sd_ipv4ll_attach_event(ll
, e
, 0) == 0);
152 assert_se(sd_ipv4ll_start(ll
) == -EINVAL
);
154 assert_se(sd_ipv4ll_set_mac(ll
, &mac_addr
) == 0);
155 assert_se(sd_ipv4ll_start(ll
) == -EINVAL
);
157 assert_se(sd_ipv4ll_set_callback(ll
, basic_request_handler
,
158 basic_request_handler_userdata
) == 0);
159 assert_se(sd_ipv4ll_start(ll
) == -EINVAL
);
161 assert_se(sd_ipv4ll_set_ifindex(ll
, 1) == 0);
162 assert_se(sd_ipv4ll_start(ll
) == 0);
164 sd_event_run(e
, (uint64_t) -1);
165 assert_se(sd_ipv4ll_start(ll
) == -EBUSY
);
167 assert_se(sd_ipv4ll_is_running(ll
));
170 sd_event_run(e
, (uint64_t) -1);
171 assert_se(recv(test_fd
[1], &arp
, sizeof(struct ether_arp
), 0) == sizeof(struct ether_arp
));
175 sd_event_run(e
, (uint64_t) -1);
176 assert_se(recv(test_fd
[1], &arp
, sizeof(struct ether_arp
), 0) == sizeof(struct ether_arp
));
179 sd_event_run(e
, (uint64_t) -1);
180 assert_se(recv(test_fd
[1], &arp
, sizeof(struct ether_arp
), 0) == sizeof(struct ether_arp
));
182 sd_event_run(e
, (uint64_t) -1);
183 assert_se(basic_request_handler_bind
== 1);
187 assert_se(basic_request_handler_stop
== 1);
190 assert_se(sd_ipv4ll_unref(ll
) == NULL
);
191 safe_close(test_fd
[1]);
194 int main(int argc
, char *argv
[]) {
195 _cleanup_(sd_event_unrefp
) sd_event
*e
= NULL
;
197 test_setup_logging(LOG_DEBUG
);
199 assert_se(sd_event_new(&e
) >= 0);
201 test_public_api_setters(e
);
202 test_basic_request(e
);