1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
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/>.
25 #include <sys/socket.h>
26 #include <sys/types.h>
29 #include "sd-ipv4ll.h"
32 #include "event-util.h"
34 #include "socket-util.h"
37 static bool verbose
= false;
38 static bool extended
= false;
39 static int test_fd
[2];
41 static int basic_request_handler_bind
= 0;
42 static int basic_request_handler_stop
= 0;
43 static void* basic_request_handler_userdata
= (void*)0xCABCAB;
44 static void basic_request_handler(sd_ipv4ll
*ll
, int event
, void *userdata
) {
45 assert_se(userdata
== basic_request_handler_userdata
);
48 case SD_IPV4LL_EVENT_STOP
:
49 basic_request_handler_stop
= 1;
51 case SD_IPV4LL_EVENT_BIND
:
52 basic_request_handler_bind
= 1;
60 static int arp_network_send_raw_socket(int fd
, int ifindex
,
61 const struct ether_arp
*arp
) {
63 assert_se(ifindex
> 0);
66 if (send(fd
, arp
, sizeof(struct ether_arp
), 0) < 0)
72 int arp_send_probe(int fd
, int ifindex
,
73 be32_t pa
, const struct ether_addr
*ha
) {
74 struct ether_arp ea
= {};
81 return arp_network_send_raw_socket(fd
, ifindex
, &ea
);
84 int arp_send_announcement(int fd
, int ifindex
,
85 be32_t pa
, const struct ether_addr
*ha
) {
86 struct ether_arp ea
= {};
93 return arp_network_send_raw_socket(fd
, ifindex
, &ea
);
96 int arp_network_bind_raw_socket(int index
, be32_t address
, const struct ether_addr
*eth_mac
) {
97 if (socketpair(AF_UNIX
, SOCK_DGRAM
| SOCK_NONBLOCK
, 0, test_fd
) < 0)
103 static void test_public_api_setters(sd_event
*e
) {
104 struct in_addr address
= {};
107 struct ether_addr mac_addr
= {
108 .ether_addr_octet
= {'A', 'B', 'C', '1', '2', '3'}};
111 printf("* %s\n", __FUNCTION__
);
113 assert_se(sd_ipv4ll_new(&ll
) == 0);
116 assert_se(sd_ipv4ll_attach_event(NULL
, NULL
, 0) == -EINVAL
);
117 assert_se(sd_ipv4ll_attach_event(ll
, e
, 0) == 0);
118 assert_se(sd_ipv4ll_attach_event(ll
, e
, 0) == -EBUSY
);
120 assert_se(sd_ipv4ll_set_callback(NULL
, NULL
, NULL
) == -EINVAL
);
121 assert_se(sd_ipv4ll_set_callback(ll
, NULL
, NULL
) == 0);
123 assert_se(sd_ipv4ll_set_address(ll
, &address
) == -EINVAL
);
124 address
.s_addr
|= htobe32(169U << 24 | 254U << 16);
125 assert_se(sd_ipv4ll_set_address(ll
, &address
) == -EINVAL
);
126 address
.s_addr
|= htobe32(0x00FF);
127 assert_se(sd_ipv4ll_set_address(ll
, &address
) == -EINVAL
);
128 address
.s_addr
|= htobe32(0xF000);
129 assert_se(sd_ipv4ll_set_address(ll
, &address
) == 0);
130 address
.s_addr
|= htobe32(0x0F00);
131 assert_se(sd_ipv4ll_set_address(ll
, &address
) == -EINVAL
);
133 assert_se(sd_ipv4ll_set_address_seed(NULL
, seed
) == -EINVAL
);
134 assert_se(sd_ipv4ll_set_address_seed(ll
, seed
) == 0);
136 assert_se(sd_ipv4ll_set_mac(NULL
, NULL
) == -EINVAL
);
137 assert_se(sd_ipv4ll_set_mac(ll
, NULL
) == -EINVAL
);
138 assert_se(sd_ipv4ll_set_mac(ll
, &mac_addr
) == 0);
140 assert_se(sd_ipv4ll_set_index(NULL
, -1) == -EINVAL
);
141 assert_se(sd_ipv4ll_set_index(ll
, -1) == -EINVAL
);
142 assert_se(sd_ipv4ll_set_index(ll
, -99) == -EINVAL
);
143 assert_se(sd_ipv4ll_set_index(ll
, 1) == 0);
144 assert_se(sd_ipv4ll_set_index(ll
, 99) == 0);
146 assert_se(sd_ipv4ll_ref(ll
) == ll
);
147 assert_se(sd_ipv4ll_unref(ll
) == NULL
);
150 assert_se(sd_ipv4ll_unref(ll
) == NULL
);
153 static void test_basic_request(sd_event
*e
) {
156 struct ether_arp arp
;
157 struct ether_addr mac_addr
= {
158 .ether_addr_octet
= {'A', 'B', 'C', '1', '2', '3'}};
161 printf("* %s\n", __FUNCTION__
);
163 assert_se(sd_ipv4ll_new(&ll
) == 0);
164 assert_se(sd_ipv4ll_start(ll
) == -EINVAL
);
166 assert_se(sd_ipv4ll_attach_event(ll
, e
, 0) == 0);
167 assert_se(sd_ipv4ll_start(ll
) == -EINVAL
);
169 assert_se(sd_ipv4ll_set_mac(ll
, &mac_addr
) == 0);
170 assert_se(sd_ipv4ll_start(ll
) == -EINVAL
);
172 assert_se(sd_ipv4ll_set_callback(ll
, basic_request_handler
,
173 basic_request_handler_userdata
) == 0);
174 assert_se(sd_ipv4ll_start(ll
) == -EINVAL
);
176 assert_se(sd_ipv4ll_set_index(ll
, 1) == 0);
177 assert_se(sd_ipv4ll_start(ll
) == 0);
179 sd_event_run(e
, (uint64_t) -1);
180 assert_se(sd_ipv4ll_start(ll
) == -EBUSY
);
182 assert_se(sd_ipv4ll_is_running(ll
));
185 sd_event_run(e
, (uint64_t) -1);
186 assert_se(read(test_fd
[1], &arp
, sizeof(struct ether_arp
)) == sizeof(struct ether_arp
));
190 sd_event_run(e
, (uint64_t) -1);
191 assert_se(read(test_fd
[1], &arp
, sizeof(struct ether_arp
)) == sizeof(struct ether_arp
));
194 sd_event_run(e
, (uint64_t) -1);
195 assert_se(read(test_fd
[1], &arp
, sizeof(struct ether_arp
)) == sizeof(struct ether_arp
));
197 sd_event_run(e
, (uint64_t) -1);
198 assert_se(basic_request_handler_bind
== 1);
202 assert_se(basic_request_handler_stop
== 1);
205 assert_se(sd_ipv4ll_unref(ll
) == NULL
);
206 safe_close(test_fd
[1]);
209 int main(int argc
, char *argv
[]) {
210 _cleanup_event_unref_ sd_event
*e
= NULL
;
212 log_set_max_level(LOG_DEBUG
);
213 log_parse_environment();
216 assert_se(sd_event_new(&e
) >= 0);
218 test_public_api_setters(e
);
219 test_basic_request(e
);