| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
/*** |
| 3 |
Copyright © 2013 Intel Corporation. All rights reserved. |
| 4 |
***/ |
| 5 |
|
| 6 |
#include <net/if_arp.h> |
| 7 |
#include <stdlib.h> |
| 8 |
#include <sys/socket.h> |
| 9 |
#include <unistd.h> |
| 10 |
|
| 11 |
#include "sd-dhcp-client.h" |
| 12 |
#include "sd-dhcp-lease.h" |
| 13 |
#include "sd-event.h" |
| 14 |
|
| 15 |
#include "dhcp-client-internal.h" |
| 16 |
#include "dhcp-message.h" |
| 17 |
#include "ether-addr-util.h" |
| 18 |
#include "fd-util.h" |
| 19 |
#include "hashmap.h" |
| 20 |
#include "in-addr-util.h" |
| 21 |
#include "iovec-util.h" |
| 22 |
#include "iovec-wrapper.h" |
| 23 |
#include "ip-util.h" |
| 24 |
#include "log.h" |
| 25 |
#include "set.h" |
| 26 |
#include "socket-util.h" |
| 27 |
#include "strv.h" |
| 28 |
#include "tests.h" |
| 29 |
|
| 30 |
static const struct hw_addr_data hw_addr = { |
| 31 |
.length = ETH_ALEN, |
| 32 |
.ether = {{ 'A', 'B', 'C', '1', '2', '3' }}, |
| 33 |
}, bcast_addr = { |
| 34 |
.length = ETH_ALEN, |
| 35 |
.ether = {{ 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF }}, |
| 36 |
}; |
| 37 |
|
| 38 |
static const uint16_t server_port = 1067; |
| 39 |
static const uint16_t client_port = 1068; |
| 40 |
static const int ip_service_type = IPTOS_CLASS_CS3; |
| 41 |
|
| 42 |
static const union in_addr_union prefix = { |
| 43 |
.bytes = { 198, 51, 100, 0 }, |
| 44 |
}, server_address = { |
| 45 |
.bytes = { 198, 51, 100, 1 }, |
| 46 |
}, client_address = { |
| 47 |
.bytes = { 198, 51, 100, 100 }, |
| 48 |
}, broadcast = { |
| 49 |
.bytes = { 198, 51, 100, 255 }, |
| 50 |
}, netmask = { |
| 51 |
.bytes = { 255, 255, 255, 0 }, |
| 52 |
}; |
| 53 |
|
| 54 |
static const usec_t lifetime = USEC_PER_DAY; |
| 55 |
|
| 56 |
static const sd_dhcp_client_id client_id_generic = { |
| 57 |
.size = 10, |
| 58 |
.id.type = 0, |
| 59 |
.id.data = { 1, 2, 3, 4, 5, 6, 7, 8, 9, }, |
| 60 |
}; |
| 61 |
|
| 62 |
/* options sent by client */ |
| 63 |
static const char * const *user_class_strv = STRV_MAKE_CONST("user-class-hoge", "user-class-foo"); |
| 64 |
static const char * const *vendor_specific_strv = STRV_MAKE_CONST("vendor-specific-hoge", "vendor-specific-foo"); |
| 65 |
static const char *vendor_class = "vendor-class"; |
| 66 |
static const char *mud_url = "https://mud-url.example.com"; |
| 67 |
static const char *hostname = "hogehoge.example.com"; |
| 68 |
static const uint32_t mtu = 3000; |
| 69 |
static const char *extra_option_163 = "private_option_163"; |
| 70 |
static const char *extra_option_164 = "private_option_164"; |
| 71 |
|
| 72 |
static void setup(sd_event_io_handler_t io_handler, sd_dhcp_client_callback_t client_handler, sd_dhcp_client **ret) { |
| 73 |
_cleanup_(sd_event_unrefp) sd_event *e = NULL; |
| 74 |
ASSERT_OK(sd_event_new(&e)); |
| 75 |
ASSERT_NOT_NULL(e); |
| 76 |
|
| 77 |
_cleanup_close_pair_ int socket_fd[2] = EBADF_PAIR; |
| 78 |
ASSERT_OK_ERRNO(socketpair(AF_UNIX, SOCK_SEQPACKET | SOCK_CLOEXEC | SOCK_NONBLOCK, 0, socket_fd)); |
| 79 |
|
| 80 |
_cleanup_(sd_dhcp_client_unrefp) sd_dhcp_client *client = NULL; |
| 81 |
ASSERT_OK(sd_dhcp_client_new(&client)); |
| 82 |
|
| 83 |
client->socket_fd = TAKE_FD(socket_fd[0]); |
| 84 |
|
| 85 |
ASSERT_OK(sd_dhcp_client_attach_event(client, e, SD_EVENT_PRIORITY_NORMAL)); |
| 86 |
ASSERT_OK(sd_dhcp_client_set_ifindex(client, 42)); |
| 87 |
ASSERT_OK(sd_dhcp_client_set_mac(client, hw_addr.bytes, bcast_addr.bytes, hw_addr.length, ARPHRD_ETHER)); |
| 88 |
ASSERT_OK(sd_dhcp_client_set_callback(client, client_handler, e)); |
| 89 |
ASSERT_OK(sd_dhcp_client_set_port(client, server_port)); |
| 90 |
ASSERT_OK(sd_dhcp_client_set_client_port(client, client_port)); |
| 91 |
ASSERT_OK(sd_dhcp_client_set_ip_service_type(client, ip_service_type)); |
| 92 |
|
| 93 |
/* options */ |
| 94 |
for (uint8_t i = 178; i <= 207; i++) /* These are currently unassigned. See sd-dhcp-protocol.h. */ |
| 95 |
ASSERT_OK(sd_dhcp_client_set_request_option(client, i)); |
| 96 |
|
| 97 |
ASSERT_OK(sd_dhcp_client_set_client_id(client, client_id_generic.id.type, client_id_generic.id.data, client_id_generic.size - 1)); |
| 98 |
ASSERT_OK(sd_dhcp_client_set_mtu(client, mtu)); |
| 99 |
ASSERT_OK(sd_dhcp_client_set_mud_url(client, mud_url)); |
| 100 |
ASSERT_OK(sd_dhcp_client_set_hostname(client, hostname)); |
| 101 |
ASSERT_OK(sd_dhcp_client_set_vendor_class_identifier(client, vendor_class)); |
| 102 |
|
| 103 |
_cleanup_(tlv_unrefp) TLV *vendor_specific = |
| 104 |
ASSERT_NOT_NULL(tlv_new(TLV_DHCP4_SUBOPTION)); |
| 105 |
uint8_t c = 0; |
| 106 |
STRV_FOREACH(s, vendor_specific_strv) |
| 107 |
ASSERT_OK(tlv_append(vendor_specific, ++c, strlen(*s), *s)); |
| 108 |
ASSERT_OK(dhcp_client_set_vendor_options(client, vendor_specific)); |
| 109 |
|
| 110 |
_cleanup_(iovw_done) struct iovec_wrapper iovw = {}; |
| 111 |
STRV_FOREACH(s, user_class_strv) |
| 112 |
ASSERT_OK(iovw_put(&iovw, (void*) *s, strlen(*s))); |
| 113 |
ASSERT_OK(dhcp_client_set_user_class(client, &iovw)); |
| 114 |
|
| 115 |
_cleanup_(tlv_unrefp) TLV *extra_options = ASSERT_NOT_NULL(tlv_new(TLV_DHCP4)); |
| 116 |
ASSERT_OK(tlv_append(extra_options, 163, strlen(extra_option_163), extra_option_163)); |
| 117 |
ASSERT_OK(tlv_append(extra_options, 164, strlen(extra_option_164), extra_option_164)); |
| 118 |
ASSERT_OK(dhcp_client_set_extra_options(client, extra_options)); |
| 119 |
|
| 120 |
/* IO event source for the fake server side */ |
| 121 |
_cleanup_(sd_event_source_unrefp) sd_event_source *s = NULL; |
| 122 |
ASSERT_OK(sd_event_add_io(e, &s, socket_fd[1], EPOLLIN, io_handler, client)); |
| 123 |
ASSERT_OK(sd_event_source_set_priority(s, SD_EVENT_PRIORITY_IMPORTANT)); |
| 124 |
ASSERT_OK(sd_event_source_set_description(s, "fake-server-io-event-source")); |
| 125 |
ASSERT_OK(sd_event_source_set_io_fd_own(s, true)); |
| 126 |
TAKE_FD(socket_fd[1]); |
| 127 |
ASSERT_OK(sd_event_source_set_floating(s, true)); |
| 128 |
|
| 129 |
*ret = TAKE_PTR(client); |
| 130 |
} |
| 131 |
|
| 132 |
static void receive_message(int fd, bool raw, bool check_xid, sd_dhcp_client *client, sd_dhcp_message **ret) { |
| 133 |
ssize_t buflen = ASSERT_OK_POSITIVE(next_datagram_size_fd(fd)); |
| 134 |
_cleanup_free_ void *buf = ASSERT_NOT_NULL(malloc0(buflen)); |
| 135 |
|
| 136 |
struct msghdr msg = { |
| 137 |
.msg_iov = &IOVEC_MAKE(buf, buflen), |
| 138 |
.msg_iovlen = 1, |
| 139 |
}; |
| 140 |
ssize_t len = ASSERT_OK_ERRNO(recvmsg_safe(fd, &msg, MSG_DONTWAIT)); |
| 141 |
|
| 142 |
struct iovec payload = IOVEC_MAKE(buf, len); |
| 143 |
if (raw) |
| 144 |
ASSERT_OK(udp_packet_verify( |
| 145 |
&payload, |
| 146 |
client->server_port, |
| 147 |
/* checksum= */ true, |
| 148 |
&payload)); |
| 149 |
|
| 150 |
ASSERT_OK(dhcp_message_parse( |
| 151 |
&payload, |
| 152 |
BOOTREQUEST, |
| 153 |
check_xid ? &client->xid : NULL, |
| 154 |
ARPHRD_ETHER, |
| 155 |
&hw_addr, |
| 156 |
ret)); |
| 157 |
} |
| 158 |
|
| 159 |
static void iovw_send(int fd, struct iovec_wrapper *iovw) { |
| 160 |
struct msghdr mh = { |
| 161 |
.msg_iov = iovw->iovec, |
| 162 |
.msg_iovlen = iovw->count, |
| 163 |
}; |
| 164 |
ASSERT_OK_ERRNO(sendmsg(fd, &mh, MSG_NOSIGNAL)); |
| 165 |
} |
| 166 |
|
| 167 |
static void send_message(int fd, bool raw, sd_dhcp_client *client, sd_dhcp_message *m) { |
| 168 |
_cleanup_(iovw_done_free) struct iovec_wrapper payload = {}; |
| 169 |
ASSERT_OK(dhcp_message_build(m, &payload)); |
| 170 |
|
| 171 |
if (!raw) { |
| 172 |
iovw_send(fd, &payload); |
| 173 |
return; |
| 174 |
} |
| 175 |
|
| 176 |
struct iphdr ip; |
| 177 |
struct udphdr udp; |
| 178 |
ASSERT_OK(udp_packet_build( |
| 179 |
server_address.in.s_addr, |
| 180 |
client->server_port, |
| 181 |
m->header.yiaddr, |
| 182 |
client->port, |
| 183 |
client->ip_service_type, |
| 184 |
&payload, |
| 185 |
&ip, |
| 186 |
&udp)); |
| 187 |
|
| 188 |
_cleanup_(iovw_done) struct iovec_wrapper iovw = {}; |
| 189 |
ASSERT_OK(iovw_put(&iovw, &ip, sizeof(struct iphdr))); |
| 190 |
ASSERT_OK(iovw_put(&iovw, &udp, sizeof(struct udphdr))); |
| 191 |
ASSERT_OK(iovw_put_iovw(&iovw, &payload)); |
| 192 |
iovw_send(fd, &iovw); |
| 193 |
} |
| 194 |
|
| 195 |
static void create_reply(sd_dhcp_client *client, sd_dhcp_message *request, uint8_t type, sd_dhcp_message **ret) { |
| 196 |
assert(ret); |
| 197 |
|
| 198 |
struct hw_addr_data hw; |
| 199 |
ASSERT_OK(dhcp_message_get_hw_addr(request, &hw)); |
| 200 |
|
| 201 |
_cleanup_(sd_dhcp_message_unrefp) sd_dhcp_message *m = NULL; |
| 202 |
ASSERT_OK(dhcp_message_new(&m)); |
| 203 |
ASSERT_OK(dhcp_message_init_header( |
| 204 |
m, |
| 205 |
BOOTREPLY, |
| 206 |
be32toh(request->header.xid), |
| 207 |
request->header.htype, |
| 208 |
&hw)); |
| 209 |
|
| 210 |
if (client->bootp) { |
| 211 |
m->header.yiaddr = client_address.in.s_addr; |
| 212 |
m->header.siaddr = server_address.in.s_addr; |
| 213 |
ASSERT_OK(dhcp_message_append_option_address(m, SD_DHCP_OPTION_SUBNET_MASK, &netmask.in)); |
| 214 |
ASSERT_OK(dhcp_message_append_option_address(m, SD_DHCP_OPTION_BROADCAST, &broadcast.in)); |
| 215 |
|
| 216 |
*ret = TAKE_PTR(m); |
| 217 |
return; |
| 218 |
} |
| 219 |
|
| 220 |
ASSERT_OK(dhcp_message_append_option_u8(m, SD_DHCP_OPTION_MESSAGE_TYPE, type)); |
| 221 |
ASSERT_OK(dhcp_message_append_option_address(m, SD_DHCP_OPTION_SERVER_IDENTIFIER, &server_address.in)); |
| 222 |
|
| 223 |
switch (type) { |
| 224 |
case DHCP_OFFER: |
| 225 |
case DHCP_ACK: |
| 226 |
m->header.yiaddr = client_address.in.s_addr; |
| 227 |
ASSERT_OK(dhcp_message_append_option_address(m, SD_DHCP_OPTION_SUBNET_MASK, &netmask.in)); |
| 228 |
ASSERT_OK(dhcp_message_append_option_address(m, SD_DHCP_OPTION_BROADCAST, &broadcast.in)); |
| 229 |
ASSERT_OK(dhcp_message_append_option_be32(m, SD_DHCP_OPTION_IP_ADDRESS_LEASE_TIME, usec_to_be32_sec(lifetime))); |
| 230 |
/* The following two options are intentionally set with spurious values, to test the adjusting logic. */ |
| 231 |
ASSERT_OK(dhcp_message_append_option_be32(m, SD_DHCP_OPTION_REBINDING_TIME, usec_to_be32_sec(lifetime + USEC_PER_SEC))); |
| 232 |
ASSERT_OK(dhcp_message_append_option_be32(m, SD_DHCP_OPTION_RENEWAL_TIME, usec_to_be32_sec(lifetime - USEC_PER_SEC))); |
| 233 |
break; |
| 234 |
|
| 235 |
case DHCP_NAK: |
| 236 |
ASSERT_OK(dhcp_message_append_option_string(m, SD_DHCP_OPTION_ERROR_MESSAGE, "test error message")); |
| 237 |
break; |
| 238 |
|
| 239 |
default: |
| 240 |
; |
| 241 |
} |
| 242 |
|
| 243 |
*ret = TAKE_PTR(m); |
| 244 |
} |
| 245 |
|
| 246 |
static void verify_header(sd_dhcp_message *m) { |
| 247 |
ASSERT_EQ(m->header.op, BOOTREQUEST); |
| 248 |
ASSERT_EQ(m->header.htype, ARPHRD_ETHER); |
| 249 |
ASSERT_EQ(memcmp_nn(m->header.chaddr, m->header.hlen, hw_addr.bytes, hw_addr.length), 0); |
| 250 |
} |
| 251 |
|
| 252 |
static void verify_basic_options(sd_dhcp_message *m, uint8_t type) { |
| 253 |
uint8_t t; |
| 254 |
ASSERT_OK(dhcp_message_get_option_u8(m, SD_DHCP_OPTION_MESSAGE_TYPE, &t)); |
| 255 |
ASSERT_EQ(t, type); |
| 256 |
|
| 257 |
sd_dhcp_client_id id; |
| 258 |
ASSERT_OK(dhcp_message_get_option_client_id(m, &id)); |
| 259 |
ASSERT_EQ(client_id_compare_func(&id, &client_id_generic), 0); |
| 260 |
} |
| 261 |
|
| 262 |
static void verify_request(sd_dhcp_message *m, uint8_t type) { |
| 263 |
verify_header(m); |
| 264 |
verify_basic_options(m, type); |
| 265 |
|
| 266 |
_cleanup_set_free_ Set *prl = NULL; |
| 267 |
ASSERT_OK(dhcp_message_get_option_parameter_request_list(m, &prl)); |
| 268 |
|
| 269 |
for (uint8_t i = 178; i <= 207; i++) |
| 270 |
ASSERT_TRUE(set_contains(prl, UINT_TO_PTR(i))); |
| 271 |
|
| 272 |
uint16_t sz; |
| 273 |
ASSERT_OK(dhcp_message_get_option_u16(m, SD_DHCP_OPTION_MAXIMUM_MESSAGE_SIZE, &sz)); |
| 274 |
ASSERT_EQ(sz, mtu); |
| 275 |
|
| 276 |
_cleanup_free_ char *str = NULL; |
| 277 |
ASSERT_OK(dhcp_message_get_option_string(m, SD_DHCP_OPTION_MUD_URL, &str)); |
| 278 |
ASSERT_STREQ(str, mud_url); |
| 279 |
|
| 280 |
str = mfree(str); |
| 281 |
ASSERT_OK(dhcp_message_get_option_hostname(m, &str)); |
| 282 |
ASSERT_STREQ(str, hostname); |
| 283 |
|
| 284 |
str = mfree(str); |
| 285 |
ASSERT_OK(dhcp_message_get_option_string(m, SD_DHCP_OPTION_VENDOR_CLASS_IDENTIFIER, &str)); |
| 286 |
ASSERT_STREQ(str, vendor_class); |
| 287 |
|
| 288 |
_cleanup_(tlv_unrefp) TLV *vendor_specific = NULL; |
| 289 |
ASSERT_OK(dhcp_message_get_option_sub_tlv( |
| 290 |
m, |
| 291 |
SD_DHCP_OPTION_VENDOR_SPECIFIC_INFORMATION, |
| 292 |
TLV_DHCP4_SUBOPTION, |
| 293 |
&vendor_specific)); |
| 294 |
ASSERT_EQ(hashmap_size(vendor_specific->entries), strv_length((char**) vendor_specific_strv)); |
| 295 |
uint8_t c = 0; |
| 296 |
STRV_FOREACH(s, vendor_specific_strv) { |
| 297 |
struct iovec v; |
| 298 |
ASSERT_OK(tlv_get(vendor_specific, ++c, &v)); |
| 299 |
ASSERT_EQ(memcmp_nn(v.iov_base, v.iov_len, *s, strlen(*s)), 0); |
| 300 |
} |
| 301 |
|
| 302 |
_cleanup_(iovw_done) struct iovec_wrapper iovw = {}; |
| 303 |
STRV_FOREACH(s, user_class_strv) |
| 304 |
ASSERT_OK(iovw_put(&iovw, (void*) *s, strlen(*s))); |
| 305 |
_cleanup_(iovw_done_free) struct iovec_wrapper user_class = {}; |
| 306 |
ASSERT_OK(dhcp_message_get_option_length_prefixed_data(m, SD_DHCP_OPTION_USER_CLASS, /* length_size= */ 1, &user_class)); |
| 307 |
ASSERT_TRUE(iovw_equal(&user_class, &iovw)); |
| 308 |
|
| 309 |
str = mfree(str); |
| 310 |
ASSERT_OK(dhcp_message_get_option_string(m, 163, &str)); |
| 311 |
ASSERT_STREQ(str, extra_option_163); |
| 312 |
|
| 313 |
str = mfree(str); |
| 314 |
ASSERT_OK(dhcp_message_get_option_string(m, 164, &str)); |
| 315 |
ASSERT_STREQ(str, extra_option_164); |
| 316 |
} |
| 317 |
|
| 318 |
static void verify_anonymized_request(sd_dhcp_message *m, uint8_t type) { |
| 319 |
verify_header(m); |
| 320 |
verify_basic_options(m, type); |
| 321 |
|
| 322 |
_cleanup_set_free_ Set *prl = NULL; |
| 323 |
ASSERT_OK(dhcp_message_get_option_parameter_request_list(m, &prl)); |
| 324 |
|
| 325 |
for (uint8_t i = 178; i <= 207; i++) |
| 326 |
ASSERT_FALSE(set_contains(prl, UINT_TO_PTR(i))); |
| 327 |
|
| 328 |
uint8_t code; |
| 329 |
FOREACH_ARGUMENT(code, |
| 330 |
SD_DHCP_OPTION_MAXIMUM_MESSAGE_SIZE, |
| 331 |
SD_DHCP_OPTION_MUD_URL, |
| 332 |
SD_DHCP_OPTION_HOST_NAME, |
| 333 |
SD_DHCP_OPTION_FQDN, |
| 334 |
SD_DHCP_OPTION_VENDOR_CLASS_IDENTIFIER, |
| 335 |
SD_DHCP_OPTION_VENDOR_SPECIFIC_INFORMATION, |
| 336 |
SD_DHCP_OPTION_USER_CLASS, |
| 337 |
163, |
| 338 |
164) |
| 339 |
ASSERT_FALSE(dhcp_message_has_option(m, code)); |
| 340 |
} |
| 341 |
|
| 342 |
static void verify_request_server_address(sd_dhcp_message *m) { |
| 343 |
struct in_addr a; |
| 344 |
ASSERT_OK(dhcp_message_get_option_address(m, SD_DHCP_OPTION_SERVER_IDENTIFIER, &a)); |
| 345 |
ASSERT_TRUE(in4_addr_equal(&a, &server_address.in)); |
| 346 |
} |
| 347 |
|
| 348 |
static void verify_request_client_address(sd_dhcp_message *m, bool header) { |
| 349 |
if (header) |
| 350 |
ASSERT_EQ(m->header.ciaddr, client_address.in.s_addr); |
| 351 |
else { |
| 352 |
struct in_addr a; |
| 353 |
ASSERT_OK(dhcp_message_get_option_address(m, SD_DHCP_OPTION_REQUESTED_IP_ADDRESS, &a)); |
| 354 |
ASSERT_TRUE(in4_addr_equal(&a, &client_address.in)); |
| 355 |
} |
| 356 |
} |
| 357 |
|
| 358 |
static void verify_reply(sd_dhcp_client *client, DHCPState state) { |
| 359 |
ASSERT_EQ(client->state, state); |
| 360 |
|
| 361 |
sd_dhcp_lease *lease; |
| 362 |
ASSERT_OK(sd_dhcp_client_get_lease(client, &lease)); |
| 363 |
|
| 364 |
struct in_addr a; |
| 365 |
ASSERT_OK(sd_dhcp_lease_get_address(lease, &a)); |
| 366 |
ASSERT_TRUE(in4_addr_equal(&a, &client_address.in)); |
| 367 |
|
| 368 |
ASSERT_OK(sd_dhcp_lease_get_server_identifier(lease, &a)); |
| 369 |
ASSERT_TRUE(in4_addr_equal(&a, &server_address.in)); |
| 370 |
|
| 371 |
ASSERT_OK(sd_dhcp_lease_get_broadcast(lease, &a)); |
| 372 |
ASSERT_TRUE(in4_addr_equal(&a, &broadcast.in)); |
| 373 |
|
| 374 |
ASSERT_OK(sd_dhcp_lease_get_netmask(lease, &a)); |
| 375 |
ASSERT_TRUE(in4_addr_equal(&a, &netmask.in)); |
| 376 |
|
| 377 |
uint8_t prefixlen; |
| 378 |
ASSERT_OK(sd_dhcp_lease_get_prefix(lease, &a, &prefixlen)); |
| 379 |
ASSERT_TRUE(in4_addr_equal(&a, &prefix.in)); |
| 380 |
ASSERT_EQ(prefixlen, 24u); |
| 381 |
|
| 382 |
if (client->bootp) { |
| 383 |
usec_t t; |
| 384 |
ASSERT_OK(sd_dhcp_lease_get_lifetime(lease, &t)); |
| 385 |
ASSERT_EQ(t, USEC_INFINITY); |
| 386 |
} else { |
| 387 |
usec_t t; |
| 388 |
ASSERT_OK(sd_dhcp_lease_get_lifetime(lease, &t)); |
| 389 |
ASSERT_EQ(t, lifetime); |
| 390 |
ASSERT_OK(sd_dhcp_lease_get_t1(lease, &t)); |
| 391 |
ASSERT_EQ(t, lifetime / 2); |
| 392 |
ASSERT_OK(sd_dhcp_lease_get_t2(lease, &t)); |
| 393 |
ASSERT_EQ(t, lifetime * 7 / 8); |
| 394 |
} |
| 395 |
} |
| 396 |
|
| 397 |
static int basic_io_handler(sd_event_source *s, int fd, uint32_t revents, void *userdata) { |
| 398 |
sd_dhcp_client *client = ASSERT_PTR(userdata); |
| 399 |
static unsigned count = 0; |
| 400 |
|
| 401 |
count++; |
| 402 |
log_debug("%s: count=%u", __func__, count); |
| 403 |
|
| 404 |
switch (count) { |
| 405 |
case 1: { |
| 406 |
_cleanup_(sd_dhcp_message_unrefp) sd_dhcp_message *request = NULL; |
| 407 |
receive_message(fd, /* raw= */ true, /* check_xid= */ true, client, &request); |
| 408 |
|
| 409 |
verify_request(request, DHCP_DISCOVER); |
| 410 |
|
| 411 |
_cleanup_(sd_dhcp_message_unrefp) sd_dhcp_message *reply = NULL; |
| 412 |
create_reply(client, request, DHCP_OFFER, &reply); |
| 413 |
|
| 414 |
send_message(fd, /* raw= */ true, client, reply); |
| 415 |
break; |
| 416 |
} |
| 417 |
case 2: { |
| 418 |
_cleanup_(sd_dhcp_message_unrefp) sd_dhcp_message *request = NULL; |
| 419 |
receive_message(fd, /* raw= */ true, /* check_xid= */ true, client, &request); |
| 420 |
|
| 421 |
/* REQUEST (selecting) */ |
| 422 |
verify_request(request, DHCP_REQUEST); |
| 423 |
verify_request_server_address(request); |
| 424 |
verify_request_client_address(request, /* header= */ false); |
| 425 |
|
| 426 |
_cleanup_(sd_dhcp_message_unrefp) sd_dhcp_message *reply = NULL; |
| 427 |
create_reply(client, request, DHCP_ACK, &reply); |
| 428 |
|
| 429 |
send_message(fd, /* raw= */ true, client, reply); |
| 430 |
break; |
| 431 |
} |
| 432 |
case 3: { |
| 433 |
/* In this stage, client is already restarted and a new xid is picked. */ |
| 434 |
_cleanup_(sd_dhcp_message_unrefp) sd_dhcp_message *request = NULL; |
| 435 |
receive_message(fd, /* raw= */ true, /* check_xid= */ false, client, &request); |
| 436 |
|
| 437 |
verify_header(request); |
| 438 |
verify_basic_options(request, DHCP_DECLINE); |
| 439 |
verify_request_server_address(request); |
| 440 |
verify_request_client_address(request, /* header= */ false); |
| 441 |
break; |
| 442 |
} |
| 443 |
case 4: { |
| 444 |
_cleanup_(sd_dhcp_message_unrefp) sd_dhcp_message *request = NULL; |
| 445 |
receive_message(fd, /* raw= */ true, /* check_xid= */ true, client, &request); |
| 446 |
|
| 447 |
verify_request(request, DHCP_DISCOVER); |
| 448 |
|
| 449 |
_cleanup_(sd_dhcp_message_unrefp) sd_dhcp_message *reply = NULL; |
| 450 |
create_reply(client, request, DHCP_OFFER, &reply); |
| 451 |
|
| 452 |
send_message(fd, /* raw= */ true, client, reply); |
| 453 |
break; |
| 454 |
} |
| 455 |
case 5: { |
| 456 |
_cleanup_(sd_dhcp_message_unrefp) sd_dhcp_message *request = NULL; |
| 457 |
receive_message(fd, /* raw= */ true, /* check_xid= */ true, client, &request); |
| 458 |
|
| 459 |
/* REQUEST (selecting) */ |
| 460 |
verify_request(request, DHCP_REQUEST); |
| 461 |
verify_request_server_address(request); |
| 462 |
verify_request_client_address(request, /* header= */ false); |
| 463 |
|
| 464 |
_cleanup_(sd_dhcp_message_unrefp) sd_dhcp_message *reply = NULL; |
| 465 |
create_reply(client, request, DHCP_ACK, &reply); |
| 466 |
|
| 467 |
send_message(fd, /* raw= */ true, client, reply); |
| 468 |
break; |
| 469 |
} |
| 470 |
case 6: { |
| 471 |
_cleanup_(sd_dhcp_message_unrefp) sd_dhcp_message *request = NULL; |
| 472 |
receive_message(fd, /* raw= */ false, /* check_xid= */ true, client, &request); |
| 473 |
|
| 474 |
/* REQUEST (renewing) */ |
| 475 |
verify_request(request, DHCP_REQUEST); |
| 476 |
verify_request_client_address(request, /* header= */ true); |
| 477 |
|
| 478 |
_cleanup_(sd_dhcp_message_unrefp) sd_dhcp_message *reply = NULL; |
| 479 |
create_reply(client, request, DHCP_ACK, &reply); |
| 480 |
|
| 481 |
send_message(fd, /* raw= */ false, client, reply); |
| 482 |
break; |
| 483 |
} |
| 484 |
case 7: { |
| 485 |
_cleanup_(sd_dhcp_message_unrefp) sd_dhcp_message *request = NULL; |
| 486 |
receive_message(fd, /* raw= */ false, /* check_xid= */ true, client, &request); |
| 487 |
|
| 488 |
/* REQUEST (renewing) */ |
| 489 |
verify_request(request, DHCP_REQUEST); |
| 490 |
verify_request_client_address(request, /* header= */ true); |
| 491 |
|
| 492 |
_cleanup_(sd_dhcp_message_unrefp) sd_dhcp_message *reply = NULL; |
| 493 |
create_reply(client, request, DHCP_ACK, &reply); |
| 494 |
|
| 495 |
send_message(fd, /* raw= */ false, client, reply); |
| 496 |
break; |
| 497 |
} |
| 498 |
case 8: { |
| 499 |
_cleanup_(sd_dhcp_message_unrefp) sd_dhcp_message *request = NULL; |
| 500 |
receive_message(fd, /* raw= */ true, /* check_xid= */ true, client, &request); |
| 501 |
|
| 502 |
/* REQUEST (rebinding) */ |
| 503 |
verify_request(request, DHCP_REQUEST); |
| 504 |
verify_request_client_address(request, /* header= */ true); |
| 505 |
|
| 506 |
_cleanup_(sd_dhcp_message_unrefp) sd_dhcp_message *reply = NULL; |
| 507 |
create_reply(client, request, DHCP_ACK, &reply); |
| 508 |
|
| 509 |
send_message(fd, /* raw= */ true, client, reply); |
| 510 |
break; |
| 511 |
} |
| 512 |
case 9: { |
| 513 |
/* In this stage, client is already stopped and the xid has been cleared. */ |
| 514 |
_cleanup_(sd_dhcp_message_unrefp) sd_dhcp_message *request = NULL; |
| 515 |
receive_message(fd, /* raw= */ false, /* check_xid= */ false, client, &request); |
| 516 |
|
| 517 |
verify_header(request); |
| 518 |
verify_basic_options(request, DHCP_RELEASE); |
| 519 |
verify_request_server_address(request); |
| 520 |
verify_request_client_address(request, /* header= */ true); |
| 521 |
|
| 522 |
ASSERT_OK(sd_event_exit(sd_dhcp_client_get_event(client), 0)); |
| 523 |
break; |
| 524 |
} |
| 525 |
default: |
| 526 |
assert_not_reached(); |
| 527 |
} |
| 528 |
|
| 529 |
return 0; |
| 530 |
} |
| 531 |
|
| 532 |
static int restart_now_defer_handler(sd_event_source *s, void *userdata) { |
| 533 |
sd_dhcp_client *client = ASSERT_PTR(userdata); |
| 534 |
|
| 535 |
ASSERT_OK(sd_event_source_set_time_relative(client->timeout_resend, /* usec= */ 0)); |
| 536 |
return 0; |
| 537 |
} |
| 538 |
|
| 539 |
static int basic_client_handler(sd_dhcp_client *client, int event, void *userdata) { |
| 540 |
sd_event *e = ASSERT_PTR(userdata); |
| 541 |
static unsigned count = 0; |
| 542 |
|
| 543 |
count++; |
| 544 |
log_debug("%s: count=%u, event=%i", __func__, count, event); |
| 545 |
|
| 546 |
switch (count) { |
| 547 |
case 1: |
| 548 |
ASSERT_EQ(event, SD_DHCP_CLIENT_EVENT_SELECTING); |
| 549 |
verify_reply(client, DHCP_STATE_SELECTING); |
| 550 |
break; |
| 551 |
case 2: |
| 552 |
ASSERT_EQ(event, SD_DHCP_CLIENT_EVENT_IP_ACQUIRE); |
| 553 |
verify_reply(client, DHCP_STATE_BOUND); |
| 554 |
/* decline the bound lease, and restart the cycle. */ |
| 555 |
ASSERT_OK(sd_dhcp_client_send_decline(client)); |
| 556 |
break; |
| 557 |
case 3: |
| 558 |
ASSERT_EQ(event, SD_DHCP_CLIENT_EVENT_EXPIRED); |
| 559 |
verify_reply(client, DHCP_STATE_BOUND); |
| 560 |
/* on decline, the client will be restarted with a delay. Let's boost the restart timer. */ |
| 561 |
ASSERT_OK(sd_event_add_defer(e, /* ret= */ NULL, restart_now_defer_handler, client)); |
| 562 |
break; |
| 563 |
case 4: |
| 564 |
ASSERT_EQ(event, SD_DHCP_CLIENT_EVENT_SELECTING); |
| 565 |
verify_reply(client, DHCP_STATE_SELECTING); |
| 566 |
break; |
| 567 |
case 5: |
| 568 |
ASSERT_EQ(event, SD_DHCP_CLIENT_EVENT_IP_ACQUIRE); |
| 569 |
verify_reply(client, DHCP_STATE_BOUND); |
| 570 |
/* renew the lease manually */ |
| 571 |
ASSERT_OK(sd_dhcp_client_send_renew(client)); |
| 572 |
break; |
| 573 |
case 6: |
| 574 |
ASSERT_EQ(event, SD_DHCP_CLIENT_EVENT_RENEW); |
| 575 |
verify_reply(client, DHCP_STATE_BOUND); |
| 576 |
/* renew the lease by timer, triggering the corresponding timer event source now. */ |
| 577 |
ASSERT_OK(sd_event_source_set_time_relative(client->timeout_t1, /* usec= */ 0)); |
| 578 |
break; |
| 579 |
case 7: |
| 580 |
ASSERT_EQ(event, SD_DHCP_CLIENT_EVENT_RENEW); |
| 581 |
verify_reply(client, DHCP_STATE_BOUND); |
| 582 |
/* rebind the lease, triggering the corresponding timer event source now. */ |
| 583 |
ASSERT_OK(sd_event_source_set_time_relative(client->timeout_t2, /* usec= */ 0)); |
| 584 |
break; |
| 585 |
case 8: |
| 586 |
ASSERT_EQ(event, SD_DHCP_CLIENT_EVENT_RENEW); |
| 587 |
verify_reply(client, DHCP_STATE_BOUND); |
| 588 |
/* release and stop. */ |
| 589 |
ASSERT_OK(sd_dhcp_client_stop(client)); |
| 590 |
break; |
| 591 |
case 9: |
| 592 |
ASSERT_EQ(event, SD_DHCP_CLIENT_EVENT_STOP); |
| 593 |
verify_reply(client, DHCP_STATE_BOUND); |
| 594 |
break; |
| 595 |
default: |
| 596 |
assert_not_reached(); |
| 597 |
} |
| 598 |
|
| 599 |
return 0; |
| 600 |
} |
| 601 |
|
| 602 |
TEST(basic) { |
| 603 |
_cleanup_(sd_dhcp_client_unrefp) sd_dhcp_client *client = NULL; |
| 604 |
setup(basic_io_handler, basic_client_handler, &client); |
| 605 |
ASSERT_OK(sd_dhcp_client_set_send_release(client, true)); |
| 606 |
ASSERT_OK(sd_dhcp_client_start(client)); |
| 607 |
ASSERT_OK(sd_event_loop(sd_dhcp_client_get_event(client))); |
| 608 |
} |
| 609 |
|
| 610 |
static int anonymize_io_handler(sd_event_source *s, int fd, uint32_t revents, void *userdata) { |
| 611 |
sd_dhcp_client *client = ASSERT_PTR(userdata); |
| 612 |
static unsigned count = 0; |
| 613 |
|
| 614 |
count++; |
| 615 |
log_debug("%s: count=%u", __func__, count); |
| 616 |
|
| 617 |
switch (count) { |
| 618 |
case 1: { |
| 619 |
_cleanup_(sd_dhcp_message_unrefp) sd_dhcp_message *request = NULL; |
| 620 |
receive_message(fd, /* raw= */ true, /* check_xid= */ true, client, &request); |
| 621 |
|
| 622 |
verify_anonymized_request(request, DHCP_DISCOVER); |
| 623 |
|
| 624 |
_cleanup_(sd_dhcp_message_unrefp) sd_dhcp_message *reply = NULL; |
| 625 |
create_reply(client, request, DHCP_OFFER, &reply); |
| 626 |
|
| 627 |
send_message(fd, /* raw= */ true, client, reply); |
| 628 |
break; |
| 629 |
} |
| 630 |
case 2: { |
| 631 |
_cleanup_(sd_dhcp_message_unrefp) sd_dhcp_message *request = NULL; |
| 632 |
receive_message(fd, /* raw= */ true, /* check_xid= */ true, client, &request); |
| 633 |
|
| 634 |
/* REQUEST (selecting) */ |
| 635 |
verify_anonymized_request(request, DHCP_REQUEST); |
| 636 |
verify_request_server_address(request); |
| 637 |
|
| 638 |
_cleanup_(sd_dhcp_message_unrefp) sd_dhcp_message *reply = NULL; |
| 639 |
create_reply(client, request, DHCP_ACK, &reply); |
| 640 |
|
| 641 |
send_message(fd, /* raw= */ true, client, reply); |
| 642 |
break; |
| 643 |
} |
| 644 |
default: |
| 645 |
assert_not_reached(); |
| 646 |
} |
| 647 |
|
| 648 |
return 0; |
| 649 |
} |
| 650 |
|
| 651 |
static int anonymize_client_handler(sd_dhcp_client *client, int event, void *userdata) { |
| 652 |
sd_event *e = ASSERT_PTR(userdata); |
| 653 |
static unsigned count = 0; |
| 654 |
|
| 655 |
count++; |
| 656 |
log_debug("%s: count=%u, event=%i", __func__, count, event); |
| 657 |
|
| 658 |
switch (count) { |
| 659 |
case 1: |
| 660 |
ASSERT_EQ(event, SD_DHCP_CLIENT_EVENT_SELECTING); |
| 661 |
verify_reply(client, DHCP_STATE_SELECTING); |
| 662 |
break; |
| 663 |
case 2: |
| 664 |
ASSERT_EQ(event, SD_DHCP_CLIENT_EVENT_IP_ACQUIRE); |
| 665 |
verify_reply(client, DHCP_STATE_BOUND); |
| 666 |
ASSERT_OK(sd_event_exit(e, 0)); |
| 667 |
break; |
| 668 |
default: |
| 669 |
assert_not_reached(); |
| 670 |
} |
| 671 |
|
| 672 |
return 0; |
| 673 |
} |
| 674 |
|
| 675 |
TEST(anonymize) { |
| 676 |
_cleanup_(sd_dhcp_client_unrefp) sd_dhcp_client *client = NULL; |
| 677 |
setup(anonymize_io_handler, anonymize_client_handler, &client); |
| 678 |
ASSERT_OK(sd_dhcp_client_set_anonymize(client, true)); |
| 679 |
ASSERT_OK(sd_dhcp_client_start(client)); |
| 680 |
ASSERT_OK(sd_event_loop(sd_dhcp_client_get_event(client))); |
| 681 |
} |
| 682 |
|
| 683 |
static int rapid_commit_io_handler(sd_event_source *s, int fd, uint32_t revents, void *userdata) { |
| 684 |
sd_dhcp_client *client = ASSERT_PTR(userdata); |
| 685 |
static unsigned count = 0; |
| 686 |
|
| 687 |
count++; |
| 688 |
log_debug("%s: count=%u", __func__, count); |
| 689 |
|
| 690 |
switch (count) { |
| 691 |
case 1: { |
| 692 |
_cleanup_(sd_dhcp_message_unrefp) sd_dhcp_message *request = NULL; |
| 693 |
receive_message(fd, /* raw= */ true, /* check_xid= */ true, client, &request); |
| 694 |
|
| 695 |
verify_request(request, DHCP_DISCOVER); |
| 696 |
ASSERT_TRUE(dhcp_message_has_option(request, SD_DHCP_OPTION_RAPID_COMMIT)); |
| 697 |
|
| 698 |
_cleanup_(sd_dhcp_message_unrefp) sd_dhcp_message *reply = NULL; |
| 699 |
create_reply(client, request, DHCP_ACK, &reply); |
| 700 |
ASSERT_OK(dhcp_message_append_option_flag(reply, SD_DHCP_OPTION_RAPID_COMMIT)); |
| 701 |
|
| 702 |
send_message(fd, /* raw= */ true, client, reply); |
| 703 |
break; |
| 704 |
} |
| 705 |
default: |
| 706 |
assert_not_reached(); |
| 707 |
} |
| 708 |
|
| 709 |
return 0; |
| 710 |
} |
| 711 |
|
| 712 |
static int rapid_commit_client_handler(sd_dhcp_client *client, int event, void *userdata) { |
| 713 |
sd_event *e = ASSERT_PTR(userdata); |
| 714 |
static unsigned count = 0; |
| 715 |
|
| 716 |
count++; |
| 717 |
log_debug("%s: count=%u, event=%i", __func__, count, event); |
| 718 |
|
| 719 |
switch (count) { |
| 720 |
case 1: |
| 721 |
ASSERT_EQ(event, SD_DHCP_CLIENT_EVENT_IP_ACQUIRE); |
| 722 |
verify_reply(client, DHCP_STATE_BOUND); |
| 723 |
ASSERT_OK(sd_event_exit(e, 0)); |
| 724 |
break; |
| 725 |
default: |
| 726 |
assert_not_reached(); |
| 727 |
} |
| 728 |
|
| 729 |
return 0; |
| 730 |
} |
| 731 |
|
| 732 |
TEST(rapid_commit) { |
| 733 |
_cleanup_(sd_dhcp_client_unrefp) sd_dhcp_client *client = NULL; |
| 734 |
setup(rapid_commit_io_handler, rapid_commit_client_handler, &client); |
| 735 |
ASSERT_OK(sd_dhcp_client_set_rapid_commit(client, true)); |
| 736 |
ASSERT_OK(sd_dhcp_client_start(client)); |
| 737 |
ASSERT_OK(sd_event_loop(sd_dhcp_client_get_event(client))); |
| 738 |
} |
| 739 |
|
| 740 |
static int init_reboot_io_handler(sd_event_source *s, int fd, uint32_t revents, void *userdata) { |
| 741 |
sd_dhcp_client *client = ASSERT_PTR(userdata); |
| 742 |
static unsigned count = 0; |
| 743 |
|
| 744 |
count++; |
| 745 |
log_debug("%s: count=%u", __func__, count); |
| 746 |
|
| 747 |
switch (count) { |
| 748 |
case 1: { |
| 749 |
_cleanup_(sd_dhcp_message_unrefp) sd_dhcp_message *request = NULL; |
| 750 |
receive_message(fd, /* raw= */ true, /* check_xid= */ true, client, &request); |
| 751 |
|
| 752 |
verify_request(request, DHCP_REQUEST); |
| 753 |
verify_request_client_address(request, /* header= */ false); |
| 754 |
|
| 755 |
_cleanup_(sd_dhcp_message_unrefp) sd_dhcp_message *reply = NULL; |
| 756 |
create_reply(client, request, DHCP_ACK, &reply); |
| 757 |
|
| 758 |
send_message(fd, /* raw= */ true, client, reply); |
| 759 |
break; |
| 760 |
} |
| 761 |
default: |
| 762 |
assert_not_reached(); |
| 763 |
} |
| 764 |
|
| 765 |
return 0; |
| 766 |
} |
| 767 |
|
| 768 |
static int init_reboot_client_handler(sd_dhcp_client *client, int event, void *userdata) { |
| 769 |
sd_event *e = ASSERT_PTR(userdata); |
| 770 |
static unsigned count = 0; |
| 771 |
|
| 772 |
count++; |
| 773 |
log_debug("%s: count=%u, event=%i", __func__, count, event); |
| 774 |
|
| 775 |
switch (count) { |
| 776 |
case 1: |
| 777 |
ASSERT_EQ(event, SD_DHCP_CLIENT_EVENT_IP_ACQUIRE); |
| 778 |
verify_reply(client, DHCP_STATE_BOUND); |
| 779 |
ASSERT_OK(sd_event_exit(e, 0)); |
| 780 |
break; |
| 781 |
default: |
| 782 |
assert_not_reached(); |
| 783 |
} |
| 784 |
|
| 785 |
return 0; |
| 786 |
} |
| 787 |
|
| 788 |
TEST(init_reboot) { |
| 789 |
_cleanup_(sd_dhcp_client_unrefp) sd_dhcp_client *client = NULL; |
| 790 |
setup(init_reboot_io_handler, init_reboot_client_handler, &client); |
| 791 |
ASSERT_OK(sd_dhcp_client_set_request_address(client, &client_address.in)); |
| 792 |
ASSERT_OK(sd_dhcp_client_start(client)); |
| 793 |
ASSERT_OK(sd_event_loop(sd_dhcp_client_get_event(client))); |
| 794 |
} |
| 795 |
|
| 796 |
static int bootp_io_handler(sd_event_source *s, int fd, uint32_t revents, void *userdata) { |
| 797 |
sd_dhcp_client *client = ASSERT_PTR(userdata); |
| 798 |
static unsigned count = 0; |
| 799 |
|
| 800 |
count++; |
| 801 |
log_debug("%s: count=%u", __func__, count); |
| 802 |
|
| 803 |
switch (count) { |
| 804 |
case 1: { |
| 805 |
_cleanup_(sd_dhcp_message_unrefp) sd_dhcp_message *request = NULL; |
| 806 |
receive_message(fd, /* raw= */ true, /* check_xid= */ true, client, &request); |
| 807 |
|
| 808 |
verify_header(request); |
| 809 |
ASSERT_TRUE(tlv_isempty(&request->options)); |
| 810 |
|
| 811 |
_cleanup_(sd_dhcp_message_unrefp) sd_dhcp_message *reply = NULL; |
| 812 |
create_reply(client, request, DHCP_ACK, &reply); |
| 813 |
|
| 814 |
send_message(fd, /* raw= */ true, client, reply); |
| 815 |
break; |
| 816 |
} |
| 817 |
default: |
| 818 |
assert_not_reached(); |
| 819 |
} |
| 820 |
|
| 821 |
return 0; |
| 822 |
} |
| 823 |
|
| 824 |
static int bootp_client_handler(sd_dhcp_client *client, int event, void *userdata) { |
| 825 |
sd_event *e = ASSERT_PTR(userdata); |
| 826 |
static unsigned count = 0; |
| 827 |
|
| 828 |
count++; |
| 829 |
log_debug("%s: count=%u, event=%i", __func__, count, event); |
| 830 |
|
| 831 |
switch (count) { |
| 832 |
case 1: |
| 833 |
ASSERT_EQ(event, SD_DHCP_CLIENT_EVENT_IP_ACQUIRE); |
| 834 |
verify_reply(client, DHCP_STATE_BOUND); |
| 835 |
ASSERT_OK(sd_event_exit(e, 0)); |
| 836 |
break; |
| 837 |
default: |
| 838 |
assert_not_reached(); |
| 839 |
} |
| 840 |
|
| 841 |
return 0; |
| 842 |
} |
| 843 |
|
| 844 |
TEST(bootp) { |
| 845 |
_cleanup_(sd_dhcp_client_unrefp) sd_dhcp_client *client = NULL; |
| 846 |
setup(bootp_io_handler, bootp_client_handler, &client); |
| 847 |
ASSERT_OK(sd_dhcp_client_set_bootp(client, true)); |
| 848 |
ASSERT_OK(sd_dhcp_client_start(client)); |
| 849 |
ASSERT_OK(sd_event_loop(sd_dhcp_client_get_event(client))); |
| 850 |
} |
| 851 |
|
| 852 |
static int ipv6_only_io_handler(sd_event_source *s, int fd, uint32_t revents, void *userdata) { |
| 853 |
sd_dhcp_client *client = ASSERT_PTR(userdata); |
| 854 |
static unsigned count = 0; |
| 855 |
|
| 856 |
count++; |
| 857 |
log_debug("%s: count=%u", __func__, count); |
| 858 |
|
| 859 |
/* This is used multiple times. */ |
| 860 |
switch (count) { |
| 861 |
case 1: /* test case: before discover */ |
| 862 |
case 2: /* test case: after offer */ |
| 863 |
case 3: /* test case: before request */ |
| 864 |
case 4: /* test case: before ack */ |
| 865 |
case 6: { /* test case: after ack */ |
| 866 |
_cleanup_(sd_dhcp_message_unrefp) sd_dhcp_message *request = NULL; |
| 867 |
receive_message(fd, /* raw= */ true, /* check_xid= */ true, client, &request); |
| 868 |
|
| 869 |
verify_request(request, DHCP_DISCOVER); |
| 870 |
|
| 871 |
_cleanup_(sd_dhcp_message_unrefp) sd_dhcp_message *reply = NULL; |
| 872 |
create_reply(client, request, DHCP_OFFER, &reply); |
| 873 |
|
| 874 |
ASSERT_OK(dhcp_message_append_option_be32(reply, SD_DHCP_OPTION_IPV6_ONLY_PREFERRED, usec_to_be32_sec(10 * USEC_PER_SEC))); |
| 875 |
|
| 876 |
send_message(fd, /* raw= */ true, client, reply); |
| 877 |
break; |
| 878 |
} |
| 879 |
case 5: /* test case: before ack */ |
| 880 |
case 7: { /* test case: after ack */ |
| 881 |
_cleanup_(sd_dhcp_message_unrefp) sd_dhcp_message *request = NULL; |
| 882 |
receive_message(fd, /* raw= */ true, /* check_xid= */ true, client, &request); |
| 883 |
|
| 884 |
/* REQUEST (selecting) */ |
| 885 |
verify_request(request, DHCP_REQUEST); |
| 886 |
verify_request_server_address(request); |
| 887 |
verify_request_client_address(request, /* header= */ false); |
| 888 |
|
| 889 |
_cleanup_(sd_dhcp_message_unrefp) sd_dhcp_message *reply = NULL; |
| 890 |
create_reply(client, request, DHCP_ACK, &reply); |
| 891 |
|
| 892 |
ASSERT_OK(dhcp_message_append_option_be32(reply, SD_DHCP_OPTION_IPV6_ONLY_PREFERRED, usec_to_be32_sec(10 * USEC_PER_SEC))); |
| 893 |
|
| 894 |
send_message(fd, /* raw= */ true, client, reply); |
| 895 |
break; |
| 896 |
} |
| 897 |
case 8: { /* test case: init-reboot */ |
| 898 |
_cleanup_(sd_dhcp_message_unrefp) sd_dhcp_message *request = NULL; |
| 899 |
receive_message(fd, /* raw= */ true, /* check_xid= */ true, client, &request); |
| 900 |
|
| 901 |
/* REQUEST (init-reboot) */ |
| 902 |
verify_request(request, DHCP_REQUEST); |
| 903 |
verify_request_client_address(request, /* header= */ false); |
| 904 |
|
| 905 |
_cleanup_(sd_dhcp_message_unrefp) sd_dhcp_message *reply = NULL; |
| 906 |
create_reply(client, request, DHCP_ACK, &reply); |
| 907 |
|
| 908 |
ASSERT_OK(dhcp_message_append_option_be32(reply, SD_DHCP_OPTION_IPV6_ONLY_PREFERRED, usec_to_be32_sec(10 * USEC_PER_SEC))); |
| 909 |
|
| 910 |
send_message(fd, /* raw= */ true, client, reply); |
| 911 |
break; |
| 912 |
} |
| 913 |
case 9: { /* test case: rapid commit */ |
| 914 |
_cleanup_(sd_dhcp_message_unrefp) sd_dhcp_message *request = NULL; |
| 915 |
receive_message(fd, /* raw= */ true, /* check_xid= */ true, client, &request); |
| 916 |
|
| 917 |
verify_request(request, DHCP_DISCOVER); |
| 918 |
|
| 919 |
_cleanup_(sd_dhcp_message_unrefp) sd_dhcp_message *reply = NULL; |
| 920 |
create_reply(client, request, DHCP_ACK, &reply); |
| 921 |
ASSERT_OK(dhcp_message_append_option_flag(reply, SD_DHCP_OPTION_RAPID_COMMIT)); |
| 922 |
|
| 923 |
ASSERT_OK(dhcp_message_append_option_be32(reply, SD_DHCP_OPTION_IPV6_ONLY_PREFERRED, usec_to_be32_sec(10 * USEC_PER_SEC))); |
| 924 |
|
| 925 |
send_message(fd, /* raw= */ true, client, reply); |
| 926 |
break; |
| 927 |
} |
| 928 |
default: |
| 929 |
assert_not_reached(); |
| 930 |
} |
| 931 |
|
| 932 |
return 0; |
| 933 |
} |
| 934 |
|
| 935 |
static int ipv6_only_before_discover_client_handler(sd_dhcp_client *client, int event, void *userdata) { |
| 936 |
sd_event *e = ASSERT_PTR(userdata); |
| 937 |
static unsigned count = 0; |
| 938 |
|
| 939 |
count++; |
| 940 |
log_debug("%s: count=%u, event=%i", __func__, count, event); |
| 941 |
|
| 942 |
switch (count) { |
| 943 |
case 1: |
| 944 |
ASSERT_EQ(event, SD_DHCP_CLIENT_EVENT_SELECTING); |
| 945 |
verify_reply(client, DHCP_STATE_SELECTING); |
| 946 |
break; |
| 947 |
case 2: |
| 948 |
ASSERT_EQ(event, SD_DHCP_CLIENT_EVENT_STOP); |
| 949 |
verify_reply(client, DHCP_STATE_REQUESTING); |
| 950 |
ASSERT_OK(sd_event_exit(e, 0)); |
| 951 |
break; |
| 952 |
default: |
| 953 |
assert_not_reached(); |
| 954 |
} |
| 955 |
|
| 956 |
return 0; |
| 957 |
} |
| 958 |
|
| 959 |
static int ipv6_only_after_offer_client_handler(sd_dhcp_client *client, int event, void *userdata) { |
| 960 |
sd_event *e = ASSERT_PTR(userdata); |
| 961 |
static unsigned count = 0; |
| 962 |
|
| 963 |
count++; |
| 964 |
log_debug("%s: count=%u, event=%i", __func__, count, event); |
| 965 |
|
| 966 |
switch (count) { |
| 967 |
case 1: |
| 968 |
ASSERT_EQ(event, SD_DHCP_CLIENT_EVENT_SELECTING); |
| 969 |
verify_reply(client, DHCP_STATE_SELECTING); |
| 970 |
ASSERT_OK(sd_dhcp_client_set_ipv6_connectivity(client, true)); |
| 971 |
break; |
| 972 |
case 2: |
| 973 |
ASSERT_EQ(event, SD_DHCP_CLIENT_EVENT_STOP); |
| 974 |
verify_reply(client, DHCP_STATE_REQUESTING); |
| 975 |
ASSERT_OK(sd_event_exit(e, 0)); |
| 976 |
break; |
| 977 |
default: |
| 978 |
assert_not_reached(); |
| 979 |
} |
| 980 |
|
| 981 |
return 0; |
| 982 |
} |
| 983 |
|
| 984 |
static int ipv6_only_before_request_defer_handler(sd_event_source *s, void *userdata) { |
| 985 |
sd_dhcp_client *client = ASSERT_PTR(userdata); |
| 986 |
|
| 987 |
ASSERT_EQ(client->state, DHCP_STATE_REQUESTING); |
| 988 |
ASSERT_EQ(client->request_attempt, 0u); |
| 989 |
|
| 990 |
ASSERT_OK(sd_dhcp_client_set_ipv6_connectivity(client, true)); |
| 991 |
ASSERT_EQ(client->state, DHCP_STATE_STOPPED); |
| 992 |
|
| 993 |
ASSERT_OK(sd_event_source_set_enabled(s, SD_EVENT_OFF)); |
| 994 |
return 0; |
| 995 |
} |
| 996 |
|
| 997 |
static int ipv6_only_before_request_client_handler(sd_dhcp_client *client, int event, void *userdata) { |
| 998 |
sd_event *e = ASSERT_PTR(userdata); |
| 999 |
static unsigned count = 0; |
| 1000 |
|
| 1001 |
count++; |
| 1002 |
log_debug("%s: count=%u, event=%i", __func__, count, event); |
| 1003 |
|
| 1004 |
switch (count) { |
| 1005 |
case 1: |
| 1006 |
ASSERT_EQ(event, SD_DHCP_CLIENT_EVENT_SELECTING); |
| 1007 |
verify_reply(client, DHCP_STATE_SELECTING); |
| 1008 |
ASSERT_OK(sd_event_add_defer(e, /* ret= */ NULL, ipv6_only_before_request_defer_handler, client)); |
| 1009 |
break; |
| 1010 |
case 2: |
| 1011 |
ASSERT_EQ(event, SD_DHCP_CLIENT_EVENT_STOP); |
| 1012 |
verify_reply(client, DHCP_STATE_REQUESTING); |
| 1013 |
ASSERT_OK(sd_event_exit(e, 0)); |
| 1014 |
break; |
| 1015 |
default: |
| 1016 |
assert_not_reached(); |
| 1017 |
} |
| 1018 |
|
| 1019 |
return 0; |
| 1020 |
} |
| 1021 |
|
| 1022 |
static int ipv6_only_before_ack_post_handler(sd_event_source *s, void *userdata) { |
| 1023 |
sd_dhcp_client *client = ASSERT_PTR(userdata); |
| 1024 |
|
| 1025 |
if (sd_dhcp_client_is_waiting_for_ipv6_connectivity(client)) |
| 1026 |
/* Boost the time for sending DHCPREQUEST */ |
| 1027 |
ASSERT_OK(sd_event_source_set_time_relative(client->timeout_resend, /* usec= */ 0)); |
| 1028 |
|
| 1029 |
else if (client->state == DHCP_STATE_REQUESTING) { |
| 1030 |
ASSERT_EQ(client->request_attempt, 1u); |
| 1031 |
|
| 1032 |
/* Set IPv6 connectivity after a DHCPREQUEST sent. */ |
| 1033 |
ASSERT_OK(sd_dhcp_client_set_ipv6_connectivity(client, true)); |
| 1034 |
/* Still running */ |
| 1035 |
ASSERT_EQ(client->state, DHCP_STATE_REQUESTING); |
| 1036 |
|
| 1037 |
} else if (client->state == DHCP_STATE_BOUND) |
| 1038 |
ASSERT_OK(sd_dhcp_client_stop(client)); |
| 1039 |
|
| 1040 |
return 0; |
| 1041 |
} |
| 1042 |
|
| 1043 |
static int ipv6_only_before_ack_client_handler(sd_dhcp_client *client, int event, void *userdata) { |
| 1044 |
sd_event *e = ASSERT_PTR(userdata); |
| 1045 |
|
| 1046 |
static unsigned count = 0; |
| 1047 |
|
| 1048 |
count++; |
| 1049 |
log_debug("%s: count=%u, event=%i", __func__, count, event); |
| 1050 |
|
| 1051 |
switch (count) { |
| 1052 |
case 1: |
| 1053 |
ASSERT_EQ(event, SD_DHCP_CLIENT_EVENT_SELECTING); |
| 1054 |
verify_reply(client, DHCP_STATE_SELECTING); |
| 1055 |
ASSERT_OK(sd_event_add_post(e, /* ret= */ NULL, ipv6_only_before_ack_post_handler, client)); |
| 1056 |
break; |
| 1057 |
case 2: |
| 1058 |
ASSERT_EQ(event, SD_DHCP_CLIENT_EVENT_IP_ACQUIRE); |
| 1059 |
verify_reply(client, DHCP_STATE_BOUND); |
| 1060 |
break; |
| 1061 |
case 3: |
| 1062 |
ASSERT_EQ(event, SD_DHCP_CLIENT_EVENT_STOP); |
| 1063 |
verify_reply(client, DHCP_STATE_BOUND); |
| 1064 |
ASSERT_OK(sd_event_exit(e, 0)); |
| 1065 |
break; |
| 1066 |
default: |
| 1067 |
assert_not_reached(); |
| 1068 |
} |
| 1069 |
|
| 1070 |
return 0; |
| 1071 |
} |
| 1072 |
|
| 1073 |
static int ipv6_only_after_ack_defer_handler(sd_event_source *s, void *userdata) { |
| 1074 |
sd_dhcp_client *client = ASSERT_PTR(userdata); |
| 1075 |
|
| 1076 |
ASSERT_EQ(client->state, DHCP_STATE_REQUESTING); |
| 1077 |
/* Boost the time for sending DHCPREQUEST */ |
| 1078 |
ASSERT_OK(sd_event_source_set_time_relative(client->timeout_resend, /* usec= */ 0)); |
| 1079 |
return 0; |
| 1080 |
} |
| 1081 |
|
| 1082 |
static int ipv6_only_after_ack_client_handler(sd_dhcp_client *client, int event, void *userdata) { |
| 1083 |
sd_event *e = ASSERT_PTR(userdata); |
| 1084 |
static unsigned count = 0; |
| 1085 |
|
| 1086 |
count++; |
| 1087 |
log_debug("%s: count=%u, event=%i", __func__, count, event); |
| 1088 |
|
| 1089 |
switch (count) { |
| 1090 |
case 1: |
| 1091 |
ASSERT_EQ(event, SD_DHCP_CLIENT_EVENT_SELECTING); |
| 1092 |
verify_reply(client, DHCP_STATE_SELECTING); |
| 1093 |
ASSERT_OK(sd_event_add_defer(e, /* ret= */ NULL, ipv6_only_after_ack_defer_handler, client)); |
| 1094 |
break; |
| 1095 |
case 2: |
| 1096 |
ASSERT_EQ(event, SD_DHCP_CLIENT_EVENT_IP_ACQUIRE); |
| 1097 |
verify_reply(client, DHCP_STATE_BOUND); |
| 1098 |
ASSERT_OK(sd_dhcp_client_set_ipv6_connectivity(client, true)); |
| 1099 |
/* still running */ |
| 1100 |
ASSERT_EQ(client->state, DHCP_STATE_BOUND); |
| 1101 |
ASSERT_OK(sd_event_exit(e, 0)); |
| 1102 |
break; |
| 1103 |
default: |
| 1104 |
assert_not_reached(); |
| 1105 |
} |
| 1106 |
|
| 1107 |
return 0; |
| 1108 |
} |
| 1109 |
|
| 1110 |
static int ipv6_only_init_reboot_client_handler(sd_dhcp_client *client, int event, void *userdata) { |
| 1111 |
sd_event *e = ASSERT_PTR(userdata); |
| 1112 |
static unsigned count = 0; |
| 1113 |
|
| 1114 |
count++; |
| 1115 |
log_debug("%s: count=%u, event=%i", __func__, count, event); |
| 1116 |
|
| 1117 |
switch (count) { |
| 1118 |
case 1: |
| 1119 |
ASSERT_EQ(event, SD_DHCP_CLIENT_EVENT_IP_ACQUIRE); |
| 1120 |
verify_reply(client, DHCP_STATE_BOUND); |
| 1121 |
ASSERT_OK(sd_event_exit(e, 0)); |
| 1122 |
break; |
| 1123 |
default: |
| 1124 |
assert_not_reached(); |
| 1125 |
} |
| 1126 |
|
| 1127 |
return 0; |
| 1128 |
} |
| 1129 |
|
| 1130 |
static int ipv6_only_rapid_commit_client_handler(sd_dhcp_client *client, int event, void *userdata) { |
| 1131 |
sd_event *e = ASSERT_PTR(userdata); |
| 1132 |
static unsigned count = 0; |
| 1133 |
|
| 1134 |
count++; |
| 1135 |
log_debug("%s: count=%u, event=%i", __func__, count, event); |
| 1136 |
|
| 1137 |
switch (count) { |
| 1138 |
case 1: |
| 1139 |
ASSERT_EQ(event, SD_DHCP_CLIENT_EVENT_IP_ACQUIRE); |
| 1140 |
verify_reply(client, DHCP_STATE_BOUND); |
| 1141 |
ASSERT_OK(sd_event_exit(e, 0)); |
| 1142 |
break; |
| 1143 |
default: |
| 1144 |
assert_not_reached(); |
| 1145 |
} |
| 1146 |
|
| 1147 |
return 0; |
| 1148 |
} |
| 1149 |
|
| 1150 |
TEST(ipv6_only) { |
| 1151 |
_cleanup_(sd_dhcp_client_unrefp) sd_dhcp_client *client = NULL; |
| 1152 |
|
| 1153 |
/* case 1: IPv6 connectivity is acquired before starting the client. */ |
| 1154 |
setup(ipv6_only_io_handler, ipv6_only_before_discover_client_handler, &client); |
| 1155 |
ASSERT_OK(sd_dhcp_client_set_request_option(client, SD_DHCP_OPTION_IPV6_ONLY_PREFERRED)); |
| 1156 |
ASSERT_OK(sd_dhcp_client_set_ipv6_connectivity(client, true)); |
| 1157 |
ASSERT_OK(sd_dhcp_client_start(client)); |
| 1158 |
ASSERT_OK(sd_event_loop(sd_dhcp_client_get_event(client))); |
| 1159 |
|
| 1160 |
client = sd_dhcp_client_unref(client); |
| 1161 |
|
| 1162 |
/* case 2: IPv6 connectivity is acquired after DHCPOFFER received. */ |
| 1163 |
setup(ipv6_only_io_handler, ipv6_only_after_offer_client_handler, &client); |
| 1164 |
ASSERT_OK(sd_dhcp_client_set_request_option(client, SD_DHCP_OPTION_IPV6_ONLY_PREFERRED)); |
| 1165 |
ASSERT_OK(sd_dhcp_client_start(client)); |
| 1166 |
ASSERT_OK(sd_event_loop(sd_dhcp_client_get_event(client))); |
| 1167 |
|
| 1168 |
client = sd_dhcp_client_unref(client); |
| 1169 |
|
| 1170 |
/* case 3: IPv6 connectivity is acquired before sending DHCPREQUEST. */ |
| 1171 |
setup(ipv6_only_io_handler, ipv6_only_before_request_client_handler, &client); |
| 1172 |
ASSERT_OK(sd_dhcp_client_set_request_option(client, SD_DHCP_OPTION_IPV6_ONLY_PREFERRED)); |
| 1173 |
ASSERT_OK(sd_dhcp_client_start(client)); |
| 1174 |
ASSERT_OK(sd_event_loop(sd_dhcp_client_get_event(client))); |
| 1175 |
|
| 1176 |
client = sd_dhcp_client_unref(client); |
| 1177 |
|
| 1178 |
/* case 4: IPv6 connectivity is acquired before DHCPACK received. */ |
| 1179 |
setup(ipv6_only_io_handler, ipv6_only_before_ack_client_handler, &client); |
| 1180 |
ASSERT_OK(sd_dhcp_client_set_request_option(client, SD_DHCP_OPTION_IPV6_ONLY_PREFERRED)); |
| 1181 |
ASSERT_OK(sd_dhcp_client_start(client)); |
| 1182 |
ASSERT_OK(sd_event_loop(sd_dhcp_client_get_event(client))); |
| 1183 |
|
| 1184 |
client = sd_dhcp_client_unref(client); |
| 1185 |
|
| 1186 |
/* case 5: IPv6 connectivity is acquired after DHCPACK received. */ |
| 1187 |
setup(ipv6_only_io_handler, ipv6_only_after_ack_client_handler, &client); |
| 1188 |
ASSERT_OK(sd_dhcp_client_set_request_option(client, SD_DHCP_OPTION_IPV6_ONLY_PREFERRED)); |
| 1189 |
ASSERT_OK(sd_dhcp_client_start(client)); |
| 1190 |
ASSERT_OK(sd_event_loop(sd_dhcp_client_get_event(client))); |
| 1191 |
|
| 1192 |
client = sd_dhcp_client_unref(client); |
| 1193 |
|
| 1194 |
/* case 6: IPv6 connectivity is acquired on reboot. */ |
| 1195 |
setup(ipv6_only_io_handler, ipv6_only_init_reboot_client_handler, &client); |
| 1196 |
ASSERT_OK(sd_dhcp_client_set_request_option(client, SD_DHCP_OPTION_IPV6_ONLY_PREFERRED)); |
| 1197 |
ASSERT_OK(sd_dhcp_client_set_request_address(client, &client_address.in)); |
| 1198 |
ASSERT_OK(sd_dhcp_client_set_ipv6_connectivity(client, true)); |
| 1199 |
ASSERT_OK(sd_dhcp_client_start(client)); |
| 1200 |
ASSERT_OK(sd_event_loop(sd_dhcp_client_get_event(client))); |
| 1201 |
|
| 1202 |
client = sd_dhcp_client_unref(client); |
| 1203 |
|
| 1204 |
/* case 7: IPv6 connectivity is acquired on rapid commit. */ |
| 1205 |
setup(ipv6_only_io_handler, ipv6_only_rapid_commit_client_handler, &client); |
| 1206 |
ASSERT_OK(sd_dhcp_client_set_rapid_commit(client, true)); |
| 1207 |
ASSERT_OK(sd_dhcp_client_set_request_option(client, SD_DHCP_OPTION_IPV6_ONLY_PREFERRED)); |
| 1208 |
ASSERT_OK(sd_dhcp_client_set_ipv6_connectivity(client, true)); |
| 1209 |
ASSERT_OK(sd_dhcp_client_start(client)); |
| 1210 |
ASSERT_OK(sd_event_loop(sd_dhcp_client_get_event(client))); |
| 1211 |
} |
| 1212 |
|
| 1213 |
static int discover_attempt_io_handler(sd_event_source *s, int fd, uint32_t revents, void *userdata) { |
| 1214 |
sd_dhcp_client *client = ASSERT_PTR(userdata); |
| 1215 |
static unsigned count = 0; |
| 1216 |
|
| 1217 |
count++; |
| 1218 |
log_debug("%s: count=%u", __func__, count); |
| 1219 |
|
| 1220 |
switch (count) { |
| 1221 |
case 1 ... 5: /* test case: no reply */ |
| 1222 |
case 6 ... 9: /* test case: reset on bound (1st cycle) */ |
| 1223 |
case 12 ... 15: { /* test case: reset on bound (2nd cycle) */ |
| 1224 |
_cleanup_(sd_dhcp_message_unrefp) sd_dhcp_message *request = NULL; |
| 1225 |
receive_message(fd, /* raw= */ true, /* check_xid= */ true, client, &request); |
| 1226 |
|
| 1227 |
verify_request(request, DHCP_DISCOVER); |
| 1228 |
|
| 1229 |
/* Boost the retransmit timer. */ |
| 1230 |
ASSERT_OK(sd_event_source_set_time_relative(client->timeout_resend, /* usec= */ 0)); |
| 1231 |
break; |
| 1232 |
} |
| 1233 |
case 10: |
| 1234 |
case 16: { |
| 1235 |
_cleanup_(sd_dhcp_message_unrefp) sd_dhcp_message *request = NULL; |
| 1236 |
receive_message(fd, /* raw= */ true, /* check_xid= */ true, client, &request); |
| 1237 |
|
| 1238 |
verify_request(request, DHCP_DISCOVER); |
| 1239 |
|
| 1240 |
_cleanup_(sd_dhcp_message_unrefp) sd_dhcp_message *reply = NULL; |
| 1241 |
create_reply(client, request, DHCP_OFFER, &reply); |
| 1242 |
|
| 1243 |
send_message(fd, /* raw= */ true, client, reply); |
| 1244 |
break; |
| 1245 |
} |
| 1246 |
case 11: |
| 1247 |
case 17: { |
| 1248 |
_cleanup_(sd_dhcp_message_unrefp) sd_dhcp_message *request = NULL; |
| 1249 |
receive_message(fd, /* raw= */ true, /* check_xid= */ true, client, &request); |
| 1250 |
|
| 1251 |
/* REQUEST (selecting) */ |
| 1252 |
verify_request(request, DHCP_REQUEST); |
| 1253 |
verify_request_server_address(request); |
| 1254 |
verify_request_client_address(request, /* header= */ false); |
| 1255 |
|
| 1256 |
_cleanup_(sd_dhcp_message_unrefp) sd_dhcp_message *reply = NULL; |
| 1257 |
create_reply(client, request, DHCP_ACK, &reply); |
| 1258 |
|
| 1259 |
send_message(fd, /* raw= */ true, client, reply); |
| 1260 |
break; |
| 1261 |
} |
| 1262 |
default: |
| 1263 |
assert_not_reached(); |
| 1264 |
} |
| 1265 |
|
| 1266 |
return 0; |
| 1267 |
} |
| 1268 |
|
| 1269 |
static int discover_attempt_no_reply_client_handler(sd_dhcp_client *client, int event, void *userdata) { |
| 1270 |
sd_event *e = ASSERT_PTR(userdata); |
| 1271 |
static unsigned count = 0; |
| 1272 |
|
| 1273 |
count++; |
| 1274 |
log_debug("%s: count=%u, event=%i", __func__, count, event); |
| 1275 |
|
| 1276 |
switch (count) { |
| 1277 |
case 1 ... 3: /* The event triggered after sending the 3rd, 4th, 5th DISCOVER message. */ |
| 1278 |
ASSERT_EQ(event, SD_DHCP_CLIENT_EVENT_TRANSIENT_FAILURE); |
| 1279 |
ASSERT_EQ(client->state, DHCP_STATE_SELECTING); |
| 1280 |
ASSERT_EQ(client->discover_attempt, count + 2u); |
| 1281 |
break; |
| 1282 |
case 4: |
| 1283 |
ASSERT_EQ(event, -ETIMEDOUT); |
| 1284 |
ASSERT_EQ(client->state, DHCP_STATE_SELECTING); |
| 1285 |
ASSERT_EQ(client->discover_attempt, 5u); |
| 1286 |
ASSERT_OK(sd_event_exit(e, 0)); |
| 1287 |
break; |
| 1288 |
default: |
| 1289 |
assert_not_reached(); |
| 1290 |
} |
| 1291 |
|
| 1292 |
return 0; |
| 1293 |
} |
| 1294 |
|
| 1295 |
static int discover_attempt_reset_on_bound_client_handler(sd_dhcp_client *client, int event, void *userdata) { |
| 1296 |
sd_event *e = ASSERT_PTR(userdata); |
| 1297 |
static unsigned count = 0; |
| 1298 |
|
| 1299 |
count++; |
| 1300 |
log_debug("%s: count=%u, event=%i", __func__, count, event); |
| 1301 |
|
| 1302 |
switch (count) { |
| 1303 |
case 1 ... 3: /* The event triggered after sending the 3rd, 4th, 5th DISCOVER message. */ |
| 1304 |
ASSERT_EQ(event, SD_DHCP_CLIENT_EVENT_TRANSIENT_FAILURE); |
| 1305 |
ASSERT_EQ(client->state, DHCP_STATE_SELECTING); |
| 1306 |
ASSERT_EQ(client->discover_attempt, count + 2u); |
| 1307 |
break; |
| 1308 |
case 4: |
| 1309 |
ASSERT_EQ(event, SD_DHCP_CLIENT_EVENT_SELECTING); |
| 1310 |
verify_reply(client, DHCP_STATE_SELECTING); |
| 1311 |
ASSERT_EQ(client->discover_attempt, 5u); |
| 1312 |
break; |
| 1313 |
case 5: |
| 1314 |
ASSERT_EQ(event, SD_DHCP_CLIENT_EVENT_IP_ACQUIRE); |
| 1315 |
verify_reply(client, DHCP_STATE_BOUND); |
| 1316 |
ASSERT_EQ(client->discover_attempt, 0u); |
| 1317 |
/* expire the lease now. */ |
| 1318 |
ASSERT_OK(sd_event_source_set_time_relative(client->timeout_expire, 0)); |
| 1319 |
break; |
| 1320 |
case 6: |
| 1321 |
ASSERT_EQ(event, SD_DHCP_CLIENT_EVENT_EXPIRED); |
| 1322 |
verify_reply(client, DHCP_STATE_BOUND); |
| 1323 |
ASSERT_EQ(client->discover_attempt, 0u); |
| 1324 |
break; |
| 1325 |
case 7 ... 9: /* The event triggered after sending the 3rd, 4th, 5th DISCOVER message. */ |
| 1326 |
ASSERT_EQ(event, SD_DHCP_CLIENT_EVENT_TRANSIENT_FAILURE); |
| 1327 |
ASSERT_EQ(client->state, DHCP_STATE_SELECTING); |
| 1328 |
ASSERT_EQ(client->discover_attempt, count - 4u); |
| 1329 |
break; |
| 1330 |
case 10: |
| 1331 |
ASSERT_EQ(event, SD_DHCP_CLIENT_EVENT_SELECTING); |
| 1332 |
verify_reply(client, DHCP_STATE_SELECTING); |
| 1333 |
ASSERT_EQ(client->discover_attempt, 5u); |
| 1334 |
break; |
| 1335 |
case 11: |
| 1336 |
ASSERT_EQ(event, SD_DHCP_CLIENT_EVENT_IP_ACQUIRE); |
| 1337 |
verify_reply(client, DHCP_STATE_BOUND); |
| 1338 |
ASSERT_EQ(client->discover_attempt, 0u); |
| 1339 |
ASSERT_OK(sd_dhcp_client_stop(client)); |
| 1340 |
break; |
| 1341 |
case 12: |
| 1342 |
ASSERT_EQ(event, SD_DHCP_CLIENT_EVENT_STOP); |
| 1343 |
verify_reply(client, DHCP_STATE_BOUND); |
| 1344 |
ASSERT_EQ(client->discover_attempt, 0u); |
| 1345 |
ASSERT_OK(sd_event_exit(e, 0)); |
| 1346 |
break; |
| 1347 |
default: |
| 1348 |
assert_not_reached(); |
| 1349 |
} |
| 1350 |
|
| 1351 |
return 0; |
| 1352 |
} |
| 1353 |
|
| 1354 |
TEST(discover_attempt) { |
| 1355 |
_cleanup_(sd_dhcp_client_unrefp) sd_dhcp_client *client = NULL; |
| 1356 |
|
| 1357 |
/* case 1: no reply */ |
| 1358 |
setup(discover_attempt_io_handler, discover_attempt_no_reply_client_handler, &client); |
| 1359 |
ASSERT_OK(sd_dhcp_client_set_max_attempts(client, 5)); |
| 1360 |
ASSERT_OK(sd_dhcp_client_start(client)); |
| 1361 |
ASSERT_OK(sd_event_loop(sd_dhcp_client_get_event(client))); |
| 1362 |
|
| 1363 |
client = sd_dhcp_client_unref(client); |
| 1364 |
|
| 1365 |
/* case 2: attempt is reset on bound */ |
| 1366 |
setup(discover_attempt_io_handler, discover_attempt_reset_on_bound_client_handler, &client); |
| 1367 |
ASSERT_OK(sd_dhcp_client_set_max_attempts(client, 5)); |
| 1368 |
ASSERT_OK(sd_dhcp_client_start(client)); |
| 1369 |
ASSERT_OK(sd_event_loop(sd_dhcp_client_get_event(client))); |
| 1370 |
} |
| 1371 |
|
| 1372 |
static int intro(void) { |
| 1373 |
ASSERT_OK_ERRNO(setenv("SYSTEMD_NETWORK_TEST_MODE", "1", /* overwrite= */ true)); |
| 1374 |
return 0; |
| 1375 |
} |
| 1376 |
|
| 1377 |
DEFINE_TEST_MAIN_WITH_INTRO(LOG_DEBUG, intro); |