]>
Commit | Line | Data |
---|---|---|
be391925 PF |
1 | /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/ |
2 | ||
3 | /*** | |
4 | This file is part of systemd. | |
5 | ||
6 | Copyright (C) 2013 Intel Corporation. All rights reserved. | |
7 | ||
8 | systemd is free software; you can redistribute it and/or modify it | |
9 | under the terms of the GNU Lesser General Public License as published by | |
10 | the Free Software Foundation; either version 2.1 of the License, or | |
11 | (at your option) any later version. | |
12 | ||
13 | systemd is distributed in the hope that it will be useful, but | |
14 | WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
16 | Lesser General Public License for more details. | |
17 | ||
18 | You should have received a copy of the GNU Lesser General Public License | |
19 | along with systemd; If not, see <http://www.gnu.org/licenses/>. | |
20 | ***/ | |
21 | ||
22 | #include <stdlib.h> | |
23 | #include <assert.h> | |
24 | #include <errno.h> | |
39b7f596 | 25 | #include <stdio.h> |
8c00042c PF |
26 | #include <sys/types.h> |
27 | #include <sys/socket.h> | |
28 | #include <unistd.h> | |
be391925 | 29 | |
290c7324 | 30 | #include "util.h" |
8c00042c | 31 | #include "socket-util.h" |
290c7324 | 32 | |
be391925 | 33 | #include "dhcp-protocol.h" |
290c7324 | 34 | #include "dhcp-internal.h" |
be391925 PF |
35 | #include "sd-dhcp-client.h" |
36 | ||
290c7324 PF |
37 | static struct ether_addr mac_addr = { |
38 | .ether_addr_octet = {'A', 'B', 'C', '1', '2', '3'} | |
39 | }; | |
40 | ||
6c8a0f07 PF |
41 | typedef int (*test_callback_recv_t)(size_t size, DHCPMessage *dhcp); |
42 | ||
ede286f9 | 43 | static bool verbose = false; |
8c00042c | 44 | static int test_fd[2]; |
6c8a0f07 | 45 | static test_callback_recv_t callback_recv; |
234ed3eb | 46 | static be32_t xid; |
8fc8e3ab PF |
47 | static sd_event_source *test_hangcheck; |
48 | ||
49 | static int test_dhcp_hangcheck(sd_event_source *s, uint64_t usec, | |
50 | void *userdata) | |
51 | { | |
52 | assert_not_reached("Test case should have completed in 2 seconds"); | |
53 | ||
54 | return 0; | |
55 | } | |
8c00042c | 56 | |
d3d8ac2f | 57 | static void test_request_basic(sd_event *e) |
be391925 | 58 | { |
b25ef18b TG |
59 | int r; |
60 | ||
be391925 PF |
61 | sd_dhcp_client *client; |
62 | ||
ede286f9 PF |
63 | if (verbose) |
64 | printf("* %s\n", __FUNCTION__); | |
65 | ||
b25ef18b | 66 | r = sd_dhcp_client_new(&client); |
be391925 | 67 | |
12e0f830 TG |
68 | assert_se(r >= 0); |
69 | assert_se(client); | |
be391925 | 70 | |
b25ef18b | 71 | r = sd_dhcp_client_attach_event(client, e, 0); |
12e0f830 | 72 | assert_se(r >= 0); |
b25ef18b | 73 | |
12e0f830 TG |
74 | assert_se(sd_dhcp_client_set_request_option(NULL, 0) == -EINVAL); |
75 | assert_se(sd_dhcp_client_set_request_address(NULL, NULL) == -EINVAL); | |
76 | assert_se(sd_dhcp_client_set_index(NULL, 0) == -EINVAL); | |
be391925 | 77 | |
12e0f830 TG |
78 | assert_se(sd_dhcp_client_set_index(client, 15) == 0); |
79 | assert_se(sd_dhcp_client_set_index(client, -42) == -EINVAL); | |
80 | assert_se(sd_dhcp_client_set_index(client, -1) == 0); | |
be391925 | 81 | |
12e0f830 | 82 | assert_se(sd_dhcp_client_set_request_option(client, |
be391925 | 83 | DHCP_OPTION_SUBNET_MASK) == -EEXIST); |
12e0f830 | 84 | assert_se(sd_dhcp_client_set_request_option(client, |
be391925 | 85 | DHCP_OPTION_ROUTER) == -EEXIST); |
12e0f830 | 86 | assert_se(sd_dhcp_client_set_request_option(client, |
be391925 | 87 | DHCP_OPTION_HOST_NAME) == -EEXIST); |
12e0f830 | 88 | assert_se(sd_dhcp_client_set_request_option(client, |
be391925 | 89 | DHCP_OPTION_DOMAIN_NAME) == -EEXIST); |
12e0f830 | 90 | assert_se(sd_dhcp_client_set_request_option(client, |
be391925 PF |
91 | DHCP_OPTION_DOMAIN_NAME_SERVER) |
92 | == -EEXIST); | |
12e0f830 | 93 | assert_se(sd_dhcp_client_set_request_option(client, |
be391925 PF |
94 | DHCP_OPTION_NTP_SERVER) == -EEXIST); |
95 | ||
12e0f830 | 96 | assert_se(sd_dhcp_client_set_request_option(client, |
be391925 | 97 | DHCP_OPTION_PAD) == -EINVAL); |
12e0f830 | 98 | assert_se(sd_dhcp_client_set_request_option(client, |
be391925 | 99 | DHCP_OPTION_END) == -EINVAL); |
12e0f830 | 100 | assert_se(sd_dhcp_client_set_request_option(client, |
be391925 | 101 | DHCP_OPTION_MESSAGE_TYPE) == -EINVAL); |
12e0f830 | 102 | assert_se(sd_dhcp_client_set_request_option(client, |
be391925 | 103 | DHCP_OPTION_OVERLOAD) == -EINVAL); |
12e0f830 | 104 | assert_se(sd_dhcp_client_set_request_option(client, |
be391925 PF |
105 | DHCP_OPTION_PARAMETER_REQUEST_LIST) |
106 | == -EINVAL); | |
107 | ||
12e0f830 TG |
108 | assert_se(sd_dhcp_client_set_request_option(client, 33) == 0); |
109 | assert_se(sd_dhcp_client_set_request_option(client, 33) == -EEXIST); | |
110 | assert_se(sd_dhcp_client_set_request_option(client, 44) == 0); | |
111 | assert_se(sd_dhcp_client_set_request_option(client, 33) == -EEXIST); | |
be391925 PF |
112 | } |
113 | ||
39b7f596 PF |
114 | static void test_checksum(void) |
115 | { | |
116 | uint8_t buf[20] = { | |
117 | 0x45, 0x00, 0x02, 0x40, 0x00, 0x00, 0x00, 0x00, | |
118 | 0x40, 0x11, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, | |
119 | 0xff, 0xff, 0xff, 0xff | |
120 | }; | |
121 | ||
ede286f9 PF |
122 | if (verbose) |
123 | printf("* %s\n", __FUNCTION__); | |
124 | ||
a838c939 | 125 | assert_se(dhcp_packet_checksum(&buf, 20) == be16toh(0x78ae)); |
39b7f596 PF |
126 | } |
127 | ||
290c7324 PF |
128 | static int check_options(uint8_t code, uint8_t len, const uint8_t *option, |
129 | void *user_data) | |
130 | { | |
131 | return 0; | |
132 | } | |
133 | ||
8c00042c PF |
134 | int dhcp_network_send_raw_socket(int s, const union sockaddr_union *link, |
135 | const void *packet, size_t len) | |
290c7324 PF |
136 | { |
137 | size_t size; | |
f937d3d6 | 138 | _cleanup_free_ DHCPPacket *discover; |
290c7324 | 139 | uint16_t ip_check, udp_check; |
290c7324 | 140 | |
12e0f830 TG |
141 | assert_se(s >= 0); |
142 | assert_se(packet); | |
290c7324 PF |
143 | |
144 | size = sizeof(DHCPPacket) + 4; | |
12e0f830 | 145 | assert_se(len > size); |
290c7324 PF |
146 | |
147 | discover = memdup(packet, len); | |
148 | ||
12e0f830 TG |
149 | assert_se(discover->ip.ttl == IPDEFTTL); |
150 | assert_se(discover->ip.protocol == IPPROTO_UDP); | |
151 | assert_se(discover->ip.saddr == INADDR_ANY); | |
152 | assert_se(discover->ip.daddr == INADDR_BROADCAST); | |
153 | assert_se(discover->udp.source == be16toh(DHCP_PORT_CLIENT)); | |
154 | assert_se(discover->udp.dest == be16toh(DHCP_PORT_SERVER)); | |
290c7324 PF |
155 | |
156 | ip_check = discover->ip.check; | |
157 | ||
158 | discover->ip.ttl = 0; | |
159 | discover->ip.check = discover->udp.len; | |
160 | ||
a838c939 | 161 | udp_check = ~dhcp_packet_checksum(&discover->ip.ttl, len - 8); |
12e0f830 | 162 | assert_se(udp_check == 0xffff); |
290c7324 PF |
163 | |
164 | discover->ip.ttl = IPDEFTTL; | |
165 | discover->ip.check = ip_check; | |
166 | ||
a838c939 | 167 | ip_check = ~dhcp_packet_checksum(&discover->ip, sizeof(discover->ip)); |
12e0f830 | 168 | assert_se(ip_check == 0xffff); |
290c7324 | 169 | |
12e0f830 TG |
170 | assert_se(discover->dhcp.xid); |
171 | assert_se(memcmp(discover->dhcp.chaddr, | |
6c8a0f07 PF |
172 | &mac_addr.ether_addr_octet, 6) == 0); |
173 | ||
290c7324 PF |
174 | size = len - sizeof(struct iphdr) - sizeof(struct udphdr); |
175 | ||
12e0f830 | 176 | assert_se(callback_recv); |
6c8a0f07 | 177 | callback_recv(size, &discover->dhcp); |
290c7324 PF |
178 | |
179 | return 575; | |
180 | } | |
181 | ||
8c00042c PF |
182 | int dhcp_network_bind_raw_socket(int index, union sockaddr_union *link) |
183 | { | |
184 | if (socketpair(AF_UNIX, SOCK_STREAM, 0, test_fd) < 0) | |
185 | return -errno; | |
186 | ||
187 | return test_fd[0]; | |
188 | } | |
189 | ||
080ab276 | 190 | int dhcp_network_bind_udp_socket(int index, be32_t address, uint16_t port) |
234fc2df PF |
191 | { |
192 | return 0; | |
193 | } | |
194 | ||
080ab276 | 195 | int dhcp_network_send_udp_socket(int s, be32_t address, uint16_t port, |
234fc2df PF |
196 | const void *packet, size_t len) |
197 | { | |
198 | return 0; | |
199 | } | |
200 | ||
6c8a0f07 PF |
201 | static int test_discover_message_verify(size_t size, struct DHCPMessage *dhcp) |
202 | { | |
203 | int res; | |
204 | ||
205 | res = dhcp_option_parse(dhcp, size, check_options, NULL); | |
12e0f830 | 206 | assert_se(res == DHCP_DISCOVER); |
6c8a0f07 PF |
207 | |
208 | if (verbose) | |
209 | printf(" recv DHCP Discover 0x%08x\n", be32toh(dhcp->xid)); | |
210 | ||
211 | return 0; | |
212 | } | |
213 | ||
d3d8ac2f | 214 | static void test_discover_message(sd_event *e) |
290c7324 PF |
215 | { |
216 | sd_dhcp_client *client; | |
b25ef18b | 217 | int res, r; |
290c7324 | 218 | |
ede286f9 PF |
219 | if (verbose) |
220 | printf("* %s\n", __FUNCTION__); | |
221 | ||
b25ef18b | 222 | r = sd_dhcp_client_new(&client); |
12e0f830 TG |
223 | assert_se(r >= 0); |
224 | assert_se(client); | |
290c7324 | 225 | |
b25ef18b | 226 | r = sd_dhcp_client_attach_event(client, e, 0); |
12e0f830 | 227 | assert_se(r >= 0); |
b25ef18b | 228 | |
12e0f830 TG |
229 | assert_se(sd_dhcp_client_set_index(client, 42) >= 0); |
230 | assert_se(sd_dhcp_client_set_mac(client, &mac_addr) >= 0); | |
290c7324 | 231 | |
12e0f830 | 232 | assert_se(sd_dhcp_client_set_request_option(client, 248) >= 0); |
290c7324 | 233 | |
6c8a0f07 PF |
234 | callback_recv = test_discover_message_verify; |
235 | ||
290c7324 PF |
236 | res = sd_dhcp_client_start(client); |
237 | ||
12e0f830 | 238 | assert_se(res == 0 || res == -EINPROGRESS); |
8c00042c | 239 | |
6c8a0f07 PF |
240 | sd_event_run(e, (uint64_t) -1); |
241 | ||
242 | sd_dhcp_client_stop(client); | |
243 | sd_dhcp_client_free(client); | |
244 | ||
8c00042c PF |
245 | close(test_fd[0]); |
246 | close(test_fd[1]); | |
6c8a0f07 PF |
247 | |
248 | callback_recv = NULL; | |
290c7324 PF |
249 | } |
250 | ||
234ed3eb PF |
251 | static uint8_t test_addr_acq_offer[] = { |
252 | 0x45, 0x10, 0x01, 0x48, 0x00, 0x00, 0x00, 0x00, | |
253 | 0x80, 0x11, 0xb3, 0x84, 0xc0, 0xa8, 0x02, 0x01, | |
254 | 0xc0, 0xa8, 0x02, 0xbf, 0x00, 0x43, 0x00, 0x44, | |
255 | 0x01, 0x34, 0x00, 0x00, 0x02, 0x01, 0x06, 0x00, | |
256 | 0x6f, 0x95, 0x2f, 0x30, 0x00, 0x00, 0x00, 0x00, | |
257 | 0x00, 0x00, 0x00, 0x00, 0xc0, 0xa8, 0x02, 0xbf, | |
258 | 0xc0, 0xa8, 0x02, 0x01, 0x00, 0x00, 0x00, 0x00, | |
259 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, | |
260 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, | |
261 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, | |
262 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, | |
263 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, | |
264 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, | |
265 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, | |
266 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, | |
267 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, | |
268 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, | |
269 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, | |
270 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, | |
271 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, | |
272 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, | |
273 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, | |
274 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, | |
275 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, | |
276 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, | |
277 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, | |
278 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, | |
279 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, | |
280 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, | |
281 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, | |
282 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, | |
283 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, | |
284 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, | |
285 | 0x63, 0x82, 0x53, 0x63, 0x35, 0x01, 0x02, 0x36, | |
286 | 0x04, 0xc0, 0xa8, 0x02, 0x01, 0x33, 0x04, 0x00, | |
287 | 0x00, 0x02, 0x58, 0x01, 0x04, 0xff, 0xff, 0xff, | |
288 | 0x00, 0x2a, 0x04, 0xc0, 0xa8, 0x02, 0x01, 0x0f, | |
289 | 0x09, 0x6c, 0x61, 0x62, 0x2e, 0x69, 0x6e, 0x74, | |
290 | 0x72, 0x61, 0x03, 0x04, 0xc0, 0xa8, 0x02, 0x01, | |
291 | 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, | |
292 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, | |
293 | }; | |
294 | ||
295 | static uint8_t test_addr_acq_ack[] = { | |
296 | 0x45, 0x10, 0x01, 0x48, 0x00, 0x00, 0x00, 0x00, | |
297 | 0x80, 0x11, 0xb3, 0x84, 0xc0, 0xa8, 0x02, 0x01, | |
298 | 0xc0, 0xa8, 0x02, 0xbf, 0x00, 0x43, 0x00, 0x44, | |
299 | 0x01, 0x34, 0x00, 0x00, 0x02, 0x01, 0x06, 0x00, | |
300 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, | |
301 | 0x00, 0x00, 0x00, 0x00, 0xc0, 0xa8, 0x02, 0xbf, | |
302 | 0xc0, 0xa8, 0x02, 0x01, 0x00, 0x00, 0x00, 0x00, | |
303 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, | |
304 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, | |
305 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, | |
306 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, | |
307 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, | |
308 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, | |
309 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, | |
310 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, | |
311 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, | |
312 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, | |
313 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, | |
314 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, | |
315 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, | |
316 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, | |
317 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, | |
318 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, | |
319 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, | |
320 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, | |
321 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, | |
322 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, | |
323 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, | |
324 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, | |
325 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, | |
326 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, | |
327 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, | |
328 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, | |
329 | 0x63, 0x82, 0x53, 0x63, 0x35, 0x01, 0x05, 0x36, | |
330 | 0x04, 0xc0, 0xa8, 0x02, 0x01, 0x33, 0x04, 0x00, | |
331 | 0x00, 0x02, 0x58, 0x01, 0x04, 0xff, 0xff, 0xff, | |
332 | 0x00, 0x2a, 0x04, 0xc0, 0xa8, 0x02, 0x01, 0x0f, | |
333 | 0x09, 0x6c, 0x61, 0x62, 0x2e, 0x69, 0x6e, 0x74, | |
334 | 0x72, 0x61, 0x03, 0x04, 0xc0, 0xa8, 0x02, 0x01, | |
335 | 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, | |
336 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, | |
337 | }; | |
338 | ||
339 | static void test_addr_acq_acquired(sd_dhcp_client *client, int event, | |
340 | void *userdata) | |
341 | { | |
342 | sd_event *e = userdata; | |
343 | sd_dhcp_lease *lease; | |
344 | struct in_addr addr; | |
345 | ||
12e0f830 TG |
346 | assert_se(client); |
347 | assert_se(event == DHCP_EVENT_IP_ACQUIRE); | |
234ed3eb | 348 | |
12e0f830 TG |
349 | assert_se(sd_dhcp_client_get_lease(client, &lease) >= 0); |
350 | assert_se(lease); | |
234ed3eb | 351 | |
12e0f830 TG |
352 | assert_se(sd_dhcp_lease_get_address(lease, &addr) >= 0); |
353 | assert_se(memcmp(&addr.s_addr, &test_addr_acq_ack[44], | |
234ed3eb PF |
354 | sizeof(addr.s_addr)) == 0); |
355 | ||
12e0f830 TG |
356 | assert_se(sd_dhcp_lease_get_netmask(lease, &addr) >= 0); |
357 | assert_se(memcmp(&addr.s_addr, &test_addr_acq_ack[285], | |
234ed3eb PF |
358 | sizeof(addr.s_addr)) == 0); |
359 | ||
12e0f830 TG |
360 | assert_se(sd_dhcp_lease_get_router(lease, &addr) >= 0); |
361 | assert_se(memcmp(&addr.s_addr, &test_addr_acq_ack[308], | |
234ed3eb PF |
362 | sizeof(addr.s_addr)) == 0); |
363 | ||
364 | if (verbose) | |
365 | printf(" DHCP address acquired\n"); | |
366 | ||
367 | sd_event_exit(e, 0); | |
368 | } | |
369 | ||
370 | static int test_addr_acq_recv_request(size_t size, DHCPMessage *request) | |
371 | { | |
372 | uint16_t udp_check = 0; | |
373 | int res; | |
374 | ||
375 | res = dhcp_option_parse(request, size, check_options, NULL); | |
12e0f830 TG |
376 | assert_se(res == DHCP_REQUEST); |
377 | assert_se(xid == request->xid); | |
234ed3eb PF |
378 | |
379 | if (verbose) | |
380 | printf(" recv DHCP Request 0x%08x\n", be32toh(xid)); | |
381 | ||
382 | memcpy(&test_addr_acq_ack[26], &udp_check, sizeof(udp_check)); | |
383 | memcpy(&test_addr_acq_ack[32], &xid, sizeof(xid)); | |
384 | memcpy(&test_addr_acq_ack[56], &mac_addr.ether_addr_octet, | |
385 | ETHER_ADDR_LEN); | |
386 | ||
387 | callback_recv = NULL; | |
388 | ||
389 | res = write(test_fd[1], test_addr_acq_ack, | |
390 | sizeof(test_addr_acq_ack)); | |
12e0f830 | 391 | assert_se(res == sizeof(test_addr_acq_ack)); |
234ed3eb PF |
392 | |
393 | if (verbose) | |
394 | printf(" send DHCP Ack\n"); | |
395 | ||
396 | return 0; | |
397 | }; | |
398 | ||
399 | static int test_addr_acq_recv_discover(size_t size, DHCPMessage *discover) | |
400 | { | |
401 | uint16_t udp_check = 0; | |
402 | int res; | |
403 | ||
404 | res = dhcp_option_parse(discover, size, check_options, NULL); | |
12e0f830 | 405 | assert_se(res == DHCP_DISCOVER); |
234ed3eb PF |
406 | |
407 | xid = discover->xid; | |
408 | ||
409 | if (verbose) | |
410 | printf(" recv DHCP Discover 0x%08x\n", be32toh(xid)); | |
411 | ||
412 | memcpy(&test_addr_acq_offer[26], &udp_check, sizeof(udp_check)); | |
413 | memcpy(&test_addr_acq_offer[32], &xid, sizeof(xid)); | |
414 | memcpy(&test_addr_acq_offer[56], &mac_addr.ether_addr_octet, | |
415 | ETHER_ADDR_LEN); | |
416 | ||
417 | callback_recv = test_addr_acq_recv_request; | |
418 | ||
419 | res = write(test_fd[1], test_addr_acq_offer, | |
420 | sizeof(test_addr_acq_offer)); | |
12e0f830 | 421 | assert_se(res == sizeof(test_addr_acq_offer)); |
234ed3eb PF |
422 | |
423 | if (verbose) | |
424 | printf(" send DHCP Offer\n"); | |
425 | ||
426 | return 0; | |
427 | } | |
428 | ||
429 | static void test_addr_acq(sd_event *e) | |
430 | { | |
8fc8e3ab | 431 | usec_t time_now = now(CLOCK_MONOTONIC); |
234ed3eb PF |
432 | sd_dhcp_client *client; |
433 | int res, r; | |
434 | ||
435 | if (verbose) | |
436 | printf("* %s\n", __FUNCTION__); | |
437 | ||
438 | r = sd_dhcp_client_new(&client); | |
12e0f830 TG |
439 | assert_se(r >= 0); |
440 | assert_se(client); | |
234ed3eb PF |
441 | |
442 | r = sd_dhcp_client_attach_event(client, e, 0); | |
12e0f830 | 443 | assert_se(r >= 0); |
234ed3eb | 444 | |
12e0f830 TG |
445 | assert_se(sd_dhcp_client_set_index(client, 42) >= 0); |
446 | assert_se(sd_dhcp_client_set_mac(client, &mac_addr) >= 0); | |
234ed3eb | 447 | |
12e0f830 | 448 | assert_se(sd_dhcp_client_set_callback(client, test_addr_acq_acquired, e) |
234ed3eb PF |
449 | >= 0); |
450 | ||
451 | callback_recv = test_addr_acq_recv_discover; | |
452 | ||
8fc8e3ab PF |
453 | assert_se(sd_event_add_monotonic(e, &test_hangcheck, |
454 | time_now + 2 * USEC_PER_SEC, 0, | |
455 | test_dhcp_hangcheck, NULL) >= 0); | |
456 | ||
234ed3eb | 457 | res = sd_dhcp_client_start(client); |
12e0f830 | 458 | assert_se(res == 0 || res == -EINPROGRESS); |
234ed3eb PF |
459 | |
460 | sd_event_loop(e); | |
461 | ||
8fc8e3ab PF |
462 | test_hangcheck = sd_event_source_unref(test_hangcheck); |
463 | ||
234ed3eb PF |
464 | sd_dhcp_client_set_callback(client, NULL, NULL); |
465 | sd_dhcp_client_stop(client); | |
466 | sd_dhcp_client_free(client); | |
467 | ||
468 | close(test_fd[0]); | |
469 | close(test_fd[1]); | |
470 | ||
471 | callback_recv = NULL; | |
472 | xid = 0; | |
473 | } | |
474 | ||
be391925 PF |
475 | int main(int argc, char *argv[]) |
476 | { | |
d3d8ac2f PF |
477 | sd_event *e; |
478 | ||
12e0f830 | 479 | assert_se(sd_event_new(&e) >= 0); |
d3d8ac2f PF |
480 | |
481 | test_request_basic(e); | |
39b7f596 | 482 | test_checksum(); |
be391925 | 483 | |
d3d8ac2f | 484 | test_discover_message(e); |
234ed3eb | 485 | test_addr_acq(e); |
290c7324 | 486 | |
be391925 PF |
487 | return 0; |
488 | } |