| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
|
| 3 |
#include "dns-answer.h" |
| 4 |
#include "dns-packet.h" |
| 5 |
#include "dns-question.h" |
| 6 |
#include "dns-rr.h" |
| 7 |
#include "resolved-dns-synthesize.h" |
| 8 |
#include "resolved-manager.h" |
| 9 |
#include "tests.h" |
| 10 |
|
| 11 |
/* ================================================================ |
| 12 |
* dns_synthesize_family(), dns_synthesize_protocol() |
| 13 |
* ================================================================ */ |
| 14 |
|
| 15 |
TEST(dns_synthesize_family_and_protocol) { |
| 16 |
int flags; |
| 17 |
|
| 18 |
flags = SD_RESOLVED_FLAGS_MAKE(DNS_PROTOCOL_DNS, AF_INET, false, false); |
| 19 |
ASSERT_EQ(dns_synthesize_family(flags), AF_UNSPEC); |
| 20 |
ASSERT_EQ(dns_synthesize_protocol(flags), DNS_PROTOCOL_DNS); |
| 21 |
|
| 22 |
flags = SD_RESOLVED_FLAGS_MAKE(DNS_PROTOCOL_LLMNR, AF_INET6, false, false); |
| 23 |
ASSERT_EQ(dns_synthesize_family(flags), AF_INET6); |
| 24 |
ASSERT_EQ(dns_synthesize_protocol(flags), DNS_PROTOCOL_LLMNR); |
| 25 |
|
| 26 |
flags = SD_RESOLVED_FLAGS_MAKE(DNS_PROTOCOL_MDNS, AF_INET, false, false); |
| 27 |
ASSERT_EQ(dns_synthesize_family(flags), AF_INET); |
| 28 |
ASSERT_EQ(dns_synthesize_protocol(flags), DNS_PROTOCOL_MDNS); |
| 29 |
} |
| 30 |
|
| 31 |
/* ================================================================ |
| 32 |
* dns_synthesize_answer() |
| 33 |
* ================================================================ */ |
| 34 |
|
| 35 |
TEST(dns_synthesize_answer_empty) { |
| 36 |
Manager manager = {}; |
| 37 |
_cleanup_(dns_question_unrefp) DnsQuestion *question = NULL; |
| 38 |
_cleanup_(dns_resource_key_unrefp) DnsResourceKey *key = NULL; |
| 39 |
_cleanup_(dns_answer_unrefp) DnsAnswer *answer = NULL; |
| 40 |
|
| 41 |
question = dns_question_new(1); |
| 42 |
ASSERT_NOT_NULL(question); |
| 43 |
|
| 44 |
key = dns_resource_key_new(DNS_CLASS_IN, DNS_TYPE_A, "www.example.com"); |
| 45 |
ASSERT_NOT_NULL(key); |
| 46 |
|
| 47 |
ASSERT_OK(dns_question_add(question, key, 0)); |
| 48 |
|
| 49 |
answer = dns_answer_new(0); |
| 50 |
ASSERT_NOT_NULL(answer); |
| 51 |
|
| 52 |
ASSERT_FALSE(dns_synthesize_answer(&manager, question, 0, &answer)); |
| 53 |
ASSERT_TRUE(dns_answer_isempty(answer)); |
| 54 |
} |
| 55 |
|
| 56 |
TEST(dns_synthesize_answer_reverse) { |
| 57 |
Manager manager = {}; |
| 58 |
_cleanup_(dns_question_unrefp) DnsQuestion *question = NULL; |
| 59 |
_cleanup_(dns_resource_key_unrefp) DnsResourceKey *key = NULL; |
| 60 |
_cleanup_(dns_answer_unrefp) DnsAnswer *answer = NULL; |
| 61 |
|
| 62 |
question = dns_question_new(1); |
| 63 |
ASSERT_NOT_NULL(question); |
| 64 |
|
| 65 |
key = dns_resource_key_new(DNS_CLASS_IN, DNS_TYPE_A, "127.0.0.0.in-addr.arpa"); |
| 66 |
ASSERT_NOT_NULL(key); |
| 67 |
|
| 68 |
ASSERT_OK(dns_question_add(question, key, 0)); |
| 69 |
|
| 70 |
answer = dns_answer_new(0); |
| 71 |
ASSERT_NOT_NULL(answer); |
| 72 |
|
| 73 |
ASSERT_ERROR(dns_synthesize_answer(&manager, question, 0, &answer), ENXIO); |
| 74 |
ASSERT_TRUE(dns_answer_isempty(answer)); |
| 75 |
} |
| 76 |
|
| 77 |
TEST(dns_synthesize_answer_localhost) { |
| 78 |
Manager manager = {}; |
| 79 |
_cleanup_(dns_question_unrefp) DnsQuestion *question = NULL; |
| 80 |
_cleanup_(dns_resource_key_unrefp) DnsResourceKey *key = NULL; |
| 81 |
_cleanup_(dns_answer_unrefp) DnsAnswer *answer = NULL; |
| 82 |
_cleanup_(dns_resource_record_unrefp) DnsResourceRecord *rr = NULL; |
| 83 |
|
| 84 |
question = dns_question_new(1); |
| 85 |
ASSERT_NOT_NULL(question); |
| 86 |
|
| 87 |
key = dns_resource_key_new(DNS_CLASS_IN, DNS_TYPE_A, "localhost"); |
| 88 |
ASSERT_NOT_NULL(key); |
| 89 |
|
| 90 |
ASSERT_OK(dns_question_add(question, key, 0)); |
| 91 |
|
| 92 |
ASSERT_TRUE(dns_synthesize_answer(&manager, question, 0, &answer)); |
| 93 |
|
| 94 |
rr = dns_resource_record_new_full(DNS_CLASS_IN, DNS_TYPE_A, "localhost"); |
| 95 |
ASSERT_NOT_NULL(rr); |
| 96 |
rr->a.in_addr.s_addr = htobe32(0x7f000001); |
| 97 |
|
| 98 |
ASSERT_TRUE(dns_answer_contains(answer, rr)); |
| 99 |
} |
| 100 |
|
| 101 |
TEST(dns_synthesize_answer_own_hostname) { |
| 102 |
Manager manager = {}; |
| 103 |
_cleanup_(dns_question_unrefp) DnsQuestion *question = NULL; |
| 104 |
_cleanup_(dns_resource_key_unrefp) DnsResourceKey *key = NULL; |
| 105 |
_cleanup_(dns_answer_unrefp) DnsAnswer *answer = NULL; |
| 106 |
_cleanup_(dns_resource_record_unrefp) DnsResourceRecord *rr = NULL; |
| 107 |
|
| 108 |
question = dns_question_new(1); |
| 109 |
ASSERT_NOT_NULL(question); |
| 110 |
|
| 111 |
key = dns_resource_key_new(DNS_CLASS_IN, DNS_TYPE_A, "resolver.local"); |
| 112 |
ASSERT_NOT_NULL(key); |
| 113 |
|
| 114 |
ASSERT_OK(dns_question_add(question, key, 0)); |
| 115 |
|
| 116 |
manager.full_hostname = (char *)"resolver.local"; |
| 117 |
|
| 118 |
ASSERT_TRUE(dns_synthesize_answer(&manager, question, 0, &answer)); |
| 119 |
|
| 120 |
rr = dns_resource_record_new_full(DNS_CLASS_IN, DNS_TYPE_A, "resolver.local"); |
| 121 |
ASSERT_NOT_NULL(rr); |
| 122 |
|
| 123 |
ASSERT_TRUE(dns_answer_match_key(answer, rr->key, NULL)); |
| 124 |
} |
| 125 |
|
| 126 |
TEST(dns_synthesize_answer_stub) { |
| 127 |
Manager manager = {}; |
| 128 |
_cleanup_(dns_question_unrefp) DnsQuestion *question = NULL; |
| 129 |
_cleanup_(dns_resource_key_unrefp) DnsResourceKey *key = NULL; |
| 130 |
_cleanup_(dns_answer_unrefp) DnsAnswer *answer = NULL; |
| 131 |
_cleanup_(dns_resource_record_unrefp) DnsResourceRecord *rr = NULL; |
| 132 |
|
| 133 |
question = dns_question_new(1); |
| 134 |
ASSERT_NOT_NULL(question); |
| 135 |
|
| 136 |
key = dns_resource_key_new(DNS_CLASS_IN, DNS_TYPE_A, "_localdnsstub"); |
| 137 |
ASSERT_NOT_NULL(key); |
| 138 |
|
| 139 |
ASSERT_OK(dns_question_add(question, key, 0)); |
| 140 |
|
| 141 |
ASSERT_TRUE(dns_synthesize_answer(&manager, question, 0, &answer)); |
| 142 |
|
| 143 |
rr = dns_resource_record_new_full(DNS_CLASS_IN, DNS_TYPE_A, "_localdnsstub"); |
| 144 |
ASSERT_NOT_NULL(rr); |
| 145 |
|
| 146 |
ASSERT_TRUE(dns_answer_match_key(answer, rr->key, NULL)); |
| 147 |
} |
| 148 |
|
| 149 |
TEST(dns_synthesize_answer_localhost_ptr) { |
| 150 |
Manager manager = {}; |
| 151 |
_cleanup_(dns_question_unrefp) DnsQuestion *question = NULL; |
| 152 |
_cleanup_(dns_resource_key_unrefp) DnsResourceKey *key = NULL; |
| 153 |
_cleanup_(dns_answer_unrefp) DnsAnswer *answer = NULL; |
| 154 |
_cleanup_(dns_resource_record_unrefp) DnsResourceRecord *rr = NULL; |
| 155 |
|
| 156 |
question = dns_question_new(1); |
| 157 |
ASSERT_NOT_NULL(question); |
| 158 |
|
| 159 |
key = dns_resource_key_new(DNS_CLASS_IN, DNS_TYPE_PTR, "1.0.0.127.in-addr.arpa"); |
| 160 |
ASSERT_NOT_NULL(key); |
| 161 |
|
| 162 |
ASSERT_OK(dns_question_add(question, key, 0)); |
| 163 |
|
| 164 |
ASSERT_TRUE(dns_synthesize_answer(&manager, question, 0, &answer)); |
| 165 |
|
| 166 |
rr = dns_resource_record_new_full(DNS_CLASS_IN, DNS_TYPE_PTR, "1.0.0.127.in-addr.arpa"); |
| 167 |
ASSERT_NOT_NULL(rr); |
| 168 |
|
| 169 |
rr->ptr.name = strdup("localhost"); |
| 170 |
ASSERT_NOT_NULL(rr->ptr.name); |
| 171 |
ASSERT_TRUE(dns_answer_contains(answer, rr)); |
| 172 |
} |
| 173 |
|
| 174 |
TEST(dns_synthesize_answer_address) { |
| 175 |
Manager manager = {}; |
| 176 |
_cleanup_(dns_question_unrefp) DnsQuestion *question = NULL; |
| 177 |
_cleanup_(dns_resource_key_unrefp) DnsResourceKey *key = NULL; |
| 178 |
_cleanup_(dns_answer_unrefp) DnsAnswer *answer = NULL; |
| 179 |
|
| 180 |
question = dns_question_new(1); |
| 181 |
ASSERT_NOT_NULL(question); |
| 182 |
|
| 183 |
key = dns_resource_key_new(DNS_CLASS_IN, DNS_TYPE_PTR, "0.1.254.169.in-addr.arpa"); |
| 184 |
ASSERT_NOT_NULL(key); |
| 185 |
|
| 186 |
ASSERT_OK(dns_question_add(question, key, 0)); |
| 187 |
|
| 188 |
manager.full_hostname = (char *)"resolver.local"; |
| 189 |
manager.llmnr_hostname = (char *)"llmnr.resolver.local"; |
| 190 |
manager.mdns_hostname = (char *)"mdns.resolver.local"; |
| 191 |
|
| 192 |
answer = dns_answer_new(0); |
| 193 |
ASSERT_NOT_NULL(answer); |
| 194 |
|
| 195 |
ASSERT_FALSE(dns_synthesize_answer(&manager, question, 0, &answer)); |
| 196 |
ASSERT_TRUE(dns_answer_isempty(answer)); |
| 197 |
} |
| 198 |
|
| 199 |
TEST(dns_synthesize_answer_address_local_hostname) { |
| 200 |
Manager manager = {}; |
| 201 |
_cleanup_(dns_question_unrefp) DnsQuestion *question = NULL; |
| 202 |
_cleanup_(dns_resource_key_unrefp) DnsResourceKey *key = NULL; |
| 203 |
_cleanup_(dns_answer_unrefp) DnsAnswer *answer = NULL; |
| 204 |
DnsResourceRecord *rr = NULL; |
| 205 |
|
| 206 |
question = dns_question_new(1); |
| 207 |
ASSERT_NOT_NULL(question); |
| 208 |
|
| 209 |
key = dns_resource_key_new(DNS_CLASS_IN, DNS_TYPE_PTR, "2.0.0.127.in-addr.arpa"); |
| 210 |
ASSERT_NOT_NULL(key); |
| 211 |
|
| 212 |
ASSERT_OK(dns_question_add(question, key, 0)); |
| 213 |
|
| 214 |
manager.full_hostname = (char *)"resolver.local"; |
| 215 |
manager.llmnr_hostname = (char *)"llmnr.resolver.local"; |
| 216 |
manager.mdns_hostname = (char *)"mdns.resolver.local"; |
| 217 |
|
| 218 |
ASSERT_TRUE(dns_synthesize_answer(&manager, question, 0, &answer)); |
| 219 |
|
| 220 |
rr = dns_resource_record_new_full(DNS_CLASS_IN, DNS_TYPE_PTR, "2.0.0.127.in-addr.arpa"); |
| 221 |
ASSERT_NOT_NULL(rr); |
| 222 |
rr->ptr.name = strdup("resolver.local"); |
| 223 |
ASSERT_NOT_NULL(rr->ptr.name); |
| 224 |
ASSERT_TRUE(dns_answer_contains(answer, rr)); |
| 225 |
dns_resource_record_unref(rr); |
| 226 |
|
| 227 |
rr = dns_resource_record_new_full(DNS_CLASS_IN, DNS_TYPE_PTR, "2.0.0.127.in-addr.arpa"); |
| 228 |
ASSERT_NOT_NULL(rr); |
| 229 |
rr->ptr.name = strdup("llmnr.resolver.local"); |
| 230 |
ASSERT_NOT_NULL(rr->ptr.name); |
| 231 |
ASSERT_TRUE(dns_answer_contains(answer, rr)); |
| 232 |
dns_resource_record_unref(rr); |
| 233 |
|
| 234 |
rr = dns_resource_record_new_full(DNS_CLASS_IN, DNS_TYPE_PTR, "2.0.0.127.in-addr.arpa"); |
| 235 |
ASSERT_NOT_NULL(rr); |
| 236 |
rr->ptr.name = strdup("mdns.resolver.local"); |
| 237 |
ASSERT_NOT_NULL(rr->ptr.name); |
| 238 |
ASSERT_TRUE(dns_answer_contains(answer, rr)); |
| 239 |
dns_resource_record_unref(rr); |
| 240 |
|
| 241 |
rr = dns_resource_record_new_full(DNS_CLASS_IN, DNS_TYPE_PTR, "2.0.0.127.in-addr.arpa"); |
| 242 |
ASSERT_NOT_NULL(rr); |
| 243 |
rr->ptr.name = strdup("localhost"); |
| 244 |
ASSERT_NOT_NULL(rr->ptr.name); |
| 245 |
ASSERT_TRUE(dns_answer_contains(answer, rr)); |
| 246 |
dns_resource_record_unref(rr); |
| 247 |
} |
| 248 |
|
| 249 |
TEST(dns_synthesize_answer_address_local_dns_stub) { |
| 250 |
Manager manager = {}; |
| 251 |
_cleanup_(dns_question_unrefp) DnsQuestion *question = NULL; |
| 252 |
_cleanup_(dns_resource_key_unrefp) DnsResourceKey *key = NULL; |
| 253 |
_cleanup_(dns_answer_unrefp) DnsAnswer *answer = NULL; |
| 254 |
_cleanup_(dns_resource_record_unrefp) DnsResourceRecord *rr = NULL; |
| 255 |
|
| 256 |
question = dns_question_new(1); |
| 257 |
ASSERT_NOT_NULL(question); |
| 258 |
|
| 259 |
key = dns_resource_key_new(DNS_CLASS_IN, DNS_TYPE_PTR, "53.0.0.127.in-addr.arpa"); |
| 260 |
ASSERT_NOT_NULL(key); |
| 261 |
|
| 262 |
ASSERT_OK(dns_question_add(question, key, 0)); |
| 263 |
|
| 264 |
manager.full_hostname = (char *)"resolver.local"; |
| 265 |
manager.llmnr_hostname = (char *)"llmnr.resolver.local"; |
| 266 |
manager.mdns_hostname = (char *)"mdns.resolver.local"; |
| 267 |
|
| 268 |
ASSERT_TRUE(dns_synthesize_answer(&manager, question, 0, &answer)); |
| 269 |
|
| 270 |
rr = dns_resource_record_new_full(DNS_CLASS_IN, DNS_TYPE_PTR, "53.0.0.127.in-addr.arpa"); |
| 271 |
ASSERT_NOT_NULL(rr); |
| 272 |
rr->ptr.name = strdup("_localdnsstub"); |
| 273 |
ASSERT_NOT_NULL(rr->ptr.name); |
| 274 |
ASSERT_TRUE(dns_answer_contains(answer, rr)); |
| 275 |
} |
| 276 |
|
| 277 |
TEST(dns_synthesize_answer_address_local_dns_proxy) { |
| 278 |
Manager manager = {}; |
| 279 |
_cleanup_(dns_question_unrefp) DnsQuestion *question = NULL; |
| 280 |
_cleanup_(dns_resource_key_unrefp) DnsResourceKey *key = NULL; |
| 281 |
_cleanup_(dns_answer_unrefp) DnsAnswer *answer = NULL; |
| 282 |
_cleanup_(dns_resource_record_unrefp) DnsResourceRecord *rr = NULL; |
| 283 |
|
| 284 |
question = dns_question_new(1); |
| 285 |
ASSERT_NOT_NULL(question); |
| 286 |
|
| 287 |
key = dns_resource_key_new(DNS_CLASS_IN, DNS_TYPE_PTR, "54.0.0.127.in-addr.arpa"); |
| 288 |
ASSERT_NOT_NULL(key); |
| 289 |
|
| 290 |
ASSERT_OK(dns_question_add(question, key, 0)); |
| 291 |
|
| 292 |
manager.full_hostname = (char *)"resolver.local"; |
| 293 |
manager.llmnr_hostname = (char *)"llmnr.resolver.local"; |
| 294 |
manager.mdns_hostname = (char *)"mdns.resolver.local"; |
| 295 |
|
| 296 |
ASSERT_TRUE(dns_synthesize_answer(&manager, question, 0, &answer)); |
| 297 |
|
| 298 |
rr = dns_resource_record_new_full(DNS_CLASS_IN, DNS_TYPE_PTR, "54.0.0.127.in-addr.arpa"); |
| 299 |
ASSERT_NOT_NULL(rr); |
| 300 |
rr->ptr.name = strdup("_localdnsproxy"); |
| 301 |
ASSERT_NOT_NULL(rr->ptr.name); |
| 302 |
ASSERT_TRUE(dns_answer_contains(answer, rr)); |
| 303 |
} |
| 304 |
|
| 305 |
DEFINE_TEST_MAIN(LOG_DEBUG); |