| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
|
| 3 |
#include <stdlib.h> |
| 4 |
#include <sys/socket.h> |
| 5 |
#include <unistd.h> |
| 6 |
|
| 7 |
#include "sd-json.h" |
| 8 |
|
| 9 |
#include "dns-answer.h" |
| 10 |
#include "dns-packet.h" |
| 11 |
#include "dns-rr.h" |
| 12 |
#include "dns-type.h" |
| 13 |
#include "fd-util.h" |
| 14 |
#include "fileio.h" |
| 15 |
#include "resolve-util.h" |
| 16 |
#include "resolved-def.h" |
| 17 |
#include "resolved-dns-cache.h" |
| 18 |
#include "resolved-dns-dnssec.h" |
| 19 |
#include "tests.h" |
| 20 |
#include "time-util.h" |
| 21 |
#include "tmpfile-util.h" |
| 22 |
|
| 23 |
static DnsCache new_cache(void) { |
| 24 |
return (DnsCache) { |
| 25 |
.cache_max = DEFAULT_CACHE_MAX, |
| 26 |
}; |
| 27 |
} |
| 28 |
|
| 29 |
typedef struct PutArgs { |
| 30 |
DnsCacheMode cache_mode; |
| 31 |
DnsProtocol protocol; |
| 32 |
DnsResourceKey *key; |
| 33 |
int rcode; |
| 34 |
DnsAnswer *answer; |
| 35 |
DnsPacket *full_packet; |
| 36 |
uint64_t query_flags; |
| 37 |
DnssecResult dnssec_result; |
| 38 |
uint32_t nsec_ttl; |
| 39 |
int owner_family; |
| 40 |
const union in_addr_union owner_address; |
| 41 |
usec_t stale_retention_usec; |
| 42 |
} PutArgs; |
| 43 |
|
| 44 |
static PutArgs mk_put_args(void) { |
| 45 |
PutArgs put_args = { |
| 46 |
.cache_mode = DNS_CACHE_MODE_YES, |
| 47 |
.protocol = DNS_PROTOCOL_DNS, |
| 48 |
.key = NULL, |
| 49 |
.rcode = DNS_RCODE_SUCCESS, |
| 50 |
.answer = dns_answer_new(0), |
| 51 |
.full_packet = NULL, |
| 52 |
.query_flags = SD_RESOLVED_AUTHENTICATED | SD_RESOLVED_CONFIDENTIAL, |
| 53 |
.dnssec_result = DNSSEC_UNSIGNED, |
| 54 |
.nsec_ttl = 3600, |
| 55 |
.owner_family = AF_INET, |
| 56 |
.owner_address = { .in.s_addr = htobe32(0x01020304) }, |
| 57 |
.stale_retention_usec = 0 |
| 58 |
}; |
| 59 |
|
| 60 |
ASSERT_NOT_NULL(put_args.answer); |
| 61 |
return put_args; |
| 62 |
} |
| 63 |
|
| 64 |
static int cache_put(DnsCache *cache, PutArgs *args) { |
| 65 |
ASSERT_NOT_NULL(cache); |
| 66 |
ASSERT_NOT_NULL(args); |
| 67 |
|
| 68 |
return dns_cache_put(cache, |
| 69 |
args->cache_mode, |
| 70 |
args->protocol, |
| 71 |
args->key, |
| 72 |
args->rcode, |
| 73 |
args->answer, |
| 74 |
args->full_packet, |
| 75 |
args->query_flags, |
| 76 |
args->dnssec_result, |
| 77 |
args->nsec_ttl, |
| 78 |
args->owner_family, |
| 79 |
&args->owner_address, |
| 80 |
args->stale_retention_usec); |
| 81 |
} |
| 82 |
|
| 83 |
static void dns_cache_unrefp(DnsCache *cache) { |
| 84 |
dns_cache_flush(cache); |
| 85 |
} |
| 86 |
|
| 87 |
static void put_args_unrefp(PutArgs *args) { |
| 88 |
ASSERT_NOT_NULL(args); |
| 89 |
|
| 90 |
dns_resource_key_unref(args->key); |
| 91 |
dns_answer_unref(args->answer); |
| 92 |
dns_packet_unref(args->full_packet); |
| 93 |
} |
| 94 |
|
| 95 |
static char* checked_strdup(const char *str) { |
| 96 |
char *copy = strdup(str); |
| 97 |
ASSERT_NOT_NULL(copy); |
| 98 |
return copy; |
| 99 |
} |
| 100 |
|
| 101 |
static void answer_add_a(PutArgs *args, DnsResourceKey *key, int addr, int ttl, DnsAnswerFlags flags) { |
| 102 |
_cleanup_(dns_resource_record_unrefp) DnsResourceRecord *rr = NULL; |
| 103 |
|
| 104 |
rr = dns_resource_record_new(key); |
| 105 |
ASSERT_NOT_NULL(rr); |
| 106 |
rr->a.in_addr.s_addr = htobe32(addr); |
| 107 |
rr->ttl = ttl; |
| 108 |
dns_answer_add(args->answer, rr, 1, flags, NULL); |
| 109 |
} |
| 110 |
|
| 111 |
static void answer_add_cname(PutArgs *args, DnsResourceKey *key, const char *alias, int ttl, DnsAnswerFlags flags) { |
| 112 |
_cleanup_(dns_resource_record_unrefp) DnsResourceRecord *rr = NULL; |
| 113 |
|
| 114 |
rr = dns_resource_record_new(key); |
| 115 |
ASSERT_NOT_NULL(rr); |
| 116 |
rr->cname.name = checked_strdup(alias); |
| 117 |
rr->ttl = ttl; |
| 118 |
dns_answer_add(args->answer, rr, 1, flags, NULL); |
| 119 |
} |
| 120 |
|
| 121 |
static void answer_add_opt(PutArgs *args, DnsResourceKey *key, int ttl, DnsAnswerFlags flags) { |
| 122 |
_cleanup_(dns_resource_record_unrefp) DnsResourceRecord *rr = NULL; |
| 123 |
|
| 124 |
rr = dns_resource_record_new(key); |
| 125 |
ASSERT_NOT_NULL(rr); |
| 126 |
rr->opt.data_size = 0; |
| 127 |
rr->ttl = ttl; |
| 128 |
dns_answer_add(args->answer, rr, 1, flags, NULL); |
| 129 |
} |
| 130 |
|
| 131 |
#define BY_IDX(json, idx) sd_json_variant_by_index(json, idx) |
| 132 |
#define BY_KEY(json, key) sd_json_variant_by_key(json, key) |
| 133 |
#define INTVAL(json) sd_json_variant_integer(json) |
| 134 |
#define STRVAL(json) sd_json_variant_string(json) |
| 135 |
|
| 136 |
/* ================================================================ |
| 137 |
* dns_cache_put() |
| 138 |
* ================================================================ */ |
| 139 |
|
| 140 |
TEST(dns_a_success_is_cached) { |
| 141 |
_cleanup_(dns_cache_unrefp) DnsCache cache = new_cache(); |
| 142 |
_cleanup_(put_args_unrefp) PutArgs put_args = mk_put_args(); |
| 143 |
|
| 144 |
put_args.key = dns_resource_key_new(DNS_CLASS_IN, DNS_TYPE_A, "www.example.com"); |
| 145 |
ASSERT_NOT_NULL(put_args.key); |
| 146 |
put_args.rcode = DNS_RCODE_SUCCESS; |
| 147 |
answer_add_a(&put_args, put_args.key, 0xc0a8017f, 3600, DNS_ANSWER_CACHEABLE); |
| 148 |
|
| 149 |
ASSERT_OK(cache_put(&cache, &put_args)); |
| 150 |
ASSERT_FALSE(dns_cache_is_empty(&cache)); |
| 151 |
} |
| 152 |
|
| 153 |
TEST(dns_a_success_non_matching_type_is_cached) { |
| 154 |
_cleanup_(dns_cache_unrefp) DnsCache cache = new_cache(); |
| 155 |
_cleanup_(put_args_unrefp) PutArgs put_args = mk_put_args(); |
| 156 |
_cleanup_(dns_resource_key_unrefp) DnsResourceKey *key = NULL; |
| 157 |
|
| 158 |
put_args.key = dns_resource_key_new(DNS_CLASS_IN, DNS_TYPE_A, "www.example.com"); |
| 159 |
ASSERT_NOT_NULL(put_args.key); |
| 160 |
put_args.rcode = DNS_RCODE_SUCCESS; |
| 161 |
|
| 162 |
key = dns_resource_key_new(DNS_CLASS_IN, DNS_TYPE_AAAA, "www.example.com"); |
| 163 |
ASSERT_NOT_NULL(key); |
| 164 |
answer_add_a(&put_args, key, 0xc0a8017f, 3600, DNS_ANSWER_CACHEABLE); |
| 165 |
|
| 166 |
ASSERT_OK(cache_put(&cache, &put_args)); |
| 167 |
ASSERT_FALSE(dns_cache_is_empty(&cache)); |
| 168 |
} |
| 169 |
|
| 170 |
TEST(dns_a_success_non_matching_name_is_cached) { |
| 171 |
_cleanup_(dns_cache_unrefp) DnsCache cache = new_cache(); |
| 172 |
_cleanup_(put_args_unrefp) PutArgs put_args = mk_put_args(); |
| 173 |
_cleanup_(dns_resource_key_unrefp) DnsResourceKey *key = NULL; |
| 174 |
|
| 175 |
put_args.key = dns_resource_key_new(DNS_CLASS_IN, DNS_TYPE_A, "www.example.com"); |
| 176 |
ASSERT_NOT_NULL(put_args.key); |
| 177 |
put_args.rcode = DNS_RCODE_SUCCESS; |
| 178 |
|
| 179 |
key = dns_resource_key_new(DNS_CLASS_IN, DNS_TYPE_A, "example.com"); |
| 180 |
ASSERT_NOT_NULL(key); |
| 181 |
answer_add_a(&put_args, key, 0xc0a8017f, 3600, DNS_ANSWER_CACHEABLE); |
| 182 |
|
| 183 |
ASSERT_OK(cache_put(&cache, &put_args)); |
| 184 |
ASSERT_FALSE(dns_cache_is_empty(&cache)); |
| 185 |
} |
| 186 |
|
| 187 |
TEST(dns_a_success_mdns_no_key_is_cached) { |
| 188 |
_cleanup_(dns_cache_unrefp) DnsCache cache = new_cache(); |
| 189 |
_cleanup_(put_args_unrefp) PutArgs put_args = mk_put_args(); |
| 190 |
_cleanup_(dns_resource_key_unrefp) DnsResourceKey *key = NULL; |
| 191 |
|
| 192 |
put_args.protocol = DNS_PROTOCOL_MDNS; |
| 193 |
put_args.rcode = DNS_RCODE_SUCCESS; |
| 194 |
|
| 195 |
key = dns_resource_key_new(DNS_CLASS_IN, DNS_TYPE_A, "example.com"); |
| 196 |
ASSERT_NOT_NULL(key); |
| 197 |
answer_add_a(&put_args, key, 0xc0a8017f, 3600, DNS_ANSWER_CACHEABLE); |
| 198 |
|
| 199 |
ASSERT_OK(cache_put(&cache, &put_args)); |
| 200 |
ASSERT_FALSE(dns_cache_is_empty(&cache)); |
| 201 |
} |
| 202 |
|
| 203 |
TEST(dns_a_success_mdns_update_existing) { |
| 204 |
_cleanup_(dns_cache_unrefp) DnsCache cache = new_cache(); |
| 205 |
_cleanup_(put_args_unrefp) PutArgs args1 = mk_put_args(), args2 = mk_put_args(); |
| 206 |
DnsResourceKey *key = NULL; |
| 207 |
|
| 208 |
args1.protocol = DNS_PROTOCOL_MDNS; |
| 209 |
args1.rcode = DNS_RCODE_SUCCESS; |
| 210 |
key = dns_resource_key_new(DNS_CLASS_IN, DNS_TYPE_A, "example.com"); |
| 211 |
ASSERT_NOT_NULL(key); |
| 212 |
answer_add_a(&args1, key, 0xc0a8017f, 3600, DNS_ANSWER_CACHEABLE | DNS_ANSWER_SHARED_OWNER); |
| 213 |
dns_resource_key_unref(key); |
| 214 |
|
| 215 |
ASSERT_OK(cache_put(&cache, &args1)); |
| 216 |
|
| 217 |
args2.protocol = DNS_PROTOCOL_MDNS; |
| 218 |
args2.rcode = DNS_RCODE_SUCCESS; |
| 219 |
key = dns_resource_key_new(DNS_CLASS_IN, DNS_TYPE_A, "example.com"); |
| 220 |
ASSERT_NOT_NULL(key); |
| 221 |
answer_add_a(&args2, key, 0xc0a8017f, 2400, DNS_ANSWER_CACHEABLE | DNS_ANSWER_SHARED_OWNER); |
| 222 |
dns_resource_key_unref(key); |
| 223 |
|
| 224 |
ASSERT_OK(cache_put(&cache, &args2)); |
| 225 |
|
| 226 |
ASSERT_EQ(dns_cache_size(&cache), 1u); |
| 227 |
} |
| 228 |
|
| 229 |
TEST(dns_a_success_mdns_zero_ttl_removes_existing) { |
| 230 |
_cleanup_(dns_cache_unrefp) DnsCache cache = new_cache(); |
| 231 |
_cleanup_(put_args_unrefp) PutArgs args1 = mk_put_args(), args2 = mk_put_args(); |
| 232 |
DnsResourceKey *key = NULL; |
| 233 |
|
| 234 |
args1.protocol = DNS_PROTOCOL_MDNS; |
| 235 |
args1.rcode = DNS_RCODE_SUCCESS; |
| 236 |
key = dns_resource_key_new(DNS_CLASS_IN, DNS_TYPE_A, "example.com"); |
| 237 |
ASSERT_NOT_NULL(key); |
| 238 |
answer_add_a(&args1, key, 0xc0a8017f, 3600, DNS_ANSWER_CACHEABLE | DNS_ANSWER_SHARED_OWNER); |
| 239 |
dns_resource_key_unref(key); |
| 240 |
|
| 241 |
ASSERT_OK(cache_put(&cache, &args1)); |
| 242 |
|
| 243 |
args2.protocol = DNS_PROTOCOL_MDNS; |
| 244 |
args2.rcode = DNS_RCODE_SUCCESS; |
| 245 |
key = dns_resource_key_new(DNS_CLASS_IN, DNS_TYPE_A, "example.com"); |
| 246 |
ASSERT_NOT_NULL(key); |
| 247 |
answer_add_a(&args2, key, 0xc0a8017f, 0, DNS_ANSWER_CACHEABLE | DNS_ANSWER_SHARED_OWNER); |
| 248 |
dns_resource_key_unref(key); |
| 249 |
|
| 250 |
ASSERT_OK(cache_put(&cache, &args2)); |
| 251 |
|
| 252 |
ASSERT_TRUE(dns_cache_is_empty(&cache)); |
| 253 |
} |
| 254 |
|
| 255 |
TEST(dns_a_success_mdns_same_key_different_payloads) { |
| 256 |
_cleanup_(dns_cache_unrefp) DnsCache cache = new_cache(); |
| 257 |
_cleanup_(put_args_unrefp) PutArgs put_args = mk_put_args(); |
| 258 |
DnsResourceKey *key = NULL; |
| 259 |
|
| 260 |
put_args.protocol = DNS_PROTOCOL_MDNS; |
| 261 |
put_args.rcode = DNS_RCODE_SUCCESS; |
| 262 |
|
| 263 |
key = dns_resource_key_new(DNS_CLASS_IN, DNS_TYPE_A, "example.com"); |
| 264 |
ASSERT_NOT_NULL(key); |
| 265 |
answer_add_a(&put_args, key, 0xc0a8017f, 3600, DNS_ANSWER_CACHEABLE | DNS_ANSWER_SHARED_OWNER); |
| 266 |
dns_resource_key_unref(key); |
| 267 |
|
| 268 |
key = dns_resource_key_new(DNS_CLASS_IN, DNS_TYPE_A, "example.com"); |
| 269 |
ASSERT_NOT_NULL(key); |
| 270 |
answer_add_a(&put_args, key, 0x7f01a8cc, 2400, DNS_ANSWER_CACHEABLE | DNS_ANSWER_SHARED_OWNER); |
| 271 |
dns_resource_key_unref(key); |
| 272 |
|
| 273 |
ASSERT_EQ(dns_answer_size(put_args.answer), 2u); |
| 274 |
|
| 275 |
ASSERT_OK(cache_put(&cache, &put_args)); |
| 276 |
ASSERT_EQ(dns_cache_size(&cache), 1u); |
| 277 |
} |
| 278 |
|
| 279 |
TEST(dns_a_success_escaped_key_returns_error) { |
| 280 |
_cleanup_(dns_cache_unrefp) DnsCache cache = new_cache(); |
| 281 |
_cleanup_(put_args_unrefp) PutArgs put_args = mk_put_args(); |
| 282 |
_cleanup_(dns_resource_key_unrefp) DnsResourceKey *key = NULL; |
| 283 |
|
| 284 |
put_args.key = dns_resource_key_new(DNS_CLASS_IN, DNS_TYPE_A, "www.example.com"); |
| 285 |
ASSERT_NOT_NULL(put_args.key); |
| 286 |
put_args.rcode = DNS_RCODE_SUCCESS; |
| 287 |
|
| 288 |
key = dns_resource_key_new(DNS_CLASS_IN, DNS_TYPE_A, "www.\\example.com"); |
| 289 |
ASSERT_NOT_NULL(key); |
| 290 |
answer_add_a(&put_args, key, 0xc0a8017f, 3600, DNS_ANSWER_CACHEABLE); |
| 291 |
|
| 292 |
ASSERT_ERROR(cache_put(&cache, &put_args), EINVAL); |
| 293 |
ASSERT_TRUE(dns_cache_is_empty(&cache)); |
| 294 |
} |
| 295 |
|
| 296 |
TEST(dns_a_success_empty_answer_is_not_cached) { |
| 297 |
_cleanup_(dns_cache_unrefp) DnsCache cache = new_cache(); |
| 298 |
_cleanup_(put_args_unrefp) PutArgs put_args = mk_put_args(); |
| 299 |
|
| 300 |
put_args.key = dns_resource_key_new(DNS_CLASS_IN, DNS_TYPE_A, "www.example.com"); |
| 301 |
ASSERT_NOT_NULL(put_args.key); |
| 302 |
put_args.rcode = DNS_RCODE_SUCCESS; |
| 303 |
|
| 304 |
ASSERT_OK(cache_put(&cache, &put_args)); |
| 305 |
ASSERT_TRUE(dns_cache_is_empty(&cache)); |
| 306 |
} |
| 307 |
|
| 308 |
TEST(dns_a_success_any_class_is_not_cached) { |
| 309 |
_cleanup_(dns_cache_unrefp) DnsCache cache = new_cache(); |
| 310 |
_cleanup_(put_args_unrefp) PutArgs put_args = mk_put_args(); |
| 311 |
|
| 312 |
put_args.key = dns_resource_key_new(DNS_CLASS_ANY, DNS_TYPE_A, "www.example.com"); |
| 313 |
ASSERT_NOT_NULL(put_args.key); |
| 314 |
put_args.rcode = DNS_RCODE_SUCCESS; |
| 315 |
answer_add_a(&put_args, put_args.key, 0xc0a8017f, 3600, DNS_ANSWER_CACHEABLE); |
| 316 |
|
| 317 |
ASSERT_OK(cache_put(&cache, &put_args)); |
| 318 |
ASSERT_TRUE(dns_cache_is_empty(&cache)); |
| 319 |
} |
| 320 |
|
| 321 |
TEST(dns_a_success_any_type_not_cached) { |
| 322 |
_cleanup_(dns_cache_unrefp) DnsCache cache = new_cache(); |
| 323 |
_cleanup_(put_args_unrefp) PutArgs put_args = mk_put_args(); |
| 324 |
|
| 325 |
put_args.key = dns_resource_key_new(DNS_CLASS_IN, DNS_TYPE_ANY, "www.example.com"); |
| 326 |
ASSERT_NOT_NULL(put_args.key); |
| 327 |
put_args.rcode = DNS_RCODE_SUCCESS; |
| 328 |
answer_add_opt(&put_args, put_args.key, 3600, DNS_ANSWER_CACHEABLE); |
| 329 |
|
| 330 |
ASSERT_OK(cache_put(&cache, &put_args)); |
| 331 |
ASSERT_TRUE(dns_cache_is_empty(&cache)); |
| 332 |
} |
| 333 |
|
| 334 |
TEST(dns_a_success_opt_not_cached) { |
| 335 |
_cleanup_(dns_cache_unrefp) DnsCache cache = new_cache(); |
| 336 |
_cleanup_(put_args_unrefp) PutArgs put_args = mk_put_args(); |
| 337 |
|
| 338 |
put_args.key = dns_resource_key_new(DNS_CLASS_IN, DNS_TYPE_OPT, "www.example.com"); |
| 339 |
ASSERT_NOT_NULL(put_args.key); |
| 340 |
put_args.rcode = DNS_RCODE_SUCCESS; |
| 341 |
answer_add_opt(&put_args, put_args.key, 3600, DNS_ANSWER_CACHEABLE); |
| 342 |
|
| 343 |
ASSERT_OK(cache_put(&cache, &put_args)); |
| 344 |
ASSERT_TRUE(dns_cache_is_empty(&cache)); |
| 345 |
} |
| 346 |
|
| 347 |
TEST(dns_a_nxdomain_is_cached) { |
| 348 |
_cleanup_(dns_cache_unrefp) DnsCache cache = new_cache(); |
| 349 |
_cleanup_(put_args_unrefp) PutArgs put_args = mk_put_args(); |
| 350 |
|
| 351 |
put_args.key = dns_resource_key_new(DNS_CLASS_IN, DNS_TYPE_A, "www.example.com"); |
| 352 |
ASSERT_NOT_NULL(put_args.key); |
| 353 |
put_args.rcode = DNS_RCODE_NXDOMAIN; |
| 354 |
dns_answer_add_soa(put_args.answer, "example.com", 3600, 0); |
| 355 |
|
| 356 |
ASSERT_OK(cache_put(&cache, &put_args)); |
| 357 |
ASSERT_FALSE(dns_cache_is_empty(&cache)); |
| 358 |
} |
| 359 |
|
| 360 |
TEST(dns_a_nxdomain_no_soa_not_cached) { |
| 361 |
_cleanup_(dns_cache_unrefp) DnsCache cache = new_cache(); |
| 362 |
_cleanup_(put_args_unrefp) PutArgs put_args = mk_put_args(); |
| 363 |
|
| 364 |
put_args.key = dns_resource_key_new(DNS_CLASS_IN, DNS_TYPE_A, "www.example.com"); |
| 365 |
ASSERT_NOT_NULL(put_args.key); |
| 366 |
put_args.rcode = DNS_RCODE_NXDOMAIN; |
| 367 |
|
| 368 |
ASSERT_OK(cache_put(&cache, &put_args)); |
| 369 |
ASSERT_TRUE(dns_cache_is_empty(&cache)); |
| 370 |
} |
| 371 |
|
| 372 |
TEST(dns_a_nxdomain_any_class_is_not_cached) { |
| 373 |
_cleanup_(dns_cache_unrefp) DnsCache cache = new_cache(); |
| 374 |
_cleanup_(put_args_unrefp) PutArgs put_args = mk_put_args(); |
| 375 |
|
| 376 |
put_args.key = dns_resource_key_new(DNS_CLASS_ANY, DNS_TYPE_A, "www.example.com"); |
| 377 |
ASSERT_NOT_NULL(put_args.key); |
| 378 |
put_args.rcode = DNS_RCODE_NXDOMAIN; |
| 379 |
dns_answer_add_soa(put_args.answer, "example.com", 3600, 0); |
| 380 |
|
| 381 |
ASSERT_OK(cache_put(&cache, &put_args)); |
| 382 |
ASSERT_TRUE(dns_cache_is_empty(&cache)); |
| 383 |
} |
| 384 |
|
| 385 |
TEST(dns_a_nxdomain_any_type_not_cached) { |
| 386 |
_cleanup_(dns_cache_unrefp) DnsCache cache = new_cache(); |
| 387 |
_cleanup_(put_args_unrefp) PutArgs put_args = mk_put_args(); |
| 388 |
|
| 389 |
put_args.key = dns_resource_key_new(DNS_CLASS_IN, DNS_TYPE_ANY, "www.example.com"); |
| 390 |
ASSERT_NOT_NULL(put_args.key); |
| 391 |
put_args.rcode = DNS_RCODE_NXDOMAIN; |
| 392 |
dns_answer_add_soa(put_args.answer, "example.com", 3600, 0); |
| 393 |
|
| 394 |
ASSERT_OK(cache_put(&cache, &put_args)); |
| 395 |
ASSERT_TRUE(dns_cache_is_empty(&cache)); |
| 396 |
} |
| 397 |
|
| 398 |
TEST(dns_a_nxdomain_opt_not_cached) { |
| 399 |
_cleanup_(dns_cache_unrefp) DnsCache cache = new_cache(); |
| 400 |
_cleanup_(put_args_unrefp) PutArgs put_args = mk_put_args(); |
| 401 |
|
| 402 |
put_args.key = dns_resource_key_new(DNS_CLASS_IN, DNS_TYPE_OPT, "www.example.com"); |
| 403 |
ASSERT_NOT_NULL(put_args.key); |
| 404 |
put_args.rcode = DNS_RCODE_NXDOMAIN; |
| 405 |
dns_answer_add_soa(put_args.answer, "example.com", 3600, 0); |
| 406 |
|
| 407 |
ASSERT_OK(cache_put(&cache, &put_args)); |
| 408 |
ASSERT_TRUE(dns_cache_is_empty(&cache)); |
| 409 |
} |
| 410 |
|
| 411 |
TEST(dns_a_servfail_is_cached) { |
| 412 |
_cleanup_(dns_cache_unrefp) DnsCache cache = new_cache(); |
| 413 |
_cleanup_(put_args_unrefp) PutArgs put_args = mk_put_args(); |
| 414 |
|
| 415 |
put_args.key = dns_resource_key_new(DNS_CLASS_IN, DNS_TYPE_A, "www.example.com"); |
| 416 |
ASSERT_NOT_NULL(put_args.key); |
| 417 |
put_args.rcode = DNS_RCODE_SERVFAIL; |
| 418 |
|
| 419 |
ASSERT_OK(cache_put(&cache, &put_args)); |
| 420 |
ASSERT_FALSE(dns_cache_is_empty(&cache)); |
| 421 |
} |
| 422 |
|
| 423 |
TEST(dns_a_refused_is_not_cached) { |
| 424 |
_cleanup_(dns_cache_unrefp) DnsCache cache = new_cache(); |
| 425 |
_cleanup_(put_args_unrefp) PutArgs put_args = mk_put_args(); |
| 426 |
|
| 427 |
put_args.key = dns_resource_key_new(DNS_CLASS_IN, DNS_TYPE_A, "www.example.com"); |
| 428 |
ASSERT_NOT_NULL(put_args.key); |
| 429 |
put_args.rcode = DNS_RCODE_REFUSED; |
| 430 |
|
| 431 |
ASSERT_OK(cache_put(&cache, &put_args)); |
| 432 |
ASSERT_TRUE(dns_cache_is_empty(&cache)); |
| 433 |
} |
| 434 |
|
| 435 |
TEST(dns_a_success_zero_ttl_is_not_cached) { |
| 436 |
_cleanup_(dns_cache_unrefp) DnsCache cache = new_cache(); |
| 437 |
_cleanup_(put_args_unrefp) PutArgs put_args = mk_put_args(); |
| 438 |
|
| 439 |
put_args.key = dns_resource_key_new(DNS_CLASS_IN, DNS_TYPE_A, "www.example.com"); |
| 440 |
ASSERT_NOT_NULL(put_args.key); |
| 441 |
put_args.rcode = DNS_RCODE_SUCCESS; |
| 442 |
answer_add_a(&put_args, put_args.key, 0xc0a8017f, 0, DNS_ANSWER_CACHEABLE); |
| 443 |
|
| 444 |
ASSERT_OK(cache_put(&cache, &put_args)); |
| 445 |
ASSERT_TRUE(dns_cache_is_empty(&cache)); |
| 446 |
} |
| 447 |
|
| 448 |
TEST(dns_a_success_zero_ttl_removes_existing_entry) { |
| 449 |
_cleanup_(dns_cache_unrefp) DnsCache cache = new_cache(); |
| 450 |
_cleanup_(put_args_unrefp) PutArgs put_args = mk_put_args(); |
| 451 |
|
| 452 |
put_args.key = dns_resource_key_new(DNS_CLASS_IN, DNS_TYPE_A, "www.example.com"); |
| 453 |
ASSERT_NOT_NULL(put_args.key); |
| 454 |
put_args.rcode = DNS_RCODE_SUCCESS; |
| 455 |
answer_add_a(&put_args, put_args.key, 0xc0a8017f, 3600, DNS_ANSWER_CACHEABLE); |
| 456 |
|
| 457 |
ASSERT_OK(cache_put(&cache, &put_args)); |
| 458 |
ASSERT_FALSE(dns_cache_is_empty(&cache)); |
| 459 |
|
| 460 |
dns_answer_unref(put_args.answer); |
| 461 |
put_args.answer = dns_answer_new(1); |
| 462 |
ASSERT_NOT_NULL(put_args.answer); |
| 463 |
answer_add_a(&put_args, put_args.key, 0xc0a8017f, 0, DNS_ANSWER_CACHEABLE); |
| 464 |
|
| 465 |
ASSERT_OK(cache_put(&cache, &put_args)); |
| 466 |
ASSERT_TRUE(dns_cache_is_empty(&cache)); |
| 467 |
} |
| 468 |
|
| 469 |
TEST(dns_a_success_not_cacheable_is_not_cached) { |
| 470 |
_cleanup_(dns_cache_unrefp) DnsCache cache = new_cache(); |
| 471 |
_cleanup_(put_args_unrefp) PutArgs put_args = mk_put_args(); |
| 472 |
|
| 473 |
put_args.key = dns_resource_key_new(DNS_CLASS_IN, DNS_TYPE_A, "www.example.com"); |
| 474 |
ASSERT_NOT_NULL(put_args.key); |
| 475 |
put_args.rcode = DNS_RCODE_SUCCESS; |
| 476 |
answer_add_a(&put_args, put_args.key, 0xc0a8017f, 3600, 0); |
| 477 |
|
| 478 |
ASSERT_OK(cache_put(&cache, &put_args)); |
| 479 |
ASSERT_TRUE(dns_cache_is_empty(&cache)); |
| 480 |
} |
| 481 |
|
| 482 |
TEST(dns_a_to_cname_success_is_cached) { |
| 483 |
_cleanup_(dns_cache_unrefp) DnsCache cache = new_cache(); |
| 484 |
_cleanup_(put_args_unrefp) PutArgs put_args = mk_put_args(); |
| 485 |
_cleanup_(dns_resource_key_unrefp) DnsResourceKey *key = NULL; |
| 486 |
|
| 487 |
put_args.key = dns_resource_key_new(DNS_CLASS_IN, DNS_TYPE_A, "www.example.com"); |
| 488 |
ASSERT_NOT_NULL(put_args.key); |
| 489 |
put_args.rcode = DNS_RCODE_SUCCESS; |
| 490 |
|
| 491 |
key = dns_resource_key_new(DNS_CLASS_IN, DNS_TYPE_CNAME, "www.example.com"); |
| 492 |
ASSERT_NOT_NULL(key); |
| 493 |
answer_add_cname(&put_args, key, "example.com", 3600, DNS_ANSWER_CACHEABLE); |
| 494 |
|
| 495 |
ASSERT_OK(cache_put(&cache, &put_args)); |
| 496 |
ASSERT_FALSE(dns_cache_is_empty(&cache)); |
| 497 |
} |
| 498 |
|
| 499 |
TEST(dns_a_to_cname_success_escaped_name_returns_error) { |
| 500 |
_cleanup_(dns_cache_unrefp) DnsCache cache = new_cache(); |
| 501 |
_cleanup_(put_args_unrefp) PutArgs put_args = mk_put_args(); |
| 502 |
_cleanup_(dns_resource_key_unrefp) DnsResourceKey *key = NULL; |
| 503 |
|
| 504 |
put_args.key = dns_resource_key_new(DNS_CLASS_IN, DNS_TYPE_A, "www.example.com"); |
| 505 |
ASSERT_NOT_NULL(put_args.key); |
| 506 |
put_args.rcode = DNS_RCODE_SUCCESS; |
| 507 |
|
| 508 |
key = dns_resource_key_new(DNS_CLASS_IN, DNS_TYPE_CNAME, "www.\\example.com"); |
| 509 |
ASSERT_NOT_NULL(key); |
| 510 |
answer_add_cname(&put_args, key, "example.com", 3600, DNS_ANSWER_CACHEABLE); |
| 511 |
|
| 512 |
ASSERT_ERROR(cache_put(&cache, &put_args), EINVAL); |
| 513 |
ASSERT_TRUE(dns_cache_is_empty(&cache)); |
| 514 |
} |
| 515 |
|
| 516 |
TEST(dns_cache_size_honored) { |
| 517 |
_cleanup_(dns_cache_unrefp) DnsCache cache = new_cache(); |
| 518 |
_cleanup_(put_args_unrefp) PutArgs put_args = mk_put_args(); |
| 519 |
|
| 520 |
cache.cache_max = 4; |
| 521 |
|
| 522 |
put_args.key = dns_resource_key_new(DNS_CLASS_IN, DNS_TYPE_A, "one.example.com"); |
| 523 |
ASSERT_NOT_NULL(put_args.key); |
| 524 |
put_args.rcode = DNS_RCODE_SUCCESS; |
| 525 |
answer_add_a(&put_args, put_args.key, 0xc0a80101, 3600, DNS_ANSWER_CACHEABLE); |
| 526 |
ASSERT_OK(cache_put(&cache, &put_args)); |
| 527 |
|
| 528 |
dns_resource_key_unref(put_args.key); |
| 529 |
dns_answer_unref(put_args.answer); |
| 530 |
put_args.answer = dns_answer_new(1); |
| 531 |
ASSERT_NOT_NULL(put_args.answer); |
| 532 |
|
| 533 |
put_args.key = dns_resource_key_new(DNS_CLASS_IN, DNS_TYPE_A, "two.example.com"); |
| 534 |
ASSERT_NOT_NULL(put_args.key); |
| 535 |
answer_add_a(&put_args, put_args.key, 0xc0a80102, 3600, DNS_ANSWER_CACHEABLE); |
| 536 |
ASSERT_OK(cache_put(&cache, &put_args)); |
| 537 |
|
| 538 |
dns_resource_key_unref(put_args.key); |
| 539 |
dns_answer_unref(put_args.answer); |
| 540 |
put_args.answer = dns_answer_new(1); |
| 541 |
ASSERT_NOT_NULL(put_args.answer); |
| 542 |
|
| 543 |
put_args.key = dns_resource_key_new(DNS_CLASS_IN, DNS_TYPE_A, "three.example.com"); |
| 544 |
ASSERT_NOT_NULL(put_args.key); |
| 545 |
answer_add_a(&put_args, put_args.key, 0xc0a80103, 3600, DNS_ANSWER_CACHEABLE); |
| 546 |
ASSERT_OK(cache_put(&cache, &put_args)); |
| 547 |
|
| 548 |
dns_resource_key_unref(put_args.key); |
| 549 |
dns_answer_unref(put_args.answer); |
| 550 |
put_args.answer = dns_answer_new(1); |
| 551 |
ASSERT_NOT_NULL(put_args.answer); |
| 552 |
|
| 553 |
put_args.key = dns_resource_key_new(DNS_CLASS_IN, DNS_TYPE_A, "four.example.com"); |
| 554 |
ASSERT_NOT_NULL(put_args.key); |
| 555 |
answer_add_a(&put_args, put_args.key, 0xc0a80104, 3600, DNS_ANSWER_CACHEABLE); |
| 556 |
ASSERT_OK(cache_put(&cache, &put_args)); |
| 557 |
|
| 558 |
dns_resource_key_unref(put_args.key); |
| 559 |
dns_answer_unref(put_args.answer); |
| 560 |
put_args.answer = dns_answer_new(1); |
| 561 |
ASSERT_NOT_NULL(put_args.answer); |
| 562 |
|
| 563 |
put_args.key = dns_resource_key_new(DNS_CLASS_IN, DNS_TYPE_A, "five.example.com"); |
| 564 |
ASSERT_NOT_NULL(put_args.key); |
| 565 |
answer_add_a(&put_args, put_args.key, 0xc0a80105, 3600, DNS_ANSWER_CACHEABLE); |
| 566 |
ASSERT_OK(cache_put(&cache, &put_args)); |
| 567 |
|
| 568 |
/* Each dns_cache_put() call reserves space for both the answer RR and the key (cache_keys=2), |
| 569 |
* so eviction triggers when prioq_size + 2 >= cache_max (i.e. at the 3rd entry with cache_max=4). |
| 570 |
* After 5 inserts, only the last 2 entries remain. */ |
| 571 |
ASSERT_EQ(dns_cache_size(&cache), 2u); |
| 572 |
} |
| 573 |
|
| 574 |
TEST(dns_cache_size_zero_evicts_all) { |
| 575 |
_cleanup_(dns_cache_unrefp) DnsCache cache = new_cache(); |
| 576 |
_cleanup_(put_args_unrefp) PutArgs put_args = mk_put_args(); |
| 577 |
|
| 578 |
cache.cache_max = 0; |
| 579 |
|
| 580 |
put_args.key = dns_resource_key_new(DNS_CLASS_IN, DNS_TYPE_A, "www.example.com"); |
| 581 |
ASSERT_NOT_NULL(put_args.key); |
| 582 |
put_args.rcode = DNS_RCODE_SUCCESS; |
| 583 |
answer_add_a(&put_args, put_args.key, 0xc0a8017f, 3600, DNS_ANSWER_CACHEABLE); |
| 584 |
ASSERT_OK(cache_put(&cache, &put_args)); |
| 585 |
|
| 586 |
ASSERT_TRUE(dns_cache_is_empty(&cache)); |
| 587 |
} |
| 588 |
|
| 589 |
/* ================================================================ |
| 590 |
* dns_cache_lookup() |
| 591 |
* ================================================================ */ |
| 592 |
|
| 593 |
TEST(dns_cache_lookup_miss) { |
| 594 |
_cleanup_(dns_cache_unrefp) DnsCache cache = new_cache(); |
| 595 |
_cleanup_(dns_answer_unrefp) DnsAnswer *ret_answer = NULL; |
| 596 |
_cleanup_(dns_packet_unrefp) DnsPacket *ret_full_packet = NULL; |
| 597 |
_cleanup_(dns_resource_key_unrefp) DnsResourceKey *key = NULL; |
| 598 |
int query_flags, ret_rcode; |
| 599 |
uint64_t ret_query_flags; |
| 600 |
|
| 601 |
key = dns_resource_key_new(DNS_CLASS_IN, DNS_TYPE_A, "www.example.com"); |
| 602 |
ASSERT_NOT_NULL(key); |
| 603 |
query_flags = 0; |
| 604 |
ASSERT_FALSE(dns_cache_lookup(&cache, key, query_flags, &ret_rcode, &ret_answer, &ret_full_packet, &ret_query_flags, NULL)); |
| 605 |
|
| 606 |
ASSERT_EQ(cache.n_hit, 0u); |
| 607 |
ASSERT_EQ(cache.n_miss, 1u); |
| 608 |
|
| 609 |
ASSERT_EQ(ret_rcode, DNS_RCODE_SUCCESS); |
| 610 |
ASSERT_EQ(ret_query_flags, 0u); |
| 611 |
|
| 612 |
ASSERT_EQ(dns_answer_size(ret_answer), 0u); |
| 613 |
} |
| 614 |
|
| 615 |
TEST(dns_cache_lookup_success) { |
| 616 |
_cleanup_(dns_cache_unrefp) DnsCache cache = new_cache(); |
| 617 |
_cleanup_(put_args_unrefp) PutArgs put_args = mk_put_args(); |
| 618 |
_cleanup_(dns_answer_unrefp) DnsAnswer *ret_answer = NULL; |
| 619 |
_cleanup_(dns_packet_unrefp) DnsPacket *ret_full_packet = NULL; |
| 620 |
_cleanup_(dns_resource_record_unrefp) DnsResourceRecord *rr = NULL; |
| 621 |
_cleanup_(dns_resource_key_unrefp) DnsResourceKey *key = NULL; |
| 622 |
int query_flags, ret_rcode; |
| 623 |
uint64_t ret_query_flags; |
| 624 |
|
| 625 |
put_args.key = dns_resource_key_new(DNS_CLASS_IN, DNS_TYPE_A, "www.example.com"); |
| 626 |
ASSERT_NOT_NULL(put_args.key); |
| 627 |
put_args.rcode = DNS_RCODE_SUCCESS; |
| 628 |
answer_add_a(&put_args, put_args.key, 0xc0a8017f, 3600, DNS_ANSWER_CACHEABLE); |
| 629 |
cache_put(&cache, &put_args); |
| 630 |
|
| 631 |
ASSERT_EQ(dns_cache_size(&cache), 1u); |
| 632 |
|
| 633 |
key = dns_resource_key_new(DNS_CLASS_IN, DNS_TYPE_A, "www.example.com"); |
| 634 |
ASSERT_NOT_NULL(key); |
| 635 |
query_flags = 0; |
| 636 |
ASSERT_OK_POSITIVE(dns_cache_lookup(&cache, key, query_flags, &ret_rcode, &ret_answer, &ret_full_packet, &ret_query_flags, NULL)); |
| 637 |
|
| 638 |
ASSERT_EQ(cache.n_hit, 1u); |
| 639 |
ASSERT_EQ(cache.n_miss, 0u); |
| 640 |
|
| 641 |
ASSERT_EQ(ret_rcode, DNS_RCODE_SUCCESS); |
| 642 |
ASSERT_EQ(ret_query_flags, SD_RESOLVED_CONFIDENTIAL); |
| 643 |
|
| 644 |
ASSERT_EQ(dns_answer_size(ret_answer), 1u); |
| 645 |
|
| 646 |
rr = dns_resource_record_new_full(DNS_CLASS_IN, DNS_TYPE_A, "www.example.com"); |
| 647 |
ASSERT_NOT_NULL(rr); |
| 648 |
rr->a.in_addr.s_addr = htobe32(0xc0a8017f); |
| 649 |
ASSERT_TRUE(dns_answer_contains(ret_answer, rr)); |
| 650 |
} |
| 651 |
|
| 652 |
TEST(dns_cache_lookup_clamp_ttl) { |
| 653 |
_cleanup_(dns_cache_unrefp) DnsCache cache = new_cache(); |
| 654 |
_cleanup_(put_args_unrefp) PutArgs put_args = mk_put_args(); |
| 655 |
_cleanup_(dns_answer_unrefp) DnsAnswer *ret_answer = NULL; |
| 656 |
_cleanup_(dns_packet_unrefp) DnsPacket *ret_full_packet = NULL; |
| 657 |
_cleanup_(dns_resource_record_unrefp) DnsResourceRecord *rr = NULL; |
| 658 |
_cleanup_(dns_resource_key_unrefp) DnsResourceKey *key = NULL; |
| 659 |
int query_flags, ret_rcode; |
| 660 |
uint64_t ret_query_flags; |
| 661 |
|
| 662 |
put_args.key = dns_resource_key_new(DNS_CLASS_IN, DNS_TYPE_A, "www.example.com"); |
| 663 |
ASSERT_NOT_NULL(put_args.key); |
| 664 |
put_args.rcode = DNS_RCODE_SUCCESS; |
| 665 |
answer_add_a(&put_args, put_args.key, 0xc0a8017f, 3600, DNS_ANSWER_CACHEABLE); |
| 666 |
cache_put(&cache, &put_args); |
| 667 |
|
| 668 |
ASSERT_EQ(dns_cache_size(&cache), 1u); |
| 669 |
|
| 670 |
key = dns_resource_key_new(DNS_CLASS_IN, DNS_TYPE_A, "www.example.com"); |
| 671 |
ASSERT_NOT_NULL(key); |
| 672 |
query_flags = SD_RESOLVED_CLAMP_TTL; |
| 673 |
ASSERT_OK_POSITIVE(dns_cache_lookup(&cache, key, query_flags, &ret_rcode, &ret_answer, &ret_full_packet, &ret_query_flags, NULL)); |
| 674 |
|
| 675 |
ASSERT_EQ(cache.n_hit, 1u); |
| 676 |
ASSERT_EQ(cache.n_miss, 0u); |
| 677 |
|
| 678 |
ASSERT_EQ(ret_rcode, DNS_RCODE_SUCCESS); |
| 679 |
ASSERT_EQ(ret_query_flags, SD_RESOLVED_CONFIDENTIAL); |
| 680 |
|
| 681 |
ASSERT_EQ(dns_answer_size(ret_answer), 1u); |
| 682 |
|
| 683 |
rr = dns_resource_record_new_full(DNS_CLASS_IN, DNS_TYPE_A, "www.example.com"); |
| 684 |
ASSERT_NOT_NULL(rr); |
| 685 |
rr->a.in_addr.s_addr = htobe32(0xc0a8017f); |
| 686 |
ASSERT_TRUE(dns_answer_contains(ret_answer, rr)); |
| 687 |
} |
| 688 |
|
| 689 |
TEST(dns_cache_lookup_returns_most_recent_response) { |
| 690 |
_cleanup_(dns_cache_unrefp) DnsCache cache = new_cache(); |
| 691 |
_cleanup_(put_args_unrefp) PutArgs args1 = mk_put_args(), args2 = mk_put_args(); |
| 692 |
_cleanup_(dns_answer_unrefp) DnsAnswer *ret_answer = NULL; |
| 693 |
_cleanup_(dns_packet_unrefp) DnsPacket *ret_full_packet = NULL; |
| 694 |
_cleanup_(dns_resource_key_unrefp) DnsResourceKey *key = NULL; |
| 695 |
DnsResourceRecord *rr = NULL; |
| 696 |
int query_flags, ret_rcode; |
| 697 |
uint64_t ret_query_flags; |
| 698 |
|
| 699 |
args1.key = dns_resource_key_new(DNS_CLASS_IN, DNS_TYPE_A, "www.example.com"); |
| 700 |
ASSERT_NOT_NULL(args1.key); |
| 701 |
args1.rcode = DNS_RCODE_SUCCESS; |
| 702 |
answer_add_a(&args1, args1.key, 0xc0a8017f, 3600, DNS_ANSWER_CACHEABLE); |
| 703 |
cache_put(&cache, &args1); |
| 704 |
|
| 705 |
args2.key = dns_resource_key_new(DNS_CLASS_IN, DNS_TYPE_A, "www.example.com"); |
| 706 |
ASSERT_NOT_NULL(args2.key); |
| 707 |
args2.rcode = DNS_RCODE_SUCCESS; |
| 708 |
answer_add_a(&args2, args2.key, 0x7f01a8c0, 2400, DNS_ANSWER_CACHEABLE); |
| 709 |
cache_put(&cache, &args2); |
| 710 |
|
| 711 |
ASSERT_EQ(dns_cache_size(&cache), 1u); |
| 712 |
|
| 713 |
key = dns_resource_key_new(DNS_CLASS_IN, DNS_TYPE_A, "www.example.com"); |
| 714 |
ASSERT_NOT_NULL(key); |
| 715 |
query_flags = 0; |
| 716 |
ASSERT_OK_POSITIVE(dns_cache_lookup(&cache, key, query_flags, &ret_rcode, &ret_answer, &ret_full_packet, &ret_query_flags, NULL)); |
| 717 |
|
| 718 |
ASSERT_EQ(cache.n_hit, 1u); |
| 719 |
ASSERT_EQ(cache.n_miss, 0u); |
| 720 |
|
| 721 |
ASSERT_EQ(ret_rcode, DNS_RCODE_SUCCESS); |
| 722 |
ASSERT_EQ(ret_query_flags, SD_RESOLVED_CONFIDENTIAL); |
| 723 |
|
| 724 |
ASSERT_EQ(dns_answer_size(ret_answer), 1u); |
| 725 |
|
| 726 |
rr = dns_resource_record_new_full(DNS_CLASS_IN, DNS_TYPE_A, "www.example.com"); |
| 727 |
ASSERT_NOT_NULL(rr); |
| 728 |
rr->a.in_addr.s_addr = htobe32(0x7f01a8c0); |
| 729 |
ASSERT_TRUE(dns_answer_contains(ret_answer, rr)); |
| 730 |
dns_resource_record_unref(rr); |
| 731 |
|
| 732 |
rr = dns_resource_record_new_full(DNS_CLASS_IN, DNS_TYPE_A, "www.example.com"); |
| 733 |
ASSERT_NOT_NULL(rr); |
| 734 |
rr->a.in_addr.s_addr = htobe32(0xc0a8017f); |
| 735 |
ASSERT_FALSE(dns_answer_contains(ret_answer, rr)); |
| 736 |
dns_resource_record_unref(rr); |
| 737 |
} |
| 738 |
|
| 739 |
TEST(dns_cache_lookup_retains_multiple_answers_from_one_response) { |
| 740 |
_cleanup_(dns_cache_unrefp) DnsCache cache = new_cache(); |
| 741 |
_cleanup_(put_args_unrefp) PutArgs put_args = mk_put_args(); |
| 742 |
_cleanup_(dns_answer_unrefp) DnsAnswer *ret_answer = NULL; |
| 743 |
_cleanup_(dns_packet_unrefp) DnsPacket *ret_full_packet = NULL; |
| 744 |
_cleanup_(dns_resource_key_unrefp) DnsResourceKey *key = NULL; |
| 745 |
DnsResourceRecord *rr = NULL; |
| 746 |
int query_flags, ret_rcode; |
| 747 |
uint64_t ret_query_flags; |
| 748 |
|
| 749 |
put_args.key = dns_resource_key_new(DNS_CLASS_IN, DNS_TYPE_A, "www.example.com"); |
| 750 |
ASSERT_NOT_NULL(put_args.key); |
| 751 |
put_args.rcode = DNS_RCODE_SUCCESS; |
| 752 |
answer_add_a(&put_args, put_args.key, 0xc0a8017f, 3600, DNS_ANSWER_CACHEABLE); |
| 753 |
answer_add_a(&put_args, put_args.key, 0x7f01a8cc, 3600, DNS_ANSWER_CACHEABLE); |
| 754 |
cache_put(&cache, &put_args); |
| 755 |
|
| 756 |
ASSERT_EQ(dns_cache_size(&cache), 1u); |
| 757 |
|
| 758 |
key = dns_resource_key_new(DNS_CLASS_IN, DNS_TYPE_A, "www.example.com"); |
| 759 |
ASSERT_NOT_NULL(key); |
| 760 |
query_flags = 0; |
| 761 |
ASSERT_OK_POSITIVE(dns_cache_lookup(&cache, key, query_flags, &ret_rcode, &ret_answer, &ret_full_packet, &ret_query_flags, NULL)); |
| 762 |
|
| 763 |
ASSERT_EQ(cache.n_hit, 1u); |
| 764 |
ASSERT_EQ(cache.n_miss, 0u); |
| 765 |
|
| 766 |
ASSERT_EQ(ret_rcode, DNS_RCODE_SUCCESS); |
| 767 |
ASSERT_EQ(ret_query_flags, SD_RESOLVED_CONFIDENTIAL); |
| 768 |
|
| 769 |
ASSERT_EQ(dns_answer_size(ret_answer), 2u); |
| 770 |
|
| 771 |
rr = dns_resource_record_new_full(DNS_CLASS_IN, DNS_TYPE_A, "www.example.com"); |
| 772 |
ASSERT_NOT_NULL(rr); |
| 773 |
rr->a.in_addr.s_addr = htobe32(0x7f01a8cc); |
| 774 |
ASSERT_TRUE(dns_answer_contains(ret_answer, rr)); |
| 775 |
dns_resource_record_unref(rr); |
| 776 |
|
| 777 |
rr = dns_resource_record_new_full(DNS_CLASS_IN, DNS_TYPE_A, "www.example.com"); |
| 778 |
ASSERT_NOT_NULL(rr); |
| 779 |
rr->a.in_addr.s_addr = htobe32(0xc0a8017f); |
| 780 |
ASSERT_TRUE(dns_answer_contains(ret_answer, rr)); |
| 781 |
dns_resource_record_unref(rr); |
| 782 |
} |
| 783 |
|
| 784 |
TEST(dns_cache_lookup_nxdomain) { |
| 785 |
_cleanup_(dns_cache_unrefp) DnsCache cache = new_cache(); |
| 786 |
_cleanup_(put_args_unrefp) PutArgs put_args = mk_put_args(); |
| 787 |
_cleanup_(dns_answer_unrefp) DnsAnswer *ret_answer = NULL; |
| 788 |
_cleanup_(dns_packet_unrefp) DnsPacket *ret_full_packet = NULL; |
| 789 |
_cleanup_(dns_resource_record_unrefp) DnsResourceRecord *rr = NULL; |
| 790 |
_cleanup_(dns_resource_key_unrefp) DnsResourceKey *key = NULL; |
| 791 |
int query_flags, ret_rcode; |
| 792 |
uint64_t ret_query_flags; |
| 793 |
|
| 794 |
put_args.key = dns_resource_key_new(DNS_CLASS_IN, DNS_TYPE_A, "www.example.com"); |
| 795 |
ASSERT_NOT_NULL(put_args.key); |
| 796 |
put_args.rcode = DNS_RCODE_NXDOMAIN; |
| 797 |
dns_answer_add_soa(put_args.answer, "example.com", 3600, 0); |
| 798 |
cache_put(&cache, &put_args); |
| 799 |
|
| 800 |
ASSERT_EQ(dns_cache_size(&cache), 1u); |
| 801 |
|
| 802 |
key = dns_resource_key_new(DNS_CLASS_IN, DNS_TYPE_A, "www.example.com"); |
| 803 |
ASSERT_NOT_NULL(key); |
| 804 |
query_flags = 0; |
| 805 |
ASSERT_OK_POSITIVE(dns_cache_lookup(&cache, key, query_flags, &ret_rcode, &ret_answer, &ret_full_packet, &ret_query_flags, NULL)); |
| 806 |
|
| 807 |
ASSERT_EQ(cache.n_hit, 1u); |
| 808 |
ASSERT_EQ(cache.n_miss, 0u); |
| 809 |
|
| 810 |
ASSERT_EQ(ret_rcode, DNS_RCODE_NXDOMAIN); |
| 811 |
ASSERT_EQ(ret_query_flags, (SD_RESOLVED_AUTHENTICATED | SD_RESOLVED_CONFIDENTIAL)); |
| 812 |
|
| 813 |
ASSERT_EQ(dns_answer_size(ret_answer), 1u); |
| 814 |
|
| 815 |
rr = dns_resource_record_new_full(DNS_CLASS_IN, DNS_TYPE_SOA, "example.com"); |
| 816 |
ASSERT_NOT_NULL(rr); |
| 817 |
rr->soa.mname = checked_strdup("example.com"); |
| 818 |
rr->soa.rname = checked_strdup("root.example.com"); |
| 819 |
rr->soa.serial = 1; |
| 820 |
rr->soa.refresh = 1; |
| 821 |
rr->soa.retry = 1; |
| 822 |
rr->soa.expire = 1; |
| 823 |
rr->soa.minimum = 3600; |
| 824 |
ASSERT_TRUE(dns_answer_contains(ret_answer, rr)); |
| 825 |
} |
| 826 |
|
| 827 |
TEST(dns_cache_lookup_any_always_misses) { |
| 828 |
_cleanup_(dns_cache_unrefp) DnsCache cache = new_cache(); |
| 829 |
_cleanup_(put_args_unrefp) PutArgs put_args = mk_put_args(); |
| 830 |
_cleanup_(dns_answer_unrefp) DnsAnswer *ret_answer = NULL; |
| 831 |
_cleanup_(dns_packet_unrefp) DnsPacket *ret_full_packet = NULL; |
| 832 |
_cleanup_(dns_resource_key_unrefp) DnsResourceKey *key = NULL; |
| 833 |
int query_flags, ret_rcode; |
| 834 |
uint64_t ret_query_flags; |
| 835 |
|
| 836 |
put_args.key = dns_resource_key_new(DNS_CLASS_IN, DNS_TYPE_A, "www.example.com"); |
| 837 |
ASSERT_NOT_NULL(put_args.key); |
| 838 |
put_args.rcode = DNS_RCODE_SUCCESS; |
| 839 |
answer_add_a(&put_args, put_args.key, 0xc0a8017f, 3600, DNS_ANSWER_CACHEABLE); |
| 840 |
cache_put(&cache, &put_args); |
| 841 |
|
| 842 |
ASSERT_EQ(dns_cache_size(&cache), 1u); |
| 843 |
|
| 844 |
key = dns_resource_key_new(DNS_CLASS_IN, DNS_TYPE_ANY, "www.example.com"); |
| 845 |
ASSERT_NOT_NULL(key); |
| 846 |
query_flags = 0; |
| 847 |
ASSERT_FALSE(dns_cache_lookup(&cache, key, query_flags, &ret_rcode, &ret_answer, &ret_full_packet, &ret_query_flags, NULL)); |
| 848 |
|
| 849 |
ASSERT_EQ(cache.n_hit, 0u); |
| 850 |
ASSERT_EQ(cache.n_miss, 1u); |
| 851 |
|
| 852 |
ASSERT_EQ(ret_rcode, DNS_RCODE_SUCCESS); |
| 853 |
ASSERT_EQ(ret_query_flags, 0u); |
| 854 |
|
| 855 |
ASSERT_EQ(dns_answer_size(ret_answer), 0u); |
| 856 |
} |
| 857 |
|
| 858 |
TEST(dns_cache_lookup_mdns_multiple_shared_responses_are_cached) { |
| 859 |
_cleanup_(dns_cache_unrefp) DnsCache cache = new_cache(); |
| 860 |
_cleanup_(put_args_unrefp) PutArgs args1 = mk_put_args(), args2 = mk_put_args(); |
| 861 |
_cleanup_(dns_answer_unrefp) DnsAnswer *ret_answer = NULL; |
| 862 |
_cleanup_(dns_packet_unrefp) DnsPacket *ret_full_packet = NULL; |
| 863 |
DnsResourceKey *key = NULL; |
| 864 |
DnsResourceRecord *rr = NULL; |
| 865 |
int query_flags, ret_rcode; |
| 866 |
uint64_t ret_query_flags; |
| 867 |
|
| 868 |
args1.protocol = DNS_PROTOCOL_MDNS; |
| 869 |
args1.rcode = DNS_RCODE_SUCCESS; |
| 870 |
key = dns_resource_key_new(DNS_CLASS_IN, DNS_TYPE_A, "example.com"); |
| 871 |
ASSERT_NOT_NULL(key); |
| 872 |
answer_add_a(&args1, key, 0xc0a8017f, 3600, DNS_ANSWER_CACHEABLE | DNS_ANSWER_SHARED_OWNER); |
| 873 |
ASSERT_OK(cache_put(&cache, &args1)); |
| 874 |
dns_resource_key_unref(key); |
| 875 |
|
| 876 |
args2.protocol = DNS_PROTOCOL_MDNS; |
| 877 |
args2.rcode = DNS_RCODE_SUCCESS; |
| 878 |
key = dns_resource_key_new(DNS_CLASS_IN, DNS_TYPE_A, "example.com"); |
| 879 |
ASSERT_NOT_NULL(key); |
| 880 |
answer_add_a(&args2, key, 0x7f01a8cc, 3600, DNS_ANSWER_CACHEABLE | DNS_ANSWER_SHARED_OWNER); |
| 881 |
ASSERT_OK(cache_put(&cache, &args2)); |
| 882 |
dns_resource_key_unref(key); |
| 883 |
|
| 884 |
ASSERT_FALSE(dns_cache_is_empty(&cache)); |
| 885 |
ASSERT_EQ(dns_cache_size(&cache), 1u); |
| 886 |
|
| 887 |
key = dns_resource_key_new(DNS_CLASS_IN, DNS_TYPE_A, "example.com"); |
| 888 |
ASSERT_NOT_NULL(key); |
| 889 |
query_flags = 0; |
| 890 |
ASSERT_OK_POSITIVE(dns_cache_lookup(&cache, key, query_flags, &ret_rcode, &ret_answer, &ret_full_packet, &ret_query_flags, NULL)); |
| 891 |
dns_resource_key_unref(key); |
| 892 |
|
| 893 |
ASSERT_EQ(cache.n_hit, 1u); |
| 894 |
ASSERT_EQ(cache.n_miss, 0u); |
| 895 |
|
| 896 |
ASSERT_EQ(ret_rcode, DNS_RCODE_SUCCESS); |
| 897 |
ASSERT_EQ(ret_query_flags, SD_RESOLVED_CONFIDENTIAL); |
| 898 |
|
| 899 |
ASSERT_EQ(dns_answer_size(ret_answer), 2u); |
| 900 |
|
| 901 |
rr = dns_resource_record_new_full(DNS_CLASS_IN, DNS_TYPE_A, "example.com"); |
| 902 |
ASSERT_NOT_NULL(rr); |
| 903 |
rr->a.in_addr.s_addr = htobe32(0xc0a8017f); |
| 904 |
ASSERT_TRUE(dns_answer_contains(ret_answer, rr)); |
| 905 |
dns_resource_record_unref(rr); |
| 906 |
|
| 907 |
rr = dns_resource_record_new_full(DNS_CLASS_IN, DNS_TYPE_A, "example.com"); |
| 908 |
ASSERT_NOT_NULL(rr); |
| 909 |
rr->a.in_addr.s_addr = htobe32(0x7f01a8cc); |
| 910 |
ASSERT_TRUE(dns_answer_contains(ret_answer, rr)); |
| 911 |
dns_resource_record_unref(rr); |
| 912 |
} |
| 913 |
|
| 914 |
TEST(dns_cache_lookup_mdns_multiple_unshared_responses_are_not_cached) { |
| 915 |
_cleanup_(dns_cache_unrefp) DnsCache cache = new_cache(); |
| 916 |
_cleanup_(put_args_unrefp) PutArgs args1 = mk_put_args(), args2 = mk_put_args(); |
| 917 |
_cleanup_(dns_answer_unrefp) DnsAnswer *ret_answer = NULL; |
| 918 |
_cleanup_(dns_packet_unrefp) DnsPacket *ret_full_packet = NULL; |
| 919 |
DnsResourceKey *key = NULL; |
| 920 |
DnsResourceRecord *rr = NULL; |
| 921 |
int query_flags, ret_rcode; |
| 922 |
uint64_t ret_query_flags; |
| 923 |
|
| 924 |
args1.protocol = DNS_PROTOCOL_MDNS; |
| 925 |
args1.rcode = DNS_RCODE_SUCCESS; |
| 926 |
key = dns_resource_key_new(DNS_CLASS_IN, DNS_TYPE_A, "example.com"); |
| 927 |
ASSERT_NOT_NULL(key); |
| 928 |
answer_add_a(&args1, key, 0xc0a8017f, 3600, DNS_ANSWER_CACHEABLE); |
| 929 |
ASSERT_OK(cache_put(&cache, &args1)); |
| 930 |
dns_resource_key_unref(key); |
| 931 |
|
| 932 |
args2.protocol = DNS_PROTOCOL_MDNS; |
| 933 |
args2.rcode = DNS_RCODE_SUCCESS; |
| 934 |
key = dns_resource_key_new(DNS_CLASS_IN, DNS_TYPE_A, "example.com"); |
| 935 |
ASSERT_NOT_NULL(key); |
| 936 |
answer_add_a(&args2, key, 0x7f01a8cc, 3600, DNS_ANSWER_CACHEABLE); |
| 937 |
ASSERT_OK(cache_put(&cache, &args2)); |
| 938 |
dns_resource_key_unref(key); |
| 939 |
|
| 940 |
ASSERT_FALSE(dns_cache_is_empty(&cache)); |
| 941 |
ASSERT_EQ(dns_cache_size(&cache), 1u); |
| 942 |
|
| 943 |
key = dns_resource_key_new(DNS_CLASS_IN, DNS_TYPE_A, "example.com"); |
| 944 |
ASSERT_NOT_NULL(key); |
| 945 |
query_flags = 0; |
| 946 |
ASSERT_OK_POSITIVE(dns_cache_lookup(&cache, key, query_flags, &ret_rcode, &ret_answer, &ret_full_packet, &ret_query_flags, NULL)); |
| 947 |
dns_resource_key_unref(key); |
| 948 |
|
| 949 |
ASSERT_EQ(cache.n_hit, 1u); |
| 950 |
ASSERT_EQ(cache.n_miss, 0u); |
| 951 |
|
| 952 |
ASSERT_EQ(ret_rcode, DNS_RCODE_SUCCESS); |
| 953 |
ASSERT_EQ(ret_query_flags, SD_RESOLVED_CONFIDENTIAL); |
| 954 |
|
| 955 |
ASSERT_EQ(dns_answer_size(ret_answer), 1u); |
| 956 |
|
| 957 |
rr = dns_resource_record_new_full(DNS_CLASS_IN, DNS_TYPE_A, "example.com"); |
| 958 |
ASSERT_NOT_NULL(rr); |
| 959 |
rr->a.in_addr.s_addr = htobe32(0xc0a8017f); |
| 960 |
ASSERT_FALSE(dns_answer_contains(ret_answer, rr)); |
| 961 |
dns_resource_record_unref(rr); |
| 962 |
|
| 963 |
rr = dns_resource_record_new_full(DNS_CLASS_IN, DNS_TYPE_A, "example.com"); |
| 964 |
ASSERT_NOT_NULL(rr); |
| 965 |
rr->a.in_addr.s_addr = htobe32(0x7f01a8cc); |
| 966 |
ASSERT_TRUE(dns_answer_contains(ret_answer, rr)); |
| 967 |
dns_resource_record_unref(rr); |
| 968 |
} |
| 969 |
|
| 970 |
/* ================================================================ |
| 971 |
* dns_cache_prune(), dns_cache_expiry_in_one_second() |
| 972 |
* ================================================================ */ |
| 973 |
|
| 974 |
TEST(dns_cache_prune) { |
| 975 |
_cleanup_(dns_cache_unrefp) DnsCache cache = new_cache(); |
| 976 |
_cleanup_(put_args_unrefp) PutArgs put_args = mk_put_args(); |
| 977 |
DnsResourceKey *key = NULL; |
| 978 |
|
| 979 |
key = dns_resource_key_new(DNS_CLASS_IN, DNS_TYPE_A, "ns1.example.com"); |
| 980 |
ASSERT_NOT_NULL(key); |
| 981 |
answer_add_a(&put_args, key, 0xc0a8017f, 1, DNS_ANSWER_CACHEABLE); |
| 982 |
dns_resource_key_unref(key); |
| 983 |
|
| 984 |
key = dns_resource_key_new(DNS_CLASS_IN, DNS_TYPE_A, "ns2.example.com"); |
| 985 |
ASSERT_NOT_NULL(key); |
| 986 |
answer_add_a(&put_args, key, 0x7f01a8cc, 3, DNS_ANSWER_CACHEABLE); |
| 987 |
dns_resource_key_unref(key); |
| 988 |
|
| 989 |
cache_put(&cache, &put_args); |
| 990 |
|
| 991 |
dns_cache_prune(&cache); |
| 992 |
ASSERT_EQ(dns_cache_size(&cache), 2u); |
| 993 |
ASSERT_TRUE(dns_cache_expiry_in_one_second(&cache, now(CLOCK_BOOTTIME))); |
| 994 |
|
| 995 |
sleep(2); |
| 996 |
|
| 997 |
dns_cache_prune(&cache); |
| 998 |
ASSERT_EQ(dns_cache_size(&cache), 1u); |
| 999 |
ASSERT_TRUE(dns_cache_expiry_in_one_second(&cache, now(CLOCK_BOOTTIME))); |
| 1000 |
|
| 1001 |
sleep(2); |
| 1002 |
|
| 1003 |
dns_cache_prune(&cache); |
| 1004 |
ASSERT_TRUE(dns_cache_is_empty(&cache)); |
| 1005 |
ASSERT_FALSE(dns_cache_expiry_in_one_second(&cache, now(CLOCK_BOOTTIME))); |
| 1006 |
} |
| 1007 |
|
| 1008 |
/* ================================================================ |
| 1009 |
* dns_cache_check_conflicts() |
| 1010 |
* ================================================================ */ |
| 1011 |
|
| 1012 |
TEST(dns_cache_check_conflicts_same_key_and_owner) { |
| 1013 |
_cleanup_(dns_cache_unrefp) DnsCache cache = new_cache(); |
| 1014 |
_cleanup_(put_args_unrefp) PutArgs put_args = mk_put_args(); |
| 1015 |
_cleanup_(dns_resource_record_unrefp) DnsResourceRecord *rr = NULL; |
| 1016 |
|
| 1017 |
put_args.key = dns_resource_key_new(DNS_CLASS_IN, DNS_TYPE_A, "ns1.example.com"); |
| 1018 |
ASSERT_NOT_NULL(put_args.key); |
| 1019 |
answer_add_a(&put_args, put_args.key, 0xc0a8017f, 3600, DNS_ANSWER_CACHEABLE); |
| 1020 |
cache_put(&cache, &put_args); |
| 1021 |
|
| 1022 |
union in_addr_union owner_addr = { .in.s_addr = htobe32(0x01020304) }; |
| 1023 |
|
| 1024 |
rr = dns_resource_record_new_full(DNS_CLASS_IN, DNS_TYPE_A, "ns1.example.com"); |
| 1025 |
ASSERT_NOT_NULL(rr); |
| 1026 |
ASSERT_FALSE(dns_cache_check_conflicts(&cache, rr, AF_INET, &owner_addr)); |
| 1027 |
} |
| 1028 |
|
| 1029 |
TEST(dns_cache_check_conflicts_same_key_different_owner) { |
| 1030 |
_cleanup_(dns_cache_unrefp) DnsCache cache = new_cache(); |
| 1031 |
_cleanup_(put_args_unrefp) PutArgs put_args = mk_put_args(); |
| 1032 |
_cleanup_(dns_resource_record_unrefp) DnsResourceRecord *rr = NULL; |
| 1033 |
|
| 1034 |
put_args.key = dns_resource_key_new(DNS_CLASS_IN, DNS_TYPE_A, "ns1.example.com"); |
| 1035 |
ASSERT_NOT_NULL(put_args.key); |
| 1036 |
answer_add_a(&put_args, put_args.key, 0xc0a8017f, 3600, DNS_ANSWER_CACHEABLE); |
| 1037 |
cache_put(&cache, &put_args); |
| 1038 |
|
| 1039 |
union in_addr_union owner_addr = { .in.s_addr = htobe32(0x01020305) }; |
| 1040 |
|
| 1041 |
rr = dns_resource_record_new_full(DNS_CLASS_IN, DNS_TYPE_A, "ns1.example.com"); |
| 1042 |
ASSERT_NOT_NULL(rr); |
| 1043 |
ASSERT_TRUE(dns_cache_check_conflicts(&cache, rr, AF_INET, &owner_addr)); |
| 1044 |
} |
| 1045 |
|
| 1046 |
TEST(dns_cache_check_conflicts_different_key) { |
| 1047 |
_cleanup_(dns_cache_unrefp) DnsCache cache = new_cache(); |
| 1048 |
_cleanup_(put_args_unrefp) PutArgs put_args = mk_put_args(); |
| 1049 |
_cleanup_(dns_resource_record_unrefp) DnsResourceRecord *rr = NULL; |
| 1050 |
|
| 1051 |
put_args.key = dns_resource_key_new(DNS_CLASS_IN, DNS_TYPE_A, "ns2.example.com"); |
| 1052 |
ASSERT_NOT_NULL(put_args.key); |
| 1053 |
answer_add_a(&put_args, put_args.key, 0xc0a8017f, 3600, DNS_ANSWER_CACHEABLE); |
| 1054 |
cache_put(&cache, &put_args); |
| 1055 |
|
| 1056 |
union in_addr_union owner_addr = { .in.s_addr = htobe32(0x01020305) }; |
| 1057 |
|
| 1058 |
rr = dns_resource_record_new_full(DNS_CLASS_IN, DNS_TYPE_A, "ns1.example.com"); |
| 1059 |
ASSERT_NOT_NULL(rr); |
| 1060 |
ASSERT_FALSE(dns_cache_check_conflicts(&cache, rr, AF_INET, &owner_addr)); |
| 1061 |
} |
| 1062 |
|
| 1063 |
/* ================================================================ |
| 1064 |
* dns_cache_export_shared_to_packet() |
| 1065 |
* ================================================================ */ |
| 1066 |
|
| 1067 |
TEST(dns_cache_export_shared_to_packet) { |
| 1068 |
_cleanup_(dns_cache_unrefp) DnsCache cache = new_cache(); |
| 1069 |
_cleanup_(put_args_unrefp) PutArgs args1 = mk_put_args(), args2 = mk_put_args(); |
| 1070 |
_cleanup_(dns_packet_unrefp) DnsPacket *packet = NULL; |
| 1071 |
DnsResourceKey *key = NULL; |
| 1072 |
|
| 1073 |
args1.protocol = DNS_PROTOCOL_MDNS; |
| 1074 |
args1.rcode = DNS_RCODE_SUCCESS; |
| 1075 |
|
| 1076 |
key = dns_resource_key_new(DNS_CLASS_IN, DNS_TYPE_A, "shared.example.com"); |
| 1077 |
ASSERT_NOT_NULL(key); |
| 1078 |
answer_add_a(&args1, key, 0xc0a8017f, 3600, DNS_ANSWER_CACHEABLE | DNS_ANSWER_SHARED_OWNER); |
| 1079 |
dns_resource_key_unref(key); |
| 1080 |
|
| 1081 |
key = dns_resource_key_new(DNS_CLASS_IN, DNS_TYPE_A, "unshared.example.com"); |
| 1082 |
ASSERT_NOT_NULL(key); |
| 1083 |
answer_add_a(&args1, key, 0xa87f01c0, 2400, DNS_ANSWER_CACHEABLE); |
| 1084 |
dns_resource_key_unref(key); |
| 1085 |
|
| 1086 |
cache_put(&cache, &args1); |
| 1087 |
|
| 1088 |
args2.protocol = DNS_PROTOCOL_DNS; |
| 1089 |
args2.key = dns_resource_key_new(DNS_CLASS_IN, DNS_TYPE_A, "dns.example.com"); |
| 1090 |
ASSERT_NOT_NULL(args2.key); |
| 1091 |
args2.rcode = DNS_RCODE_SUCCESS; |
| 1092 |
answer_add_a(&args2, args2.key, 0xa9fe0100, 2400, DNS_ANSWER_CACHEABLE); |
| 1093 |
cache_put(&cache, &args2); |
| 1094 |
|
| 1095 |
dns_packet_new(&packet, DNS_PROTOCOL_MDNS, 0, DNS_PACKET_SIZE_MAX); |
| 1096 |
ASSERT_NOT_NULL(packet); |
| 1097 |
ASSERT_OK(dns_cache_export_shared_to_packet(&cache, packet, 0, 50)); |
| 1098 |
|
| 1099 |
const uint8_t data[] = { |
| 1100 |
0x00, 0x00, 0x00, 0x00, |
| 1101 |
0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, |
| 1102 |
|
| 1103 |
/* name */ 0x06, 's', 'h', 'a', 'r', 'e', 'd', |
| 1104 |
0x07, 'e', 'x', 'a', 'm', 'p', 'l', 'e', |
| 1105 |
0x03, 'c', 'o', 'm', |
| 1106 |
0x00, |
| 1107 |
/* A */ 0x00, 0x01, |
| 1108 |
/* IN */ 0x00, 0x01, |
| 1109 |
/* ttl */ 0x00, 0x00, 0x0e, 0x10, |
| 1110 |
/* rdata */ 0x00, 0x04, |
| 1111 |
/* ip */ 0xc0, 0xa8, 0x01, 0x7f |
| 1112 |
}; |
| 1113 |
|
| 1114 |
ASSERT_EQ(packet->size, sizeof(data)); |
| 1115 |
ASSERT_EQ(memcmp(DNS_PACKET_DATA(packet), data, sizeof(data)), 0); |
| 1116 |
} |
| 1117 |
|
| 1118 |
TEST(dns_cache_export_shared_to_packet_multi) { |
| 1119 |
_cleanup_(dns_cache_unrefp) DnsCache cache = new_cache(); |
| 1120 |
_cleanup_(put_args_unrefp) PutArgs put_args = mk_put_args(); |
| 1121 |
_cleanup_(dns_packet_unrefp) DnsPacket *packet = NULL; |
| 1122 |
DnsResourceKey *key = NULL; |
| 1123 |
|
| 1124 |
put_args.protocol = DNS_PROTOCOL_MDNS; |
| 1125 |
put_args.rcode = DNS_RCODE_SUCCESS; |
| 1126 |
|
| 1127 |
key = dns_resource_key_new(DNS_CLASS_IN, DNS_TYPE_A, "shared1.example.com"); |
| 1128 |
ASSERT_NOT_NULL(key); |
| 1129 |
answer_add_a(&put_args, key, 0xc0a8017f, 3600, DNS_ANSWER_CACHEABLE | DNS_ANSWER_SHARED_OWNER); |
| 1130 |
dns_resource_key_unref(key); |
| 1131 |
|
| 1132 |
key = dns_resource_key_new(DNS_CLASS_IN, DNS_TYPE_A, "unshared.example.com"); |
| 1133 |
ASSERT_NOT_NULL(key); |
| 1134 |
answer_add_a(&put_args, key, 0xa87f01c0, 2400, DNS_ANSWER_CACHEABLE); |
| 1135 |
dns_resource_key_unref(key); |
| 1136 |
|
| 1137 |
key = dns_resource_key_new(DNS_CLASS_IN, DNS_TYPE_A, "shared2.example.com"); |
| 1138 |
ASSERT_NOT_NULL(key); |
| 1139 |
answer_add_a(&put_args, key, 0x7f01a8cc, 1800, DNS_ANSWER_CACHEABLE | DNS_ANSWER_SHARED_OWNER); |
| 1140 |
dns_resource_key_unref(key); |
| 1141 |
|
| 1142 |
cache_put(&cache, &put_args); |
| 1143 |
|
| 1144 |
dns_packet_new(&packet, DNS_PROTOCOL_MDNS, 0, DNS_PACKET_SIZE_MAX); |
| 1145 |
ASSERT_NOT_NULL(packet); |
| 1146 |
ASSERT_OK(dns_cache_export_shared_to_packet(&cache, packet, 0, 1)); |
| 1147 |
|
| 1148 |
const uint8_t data1[] = { |
| 1149 |
0x00, 0x00, 0x00, 0x00, |
| 1150 |
0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, |
| 1151 |
|
| 1152 |
/* name */ 0x07, 's', 'h', 'a', 'r', 'e', 'd', '1', |
| 1153 |
0x07, 'e', 'x', 'a', 'm', 'p', 'l', 'e', |
| 1154 |
0x03, 'c', 'o', 'm', |
| 1155 |
0x00, |
| 1156 |
/* A */ 0x00, 0x01, |
| 1157 |
/* IN */ 0x00, 0x01, |
| 1158 |
/* ttl */ 0x00, 0x00, 0x0e, 0x10, |
| 1159 |
/* rdata */ 0x00, 0x04, |
| 1160 |
/* ip */ 0xc0, 0xa8, 0x01, 0x7f |
| 1161 |
}; |
| 1162 |
|
| 1163 |
const uint8_t data2[] = { |
| 1164 |
0x00, 0x00, 0x00, 0x00, |
| 1165 |
0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, |
| 1166 |
|
| 1167 |
/* name */ 0x07, 's', 'h', 'a', 'r', 'e', 'd', '2', |
| 1168 |
0x07, 'e', 'x', 'a', 'm', 'p', 'l', 'e', |
| 1169 |
0x03, 'c', 'o', 'm', |
| 1170 |
0x00, |
| 1171 |
/* A */ 0x00, 0x01, |
| 1172 |
/* IN */ 0x00, 0x01, |
| 1173 |
/* ttl */ 0x00, 0x00, 0x07, 0x08, |
| 1174 |
/* rdata */ 0x00, 0x04, |
| 1175 |
/* ip */ 0x7f, 0x01, 0xa8, 0xcc |
| 1176 |
}; |
| 1177 |
|
| 1178 |
size_t size1 = sizeof(data1), size2 = sizeof(data2); |
| 1179 |
|
| 1180 |
/* cache key order is not deterministic; the packets could come out in either order */ |
| 1181 |
|
| 1182 |
if (memcmp(DNS_PACKET_DATA(packet), data1, size1) == 0) { |
| 1183 |
ASSERT_EQ(packet->size, size1); |
| 1184 |
ASSERT_EQ(memcmp(DNS_PACKET_DATA(packet), data1, size1), 0); |
| 1185 |
|
| 1186 |
ASSERT_EQ(packet->more->size, size2); |
| 1187 |
ASSERT_EQ(memcmp(DNS_PACKET_DATA(packet->more), data2, size2), 0); |
| 1188 |
} else { |
| 1189 |
ASSERT_EQ(packet->size, size2); |
| 1190 |
ASSERT_EQ(memcmp(DNS_PACKET_DATA(packet), data2, size2), 0); |
| 1191 |
|
| 1192 |
ASSERT_EQ(packet->more->size, size1); |
| 1193 |
ASSERT_EQ(memcmp(DNS_PACKET_DATA(packet->more), data1, size1), 0); |
| 1194 |
} |
| 1195 |
|
| 1196 |
ASSERT_NULL(packet->more->more); |
| 1197 |
} |
| 1198 |
|
| 1199 |
/* ================================================================ |
| 1200 |
* dns_cache_dump() |
| 1201 |
* ================================================================ */ |
| 1202 |
|
| 1203 |
static int cmpstring(const void *a, const void *b) { |
| 1204 |
ASSERT_NOT_NULL(a); |
| 1205 |
ASSERT_NOT_NULL(b); |
| 1206 |
|
| 1207 |
return strcmp(*(const char **)a, *(const char **)b); |
| 1208 |
} |
| 1209 |
|
| 1210 |
static void check_dump_contents(FILE *f, const char **expected, size_t n) { |
| 1211 |
char *actual[n]; |
| 1212 |
rewind(f); |
| 1213 |
|
| 1214 |
for (size_t i = 0; i < n; i++) { |
| 1215 |
size_t length = read_line(f, 1024, &actual[i]); |
| 1216 |
ASSERT_GT(length, 0u); |
| 1217 |
} |
| 1218 |
|
| 1219 |
qsort(actual, n, sizeof(char *), cmpstring); |
| 1220 |
|
| 1221 |
for (size_t i = 0; i < n; i++) |
| 1222 |
ASSERT_STREQ(actual[i], expected[i]); |
| 1223 |
|
| 1224 |
for (size_t i = 0; i < n; i++) |
| 1225 |
free(actual[i]); |
| 1226 |
} |
| 1227 |
|
| 1228 |
TEST(dns_cache_dump_single_a) { |
| 1229 |
_cleanup_(dns_cache_unrefp) DnsCache cache = new_cache(); |
| 1230 |
_cleanup_(put_args_unrefp) PutArgs put_args = mk_put_args(); |
| 1231 |
|
| 1232 |
put_args.key = dns_resource_key_new(DNS_CLASS_IN, DNS_TYPE_A, "www.example.com"); |
| 1233 |
ASSERT_NOT_NULL(put_args.key); |
| 1234 |
put_args.rcode = DNS_RCODE_SUCCESS; |
| 1235 |
answer_add_a(&put_args, put_args.key, 0xc0a8017f, 3600, DNS_ANSWER_CACHEABLE); |
| 1236 |
cache_put(&cache, &put_args); |
| 1237 |
|
| 1238 |
ASSERT_EQ(dns_cache_size(&cache), 1u); |
| 1239 |
|
| 1240 |
_cleanup_(unlink_tempfilep) char p[] = "/tmp/dns-cache-dump-single-a-XXXXXX"; |
| 1241 |
_cleanup_fclose_ FILE *f = NULL; |
| 1242 |
fmkostemp_safe(p, "r+", &f); |
| 1243 |
dns_cache_dump(&cache, f); |
| 1244 |
|
| 1245 |
const char *expected[] = { |
| 1246 |
"\twww.example.com IN A 192.168.1.127" |
| 1247 |
}; |
| 1248 |
check_dump_contents(f, expected, 1); |
| 1249 |
} |
| 1250 |
|
| 1251 |
TEST(dns_cache_dump_a_with_cname) { |
| 1252 |
_cleanup_(dns_cache_unrefp) DnsCache cache = new_cache(); |
| 1253 |
_cleanup_(put_args_unrefp) PutArgs put_args = mk_put_args(); |
| 1254 |
_cleanup_(dns_resource_key_unrefp) DnsResourceKey *key = NULL; |
| 1255 |
|
| 1256 |
put_args.key = dns_resource_key_new(DNS_CLASS_IN, DNS_TYPE_A, "www.example.com"); |
| 1257 |
ASSERT_NOT_NULL(put_args.key); |
| 1258 |
put_args.rcode = DNS_RCODE_SUCCESS; |
| 1259 |
|
| 1260 |
key = dns_resource_key_new(DNS_CLASS_IN, DNS_TYPE_CNAME, "www.example.com"); |
| 1261 |
ASSERT_NOT_NULL(key); |
| 1262 |
answer_add_cname(&put_args, key, "example.com", 3600, DNS_ANSWER_CACHEABLE); |
| 1263 |
|
| 1264 |
dns_resource_key_unref(key); |
| 1265 |
key = dns_resource_key_new(DNS_CLASS_IN, DNS_TYPE_A, "example.com"); |
| 1266 |
ASSERT_NOT_NULL(key); |
| 1267 |
answer_add_a(&put_args, key, 0xc0a8017f, 3600, DNS_ANSWER_CACHEABLE); |
| 1268 |
|
| 1269 |
cache_put(&cache, &put_args); |
| 1270 |
|
| 1271 |
ASSERT_EQ(dns_cache_size(&cache), 2u); |
| 1272 |
|
| 1273 |
_cleanup_(unlink_tempfilep) char p[] = "/tmp/dns-cache-dump-a-with-cname-XXXXXX"; |
| 1274 |
_cleanup_fclose_ FILE *f = NULL; |
| 1275 |
fmkostemp_safe(p, "r+", &f); |
| 1276 |
dns_cache_dump(&cache, f); |
| 1277 |
|
| 1278 |
const char *expected[] = { |
| 1279 |
"\texample.com IN A 192.168.1.127", |
| 1280 |
"\twww.example.com IN CNAME example.com" |
| 1281 |
}; |
| 1282 |
check_dump_contents(f, expected, 2); |
| 1283 |
} |
| 1284 |
|
| 1285 |
/* ================================================================ |
| 1286 |
* dns_cache_dump_to_json() |
| 1287 |
* ================================================================ */ |
| 1288 |
|
| 1289 |
TEST(dns_cache_dump_json_basic) { |
| 1290 |
_cleanup_(dns_cache_unrefp) DnsCache cache = new_cache(); |
| 1291 |
_cleanup_(put_args_unrefp) PutArgs put_args = mk_put_args(); |
| 1292 |
_cleanup_(sd_json_variant_unrefp) sd_json_variant *json = NULL, *expected = NULL; |
| 1293 |
sd_json_variant *item = NULL, *rr = NULL; |
| 1294 |
_cleanup_free_ char *str = calloc(256, sizeof(char)); |
| 1295 |
|
| 1296 |
ASSERT_NOT_NULL(str); |
| 1297 |
|
| 1298 |
put_args.key = dns_resource_key_new(DNS_CLASS_IN, DNS_TYPE_A, "www.example.com"); |
| 1299 |
ASSERT_NOT_NULL(put_args.key); |
| 1300 |
put_args.rcode = DNS_RCODE_SUCCESS; |
| 1301 |
answer_add_a(&put_args, put_args.key, 0xc0a8017f, 3600, DNS_ANSWER_CACHEABLE); |
| 1302 |
cache_put(&cache, &put_args); |
| 1303 |
|
| 1304 |
ASSERT_EQ(dns_cache_size(&cache), 1u); |
| 1305 |
|
| 1306 |
ASSERT_OK(dns_cache_dump_to_json(&cache, &json)); |
| 1307 |
ASSERT_NOT_NULL(json); |
| 1308 |
|
| 1309 |
ASSERT_TRUE(sd_json_variant_is_array(json)); |
| 1310 |
ASSERT_EQ(sd_json_variant_elements(json), 1u); |
| 1311 |
|
| 1312 |
item = BY_IDX(json, 0); |
| 1313 |
ASSERT_NOT_NULL(item); |
| 1314 |
|
| 1315 |
sprintf(str, "{ \"class\": %d, \"type\": %d, \"name\": \"www.example.com\" }", DNS_CLASS_IN, DNS_TYPE_A); |
| 1316 |
ASSERT_OK(sd_json_parse(str, 0, &expected, NULL, NULL)); |
| 1317 |
ASSERT_TRUE(sd_json_variant_equal(BY_KEY(item, "key"), expected)); |
| 1318 |
|
| 1319 |
ASSERT_TRUE(sd_json_variant_is_array(BY_KEY(item, "rrs"))); |
| 1320 |
ASSERT_EQ(sd_json_variant_elements(BY_KEY(item, "rrs")), 1u); |
| 1321 |
|
| 1322 |
rr = BY_KEY(BY_IDX(BY_KEY(item, "rrs"), 0), "rr"); |
| 1323 |
ASSERT_NOT_NULL(rr); |
| 1324 |
ASSERT_TRUE(sd_json_variant_equal(BY_KEY(rr, "key"), expected)); |
| 1325 |
|
| 1326 |
sd_json_variant_unref(expected); |
| 1327 |
|
| 1328 |
sprintf(str, "[192, 168, 1, 127]"); |
| 1329 |
ASSERT_OK(sd_json_parse(str, 0, &expected, NULL, NULL)); |
| 1330 |
ASSERT_TRUE(sd_json_variant_equal(BY_KEY(rr, "address"), expected)); |
| 1331 |
|
| 1332 |
ASSERT_TRUE(sd_json_variant_is_string(BY_KEY(BY_IDX(BY_KEY(item, "rrs"), 0), "raw"))); |
| 1333 |
ASSERT_TRUE(sd_json_variant_is_integer(BY_KEY(item, "until"))); |
| 1334 |
} |
| 1335 |
|
| 1336 |
DEFINE_TEST_MAIN(LOG_DEBUG); |