| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
|
| 3 |
#include "dns-question.h" |
| 4 |
#include "dns-rr.h" |
| 5 |
#include "dns-type.h" |
| 6 |
#include "in-addr-util.h" |
| 7 |
#include "memstream-util.h" |
| 8 |
#include "tests.h" |
| 9 |
|
| 10 |
/* ================================================================ |
| 11 |
* dns_question_add() |
| 12 |
* ================================================================ */ |
| 13 |
|
| 14 |
TEST(dns_question_add) { |
| 15 |
_cleanup_(dns_question_unrefp) DnsQuestion *question = NULL; |
| 16 |
_cleanup_(dns_resource_key_unrefp) DnsResourceKey *key = NULL; |
| 17 |
|
| 18 |
ASSERT_NOT_NULL(key = dns_resource_key_new(DNS_CLASS_IN, DNS_TYPE_A, "www.example.com")); |
| 19 |
|
| 20 |
/* NULL question */ |
| 21 |
ASSERT_EQ(dns_question_size(question), 0u); |
| 22 |
ASSERT_TRUE(dns_question_isempty(question)); |
| 23 |
|
| 24 |
/* zero-size question */ |
| 25 |
ASSERT_NOT_NULL(question = dns_question_new(0)); |
| 26 |
ASSERT_ERROR(dns_question_add(question, key, /* flags= */ 0), ENOSPC); |
| 27 |
ASSERT_OK_ZERO(dns_question_contains_key(question, key)); |
| 28 |
ASSERT_EQ(dns_question_size(question), 0u); |
| 29 |
ASSERT_TRUE(dns_question_isempty(question)); |
| 30 |
question = dns_question_unref(question); |
| 31 |
|
| 32 |
/* single question */ |
| 33 |
ASSERT_NOT_NULL(question = dns_question_new(1)); |
| 34 |
ASSERT_OK(dns_question_add(question, key, /* flags= */ 0)); |
| 35 |
ASSERT_OK_POSITIVE(dns_question_contains_key(question, key)); |
| 36 |
ASSERT_EQ(dns_question_size(question), 1u); |
| 37 |
ASSERT_FALSE(dns_question_isempty(question)); |
| 38 |
} |
| 39 |
|
| 40 |
/* ================================================================ |
| 41 |
* dns_question_new_address() |
| 42 |
* ================================================================ */ |
| 43 |
|
| 44 |
TEST(dns_question_new_address) { |
| 45 |
_cleanup_(dns_question_unrefp) DnsQuestion *question = NULL; |
| 46 |
_cleanup_(dns_resource_key_unrefp) DnsResourceKey *key4 = NULL, *key6 = NULL; |
| 47 |
|
| 48 |
ASSERT_NOT_NULL(key4 = dns_resource_key_new(DNS_CLASS_IN, DNS_TYPE_A, "www.example.com")); |
| 49 |
ASSERT_NOT_NULL(key6 = dns_resource_key_new(DNS_CLASS_IN, DNS_TYPE_AAAA, "www.example.com")); |
| 50 |
|
| 51 |
ASSERT_OK(dns_question_new_address(&question, AF_INET, "www.example.com", 0)); |
| 52 |
ASSERT_NOT_NULL(question); |
| 53 |
ASSERT_EQ(dns_question_size(question), 1u); |
| 54 |
ASSERT_OK_POSITIVE(dns_question_contains_key(question, key4)); |
| 55 |
ASSERT_OK_ZERO(dns_question_contains_key(question, key6)); |
| 56 |
question = dns_question_unref(question); |
| 57 |
|
| 58 |
ASSERT_OK(dns_question_new_address(&question, AF_INET6, "www.example.com", 0)); |
| 59 |
ASSERT_NOT_NULL(question); |
| 60 |
ASSERT_EQ(dns_question_size(question), 1u); |
| 61 |
ASSERT_OK_ZERO(dns_question_contains_key(question, key4)); |
| 62 |
ASSERT_OK_POSITIVE(dns_question_contains_key(question, key6)); |
| 63 |
} |
| 64 |
|
| 65 |
#if HAVE_LIBIDN2 |
| 66 |
TEST(dns_question_new_address_convert_idna) { |
| 67 |
_cleanup_(dns_question_unrefp) DnsQuestion *question = NULL; |
| 68 |
_cleanup_(dns_resource_key_unrefp) DnsResourceKey *key = NULL; |
| 69 |
|
| 70 |
ASSERT_OK(dns_question_new_address(&question, AF_INET, "www.\xF0\x9F\x98\xB1.com", 1)); |
| 71 |
ASSERT_NOT_NULL(question); |
| 72 |
|
| 73 |
ASSERT_NOT_NULL(key = dns_resource_key_new(DNS_CLASS_IN, DNS_TYPE_A, "www.xn--s38h.com")); |
| 74 |
|
| 75 |
ASSERT_EQ(dns_question_size(question), 1u); |
| 76 |
ASSERT_OK_POSITIVE(dns_question_contains_key(question, key)); |
| 77 |
} |
| 78 |
#endif |
| 79 |
|
| 80 |
/* ================================================================ |
| 81 |
* dns_question_new_reverse() |
| 82 |
* ================================================================ */ |
| 83 |
|
| 84 |
TEST(dns_question_new_reverse) { |
| 85 |
_cleanup_(dns_question_unrefp) DnsQuestion *question = NULL; |
| 86 |
_cleanup_(dns_resource_key_unrefp) DnsResourceKey *key = NULL; |
| 87 |
|
| 88 |
union in_addr_union addr = { .in.s_addr = htobe32(0xc0a8017f) }; |
| 89 |
|
| 90 |
ASSERT_OK(dns_question_new_reverse(&question, AF_INET, &addr)); |
| 91 |
ASSERT_NOT_NULL(question); |
| 92 |
|
| 93 |
ASSERT_NOT_NULL(key = dns_resource_key_new(DNS_CLASS_IN, DNS_TYPE_PTR, "127.1.168.192.in-addr.arpa")); |
| 94 |
|
| 95 |
ASSERT_EQ(dns_question_size(question), 1u); |
| 96 |
ASSERT_OK_POSITIVE(dns_question_contains_key(question, key)); |
| 97 |
} |
| 98 |
|
| 99 |
/* ================================================================ |
| 100 |
* dns_question_new_service() |
| 101 |
* ================================================================ */ |
| 102 |
|
| 103 |
TEST(dns_question_new_service) { |
| 104 |
_cleanup_(dns_question_unrefp) DnsQuestion *question = NULL; |
| 105 |
_cleanup_(dns_resource_key_unrefp) DnsResourceKey *key = NULL; |
| 106 |
|
| 107 |
/* no domain */ |
| 108 |
ASSERT_ERROR(dns_question_new_service( |
| 109 |
&question, |
| 110 |
/* service= */ NULL, |
| 111 |
"_xmpp._tcp", |
| 112 |
/* domain= */ NULL, |
| 113 |
/* with_txt= */ false, |
| 114 |
/* convert_idna= */ false), EINVAL); |
| 115 |
ASSERT_NULL(question); |
| 116 |
|
| 117 |
/* domain only */ |
| 118 |
ASSERT_OK(dns_question_new_service( |
| 119 |
&question, |
| 120 |
/* service= */ NULL, |
| 121 |
/* type= */ NULL, |
| 122 |
"www.example.com", |
| 123 |
/* with_txt= */ false, |
| 124 |
/* convert_idna= */ false)); |
| 125 |
ASSERT_NOT_NULL(question); |
| 126 |
ASSERT_EQ(dns_question_size(question), 1u); |
| 127 |
ASSERT_NOT_NULL(key = dns_resource_key_new(DNS_CLASS_IN, DNS_TYPE_SRV, "www.example.com")); |
| 128 |
ASSERT_OK_POSITIVE(dns_question_contains_key(question, key)); |
| 129 |
|
| 130 |
question = dns_question_unref(question); |
| 131 |
key = dns_resource_key_unref(key); |
| 132 |
|
| 133 |
/* convert idna without type -> ignored */ |
| 134 |
ASSERT_OK(dns_question_new_service( |
| 135 |
&question, |
| 136 |
/* service= */ NULL, |
| 137 |
/* type= */ NULL, |
| 138 |
"\xF0\x9F\x98\xB1.com", |
| 139 |
/* with_txt= */ false, |
| 140 |
/* convert_idna= */ true)); |
| 141 |
ASSERT_NOT_NULL(question); |
| 142 |
ASSERT_EQ(dns_question_size(question), 1u); |
| 143 |
ASSERT_NOT_NULL(key = dns_resource_key_new(DNS_CLASS_IN, DNS_TYPE_SRV, "\xF0\x9F\x98\xB1.com")); |
| 144 |
ASSERT_OK_POSITIVE(dns_question_contains_key(question, key)); |
| 145 |
|
| 146 |
question = dns_question_unref(question); |
| 147 |
key = dns_resource_key_unref(key); |
| 148 |
|
| 149 |
/* with type */ |
| 150 |
ASSERT_OK(dns_question_new_service( |
| 151 |
&question, |
| 152 |
/* service= */ NULL, |
| 153 |
"_xmpp._tcp", |
| 154 |
"example.com", |
| 155 |
/* with_txt= */ false, |
| 156 |
/* convert_idna= */ false)); |
| 157 |
ASSERT_NOT_NULL(question); |
| 158 |
ASSERT_EQ(dns_question_size(question), 1u); |
| 159 |
ASSERT_NOT_NULL(key = dns_resource_key_new(DNS_CLASS_IN, DNS_TYPE_SRV, "_xmpp._tcp.example.com")); |
| 160 |
ASSERT_OK_POSITIVE(dns_question_contains_key(question, key)); |
| 161 |
|
| 162 |
question = dns_question_unref(question); |
| 163 |
key = dns_resource_key_unref(key); |
| 164 |
|
| 165 |
#if HAVE_LIBIDN2 |
| 166 |
/* convert idna with type */ |
| 167 |
ASSERT_OK(dns_question_new_service( |
| 168 |
&question, |
| 169 |
/* service= */ NULL, |
| 170 |
"_xmpp._tcp", |
| 171 |
"\xF0\x9F\x98\xB1.com", |
| 172 |
/* with_txt= */ false, |
| 173 |
/* convert_idna= */ true)); |
| 174 |
ASSERT_NOT_NULL(question); |
| 175 |
ASSERT_EQ(dns_question_size(question), 1u); |
| 176 |
ASSERT_NOT_NULL(key = dns_resource_key_new(DNS_CLASS_IN, DNS_TYPE_SRV, "_xmpp._tcp.xn--s38h.com")); |
| 177 |
ASSERT_OK_POSITIVE(dns_question_contains_key(question, key)); |
| 178 |
|
| 179 |
question = dns_question_unref(question); |
| 180 |
key = dns_resource_key_unref(key); |
| 181 |
|
| 182 |
/* with txt */ |
| 183 |
ASSERT_OK(dns_question_new_service( |
| 184 |
&question, |
| 185 |
/* service= */ NULL, |
| 186 |
"_xmpp._tcp", |
| 187 |
"\xF0\x9F\x98\xB1.com", |
| 188 |
/* with_txt= */ true, |
| 189 |
/* convert_idna= */ true)); |
| 190 |
ASSERT_NOT_NULL(question); |
| 191 |
ASSERT_EQ(dns_question_size(question), 2u); |
| 192 |
ASSERT_NOT_NULL(key = dns_resource_key_new(DNS_CLASS_IN, DNS_TYPE_SRV, "_xmpp._tcp.xn--s38h.com")); |
| 193 |
ASSERT_OK_POSITIVE(dns_question_contains_key(question, key)); |
| 194 |
dns_resource_key_unref(key); |
| 195 |
ASSERT_NOT_NULL(key = dns_resource_key_new(DNS_CLASS_IN, DNS_TYPE_TXT, "_xmpp._tcp.xn--s38h.com")); |
| 196 |
ASSERT_OK_POSITIVE(dns_question_contains_key(question, key)); |
| 197 |
|
| 198 |
question = dns_question_unref(question); |
| 199 |
key = dns_resource_key_unref(key); |
| 200 |
#endif |
| 201 |
|
| 202 |
/* invalid type */ |
| 203 |
ASSERT_ERROR(dns_question_new_service( |
| 204 |
&question, |
| 205 |
/* service= */ NULL, |
| 206 |
"_xmpp.tcp", |
| 207 |
"example.com", |
| 208 |
/* with_txt= */ false, |
| 209 |
/* convert_idna= */ false), EINVAL); |
| 210 |
ASSERT_NULL(question); |
| 211 |
|
| 212 |
/* invalid type (too short) */ |
| 213 |
ASSERT_ERROR(dns_question_new_service( |
| 214 |
&question, |
| 215 |
/* service= */ NULL, |
| 216 |
"_xmpp", |
| 217 |
"example.com", |
| 218 |
/* with_txt= */ false, |
| 219 |
/* convert_idna= */ false), EINVAL); |
| 220 |
ASSERT_NULL(question); |
| 221 |
|
| 222 |
/* invalid type (too long) */ |
| 223 |
ASSERT_ERROR(dns_question_new_service( |
| 224 |
&question, |
| 225 |
/* service= */ NULL, |
| 226 |
"_xmpp._tcp._extra", |
| 227 |
"example.com", |
| 228 |
/* with_txt= */ false, |
| 229 |
/* convert_idna= */ false), EINVAL); |
| 230 |
ASSERT_NULL(question); |
| 231 |
|
| 232 |
/* with service and type */ |
| 233 |
ASSERT_OK(dns_question_new_service( |
| 234 |
&question, |
| 235 |
"service", |
| 236 |
"_xmpp._tcp", |
| 237 |
"example.com", |
| 238 |
/* with_txt= */ false, |
| 239 |
/* convert_idna= */ false)); |
| 240 |
ASSERT_NOT_NULL(question); |
| 241 |
ASSERT_EQ(dns_question_size(question), 1u); |
| 242 |
ASSERT_NOT_NULL(key = dns_resource_key_new(DNS_CLASS_IN, DNS_TYPE_SRV, "service._xmpp._tcp.example.com")); |
| 243 |
ASSERT_OK_POSITIVE(dns_question_contains_key(question, key)); |
| 244 |
|
| 245 |
question = dns_question_unref(question); |
| 246 |
key = dns_resource_key_unref(key); |
| 247 |
|
| 248 |
/* with service but without type */ |
| 249 |
ASSERT_ERROR(dns_question_new_service( |
| 250 |
&question, |
| 251 |
"service", |
| 252 |
/* type= */ NULL, |
| 253 |
"example.com", |
| 254 |
/* with_txt= */ false, |
| 255 |
/* convert_idna= */ false), EINVAL); |
| 256 |
ASSERT_NULL(question); |
| 257 |
} |
| 258 |
|
| 259 |
/* ================================================================ |
| 260 |
* dns_question_matches_rr() |
| 261 |
* ================================================================ */ |
| 262 |
|
| 263 |
TEST(dns_question_matches_rr) { |
| 264 |
_cleanup_(dns_question_unrefp) DnsQuestion *question = NULL; |
| 265 |
DnsResourceKey *key; |
| 266 |
DnsResourceRecord *rr; |
| 267 |
|
| 268 |
ASSERT_NOT_NULL(question = dns_question_new(2)); |
| 269 |
|
| 270 |
ASSERT_NOT_NULL(key = dns_resource_key_new(DNS_CLASS_IN, DNS_TYPE_A, "www.example.com")); |
| 271 |
ASSERT_OK(dns_question_add(question, key, /* flags= */ 0)); |
| 272 |
dns_resource_key_unref(key); |
| 273 |
|
| 274 |
ASSERT_NOT_NULL(key = dns_resource_key_new(DNS_CLASS_IN, DNS_TYPE_A, "mail.example.com")); |
| 275 |
ASSERT_OK(dns_question_add(question, key, /* flags= */ 0)); |
| 276 |
dns_resource_key_unref(key); |
| 277 |
|
| 278 |
ASSERT_NOT_NULL(rr = dns_resource_record_new_full(DNS_CLASS_IN, DNS_TYPE_A, "www.example.com")); |
| 279 |
ASSERT_OK_POSITIVE(dns_question_matches_rr(question, rr, /* search_domain= */ NULL)); |
| 280 |
dns_resource_record_unref(rr); |
| 281 |
|
| 282 |
ASSERT_NOT_NULL(rr = dns_resource_record_new_full(DNS_CLASS_IN, DNS_TYPE_A, "mail.example.com")); |
| 283 |
ASSERT_OK_POSITIVE(dns_question_matches_rr(question, rr, /* search_domain= */ NULL)); |
| 284 |
dns_resource_record_unref(rr); |
| 285 |
|
| 286 |
ASSERT_NOT_NULL(rr = dns_resource_record_new_full(DNS_CLASS_IN, DNS_TYPE_CNAME, "mail.example.com")); |
| 287 |
ASSERT_OK_ZERO(dns_question_matches_rr(question, rr, /* search_domain= */ NULL)); |
| 288 |
dns_resource_record_unref(rr); |
| 289 |
} |
| 290 |
|
| 291 |
/* ================================================================ |
| 292 |
* dns_question_matches_cname_or_dname() |
| 293 |
* ================================================================ */ |
| 294 |
|
| 295 |
TEST(dns_question_matches_cname_or_dname) { |
| 296 |
_cleanup_(dns_question_unrefp) DnsQuestion *question = NULL; |
| 297 |
_cleanup_(dns_resource_key_unrefp) DnsResourceKey *keya = NULL, *keyc = NULL; |
| 298 |
DnsResourceRecord *rr; |
| 299 |
|
| 300 |
ASSERT_NOT_NULL(question = dns_question_new(1)); |
| 301 |
ASSERT_NOT_NULL(keya = dns_resource_key_new(DNS_CLASS_IN, DNS_TYPE_A, "www.example.com")); |
| 302 |
ASSERT_OK(dns_question_add(question, keya, /* flags= */ 0)); |
| 303 |
|
| 304 |
/* cname */ |
| 305 |
ASSERT_NOT_NULL(rr = dns_resource_record_new_full(DNS_CLASS_IN, DNS_TYPE_CNAME, "www.example.com")); |
| 306 |
ASSERT_OK_POSITIVE(dns_question_matches_cname_or_dname(question, rr, /* search_domain= */ NULL)); |
| 307 |
dns_resource_record_unref(rr); |
| 308 |
|
| 309 |
/* dname */ |
| 310 |
ASSERT_NOT_NULL(rr = dns_resource_record_new_full(DNS_CLASS_IN, DNS_TYPE_DNAME, "example.com")); |
| 311 |
ASSERT_OK_POSITIVE(dns_question_matches_cname_or_dname(question, rr, /* search_domain= */ NULL)); |
| 312 |
dns_resource_record_unref(rr); |
| 313 |
|
| 314 |
/* A record -> fail */ |
| 315 |
ASSERT_NOT_NULL(rr = dns_resource_record_new_full(DNS_CLASS_IN, DNS_TYPE_A, "www.example.com")); |
| 316 |
ASSERT_OK_ZERO(dns_question_matches_cname_or_dname(question, rr, /* search_domain= */ NULL)); |
| 317 |
dns_resource_record_unref(rr); |
| 318 |
|
| 319 |
dns_question_unref(question); |
| 320 |
ASSERT_NOT_NULL(question = dns_question_new(2)); |
| 321 |
|
| 322 |
ASSERT_NOT_NULL(keyc = dns_resource_key_new(DNS_CLASS_IN, DNS_TYPE_CNAME, "example.com")); |
| 323 |
ASSERT_OK(dns_question_add(question, keyc, /* flags= */ 0)); |
| 324 |
ASSERT_OK(dns_question_add(question, keya, /* flags= */ 0)); |
| 325 |
|
| 326 |
/* refuse cname if question has cname */ |
| 327 |
ASSERT_NOT_NULL(rr = dns_resource_record_new_full(DNS_CLASS_IN, DNS_TYPE_CNAME, "www.example.com")); |
| 328 |
ASSERT_OK_ZERO(dns_question_matches_cname_or_dname(question, rr, /* search_domain= */ NULL)); |
| 329 |
dns_resource_record_unref(rr); |
| 330 |
} |
| 331 |
|
| 332 |
/* ================================================================ |
| 333 |
* dns_question_is_valid_for_query() |
| 334 |
* ================================================================ */ |
| 335 |
|
| 336 |
TEST(dns_question_is_valid_for_query) { |
| 337 |
_cleanup_(dns_question_unrefp) DnsQuestion *question = NULL; |
| 338 |
_cleanup_(dns_resource_key_unrefp) DnsResourceKey *key = NULL; |
| 339 |
|
| 340 |
/* NULL question */ |
| 341 |
ASSERT_OK_ZERO(dns_question_is_valid_for_query(question)); |
| 342 |
|
| 343 |
/* empty question */ |
| 344 |
ASSERT_NOT_NULL(question = dns_question_new(0)); |
| 345 |
ASSERT_OK_ZERO(dns_question_is_valid_for_query(question)); |
| 346 |
|
| 347 |
question = dns_question_unref(question); |
| 348 |
|
| 349 |
/* single key */ |
| 350 |
ASSERT_NOT_NULL(question = dns_question_new(1)); |
| 351 |
ASSERT_NOT_NULL(key = dns_resource_key_new(DNS_CLASS_IN, DNS_TYPE_A, "www.example.com")); |
| 352 |
ASSERT_OK(dns_question_add(question, key, /* flags= */ 0)); |
| 353 |
ASSERT_OK_POSITIVE(dns_question_is_valid_for_query(question)); |
| 354 |
|
| 355 |
question = dns_question_unref(question); |
| 356 |
key = dns_resource_key_unref(key); |
| 357 |
|
| 358 |
/* invalid type */ |
| 359 |
ASSERT_NOT_NULL(question = dns_question_new(1)); |
| 360 |
ASSERT_NOT_NULL(key = dns_resource_key_new(DNS_CLASS_IN, DNS_TYPE_OPT, "www.example.com")); |
| 361 |
ASSERT_OK(dns_question_add(question, key, /* flags= */ 0)); |
| 362 |
ASSERT_OK_ZERO(dns_question_is_valid_for_query(question)); |
| 363 |
|
| 364 |
question = dns_question_unref(question); |
| 365 |
key = dns_resource_key_unref(key); |
| 366 |
|
| 367 |
/* multiple keys with the same name */ |
| 368 |
ASSERT_NOT_NULL(question = dns_question_new(2)); |
| 369 |
ASSERT_NOT_NULL(key = dns_resource_key_new(DNS_CLASS_IN, DNS_TYPE_A, "www.example.com")); |
| 370 |
ASSERT_OK(dns_question_add(question, key, /* flags= */ 0)); |
| 371 |
dns_resource_key_unref(key); |
| 372 |
ASSERT_NOT_NULL(key = dns_resource_key_new(DNS_CLASS_IN, DNS_TYPE_AAAA, "www.EXAMPLE.com")); |
| 373 |
ASSERT_OK(dns_question_add(question, key, /* flags= */ 0)); |
| 374 |
ASSERT_OK_POSITIVE(dns_question_is_valid_for_query(question)); |
| 375 |
|
| 376 |
question = dns_question_unref(question); |
| 377 |
key = dns_resource_key_unref(key); |
| 378 |
|
| 379 |
/* multiple keys with different names */ |
| 380 |
ASSERT_NOT_NULL(question = dns_question_new(2)); |
| 381 |
ASSERT_NOT_NULL(key = dns_resource_key_new(DNS_CLASS_IN, DNS_TYPE_A, "www.example.com")); |
| 382 |
ASSERT_OK(dns_question_add(question, key, /* flags= */ 0)); |
| 383 |
dns_resource_key_unref(key); |
| 384 |
ASSERT_NOT_NULL(key = dns_resource_key_new(DNS_CLASS_IN, DNS_TYPE_AAAA, "www.example.org")); |
| 385 |
ASSERT_OK(dns_question_add(question, key, /* flags= */ 0)); |
| 386 |
ASSERT_OK_ZERO(dns_question_is_valid_for_query(question)); |
| 387 |
} |
| 388 |
|
| 389 |
/* ================================================================ |
| 390 |
* dns_question_is_equal() |
| 391 |
* ================================================================ */ |
| 392 |
|
| 393 |
TEST(dns_question_is_equal) { |
| 394 |
_cleanup_(dns_question_unrefp) DnsQuestion *a = NULL, *b = NULL; |
| 395 |
DnsResourceKey *key; |
| 396 |
|
| 397 |
/* NULL */ |
| 398 |
ASSERT_OK_POSITIVE(dns_question_is_equal(NULL, NULL)); |
| 399 |
|
| 400 |
/* empty questions */ |
| 401 |
ASSERT_NOT_NULL(a = dns_question_new(0)); |
| 402 |
ASSERT_NOT_NULL(b = dns_question_new(0)); |
| 403 |
ASSERT_OK_POSITIVE(dns_question_is_equal(a, NULL)); |
| 404 |
ASSERT_OK_POSITIVE(dns_question_is_equal(NULL, a)); |
| 405 |
ASSERT_OK_POSITIVE(dns_question_is_equal(a, a)); |
| 406 |
ASSERT_OK_POSITIVE(dns_question_is_equal(a, b)); |
| 407 |
|
| 408 |
a = dns_question_unref(a); |
| 409 |
|
| 410 |
/* an address question (with NULL, self, and an empty) */ |
| 411 |
ASSERT_OK(dns_question_new_address(&a, AF_INET, "www.example.com", /* convert_idna= */ false)); |
| 412 |
ASSERT_NOT_NULL(a); |
| 413 |
ASSERT_OK_ZERO(dns_question_is_equal(a, NULL)); |
| 414 |
ASSERT_OK_ZERO(dns_question_is_equal(NULL, a)); |
| 415 |
ASSERT_OK_POSITIVE(dns_question_is_equal(a, a)); |
| 416 |
ASSERT_OK_ZERO(dns_question_is_equal(a, b)); |
| 417 |
ASSERT_OK_ZERO(dns_question_is_equal(b, a)); |
| 418 |
|
| 419 |
b = dns_question_unref(b); |
| 420 |
|
| 421 |
/* an address question (with same name) */ |
| 422 |
ASSERT_OK(dns_question_new_address(&b, AF_INET, "www.EXAMPLE.com", /* convert_idna= */ false)); |
| 423 |
ASSERT_NOT_NULL(b); |
| 424 |
ASSERT_OK_POSITIVE(dns_question_is_equal(a, b)); |
| 425 |
ASSERT_OK_POSITIVE(dns_question_is_equal(b, a)); |
| 426 |
|
| 427 |
b = dns_question_unref(b); |
| 428 |
|
| 429 |
/* an address question (with different name) */ |
| 430 |
ASSERT_OK(dns_question_new_address(&b, AF_INET, "www.EXAMPLE.org", /* convert_idna= */ false)); |
| 431 |
ASSERT_NOT_NULL(b); |
| 432 |
ASSERT_OK_ZERO(dns_question_is_equal(a, b)); |
| 433 |
ASSERT_OK_ZERO(dns_question_is_equal(b, a)); |
| 434 |
|
| 435 |
b = dns_question_unref(b); |
| 436 |
|
| 437 |
/* an address question (with different type) */ |
| 438 |
ASSERT_OK(dns_question_new_address(&b, AF_INET6, "www.example.com", /* convert_idna= */ false)); |
| 439 |
ASSERT_NOT_NULL(b); |
| 440 |
ASSERT_OK_ZERO(dns_question_is_equal(a, b)); |
| 441 |
ASSERT_OK_ZERO(dns_question_is_equal(b, a)); |
| 442 |
|
| 443 |
b = dns_question_unref(b); |
| 444 |
|
| 445 |
/* number of keys are different */ |
| 446 |
ASSERT_NOT_NULL(b = dns_question_new(2)); |
| 447 |
|
| 448 |
ASSERT_NOT_NULL(key = dns_resource_key_new(DNS_CLASS_IN, DNS_TYPE_A, "www.example.com")); |
| 449 |
ASSERT_OK(dns_question_add(b, key, /* flags= */ 0)); |
| 450 |
dns_resource_key_unref(key); |
| 451 |
|
| 452 |
ASSERT_NOT_NULL(key = dns_resource_key_new(DNS_CLASS_IN, DNS_TYPE_AAAA, "www.example.com")); |
| 453 |
ASSERT_OK(dns_question_add(b, key, /* flags= */ 0)); |
| 454 |
dns_resource_key_unref(key); |
| 455 |
|
| 456 |
ASSERT_OK_ZERO(dns_question_is_equal(a, b)); |
| 457 |
ASSERT_OK_ZERO(dns_question_is_equal(b, a)); |
| 458 |
|
| 459 |
a = dns_question_unref(a); |
| 460 |
|
| 461 |
/* same keys with different order */ |
| 462 |
ASSERT_NOT_NULL(a = dns_question_new(2)); |
| 463 |
|
| 464 |
ASSERT_NOT_NULL(key = dns_resource_key_new(DNS_CLASS_IN, DNS_TYPE_AAAA, "www.example.com")); |
| 465 |
ASSERT_OK(dns_question_add(a, key, /* flags= */ 0)); |
| 466 |
dns_resource_key_unref(key); |
| 467 |
|
| 468 |
ASSERT_NOT_NULL(key = dns_resource_key_new(DNS_CLASS_IN, DNS_TYPE_A, "www.example.com")); |
| 469 |
ASSERT_OK(dns_question_add(a, key, /* flags= */ 0)); |
| 470 |
dns_resource_key_unref(key); |
| 471 |
|
| 472 |
ASSERT_OK_POSITIVE(dns_question_is_equal(a, b)); |
| 473 |
ASSERT_OK_POSITIVE(dns_question_is_equal(b, a)); |
| 474 |
} |
| 475 |
|
| 476 |
/* ================================================================ |
| 477 |
* dns_question_cname_redirect() |
| 478 |
* ================================================================ */ |
| 479 |
|
| 480 |
TEST(dns_question_cname_redirect) { |
| 481 |
_cleanup_(dns_question_unrefp) DnsQuestion *question = NULL, *expected = NULL, *ret = NULL; |
| 482 |
_cleanup_(dns_resource_record_unrefp) DnsResourceRecord *rr = NULL; |
| 483 |
DnsResourceKey *key; |
| 484 |
|
| 485 |
/* prepare cname record */ |
| 486 |
ASSERT_NOT_NULL(rr = dns_resource_record_new_full(DNS_CLASS_IN, DNS_TYPE_CNAME, "www.example.com")); |
| 487 |
rr->cname.name = strdup("example.com"); |
| 488 |
|
| 489 |
ASSERT_OK(dns_question_new_address(&expected, AF_INET, "example.com", /* convert_idna= */ false)); |
| 490 |
ASSERT_NOT_NULL(expected); |
| 491 |
|
| 492 |
/* NULL */ |
| 493 |
ASSERT_OK_ZERO(dns_question_cname_redirect(NULL, rr, &ret)); |
| 494 |
ASSERT_NULL(ret); |
| 495 |
|
| 496 |
/* an empty question */ |
| 497 |
ASSERT_NOT_NULL(question = dns_question_new(0)); |
| 498 |
ASSERT_OK_ZERO(dns_question_cname_redirect(question, rr, &ret)); |
| 499 |
ASSERT_NULL(ret); |
| 500 |
|
| 501 |
question = dns_question_unref(question); |
| 502 |
|
| 503 |
/* match cname */ |
| 504 |
ASSERT_OK(dns_question_new_address(&question, AF_INET, "www.example.com", /* convert_idna= */ false)); |
| 505 |
ASSERT_OK_POSITIVE(dns_question_cname_redirect(question, rr, &ret)); |
| 506 |
ASSERT_OK_POSITIVE(dns_question_is_equal(ret, expected)); |
| 507 |
|
| 508 |
question = dns_question_unref(question); |
| 509 |
ret = dns_question_unref(ret); |
| 510 |
|
| 511 |
/* same name */ |
| 512 |
ASSERT_OK(dns_question_new_address(&question, AF_INET, "example.com", /* convert_idna= */ false)); |
| 513 |
ASSERT_OK_ZERO(dns_question_cname_redirect(question, rr, &ret)); |
| 514 |
ASSERT_NULL(ret); |
| 515 |
|
| 516 |
question = dns_question_unref(question); |
| 517 |
|
| 518 |
/* no match (same domain) */ |
| 519 |
ASSERT_OK(dns_question_new_address(&question, AF_INET, "mail.example.com", /* convert_idna= */ false)); |
| 520 |
ASSERT_OK_POSITIVE(dns_question_cname_redirect(question, rr, &ret)); |
| 521 |
ASSERT_OK_POSITIVE(dns_question_is_equal(ret, expected)); |
| 522 |
|
| 523 |
question = dns_question_unref(question); |
| 524 |
ret = dns_question_unref(ret); |
| 525 |
|
| 526 |
/* no match (different domain) */ |
| 527 |
ASSERT_OK(dns_question_new_address(&question, AF_INET, "www.example.org", /* convert_idna= */ false)); |
| 528 |
ASSERT_OK_POSITIVE(dns_question_cname_redirect(question, rr, &ret)); |
| 529 |
ASSERT_OK_POSITIVE(dns_question_is_equal(ret, expected)); |
| 530 |
|
| 531 |
question = dns_question_unref(question); |
| 532 |
expected = dns_question_unref(expected); |
| 533 |
ret = dns_question_unref(ret); |
| 534 |
|
| 535 |
/* prepare dname record */ |
| 536 |
dns_resource_record_unref(rr); |
| 537 |
ASSERT_NOT_NULL(rr = dns_resource_record_new_full(DNS_CLASS_IN, DNS_TYPE_DNAME, "example.com")); |
| 538 |
rr->dname.name = strdup("v2.example.com"); |
| 539 |
|
| 540 |
ASSERT_OK(dns_question_new_address(&expected, AF_INET, "www.v2.example.com", /* convert_idna= */ false)); |
| 541 |
|
| 542 |
/* match dname */ |
| 543 |
ASSERT_OK(dns_question_new_address(&question, AF_INET, "www.example.com", /* convert_idna= */ false)); |
| 544 |
ASSERT_OK_POSITIVE(dns_question_cname_redirect(question, rr, &ret)); |
| 545 |
ASSERT_OK_POSITIVE(dns_question_is_equal(ret, expected)); |
| 546 |
|
| 547 |
question = dns_question_unref(question); |
| 548 |
expected = dns_question_unref(expected); |
| 549 |
ret = dns_question_unref(ret); |
| 550 |
|
| 551 |
/* multiple dname match */ |
| 552 |
ASSERT_NOT_NULL(question = dns_question_new(2)); |
| 553 |
|
| 554 |
ASSERT_NOT_NULL(key = dns_resource_key_new(DNS_CLASS_IN, DNS_TYPE_A, "www.example.com")); |
| 555 |
ASSERT_OK(dns_question_add(question, key, /* flags= */ 0)); |
| 556 |
dns_resource_key_unref(key); |
| 557 |
|
| 558 |
ASSERT_NOT_NULL(key = dns_resource_key_new(DNS_CLASS_IN, DNS_TYPE_A, "mail.example.com")); |
| 559 |
ASSERT_OK(dns_question_add(question, key, /* flags= */ 0)); |
| 560 |
dns_resource_key_unref(key); |
| 561 |
|
| 562 |
ASSERT_OK_POSITIVE(dns_question_cname_redirect(question, rr, &ret)); |
| 563 |
ASSERT_NOT_NULL(ret); |
| 564 |
ASSERT_EQ(dns_question_size(ret), 2u); |
| 565 |
|
| 566 |
ASSERT_NOT_NULL(key = dns_resource_key_new(DNS_CLASS_IN, DNS_TYPE_A, "www.v2.example.com")); |
| 567 |
ASSERT_OK_POSITIVE(dns_question_contains_key(ret, key)); |
| 568 |
dns_resource_key_unref(key); |
| 569 |
|
| 570 |
ASSERT_NOT_NULL(key = dns_resource_key_new(DNS_CLASS_IN, DNS_TYPE_A, "mail.v2.example.com")); |
| 571 |
ASSERT_OK_POSITIVE(dns_question_contains_key(ret, key)); |
| 572 |
dns_resource_key_unref(key); |
| 573 |
} |
| 574 |
|
| 575 |
/* ================================================================ |
| 576 |
* dns_question_dump() |
| 577 |
* ================================================================ */ |
| 578 |
|
| 579 |
TEST(dns_question_dump) { |
| 580 |
_cleanup_(dns_question_unrefp) DnsQuestion *question = NULL; |
| 581 |
_cleanup_(memstream_done) MemStream ms = {}; |
| 582 |
_cleanup_free_ char *buf = NULL; |
| 583 |
FILE *f; |
| 584 |
|
| 585 |
ASSERT_NOT_NULL(question = dns_question_new(3)); |
| 586 |
|
| 587 |
uint16_t type; |
| 588 |
FOREACH_ARGUMENT(type, DNS_TYPE_A, DNS_TYPE_AAAA, DNS_TYPE_TXT) { |
| 589 |
_cleanup_(dns_resource_key_unrefp) DnsResourceKey *key = NULL; |
| 590 |
|
| 591 |
ASSERT_NOT_NULL(key = dns_resource_key_new(DNS_CLASS_IN, type, "www.example.com")); |
| 592 |
ASSERT_OK(dns_question_add(question, key, /* flags= */ 0)); |
| 593 |
} |
| 594 |
|
| 595 |
ASSERT_EQ(dns_question_size(question), 3u); |
| 596 |
|
| 597 |
ASSERT_NOT_NULL(f = memstream_init(&ms)); |
| 598 |
dns_question_dump(question, f); |
| 599 |
ASSERT_OK(memstream_finalize(&ms, &buf, /* ret_size= */ NULL)); |
| 600 |
ASSERT_STREQ(buf, |
| 601 |
"\twww.example.com IN A\n" |
| 602 |
"\twww.example.com IN AAAA\n" |
| 603 |
"\twww.example.com IN TXT\n"); |
| 604 |
} |
| 605 |
|
| 606 |
/* ================================================================ |
| 607 |
* dns_question_first_name() |
| 608 |
* ================================================================ */ |
| 609 |
|
| 610 |
TEST(dns_question_first_name) { |
| 611 |
_cleanup_(dns_question_unrefp) DnsQuestion *question = NULL; |
| 612 |
DnsResourceKey *key; |
| 613 |
|
| 614 |
/* NULL */ |
| 615 |
ASSERT_NULL(dns_question_first_name(NULL)); |
| 616 |
|
| 617 |
/* an empty question */ |
| 618 |
ASSERT_NOT_NULL(question = dns_question_new(0)); |
| 619 |
ASSERT_NULL(dns_question_first_name(question)); |
| 620 |
question = dns_question_unref(question); |
| 621 |
|
| 622 |
/* multiple keys */ |
| 623 |
ASSERT_NOT_NULL(question = dns_question_new(2)); |
| 624 |
|
| 625 |
ASSERT_NOT_NULL(key = dns_resource_key_new(DNS_CLASS_IN, DNS_TYPE_A, "www.example.com")); |
| 626 |
ASSERT_OK(dns_question_add(question, key, /* flags= */ 0)); |
| 627 |
dns_resource_key_unref(key); |
| 628 |
|
| 629 |
ASSERT_NOT_NULL(key = dns_resource_key_new(DNS_CLASS_IN, DNS_TYPE_A, "mail.example.com")); |
| 630 |
ASSERT_OK(dns_question_add(question, key, /* flags= */ 0)); |
| 631 |
dns_resource_key_unref(key); |
| 632 |
|
| 633 |
ASSERT_STREQ(dns_question_first_name(question), "www.example.com"); |
| 634 |
} |
| 635 |
|
| 636 |
/* ================================================================ |
| 637 |
* dns_question_merge() |
| 638 |
* ================================================================ */ |
| 639 |
|
| 640 |
TEST(dns_question_merge_empty_first) { |
| 641 |
_cleanup_(dns_question_unrefp) DnsQuestion *a = NULL, *b = NULL, *ret = NULL; |
| 642 |
DnsResourceKey *key; |
| 643 |
|
| 644 |
ASSERT_NOT_NULL(a = dns_question_new(0)); |
| 645 |
ASSERT_NOT_NULL(b = dns_question_new(0)); |
| 646 |
|
| 647 |
/* trivial cases */ |
| 648 |
ASSERT_OK(dns_question_merge(NULL, NULL, &ret)); |
| 649 |
ASSERT_NULL(ret); |
| 650 |
ASSERT_OK(dns_question_merge(NULL, a, &ret)); |
| 651 |
ASSERT_NULL(ret); |
| 652 |
ASSERT_OK(dns_question_merge(a, NULL, &ret)); |
| 653 |
ASSERT_PTR_EQ(ret, a); |
| 654 |
ret = dns_question_unref(ret); |
| 655 |
ASSERT_OK(dns_question_merge(a, a, &ret)); |
| 656 |
ASSERT_PTR_EQ(ret, a); |
| 657 |
ret = dns_question_unref(ret); |
| 658 |
ASSERT_OK(dns_question_merge(a, b, &ret)); |
| 659 |
ASSERT_PTR_EQ(ret, a); |
| 660 |
ret = dns_question_unref(ret); |
| 661 |
|
| 662 |
a = dns_question_unref(a); |
| 663 |
|
| 664 |
/* single question */ |
| 665 |
ASSERT_OK(dns_question_new_address(&a, AF_INET, "www.example.com", /* convert_idna= */ false)); |
| 666 |
ASSERT_OK(dns_question_merge(NULL, a, &ret)); |
| 667 |
ASSERT_PTR_EQ(ret, a); |
| 668 |
ret = dns_question_unref(ret); |
| 669 |
ASSERT_OK(dns_question_merge(a, NULL, &ret)); |
| 670 |
ASSERT_PTR_EQ(ret, a); |
| 671 |
ret = dns_question_unref(ret); |
| 672 |
ASSERT_OK(dns_question_merge(a, a, &ret)); |
| 673 |
ASSERT_PTR_EQ(ret, a); |
| 674 |
ret = dns_question_unref(ret); |
| 675 |
ASSERT_OK(dns_question_merge(a, b, &ret)); |
| 676 |
ASSERT_PTR_EQ(ret, a); |
| 677 |
ret = dns_question_unref(ret); |
| 678 |
ASSERT_OK(dns_question_merge(b, a, &ret)); |
| 679 |
ASSERT_PTR_EQ(ret, a); |
| 680 |
ret = dns_question_unref(ret); |
| 681 |
|
| 682 |
b = dns_question_unref(b); |
| 683 |
|
| 684 |
/* multiple questions */ |
| 685 |
ASSERT_NOT_NULL(b = dns_question_new(2)); |
| 686 |
|
| 687 |
ASSERT_NOT_NULL(key = dns_resource_key_new(DNS_CLASS_IN, DNS_TYPE_AAAA, "www.example.com")); |
| 688 |
ASSERT_OK(dns_question_add(b, key, /* flags= */ 0)); |
| 689 |
dns_resource_key_unref(key); |
| 690 |
|
| 691 |
ASSERT_NOT_NULL(key = dns_resource_key_new(DNS_CLASS_IN, DNS_TYPE_TXT, "www.example.com")); |
| 692 |
ASSERT_OK(dns_question_add(b, key, /* flags= */ 0)); |
| 693 |
dns_resource_key_unref(key); |
| 694 |
|
| 695 |
ASSERT_OK(dns_question_merge(a, b, &ret)); |
| 696 |
ASSERT_EQ(dns_question_size(ret), 3u); |
| 697 |
|
| 698 |
uint16_t types[3]; |
| 699 |
size_t i = 0; |
| 700 |
DNS_QUESTION_FOREACH(key, ret) |
| 701 |
types[i++] = key->type; |
| 702 |
|
| 703 |
ASSERT_EQ(types[0], DNS_TYPE_A); |
| 704 |
ASSERT_EQ(types[1], DNS_TYPE_AAAA); |
| 705 |
ASSERT_EQ(types[2], DNS_TYPE_TXT); |
| 706 |
} |
| 707 |
|
| 708 |
DEFINE_TEST_MAIN(LOG_DEBUG); |