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/types.h>
26 #include <sys/socket.h>
30 #include "socket-util.h"
31 #include "event-util.h"
33 #include "sd-ipv4ll.h"
34 #include "ipv4ll-internal.h"
36 static bool verbose
= false;
37 static bool extended
= false;
38 static int test_fd
[2];
40 static int basic_request_handler_bind
= 0;
41 static int basic_request_handler_stop
= 0;
42 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 IPV4LL_EVENT_STOP
:
48 basic_request_handler_stop
= 1;
50 case IPV4LL_EVENT_BIND
:
51 basic_request_handler_bind
= 1;
59 int arp_network_send_raw_socket(int fd
, const union sockaddr_union
*link
,
60 const struct ether_arp
*arp
) {
65 if (send(fd
, arp
, sizeof(struct ether_arp
), 0) < 0)
71 int arp_network_bind_raw_socket(int index
, union sockaddr_union
*link
) {
72 if (socketpair(AF_UNIX
, SOCK_DGRAM
| SOCK_NONBLOCK
, 0, test_fd
) < 0)
78 static void test_arp_header(struct ether_arp
*arp
) {
80 assert_se(arp
->ea_hdr
.ar_hrd
== htons(ARPHRD_ETHER
)); /* HTYPE */
81 assert_se(arp
->ea_hdr
.ar_pro
== htons(ETHERTYPE_IP
)); /* PTYPE */
82 assert_se(arp
->ea_hdr
.ar_hln
== ETH_ALEN
); /* HLEN */
83 assert_se(arp
->ea_hdr
.ar_pln
== sizeof arp
->arp_spa
); /* PLEN */
84 assert_se(arp
->ea_hdr
.ar_op
== htons(ARPOP_REQUEST
)); /* REQUEST */
87 static void test_arp_probe(void) {
89 struct ether_addr mac_addr
= {
90 .ether_addr_octet
= {'A', 'B', 'C', '1', '2', '3'}};
94 printf("* %s\n", __FUNCTION__
);
96 arp_packet_probe(&arp
, pa
, &mac_addr
);
97 test_arp_header(&arp
);
98 assert_se(memcmp(arp
.arp_sha
, &mac_addr
, ETH_ALEN
) == 0);
99 assert_se(memcmp(arp
.arp_tpa
, &pa
, sizeof(pa
)) == 0);
102 static void test_arp_announce(void) {
103 struct ether_arp arp
;
104 struct ether_addr mac_addr
= {
105 .ether_addr_octet
= {'A', 'B', 'C', '1', '2', '3'}};
109 printf("* %s\n", __FUNCTION__
);
111 arp_packet_announcement(&arp
, pa
, &mac_addr
);
112 test_arp_header(&arp
);
113 assert_se(memcmp(arp
.arp_sha
, &mac_addr
, ETH_ALEN
) == 0);
114 assert_se(memcmp(arp
.arp_tpa
, &pa
, sizeof(pa
)) == 0);
115 assert_se(memcmp(arp
.arp_spa
, &pa
, sizeof(pa
)) == 0);
118 static void test_public_api_setters(sd_event
*e
) {
121 struct ether_addr mac_addr
= {
122 .ether_addr_octet
= {'A', 'B', 'C', '1', '2', '3'}};
125 printf("* %s\n", __FUNCTION__
);
127 assert_se(sd_ipv4ll_new(&ll
) == 0);
130 assert_se(sd_ipv4ll_attach_event(NULL
, NULL
, 0) == -EINVAL
);
131 assert_se(sd_ipv4ll_attach_event(ll
, e
, 0) == 0);
132 assert_se(sd_ipv4ll_attach_event(ll
, e
, 0) == -EBUSY
);
134 assert_se(sd_ipv4ll_set_callback(NULL
, NULL
, NULL
) == -EINVAL
);
135 assert_se(sd_ipv4ll_set_callback(ll
, NULL
, NULL
) == 0);
137 assert_se(sd_ipv4ll_set_address_seed(NULL
, NULL
) == -EINVAL
);
138 assert_se(sd_ipv4ll_set_address_seed(ll
, NULL
) == -EINVAL
);
139 assert_se(sd_ipv4ll_set_address_seed(ll
, seed
) == 0);
141 assert_se(sd_ipv4ll_set_mac(NULL
, NULL
) == -EINVAL
);
142 assert_se(sd_ipv4ll_set_mac(ll
, NULL
) == -EINVAL
);
143 assert_se(sd_ipv4ll_set_mac(ll
, &mac_addr
) == 0);
145 assert_se(sd_ipv4ll_set_index(NULL
, -1) == -EINVAL
);
146 assert_se(sd_ipv4ll_set_index(ll
, -1) == -EINVAL
);
147 assert_se(sd_ipv4ll_set_index(ll
, -99) == -EINVAL
);
148 assert_se(sd_ipv4ll_set_index(ll
, 1) == 0);
149 assert_se(sd_ipv4ll_set_index(ll
, 99) == 0);
151 assert_se(sd_ipv4ll_ref(ll
) == ll
);
152 assert_se(sd_ipv4ll_unref(ll
) == ll
);
155 assert_se(sd_ipv4ll_unref(ll
) == NULL
);
158 static void test_basic_request(sd_event
*e
) {
161 struct ether_arp arp
;
162 struct ether_addr mac_addr
= {
163 .ether_addr_octet
= {'A', 'B', 'C', '1', '2', '3'}};
166 printf("* %s\n", __FUNCTION__
);
168 assert_se(sd_ipv4ll_new(&ll
) == 0);
169 assert_se(sd_ipv4ll_start(ll
) == -EINVAL
);
171 assert_se(sd_ipv4ll_attach_event(ll
, e
, 0) == 0);
172 assert_se(sd_ipv4ll_start(ll
) == -EINVAL
);
174 assert_se(sd_ipv4ll_set_mac(ll
, &mac_addr
) == 0);
175 assert_se(sd_ipv4ll_start(ll
) == -EINVAL
);
177 assert_se(sd_ipv4ll_set_callback(ll
, basic_request_handler
,
178 basic_request_handler_userdata
) == 0);
179 assert_se(sd_ipv4ll_start(ll
) == -EINVAL
);
181 assert_se(sd_ipv4ll_set_index(ll
, 1) == 0);
182 assert_se(sd_ipv4ll_start(ll
) == 0);
184 sd_event_run(e
, (uint64_t) -1);
185 assert_se(sd_ipv4ll_start(ll
) == -EBUSY
);
188 sd_event_run(e
, (uint64_t) -1);
189 assert_se(read(test_fd
[1], &arp
, sizeof(struct ether_arp
)) == sizeof(struct ether_arp
));
190 test_arp_header(&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
));
196 test_arp_header(&arp
);
199 sd_event_run(e
, (uint64_t) -1);
200 assert_se(read(test_fd
[1], &arp
, sizeof(struct ether_arp
)) == sizeof(struct ether_arp
));
201 test_arp_header(&arp
);
203 sd_event_run(e
, (uint64_t) -1);
204 assert_se(basic_request_handler_bind
== 1);
208 assert_se(basic_request_handler_stop
== 1);
211 assert_se(sd_ipv4ll_unref(ll
) == NULL
);
212 safe_close(test_fd
[1]);
215 int main(int argc
, char *argv
[]) {
216 _cleanup_event_unref_ sd_event
*e
= NULL
;
218 assert_se(sd_event_new(&e
) >= 0);
220 test_public_api_setters(e
);
223 test_basic_request(e
);