| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
|
| 3 |
#include "dns-answer.h" |
| 4 |
#include "dns-rr.h" |
| 5 |
#include "dns-type.h" |
| 6 |
#include "memstream-util.h" |
| 7 |
#include "strv.h" |
| 8 |
#include "tests.h" |
| 9 |
|
| 10 |
/* ================================================================ |
| 11 |
* dns_answer_add() |
| 12 |
* ================================================================ */ |
| 13 |
|
| 14 |
TEST(dns_answer_add_a) { |
| 15 |
_cleanup_(dns_answer_unrefp) DnsAnswer *answer = NULL; |
| 16 |
_cleanup_(dns_resource_record_unrefp) DnsResourceRecord *rr = NULL; |
| 17 |
|
| 18 |
ASSERT_NOT_NULL(answer = dns_answer_new(0)); |
| 19 |
|
| 20 |
ASSERT_NOT_NULL(rr = dns_resource_record_new_full(DNS_CLASS_IN, DNS_TYPE_A, "www.example.com")); |
| 21 |
rr->a.in_addr.s_addr = htobe32(0xc0a8017f); |
| 22 |
ASSERT_OK_POSITIVE(dns_answer_add(answer, rr, 1, DNS_ANSWER_CACHEABLE, /* rrsig= */ NULL)); |
| 23 |
|
| 24 |
ASSERT_TRUE(dns_answer_contains(answer, rr)); |
| 25 |
} |
| 26 |
|
| 27 |
/* ================================================================ |
| 28 |
* dns_answer_match_key() |
| 29 |
* ================================================================ */ |
| 30 |
|
| 31 |
TEST(dns_answer_match_key_single) { |
| 32 |
_cleanup_(dns_answer_unrefp) DnsAnswer *answer = NULL; |
| 33 |
_cleanup_(dns_resource_record_unrefp) DnsResourceRecord *rr = NULL; |
| 34 |
DnsResourceKey *key; |
| 35 |
DnsAnswerFlags flags; |
| 36 |
|
| 37 |
ASSERT_NOT_NULL(answer = dns_answer_new(0)); |
| 38 |
|
| 39 |
ASSERT_NOT_NULL(rr = dns_resource_record_new_full(DNS_CLASS_IN, DNS_TYPE_A, "www.example.com")); |
| 40 |
rr->a.in_addr.s_addr = htobe32(0xc0a8017f); |
| 41 |
ASSERT_OK_POSITIVE(dns_answer_add(answer, rr, 1, DNS_ANSWER_CACHEABLE, /* rrsig= */ NULL)); |
| 42 |
|
| 43 |
ASSERT_EQ(dns_answer_size(answer), 1u); |
| 44 |
|
| 45 |
ASSERT_OK_POSITIVE(dns_answer_match_key(answer, rr->key, /* ret_flags= */ NULL)); |
| 46 |
ASSERT_OK_POSITIVE(dns_answer_match_key(answer, rr->key, &flags)); |
| 47 |
ASSERT_EQ((int)flags, DNS_ANSWER_CACHEABLE); |
| 48 |
|
| 49 |
/* ANY class matches */ |
| 50 |
ASSERT_NOT_NULL(key = dns_resource_key_new(DNS_CLASS_ANY, DNS_TYPE_A, "www.example.com")); |
| 51 |
ASSERT_OK_POSITIVE(dns_answer_match_key(answer, key, /* ret_flags= */ NULL)); |
| 52 |
dns_resource_key_unref(key); |
| 53 |
|
| 54 |
/* ANY type matches */ |
| 55 |
ASSERT_NOT_NULL(key = dns_resource_key_new(DNS_CLASS_IN, DNS_TYPE_ANY, "www.example.com")); |
| 56 |
ASSERT_OK_POSITIVE(dns_answer_match_key(answer, key, /* ret_flags= */ NULL)); |
| 57 |
dns_resource_key_unref(key); |
| 58 |
|
| 59 |
/* non-matching type */ |
| 60 |
ASSERT_NOT_NULL(key = dns_resource_key_new(DNS_CLASS_IN, DNS_TYPE_CNAME, "www.example.com")); |
| 61 |
ASSERT_OK_ZERO(dns_answer_match_key(answer, key, /* ret_flags= */ NULL)); |
| 62 |
dns_resource_key_unref(key); |
| 63 |
|
| 64 |
/* name is case-insensitive */ |
| 65 |
ASSERT_NOT_NULL(key = dns_resource_key_new(DNS_CLASS_IN, DNS_TYPE_A, "WWW.EXAMPLE.COM")); |
| 66 |
ASSERT_OK_POSITIVE(dns_answer_match_key(answer, key, /* ret_flags= */ NULL)); |
| 67 |
dns_resource_key_unref(key); |
| 68 |
|
| 69 |
/* non-matching name */ |
| 70 |
ASSERT_NOT_NULL(key = dns_resource_key_new(DNS_CLASS_IN, DNS_TYPE_A, "example.com")); |
| 71 |
ASSERT_OK_ZERO(dns_answer_match_key(answer, key, /* ret_flags= */ NULL)); |
| 72 |
dns_resource_key_unref(key); |
| 73 |
|
| 74 |
/* name containing an error */ |
| 75 |
ASSERT_NOT_NULL(key = dns_resource_key_new(DNS_CLASS_IN, DNS_TYPE_A, "www.\\example.com")); |
| 76 |
ASSERT_ERROR(dns_answer_match_key(answer, key, /* ret_flags= */ NULL), EINVAL); |
| 77 |
dns_resource_key_unref(key); |
| 78 |
} |
| 79 |
|
| 80 |
TEST(dns_answer_match_key_multiple) { |
| 81 |
_cleanup_(dns_answer_unrefp) DnsAnswer *answer = NULL; |
| 82 |
DnsResourceRecord *rr; |
| 83 |
DnsResourceKey *key; |
| 84 |
DnsAnswerFlags flags; |
| 85 |
|
| 86 |
ASSERT_NOT_NULL(answer = dns_answer_new(0)); |
| 87 |
|
| 88 |
ASSERT_NOT_NULL(rr = dns_resource_record_new_full(DNS_CLASS_IN, DNS_TYPE_TXT, "www.example.com")); |
| 89 |
ASSERT_OK_POSITIVE(dns_answer_add(answer, rr, 1, DNS_ANSWER_SECTION_ANSWER | DNS_ANSWER_AUTHENTICATED, /* rrsig= */ NULL)); |
| 90 |
dns_resource_record_unref(rr); |
| 91 |
|
| 92 |
ASSERT_NOT_NULL(rr = dns_resource_record_new_full(DNS_CLASS_IN, DNS_TYPE_A, "www.example.com")); |
| 93 |
rr->a.in_addr.s_addr = htobe32(0xc0a8017f); |
| 94 |
ASSERT_OK_POSITIVE(dns_answer_add(answer, rr, 1, DNS_ANSWER_SECTION_ANSWER | DNS_ANSWER_CACHEABLE, /* rrsig= */ NULL)); |
| 95 |
dns_resource_record_unref(rr); |
| 96 |
|
| 97 |
ASSERT_EQ(dns_answer_size(answer), 2u); |
| 98 |
|
| 99 |
ASSERT_NOT_NULL(key = dns_resource_key_new(DNS_CLASS_IN, DNS_TYPE_A, "www.example.com")); |
| 100 |
ASSERT_OK_POSITIVE(dns_answer_match_key(answer, key, &flags)); |
| 101 |
ASSERT_EQ((int) flags, DNS_ANSWER_SECTION_ANSWER | DNS_ANSWER_CACHEABLE); |
| 102 |
dns_resource_key_unref(key); |
| 103 |
|
| 104 |
ASSERT_NOT_NULL(key = dns_resource_key_new(DNS_CLASS_IN, DNS_TYPE_ANY, "www.example.com")); |
| 105 |
ASSERT_OK_POSITIVE(dns_answer_match_key(answer, key, &flags)); |
| 106 |
ASSERT_EQ((int) flags, DNS_ANSWER_SECTION_ANSWER); |
| 107 |
dns_resource_key_unref(key); |
| 108 |
} |
| 109 |
|
| 110 |
/* ================================================================ |
| 111 |
* dns_answer_find_soa() |
| 112 |
* ================================================================ */ |
| 113 |
|
| 114 |
TEST(dns_answer_find_soa) { |
| 115 |
_cleanup_(dns_answer_unrefp) DnsAnswer *answer = NULL; |
| 116 |
DnsResourceRecord *rr; |
| 117 |
DnsResourceKey *key; |
| 118 |
DnsAnswerFlags flags; |
| 119 |
|
| 120 |
ASSERT_NOT_NULL(answer = dns_answer_new(0)); |
| 121 |
|
| 122 |
ASSERT_NOT_NULL(key = dns_resource_key_new(DNS_CLASS_IN, DNS_TYPE_A, "www.example.com")); |
| 123 |
ASSERT_OK_ZERO(dns_answer_find_soa(answer, key, &rr, &flags)); |
| 124 |
dns_resource_key_unref(key); |
| 125 |
|
| 126 |
ASSERT_OK_POSITIVE(dns_answer_add_soa(answer, "example.com", 3600, 1)); |
| 127 |
|
| 128 |
/* does not find SOA keys */ |
| 129 |
ASSERT_NOT_NULL(key = dns_resource_key_new(DNS_CLASS_IN, DNS_TYPE_SOA, "example.com")); |
| 130 |
ASSERT_OK_ZERO(dns_answer_find_soa(answer, key, &rr, &flags)); |
| 131 |
ASSERT_NULL(rr); |
| 132 |
ASSERT_EQ((int) flags, 0); |
| 133 |
dns_resource_key_unref(key); |
| 134 |
|
| 135 |
/* finds matching A key */ |
| 136 |
ASSERT_NOT_NULL(key = dns_resource_key_new(DNS_CLASS_IN, DNS_TYPE_A, "www.example.com")); |
| 137 |
ASSERT_OK_POSITIVE(dns_answer_find_soa(answer, key, &rr, &flags)); |
| 138 |
ASSERT_EQ((int) flags, DNS_ANSWER_AUTHENTICATED); |
| 139 |
dns_resource_key_unref(key); |
| 140 |
|
| 141 |
ASSERT_NOT_NULL(key = dns_resource_key_new(DNS_CLASS_IN, DNS_TYPE_SOA, "example.com")); |
| 142 |
ASSERT_OK_POSITIVE(dns_resource_key_equal(rr->key, key)); |
| 143 |
dns_resource_key_unref(key); |
| 144 |
|
| 145 |
/* finds matching A key suddomain */ |
| 146 |
ASSERT_NOT_NULL(key = dns_resource_key_new(DNS_CLASS_IN, DNS_TYPE_A, "foo.www.example.com")); |
| 147 |
ASSERT_OK_POSITIVE(dns_answer_find_soa(answer, key, &rr, &flags)); |
| 148 |
ASSERT_EQ((int) flags, DNS_ANSWER_AUTHENTICATED); |
| 149 |
dns_resource_key_unref(key); |
| 150 |
|
| 151 |
ASSERT_NOT_NULL(key = dns_resource_key_new(DNS_CLASS_IN, DNS_TYPE_SOA, "example.com")); |
| 152 |
ASSERT_OK_POSITIVE(dns_resource_key_equal(rr->key, key)); |
| 153 |
dns_resource_key_unref(key); |
| 154 |
|
| 155 |
/* does not match simple prefix */ |
| 156 |
ASSERT_NOT_NULL(key = dns_resource_key_new(DNS_CLASS_IN, DNS_TYPE_A, "fooexample.com")); |
| 157 |
ASSERT_OK_ZERO(dns_answer_find_soa(answer, key, &rr, &flags)); |
| 158 |
ASSERT_NULL(rr); |
| 159 |
ASSERT_EQ((int) flags, 0); |
| 160 |
dns_resource_key_unref(key); |
| 161 |
|
| 162 |
/* does not match parent domain */ |
| 163 |
ASSERT_NOT_NULL(key = dns_resource_key_new(DNS_CLASS_IN, DNS_TYPE_A, "com")); |
| 164 |
ASSERT_OK_ZERO(dns_answer_find_soa(answer, key, &rr, &flags)); |
| 165 |
ASSERT_NULL(rr); |
| 166 |
ASSERT_EQ((int) flags, 0); |
| 167 |
dns_resource_key_unref(key); |
| 168 |
|
| 169 |
/* returns an error for bad escapes */ |
| 170 |
ASSERT_NOT_NULL(key = dns_resource_key_new(DNS_CLASS_IN, DNS_TYPE_A, "www.\\example.com")); |
| 171 |
rr = POINTER_MAX; |
| 172 |
flags = 4321; |
| 173 |
ASSERT_ERROR(dns_answer_find_soa(answer, key, &rr, &flags), EINVAL); |
| 174 |
ASSERT_PTR_EQ(rr, POINTER_MAX); |
| 175 |
ASSERT_EQ((int) flags, 4321); |
| 176 |
dns_resource_key_unref(key); |
| 177 |
} |
| 178 |
|
| 179 |
TEST(dns_answer_find_soa_multi) { |
| 180 |
_cleanup_(dns_answer_unrefp) DnsAnswer *answer = NULL; |
| 181 |
DnsResourceKey *key; |
| 182 |
DnsResourceRecord *rr; |
| 183 |
DnsAnswerFlags flags; |
| 184 |
|
| 185 |
ASSERT_NOT_NULL(answer = dns_answer_new(0)); |
| 186 |
|
| 187 |
ASSERT_OK_POSITIVE(dns_answer_add_soa(answer, "example.com", 3600, 1)); |
| 188 |
ASSERT_OK_POSITIVE(dns_answer_add_soa(answer, "example.org", 3600, 1)); |
| 189 |
|
| 190 |
ASSERT_NOT_NULL(key = dns_resource_key_new(DNS_CLASS_IN, DNS_TYPE_A, "www.example.com")); |
| 191 |
ASSERT_OK_POSITIVE(dns_answer_find_soa(answer, key, &rr, &flags)); |
| 192 |
ASSERT_EQ((int) flags, DNS_ANSWER_AUTHENTICATED); |
| 193 |
dns_resource_key_unref(key); |
| 194 |
|
| 195 |
ASSERT_NOT_NULL(key = dns_resource_key_new(DNS_CLASS_IN, DNS_TYPE_SOA, "example.com")); |
| 196 |
ASSERT_OK_POSITIVE(dns_resource_key_equal(rr->key, key)); |
| 197 |
ASSERT_EQ((int) flags, DNS_ANSWER_AUTHENTICATED); |
| 198 |
dns_resource_key_unref(key); |
| 199 |
|
| 200 |
ASSERT_NOT_NULL(key = dns_resource_key_new(DNS_CLASS_IN, DNS_TYPE_A, "www.example.org")); |
| 201 |
ASSERT_OK_POSITIVE(dns_answer_find_soa(answer, key, &rr, &flags)); |
| 202 |
ASSERT_EQ((int) flags, DNS_ANSWER_AUTHENTICATED); |
| 203 |
dns_resource_key_unref(key); |
| 204 |
|
| 205 |
ASSERT_NOT_NULL(key = dns_resource_key_new(DNS_CLASS_IN, DNS_TYPE_SOA, "example.org")); |
| 206 |
ASSERT_OK_POSITIVE(dns_resource_key_equal(rr->key, key)); |
| 207 |
dns_resource_key_unref(key); |
| 208 |
} |
| 209 |
|
| 210 |
/* ================================================================ |
| 211 |
* dns_answer_merge() |
| 212 |
* ================================================================ */ |
| 213 |
|
| 214 |
TEST(dns_answer_merge) { |
| 215 |
_cleanup_(dns_answer_unrefp) DnsAnswer *a = NULL, *b = NULL, *ret = NULL; |
| 216 |
_cleanup_(dns_resource_record_unrefp) DnsResourceRecord *rr_a = NULL, *rr_b = NULL; |
| 217 |
|
| 218 |
ASSERT_NOT_NULL(a = dns_answer_new(0)); |
| 219 |
ASSERT_OK(dns_answer_merge(a, a, &ret)); |
| 220 |
ASSERT_PTR_EQ(ret, a); |
| 221 |
ret = dns_answer_unref(ret); |
| 222 |
|
| 223 |
ASSERT_NOT_NULL(b = dns_answer_new(0)); |
| 224 |
ASSERT_OK_POSITIVE(dns_answer_add_soa(b, "example.com", 3600, 1)); |
| 225 |
|
| 226 |
ASSERT_OK(dns_answer_merge(a, b, &ret)); |
| 227 |
ASSERT_PTR_EQ(ret, b); |
| 228 |
ret = dns_answer_unref(ret); |
| 229 |
|
| 230 |
ASSERT_OK(dns_answer_merge(b, a, &ret)); |
| 231 |
ASSERT_PTR_EQ(ret, b); |
| 232 |
ret = dns_answer_unref(ret); |
| 233 |
|
| 234 |
ASSERT_NOT_NULL(rr_a = dns_resource_record_new_full(DNS_CLASS_IN, DNS_TYPE_A, "a.example.com")); |
| 235 |
rr_a->a.in_addr.s_addr = htobe32(0xc0a8017f); |
| 236 |
ASSERT_OK_POSITIVE(dns_answer_add(a, rr_a, 1, DNS_ANSWER_CACHEABLE, /* rrsig= */ NULL)); |
| 237 |
|
| 238 |
ASSERT_NOT_NULL(rr_b = dns_resource_record_new_full(DNS_CLASS_IN, DNS_TYPE_A, "b.example.com")); |
| 239 |
rr_b->a.in_addr.s_addr = htobe32(0xc0a80180); |
| 240 |
ASSERT_OK_POSITIVE(dns_answer_add(b, rr_b, 1, DNS_ANSWER_CACHEABLE, /* rrsig= */ NULL)); |
| 241 |
|
| 242 |
ASSERT_OK(dns_answer_merge(a, b, &ret)); |
| 243 |
ASSERT_TRUE(ret != a); |
| 244 |
ASSERT_TRUE(ret != b); |
| 245 |
|
| 246 |
ASSERT_OK_POSITIVE(dns_answer_match_key(a, rr_a->key, /* ret_flags= */ NULL)); |
| 247 |
ASSERT_OK_ZERO(dns_answer_match_key(a, rr_b->key, /* ret_flags= */ NULL)); |
| 248 |
|
| 249 |
ASSERT_OK_ZERO(dns_answer_match_key(b, rr_a->key, /* ret_flags= */ NULL)); |
| 250 |
ASSERT_OK_POSITIVE(dns_answer_match_key(b, rr_b->key, /* ret_flags= */ NULL)); |
| 251 |
|
| 252 |
ASSERT_OK_POSITIVE(dns_answer_match_key(ret, rr_a->key, /* ret_flags= */ NULL)); |
| 253 |
ASSERT_OK_POSITIVE(dns_answer_match_key(ret, rr_b->key, /* ret_flags= */ NULL)); |
| 254 |
} |
| 255 |
|
| 256 |
/* ================================================================ |
| 257 |
* dns_answer_extend() |
| 258 |
* ================================================================ */ |
| 259 |
|
| 260 |
TEST(dns_answer_extend) { |
| 261 |
_cleanup_(dns_answer_unrefp) DnsAnswer *a = NULL, *b = NULL; |
| 262 |
_cleanup_(dns_resource_record_unrefp) DnsResourceRecord *rr_a = NULL, *rr_b = NULL; |
| 263 |
|
| 264 |
ASSERT_NOT_NULL(a = dns_answer_new(0)); |
| 265 |
ASSERT_NOT_NULL(b = dns_answer_new(0)); |
| 266 |
|
| 267 |
ASSERT_NOT_NULL(rr_a = dns_resource_record_new_full(DNS_CLASS_IN, DNS_TYPE_A, "a.example.com")); |
| 268 |
rr_a->a.in_addr.s_addr = htobe32(0xc0a8017f); |
| 269 |
ASSERT_OK_POSITIVE(dns_answer_add(a, rr_a, 1, DNS_ANSWER_CACHEABLE, /* rrsig= */ NULL)); |
| 270 |
|
| 271 |
ASSERT_NOT_NULL(rr_b = dns_resource_record_new_full(DNS_CLASS_IN, DNS_TYPE_A, "b.example.com")); |
| 272 |
rr_b->a.in_addr.s_addr = htobe32(0xc0a80180); |
| 273 |
ASSERT_OK_POSITIVE(dns_answer_add(b, rr_b, 1, DNS_ANSWER_CACHEABLE, /* rrsig= */ NULL)); |
| 274 |
|
| 275 |
ASSERT_OK(dns_answer_extend(&a, b)); |
| 276 |
ASSERT_TRUE(a != b); |
| 277 |
|
| 278 |
ASSERT_OK_POSITIVE(dns_answer_match_key(a, rr_a->key, /* ret_flags= */ NULL)); |
| 279 |
ASSERT_OK_POSITIVE(dns_answer_match_key(a, rr_b->key, /* ret_flags= */ NULL)); |
| 280 |
} |
| 281 |
|
| 282 |
/* ================================================================ |
| 283 |
* dns_answer_remove_by_*() |
| 284 |
* ================================================================ */ |
| 285 |
|
| 286 |
static DnsAnswer* prepare_answer(void) { |
| 287 |
_cleanup_(dns_answer_unrefp) DnsAnswer *answer = NULL; |
| 288 |
|
| 289 |
ASSERT_NOT_NULL(answer = dns_answer_new(0)); |
| 290 |
|
| 291 |
char **hosts = STRV_MAKE("a.example.com", "b.example.com", "c.example.com"); |
| 292 |
STRV_FOREACH(h, hosts) { |
| 293 |
_cleanup_(dns_resource_record_unrefp) DnsResourceRecord *rr = NULL; |
| 294 |
|
| 295 |
ASSERT_NOT_NULL(rr = dns_resource_record_new_full(DNS_CLASS_IN, DNS_TYPE_A, *h)); |
| 296 |
rr->a.in_addr.s_addr = htobe32(0xc0a8017f); |
| 297 |
ASSERT_OK_POSITIVE(dns_answer_add(answer, rr, 1, DNS_ANSWER_CACHEABLE, /* rrsig= */ NULL)); |
| 298 |
} |
| 299 |
|
| 300 |
return TAKE_PTR(answer); |
| 301 |
} |
| 302 |
|
| 303 |
TEST(dns_answer_remove_by_key) { |
| 304 |
_cleanup_(dns_answer_unrefp) DnsAnswer *answer = prepare_answer(); |
| 305 |
DnsResourceKey *key; |
| 306 |
|
| 307 |
/* ignore non-matching class */ |
| 308 |
ASSERT_NOT_NULL(key = dns_resource_key_new(DNS_CLASS_ANY, DNS_TYPE_A, "b.example.com")); |
| 309 |
ASSERT_OK_ZERO(dns_answer_remove_by_key(&answer, key)); |
| 310 |
ASSERT_EQ(dns_answer_size(answer), 3u); |
| 311 |
dns_resource_key_unref(key); |
| 312 |
|
| 313 |
/* ignore non-matching type */ |
| 314 |
ASSERT_NOT_NULL(key = dns_resource_key_new(DNS_CLASS_IN, DNS_TYPE_AAAA, "b.example.com")); |
| 315 |
ASSERT_OK_ZERO(dns_answer_remove_by_key(&answer, key)); |
| 316 |
ASSERT_EQ(dns_answer_size(answer), 3u); |
| 317 |
dns_resource_key_unref(key); |
| 318 |
|
| 319 |
/* ignore non-matching name */ |
| 320 |
ASSERT_NOT_NULL(key = dns_resource_key_new(DNS_CLASS_IN, DNS_TYPE_A, "z.example.com")); |
| 321 |
ASSERT_OK_ZERO(dns_answer_remove_by_key(&answer, key)); |
| 322 |
ASSERT_EQ(dns_answer_size(answer), 3u); |
| 323 |
dns_resource_key_unref(key); |
| 324 |
|
| 325 |
/* remove matching key */ |
| 326 |
char **hosts = STRV_MAKE("a.example.com", "b.example.com", "c.example.com"); |
| 327 |
unsigned n = strv_length(hosts); |
| 328 |
STRV_FOREACH(h, hosts) { |
| 329 |
n--; |
| 330 |
|
| 331 |
ASSERT_NOT_NULL(key = dns_resource_key_new(DNS_CLASS_IN, DNS_TYPE_A, *h)); |
| 332 |
ASSERT_OK_POSITIVE(dns_answer_match_key(answer, key, /* ret_flags= */ NULL)); |
| 333 |
ASSERT_OK_POSITIVE(dns_answer_remove_by_key(&answer, key)); |
| 334 |
ASSERT_EQ(dns_answer_size(answer), n); |
| 335 |
|
| 336 |
ASSERT_OK_ZERO(dns_answer_match_key(answer, key, /* ret_flags= */ NULL)); |
| 337 |
ASSERT_OK_ZERO(dns_answer_remove_by_key(&answer, key)); |
| 338 |
ASSERT_EQ(dns_answer_size(answer), n); |
| 339 |
dns_resource_key_unref(key); |
| 340 |
} |
| 341 |
|
| 342 |
ASSERT_NULL(answer); |
| 343 |
} |
| 344 |
|
| 345 |
TEST(dns_answer_remove_by_rr) { |
| 346 |
_cleanup_(dns_answer_unrefp) DnsAnswer *answer = prepare_answer(); |
| 347 |
|
| 348 |
char **hosts = STRV_MAKE("a.example.com", "b.example.com", "c.example.com"); |
| 349 |
unsigned n = strv_length(hosts); |
| 350 |
STRV_FOREACH(h, hosts) { |
| 351 |
_cleanup_(dns_resource_key_unrefp) DnsResourceKey *key = NULL; |
| 352 |
_cleanup_(dns_resource_record_unrefp) DnsResourceRecord *rr = NULL; |
| 353 |
|
| 354 |
ASSERT_NOT_NULL(key = dns_resource_key_new(DNS_CLASS_IN, DNS_TYPE_A, *h)); |
| 355 |
ASSERT_OK_POSITIVE(dns_answer_match_key(answer, key, /* ret_flags= */ NULL)); |
| 356 |
|
| 357 |
/* remove nothing if the payload does not match */ |
| 358 |
ASSERT_NOT_NULL(rr = dns_resource_record_new_full(DNS_CLASS_IN, DNS_TYPE_A, *h)); |
| 359 |
rr->a.in_addr.s_addr = htobe32(0x01020304); |
| 360 |
ASSERT_OK_ZERO(dns_answer_remove_by_rr(&answer, rr)); |
| 361 |
ASSERT_EQ(dns_answer_size(answer), n); |
| 362 |
|
| 363 |
/* remove matching payload */ |
| 364 |
rr->a.in_addr.s_addr = htobe32(0xc0a8017f); |
| 365 |
ASSERT_OK_POSITIVE(dns_answer_remove_by_rr(&answer, rr)); |
| 366 |
ASSERT_EQ(dns_answer_size(answer), --n); |
| 367 |
|
| 368 |
ASSERT_OK_ZERO(dns_answer_match_key(answer, key, /* ret_flags= */ NULL)); |
| 369 |
} |
| 370 |
|
| 371 |
ASSERT_NULL(answer); |
| 372 |
} |
| 373 |
|
| 374 |
TEST(dns_answer_remove_by_answer_keys) { |
| 375 |
_cleanup_(dns_answer_unrefp) DnsAnswer *a = prepare_answer(), *b = prepare_answer(); |
| 376 |
DnsResourceKey *key; |
| 377 |
|
| 378 |
ASSERT_OK(dns_answer_remove_by_answer_keys(&a, b)); |
| 379 |
ASSERT_NULL(a); |
| 380 |
|
| 381 |
a = prepare_answer(); |
| 382 |
|
| 383 |
ASSERT_NOT_NULL(key = dns_resource_key_new(DNS_CLASS_IN, DNS_TYPE_A, "b.example.com")); |
| 384 |
ASSERT_OK_POSITIVE(dns_answer_remove_by_key(&b, key)); |
| 385 |
dns_resource_key_unref(key); |
| 386 |
|
| 387 |
ASSERT_EQ(dns_answer_size(a), 3u); |
| 388 |
ASSERT_EQ(dns_answer_size(b), 2u); |
| 389 |
|
| 390 |
ASSERT_OK(dns_answer_remove_by_answer_keys(&a, b)); |
| 391 |
|
| 392 |
ASSERT_EQ(dns_answer_size(a), 1u); |
| 393 |
ASSERT_EQ(dns_answer_size(b), 2u); |
| 394 |
|
| 395 |
ASSERT_NOT_NULL(key = dns_resource_key_new(DNS_CLASS_IN, DNS_TYPE_A, "a.example.com")); |
| 396 |
ASSERT_OK_ZERO(dns_answer_match_key(a, key, /* ret_flags= */ NULL)); |
| 397 |
dns_resource_key_unref(key); |
| 398 |
|
| 399 |
ASSERT_NOT_NULL(key = dns_resource_key_new(DNS_CLASS_IN, DNS_TYPE_A, "b.example.com")); |
| 400 |
ASSERT_OK_POSITIVE(dns_answer_match_key(a, key, /* ret_flags= */ NULL)); |
| 401 |
dns_resource_key_unref(key); |
| 402 |
|
| 403 |
ASSERT_NOT_NULL(key = dns_resource_key_new(DNS_CLASS_IN, DNS_TYPE_A, "c.example.com")); |
| 404 |
ASSERT_OK_ZERO(dns_answer_match_key(a, key, /* ret_flags= */ NULL)); |
| 405 |
dns_resource_key_unref(key); |
| 406 |
} |
| 407 |
|
| 408 |
/* ================================================================ |
| 409 |
* dns_answer_copy_by_key() |
| 410 |
* ================================================================ */ |
| 411 |
|
| 412 |
TEST(dns_answer_copy_by_key) { |
| 413 |
_cleanup_(dns_answer_unrefp) DnsAnswer *source = NULL, *target = NULL; |
| 414 |
DnsResourceRecord *rr; |
| 415 |
DnsResourceKey *key; |
| 416 |
DnsAnswerFlags flags; |
| 417 |
|
| 418 |
ASSERT_NOT_NULL(source = dns_answer_new(0)); |
| 419 |
ASSERT_NOT_NULL(target = dns_answer_new(0)); |
| 420 |
|
| 421 |
ASSERT_NOT_NULL(rr = dns_resource_record_new_full(DNS_CLASS_IN, DNS_TYPE_A, "a.example.com")); |
| 422 |
rr->a.in_addr.s_addr = htobe32(0xc0a8017f); |
| 423 |
ASSERT_OK_POSITIVE(dns_answer_add(source, rr, 1, DNS_ANSWER_CACHEABLE, /* rrsig= */ NULL)); |
| 424 |
rr = dns_resource_record_unref(rr); |
| 425 |
|
| 426 |
/* non-matching class */ |
| 427 |
ASSERT_NOT_NULL(key = dns_resource_key_new(DNS_CLASS_ANY, DNS_TYPE_A, "a.example.com")); |
| 428 |
ASSERT_OK(dns_answer_copy_by_key(&target, source, key, /* or_flags= */ 0, /* rrsig= */ NULL)); |
| 429 |
ASSERT_EQ(dns_answer_size(target), 0u); |
| 430 |
dns_resource_key_unref(key); |
| 431 |
|
| 432 |
/* non-matching type */ |
| 433 |
key = dns_resource_key_new(DNS_CLASS_IN, DNS_TYPE_AAAA, "a.example.com"); |
| 434 |
ASSERT_NOT_NULL(key); |
| 435 |
ASSERT_OK(dns_answer_copy_by_key(&target, source, key, /* or_flags= */ 0, /* rrsig= */ NULL)); |
| 436 |
ASSERT_EQ(dns_answer_size(target), 0u); |
| 437 |
dns_resource_key_unref(key); |
| 438 |
|
| 439 |
/* non-matching name */ |
| 440 |
key = dns_resource_key_new(DNS_CLASS_IN, DNS_TYPE_A, "b.example.com"); |
| 441 |
ASSERT_NOT_NULL(key); |
| 442 |
ASSERT_OK(dns_answer_copy_by_key(&target, source, key, /* or_flags= */ 0, /* rrsig= */ NULL)); |
| 443 |
ASSERT_EQ(dns_answer_size(target), 0u); |
| 444 |
dns_resource_key_unref(key); |
| 445 |
|
| 446 |
/* matching key */ |
| 447 |
ASSERT_NOT_NULL(key = dns_resource_key_new(DNS_CLASS_IN, DNS_TYPE_A, "a.example.com")); |
| 448 |
ASSERT_OK(dns_answer_copy_by_key(&target, source, key, /* or_flags= */ 0, /* rrsig= */ NULL)); |
| 449 |
ASSERT_EQ(dns_answer_size(target), 1u); |
| 450 |
ASSERT_OK_POSITIVE(dns_answer_match_key(target, key, /* ret_flags= */ NULL)); |
| 451 |
|
| 452 |
/* add one more record with the same key */ |
| 453 |
ASSERT_NOT_NULL(rr = dns_resource_record_new_full(DNS_CLASS_IN, DNS_TYPE_A, "a.example.com")); |
| 454 |
rr->a.in_addr.s_addr = htobe32(0x7f000001); |
| 455 |
ASSERT_OK_POSITIVE(dns_answer_add(source, rr, 1, DNS_ANSWER_CACHEABLE, /* rrsig= */ NULL)); |
| 456 |
dns_resource_record_unref(rr); |
| 457 |
|
| 458 |
/* check if the two records are copied */ |
| 459 |
ASSERT_OK(dns_answer_copy_by_key(&target, source, key, /* or_flags= */ 0, /* rrsig= */ NULL)); |
| 460 |
ASSERT_EQ(dns_answer_size(target), 2u); |
| 461 |
ASSERT_OK_POSITIVE(dns_answer_match_key(target, key, /* ret_flags= */ NULL)); |
| 462 |
|
| 463 |
/* try again with an empty target */ |
| 464 |
target = dns_answer_unref(target); |
| 465 |
ASSERT_OK(dns_answer_copy_by_key(&target, source, key, /* or_flags= */ 0, /* rrsig= */ NULL)); |
| 466 |
ASSERT_EQ(dns_answer_size(target), 2u); |
| 467 |
ASSERT_OK_POSITIVE(dns_answer_match_key(target, key, /* ret_flags= */ NULL)); |
| 468 |
dns_resource_key_unref(key); |
| 469 |
|
| 470 |
/* add one more record */ |
| 471 |
ASSERT_NOT_NULL(rr = dns_resource_record_new_full(DNS_CLASS_IN, DNS_TYPE_A, "b.example.com")); |
| 472 |
rr->a.in_addr.s_addr = htobe32(0x7f000001); |
| 473 |
ASSERT_OK_POSITIVE(dns_answer_add(source, rr, 1, DNS_ANSWER_CACHEABLE, /* rrsig= */ NULL)); |
| 474 |
dns_resource_record_unref(rr); |
| 475 |
|
| 476 |
/* copy with flags */ |
| 477 |
ASSERT_NOT_NULL(key = dns_resource_key_new(DNS_CLASS_IN, DNS_TYPE_A, "b.example.com")); |
| 478 |
ASSERT_OK(dns_answer_copy_by_key(&target, source, key, DNS_ANSWER_SECTION_ANSWER, /* rrsig= */ NULL)); |
| 479 |
ASSERT_EQ(dns_answer_size(target), 3u); |
| 480 |
ASSERT_TRUE(dns_answer_match_key(target, key, &flags)); |
| 481 |
ASSERT_EQ((int) flags, DNS_ANSWER_CACHEABLE | DNS_ANSWER_SECTION_ANSWER); |
| 482 |
dns_resource_key_unref(key); |
| 483 |
} |
| 484 |
|
| 485 |
/* ================================================================ |
| 486 |
* dns_answer_move_by_key() |
| 487 |
* ================================================================ */ |
| 488 |
|
| 489 |
TEST(dns_answer_move_by_key) { |
| 490 |
_cleanup_(dns_answer_unrefp) DnsAnswer *source = NULL, *target = NULL; |
| 491 |
DnsResourceRecord *rr; |
| 492 |
DnsResourceKey *key; |
| 493 |
|
| 494 |
ASSERT_NOT_NULL(source = dns_answer_new(0)); |
| 495 |
ASSERT_NOT_NULL(target = dns_answer_new(0)); |
| 496 |
|
| 497 |
ASSERT_NOT_NULL(rr = dns_resource_record_new_full(DNS_CLASS_IN, DNS_TYPE_A, "a.example.com")); |
| 498 |
rr->a.in_addr.s_addr = htobe32(0xc0a8017f); |
| 499 |
ASSERT_OK_POSITIVE(dns_answer_add(source, rr, 1, DNS_ANSWER_CACHEABLE, /* rrsig= */ NULL)); |
| 500 |
dns_resource_record_unref(rr); |
| 501 |
|
| 502 |
/* non-matching class */ |
| 503 |
ASSERT_NOT_NULL(key = dns_resource_key_new(DNS_CLASS_ANY, DNS_TYPE_A, "a.example.com")); |
| 504 |
ASSERT_OK_ZERO(dns_answer_move_by_key(&target, &source, key, /* or_flags= */ 0, /* rrsig= */ NULL)); |
| 505 |
ASSERT_EQ(dns_answer_size(source), 1u); |
| 506 |
ASSERT_EQ(dns_answer_size(target), 0u); |
| 507 |
dns_resource_key_unref(key); |
| 508 |
|
| 509 |
/* non-matching type */ |
| 510 |
ASSERT_NOT_NULL(key = dns_resource_key_new(DNS_CLASS_IN, DNS_TYPE_AAAA, "a.example.com")); |
| 511 |
ASSERT_OK_ZERO(dns_answer_move_by_key(&target, &source, key, /* or_flags= */ 0, /* rrsig= */ NULL)); |
| 512 |
ASSERT_EQ(dns_answer_size(source), 1u); |
| 513 |
ASSERT_EQ(dns_answer_size(target), 0u); |
| 514 |
dns_resource_key_unref(key); |
| 515 |
|
| 516 |
/* non-matching name */ |
| 517 |
ASSERT_NOT_NULL(key = dns_resource_key_new(DNS_CLASS_IN, DNS_TYPE_A, "b.example.com")); |
| 518 |
ASSERT_OK_ZERO(dns_answer_move_by_key(&target, &source, key, /* or_flags= */ 0, /* rrsig= */ NULL)); |
| 519 |
ASSERT_EQ(dns_answer_size(source), 1u); |
| 520 |
ASSERT_EQ(dns_answer_size(target), 0u); |
| 521 |
dns_resource_key_unref(key); |
| 522 |
|
| 523 |
/* matching key */ |
| 524 |
ASSERT_NOT_NULL(key = dns_resource_key_new(DNS_CLASS_IN, DNS_TYPE_A, "a.example.com")); |
| 525 |
ASSERT_OK_POSITIVE(dns_answer_move_by_key(&target, &source, key, /* or_flags= */ 0, /* rrsig= */ NULL)); |
| 526 |
ASSERT_NULL(source); |
| 527 |
ASSERT_EQ(dns_answer_size(target), 1u); |
| 528 |
ASSERT_OK_POSITIVE(dns_answer_match_key(target, key, /* ret_flags= */ NULL)); |
| 529 |
|
| 530 |
/* move the record back to the source */ |
| 531 |
source = TAKE_PTR(target); |
| 532 |
|
| 533 |
/* add one more record */ |
| 534 |
ASSERT_NOT_NULL(rr = dns_resource_record_new_full(DNS_CLASS_IN, DNS_TYPE_A, "b.example.com")); |
| 535 |
rr->a.in_addr.s_addr = htobe32(0x7f000001); |
| 536 |
ASSERT_OK_POSITIVE(dns_answer_add(source, rr, 1, DNS_ANSWER_CACHEABLE, /* rrsig= */ NULL)); |
| 537 |
dns_resource_record_unref(rr); |
| 538 |
|
| 539 |
/* move only a.example.com */ |
| 540 |
ASSERT_OK_POSITIVE(dns_answer_move_by_key(&target, &source, key, /* or_flags= */ 0, /* rrsig= */ NULL)); |
| 541 |
ASSERT_EQ(dns_answer_size(source), 1u); |
| 542 |
ASSERT_EQ(dns_answer_size(target), 1u); |
| 543 |
ASSERT_OK_ZERO(dns_answer_match_key(source, key, /* ret_flags= */ NULL)); |
| 544 |
ASSERT_OK_POSITIVE(dns_answer_match_key(target, key, /* ret_flags= */ NULL)); |
| 545 |
|
| 546 |
/* move the record back to the source */ |
| 547 |
ASSERT_OK_POSITIVE(dns_answer_move_by_key(&source, &target, key, /* or_flags= */ 0, /* rrsig= */ NULL)); |
| 548 |
ASSERT_EQ(dns_answer_size(source), 2u); |
| 549 |
ASSERT_EQ(dns_answer_size(target), 0u); |
| 550 |
|
| 551 |
/* add one more record */ |
| 552 |
ASSERT_NOT_NULL(rr = dns_resource_record_new_full(DNS_CLASS_IN, DNS_TYPE_A, "a.example.com")); |
| 553 |
rr->a.in_addr.s_addr = htobe32(0xc0a80180); |
| 554 |
ASSERT_OK_POSITIVE(dns_answer_add(source, rr, 1, DNS_ANSWER_CACHEABLE, /* rrsig= */ NULL)); |
| 555 |
dns_resource_record_unref(rr); |
| 556 |
|
| 557 |
/* move two records for a.example.com */ |
| 558 |
ASSERT_OK_POSITIVE(dns_answer_move_by_key(&target, &source, key, /* or_flags= */ 0, /* rrsig= */ NULL)); |
| 559 |
ASSERT_EQ(dns_answer_size(source), 1u); |
| 560 |
ASSERT_EQ(dns_answer_size(target), 2u); |
| 561 |
ASSERT_OK_ZERO(dns_answer_match_key(source, key, /* ret_flags= */ NULL)); |
| 562 |
ASSERT_OK_POSITIVE(dns_answer_match_key(target, key, /* ret_flags= */ NULL)); |
| 563 |
dns_resource_key_unref(key); |
| 564 |
} |
| 565 |
|
| 566 |
/* ================================================================ |
| 567 |
* dns_answer_has_dname_for_cname() |
| 568 |
* ================================================================ */ |
| 569 |
|
| 570 |
TEST(dns_answer_has_dname_for_cname) { |
| 571 |
_cleanup_(dns_answer_unrefp) DnsAnswer *answer = NULL; |
| 572 |
_cleanup_(dns_resource_record_unrefp) DnsResourceRecord *cname = NULL, *dname = NULL, *rr = NULL; |
| 573 |
|
| 574 |
ASSERT_NOT_NULL(answer = dns_answer_new(0)); |
| 575 |
|
| 576 |
/* no dname */ |
| 577 |
ASSERT_NOT_NULL(cname = dns_resource_record_new_full(DNS_CLASS_IN, DNS_TYPE_CNAME, "www.example.com")); |
| 578 |
ASSERT_NOT_NULL(cname->cname.name = strdup("www.v2.example.com")); |
| 579 |
ASSERT_OK_ZERO(dns_answer_has_dname_for_cname(answer, cname)); |
| 580 |
|
| 581 |
/* has matching dname */ |
| 582 |
ASSERT_NOT_NULL(dname = dns_resource_record_new_full(DNS_CLASS_IN, DNS_TYPE_DNAME, "example.com")); |
| 583 |
ASSERT_NOT_NULL(dname->dname.name = strdup("v2.example.com")); |
| 584 |
ASSERT_OK_POSITIVE(dns_answer_add(answer, dname, 1, DNS_ANSWER_CACHEABLE, /* rrsig= */ NULL)); |
| 585 |
|
| 586 |
/* no matching old suffix */ |
| 587 |
ASSERT_OK(free_and_strdup(&dname->dname.name, "www.v2.examples.com")); |
| 588 |
ASSERT_OK_ZERO(dns_answer_has_dname_for_cname(answer, cname)); |
| 589 |
ASSERT_OK(free_and_strdup(&dname->dname.name, "www.v2.example.com")); |
| 590 |
|
| 591 |
/* no matching new suffix */ |
| 592 |
ASSERT_OK(free_and_strdup(&cname->cname.name, "www.v3.example.com")); |
| 593 |
ASSERT_OK_ZERO(dns_answer_has_dname_for_cname(answer, cname)); |
| 594 |
|
| 595 |
/* Not a cname */ |
| 596 |
ASSERT_NOT_NULL(rr = dns_resource_record_new_full(DNS_CLASS_IN, DNS_TYPE_A, "www.example.com")); |
| 597 |
rr->a.in_addr.s_addr = htobe32(0xc0a8017f); |
| 598 |
ASSERT_OK_ZERO(dns_answer_has_dname_for_cname(answer, rr)); |
| 599 |
} |
| 600 |
|
| 601 |
/* ================================================================ |
| 602 |
* dns_answer_dump() |
| 603 |
* ================================================================ */ |
| 604 |
|
| 605 |
TEST(dns_answer_dump) { |
| 606 |
_cleanup_(dns_answer_unrefp) DnsAnswer *answer = NULL; |
| 607 |
DnsResourceRecord *rr; |
| 608 |
_cleanup_(memstream_done) MemStream ms = {}; |
| 609 |
_cleanup_free_ char *buf = NULL; |
| 610 |
FILE *f; |
| 611 |
|
| 612 |
ASSERT_NOT_NULL(answer = dns_answer_new(0)); |
| 613 |
|
| 614 |
ASSERT_NOT_NULL(rr = dns_resource_record_new_full(DNS_CLASS_IN, DNS_TYPE_A, "a.example.com")); |
| 615 |
rr->ttl = 1200; |
| 616 |
rr->a.in_addr.s_addr = htobe32(0xc0a8017f); |
| 617 |
ASSERT_OK_POSITIVE(dns_answer_add(answer, rr, 1, DNS_ANSWER_CACHEABLE | DNS_ANSWER_SECTION_ADDITIONAL, /* rrsig= */ NULL)); |
| 618 |
dns_resource_record_unref(rr); |
| 619 |
|
| 620 |
ASSERT_NOT_NULL(rr = dns_resource_record_new_full(DNS_CLASS_IN, DNS_TYPE_A, "b.example.com")); |
| 621 |
rr->ttl = 2400; |
| 622 |
rr->a.in_addr.s_addr = htobe32(0xc0a80180); |
| 623 |
ASSERT_OK_POSITIVE(dns_answer_add(answer, rr, 2, 0, /* rrsig= */ NULL)); |
| 624 |
dns_resource_record_unref(rr); |
| 625 |
|
| 626 |
ASSERT_NOT_NULL(rr = dns_resource_record_new_full(DNS_CLASS_IN, DNS_TYPE_A, "c.example.com")); |
| 627 |
rr->ttl = 3600; |
| 628 |
rr->a.in_addr.s_addr = htobe32(0xc0a80181); |
| 629 |
ASSERT_OK_POSITIVE(dns_answer_add(answer, rr, 3, |
| 630 |
DNS_ANSWER_AUTHENTICATED | DNS_ANSWER_SHARED_OWNER | DNS_ANSWER_SECTION_AUTHORITY | DNS_ANSWER_CACHE_FLUSH, |
| 631 |
/* rrsig= */ NULL)); |
| 632 |
dns_resource_record_unref(rr); |
| 633 |
|
| 634 |
ASSERT_NOT_NULL(rr = dns_resource_record_new_full(DNS_CLASS_IN, DNS_TYPE_CNAME, "d.example.com")); |
| 635 |
rr->ttl = 4800; |
| 636 |
rr->cname.name = strdup("www.example.com"); |
| 637 |
ASSERT_OK_POSITIVE(dns_answer_add(answer, rr, 4, DNS_ANSWER_GOODBYE | DNS_ANSWER_SECTION_ANSWER, /* rrsig= */ NULL)); |
| 638 |
dns_resource_record_unref(rr); |
| 639 |
|
| 640 |
ASSERT_EQ(dns_answer_size(answer), 4u); |
| 641 |
|
| 642 |
ASSERT_NOT_NULL(f = memstream_init(&ms)); |
| 643 |
dns_answer_dump(answer, f); |
| 644 |
ASSERT_OK(memstream_finalize(&ms, &buf, /* ret_size= */ NULL)); |
| 645 |
ASSERT_STREQ(buf, |
| 646 |
"\ta.example.com IN A 192.168.1.127\t; ttl=1200 ifindex=1 cacheable section-additional\n" |
| 647 |
"\tb.example.com IN A 192.168.1.128\t; ttl=2400 ifindex=2\n" |
| 648 |
"\tc.example.com IN A 192.168.1.129\t; ttl=3600 ifindex=3 authenticated shared-owner cache-flush section-authority\n" |
| 649 |
"\td.example.com IN CNAME www.example.com\t; ttl=4800 ifindex=4 goodbye section-answer\n"); |
| 650 |
} |
| 651 |
|
| 652 |
/* ================================================================ |
| 653 |
* dns_answer_order_by_scope() |
| 654 |
* ================================================================ */ |
| 655 |
|
| 656 |
/* link-local addresses are a9fe0100 (169.254.1.0) to a9fefeff (169.254.254.255) */ |
| 657 |
|
| 658 |
TEST(dns_answer_order_by_scope) { |
| 659 |
_cleanup_(dns_answer_unrefp) DnsAnswer *answer = NULL; |
| 660 |
DnsResourceRecord *rr; |
| 661 |
_cleanup_(memstream_done) MemStream ms = {}; |
| 662 |
_cleanup_free_ char *buf = NULL; |
| 663 |
FILE *f; |
| 664 |
|
| 665 |
ASSERT_NOT_NULL(answer = dns_answer_new(0)); |
| 666 |
|
| 667 |
ASSERT_NOT_NULL(rr = dns_resource_record_new_full(DNS_CLASS_IN, DNS_TYPE_A, "a.example.com")); |
| 668 |
rr->ttl = 3600; |
| 669 |
rr->a.in_addr.s_addr = htobe32(0xa9fe0100); |
| 670 |
ASSERT_OK_POSITIVE(dns_answer_add(answer, rr, 1, DNS_ANSWER_CACHEABLE, /* rrsig= */ NULL)); |
| 671 |
dns_resource_record_unref(rr); |
| 672 |
|
| 673 |
ASSERT_NOT_NULL(rr = dns_resource_record_new_full(DNS_CLASS_IN, DNS_TYPE_A, "b.example.com")); |
| 674 |
rr->ttl = 3600; |
| 675 |
rr->a.in_addr.s_addr = htobe32(0xc0a80404); |
| 676 |
ASSERT_OK_POSITIVE(dns_answer_add(answer, rr, 2, DNS_ANSWER_CACHEABLE, /* rrsig= */ NULL)); |
| 677 |
dns_resource_record_unref(rr); |
| 678 |
|
| 679 |
ASSERT_NOT_NULL(rr = dns_resource_record_new_full(DNS_CLASS_IN, DNS_TYPE_A, "c.example.com")); |
| 680 |
rr->ttl = 3600; |
| 681 |
rr->a.in_addr.s_addr = htobe32(0xa9fefeff); |
| 682 |
ASSERT_OK_POSITIVE(dns_answer_add(answer, rr, 3, DNS_ANSWER_CACHEABLE, /* rrsig= */ NULL)); |
| 683 |
dns_resource_record_unref(rr); |
| 684 |
|
| 685 |
dns_answer_order_by_scope(answer, /* prefer_link_local= */ true); |
| 686 |
|
| 687 |
ASSERT_NOT_NULL(f = memstream_init(&ms)); |
| 688 |
dns_answer_dump(answer, f); |
| 689 |
ASSERT_OK(memstream_finalize(&ms, &buf, /* ret_size= */ NULL)); |
| 690 |
ASSERT_STREQ(buf, |
| 691 |
"\ta.example.com IN A 169.254.1.0\t; ttl=3600 ifindex=1 cacheable\n" |
| 692 |
"\tc.example.com IN A 169.254.254.255\t; ttl=3600 ifindex=3 cacheable\n" |
| 693 |
"\tb.example.com IN A 192.168.4.4\t; ttl=3600 ifindex=2 cacheable\n"); |
| 694 |
buf = mfree(buf); |
| 695 |
memstream_done(&ms); |
| 696 |
|
| 697 |
dns_answer_order_by_scope(answer, /* prefer_link_local= */ false); |
| 698 |
|
| 699 |
ASSERT_NOT_NULL(f = memstream_init(&ms)); |
| 700 |
dns_answer_dump(answer, f); |
| 701 |
ASSERT_OK(memstream_finalize(&ms, &buf, /* ret_size= */ NULL)); |
| 702 |
ASSERT_STREQ(buf, |
| 703 |
"\tb.example.com IN A 192.168.4.4\t; ttl=3600 ifindex=2 cacheable\n" |
| 704 |
"\ta.example.com IN A 169.254.1.0\t; ttl=3600 ifindex=1 cacheable\n" |
| 705 |
"\tc.example.com IN A 169.254.254.255\t; ttl=3600 ifindex=3 cacheable\n"); |
| 706 |
} |
| 707 |
|
| 708 |
DEFINE_TEST_MAIN(LOG_DEBUG); |