| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
|
| 3 |
#include <stdlib.h> |
| 4 |
|
| 5 |
#include "sd-event.h" |
| 6 |
|
| 7 |
#include "dns-answer.h" |
| 8 |
#include "dns-packet.h" |
| 9 |
#include "dns-question.h" |
| 10 |
#include "dns-rr.h" |
| 11 |
#include "resolved-dns-query.h" |
| 12 |
#include "resolved-dns-scope.h" |
| 13 |
#include "resolved-dns-search-domain.h" |
| 14 |
#include "resolved-dns-server.h" |
| 15 |
#include "resolved-dns-transaction.h" |
| 16 |
#include "resolved-link.h" |
| 17 |
#include "resolved-manager.h" |
| 18 |
#include "tests.h" |
| 19 |
|
| 20 |
static char* checked_strdup(const char *str) { |
| 21 |
char *copy = strdup(str); |
| 22 |
ASSERT_NOT_NULL(copy); |
| 23 |
return copy; |
| 24 |
} |
| 25 |
|
| 26 |
/* ================================================================ |
| 27 |
* dns_query_new() |
| 28 |
* ================================================================ */ |
| 29 |
|
| 30 |
TEST(dns_query_new_single_question) { |
| 31 |
Manager manager = {}; |
| 32 |
_cleanup_(dns_question_unrefp) DnsQuestion *question = NULL; |
| 33 |
_cleanup_(dns_query_freep) DnsQuery *query = NULL; |
| 34 |
|
| 35 |
ASSERT_OK(dns_question_new_address(&question, AF_INET, "www.example.com", false)); |
| 36 |
ASSERT_NOT_NULL(question); |
| 37 |
|
| 38 |
ASSERT_OK(dns_query_new(&manager, &query, question, NULL, NULL, 1, 0)); |
| 39 |
ASSERT_NOT_NULL(query); |
| 40 |
} |
| 41 |
|
| 42 |
TEST(dns_query_new_multi_question_same_domain) { |
| 43 |
Manager manager = {}; |
| 44 |
_cleanup_(dns_question_unrefp) DnsQuestion *question = NULL; |
| 45 |
_cleanup_(dns_query_freep) DnsQuery *query = NULL; |
| 46 |
DnsResourceKey *key = NULL; |
| 47 |
|
| 48 |
question = dns_question_new(2); |
| 49 |
ASSERT_NOT_NULL(question); |
| 50 |
|
| 51 |
key = dns_resource_key_new(DNS_CLASS_IN, DNS_TYPE_A, "www.example.com"); |
| 52 |
ASSERT_NOT_NULL(key); |
| 53 |
ASSERT_OK(dns_question_add(question, key, 0)); |
| 54 |
dns_resource_key_unref(key); |
| 55 |
|
| 56 |
key = dns_resource_key_new(DNS_CLASS_IN, DNS_TYPE_AAAA, "www.example.com"); |
| 57 |
ASSERT_NOT_NULL(key); |
| 58 |
ASSERT_OK(dns_question_add(question, key, 0)); |
| 59 |
dns_resource_key_unref(key); |
| 60 |
|
| 61 |
ASSERT_OK(dns_query_new(&manager, &query, question, NULL, NULL, 1, 0)); |
| 62 |
ASSERT_NOT_NULL(query); |
| 63 |
} |
| 64 |
|
| 65 |
TEST(dns_query_new_multi_question_different_domain) { |
| 66 |
Manager manager = {}; |
| 67 |
_cleanup_(dns_question_unrefp) DnsQuestion *question = NULL; |
| 68 |
_cleanup_(dns_query_freep) DnsQuery *query = NULL; |
| 69 |
DnsResourceKey *key = NULL; |
| 70 |
|
| 71 |
question = dns_question_new(2); |
| 72 |
ASSERT_NOT_NULL(question); |
| 73 |
|
| 74 |
key = dns_resource_key_new(DNS_CLASS_IN, DNS_TYPE_A, "ns1.example.com"); |
| 75 |
ASSERT_NOT_NULL(key); |
| 76 |
ASSERT_OK(dns_question_add(question, key, 0)); |
| 77 |
dns_resource_key_unref(key); |
| 78 |
|
| 79 |
key = dns_resource_key_new(DNS_CLASS_IN, DNS_TYPE_AAAA, "ns2.example.com"); |
| 80 |
ASSERT_NOT_NULL(key); |
| 81 |
ASSERT_OK(dns_question_add(question, key, 0)); |
| 82 |
dns_resource_key_unref(key); |
| 83 |
|
| 84 |
ASSERT_ERROR(dns_query_new(&manager, &query, question, NULL, NULL, 1, 0), EINVAL); |
| 85 |
ASSERT_NULL(query); |
| 86 |
} |
| 87 |
|
| 88 |
#if HAVE_LIBIDN2 |
| 89 |
TEST(dns_query_new_same_utf8_and_idna) { |
| 90 |
Manager manager = {}; |
| 91 |
_cleanup_(dns_question_unrefp) DnsQuestion *q_utf8 = NULL, *q_idna = NULL; |
| 92 |
_cleanup_(dns_query_freep) DnsQuery *query = NULL; |
| 93 |
|
| 94 |
ASSERT_OK(dns_question_new_address(&q_utf8, AF_INET, "www.\xF0\x9F\x98\xB1.com", false)); |
| 95 |
ASSERT_NOT_NULL(q_utf8); |
| 96 |
|
| 97 |
ASSERT_OK(dns_question_new_address(&q_idna, AF_INET, "www.\xF0\x9F\x98\xB1.com", true)); |
| 98 |
ASSERT_NOT_NULL(q_idna); |
| 99 |
|
| 100 |
ASSERT_OK(dns_query_new(&manager, &query, q_utf8, q_idna, NULL, 1, 0)); |
| 101 |
ASSERT_NOT_NULL(query); |
| 102 |
} |
| 103 |
|
| 104 |
TEST(dns_query_new_different_utf8_and_idna) { |
| 105 |
Manager manager = {}; |
| 106 |
_cleanup_(dns_question_unrefp) DnsQuestion *q_utf8 = NULL, *q_idna = NULL; |
| 107 |
_cleanup_(dns_query_freep) DnsQuery *query = NULL; |
| 108 |
|
| 109 |
ASSERT_OK(dns_question_new_address(&q_utf8, AF_INET, "www.\xF0\x9F\x98\xB1.com", false)); |
| 110 |
ASSERT_NOT_NULL(q_utf8); |
| 111 |
|
| 112 |
ASSERT_OK(dns_question_new_address(&q_idna, AF_INET, "www.\xF0\x9F\x8E\xBC.com", true)); |
| 113 |
ASSERT_NOT_NULL(q_idna); |
| 114 |
|
| 115 |
ASSERT_OK(dns_query_new(&manager, &query, q_utf8, q_idna, NULL, 1, 0)); |
| 116 |
ASSERT_NOT_NULL(query); |
| 117 |
} |
| 118 |
#endif |
| 119 |
|
| 120 |
TEST(dns_query_new_bypass_ok) { |
| 121 |
Manager manager = {}; |
| 122 |
_cleanup_(dns_query_freep) DnsQuery *query = NULL; |
| 123 |
_cleanup_(dns_packet_unrefp) DnsPacket *packet = NULL; |
| 124 |
_cleanup_(dns_question_unrefp) DnsQuestion *question = NULL; |
| 125 |
|
| 126 |
ASSERT_OK(dns_packet_new_query(&packet, DNS_PROTOCOL_DNS, 0, false)); |
| 127 |
ASSERT_NOT_NULL(packet); |
| 128 |
|
| 129 |
ASSERT_OK(dns_question_new_address(&question, AF_INET, "www.example.com", false)); |
| 130 |
ASSERT_NOT_NULL(question); |
| 131 |
|
| 132 |
ASSERT_OK(dns_packet_append_question(packet, question)); |
| 133 |
DNS_PACKET_HEADER(packet)->qdcount = htobe16(dns_question_size(question)); |
| 134 |
ASSERT_OK(dns_packet_extract(packet)); |
| 135 |
|
| 136 |
ASSERT_OK(dns_query_new(&manager, &query, NULL, NULL, packet, 1, 0)); |
| 137 |
ASSERT_NOT_NULL(query); |
| 138 |
} |
| 139 |
|
| 140 |
TEST(dns_query_new_bypass_conflict) { |
| 141 |
Manager manager = {}; |
| 142 |
_cleanup_(dns_query_freep) DnsQuery *query = NULL; |
| 143 |
_cleanup_(dns_packet_unrefp) DnsPacket *packet = NULL; |
| 144 |
_cleanup_(dns_question_unrefp) DnsQuestion *question = NULL, *extra_q = NULL; |
| 145 |
|
| 146 |
ASSERT_OK(dns_packet_new_query(&packet, DNS_PROTOCOL_DNS, 0, false)); |
| 147 |
ASSERT_NOT_NULL(packet); |
| 148 |
|
| 149 |
ASSERT_OK(dns_question_new_address(&question, AF_INET, "www.example.com", false)); |
| 150 |
ASSERT_NOT_NULL(question); |
| 151 |
|
| 152 |
ASSERT_OK(dns_packet_append_question(packet, question)); |
| 153 |
DNS_PACKET_HEADER(packet)->qdcount = htobe16(dns_question_size(question)); |
| 154 |
ASSERT_OK(dns_packet_extract(packet)); |
| 155 |
|
| 156 |
ASSERT_OK(dns_question_new_address(&extra_q, AF_INET, "www.example.com", false)); |
| 157 |
ASSERT_NOT_NULL(extra_q); |
| 158 |
|
| 159 |
ASSERT_ERROR(dns_query_new(&manager, &query, extra_q, NULL, packet, 1, 0), EINVAL); |
| 160 |
ASSERT_NULL(query); |
| 161 |
} |
| 162 |
|
| 163 |
#define MAX_QUERIES 2048 |
| 164 |
|
| 165 |
TEST(dns_query_new_too_many_questions) { |
| 166 |
Manager manager = {}; |
| 167 |
DnsQuestion *question = NULL; |
| 168 |
DnsQuery *queries[MAX_QUERIES + 1]; |
| 169 |
|
| 170 |
for (size_t i = 0; i < MAX_QUERIES; i++) { |
| 171 |
ASSERT_OK(dns_question_new_address(&question, AF_INET, "www.example.com", false)); |
| 172 |
ASSERT_NOT_NULL(question); |
| 173 |
|
| 174 |
ASSERT_OK(dns_query_new(&manager, &queries[i], question, NULL, NULL, 1, 0)); |
| 175 |
ASSERT_NOT_NULL(queries[i]); |
| 176 |
|
| 177 |
dns_question_unref(question); |
| 178 |
} |
| 179 |
|
| 180 |
ASSERT_OK(dns_question_new_address(&question, AF_INET, "www.example.com", false)); |
| 181 |
ASSERT_NOT_NULL(question); |
| 182 |
|
| 183 |
ASSERT_ERROR(dns_query_new(&manager, &queries[MAX_QUERIES], question, NULL, NULL, 1, 0), EBUSY); |
| 184 |
dns_question_unref(question); |
| 185 |
|
| 186 |
for (size_t i = 0; i < MAX_QUERIES; i++) |
| 187 |
dns_query_free(queries[i]); |
| 188 |
} |
| 189 |
|
| 190 |
/* ================================================================ |
| 191 |
* dns_query_make_auxiliary() |
| 192 |
* ================================================================ */ |
| 193 |
|
| 194 |
TEST(dns_query_make_auxiliary) { |
| 195 |
Manager manager = {}; |
| 196 |
_cleanup_(dns_question_unrefp) DnsQuestion *qn1 = NULL, *qn2 = NULL, *qn3 = NULL; |
| 197 |
_cleanup_(dns_query_freep) DnsQuery *q1 = NULL, *q2 = NULL, *q3 = NULL; |
| 198 |
|
| 199 |
ASSERT_OK(dns_question_new_address(&qn1, AF_INET, "www.example.com", false)); |
| 200 |
ASSERT_NOT_NULL(qn1); |
| 201 |
ASSERT_OK(dns_query_new(&manager, &q1, qn1, NULL, NULL, 1, 0)); |
| 202 |
ASSERT_NOT_NULL(q1); |
| 203 |
|
| 204 |
ASSERT_OK(dns_question_new_address(&qn2, AF_INET, "www.example.net", false)); |
| 205 |
ASSERT_NOT_NULL(qn2); |
| 206 |
ASSERT_OK(dns_query_new(&manager, &q2, qn2, NULL, NULL, 1, 0)); |
| 207 |
ASSERT_NOT_NULL(q2); |
| 208 |
|
| 209 |
ASSERT_OK(dns_question_new_address(&qn3, AF_INET, "www.example.org", false)); |
| 210 |
ASSERT_NOT_NULL(qn3); |
| 211 |
ASSERT_OK(dns_query_new(&manager, &q3, qn3, NULL, NULL, 1, 0)); |
| 212 |
ASSERT_NOT_NULL(q3); |
| 213 |
|
| 214 |
ASSERT_OK(dns_query_make_auxiliary(q2, q1)); |
| 215 |
ASSERT_OK(dns_query_make_auxiliary(q3, q1)); |
| 216 |
|
| 217 |
ASSERT_EQ(q1->n_auxiliary_queries, 2u); |
| 218 |
ASSERT_TRUE(q1->auxiliary_queries == q3); |
| 219 |
ASSERT_TRUE(q1->auxiliary_queries->auxiliary_queries_next == q2); |
| 220 |
|
| 221 |
ASSERT_TRUE(q2->auxiliary_for == q1); |
| 222 |
ASSERT_TRUE(q3->auxiliary_for == q1); |
| 223 |
} |
| 224 |
|
| 225 |
/* ================================================================ |
| 226 |
* dns_query_process_cname_one() |
| 227 |
* ================================================================ */ |
| 228 |
|
| 229 |
TEST(dns_query_process_cname_one_null) { |
| 230 |
Manager manager = {}; |
| 231 |
_cleanup_(dns_question_unrefp) DnsQuestion *question = NULL; |
| 232 |
_cleanup_(dns_query_freep) DnsQuery *query = NULL; |
| 233 |
|
| 234 |
ASSERT_OK(dns_question_new_address(&question, AF_INET, "www.example.com", false)); |
| 235 |
ASSERT_NOT_NULL(question); |
| 236 |
|
| 237 |
ASSERT_OK(dns_query_new(&manager, &query, NULL, question, NULL, 1, 0)); |
| 238 |
ASSERT_NOT_NULL(query); |
| 239 |
|
| 240 |
ASSERT_EQ(dns_query_process_cname_one(query), DNS_QUERY_MATCH); |
| 241 |
} |
| 242 |
|
| 243 |
TEST(dns_query_process_cname_one_success_exact_match) { |
| 244 |
Manager manager = {}; |
| 245 |
_cleanup_(dns_question_unrefp) DnsQuestion *question = NULL; |
| 246 |
_cleanup_(dns_query_freep) DnsQuery *query = NULL; |
| 247 |
DnsResourceRecord *rr = NULL; |
| 248 |
|
| 249 |
ASSERT_OK(dns_question_new_address(&question, AF_INET, "www.example.com", false)); |
| 250 |
ASSERT_NOT_NULL(question); |
| 251 |
|
| 252 |
ASSERT_OK(dns_query_new(&manager, &query, NULL, question, NULL, 1, 0)); |
| 253 |
ASSERT_NOT_NULL(query); |
| 254 |
|
| 255 |
query->state = DNS_TRANSACTION_SUCCESS; |
| 256 |
query->answer_protocol = DNS_PROTOCOL_DNS; |
| 257 |
query->answer_family = AF_INET; |
| 258 |
query->answer = dns_answer_new(1); |
| 259 |
ASSERT_NOT_NULL(query->answer); |
| 260 |
|
| 261 |
rr = dns_resource_record_new_full(DNS_CLASS_IN, DNS_TYPE_A, "www.example.com"); |
| 262 |
ASSERT_NOT_NULL(rr); |
| 263 |
rr->ttl = 3600; |
| 264 |
rr->a.in_addr.s_addr = htobe32(0xc0a8017f); |
| 265 |
dns_answer_add(query->answer, rr, 1, 0, NULL); |
| 266 |
dns_resource_record_unref(rr); |
| 267 |
|
| 268 |
ASSERT_EQ(dns_query_process_cname_one(query), DNS_QUERY_MATCH); |
| 269 |
|
| 270 |
ASSERT_EQ(query->n_cname_redirects, 0u); |
| 271 |
} |
| 272 |
|
| 273 |
TEST(dns_query_process_cname_one_success_no_match) { |
| 274 |
Manager manager = {}; |
| 275 |
_cleanup_(dns_question_unrefp) DnsQuestion *question = NULL; |
| 276 |
_cleanup_(dns_query_freep) DnsQuery *query = NULL; |
| 277 |
DnsResourceRecord *rr = NULL; |
| 278 |
|
| 279 |
ASSERT_OK(dns_question_new_address(&question, AF_INET, "www.example.com", false)); |
| 280 |
ASSERT_NOT_NULL(question); |
| 281 |
|
| 282 |
ASSERT_OK(dns_query_new(&manager, &query, NULL, question, NULL, 1, 0)); |
| 283 |
ASSERT_NOT_NULL(query); |
| 284 |
|
| 285 |
query->state = DNS_TRANSACTION_SUCCESS; |
| 286 |
query->answer_protocol = DNS_PROTOCOL_DNS; |
| 287 |
query->answer_family = AF_INET; |
| 288 |
query->answer = dns_answer_new(1); |
| 289 |
ASSERT_NOT_NULL(query->answer); |
| 290 |
|
| 291 |
rr = dns_resource_record_new_full(DNS_CLASS_IN, DNS_TYPE_A, "tmp.example.com"); |
| 292 |
ASSERT_NOT_NULL(rr); |
| 293 |
rr->ttl = 3600; |
| 294 |
rr->a.in_addr.s_addr = htobe32(0xc0a8017f); |
| 295 |
dns_answer_add(query->answer, rr, 1, 0, NULL); |
| 296 |
dns_resource_record_unref(rr); |
| 297 |
|
| 298 |
ASSERT_EQ(dns_query_process_cname_one(query), DNS_QUERY_NOMATCH); |
| 299 |
|
| 300 |
ASSERT_EQ(query->n_cname_redirects, 0u); |
| 301 |
} |
| 302 |
|
| 303 |
TEST(dns_query_process_cname_one_success_match_cname) { |
| 304 |
Manager manager = {}; |
| 305 |
_cleanup_(dns_question_unrefp) DnsQuestion *question = NULL; |
| 306 |
_cleanup_(dns_query_freep) DnsQuery *query = NULL; |
| 307 |
DnsResourceRecord *rr = NULL; |
| 308 |
DnsResourceKey *key = NULL; |
| 309 |
|
| 310 |
ASSERT_OK(dns_question_new_address(&question, AF_INET, "www.example.com", false)); |
| 311 |
ASSERT_NOT_NULL(question); |
| 312 |
|
| 313 |
ASSERT_OK(dns_query_new(&manager, &query, NULL, question, NULL, 1, 0)); |
| 314 |
ASSERT_NOT_NULL(query); |
| 315 |
|
| 316 |
query->state = DNS_TRANSACTION_SUCCESS; |
| 317 |
query->answer_protocol = DNS_PROTOCOL_DNS; |
| 318 |
query->answer_family = AF_INET; |
| 319 |
query->answer_query_flags = SD_RESOLVED_FROM_NETWORK; |
| 320 |
query->answer = dns_answer_new(1); |
| 321 |
ASSERT_NOT_NULL(query->answer); |
| 322 |
|
| 323 |
rr = dns_resource_record_new_full(DNS_CLASS_IN, DNS_TYPE_CNAME, "www.example.com"); |
| 324 |
ASSERT_NOT_NULL(rr); |
| 325 |
rr->ttl = 3600; |
| 326 |
rr->cname.name = checked_strdup("example.com"); |
| 327 |
dns_answer_add(query->answer, rr, 1, 0, NULL); |
| 328 |
dns_resource_record_unref(rr); |
| 329 |
|
| 330 |
ASSERT_EQ(dns_query_process_cname_one(query), DNS_QUERY_CNAME); |
| 331 |
|
| 332 |
ASSERT_FALSE(dns_query_fully_authenticated(query)); |
| 333 |
ASSERT_FALSE(dns_query_fully_confidential(query)); |
| 334 |
ASSERT_FALSE(dns_query_fully_authoritative(query)); |
| 335 |
|
| 336 |
ASSERT_GT(query->flags & SD_RESOLVED_NO_SEARCH, 0u); |
| 337 |
|
| 338 |
ASSERT_EQ(query->n_cname_redirects, 1u); |
| 339 |
|
| 340 |
ASSERT_EQ(dns_question_size(query->collected_questions), 1u); |
| 341 |
ASSERT_NULL(query->question_utf8); |
| 342 |
ASSERT_EQ(dns_question_size(query->question_idna), 1u); |
| 343 |
|
| 344 |
key = dns_resource_key_new(DNS_CLASS_IN, DNS_TYPE_A, "www.example.com"); |
| 345 |
ASSERT_NOT_NULL(key); |
| 346 |
ASSERT_TRUE(dns_question_contains_key(query->collected_questions, key)); |
| 347 |
dns_resource_key_unref(key); |
| 348 |
|
| 349 |
key = dns_resource_key_new(DNS_CLASS_IN, DNS_TYPE_A, "example.com"); |
| 350 |
ASSERT_NOT_NULL(key); |
| 351 |
ASSERT_TRUE(dns_question_contains_key(query->question_idna, key)); |
| 352 |
dns_resource_key_unref(key); |
| 353 |
} |
| 354 |
|
| 355 |
TEST(dns_query_process_cname_one_success_flags) { |
| 356 |
Manager manager = {}; |
| 357 |
_cleanup_(dns_question_unrefp) DnsQuestion *question = NULL; |
| 358 |
_cleanup_(dns_query_freep) DnsQuery *query = NULL; |
| 359 |
DnsResourceRecord *rr = NULL; |
| 360 |
|
| 361 |
ASSERT_OK(dns_question_new_address(&question, AF_INET, "www.example.com", false)); |
| 362 |
ASSERT_NOT_NULL(question); |
| 363 |
|
| 364 |
ASSERT_OK(dns_query_new(&manager, &query, NULL, question, NULL, 1, 0)); |
| 365 |
ASSERT_NOT_NULL(query); |
| 366 |
|
| 367 |
query->state = DNS_TRANSACTION_SUCCESS; |
| 368 |
query->answer_protocol = DNS_PROTOCOL_DNS; |
| 369 |
query->answer_family = AF_INET; |
| 370 |
query->answer = dns_answer_new(1); |
| 371 |
ASSERT_NOT_NULL(query->answer); |
| 372 |
|
| 373 |
query->answer_query_flags = SD_RESOLVED_FROM_NETWORK | |
| 374 |
SD_RESOLVED_AUTHENTICATED | |
| 375 |
SD_RESOLVED_CONFIDENTIAL | |
| 376 |
SD_RESOLVED_SYNTHETIC; |
| 377 |
|
| 378 |
rr = dns_resource_record_new_full(DNS_CLASS_IN, DNS_TYPE_CNAME, "www.example.com"); |
| 379 |
ASSERT_NOT_NULL(rr); |
| 380 |
rr->ttl = 3600; |
| 381 |
rr->cname.name = checked_strdup("example.com"); |
| 382 |
dns_answer_add(query->answer, rr, 1, 0, NULL); |
| 383 |
dns_resource_record_unref(rr); |
| 384 |
|
| 385 |
ASSERT_EQ(dns_query_process_cname_one(query), DNS_QUERY_CNAME); |
| 386 |
|
| 387 |
ASSERT_TRUE(dns_query_fully_authenticated(query)); |
| 388 |
ASSERT_TRUE(dns_query_fully_confidential(query)); |
| 389 |
ASSERT_TRUE(dns_query_fully_authoritative(query)); |
| 390 |
} |
| 391 |
|
| 392 |
TEST(dns_query_process_cname_one_success_match_dname) { |
| 393 |
Manager manager = {}; |
| 394 |
_cleanup_(dns_question_unrefp) DnsQuestion *question = NULL; |
| 395 |
_cleanup_(dns_query_freep) DnsQuery *query = NULL; |
| 396 |
DnsResourceRecord *rr = NULL; |
| 397 |
DnsResourceKey *key = NULL; |
| 398 |
|
| 399 |
ASSERT_OK(dns_question_new_address(&question, AF_INET, "www.example.com", false)); |
| 400 |
ASSERT_NOT_NULL(question); |
| 401 |
|
| 402 |
ASSERT_OK(dns_query_new(&manager, &query, NULL, question, NULL, 1, 0)); |
| 403 |
ASSERT_NOT_NULL(query); |
| 404 |
|
| 405 |
query->state = DNS_TRANSACTION_SUCCESS; |
| 406 |
query->answer_protocol = DNS_PROTOCOL_DNS; |
| 407 |
query->answer_family = AF_INET; |
| 408 |
query->answer = dns_answer_new(1); |
| 409 |
ASSERT_NOT_NULL(query->answer); |
| 410 |
|
| 411 |
rr = dns_resource_record_new_full(DNS_CLASS_IN, DNS_TYPE_DNAME, "example.com"); |
| 412 |
ASSERT_NOT_NULL(rr); |
| 413 |
rr->ttl = 3600; |
| 414 |
rr->dname.name = checked_strdup("v2.example.com"); |
| 415 |
dns_answer_add(query->answer, rr, 1, 0, NULL); |
| 416 |
dns_resource_record_unref(rr); |
| 417 |
|
| 418 |
ASSERT_EQ(dns_query_process_cname_one(query), DNS_QUERY_CNAME); |
| 419 |
|
| 420 |
ASSERT_EQ(query->n_cname_redirects, 1u); |
| 421 |
|
| 422 |
ASSERT_EQ(dns_question_size(query->collected_questions), 1u); |
| 423 |
ASSERT_NULL(query->question_utf8); |
| 424 |
ASSERT_EQ(dns_question_size(query->question_idna), 1u); |
| 425 |
|
| 426 |
key = dns_resource_key_new(DNS_CLASS_IN, DNS_TYPE_A, "www.example.com"); |
| 427 |
ASSERT_NOT_NULL(key); |
| 428 |
ASSERT_TRUE(dns_question_contains_key(query->collected_questions, key)); |
| 429 |
dns_resource_key_unref(key); |
| 430 |
|
| 431 |
key = dns_resource_key_new(DNS_CLASS_IN, DNS_TYPE_A, "www.v2.example.com"); |
| 432 |
ASSERT_NOT_NULL(key); |
| 433 |
ASSERT_TRUE(dns_question_contains_key(query->question_idna, key)); |
| 434 |
dns_resource_key_unref(key); |
| 435 |
} |
| 436 |
|
| 437 |
#if HAVE_LIBIDN2 |
| 438 |
TEST(dns_query_process_cname_one_success_match_dname_utf8_same) { |
| 439 |
Manager manager = {}; |
| 440 |
_cleanup_(dns_question_unrefp) DnsQuestion *q_utf8 = NULL, *q_idna = NULL; |
| 441 |
_cleanup_(dns_query_freep) DnsQuery *query = NULL; |
| 442 |
DnsResourceRecord *rr = NULL; |
| 443 |
DnsResourceKey *key = NULL; |
| 444 |
|
| 445 |
ASSERT_OK(dns_question_new_address(&q_utf8, AF_INET, "www.xn--tl8h.com", false)); |
| 446 |
ASSERT_NOT_NULL(q_utf8); |
| 447 |
|
| 448 |
ASSERT_OK(dns_question_new_address(&q_idna, AF_INET, "www.\xF0\x9F\x8E\xBC.com", true)); |
| 449 |
ASSERT_NOT_NULL(q_idna); |
| 450 |
|
| 451 |
ASSERT_OK(dns_query_new(&manager, &query, q_utf8, q_idna, NULL, 1, 0)); |
| 452 |
ASSERT_NOT_NULL(query); |
| 453 |
|
| 454 |
query->state = DNS_TRANSACTION_SUCCESS; |
| 455 |
query->answer_protocol = DNS_PROTOCOL_DNS; |
| 456 |
query->answer_family = AF_INET; |
| 457 |
query->answer = dns_answer_new(1); |
| 458 |
ASSERT_NOT_NULL(query->answer); |
| 459 |
|
| 460 |
rr = dns_resource_record_new_full(DNS_CLASS_IN, DNS_TYPE_DNAME, "xn--tl8h.com"); |
| 461 |
ASSERT_NOT_NULL(rr); |
| 462 |
rr->ttl = 3600; |
| 463 |
rr->dname.name = checked_strdup("v2.xn--tl8h.com"); |
| 464 |
dns_answer_add(query->answer, rr, 1, 0, NULL); |
| 465 |
dns_resource_record_unref(rr); |
| 466 |
|
| 467 |
ASSERT_EQ(dns_query_process_cname_one(query), DNS_QUERY_CNAME); |
| 468 |
|
| 469 |
ASSERT_EQ(query->n_cname_redirects, 1u); |
| 470 |
|
| 471 |
ASSERT_EQ(dns_question_size(query->collected_questions), 1u); |
| 472 |
ASSERT_EQ(dns_question_size(query->question_utf8), 1u); |
| 473 |
ASSERT_EQ(dns_question_size(query->question_idna), 1u); |
| 474 |
|
| 475 |
key = dns_resource_key_new(DNS_CLASS_IN, DNS_TYPE_A, "www.xn--tl8h.com"); |
| 476 |
ASSERT_NOT_NULL(key); |
| 477 |
ASSERT_TRUE(dns_question_contains_key(query->collected_questions, key)); |
| 478 |
dns_resource_key_unref(key); |
| 479 |
|
| 480 |
key = dns_resource_key_new(DNS_CLASS_IN, DNS_TYPE_A, "www.v2.xn--tl8h.com"); |
| 481 |
ASSERT_NOT_NULL(key); |
| 482 |
ASSERT_TRUE(dns_question_contains_key(query->question_utf8, key)); |
| 483 |
dns_resource_key_unref(key); |
| 484 |
|
| 485 |
key = dns_resource_key_new(DNS_CLASS_IN, DNS_TYPE_A, "www.v2.xn--tl8h.com"); |
| 486 |
ASSERT_NOT_NULL(key); |
| 487 |
ASSERT_TRUE(dns_question_contains_key(query->question_idna, key)); |
| 488 |
dns_resource_key_unref(key); |
| 489 |
} |
| 490 |
|
| 491 |
TEST(dns_query_process_cname_one_success_match_dname_utf8_different) { |
| 492 |
Manager manager = {}; |
| 493 |
_cleanup_(dns_question_unrefp) DnsQuestion *q_utf8 = NULL, *q_idna = NULL; |
| 494 |
_cleanup_(dns_query_freep) DnsQuery *query = NULL; |
| 495 |
DnsResourceRecord *rr = NULL; |
| 496 |
DnsResourceKey *key = NULL; |
| 497 |
|
| 498 |
ASSERT_OK(dns_question_new_address(&q_utf8, AF_INET, "www.\xF0\x9F\x98\xB1.com", false)); |
| 499 |
ASSERT_NOT_NULL(q_utf8); |
| 500 |
|
| 501 |
ASSERT_OK(dns_question_new_address(&q_idna, AF_INET, "www.\xF0\x9F\x8E\xBC.com", true)); |
| 502 |
ASSERT_NOT_NULL(q_idna); |
| 503 |
|
| 504 |
ASSERT_OK(dns_query_new(&manager, &query, q_utf8, q_idna, NULL, 1, 0)); |
| 505 |
ASSERT_NOT_NULL(query); |
| 506 |
|
| 507 |
query->state = DNS_TRANSACTION_SUCCESS; |
| 508 |
query->answer_protocol = DNS_PROTOCOL_DNS; |
| 509 |
query->answer_family = AF_INET; |
| 510 |
query->answer = dns_answer_new(1); |
| 511 |
ASSERT_NOT_NULL(query->answer); |
| 512 |
|
| 513 |
rr = dns_resource_record_new_full(DNS_CLASS_IN, DNS_TYPE_DNAME, "xn--tl8h.com"); |
| 514 |
ASSERT_NOT_NULL(rr); |
| 515 |
rr->ttl = 3600; |
| 516 |
rr->dname.name = checked_strdup("v2.xn--tl8h.com"); |
| 517 |
dns_answer_add(query->answer, rr, 1, 0, NULL); |
| 518 |
dns_resource_record_unref(rr); |
| 519 |
|
| 520 |
ASSERT_EQ(dns_query_process_cname_one(query), DNS_QUERY_CNAME); |
| 521 |
|
| 522 |
ASSERT_EQ(query->n_cname_redirects, 1u); |
| 523 |
|
| 524 |
ASSERT_EQ(dns_question_size(query->collected_questions), 2u); |
| 525 |
ASSERT_NULL(query->question_utf8); |
| 526 |
ASSERT_EQ(dns_question_size(query->question_idna), 1u); |
| 527 |
|
| 528 |
key = dns_resource_key_new(DNS_CLASS_IN, DNS_TYPE_A, "www.\xF0\x9F\x98\xB1.com"); |
| 529 |
ASSERT_NOT_NULL(key); |
| 530 |
ASSERT_TRUE(dns_question_contains_key(query->collected_questions, key)); |
| 531 |
dns_resource_key_unref(key); |
| 532 |
|
| 533 |
key = dns_resource_key_new(DNS_CLASS_IN, DNS_TYPE_A, "www.xn--tl8h.com"); |
| 534 |
ASSERT_NOT_NULL(key); |
| 535 |
ASSERT_TRUE(dns_question_contains_key(query->collected_questions, key)); |
| 536 |
dns_resource_key_unref(key); |
| 537 |
|
| 538 |
key = dns_resource_key_new(DNS_CLASS_IN, DNS_TYPE_A, "www.v2.xn--tl8h.com"); |
| 539 |
ASSERT_NOT_NULL(key); |
| 540 |
ASSERT_TRUE(dns_question_contains_key(query->question_idna, key)); |
| 541 |
dns_resource_key_unref(key); |
| 542 |
} |
| 543 |
#endif |
| 544 |
|
| 545 |
/* ================================================================ |
| 546 |
* dns_query_process_cname_many() |
| 547 |
* ================================================================ */ |
| 548 |
|
| 549 |
TEST(dns_query_process_cname_many_success_match_multiple_cname) { |
| 550 |
Manager manager = {}; |
| 551 |
_cleanup_(dns_question_unrefp) DnsQuestion *question = NULL; |
| 552 |
_cleanup_(dns_query_freep) DnsQuery *query = NULL; |
| 553 |
DnsResourceRecord *rr = NULL; |
| 554 |
DnsResourceKey *key = NULL; |
| 555 |
|
| 556 |
ASSERT_OK(dns_question_new_address(&question, AF_INET, "www.example.com", false)); |
| 557 |
ASSERT_NOT_NULL(question); |
| 558 |
|
| 559 |
ASSERT_OK(dns_query_new(&manager, &query, NULL, question, NULL, 1, 0)); |
| 560 |
ASSERT_NOT_NULL(query); |
| 561 |
|
| 562 |
query->state = DNS_TRANSACTION_SUCCESS; |
| 563 |
query->answer_protocol = DNS_PROTOCOL_DNS; |
| 564 |
query->answer_family = AF_INET; |
| 565 |
query->answer_query_flags = SD_RESOLVED_FROM_NETWORK; |
| 566 |
query->answer = dns_answer_new(4); |
| 567 |
ASSERT_NOT_NULL(query->answer); |
| 568 |
|
| 569 |
rr = dns_resource_record_new_full(DNS_CLASS_IN, DNS_TYPE_A, "example.com"); |
| 570 |
ASSERT_NOT_NULL(rr); |
| 571 |
rr->ttl = 3600; |
| 572 |
rr->a.in_addr.s_addr = htobe32(0xc0a8017f); |
| 573 |
dns_answer_add(query->answer, rr, 1, 0, NULL); |
| 574 |
dns_resource_record_unref(rr); |
| 575 |
|
| 576 |
rr = dns_resource_record_new_full(DNS_CLASS_IN, DNS_TYPE_CNAME, "www.example.com"); |
| 577 |
ASSERT_NOT_NULL(rr); |
| 578 |
rr->ttl = 3600; |
| 579 |
rr->cname.name = checked_strdup("tmp1.example.com"); |
| 580 |
dns_answer_add(query->answer, rr, 1, 0, NULL); |
| 581 |
dns_resource_record_unref(rr); |
| 582 |
|
| 583 |
rr = dns_resource_record_new_full(DNS_CLASS_IN, DNS_TYPE_CNAME, "tmp2.example.com"); |
| 584 |
ASSERT_NOT_NULL(rr); |
| 585 |
rr->ttl = 3600; |
| 586 |
rr->cname.name = checked_strdup("example.com"); |
| 587 |
dns_answer_add(query->answer, rr, 1, 0, NULL); |
| 588 |
dns_resource_record_unref(rr); |
| 589 |
|
| 590 |
rr = dns_resource_record_new_full(DNS_CLASS_IN, DNS_TYPE_CNAME, "tmp1.example.com"); |
| 591 |
ASSERT_NOT_NULL(rr); |
| 592 |
rr->ttl = 3600; |
| 593 |
rr->cname.name = checked_strdup("tmp2.example.com"); |
| 594 |
dns_answer_add(query->answer, rr, 1, 0, NULL); |
| 595 |
dns_resource_record_unref(rr); |
| 596 |
|
| 597 |
ASSERT_EQ(dns_query_process_cname_many(query), DNS_QUERY_MATCH); |
| 598 |
|
| 599 |
ASSERT_FALSE(dns_query_fully_authenticated(query)); |
| 600 |
ASSERT_FALSE(dns_query_fully_confidential(query)); |
| 601 |
ASSERT_FALSE(dns_query_fully_authoritative(query)); |
| 602 |
|
| 603 |
ASSERT_GT(query->flags & SD_RESOLVED_NO_SEARCH, 0u); |
| 604 |
|
| 605 |
ASSERT_EQ(query->n_cname_redirects, 3u); |
| 606 |
|
| 607 |
ASSERT_EQ(dns_question_size(query->collected_questions), 3u); |
| 608 |
ASSERT_NULL(query->question_utf8); |
| 609 |
ASSERT_EQ(dns_question_size(query->question_idna), 1u); |
| 610 |
|
| 611 |
key = dns_resource_key_new(DNS_CLASS_IN, DNS_TYPE_A, "www.example.com"); |
| 612 |
ASSERT_NOT_NULL(key); |
| 613 |
ASSERT_TRUE(dns_question_contains_key(query->collected_questions, key)); |
| 614 |
dns_resource_key_unref(key); |
| 615 |
|
| 616 |
key = dns_resource_key_new(DNS_CLASS_IN, DNS_TYPE_A, "tmp1.example.com"); |
| 617 |
ASSERT_NOT_NULL(key); |
| 618 |
ASSERT_TRUE(dns_question_contains_key(query->collected_questions, key)); |
| 619 |
dns_resource_key_unref(key); |
| 620 |
|
| 621 |
key = dns_resource_key_new(DNS_CLASS_IN, DNS_TYPE_A, "tmp2.example.com"); |
| 622 |
ASSERT_NOT_NULL(key); |
| 623 |
ASSERT_TRUE(dns_question_contains_key(query->collected_questions, key)); |
| 624 |
dns_resource_key_unref(key); |
| 625 |
|
| 626 |
key = dns_resource_key_new(DNS_CLASS_IN, DNS_TYPE_A, "example.com"); |
| 627 |
ASSERT_NOT_NULL(key); |
| 628 |
ASSERT_TRUE(dns_question_contains_key(query->question_idna, key)); |
| 629 |
dns_resource_key_unref(key); |
| 630 |
} |
| 631 |
|
| 632 |
/* ================================================================ |
| 633 |
* dns_query_string() |
| 634 |
* ================================================================ */ |
| 635 |
|
| 636 |
TEST(dns_query_string_question_utf8) { |
| 637 |
Manager manager = {}; |
| 638 |
_cleanup_(dns_question_unrefp) DnsQuestion *question = NULL; |
| 639 |
_cleanup_(dns_query_freep) DnsQuery *query = NULL; |
| 640 |
|
| 641 |
ASSERT_OK(dns_question_new_address(&question, AF_INET, "utf8.example.com", false)); |
| 642 |
ASSERT_NOT_NULL(question); |
| 643 |
|
| 644 |
ASSERT_OK(dns_query_new(&manager, &query, question, NULL, NULL, 1, 0)); |
| 645 |
ASSERT_NOT_NULL(query); |
| 646 |
|
| 647 |
const char *str = dns_query_string(query); |
| 648 |
ASSERT_STREQ(str, "utf8.example.com"); |
| 649 |
} |
| 650 |
|
| 651 |
TEST(dns_query_string_question_idna) { |
| 652 |
Manager manager = {}; |
| 653 |
_cleanup_(dns_question_unrefp) DnsQuestion *question = NULL; |
| 654 |
_cleanup_(dns_query_freep) DnsQuery *query = NULL; |
| 655 |
|
| 656 |
ASSERT_OK(dns_question_new_address(&question, AF_INET, "idna.example.com", false)); |
| 657 |
ASSERT_NOT_NULL(question); |
| 658 |
|
| 659 |
ASSERT_OK(dns_query_new(&manager, &query, NULL, question, NULL, 1, 0)); |
| 660 |
ASSERT_NOT_NULL(query); |
| 661 |
|
| 662 |
const char *str = dns_query_string(query); |
| 663 |
ASSERT_STREQ(str, "idna.example.com"); |
| 664 |
} |
| 665 |
|
| 666 |
TEST(dns_query_string_question_bypass) { |
| 667 |
Manager manager = {}; |
| 668 |
_cleanup_(dns_query_freep) DnsQuery *query = NULL; |
| 669 |
_cleanup_(dns_packet_unrefp) DnsPacket * packet = NULL; |
| 670 |
|
| 671 |
ASSERT_OK(dns_packet_new_query(&packet, DNS_PROTOCOL_DNS, 0, false)); |
| 672 |
ASSERT_NOT_NULL(packet); |
| 673 |
|
| 674 |
ASSERT_OK(dns_question_new_address(&packet->question, AF_INET, "bypass.example.com", false)); |
| 675 |
ASSERT_NOT_NULL(packet->question); |
| 676 |
|
| 677 |
ASSERT_OK(dns_query_new(&manager, &query, NULL, NULL, packet, 1, 0)); |
| 678 |
ASSERT_NOT_NULL(query); |
| 679 |
|
| 680 |
const char *str = dns_query_string(query); |
| 681 |
ASSERT_STREQ(str, "bypass.example.com"); |
| 682 |
} |
| 683 |
|
| 684 |
TEST(dns_query_string_request_address) { |
| 685 |
Manager manager = {}; |
| 686 |
_cleanup_(dns_question_unrefp) DnsQuestion *question = NULL; |
| 687 |
_cleanup_(dns_query_freep) DnsQuery *query = NULL; |
| 688 |
|
| 689 |
ASSERT_OK(dns_question_new_address(&question, AF_INET, "www.example.com", false)); |
| 690 |
ASSERT_NOT_NULL(question); |
| 691 |
|
| 692 |
ASSERT_OK(dns_query_new(&manager, &query, question, NULL, NULL, 1, 0)); |
| 693 |
ASSERT_NOT_NULL(query); |
| 694 |
|
| 695 |
query->request_family = AF_INET; |
| 696 |
query->request_address.in.s_addr = htobe32(0x7f000001); |
| 697 |
query->request_address_valid = true; |
| 698 |
|
| 699 |
const char *str = dns_query_string(query); |
| 700 |
ASSERT_STREQ(str, "127.0.0.1"); |
| 701 |
} |
| 702 |
|
| 703 |
/* ================================================================ |
| 704 |
* dns_query_go() |
| 705 |
* ================================================================ */ |
| 706 |
|
| 707 |
/* Testing this function is somewhat problematic since, in addition to setting up the state for query |
| 708 |
* candidates, their scopes and transactions, it also directly initiates I/O to files and the network. In |
| 709 |
* particular: |
| 710 |
* |
| 711 |
* - The very first thing it does is try to respond to the query by reading the system /etc/hosts file, which |
| 712 |
* may be symlinked to a systemd resource. Ideally we could test this without accessing global files. |
| 713 |
* |
| 714 |
* - dns_scope_get_dns_server() calls manager_get_dns_server(), which tries to read /etc/resolv.conf. |
| 715 |
* |
| 716 |
* - A potential solution to these issues would be to let these file paths be configured instead of |
| 717 |
* hard-coded into the source. |
| 718 |
* |
| 719 |
* - dns_scope_good_domain(), by checking dns_scope_get_dns_server(), will not match with a scope that does |
| 720 |
* not have a server configured, either on the scope's link (if it has one) or the manager's main/fallback |
| 721 |
* server. Configuring a server means that dns_query_candidate_go() and then dns_transaction_go() will send |
| 722 |
* UDP/TCP traffic to that server. Ideally we'd like to test that we can set up all the candidate and |
| 723 |
* transaction state without actually causing any requests to be sent. |
| 724 |
*/ |
| 725 |
|
| 726 |
typedef struct GoConfig { |
| 727 |
bool has_scope; |
| 728 |
bool use_link; |
| 729 |
bool use_bypass; |
| 730 |
size_t n_search_domains; |
| 731 |
} GoConfig; |
| 732 |
|
| 733 |
static GoConfig mk_go_config(void) { |
| 734 |
return (GoConfig) { |
| 735 |
.has_scope = true, |
| 736 |
.use_link = false, |
| 737 |
.use_bypass = false, |
| 738 |
.n_search_domains = 0 |
| 739 |
}; |
| 740 |
} |
| 741 |
|
| 742 |
typedef struct GoEnvironment { |
| 743 |
Manager manager; |
| 744 |
DnsProtocol protocol; |
| 745 |
int family; |
| 746 |
int ifindex; |
| 747 |
Link *link; |
| 748 |
DnsScope *scope; |
| 749 |
DnsServerType server_type; |
| 750 |
union in_addr_union server_addr; |
| 751 |
char *server_name; |
| 752 |
uint16_t server_port; |
| 753 |
DnsServer *server; |
| 754 |
size_t n_search_domains; |
| 755 |
DnsSearchDomain *search_domains[4]; |
| 756 |
} GoEnvironment; |
| 757 |
|
| 758 |
static void go_env_teardown(GoEnvironment *env) { |
| 759 |
ASSERT_NOT_NULL(env); |
| 760 |
|
| 761 |
for (size_t i = 0 ; i < env->n_search_domains; i++) |
| 762 |
dns_search_domain_unref(env->search_domains[i]); |
| 763 |
|
| 764 |
dns_server_unref(env->server); |
| 765 |
dns_server_unref(env->server); |
| 766 |
free(env->server_name); |
| 767 |
dns_scope_free(env->scope); |
| 768 |
sd_event_unref(env->manager.event); |
| 769 |
} |
| 770 |
|
| 771 |
static const char *SEARCH_DOMAINS[] = { "local", "example.com", "org", "edu" }; |
| 772 |
|
| 773 |
static void go_env_setup(GoEnvironment *env, GoConfig *cfg) { |
| 774 |
ASSERT_NOT_NULL(env); |
| 775 |
ASSERT_NOT_NULL(cfg); |
| 776 |
|
| 777 |
env->manager = (Manager) {}; |
| 778 |
env->protocol = DNS_PROTOCOL_DNS; |
| 779 |
env->family = AF_INET; |
| 780 |
|
| 781 |
ASSERT_OK(sd_event_new(&env->manager.event)); |
| 782 |
ASSERT_NOT_NULL(env->manager.event); |
| 783 |
|
| 784 |
if (cfg->use_link) { |
| 785 |
env->ifindex = 1; |
| 786 |
ASSERT_OK(link_new(&env->manager, &env->link, env->ifindex)); |
| 787 |
ASSERT_NOT_NULL(env->link); |
| 788 |
env->server_type = DNS_SERVER_LINK; |
| 789 |
} else { |
| 790 |
env->ifindex = 0; |
| 791 |
env->link = NULL; |
| 792 |
env->server_type = DNS_SERVER_FALLBACK; |
| 793 |
} |
| 794 |
|
| 795 |
if (cfg->has_scope) { |
| 796 |
ASSERT_OK(dns_scope_new(&env->manager, &env->scope, env->link ? DNS_SCOPE_LINK : DNS_SCOPE_GLOBAL, env->link, /* delegate= */ NULL, env->protocol, env->family)); |
| 797 |
ASSERT_NOT_NULL(env->scope); |
| 798 |
|
| 799 |
env->server_addr.in.s_addr = htobe32(0x7f000001); |
| 800 |
env->server_name = checked_strdup("localhost"); |
| 801 |
env->server_port = 53; |
| 802 |
|
| 803 |
ASSERT_OK(dns_server_new(&env->manager, &env->server, env->server_type, |
| 804 |
env->link, /* delegate= */ NULL, env->family, &env->server_addr, env->server_port, |
| 805 |
env->ifindex, env->server_name, RESOLVE_CONFIG_SOURCE_DBUS)); |
| 806 |
|
| 807 |
ASSERT_NOT_NULL(env->server); |
| 808 |
} else { |
| 809 |
env->scope = NULL; |
| 810 |
env->server_name = NULL; |
| 811 |
env->server = NULL; |
| 812 |
} |
| 813 |
|
| 814 |
env->n_search_domains = cfg->n_search_domains; |
| 815 |
|
| 816 |
for (size_t i = 0 ; i < env->n_search_domains; i++) { |
| 817 |
DnsSearchDomainType type = (env->link == NULL) ? DNS_SEARCH_DOMAIN_SYSTEM : DNS_SEARCH_DOMAIN_LINK; |
| 818 |
ASSERT_OK(dns_search_domain_new(&env->manager, &env->search_domains[i], type, env->link, /* delegate= */ NULL, SEARCH_DOMAINS[i])); |
| 819 |
ASSERT_NOT_NULL(env->search_domains[i]); |
| 820 |
} |
| 821 |
} |
| 822 |
|
| 823 |
static void exercise_dns_query_go(GoConfig *cfg, void (*check_query)(DnsQuery *query)) { |
| 824 |
_cleanup_(go_env_teardown) GoEnvironment env = {}; |
| 825 |
_cleanup_(dns_question_unrefp) DnsQuestion *question = NULL; |
| 826 |
_cleanup_(dns_packet_unrefp) DnsPacket *packet = NULL; |
| 827 |
_cleanup_(dns_query_freep) DnsQuery *query = NULL; |
| 828 |
|
| 829 |
go_env_setup(&env, cfg); |
| 830 |
|
| 831 |
int flags = SD_RESOLVED_FLAGS_MAKE(env.protocol, env.family, false, false); |
| 832 |
|
| 833 |
if (cfg->n_search_domains > 0) { |
| 834 |
/* search domains trigger on single-label domains */ |
| 835 |
ASSERT_OK(dns_question_new_address(&question, env.family, "berlin", false)); |
| 836 |
flags &= ~SD_RESOLVED_NO_SEARCH; |
| 837 |
} else { |
| 838 |
ASSERT_OK(dns_question_new_address(&question, env.family, "www.example.com", false)); |
| 839 |
} |
| 840 |
|
| 841 |
ASSERT_NOT_NULL(question); |
| 842 |
|
| 843 |
if (cfg->use_bypass) { |
| 844 |
ASSERT_OK(dns_packet_new_query(&packet, env.protocol, 0, false)); |
| 845 |
ASSERT_NOT_NULL(packet); |
| 846 |
DNS_PACKET_HEADER(packet)->qdcount = htobe16(1); |
| 847 |
packet->question = dns_question_ref(question); |
| 848 |
ASSERT_OK(dns_packet_append_question(packet, question)); |
| 849 |
|
| 850 |
/* search domains must be turned off for bypass queries, otherwise dns_query_add_candidate() |
| 851 |
* tries to extract the domain name from question_idna which cannot exist on bypasses. */ |
| 852 |
flags |= SD_RESOLVED_NO_SEARCH; |
| 853 |
ASSERT_OK(dns_query_new(&env.manager, &query, NULL, NULL, packet, env.ifindex, flags)); |
| 854 |
} else { |
| 855 |
ASSERT_OK(dns_query_new(&env.manager, &query, question, question, NULL, env.ifindex, flags)); |
| 856 |
} |
| 857 |
|
| 858 |
ASSERT_NOT_NULL(query); |
| 859 |
ASSERT_TRUE(dns_query_go(query)); |
| 860 |
|
| 861 |
if (check_query) |
| 862 |
check_query(query); |
| 863 |
} |
| 864 |
|
| 865 |
static void check_query_no_servers(DnsQuery *query) { |
| 866 |
ASSERT_NOT_NULL(query); |
| 867 |
|
| 868 |
ASSERT_EQ(dns_answer_size(query->answer), 0u); |
| 869 |
ASSERT_EQ(query->answer_rcode, DNS_RCODE_SUCCESS); |
| 870 |
} |
| 871 |
|
| 872 |
TEST(dns_query_go) { |
| 873 |
GoConfig cfg; |
| 874 |
|
| 875 |
cfg = mk_go_config(); |
| 876 |
exercise_dns_query_go(&cfg, NULL); |
| 877 |
|
| 878 |
cfg = mk_go_config(); |
| 879 |
cfg.use_link = true; |
| 880 |
exercise_dns_query_go(&cfg, NULL); |
| 881 |
|
| 882 |
cfg = mk_go_config(); |
| 883 |
cfg.use_bypass = true; |
| 884 |
exercise_dns_query_go(&cfg, NULL); |
| 885 |
|
| 886 |
cfg = mk_go_config(); |
| 887 |
cfg.has_scope = false; |
| 888 |
exercise_dns_query_go(&cfg, check_query_no_servers); |
| 889 |
|
| 890 |
cfg = mk_go_config(); |
| 891 |
cfg.n_search_domains = 2; |
| 892 |
exercise_dns_query_go(&cfg, NULL); |
| 893 |
} |
| 894 |
|
| 895 |
static int intro(void) { |
| 896 |
/* Disable hooks in order to make test cases hermetic */ |
| 897 |
ASSERT_OK_ERRNO(setenv("SYSTEMD_RESOLVED_HOOK", "0", /* overwrite= */ false)); |
| 898 |
return EXIT_SUCCESS; |
| 899 |
} |
| 900 |
|
| 901 |
DEFINE_TEST_MAIN_WITH_INTRO(LOG_DEBUG, intro); |