| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
|
| 3 |
#include <net/if_arp.h> |
| 4 |
|
| 5 |
#include "sd-json.h" |
| 6 |
|
| 7 |
#include "alloc-util.h" |
| 8 |
#include "dhcp-client-id-internal.h" |
| 9 |
#include "dhcp-message.h" |
| 10 |
#include "dhcp-message-dump.h" |
| 11 |
#include "dhcp-route.h" |
| 12 |
#include "dns-packet.h" |
| 13 |
#include "dns-resolver-internal.h" |
| 14 |
#include "ether-addr-util.h" |
| 15 |
#include "fd-util.h" |
| 16 |
#include "iovec-util.h" |
| 17 |
#include "iovec-wrapper.h" |
| 18 |
#include "ip-util.h" |
| 19 |
#include "random-util.h" |
| 20 |
#include "set.h" |
| 21 |
#include "socket-util.h" |
| 22 |
#include "strv.h" |
| 23 |
#include "tests.h" |
| 24 |
|
| 25 |
static void verify_header(sd_dhcp_message *m, uint32_t xid, const struct hw_addr_data *hw_addr) { |
| 26 |
ASSERT_EQ(be32toh(m->header.xid), xid); |
| 27 |
|
| 28 |
ASSERT_FALSE(dhcp_message_has_broadcast_flag(m)); |
| 29 |
dhcp_message_set_broadcast_flag(m, true); |
| 30 |
ASSERT_TRUE(dhcp_message_has_broadcast_flag(m)); |
| 31 |
dhcp_message_set_broadcast_flag(m, false); |
| 32 |
ASSERT_FALSE(dhcp_message_has_broadcast_flag(m)); |
| 33 |
|
| 34 |
struct hw_addr_data a; |
| 35 |
ASSERT_OK(dhcp_message_get_hw_addr(m, &a)); |
| 36 |
ASSERT_TRUE(hw_addr_equal(&a, hw_addr)); |
| 37 |
} |
| 38 |
|
| 39 |
static void verify_flag(sd_dhcp_message *m) { |
| 40 |
ASSERT_TRUE(dhcp_message_has_option(m, SD_DHCP_OPTION_RAPID_COMMIT)); |
| 41 |
ASSERT_OK(dhcp_message_get_option_flag(m, SD_DHCP_OPTION_RAPID_COMMIT)); |
| 42 |
ASSERT_ERROR(dhcp_message_get_option_u8(m, SD_DHCP_OPTION_RAPID_COMMIT, NULL), ENODATA); /* size mismatch */ |
| 43 |
} |
| 44 |
|
| 45 |
static void verify_u8(sd_dhcp_message *m, uint8_t expected) { |
| 46 |
uint8_t u; |
| 47 |
ASSERT_OK(dhcp_message_get_option_u8(m, SD_DHCP_OPTION_MESSAGE_TYPE, &u)); |
| 48 |
ASSERT_EQ(u, expected); |
| 49 |
} |
| 50 |
|
| 51 |
static void verify_u16(sd_dhcp_message *m, uint16_t expected) { |
| 52 |
uint16_t u; |
| 53 |
ASSERT_OK(dhcp_message_get_option_u16(m, SD_DHCP_OPTION_MAXIMUM_MESSAGE_SIZE, &u)); |
| 54 |
ASSERT_EQ(u, expected); |
| 55 |
} |
| 56 |
|
| 57 |
static void verify_sec(sd_dhcp_message *m, usec_t expected) { |
| 58 |
usec_t t; |
| 59 |
ASSERT_OK(dhcp_message_get_option_sec(m, SD_DHCP_OPTION_IP_ADDRESS_LEASE_TIME, /* max_as_infinity= */ false, &t)); |
| 60 |
ASSERT_EQ(t, expected); |
| 61 |
ASSERT_OK(dhcp_message_get_option_sec(m, SD_DHCP_OPTION_IP_ADDRESS_LEASE_TIME, /* max_as_infinity= */ true, &t)); |
| 62 |
ASSERT_EQ(t, expected); |
| 63 |
ASSERT_OK(dhcp_message_get_option_sec(m, SD_DHCP_OPTION_RENEWAL_TIME, /* max_as_infinity= */ false, &t)); |
| 64 |
ASSERT_EQ(t, UINT32_MAX * USEC_PER_SEC); |
| 65 |
ASSERT_OK(dhcp_message_get_option_sec(m, SD_DHCP_OPTION_RENEWAL_TIME, /* max_as_infinity= */ true, &t)); |
| 66 |
ASSERT_EQ(t, USEC_INFINITY); |
| 67 |
} |
| 68 |
|
| 69 |
static void verify_address(sd_dhcp_message *m, const struct in_addr *expected) { |
| 70 |
struct in_addr a; |
| 71 |
ASSERT_OK(dhcp_message_get_option_be32(m, SD_DHCP_OPTION_REQUESTED_IP_ADDRESS, &a.s_addr)); |
| 72 |
ASSERT_EQ(a.s_addr, expected->s_addr); |
| 73 |
ASSERT_OK(dhcp_message_get_option_address(m, SD_DHCP_OPTION_REQUESTED_IP_ADDRESS, &a)); |
| 74 |
ASSERT_EQ(a.s_addr, expected->s_addr); |
| 75 |
} |
| 76 |
|
| 77 |
static void verify_addresses( |
| 78 |
sd_dhcp_message *m, |
| 79 |
size_t n_ntp, const struct in_addr *ntp, |
| 80 |
size_t n_sip, const struct in_addr *sip) { |
| 81 |
|
| 82 |
struct in_addr a; |
| 83 |
ASSERT_OK(dhcp_message_get_option_be32(m, SD_DHCP_OPTION_NTP_SERVER, &a.s_addr)); |
| 84 |
ASSERT_EQ(a.s_addr, ntp->s_addr); |
| 85 |
ASSERT_OK(dhcp_message_get_option_address(m, SD_DHCP_OPTION_NTP_SERVER, &a)); |
| 86 |
ASSERT_EQ(a.s_addr, ntp->s_addr); |
| 87 |
|
| 88 |
size_t n; |
| 89 |
_cleanup_free_ struct in_addr *addrs = NULL; |
| 90 |
ASSERT_OK(dhcp_message_get_option_addresses(m, SD_DHCP_OPTION_NTP_SERVER, &n, &addrs)); |
| 91 |
ASSERT_EQ(n, n_ntp); |
| 92 |
ASSERT_EQ(memcmp(addrs, ntp, sizeof(struct in_addr) * n), 0); |
| 93 |
|
| 94 |
ASSERT_ERROR(dhcp_message_get_option_be32(m, SD_DHCP_OPTION_SIP_SERVER, NULL), ENODATA); |
| 95 |
ASSERT_ERROR(dhcp_message_get_option_address(m, SD_DHCP_OPTION_SIP_SERVER, NULL), ENODATA); |
| 96 |
|
| 97 |
addrs = mfree(addrs); |
| 98 |
ASSERT_OK(dhcp_message_get_option_addresses(m, SD_DHCP_OPTION_SIP_SERVER, &n, &addrs)); |
| 99 |
ASSERT_EQ(n, n_sip); |
| 100 |
ASSERT_EQ(memcmp(addrs, sip, sizeof(struct in_addr) * n), 0); |
| 101 |
} |
| 102 |
|
| 103 |
static void verify_string(sd_dhcp_message *m, const char *expected) { |
| 104 |
_cleanup_free_ char *s = NULL; |
| 105 |
ASSERT_OK(dhcp_message_get_option_string(m, SD_DHCP_OPTION_VENDOR_CLASS_IDENTIFIER, &s)); |
| 106 |
ASSERT_STREQ(s, expected); |
| 107 |
} |
| 108 |
|
| 109 |
static void verify_multiple_strings(sd_dhcp_message *m, char * const *expected) { |
| 110 |
_cleanup_free_ char *s = NULL; |
| 111 |
ASSERT_OK(dhcp_message_get_option_string(m, SD_DHCP_OPTION_ROOT_PATH, &s)); |
| 112 |
_cleanup_free_ char *joined = ASSERT_NOT_NULL(strv_join(expected, /* separator= */ "")); |
| 113 |
ASSERT_STREQ(s, joined); |
| 114 |
} |
| 115 |
|
| 116 |
static void verify_routes(sd_dhcp_message *m, size_t n_expected, const sd_dhcp_route *expected) { |
| 117 |
uint8_t code; |
| 118 |
FOREACH_ARGUMENT(code, |
| 119 |
SD_DHCP_OPTION_STATIC_ROUTE, |
| 120 |
SD_DHCP_OPTION_CLASSLESS_STATIC_ROUTE, |
| 121 |
SD_DHCP_OPTION_PRIVATE_CLASSLESS_STATIC_ROUTE) { |
| 122 |
|
| 123 |
_cleanup_free_ sd_dhcp_route *routes = NULL; |
| 124 |
size_t n; |
| 125 |
ASSERT_OK(dhcp_message_get_option_routes(m, code, &n, &routes)); |
| 126 |
ASSERT_EQ(n, n_expected); |
| 127 |
for (size_t i = 0; i < n; i++) { |
| 128 |
ASSERT_EQ(routes[i].dst_addr.s_addr, expected[i].dst_addr.s_addr); |
| 129 |
ASSERT_EQ(routes[i].gw_addr.s_addr, expected[i].gw_addr.s_addr); |
| 130 |
ASSERT_EQ(routes[i].dst_prefixlen, expected[i].dst_prefixlen); |
| 131 |
} |
| 132 |
} |
| 133 |
} |
| 134 |
|
| 135 |
static void verify_6rd( |
| 136 |
sd_dhcp_message *m, |
| 137 |
uint8_t expected_ipv4masklen, |
| 138 |
uint8_t expected_prefixlen, |
| 139 |
const struct in6_addr *expected_prefix, |
| 140 |
size_t expected_n_br_addresses, |
| 141 |
const struct in_addr *expected_br_addresses) { |
| 142 |
|
| 143 |
uint8_t ipv4masklen, prefixlen; |
| 144 |
struct in6_addr prefix; |
| 145 |
size_t n_br_addresses; |
| 146 |
_cleanup_free_ struct in_addr *br_addresses = NULL; |
| 147 |
|
| 148 |
ASSERT_OK(dhcp_message_get_option_6rd(m, NULL, NULL, NULL, NULL, NULL)); |
| 149 |
ASSERT_OK(dhcp_message_get_option_6rd(m, &ipv4masklen, &prefixlen, &prefix, &n_br_addresses, &br_addresses)); |
| 150 |
ASSERT_EQ(ipv4masklen, expected_ipv4masklen); |
| 151 |
ASSERT_EQ(prefixlen, expected_prefixlen); |
| 152 |
ASSERT_TRUE(in6_addr_equal(&prefix, expected_prefix)); |
| 153 |
ASSERT_EQ(n_br_addresses, expected_n_br_addresses); |
| 154 |
ASSERT_EQ(memcmp(br_addresses, expected_br_addresses, sizeof(struct in_addr) * n_br_addresses), 0); |
| 155 |
} |
| 156 |
|
| 157 |
static void verify_client_id(sd_dhcp_message *m, const sd_dhcp_client_id *expected) { |
| 158 |
sd_dhcp_client_id id = {}; |
| 159 |
ASSERT_OK(dhcp_message_get_option_client_id(m, &id)); |
| 160 |
ASSERT_EQ(client_id_compare_func(&id, expected), 0); |
| 161 |
} |
| 162 |
|
| 163 |
static void verify_prl(sd_dhcp_message *m, Set *expected) { |
| 164 |
_cleanup_set_free_ Set *set = NULL; |
| 165 |
ASSERT_OK(dhcp_message_get_option_parameter_request_list(m, &set)); |
| 166 |
ASSERT_TRUE(set_equal(set, expected)); |
| 167 |
} |
| 168 |
|
| 169 |
static void verify_hostname(sd_dhcp_message *m, const char *expected) { |
| 170 |
_cleanup_free_ char *s = NULL; |
| 171 |
ASSERT_OK(dhcp_message_get_option_hostname(m, &s)); |
| 172 |
ASSERT_STREQ(s, expected); |
| 173 |
} |
| 174 |
|
| 175 |
static void verify_sub_tlv(sd_dhcp_message *m, TLV *expected) { |
| 176 |
_cleanup_(tlv_unrefp) TLV *tlv = NULL; |
| 177 |
ASSERT_OK(dhcp_message_get_option_sub_tlv( |
| 178 |
m, |
| 179 |
SD_DHCP_OPTION_VENDOR_SPECIFIC_INFORMATION, |
| 180 |
TLV_DHCP4_SUBOPTION, |
| 181 |
&tlv)); |
| 182 |
|
| 183 |
_cleanup_(iovec_done) struct iovec iov = {}, iov_expected = {}; |
| 184 |
ASSERT_OK(tlv_build(tlv, &iov)); |
| 185 |
ASSERT_OK(tlv_build(expected, &iov_expected)); |
| 186 |
ASSERT_TRUE(iovec_equal(&iov, &iov_expected)); |
| 187 |
} |
| 188 |
|
| 189 |
static void verify_length_prefixed_data(sd_dhcp_message *m, const struct iovec_wrapper *expected) { |
| 190 |
_cleanup_(iovw_done_free) struct iovec_wrapper iovw = {}; |
| 191 |
ASSERT_OK(dhcp_message_get_option_length_prefixed_data(m, SD_DHCP_OPTION_USER_CLASS, 1, &iovw)); |
| 192 |
ASSERT_TRUE(iovw_equal(&iovw, expected)); |
| 193 |
} |
| 194 |
|
| 195 |
static void verify_send_udp(sd_dhcp_message *message, uint32_t xid, const struct hw_addr_data *hw_addr) { |
| 196 |
_cleanup_close_pair_ int socket_fd[2] = EBADF_PAIR; |
| 197 |
ASSERT_OK_ERRNO(socketpair(AF_UNIX, SOCK_SEQPACKET | SOCK_CLOEXEC | SOCK_NONBLOCK, 0, socket_fd)); |
| 198 |
|
| 199 |
ASSERT_OK(dhcp_message_send_udp( |
| 200 |
message, |
| 201 |
socket_fd[0], |
| 202 |
/* src_addr= */ htobe32(0xC0000201), /* 192.0.2.1 */ |
| 203 |
/* dst_addr= */ htobe32(0xC0000202), /* 192.0.2.2 */ |
| 204 |
/* dst_port= */ DHCP_PORT_CLIENT)); |
| 205 |
|
| 206 |
ssize_t buflen = ASSERT_OK_POSITIVE(next_datagram_size_fd(socket_fd[1])); |
| 207 |
_cleanup_free_ void *buf = ASSERT_NOT_NULL(malloc0(buflen)); |
| 208 |
|
| 209 |
struct msghdr msg = { |
| 210 |
.msg_iov = &IOVEC_MAKE(buf, buflen), |
| 211 |
.msg_iovlen = 1, |
| 212 |
}; |
| 213 |
ssize_t len = ASSERT_OK_ERRNO(recvmsg_safe(socket_fd[1], &msg, MSG_DONTWAIT)); |
| 214 |
ASSERT_EQ((size_t) len, dhcp_message_payload_size(message)); |
| 215 |
|
| 216 |
_cleanup_(sd_dhcp_message_unrefp) sd_dhcp_message *m = NULL; |
| 217 |
ASSERT_OK(dhcp_message_parse( |
| 218 |
&IOVEC_MAKE(buf, len), |
| 219 |
BOOTREQUEST, |
| 220 |
&xid, |
| 221 |
ARPHRD_ETHER, |
| 222 |
hw_addr, |
| 223 |
&m)); |
| 224 |
|
| 225 |
/* Verify the received message. */ |
| 226 |
_cleanup_(iovw_done_free) struct iovec_wrapper iovw = {}, iovw2 = {}; |
| 227 |
ASSERT_OK(dhcp_message_build(message, &iovw)); |
| 228 |
ASSERT_OK(dhcp_message_build(m, &iovw2)); |
| 229 |
ASSERT_TRUE(iovw_equal(&iovw, &iovw2)); |
| 230 |
} |
| 231 |
|
| 232 |
static void verify_send_raw(sd_dhcp_message *message, uint32_t xid, const struct hw_addr_data *hw_addr) { |
| 233 |
_cleanup_close_pair_ int socket_fd[2] = EBADF_PAIR; |
| 234 |
ASSERT_OK_ERRNO(socketpair(AF_UNIX, SOCK_SEQPACKET | SOCK_CLOEXEC | SOCK_NONBLOCK, 0, socket_fd)); |
| 235 |
|
| 236 |
ASSERT_OK(dhcp_message_send_raw( |
| 237 |
message, |
| 238 |
socket_fd[0], |
| 239 |
/* ifindex= */ 42, |
| 240 |
/* src_addr= */ htobe32(0xC0000201), /* 192.0.2.1 */ |
| 241 |
/* src_port= */ DHCP_PORT_SERVER, |
| 242 |
/* dst_hw_addr= */ &(struct hw_addr_data) { |
| 243 |
.length = 6, |
| 244 |
.ether = {{ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, }}, |
| 245 |
}, |
| 246 |
/* dst_addr= */ htobe32(0xC0000202), /* 192.0.2.2 */ |
| 247 |
/* dst_port= */ DHCP_PORT_CLIENT, |
| 248 |
/* ip_service_type= */ IPTOS_CLASS_CS6)); |
| 249 |
|
| 250 |
ssize_t buflen = ASSERT_OK_POSITIVE(next_datagram_size_fd(socket_fd[1])); |
| 251 |
_cleanup_free_ void *buf = ASSERT_NOT_NULL(malloc0(buflen)); |
| 252 |
|
| 253 |
struct msghdr msg = { |
| 254 |
.msg_iov = &IOVEC_MAKE(buf, buflen), |
| 255 |
.msg_iovlen = 1, |
| 256 |
}; |
| 257 |
ssize_t len = ASSERT_OK_ERRNO(recvmsg_safe(socket_fd[1], &msg, MSG_DONTWAIT)); |
| 258 |
ASSERT_EQ((size_t) len, dhcp_message_packet_size(message)); |
| 259 |
|
| 260 |
struct iovec payload; |
| 261 |
ASSERT_OK(udp_packet_verify( |
| 262 |
&IOVEC_MAKE(buf, len), |
| 263 |
DHCP_PORT_CLIENT, |
| 264 |
/* checksum= */ true, |
| 265 |
&payload)); |
| 266 |
|
| 267 |
_cleanup_(sd_dhcp_message_unrefp) sd_dhcp_message *m = NULL; |
| 268 |
ASSERT_OK(dhcp_message_parse( |
| 269 |
&payload, |
| 270 |
BOOTREQUEST, |
| 271 |
&xid, |
| 272 |
ARPHRD_ETHER, |
| 273 |
hw_addr, |
| 274 |
&m)); |
| 275 |
|
| 276 |
/* Verify the received message. */ |
| 277 |
_cleanup_(iovw_done_free) struct iovec_wrapper iovw = {}, iovw2 = {}; |
| 278 |
ASSERT_OK(dhcp_message_build(message, &iovw)); |
| 279 |
ASSERT_OK(dhcp_message_build(m, &iovw2)); |
| 280 |
ASSERT_TRUE(iovw_equal(&iovw, &iovw2)); |
| 281 |
} |
| 282 |
|
| 283 |
TEST(dhcp_message) { |
| 284 |
_cleanup_(sd_dhcp_message_unrefp) sd_dhcp_message *m = NULL; |
| 285 |
|
| 286 |
ASSERT_OK(dhcp_message_new(&m)); |
| 287 |
ASSERT_NOT_NULL(m); |
| 288 |
|
| 289 |
uint32_t xid = random_u32(); |
| 290 |
|
| 291 |
struct hw_addr_data hw_addr = { |
| 292 |
.length = ETH_ALEN, |
| 293 |
.ether = {{ 'A', 'B', 'C', '1', '2', '3' }}, |
| 294 |
}; |
| 295 |
|
| 296 |
usec_t lease_time = USEC_PER_DAY; |
| 297 |
|
| 298 |
/* 192.0.2.42 */ |
| 299 |
struct in_addr addr = { .s_addr = htobe32(0xC000022a) }; |
| 300 |
|
| 301 |
/* 192.0.2.1 - 4 */ |
| 302 |
struct in_addr ntp[4] = { |
| 303 |
{ .s_addr = htobe32(0xC0000201) }, |
| 304 |
{ .s_addr = htobe32(0xC0000202) }, |
| 305 |
{ .s_addr = htobe32(0xC0000203) }, |
| 306 |
{ .s_addr = htobe32(0xC0000204) }, |
| 307 |
}; |
| 308 |
|
| 309 |
/* 192.0.2.17 - 20 */ |
| 310 |
struct in_addr sip[4] = { |
| 311 |
{ .s_addr = htobe32(0xC0000211) }, |
| 312 |
{ .s_addr = htobe32(0xC0000212) }, |
| 313 |
{ .s_addr = htobe32(0xC0000213) }, |
| 314 |
{ .s_addr = htobe32(0xC0000214) }, |
| 315 |
}; |
| 316 |
|
| 317 |
struct sd_dhcp_route routes[3] = { |
| 318 |
{ /* class A: 10.0.0.0/8 -> 192.0.2.33 */ |
| 319 |
.dst_addr = { .s_addr = htobe32(0x0A000000) }, |
| 320 |
.gw_addr = { .s_addr = htobe32(0xC0000221) }, |
| 321 |
.dst_prefixlen = 8, |
| 322 |
}, |
| 323 |
{ /* class B: 172.16.0.0/16 -> 192.0.2.34 */ |
| 324 |
.dst_addr = { .s_addr = htobe32(0xAC100000) }, |
| 325 |
.gw_addr = { .s_addr = htobe32(0xC0000222) }, |
| 326 |
.dst_prefixlen = 16, |
| 327 |
}, |
| 328 |
{ /* class C: 192.168.0.0/24 -> 192.0.2.35 */ |
| 329 |
.dst_addr = { .s_addr = htobe32(0xC0A80000) }, |
| 330 |
.gw_addr = { .s_addr = htobe32(0xC0000223) }, |
| 331 |
.dst_prefixlen = 24, |
| 332 |
}, |
| 333 |
}; |
| 334 |
|
| 335 |
uint8_t sixrd_ipv4masklen = 24; |
| 336 |
uint8_t sixrd_prefixlen = 64; |
| 337 |
struct in6_addr sixrd_prefix = { |
| 338 |
.s6_addr = { 0x20, 0x01, 0x0d, 0xb8, }, |
| 339 |
}; |
| 340 |
struct in_addr sixrd_br_addresses[3] = { |
| 341 |
{ .s_addr = htobe32(0xC0000231) }, |
| 342 |
{ .s_addr = htobe32(0xC0000232) }, |
| 343 |
{ .s_addr = htobe32(0xC0000233) }, |
| 344 |
}; |
| 345 |
|
| 346 |
sd_dhcp_client_id id = { |
| 347 |
.raw = { 1, 3, 3, 3, 3, 3, 3, }, |
| 348 |
.size = 7, |
| 349 |
}; |
| 350 |
|
| 351 |
_cleanup_set_free_ Set *prl = NULL; |
| 352 |
for (uint8_t i = SD_DHCP_OPTION_PRIVATE_BASE; i <= SD_DHCP_OPTION_PRIVATE_LAST; i++) |
| 353 |
ASSERT_OK(set_ensure_put(&prl, /* hash_ops= */ NULL, UINT_TO_PTR(i))); |
| 354 |
|
| 355 |
const char *hostname = "test-node.example.com"; |
| 356 |
const char *vendor_class = "hogehoge"; |
| 357 |
char **root_path = STRV_MAKE("/path/to/root", "/hogehoge/foofoo"); |
| 358 |
|
| 359 |
_cleanup_(iovw_done_free) struct iovec_wrapper user_class = {}, user_class_1 = {}, user_class_2 = {}; |
| 360 |
FOREACH_STRING(s, "hoge", "foo", "bar") { |
| 361 |
ASSERT_OK(iovw_extend(&user_class, s, strlen(s))); |
| 362 |
ASSERT_OK(iovw_extend(&user_class_1, s, strlen(s))); |
| 363 |
} |
| 364 |
FOREACH_STRING(s, "aaa", "bbb", "ccc") { |
| 365 |
ASSERT_OK(iovw_extend(&user_class, s, strlen(s))); |
| 366 |
ASSERT_OK(iovw_extend(&user_class_2, s, strlen(s))); |
| 367 |
} |
| 368 |
|
| 369 |
_cleanup_(tlv_done) TLV vendor = TLV_INIT(TLV_DHCP4_SUBOPTION); |
| 370 |
for (unsigned i = 0; i < 3; i++) { |
| 371 |
uint8_t buf[255]; |
| 372 |
memset(buf, 42 + i, sizeof(buf)); |
| 373 |
ASSERT_OK(tlv_append(&vendor, i + 1, 255, buf)); |
| 374 |
} |
| 375 |
|
| 376 |
ASSERT_OK(dhcp_message_init_header( |
| 377 |
m, |
| 378 |
BOOTREQUEST, |
| 379 |
xid, |
| 380 |
ARPHRD_ETHER, |
| 381 |
&hw_addr)); |
| 382 |
|
| 383 |
/* header */ |
| 384 |
verify_header(m, xid, &hw_addr); |
| 385 |
|
| 386 |
ASSERT_ERROR(dhcp_message_append_option(m, SD_DHCP_OPTION_PAD, 0, NULL), EINVAL); |
| 387 |
ASSERT_ERROR(dhcp_message_append_option(m, SD_DHCP_OPTION_END, 0, NULL), EINVAL); |
| 388 |
|
| 389 |
/* multiple strings */ |
| 390 |
STRV_FOREACH(s, root_path) |
| 391 |
ASSERT_OK(dhcp_message_append_option(m, SD_DHCP_OPTION_ROOT_PATH, strlen(*s), *s)); |
| 392 |
verify_multiple_strings(m, root_path); |
| 393 |
|
| 394 |
/* flag */ |
| 395 |
ASSERT_ERROR(dhcp_message_get_option_flag(m, SD_DHCP_OPTION_RAPID_COMMIT), ENODATA); |
| 396 |
ASSERT_OK(dhcp_message_append_option_flag(m, SD_DHCP_OPTION_RAPID_COMMIT)); |
| 397 |
ASSERT_ERROR(dhcp_message_append_option_flag(m, SD_DHCP_OPTION_RAPID_COMMIT), EEXIST); |
| 398 |
verify_flag(m); |
| 399 |
|
| 400 |
/* u8 */ |
| 401 |
ASSERT_ERROR(dhcp_message_get_option_u8(m, SD_DHCP_OPTION_MESSAGE_TYPE, NULL), ENODATA); |
| 402 |
ASSERT_OK(dhcp_message_append_option_u8(m, SD_DHCP_OPTION_MESSAGE_TYPE, DHCP_DISCOVER)); |
| 403 |
ASSERT_ERROR(dhcp_message_append_option_u8(m, SD_DHCP_OPTION_MESSAGE_TYPE, DHCP_REQUEST), EEXIST); |
| 404 |
verify_u8(m, DHCP_DISCOVER); |
| 405 |
|
| 406 |
/* u16 */ |
| 407 |
ASSERT_OK(dhcp_message_append_option_u16(m, SD_DHCP_OPTION_MAXIMUM_MESSAGE_SIZE, 512)); |
| 408 |
ASSERT_ERROR(dhcp_message_append_option_u16(m, SD_DHCP_OPTION_MAXIMUM_MESSAGE_SIZE, 1024), EEXIST); |
| 409 |
ASSERT_ERROR(dhcp_message_append_option_u8(m, SD_DHCP_OPTION_MAXIMUM_MESSAGE_SIZE, 32), EEXIST); |
| 410 |
verify_u16(m, 512); |
| 411 |
|
| 412 |
/* sec */ |
| 413 |
ASSERT_OK(dhcp_message_append_option_sec(m, SD_DHCP_OPTION_IP_ADDRESS_LEASE_TIME, lease_time)); |
| 414 |
ASSERT_OK(dhcp_message_append_option_sec(m, SD_DHCP_OPTION_RENEWAL_TIME, USEC_INFINITY)); |
| 415 |
verify_sec(m, lease_time); |
| 416 |
|
| 417 |
/* address */ |
| 418 |
ASSERT_OK(dhcp_message_append_option_be32(m, SD_DHCP_OPTION_REQUESTED_IP_ADDRESS, addr.s_addr)); |
| 419 |
ASSERT_ERROR(dhcp_message_append_option_be32(m, SD_DHCP_OPTION_REQUESTED_IP_ADDRESS, addr.s_addr), EEXIST); |
| 420 |
ASSERT_ERROR(dhcp_message_append_option_address(m, SD_DHCP_OPTION_REQUESTED_IP_ADDRESS, &addr), EEXIST); |
| 421 |
dhcp_message_remove_option(m, SD_DHCP_OPTION_REQUESTED_IP_ADDRESS); |
| 422 |
ASSERT_OK(dhcp_message_append_option_address(m, SD_DHCP_OPTION_REQUESTED_IP_ADDRESS, &addr)); |
| 423 |
verify_address(m, &addr); |
| 424 |
|
| 425 |
/* multiple addresses */ |
| 426 |
ASSERT_OK(dhcp_message_append_option_address(m, SD_DHCP_OPTION_NTP_SERVER, &ntp[0])); |
| 427 |
ASSERT_OK(dhcp_message_append_option_addresses(m, SD_DHCP_OPTION_NTP_SERVER, ELEMENTSOF(ntp) - 1, ntp + 1)); |
| 428 |
ASSERT_OK(dhcp_message_append_option_addresses(m, SD_DHCP_OPTION_SIP_SERVER, ELEMENTSOF(sip), sip)); |
| 429 |
ASSERT_ERROR(dhcp_message_append_option_addresses(m, SD_DHCP_OPTION_SIP_SERVER, ELEMENTSOF(sip), sip), EEXIST); |
| 430 |
ASSERT_OK(dhcp_message_append_option_addresses(m, SD_DHCP_OPTION_SIP_SERVER, 0, NULL)); |
| 431 |
verify_addresses(m, ELEMENTSOF(ntp), ntp, ELEMENTSOF(sip), sip); |
| 432 |
|
| 433 |
/* string */ |
| 434 |
ASSERT_ERROR(dhcp_message_get_option_string(m, SD_DHCP_OPTION_VENDOR_CLASS_IDENTIFIER, NULL), ENODATA); |
| 435 |
ASSERT_OK(dhcp_message_append_option(m, SD_DHCP_OPTION_VENDOR_CLASS_IDENTIFIER, 0, NULL)); |
| 436 |
ASSERT_ERROR(dhcp_message_get_option_string(m, SD_DHCP_OPTION_VENDOR_CLASS_IDENTIFIER, NULL), ENODATA); |
| 437 |
ASSERT_OK(dhcp_message_append_option(m, SD_DHCP_OPTION_VENDOR_CLASS_IDENTIFIER, 1, "\0")); |
| 438 |
ASSERT_ERROR(dhcp_message_get_option_string(m, SD_DHCP_OPTION_VENDOR_CLASS_IDENTIFIER, NULL), ENODATA); |
| 439 |
ASSERT_OK(dhcp_message_append_option(m, SD_DHCP_OPTION_VENDOR_CLASS_IDENTIFIER, 9, "hoge\0hoge")); |
| 440 |
ASSERT_ERROR(dhcp_message_get_option_string(m, SD_DHCP_OPTION_VENDOR_CLASS_IDENTIFIER, NULL), EBADMSG); |
| 441 |
ASSERT_ERROR(dhcp_message_append_option_string(m, SD_DHCP_OPTION_VENDOR_CLASS_IDENTIFIER, vendor_class), EEXIST); |
| 442 |
dhcp_message_remove_option(m, SD_DHCP_OPTION_VENDOR_CLASS_IDENTIFIER); |
| 443 |
ASSERT_OK(dhcp_message_append_option_string(m, SD_DHCP_OPTION_VENDOR_CLASS_IDENTIFIER, vendor_class)); |
| 444 |
verify_string(m, vendor_class); |
| 445 |
|
| 446 |
/* routes */ |
| 447 |
ASSERT_OK(dhcp_message_append_option_routes(m, SD_DHCP_OPTION_STATIC_ROUTE, ELEMENTSOF(routes), routes)); |
| 448 |
ASSERT_OK(dhcp_message_append_option_routes(m, SD_DHCP_OPTION_CLASSLESS_STATIC_ROUTE, ELEMENTSOF(routes), routes)); |
| 449 |
ASSERT_OK(dhcp_message_append_option_routes(m, SD_DHCP_OPTION_PRIVATE_CLASSLESS_STATIC_ROUTE, ELEMENTSOF(routes), routes)); |
| 450 |
verify_routes(m, ELEMENTSOF(routes), routes); |
| 451 |
|
| 452 |
/* 6rd */ |
| 453 |
ASSERT_ERROR(dhcp_message_append_option_6rd(m, 33, sixrd_prefixlen, &sixrd_prefix, 1, sixrd_br_addresses), EINVAL); |
| 454 |
ASSERT_ERROR(dhcp_message_append_option_6rd(m, sixrd_ipv4masklen, 127, &sixrd_prefix, 1, sixrd_br_addresses), EINVAL); |
| 455 |
ASSERT_ERROR(dhcp_message_append_option_6rd(m, sixrd_ipv4masklen, sixrd_prefixlen, &sixrd_prefix, 0, sixrd_br_addresses), EINVAL); |
| 456 |
ASSERT_ERROR(dhcp_message_append_option_6rd(m, sixrd_ipv4masklen, sixrd_prefixlen, &sixrd_prefix, SIZE_MAX, sixrd_br_addresses), ENOBUFS); |
| 457 |
ASSERT_OK(dhcp_message_append_option_6rd(m, sixrd_ipv4masklen, sixrd_prefixlen, &sixrd_prefix, 1, sixrd_br_addresses)); |
| 458 |
ASSERT_ERROR(dhcp_message_append_option_6rd(m, sixrd_ipv4masklen, sixrd_prefixlen, &sixrd_prefix, 1, sixrd_br_addresses), EEXIST); |
| 459 |
ASSERT_OK(dhcp_message_append_option_addresses(m, SD_DHCP_OPTION_6RD, ELEMENTSOF(sixrd_br_addresses) - 1, sixrd_br_addresses + 1)); |
| 460 |
verify_6rd(m, sixrd_ipv4masklen, sixrd_prefixlen, &sixrd_prefix, ELEMENTSOF(sixrd_br_addresses), sixrd_br_addresses); |
| 461 |
|
| 462 |
/* client ID */ |
| 463 |
ASSERT_OK(dhcp_message_append_option_client_id(m, &id)); |
| 464 |
verify_client_id(m, &id); |
| 465 |
|
| 466 |
/* parameter request list */ |
| 467 |
ASSERT_OK(dhcp_message_append_option_parameter_request_list(m, prl)); |
| 468 |
ASSERT_OK(dhcp_message_append_option_parameter_request_list(m, prl)); |
| 469 |
verify_prl(m, prl); |
| 470 |
|
| 471 |
/* hostname */ |
| 472 |
ASSERT_OK(dhcp_message_append_option_hostname(m, /* flags= */ 0, /* is_client= */ false, "hogehoge")); |
| 473 |
ASSERT_ERROR(dhcp_message_append_option_hostname(m, /* flags= */ 0, /* is_client= */ false, hostname), EEXIST); |
| 474 |
dhcp_message_remove_option(m, SD_DHCP_OPTION_FQDN); |
| 475 |
ASSERT_ERROR(dhcp_message_append_option_hostname(m, /* flags= */ 0, /* is_client= */ false, hostname), EEXIST); |
| 476 |
dhcp_message_remove_option(m, SD_DHCP_OPTION_HOST_NAME); |
| 477 |
ASSERT_OK(dhcp_message_append_option_hostname(m, /* flags= */ 0, /* is_client= */ false, "hogehoge.example.com")); |
| 478 |
ASSERT_ERROR(dhcp_message_append_option_hostname(m, /* flags= */ 0, /* is_client= */ false, hostname), EEXIST); |
| 479 |
dhcp_message_remove_option(m, SD_DHCP_OPTION_HOST_NAME); |
| 480 |
ASSERT_ERROR(dhcp_message_append_option_hostname(m, /* flags= */ 0, /* is_client= */ false, hostname), EEXIST); |
| 481 |
dhcp_message_remove_option(m, SD_DHCP_OPTION_FQDN); |
| 482 |
ASSERT_OK(dhcp_message_append_option_hostname(m, /* flags= */ 0, /* is_client= */ false, hostname)); |
| 483 |
verify_hostname(m, hostname); |
| 484 |
|
| 485 |
/* vendor specific */ |
| 486 |
ASSERT_OK(dhcp_message_append_option_sub_tlv(m, SD_DHCP_OPTION_VENDOR_SPECIFIC_INFORMATION, &vendor)); |
| 487 |
ASSERT_ERROR(dhcp_message_append_option_sub_tlv(m, SD_DHCP_OPTION_VENDOR_SPECIFIC_INFORMATION, &vendor), EEXIST); |
| 488 |
verify_sub_tlv(m, &vendor); |
| 489 |
|
| 490 |
/* user class */ |
| 491 |
ASSERT_OK(dhcp_message_append_option_length_prefixed_data(m, SD_DHCP_OPTION_USER_CLASS, 1, &user_class_1)); |
| 492 |
ASSERT_OK(dhcp_message_append_option_length_prefixed_data(m, SD_DHCP_OPTION_USER_CLASS, 1, &user_class_2)); |
| 493 |
verify_length_prefixed_data(m, &user_class); |
| 494 |
|
| 495 |
/* build and parse */ |
| 496 |
_cleanup_(iovw_done_free) struct iovec_wrapper iovw = {}; |
| 497 |
ASSERT_OK(dhcp_message_build(m, &iovw)); |
| 498 |
|
| 499 |
_cleanup_(iovec_done) struct iovec joined = {}; |
| 500 |
ASSERT_OK(iovw_concat(&iovw, &joined)); |
| 501 |
ASSERT_EQ(joined.iov_len, dhcp_message_payload_size(m)); |
| 502 |
|
| 503 |
_cleanup_(sd_dhcp_message_unrefp) sd_dhcp_message *m2 = NULL; |
| 504 |
ASSERT_OK(dhcp_message_parse( |
| 505 |
&joined, |
| 506 |
BOOTREQUEST, |
| 507 |
&xid, |
| 508 |
ARPHRD_ETHER, |
| 509 |
&hw_addr, |
| 510 |
&m2)); |
| 511 |
|
| 512 |
ASSERT_EQ(memcmp(&m2->header, &m->header, sizeof(m->header)), 0); |
| 513 |
|
| 514 |
/* verify parsed message */ |
| 515 |
verify_header(m2, xid, &hw_addr); |
| 516 |
verify_multiple_strings(m2, root_path); |
| 517 |
verify_flag(m2); |
| 518 |
verify_u8(m2, DHCP_DISCOVER); |
| 519 |
verify_u16(m2, 512); |
| 520 |
verify_sec(m2, lease_time); |
| 521 |
verify_address(m2, &addr); |
| 522 |
verify_addresses(m2, ELEMENTSOF(ntp), ntp, ELEMENTSOF(sip), sip); |
| 523 |
verify_string(m2, vendor_class); |
| 524 |
verify_routes(m2, ELEMENTSOF(routes), routes); |
| 525 |
verify_6rd(m2, sixrd_ipv4masklen, sixrd_prefixlen, &sixrd_prefix, ELEMENTSOF(sixrd_br_addresses), sixrd_br_addresses); |
| 526 |
verify_client_id(m2, &id); |
| 527 |
verify_prl(m2, prl); |
| 528 |
verify_hostname(m2, hostname); |
| 529 |
verify_sub_tlv(m2, &vendor); |
| 530 |
verify_length_prefixed_data(m2, &user_class); |
| 531 |
|
| 532 |
/* build again, and verify the packet */ |
| 533 |
_cleanup_(iovw_done_free) struct iovec_wrapper iovw2 = {}; |
| 534 |
ASSERT_OK(dhcp_message_build(m2, &iovw2)); |
| 535 |
ASSERT_TRUE(iovw_equal(&iovw, &iovw2)); |
| 536 |
|
| 537 |
/* json */ |
| 538 |
_cleanup_(sd_json_variant_unrefp) sd_json_variant *v = NULL; |
| 539 |
ASSERT_OK(dhcp_message_build_json(m, &v)); |
| 540 |
|
| 541 |
_cleanup_(sd_dhcp_message_unrefp) sd_dhcp_message *m3 = NULL; |
| 542 |
ASSERT_OK(dhcp_message_parse_json(v, &m3)); |
| 543 |
|
| 544 |
_cleanup_(iovw_done_free) struct iovec_wrapper iovw3 = {}; |
| 545 |
ASSERT_OK(dhcp_message_build(m3, &iovw3)); |
| 546 |
ASSERT_TRUE(iovw_equal(&iovw, &iovw3)); |
| 547 |
|
| 548 |
/* send */ |
| 549 |
verify_send_udp(m, xid, &hw_addr); |
| 550 |
verify_send_raw(m, xid, &hw_addr); |
| 551 |
|
| 552 |
/* dump */ |
| 553 |
ASSERT_OK(dump_dhcp_message(m, /* args= */ NULL, DUMP_DHCP_MESSAGE_LEGEND | DUMP_DHCP_MESSAGE_FULL)); |
| 554 |
} |
| 555 |
|
| 556 |
static void test_domains_one(size_t len, const uint8_t *data, char * const *expected) { |
| 557 |
_cleanup_strv_free_ char **strv = NULL; |
| 558 |
_cleanup_(sd_dhcp_message_unrefp) sd_dhcp_message *m = NULL; |
| 559 |
ASSERT_OK(dhcp_message_new(&m)); |
| 560 |
|
| 561 |
ASSERT_OK(dhcp_message_append_option(m, SD_DHCP_OPTION_DOMAIN_SEARCH, len, data)); |
| 562 |
ASSERT_OK(dhcp_message_get_option_domains(m, SD_DHCP_OPTION_DOMAIN_SEARCH, &strv)); |
| 563 |
ASSERT_TRUE(strv_equal(strv, expected)); |
| 564 |
|
| 565 |
dhcp_message_remove_option(m, SD_DHCP_OPTION_DOMAIN_SEARCH); |
| 566 |
strv = strv_free(strv); |
| 567 |
|
| 568 |
ASSERT_OK(dhcp_message_append_option(m, SD_DHCP_OPTION_DOMAIN_SEARCH, len / 2, data)); |
| 569 |
ASSERT_OK(dhcp_message_append_option(m, SD_DHCP_OPTION_DOMAIN_SEARCH, len - len / 2, data + len / 2)); |
| 570 |
ASSERT_OK(dhcp_message_get_option_domains(m, SD_DHCP_OPTION_DOMAIN_SEARCH, &strv)); |
| 571 |
ASSERT_TRUE(strv_equal(strv, expected)); |
| 572 |
|
| 573 |
strv = strv_free(strv); |
| 574 |
|
| 575 |
_cleanup_free_ uint8_t *sip = new(uint8_t, len + 1); |
| 576 |
sip[0] = 0; |
| 577 |
memcpy(sip + 1, data, len); |
| 578 |
|
| 579 |
ASSERT_OK(dhcp_message_append_option(m, SD_DHCP_OPTION_SIP_SERVER, len + 1, sip)); |
| 580 |
ASSERT_OK(dhcp_message_get_option_domains(m, SD_DHCP_OPTION_SIP_SERVER, &strv)); |
| 581 |
ASSERT_TRUE(strv_equal(strv, expected)); |
| 582 |
|
| 583 |
dhcp_message_remove_option(m, SD_DHCP_OPTION_SIP_SERVER); |
| 584 |
strv = strv_free(strv); |
| 585 |
|
| 586 |
ASSERT_OK(dhcp_message_append_option(m, SD_DHCP_OPTION_SIP_SERVER, (len + 1) / 2, sip)); |
| 587 |
ASSERT_OK(dhcp_message_append_option(m, SD_DHCP_OPTION_SIP_SERVER, len + 1 - (len + 1) / 2, sip + (len + 1) / 2)); |
| 588 |
ASSERT_OK(dhcp_message_get_option_domains(m, SD_DHCP_OPTION_SIP_SERVER, &strv)); |
| 589 |
ASSERT_TRUE(strv_equal(strv, expected)); |
| 590 |
|
| 591 |
ASSERT_OK(dump_dhcp_message(m, STRV_MAKE("119", "120"), DUMP_DHCP_MESSAGE_LEGEND | DUMP_DHCP_MESSAGE_FULL)); |
| 592 |
} |
| 593 |
|
| 594 |
static void test_domains_fail(size_t len, const uint8_t *data) { |
| 595 |
_cleanup_(sd_dhcp_message_unrefp) sd_dhcp_message *m = NULL; |
| 596 |
ASSERT_OK(dhcp_message_new(&m)); |
| 597 |
|
| 598 |
ASSERT_OK(dhcp_message_append_option(m, SD_DHCP_OPTION_DOMAIN_SEARCH, len, data)); |
| 599 |
ASSERT_ERROR(dhcp_message_get_option_domains(m, SD_DHCP_OPTION_DOMAIN_SEARCH, /* ret= */ NULL), EBADMSG); |
| 600 |
|
| 601 |
dhcp_message_remove_option(m, SD_DHCP_OPTION_DOMAIN_SEARCH); |
| 602 |
|
| 603 |
_cleanup_free_ uint8_t *sip = new(uint8_t, len + 1); |
| 604 |
sip[0] = 0; |
| 605 |
memcpy(sip + 1, data, len); |
| 606 |
|
| 607 |
ASSERT_OK(dhcp_message_append_option(m, SD_DHCP_OPTION_SIP_SERVER, len + 1, sip)); |
| 608 |
ASSERT_ERROR(dhcp_message_get_option_domains(m, SD_DHCP_OPTION_SIP_SERVER, /* ret= */ NULL), EBADMSG); |
| 609 |
} |
| 610 |
|
| 611 |
TEST(domains) { |
| 612 |
/* simple */ |
| 613 |
static uint8_t simple[] = { |
| 614 |
7, 'e', 'x', 'a', 'm', 'p', 'l', 'e', |
| 615 |
3, 'c', 'o', 'm', |
| 616 |
0, |
| 617 |
4, 'h', 'o', 'g', 'e', |
| 618 |
3, 'f', 'o', 'o', |
| 619 |
0, |
| 620 |
}; |
| 621 |
test_domains_one(ELEMENTSOF(simple), simple, |
| 622 |
STRV_MAKE("example.com", "hoge.foo")); |
| 623 |
|
| 624 |
/* compressed */ |
| 625 |
static uint8_t compressed[] = { |
| 626 |
4, 'h', 'o', 'g', 'e', |
| 627 |
7, 'e', 'x', 'a', 'm', 'p', 'l', 'e', |
| 628 |
3, 'c', 'o', 'm', |
| 629 |
0, |
| 630 |
3, 'f', 'o', 'o', |
| 631 |
0xc0, 5, |
| 632 |
3, 'b', 'a', 'r', |
| 633 |
0xc0, 18, |
| 634 |
3, 'b', 'a', 'z', |
| 635 |
0xc0, 0, |
| 636 |
}; |
| 637 |
test_domains_one(ELEMENTSOF(compressed), compressed, |
| 638 |
STRV_MAKE("hoge.example.com", "foo.example.com", "bar.foo.example.com", "baz.hoge.example.com")); |
| 639 |
|
| 640 |
/* invalid pointer */ |
| 641 |
static uint8_t invalid[] = { |
| 642 |
7, 'e', 'x', 'a', 'm', 'p', 'l', 'e', |
| 643 |
3, 'c', 'o', 'm', |
| 644 |
0, |
| 645 |
3, 'f', 'o', 'o', |
| 646 |
0xc0, 0xff, |
| 647 |
}; |
| 648 |
test_domains_fail(ELEMENTSOF(invalid), invalid); |
| 649 |
|
| 650 |
/* forward pointer */ |
| 651 |
static uint8_t forward[] = { |
| 652 |
4, 'h', 'o', 'g', 'e', |
| 653 |
0xc0, 11, |
| 654 |
3, 'f', 'o', 'o', |
| 655 |
7, 'e', 'x', 'a', 'm', 'p', 'l', 'e', |
| 656 |
3, 'c', 'o', 'm', |
| 657 |
0, |
| 658 |
}; |
| 659 |
test_domains_fail(ELEMENTSOF(forward), forward); |
| 660 |
|
| 661 |
/* infinite loop */ |
| 662 |
static uint8_t loop1[] = { |
| 663 |
0xc0, 0x00, |
| 664 |
}; |
| 665 |
test_domains_fail(ELEMENTSOF(loop1), loop1); |
| 666 |
|
| 667 |
static uint8_t loop2[] = { |
| 668 |
0xc0, 0x02, |
| 669 |
0xc0, 0x00, |
| 670 |
}; |
| 671 |
test_domains_fail(ELEMENTSOF(loop2), loop2); |
| 672 |
|
| 673 |
static uint8_t loop3[] = { |
| 674 |
7, 'e', 'x', 'a', 'm', 'p', 'l', 'e', |
| 675 |
0xc0, 0x00, |
| 676 |
}; |
| 677 |
test_domains_fail(ELEMENTSOF(loop3), loop3); |
| 678 |
|
| 679 |
/* unterminated */ |
| 680 |
static uint8_t unterminated[] = { |
| 681 |
7, 'e', 'x', 'a', 'm', 'p', 'l', 'e', |
| 682 |
3, 'c', 'o', 'm', |
| 683 |
}; |
| 684 |
test_domains_fail(ELEMENTSOF(unterminated), unterminated); |
| 685 |
|
| 686 |
/* truncated label */ |
| 687 |
static uint8_t truncated[] = { |
| 688 |
7, 'e', 'x', 'a', 'm', 'p', 'l', 'e', |
| 689 |
3, 'c', 'o', |
| 690 |
}; |
| 691 |
test_domains_fail(ELEMENTSOF(truncated), truncated); |
| 692 |
} |
| 693 |
|
| 694 |
TEST(dnr) { |
| 695 |
_cleanup_(sd_dhcp_message_unrefp) sd_dhcp_message *m = NULL; |
| 696 |
ASSERT_OK(dhcp_message_new(&m)); |
| 697 |
|
| 698 |
sd_dns_resolver *resolvers = NULL; |
| 699 |
size_t n_resolvers = 0; |
| 700 |
CLEANUP_ARRAY(resolvers, n_resolvers, dns_resolver_free_array); |
| 701 |
|
| 702 |
static uint8_t data[] = { |
| 703 |
/* Instance 1 */ |
| 704 |
/* length */ |
| 705 |
0, 78, |
| 706 |
/* priority */ |
| 707 |
0, 1, |
| 708 |
/* authentication domain name */ |
| 709 |
22, |
| 710 |
8, 'r', 'e', 's', 'o', 'l', 'v', 'e', 'r', |
| 711 |
7, 'e', 'x', 'a', 'm', 'p', 'l', 'e', |
| 712 |
3, 'c', 'o', 'm', |
| 713 |
0, |
| 714 |
/* addresses */ |
| 715 |
8, |
| 716 |
192, 0, 2, 1, |
| 717 |
192, 0, 2, 2, |
| 718 |
/* service parameters */ |
| 719 |
/* ALPN */ |
| 720 |
0, DNS_SVC_PARAM_KEY_ALPN, |
| 721 |
0, 14, |
| 722 |
2, 'h', '2', |
| 723 |
2, 'h', '3', |
| 724 |
3, 'd', 'o', 't', |
| 725 |
3, 'd', 'o', 'q', |
| 726 |
/* port */ |
| 727 |
0, DNS_SVC_PARAM_KEY_PORT, |
| 728 |
0, 2, |
| 729 |
0, 42, |
| 730 |
/* DoH path*/ |
| 731 |
0, DNS_SVC_PARAM_KEY_DOHPATH, |
| 732 |
0, 16, |
| 733 |
'/', 'd', 'n', 's', '-', 'q', 'u', 'e', 'r', 'y', '{', '?', 'd', 'n', 's', '}', |
| 734 |
|
| 735 |
/* Instance 2 */ |
| 736 |
/* length */ |
| 737 |
0, 44, |
| 738 |
/* priority */ |
| 739 |
0, 2, |
| 740 |
/* authentication domain name */ |
| 741 |
22, |
| 742 |
8, 'h', 'o', 'g', 'e', 'h', 'o', 'g', 'e', |
| 743 |
7, 'e', 'x', 'a', 'm', 'p', 'l', 'e', |
| 744 |
3, 'c', 'o', 'm', |
| 745 |
0, |
| 746 |
/* addresses */ |
| 747 |
4, |
| 748 |
192, 0, 2, 3, |
| 749 |
/* service parameters */ |
| 750 |
/* ALPN */ |
| 751 |
0, DNS_SVC_PARAM_KEY_ALPN, |
| 752 |
0, 4, |
| 753 |
3, 'd', 'o', 't', |
| 754 |
/* port */ |
| 755 |
0, DNS_SVC_PARAM_KEY_PORT, |
| 756 |
0, 2, |
| 757 |
0, 33, |
| 758 |
|
| 759 |
/* Instance 3 (no address, ignored) */ |
| 760 |
/* length */ |
| 761 |
0, 20, |
| 762 |
/* priority */ |
| 763 |
0, 3, |
| 764 |
/* authentication domain name */ |
| 765 |
17, |
| 766 |
3, 'f', 'o', 'o', |
| 767 |
7, 'e', 'x', 'a', 'm', 'p', 'l', 'e', |
| 768 |
3, 'c', 'o', 'm', |
| 769 |
0, |
| 770 |
|
| 771 |
/* Instance 4 (unknown alpn, ignored) */ |
| 772 |
/* length */ |
| 773 |
0, 37, |
| 774 |
/* priority */ |
| 775 |
0, 4, |
| 776 |
/* authentication domain name */ |
| 777 |
20, |
| 778 |
6, 'b', 'a', 'r', 'b', 'a', 'z', |
| 779 |
7, 'e', 'x', 'a', 'm', 'p', 'l', 'e', |
| 780 |
3, 'c', 'o', 'm', |
| 781 |
0, |
| 782 |
/* addresses */ |
| 783 |
4, |
| 784 |
192, 0, 2, 4, |
| 785 |
/* service parameters */ |
| 786 |
/* ALPN */ |
| 787 |
0, DNS_SVC_PARAM_KEY_ALPN, |
| 788 |
0, 5, |
| 789 |
4, 'h', 'o', 'g', 'e', |
| 790 |
}; |
| 791 |
|
| 792 |
ASSERT_OK(dhcp_message_append_option(m, SD_DHCP_OPTION_V4_DNR, ELEMENTSOF(data), data)); |
| 793 |
ASSERT_OK(dhcp_message_get_option_dnr(m, &n_resolvers, &resolvers)); |
| 794 |
ASSERT_EQ(n_resolvers, 2u); |
| 795 |
|
| 796 |
ASSERT_EQ(resolvers[0].priority, 1u); |
| 797 |
ASSERT_STREQ(resolvers[0].auth_name, "resolver.example.com"); |
| 798 |
ASSERT_EQ(resolvers[0].family, AF_INET); |
| 799 |
ASSERT_EQ(resolvers[0].n_addrs, 2u); |
| 800 |
ASSERT_STREQ(IN_ADDR_TO_STRING(resolvers[0].family, &resolvers[0].addrs[0]), "192.0.2.1"); |
| 801 |
ASSERT_STREQ(IN_ADDR_TO_STRING(resolvers[0].family, &resolvers[0].addrs[1]), "192.0.2.2"); |
| 802 |
ASSERT_EQ(resolvers[0].transports, SD_DNS_ALPN_HTTP_2_TLS | SD_DNS_ALPN_HTTP_3 | SD_DNS_ALPN_DOT | SD_DNS_ALPN_DOQ); |
| 803 |
ASSERT_EQ(resolvers[0].port, 42u); |
| 804 |
ASSERT_STREQ(resolvers[0].dohpath, "/dns-query{?dns}"); |
| 805 |
|
| 806 |
ASSERT_EQ(resolvers[1].priority, 2u); |
| 807 |
ASSERT_STREQ(resolvers[1].auth_name, "hogehoge.example.com"); |
| 808 |
ASSERT_EQ(resolvers[1].family, AF_INET); |
| 809 |
ASSERT_EQ(resolvers[1].n_addrs, 1u); |
| 810 |
ASSERT_STREQ(IN_ADDR_TO_STRING(resolvers[1].family, &resolvers[1].addrs[0]), "192.0.2.3"); |
| 811 |
ASSERT_EQ(resolvers[1].transports, SD_DNS_ALPN_DOT); |
| 812 |
ASSERT_EQ(resolvers[1].port, 33u); |
| 813 |
ASSERT_NULL(resolvers[1].dohpath); |
| 814 |
|
| 815 |
ASSERT_OK(dump_dhcp_message(m, STRV_MAKE("162"), DUMP_DHCP_MESSAGE_LEGEND | DUMP_DHCP_MESSAGE_FULL)); |
| 816 |
|
| 817 |
/* missing DoH path */ |
| 818 |
static uint8_t invalid[] = { |
| 819 |
/* length */ |
| 820 |
0, 35, |
| 821 |
/* priority */ |
| 822 |
0, 5, |
| 823 |
/* authentication domain name */ |
| 824 |
20, |
| 825 |
6, 'b', 'a', 'r', 'b', 'a', 'z', |
| 826 |
7, 'e', 'x', 'a', 'm', 'p', 'l', 'e', |
| 827 |
3, 'c', 'o', 'm', |
| 828 |
0, |
| 829 |
/* addresses */ |
| 830 |
4, |
| 831 |
192, 0, 2, 5, |
| 832 |
/* service parameters */ |
| 833 |
/* ALPN */ |
| 834 |
0, DNS_SVC_PARAM_KEY_ALPN, |
| 835 |
0, 3, |
| 836 |
2, 'h', '2', |
| 837 |
}; |
| 838 |
ASSERT_OK(dhcp_message_append_option(m, SD_DHCP_OPTION_V4_DNR, ELEMENTSOF(invalid), invalid)); |
| 839 |
ASSERT_ERROR(dhcp_message_get_option_dnr(m, NULL, NULL), EBADMSG); |
| 840 |
|
| 841 |
dhcp_message_remove_option(m, SD_DHCP_OPTION_V4_DNR); |
| 842 |
|
| 843 |
/* missing ALPN */ |
| 844 |
static uint8_t invalid2[] = { |
| 845 |
/* length */ |
| 846 |
0, 28, |
| 847 |
/* priority */ |
| 848 |
0, 6, |
| 849 |
/* authentication domain name */ |
| 850 |
20, |
| 851 |
6, 'b', 'a', 'r', 'b', 'a', 'z', |
| 852 |
7, 'e', 'x', 'a', 'm', 'p', 'l', 'e', |
| 853 |
3, 'c', 'o', 'm', |
| 854 |
0, |
| 855 |
/* addresses */ |
| 856 |
4, |
| 857 |
192, 0, 2, 6, |
| 858 |
}; |
| 859 |
ASSERT_OK(dhcp_message_append_option(m, SD_DHCP_OPTION_V4_DNR, ELEMENTSOF(invalid2), invalid2)); |
| 860 |
ASSERT_ERROR(dhcp_message_get_option_dnr(m, NULL, NULL), EBADMSG); |
| 861 |
|
| 862 |
dhcp_message_remove_option(m, SD_DHCP_OPTION_V4_DNR); |
| 863 |
|
| 864 |
/* truncated domain name */ |
| 865 |
static uint8_t invalid3[] = { |
| 866 |
/* length */ |
| 867 |
0, 34, |
| 868 |
/* priority */ |
| 869 |
0, 7, |
| 870 |
/* authentication domain name */ |
| 871 |
18, |
| 872 |
6, 'b', 'a', 'r', 'b', 'a', 'z', |
| 873 |
7, 'e', 'x', 'a', 'm', 'p', 'l', 'e', |
| 874 |
3, 'c', 'o', |
| 875 |
/* addresses */ |
| 876 |
4, |
| 877 |
192, 0, 2, 7, |
| 878 |
/* service parameters */ |
| 879 |
/* ALPN */ |
| 880 |
0, DNS_SVC_PARAM_KEY_ALPN, |
| 881 |
0, 4, |
| 882 |
3, 'd', 'o', 't', |
| 883 |
}; |
| 884 |
ASSERT_OK(dhcp_message_append_option(m, SD_DHCP_OPTION_V4_DNR, ELEMENTSOF(invalid3), invalid3)); |
| 885 |
ASSERT_ERROR(dhcp_message_get_option_dnr(m, NULL, NULL), EMSGSIZE); |
| 886 |
} |
| 887 |
|
| 888 |
TEST(dump_vendor) { |
| 889 |
_cleanup_(sd_dhcp_message_unrefp) sd_dhcp_message *m = NULL; |
| 890 |
ASSERT_OK(dhcp_message_new(&m)); |
| 891 |
|
| 892 |
/* Vendor Specific Information (43) -- TLV */ |
| 893 |
static uint8_t option_43[] = { |
| 894 |
1, 6, 'm', 'o', 'd', 'e', 'm', '1', |
| 895 |
2, 6, 's', 't', 'a', 't', 'u', 's', |
| 896 |
3, 6, 1, 2, 3, 4, 5, 6, |
| 897 |
}; |
| 898 |
|
| 899 |
/* Vendor Class Identifier (60) -- typically string */ |
| 900 |
static uint8_t option_60[] = { |
| 901 |
'n', 'e', 't', 'w', 'o', 'r', 'k', 'd', |
| 902 |
}; |
| 903 |
|
| 904 |
/* User Class (77) -- length-prefixed data, typically strings */ |
| 905 |
static uint8_t option_77[] = { |
| 906 |
6, 'l', 'a', 'p', 't', 'o', 'p', |
| 907 |
4, 'c', 'o', 'r', 'p', |
| 908 |
}; |
| 909 |
|
| 910 |
/* Vendor-Identifying Vendor Class -- length-prefixed data tagged with enterprise number */ |
| 911 |
static uint8_t option_124[] = { |
| 912 |
0x00, 0x00, 0x00, 0x09, /* enterprise number: 9 */ |
| 913 |
13, |
| 914 |
5, 'c', 'i', 's', 'c', 'o', |
| 915 |
6, 'f', 'o', 'o', 'b', 'a', 'r', |
| 916 |
0x00, 0x00, 0x11, 0x8b, /* enterprise number: 4491 */ |
| 917 |
21, |
| 918 |
9, 'd', 'o', 'c', 's', 'i', 's', '3', '.', '0', |
| 919 |
10, 'e', 'R', 'o', 'u', 't', 'e', 'r', '1', '.', '0', |
| 920 |
}; |
| 921 |
|
| 922 |
/* Vendor-Identifying Vendor-Specific Information (125) -- sub TLVs with enterprise number */ |
| 923 |
static uint8_t option_125[] = { |
| 924 |
0x00, 0x00, 0x00, 0x09, /* enterprise number: 9 */ |
| 925 |
12, |
| 926 |
1, 4, 'C', 'i', 's', 'c', |
| 927 |
2, 4, 'I', 'O', 'S', 'X', |
| 928 |
0x00, 0x00, 0x11, 0x8b, /* enterprise number: 4491 */ |
| 929 |
16, |
| 930 |
1, 6, 'm', 'o', 'd', 'e', 'm', '1', |
| 931 |
2, 6, 's', 't', 'a', 't', 'u', 's', |
| 932 |
}; |
| 933 |
|
| 934 |
ASSERT_OK(dhcp_message_append_option(m, 43, ELEMENTSOF(option_43), option_43)); |
| 935 |
ASSERT_OK(dhcp_message_append_option(m, 60, ELEMENTSOF(option_60), option_60)); |
| 936 |
ASSERT_OK(dhcp_message_append_option(m, 77, ELEMENTSOF(option_77), option_77)); |
| 937 |
ASSERT_OK(dhcp_message_append_option(m, 124, ELEMENTSOF(option_124), option_124)); |
| 938 |
ASSERT_OK(dhcp_message_append_option(m, 125, ELEMENTSOF(option_125), option_125)); |
| 939 |
|
| 940 |
ASSERT_OK(dump_dhcp_message(m, STRV_MAKE("43", "60", "77", "124", "125"), |
| 941 |
DUMP_DHCP_MESSAGE_LEGEND | DUMP_DHCP_MESSAGE_FULL)); |
| 942 |
} |
| 943 |
|
| 944 |
DEFINE_TEST_MAIN(LOG_DEBUG); |