]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/libsystemd-network/test-ipv4ll.c
network: s/user_data/userdata/
[thirdparty/systemd.git] / src / libsystemd-network / test-ipv4ll.c
1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
2 /***
3 This file is part of systemd.
4
5 Copyright (C) 2014 Axis Communications AB. All rights reserved.
6
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.
11
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.
16
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/>.
19 ***/
20
21 #include <stdlib.h>
22 #include <assert.h>
23 #include <errno.h>
24 #include <stdio.h>
25 #include <sys/types.h>
26 #include <sys/socket.h>
27 #include <unistd.h>
28
29 #include "util.h"
30 #include "socket-util.h"
31 #include "event-util.h"
32
33 #include "sd-ipv4ll.h"
34 #include "ipv4ll-internal.h"
35
36 static bool verbose = false;
37 static bool extended = false;
38 static int test_fd[2];
39
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);
45
46 switch(event) {
47 case IPV4LL_EVENT_STOP:
48 basic_request_handler_stop = 1;
49 break;
50 case IPV4LL_EVENT_BIND:
51 basic_request_handler_bind = 1;
52 break;
53 default:
54 assert_se(0);
55 break;
56 }
57 }
58
59 int arp_network_send_raw_socket(int fd, const union sockaddr_union *link,
60 const struct ether_arp *arp) {
61 assert_se(arp);
62 assert_se(link);
63 assert_se(fd >= 0);
64
65 if (send(fd, arp, sizeof(struct ether_arp), 0) < 0)
66 return -errno;
67
68 return 0;
69 }
70
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)
73 return -errno;
74
75 return test_fd[0];
76 }
77
78 static void test_arp_header(struct ether_arp *arp) {
79 assert_se(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 */
85 }
86
87 static void test_arp_probe(void) {
88 struct ether_arp arp;
89 struct ether_addr mac_addr = {
90 .ether_addr_octet = {'A', 'B', 'C', '1', '2', '3'}};
91 be32_t pa = 0x3030;
92
93 if (verbose)
94 printf("* %s\n", __FUNCTION__);
95
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);
100 }
101
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'}};
106 be32_t pa = 0x3131;
107
108 if (verbose)
109 printf("* %s\n", __FUNCTION__);
110
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);
116 }
117
118 static void test_public_api_setters(sd_event *e) {
119 uint8_t seed[8];
120 sd_ipv4ll *ll;
121 struct ether_addr mac_addr = {
122 .ether_addr_octet = {'A', 'B', 'C', '1', '2', '3'}};
123
124 if (verbose)
125 printf("* %s\n", __FUNCTION__);
126
127 assert_se(sd_ipv4ll_new(&ll) == 0);
128 assert_se(ll);
129
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);
133
134 assert_se(sd_ipv4ll_set_callback(NULL, NULL, NULL) == -EINVAL);
135 assert_se(sd_ipv4ll_set_callback(ll, NULL, NULL) == 0);
136
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);
140
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);
144
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);
150
151 assert_se(sd_ipv4ll_ref(ll) == ll);
152 assert_se(sd_ipv4ll_unref(ll) == ll);
153
154 /* Cleanup */
155 assert_se(sd_ipv4ll_unref(ll) == NULL);
156 }
157
158 static void test_basic_request(sd_event *e) {
159
160 sd_ipv4ll *ll;
161 struct ether_arp arp;
162 struct ether_addr mac_addr = {
163 .ether_addr_octet = {'A', 'B', 'C', '1', '2', '3'}};
164
165 if (verbose)
166 printf("* %s\n", __FUNCTION__);
167
168 assert_se(sd_ipv4ll_new(&ll) == 0);
169 assert_se(sd_ipv4ll_start(ll) == -EINVAL);
170
171 assert_se(sd_ipv4ll_attach_event(ll, e, 0) == 0);
172 assert_se(sd_ipv4ll_start(ll) == -EINVAL);
173
174 assert_se(sd_ipv4ll_set_mac(ll, &mac_addr) == 0);
175 assert_se(sd_ipv4ll_start(ll) == -EINVAL);
176
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);
180
181 assert_se(sd_ipv4ll_set_index(ll, 1) == 0);
182 assert_se(sd_ipv4ll_start(ll) == 0);
183
184 sd_event_run(e, (uint64_t) -1);
185 assert_se(sd_ipv4ll_start(ll) == -EBUSY);
186
187 /* PROBE */
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);
191
192 if (extended) {
193 /* PROBE */
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);
197
198 /* PROBE */
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);
202
203 sd_event_run(e, (uint64_t) -1);
204 assert_se(basic_request_handler_bind == 1);
205 }
206
207 sd_ipv4ll_stop(ll);
208 assert_se(basic_request_handler_stop == 1);
209
210 /* Cleanup */
211 assert_se(sd_ipv4ll_unref(ll) == NULL);
212 safe_close(test_fd[1]);
213 }
214
215 int main(int argc, char *argv[]) {
216 _cleanup_event_unref_ sd_event *e = NULL;
217
218 assert_se(sd_event_new(&e) >= 0);
219
220 test_public_api_setters(e);
221 test_arp_probe();
222 test_arp_announce();
223 test_basic_request(e);
224
225 return 0;
226 }