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"
32 #include "sd-ipv4ll.h"
33 #include "ipv4ll-internal.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_user_data = (void*)0xCABCAB;
42 static void basic_request_handler(sd_ipv4ll *ll, int event, void *userdata) {
43 assert_se(userdata == basic_request_handler_user_data);
46 case IPV4LL_EVENT_STOP:
47 basic_request_handler_stop = 1;
49 case IPV4LL_EVENT_BIND:
50 basic_request_handler_bind = 1;
58 int arp_network_send_raw_socket(int fd, const union sockaddr_union *link,
59 const struct ether_arp *arp) {
64 if (send(fd, arp, sizeof(struct ether_arp), 0) < 0)
70 int arp_network_bind_raw_socket(int index, union sockaddr_union *link) {
71 if (socketpair(AF_UNIX, SOCK_DGRAM | SOCK_NONBLOCK, 0, test_fd) < 0)
77 static void test_arp_header(struct ether_arp *arp) {
79 assert_se(arp->ea_hdr.ar_hrd == htons(ARPHRD_ETHER)); /* HTYPE */
80 assert_se(arp->ea_hdr.ar_pro == htons(ETHERTYPE_IP)); /* PTYPE */
81 assert_se(arp->ea_hdr.ar_hln == ETH_ALEN); /* HLEN */
82 assert_se(arp->ea_hdr.ar_pln == sizeof arp->arp_spa); /* PLEN */
83 assert_se(arp->ea_hdr.ar_op == htons(ARPOP_REQUEST)); /* REQUEST */
86 static void test_arp_probe(void) {
88 struct ether_addr mac_addr = {
89 .ether_addr_octet = {'A', 'B', 'C', '1', '2', '3'}};
93 printf("* %s\n", __FUNCTION__);
95 arp_packet_probe(&arp, pa, &mac_addr);
96 test_arp_header(&arp);
97 assert_se(memcmp(arp.arp_sha, &mac_addr, ETH_ALEN) == 0);
98 assert_se(memcmp(arp.arp_tpa, &pa, sizeof(pa)) == 0);
101 static void test_arp_announce(void) {
102 struct ether_arp arp;
103 struct ether_addr mac_addr = {
104 .ether_addr_octet = {'A', 'B', 'C', '1', '2', '3'}};
108 printf("* %s\n", __FUNCTION__);
110 arp_packet_announcement(&arp, pa, &mac_addr);
111 test_arp_header(&arp);
112 assert_se(memcmp(arp.arp_sha, &mac_addr, ETH_ALEN) == 0);
113 assert_se(memcmp(arp.arp_tpa, &pa, sizeof(pa)) == 0);
114 assert_se(memcmp(arp.arp_spa, &pa, sizeof(pa)) == 0);
117 static void test_public_api_setters(sd_event *e) {
120 struct ether_addr mac_addr = {
121 .ether_addr_octet = {'A', 'B', 'C', '1', '2', '3'}};
124 printf("* %s\n", __FUNCTION__);
126 assert_se(sd_ipv4ll_new(&ll) == 0);
129 assert_se(sd_ipv4ll_attach_event(NULL, NULL, 0) == -EINVAL);
130 assert_se(sd_ipv4ll_attach_event(ll, e, 0) == 0);
131 assert_se(sd_ipv4ll_attach_event(ll, e, 0) == -EBUSY);
133 assert_se(sd_ipv4ll_set_callback(NULL, NULL, NULL) == -EINVAL);
134 assert_se(sd_ipv4ll_set_callback(ll, NULL, NULL) == 0);
136 assert_se(sd_ipv4ll_set_address_seed(NULL, NULL) == -EINVAL);
137 assert_se(sd_ipv4ll_set_address_seed(ll, NULL) == -EINVAL);
138 assert_se(sd_ipv4ll_set_address_seed(ll, seed) == 0);
140 assert_se(sd_ipv4ll_set_mac(NULL, NULL) == -EINVAL);
141 assert_se(sd_ipv4ll_set_mac(ll, NULL) == -EINVAL);
142 assert_se(sd_ipv4ll_set_mac(ll, &mac_addr) == 0);
144 assert_se(sd_ipv4ll_set_index(NULL, -1) == -EINVAL);
145 assert_se(sd_ipv4ll_set_index(ll, -1) == -EINVAL);
146 assert_se(sd_ipv4ll_set_index(ll, -99) == -EINVAL);
147 assert_se(sd_ipv4ll_set_index(ll, 1) == 0);
148 assert_se(sd_ipv4ll_set_index(ll, 99) == 0);
150 assert_se(sd_ipv4ll_ref(ll) == ll);
151 assert_se(sd_ipv4ll_unref(ll) == ll);
154 assert_se(sd_ipv4ll_unref(ll) == NULL);
157 static void test_basic_request(sd_event *e) {
160 struct ether_arp arp;
161 struct ether_addr mac_addr = {
162 .ether_addr_octet = {'A', 'B', 'C', '1', '2', '3'}};
165 printf("* %s\n", __FUNCTION__);
167 assert_se(sd_ipv4ll_new(&ll) == 0);
168 assert_se(sd_ipv4ll_start(ll) == -EINVAL);
170 assert_se(sd_ipv4ll_attach_event(ll, e, 0) == 0);
171 assert_se(sd_ipv4ll_start(ll) == -EINVAL);
173 assert_se(sd_ipv4ll_set_mac(ll, &mac_addr) == 0);
174 assert_se(sd_ipv4ll_start(ll) == -EINVAL);
176 assert_se(sd_ipv4ll_set_callback(ll, basic_request_handler,
177 basic_request_handler_user_data) == 0);
178 assert_se(sd_ipv4ll_start(ll) == -EINVAL);
180 assert_se(sd_ipv4ll_set_index(ll, 1) == 0);
181 assert_se(sd_ipv4ll_start(ll) == 0);
183 sd_event_run(e, (uint64_t) -1);
184 assert_se(sd_ipv4ll_start(ll) == -EBUSY);
187 sd_event_run(e, (uint64_t) -1);
188 assert_se(read(test_fd[1], &arp, sizeof(struct ether_arp)) == sizeof(struct ether_arp));
189 test_arp_header(&arp);
193 sd_event_run(e, (uint64_t) -1);
194 assert_se(read(test_fd[1], &arp, sizeof(struct ether_arp)) == sizeof(struct ether_arp));
195 test_arp_header(&arp);
198 sd_event_run(e, (uint64_t) -1);
199 assert_se(read(test_fd[1], &arp, sizeof(struct ether_arp)) == sizeof(struct ether_arp));
200 test_arp_header(&arp);
202 sd_event_run(e, (uint64_t) -1);
203 assert_se(basic_request_handler_bind == 1);
207 assert_se(basic_request_handler_stop == 1);
210 assert_se(sd_ipv4ll_unref(ll) == NULL);
211 safe_close(test_fd[1]);
214 int main(int argc, char *argv[]) {
217 assert_se(sd_event_new(&e) >= 0);
219 test_public_api_setters(e);
222 test_basic_request(e);