• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

systemd / systemd / 16533846256

25 Jul 2025 07:44PM UTC coverage: 72.199% (+0.03%) from 72.165%
16533846256

push

github

bluca
bootctl: automatically set --graceful when running in chroot

Installing stuff in a chroot should not fail because efivars are
not available. When running in a container touching efivars is
completely disabled, but there are some cases (recovery) where
it is needed to touch them in a chroot, so don't disable them but
avoid failing the run instead.

1 of 3 new or added lines in 1 file covered. (33.33%)

223 existing lines in 36 files now uncovered.

302658 of 419200 relevant lines covered (72.2%)

732672.88 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

98.72
/src/resolve/test-dns-cache.c
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-type.h"
10
#include "fd-util.h"
11
#include "fileio.h"
12
#include "resolve-util.h"
13
#include "resolved-def.h"
14
#include "resolved-dns-answer.h"
15
#include "resolved-dns-cache.h"
16
#include "resolved-dns-dnssec.h"
17
#include "resolved-dns-packet.h"
18
#include "resolved-dns-rr.h"
19
#include "tests.h"
20
#include "time-util.h"
21
#include "tmpfile-util.h"
22

23
static DnsCache new_cache(void) {
42✔
24
        return (DnsCache) {};
42✔
25
}
26

27
typedef struct PutArgs {
28
        DnsCacheMode cache_mode;
29
        DnsProtocol protocol;
30
        DnsResourceKey *key;
31
        int rcode;
32
        DnsAnswer *answer;
33
        DnsPacket *full_packet;
34
        uint64_t query_flags;
35
        DnssecResult dnssec_result;
36
        uint32_t nsec_ttl;
37
        int owner_family;
38
        const union in_addr_union owner_address;
39
        usec_t stale_retention_usec;
40
} PutArgs;
41

42
static PutArgs mk_put_args(void) {
47✔
43
        PutArgs put_args = {
47✔
44
                .cache_mode = DNS_CACHE_MODE_YES,
45
                .protocol = DNS_PROTOCOL_DNS,
46
                .key = NULL,
47
                .rcode = DNS_RCODE_SUCCESS,
48
                .answer = dns_answer_new(0),
47✔
49
                .full_packet = NULL,
50
                .query_flags = SD_RESOLVED_AUTHENTICATED | SD_RESOLVED_CONFIDENTIAL,
51
                .dnssec_result = DNSSEC_UNSIGNED,
52
                .nsec_ttl = 3600,
53
                .owner_family = AF_INET,
54
                .owner_address = { .in.s_addr = htobe32(0x01020304) },
47✔
55
                .stale_retention_usec = 0
56
        };
57

58
        ASSERT_NOT_NULL(put_args.answer);
47✔
59
        return put_args;
47✔
60
}
61

62
static int cache_put(DnsCache *cache, PutArgs *args) {
48✔
63
        ASSERT_NOT_NULL(cache);
48✔
64
        ASSERT_NOT_NULL(args);
48✔
65

66
        return dns_cache_put(cache,
48✔
67
                args->cache_mode,
68
                args->protocol,
69
                args->key,
70
                args->rcode,
71
                args->answer,
72
                args->full_packet,
73
                args->query_flags,
74
                args->dnssec_result,
75
                args->nsec_ttl,
76
                args->owner_family,
77
                &args->owner_address,
78
                args->stale_retention_usec);
79
}
80

81
static void dns_cache_unrefp(DnsCache *cache) {
42✔
82
        dns_cache_flush(cache);
42✔
83
}
42✔
84

85
static void put_args_unrefp(PutArgs *args) {
47✔
86
        ASSERT_NOT_NULL(args);
47✔
87

88
        dns_resource_key_unref(args->key);
47✔
89
        dns_answer_unref(args->answer);
47✔
90
        dns_packet_unref(args->full_packet);
47✔
91
}
47✔
92

93
static char* checked_strdup(const char *str) {
5✔
94
        char *copy = strdup(str);
5✔
95
        ASSERT_NOT_NULL(copy);
5✔
96
        return copy;
5✔
97
}
98

99
static void answer_add_a(PutArgs *args, DnsResourceKey *key, int addr, int ttl, DnsAnswerFlags flags) {
41✔
100
        _cleanup_(dns_resource_record_unrefp) DnsResourceRecord *rr = NULL;
×
101

102
        rr = dns_resource_record_new(key);
41✔
103
        ASSERT_NOT_NULL(rr);
41✔
104
        rr->a.in_addr.s_addr = htobe32(addr);
41✔
105
        rr->ttl = ttl;
41✔
106
        dns_answer_add(args->answer, rr, 1, flags, NULL);
41✔
107
}
41✔
108

109
static void answer_add_cname(PutArgs *args, DnsResourceKey *key, const char *alias, int ttl, DnsAnswerFlags flags) {
3✔
110
        _cleanup_(dns_resource_record_unrefp) DnsResourceRecord *rr = NULL;
×
111

112
        rr = dns_resource_record_new(key);
3✔
113
        ASSERT_NOT_NULL(rr);
3✔
114
        rr->cname.name = checked_strdup(alias);
3✔
115
        rr->ttl = ttl;
3✔
116
        dns_answer_add(args->answer, rr, 1, flags, NULL);
3✔
117
}
3✔
118

119
static void answer_add_opt(PutArgs *args, DnsResourceKey *key, int ttl, DnsAnswerFlags flags) {
2✔
120
        _cleanup_(dns_resource_record_unrefp) DnsResourceRecord *rr = NULL;
×
121

122
        rr = dns_resource_record_new(key);
2✔
123
        ASSERT_NOT_NULL(rr);
2✔
124
        rr->opt.data_size = 0;
2✔
125
        rr->ttl = ttl;
2✔
126
        dns_answer_add(args->answer, rr, 1, flags, NULL);
2✔
127
}
2✔
128

129
#define BY_IDX(json, idx) sd_json_variant_by_index(json, idx)
130
#define BY_KEY(json, key) sd_json_variant_by_key(json, key)
131
#define INTVAL(json) sd_json_variant_integer(json)
132
#define STRVAL(json) sd_json_variant_string(json)
133

134
/* ================================================================
135
 * dns_cache_put()
136
 * ================================================================ */
137

138
TEST(dns_a_success_is_cached) {
1✔
139
        _cleanup_(dns_cache_unrefp) DnsCache cache = new_cache();
1✔
140
        _cleanup_(put_args_unrefp) PutArgs put_args = mk_put_args();
2✔
141

142
        put_args.key = dns_resource_key_new(DNS_CLASS_IN, DNS_TYPE_A, "www.example.com");
1✔
143
        ASSERT_NOT_NULL(put_args.key);
1✔
144
        put_args.rcode = DNS_RCODE_SUCCESS;
1✔
145
        answer_add_a(&put_args, put_args.key, 0xc0a8017f, 3600, DNS_ANSWER_CACHEABLE);
1✔
146

147
        ASSERT_OK(cache_put(&cache, &put_args));
1✔
148
        ASSERT_FALSE(dns_cache_is_empty(&cache));
1✔
149
}
1✔
150

151
TEST(dns_a_success_non_matching_type_is_cached) {
1✔
152
        _cleanup_(dns_cache_unrefp) DnsCache cache = new_cache();
1✔
153
        _cleanup_(put_args_unrefp) PutArgs put_args = mk_put_args();
2✔
154
        _cleanup_(dns_resource_key_unrefp) DnsResourceKey *key = NULL;
1✔
155

156
        put_args.key = dns_resource_key_new(DNS_CLASS_IN, DNS_TYPE_A, "www.example.com");
1✔
157
        ASSERT_NOT_NULL(put_args.key);
1✔
158
        put_args.rcode = DNS_RCODE_SUCCESS;
1✔
159

160
        key = dns_resource_key_new(DNS_CLASS_IN, DNS_TYPE_AAAA, "www.example.com");
1✔
161
        ASSERT_NOT_NULL(key);
1✔
162
        answer_add_a(&put_args, key, 0xc0a8017f, 3600, DNS_ANSWER_CACHEABLE);
1✔
163

164
        ASSERT_OK(cache_put(&cache, &put_args));
1✔
165
        ASSERT_FALSE(dns_cache_is_empty(&cache));
1✔
166
}
1✔
167

168
TEST(dns_a_success_non_matching_name_is_cached) {
1✔
169
        _cleanup_(dns_cache_unrefp) DnsCache cache = new_cache();
1✔
170
        _cleanup_(put_args_unrefp) PutArgs put_args = mk_put_args();
2✔
171
        _cleanup_(dns_resource_key_unrefp) DnsResourceKey *key = NULL;
1✔
172

173
        put_args.key = dns_resource_key_new(DNS_CLASS_IN, DNS_TYPE_A, "www.example.com");
1✔
174
        ASSERT_NOT_NULL(put_args.key);
1✔
175
        put_args.rcode = DNS_RCODE_SUCCESS;
1✔
176

177
        key = dns_resource_key_new(DNS_CLASS_IN, DNS_TYPE_A, "example.com");
1✔
178
        ASSERT_NOT_NULL(key);
1✔
179
        answer_add_a(&put_args, key, 0xc0a8017f, 3600, DNS_ANSWER_CACHEABLE);
1✔
180

181
        ASSERT_OK(cache_put(&cache, &put_args));
1✔
182
        ASSERT_FALSE(dns_cache_is_empty(&cache));
1✔
183
}
1✔
184

185
TEST(dns_a_success_mdns_no_key_is_cached) {
1✔
186
        _cleanup_(dns_cache_unrefp) DnsCache cache = new_cache();
1✔
187
        _cleanup_(put_args_unrefp) PutArgs put_args = mk_put_args();
2✔
188
        _cleanup_(dns_resource_key_unrefp) DnsResourceKey *key = NULL;
1✔
189

190
        put_args.protocol = DNS_PROTOCOL_MDNS;
1✔
191
        put_args.rcode = DNS_RCODE_SUCCESS;
1✔
192

193
        key = dns_resource_key_new(DNS_CLASS_IN, DNS_TYPE_A, "example.com");
1✔
194
        ASSERT_NOT_NULL(key);
1✔
195
        answer_add_a(&put_args, key, 0xc0a8017f, 3600, DNS_ANSWER_CACHEABLE);
1✔
196

197
        ASSERT_OK(cache_put(&cache, &put_args));
1✔
198
        ASSERT_FALSE(dns_cache_is_empty(&cache));
1✔
199
}
1✔
200

201
TEST(dns_a_success_mdns_update_existing) {
1✔
202
        _cleanup_(dns_cache_unrefp) DnsCache cache = new_cache();
1✔
203
        _cleanup_(put_args_unrefp) PutArgs args1 = mk_put_args(), args2 = mk_put_args();
2✔
204
        DnsResourceKey *key = NULL;
1✔
205

206
        args1.protocol = DNS_PROTOCOL_MDNS;
1✔
207
        args1.rcode = DNS_RCODE_SUCCESS;
1✔
208
        key = dns_resource_key_new(DNS_CLASS_IN, DNS_TYPE_A, "example.com");
1✔
209
        ASSERT_NOT_NULL(key);
1✔
210
        answer_add_a(&args1, key, 0xc0a8017f, 3600, DNS_ANSWER_CACHEABLE | DNS_ANSWER_SHARED_OWNER);
1✔
211
        dns_resource_key_unref(key);
1✔
212

213
        ASSERT_OK(cache_put(&cache, &args1));
1✔
214

215
        args2.protocol = DNS_PROTOCOL_MDNS;
1✔
216
        args2.rcode = DNS_RCODE_SUCCESS;
1✔
217
        key = dns_resource_key_new(DNS_CLASS_IN, DNS_TYPE_A, "example.com");
1✔
218
        ASSERT_NOT_NULL(key);
1✔
219
        answer_add_a(&args2, key, 0xc0a8017f, 2400, DNS_ANSWER_CACHEABLE | DNS_ANSWER_SHARED_OWNER);
1✔
220
        dns_resource_key_unref(key);
1✔
221

222
        ASSERT_OK(cache_put(&cache, &args2));
1✔
223

224
        ASSERT_EQ(dns_cache_size(&cache), 1u);
1✔
225
}
1✔
226

227
TEST(dns_a_success_mdns_zero_ttl_removes_existing) {
1✔
228
        _cleanup_(dns_cache_unrefp) DnsCache cache = new_cache();
1✔
229
        _cleanup_(put_args_unrefp) PutArgs args1 = mk_put_args(), args2 = mk_put_args();
2✔
230
        DnsResourceKey *key = NULL;
1✔
231

232
        args1.protocol = DNS_PROTOCOL_MDNS;
1✔
233
        args1.rcode = DNS_RCODE_SUCCESS;
1✔
234
        key = dns_resource_key_new(DNS_CLASS_IN, DNS_TYPE_A, "example.com");
1✔
235
        ASSERT_NOT_NULL(key);
1✔
236
        answer_add_a(&args1, key, 0xc0a8017f, 3600, DNS_ANSWER_CACHEABLE | DNS_ANSWER_SHARED_OWNER);
1✔
237
        dns_resource_key_unref(key);
1✔
238

239
        ASSERT_OK(cache_put(&cache, &args1));
1✔
240

241
        args2.protocol = DNS_PROTOCOL_MDNS;
1✔
242
        args2.rcode = DNS_RCODE_SUCCESS;
1✔
243
        key = dns_resource_key_new(DNS_CLASS_IN, DNS_TYPE_A, "example.com");
1✔
244
        ASSERT_NOT_NULL(key);
1✔
245
        answer_add_a(&args2, key, 0xc0a8017f, 0, DNS_ANSWER_CACHEABLE | DNS_ANSWER_SHARED_OWNER);
1✔
246
        dns_resource_key_unref(key);
1✔
247

248
        ASSERT_OK(cache_put(&cache, &args2));
1✔
249

250
        ASSERT_TRUE(dns_cache_is_empty(&cache));
1✔
251
}
1✔
252

253
TEST(dns_a_success_mdns_same_key_different_payloads) {
1✔
254
        _cleanup_(dns_cache_unrefp) DnsCache cache = new_cache();
1✔
255
        _cleanup_(put_args_unrefp) PutArgs put_args = mk_put_args();
2✔
256
        DnsResourceKey *key = NULL;
1✔
257

258
        put_args.protocol = DNS_PROTOCOL_MDNS;
1✔
259
        put_args.rcode = DNS_RCODE_SUCCESS;
1✔
260

261
        key = dns_resource_key_new(DNS_CLASS_IN, DNS_TYPE_A, "example.com");
1✔
262
        ASSERT_NOT_NULL(key);
1✔
263
        answer_add_a(&put_args, key, 0xc0a8017f, 3600, DNS_ANSWER_CACHEABLE | DNS_ANSWER_SHARED_OWNER);
1✔
264
        dns_resource_key_unref(key);
1✔
265

266
        key = dns_resource_key_new(DNS_CLASS_IN, DNS_TYPE_A, "example.com");
1✔
267
        ASSERT_NOT_NULL(key);
1✔
268
        answer_add_a(&put_args, key, 0x7f01a8cc, 2400, DNS_ANSWER_CACHEABLE | DNS_ANSWER_SHARED_OWNER);
1✔
269
        dns_resource_key_unref(key);
1✔
270

271
        ASSERT_EQ(dns_answer_size(put_args.answer), 2u);
2✔
272

273
        ASSERT_OK(cache_put(&cache, &put_args));
1✔
274
        ASSERT_EQ(dns_cache_size(&cache), 1u);
1✔
275
}
1✔
276

277
TEST(dns_a_success_escaped_key_returns_error) {
1✔
278
        _cleanup_(dns_cache_unrefp) DnsCache cache = new_cache();
1✔
279
        _cleanup_(put_args_unrefp) PutArgs put_args = mk_put_args();
2✔
280
        _cleanup_(dns_resource_key_unrefp) DnsResourceKey *key = NULL;
1✔
281

282
        put_args.key = dns_resource_key_new(DNS_CLASS_IN, DNS_TYPE_A, "www.example.com");
1✔
283
        ASSERT_NOT_NULL(put_args.key);
1✔
284
        put_args.rcode = DNS_RCODE_SUCCESS;
1✔
285

286
        key = dns_resource_key_new(DNS_CLASS_IN, DNS_TYPE_A, "www.\\example.com");
1✔
287
        ASSERT_NOT_NULL(key);
1✔
288
        answer_add_a(&put_args, key, 0xc0a8017f, 3600, DNS_ANSWER_CACHEABLE);
1✔
289

290
        ASSERT_ERROR(cache_put(&cache, &put_args), EINVAL);
1✔
291
        ASSERT_TRUE(dns_cache_is_empty(&cache));
1✔
292
}
1✔
293

294
TEST(dns_a_success_empty_answer_is_not_cached) {
1✔
295
        _cleanup_(dns_cache_unrefp) DnsCache cache = new_cache();
1✔
296
        _cleanup_(put_args_unrefp) PutArgs put_args = mk_put_args();
2✔
297

298
        put_args.key = dns_resource_key_new(DNS_CLASS_IN, DNS_TYPE_A, "www.example.com");
1✔
299
        ASSERT_NOT_NULL(put_args.key);
1✔
300
        put_args.rcode = DNS_RCODE_SUCCESS;
1✔
301

302
        ASSERT_OK(cache_put(&cache, &put_args));
1✔
303
        ASSERT_TRUE(dns_cache_is_empty(&cache));
1✔
304
}
1✔
305

306
TEST(dns_a_success_any_class_is_not_cached) {
1✔
307
        _cleanup_(dns_cache_unrefp) DnsCache cache = new_cache();
1✔
308
        _cleanup_(put_args_unrefp) PutArgs put_args = mk_put_args();
2✔
309

310
        put_args.key = dns_resource_key_new(DNS_CLASS_ANY, DNS_TYPE_A, "www.example.com");
1✔
311
        ASSERT_NOT_NULL(put_args.key);
1✔
312
        put_args.rcode = DNS_RCODE_SUCCESS;
1✔
313
        answer_add_a(&put_args, put_args.key, 0xc0a8017f, 3600, DNS_ANSWER_CACHEABLE);
1✔
314

315
        ASSERT_OK(cache_put(&cache, &put_args));
1✔
316
        ASSERT_TRUE(dns_cache_is_empty(&cache));
1✔
317
}
1✔
318

319
TEST(dns_a_success_any_type_not_cached) {
1✔
320
        _cleanup_(dns_cache_unrefp) DnsCache cache = new_cache();
1✔
321
        _cleanup_(put_args_unrefp) PutArgs put_args = mk_put_args();
2✔
322

323
        put_args.key = dns_resource_key_new(DNS_CLASS_IN, DNS_TYPE_ANY, "www.example.com");
1✔
324
        ASSERT_NOT_NULL(put_args.key);
1✔
325
        put_args.rcode = DNS_RCODE_SUCCESS;
1✔
326
        answer_add_opt(&put_args, put_args.key, 3600, DNS_ANSWER_CACHEABLE);
1✔
327

328
        ASSERT_OK(cache_put(&cache, &put_args));
1✔
329
        ASSERT_TRUE(dns_cache_is_empty(&cache));
1✔
330
}
1✔
331

332
TEST(dns_a_success_opt_not_cached) {
1✔
333
        _cleanup_(dns_cache_unrefp) DnsCache cache = new_cache();
1✔
334
        _cleanup_(put_args_unrefp) PutArgs put_args = mk_put_args();
2✔
335

336
        put_args.key = dns_resource_key_new(DNS_CLASS_IN, DNS_TYPE_OPT, "www.example.com");
1✔
337
        ASSERT_NOT_NULL(put_args.key);
1✔
338
        put_args.rcode = DNS_RCODE_SUCCESS;
1✔
339
        answer_add_opt(&put_args, put_args.key, 3600, DNS_ANSWER_CACHEABLE);
1✔
340

341
        ASSERT_OK(cache_put(&cache, &put_args));
1✔
342
        ASSERT_TRUE(dns_cache_is_empty(&cache));
1✔
343
}
1✔
344

345
TEST(dns_a_nxdomain_is_cached) {
1✔
346
        _cleanup_(dns_cache_unrefp) DnsCache cache = new_cache();
1✔
347
        _cleanup_(put_args_unrefp) PutArgs put_args = mk_put_args();
2✔
348

349
        put_args.key = dns_resource_key_new(DNS_CLASS_IN, DNS_TYPE_A, "www.example.com");
1✔
350
        ASSERT_NOT_NULL(put_args.key);
1✔
351
        put_args.rcode = DNS_RCODE_NXDOMAIN;
1✔
352
        dns_answer_add_soa(put_args.answer, "example.com", 3600, 0);
1✔
353

354
        ASSERT_OK(cache_put(&cache, &put_args));
1✔
355
        ASSERT_FALSE(dns_cache_is_empty(&cache));
1✔
356
}
1✔
357

358
TEST(dns_a_nxdomain_no_soa_not_cached) {
1✔
359
        _cleanup_(dns_cache_unrefp) DnsCache cache = new_cache();
1✔
360
        _cleanup_(put_args_unrefp) PutArgs put_args = mk_put_args();
2✔
361

362
        put_args.key = dns_resource_key_new(DNS_CLASS_IN, DNS_TYPE_A, "www.example.com");
1✔
363
        ASSERT_NOT_NULL(put_args.key);
1✔
364
        put_args.rcode = DNS_RCODE_NXDOMAIN;
1✔
365

366
        ASSERT_OK(cache_put(&cache, &put_args));
1✔
367
        ASSERT_TRUE(dns_cache_is_empty(&cache));
1✔
368
}
1✔
369

370
TEST(dns_a_nxdomain_any_class_is_not_cached) {
1✔
371
        _cleanup_(dns_cache_unrefp) DnsCache cache = new_cache();
1✔
372
        _cleanup_(put_args_unrefp) PutArgs put_args = mk_put_args();
2✔
373

374
        put_args.key = dns_resource_key_new(DNS_CLASS_ANY, DNS_TYPE_A, "www.example.com");
1✔
375
        ASSERT_NOT_NULL(put_args.key);
1✔
376
        put_args.rcode = DNS_RCODE_NXDOMAIN;
1✔
377
        dns_answer_add_soa(put_args.answer, "example.com", 3600, 0);
1✔
378

379
        ASSERT_OK(cache_put(&cache, &put_args));
1✔
380
        ASSERT_TRUE(dns_cache_is_empty(&cache));
1✔
381
}
1✔
382

383
TEST(dns_a_nxdomain_any_type_not_cached) {
1✔
384
        _cleanup_(dns_cache_unrefp) DnsCache cache = new_cache();
1✔
385
        _cleanup_(put_args_unrefp) PutArgs put_args = mk_put_args();
2✔
386

387
        put_args.key = dns_resource_key_new(DNS_CLASS_IN, DNS_TYPE_ANY, "www.example.com");
1✔
388
        ASSERT_NOT_NULL(put_args.key);
1✔
389
        put_args.rcode = DNS_RCODE_NXDOMAIN;
1✔
390
        dns_answer_add_soa(put_args.answer, "example.com", 3600, 0);
1✔
391

392
        ASSERT_OK(cache_put(&cache, &put_args));
1✔
393
        ASSERT_TRUE(dns_cache_is_empty(&cache));
1✔
394
}
1✔
395

396
TEST(dns_a_nxdomain_opt_not_cached) {
1✔
397
        _cleanup_(dns_cache_unrefp) DnsCache cache = new_cache();
1✔
398
        _cleanup_(put_args_unrefp) PutArgs put_args = mk_put_args();
2✔
399

400
        put_args.key = dns_resource_key_new(DNS_CLASS_IN, DNS_TYPE_OPT, "www.example.com");
1✔
401
        ASSERT_NOT_NULL(put_args.key);
1✔
402
        put_args.rcode = DNS_RCODE_NXDOMAIN;
1✔
403
        dns_answer_add_soa(put_args.answer, "example.com", 3600, 0);
1✔
404

405
        ASSERT_OK(cache_put(&cache, &put_args));
1✔
406
        ASSERT_TRUE(dns_cache_is_empty(&cache));
1✔
407
}
1✔
408

409
TEST(dns_a_servfail_is_cached) {
1✔
410
        _cleanup_(dns_cache_unrefp) DnsCache cache = new_cache();
1✔
411
        _cleanup_(put_args_unrefp) PutArgs put_args = mk_put_args();
2✔
412

413
        put_args.key = dns_resource_key_new(DNS_CLASS_IN, DNS_TYPE_A, "www.example.com");
1✔
414
        ASSERT_NOT_NULL(put_args.key);
1✔
415
        put_args.rcode = DNS_RCODE_SERVFAIL;
1✔
416

417
        ASSERT_OK(cache_put(&cache, &put_args));
1✔
418
        ASSERT_FALSE(dns_cache_is_empty(&cache));
1✔
419
}
1✔
420

421
TEST(dns_a_refused_is_not_cached) {
1✔
422
        _cleanup_(dns_cache_unrefp) DnsCache cache = new_cache();
1✔
423
        _cleanup_(put_args_unrefp) PutArgs put_args = mk_put_args();
2✔
424

425
        put_args.key = dns_resource_key_new(DNS_CLASS_IN, DNS_TYPE_A, "www.example.com");
1✔
426
        ASSERT_NOT_NULL(put_args.key);
1✔
427
        put_args.rcode = DNS_RCODE_REFUSED;
1✔
428

429
        ASSERT_OK(cache_put(&cache, &put_args));
1✔
430
        ASSERT_TRUE(dns_cache_is_empty(&cache));
1✔
431
}
1✔
432

433
TEST(dns_a_success_zero_ttl_is_not_cached) {
1✔
434
        _cleanup_(dns_cache_unrefp) DnsCache cache = new_cache();
1✔
435
        _cleanup_(put_args_unrefp) PutArgs put_args = mk_put_args();
2✔
436

437
        put_args.key = dns_resource_key_new(DNS_CLASS_IN, DNS_TYPE_A, "www.example.com");
1✔
438
        ASSERT_NOT_NULL(put_args.key);
1✔
439
        put_args.rcode = DNS_RCODE_SUCCESS;
1✔
440
        answer_add_a(&put_args, put_args.key, 0xc0a8017f, 0, DNS_ANSWER_CACHEABLE);
1✔
441

442
        ASSERT_OK(cache_put(&cache, &put_args));
1✔
443
        ASSERT_TRUE(dns_cache_is_empty(&cache));
1✔
444
}
1✔
445

446
TEST(dns_a_success_zero_ttl_removes_existing_entry) {
1✔
447
        _cleanup_(dns_cache_unrefp) DnsCache cache = new_cache();
1✔
448
        _cleanup_(put_args_unrefp) PutArgs put_args = mk_put_args();
2✔
449

450
        put_args.key = dns_resource_key_new(DNS_CLASS_IN, DNS_TYPE_A, "www.example.com");
1✔
451
        ASSERT_NOT_NULL(put_args.key);
1✔
452
        put_args.rcode = DNS_RCODE_SUCCESS;
1✔
453
        answer_add_a(&put_args, put_args.key, 0xc0a8017f, 3600, DNS_ANSWER_CACHEABLE);
1✔
454

455
        ASSERT_OK(cache_put(&cache, &put_args));
1✔
456
        ASSERT_FALSE(dns_cache_is_empty(&cache));
1✔
457

458
        dns_answer_unref(put_args.answer);
1✔
459
        put_args.answer = dns_answer_new(1);
1✔
460
        ASSERT_NOT_NULL(put_args.answer);
1✔
461
        answer_add_a(&put_args, put_args.key, 0xc0a8017f, 0, DNS_ANSWER_CACHEABLE);
1✔
462

463
        ASSERT_OK(cache_put(&cache, &put_args));
1✔
464
        ASSERT_TRUE(dns_cache_is_empty(&cache));
1✔
465
}
1✔
466

467
TEST(dns_a_success_not_cacheable_is_not_cached) {
1✔
468
        _cleanup_(dns_cache_unrefp) DnsCache cache = new_cache();
1✔
469
        _cleanup_(put_args_unrefp) PutArgs put_args = mk_put_args();
2✔
470

471
        put_args.key = dns_resource_key_new(DNS_CLASS_IN, DNS_TYPE_A, "www.example.com");
1✔
472
        ASSERT_NOT_NULL(put_args.key);
1✔
473
        put_args.rcode = DNS_RCODE_SUCCESS;
1✔
474
        answer_add_a(&put_args, put_args.key, 0xc0a8017f, 3600, 0);
1✔
475

476
        ASSERT_OK(cache_put(&cache, &put_args));
1✔
477
        ASSERT_TRUE(dns_cache_is_empty(&cache));
1✔
478
}
1✔
479

480
TEST(dns_a_to_cname_success_is_cached) {
1✔
481
        _cleanup_(dns_cache_unrefp) DnsCache cache = new_cache();
1✔
482
        _cleanup_(put_args_unrefp) PutArgs put_args = mk_put_args();
2✔
483
        _cleanup_(dns_resource_key_unrefp) DnsResourceKey *key = NULL;
1✔
484

485
        put_args.key = dns_resource_key_new(DNS_CLASS_IN, DNS_TYPE_A, "www.example.com");
1✔
486
        ASSERT_NOT_NULL(put_args.key);
1✔
487
        put_args.rcode = DNS_RCODE_SUCCESS;
1✔
488

489
        key = dns_resource_key_new(DNS_CLASS_IN, DNS_TYPE_CNAME, "www.example.com");
1✔
490
        ASSERT_NOT_NULL(key);
1✔
491
        answer_add_cname(&put_args, key, "example.com", 3600, DNS_ANSWER_CACHEABLE);
1✔
492

493
        ASSERT_OK(cache_put(&cache, &put_args));
1✔
494
        ASSERT_FALSE(dns_cache_is_empty(&cache));
1✔
495
}
1✔
496

497
TEST(dns_a_to_cname_success_escaped_name_returns_error) {
1✔
498
        _cleanup_(dns_cache_unrefp) DnsCache cache = new_cache();
1✔
499
        _cleanup_(put_args_unrefp) PutArgs put_args = mk_put_args();
2✔
500
        _cleanup_(dns_resource_key_unrefp) DnsResourceKey *key = NULL;
1✔
501

502
        put_args.key = dns_resource_key_new(DNS_CLASS_IN, DNS_TYPE_A, "www.example.com");
1✔
503
        ASSERT_NOT_NULL(put_args.key);
1✔
504
        put_args.rcode = DNS_RCODE_SUCCESS;
1✔
505

506
        key = dns_resource_key_new(DNS_CLASS_IN, DNS_TYPE_CNAME, "www.\\example.com");
1✔
507
        ASSERT_NOT_NULL(key);
1✔
508
        answer_add_cname(&put_args, key, "example.com", 3600, DNS_ANSWER_CACHEABLE);
1✔
509

510
        ASSERT_ERROR(cache_put(&cache, &put_args), EINVAL);
1✔
511
        ASSERT_TRUE(dns_cache_is_empty(&cache));
1✔
512
}
1✔
513

514
/* ================================================================
515
 * dns_cache_lookup()
516
 * ================================================================ */
517

518
TEST(dns_cache_lookup_miss) {
1✔
519
        _cleanup_(dns_cache_unrefp) DnsCache cache = new_cache();
1✔
520
        _cleanup_(dns_answer_unrefp) DnsAnswer *ret_answer = NULL;
1✔
521
        _cleanup_(dns_packet_unrefp) DnsPacket *ret_full_packet = NULL;
1✔
522
        _cleanup_(dns_resource_key_unrefp) DnsResourceKey *key = NULL;
×
523
        int query_flags, ret_rcode;
1✔
524
        uint64_t ret_query_flags;
1✔
525

526
        key = dns_resource_key_new(DNS_CLASS_IN, DNS_TYPE_A, "www.example.com");
1✔
527
        ASSERT_NOT_NULL(key);
1✔
528
        query_flags = 0;
1✔
529
        ASSERT_FALSE(dns_cache_lookup(&cache, key, query_flags, &ret_rcode, &ret_answer, &ret_full_packet, &ret_query_flags, NULL));
1✔
530

531
        ASSERT_EQ(cache.n_hit, 0u);
1✔
532
        ASSERT_EQ(cache.n_miss, 1u);
1✔
533

534
        ASSERT_EQ(ret_rcode, DNS_RCODE_SUCCESS);
1✔
535
        ASSERT_EQ(ret_query_flags, 0u);
1✔
536

537
        ASSERT_EQ(dns_answer_size(ret_answer), 0u);
1✔
538
}
1✔
539

540
TEST(dns_cache_lookup_success) {
1✔
541
        _cleanup_(dns_cache_unrefp) DnsCache cache = new_cache();
1✔
542
        _cleanup_(put_args_unrefp) PutArgs put_args = mk_put_args();
2✔
543
        _cleanup_(dns_answer_unrefp) DnsAnswer *ret_answer = NULL;
1✔
544
        _cleanup_(dns_packet_unrefp) DnsPacket *ret_full_packet = NULL;
1✔
545
        _cleanup_(dns_resource_record_unrefp) DnsResourceRecord *rr = NULL;
1✔
546
        _cleanup_(dns_resource_key_unrefp) DnsResourceKey *key = NULL;
1✔
547
        int query_flags, ret_rcode;
1✔
548
        uint64_t ret_query_flags;
1✔
549

550
        put_args.key = dns_resource_key_new(DNS_CLASS_IN, DNS_TYPE_A, "www.example.com");
1✔
551
        ASSERT_NOT_NULL(put_args.key);
1✔
552
        put_args.rcode = DNS_RCODE_SUCCESS;
1✔
553
        answer_add_a(&put_args, put_args.key, 0xc0a8017f, 3600, DNS_ANSWER_CACHEABLE);
1✔
554
        cache_put(&cache, &put_args);
1✔
555

556
        ASSERT_EQ(dns_cache_size(&cache), 1u);
1✔
557

558
        key = dns_resource_key_new(DNS_CLASS_IN, DNS_TYPE_A, "www.example.com");
1✔
559
        ASSERT_NOT_NULL(key);
1✔
560
        query_flags = 0;
1✔
561
        ASSERT_OK_POSITIVE(dns_cache_lookup(&cache, key, query_flags, &ret_rcode, &ret_answer, &ret_full_packet, &ret_query_flags, NULL));
1✔
562

563
        ASSERT_EQ(cache.n_hit, 1u);
1✔
564
        ASSERT_EQ(cache.n_miss, 0u);
1✔
565

566
        ASSERT_EQ(ret_rcode, DNS_RCODE_SUCCESS);
1✔
567
        ASSERT_EQ(ret_query_flags, SD_RESOLVED_CONFIDENTIAL);
1✔
568

569
        ASSERT_EQ(dns_answer_size(ret_answer), 1u);
2✔
570

571
        rr = dns_resource_record_new_full(DNS_CLASS_IN, DNS_TYPE_A, "www.example.com");
1✔
572
        ASSERT_NOT_NULL(rr);
1✔
573
        rr->a.in_addr.s_addr = htobe32(0xc0a8017f);
1✔
574
        ASSERT_TRUE(dns_answer_contains(ret_answer, rr));
1✔
575
}
1✔
576

577
TEST(dns_cache_lookup_clamp_ttl) {
1✔
578
        _cleanup_(dns_cache_unrefp) DnsCache cache = new_cache();
1✔
579
        _cleanup_(put_args_unrefp) PutArgs put_args = mk_put_args();
2✔
580
        _cleanup_(dns_answer_unrefp) DnsAnswer *ret_answer = NULL;
1✔
581
        _cleanup_(dns_packet_unrefp) DnsPacket *ret_full_packet = NULL;
1✔
582
        _cleanup_(dns_resource_record_unrefp) DnsResourceRecord *rr = NULL;
1✔
583
        _cleanup_(dns_resource_key_unrefp) DnsResourceKey *key = NULL;
1✔
584
        int query_flags, ret_rcode;
1✔
585
        uint64_t ret_query_flags;
1✔
586

587
        put_args.key = dns_resource_key_new(DNS_CLASS_IN, DNS_TYPE_A, "www.example.com");
1✔
588
        ASSERT_NOT_NULL(put_args.key);
1✔
589
        put_args.rcode = DNS_RCODE_SUCCESS;
1✔
590
        answer_add_a(&put_args, put_args.key, 0xc0a8017f, 3600, DNS_ANSWER_CACHEABLE);
1✔
591
        cache_put(&cache, &put_args);
1✔
592

593
        ASSERT_EQ(dns_cache_size(&cache), 1u);
1✔
594

595
        key = dns_resource_key_new(DNS_CLASS_IN, DNS_TYPE_A, "www.example.com");
1✔
596
        ASSERT_NOT_NULL(key);
1✔
597
        query_flags = SD_RESOLVED_CLAMP_TTL;
1✔
598
        ASSERT_OK_POSITIVE(dns_cache_lookup(&cache, key, query_flags, &ret_rcode, &ret_answer, &ret_full_packet, &ret_query_flags, NULL));
1✔
599

600
        ASSERT_EQ(cache.n_hit, 1u);
1✔
601
        ASSERT_EQ(cache.n_miss, 0u);
1✔
602

603
        ASSERT_EQ(ret_rcode, DNS_RCODE_SUCCESS);
1✔
604
        ASSERT_EQ(ret_query_flags, SD_RESOLVED_CONFIDENTIAL);
1✔
605

606
        ASSERT_EQ(dns_answer_size(ret_answer), 1u);
2✔
607

608
        rr = dns_resource_record_new_full(DNS_CLASS_IN, DNS_TYPE_A, "www.example.com");
1✔
609
        ASSERT_NOT_NULL(rr);
1✔
610
        rr->a.in_addr.s_addr = htobe32(0xc0a8017f);
1✔
611
        ASSERT_TRUE(dns_answer_contains(ret_answer, rr));
1✔
612
}
1✔
613

614
TEST(dns_cache_lookup_returns_most_recent_response) {
1✔
615
        _cleanup_(dns_cache_unrefp) DnsCache cache = new_cache();
1✔
616
        _cleanup_(put_args_unrefp) PutArgs args1 = mk_put_args(), args2 = mk_put_args();
2✔
617
        _cleanup_(dns_answer_unrefp) DnsAnswer *ret_answer = NULL;
1✔
618
        _cleanup_(dns_packet_unrefp) DnsPacket *ret_full_packet = NULL;
1✔
619
        _cleanup_(dns_resource_key_unrefp) DnsResourceKey *key = NULL;
1✔
620
        DnsResourceRecord *rr = NULL;
1✔
621
        int query_flags, ret_rcode;
1✔
622
        uint64_t ret_query_flags;
1✔
623

624
        args1.key = dns_resource_key_new(DNS_CLASS_IN, DNS_TYPE_A, "www.example.com");
1✔
625
        ASSERT_NOT_NULL(args1.key);
1✔
626
        args1.rcode = DNS_RCODE_SUCCESS;
1✔
627
        answer_add_a(&args1, args1.key, 0xc0a8017f, 3600, DNS_ANSWER_CACHEABLE);
1✔
628
        cache_put(&cache, &args1);
1✔
629

630
        args2.key = dns_resource_key_new(DNS_CLASS_IN, DNS_TYPE_A, "www.example.com");
1✔
631
        ASSERT_NOT_NULL(args2.key);
1✔
632
        args2.rcode = DNS_RCODE_SUCCESS;
1✔
633
        answer_add_a(&args2, args2.key, 0x7f01a8c0, 2400, DNS_ANSWER_CACHEABLE);
1✔
634
        cache_put(&cache, &args2);
1✔
635

636
        ASSERT_EQ(dns_cache_size(&cache), 1u);
1✔
637

638
        key = dns_resource_key_new(DNS_CLASS_IN, DNS_TYPE_A, "www.example.com");
1✔
639
        ASSERT_NOT_NULL(key);
1✔
640
        query_flags = 0;
1✔
641
        ASSERT_OK_POSITIVE(dns_cache_lookup(&cache, key, query_flags, &ret_rcode, &ret_answer, &ret_full_packet, &ret_query_flags, NULL));
1✔
642

643
        ASSERT_EQ(cache.n_hit, 1u);
1✔
644
        ASSERT_EQ(cache.n_miss, 0u);
1✔
645

646
        ASSERT_EQ(ret_rcode, DNS_RCODE_SUCCESS);
1✔
647
        ASSERT_EQ(ret_query_flags, SD_RESOLVED_CONFIDENTIAL);
1✔
648

649
        ASSERT_EQ(dns_answer_size(ret_answer), 1u);
2✔
650

651
        rr = dns_resource_record_new_full(DNS_CLASS_IN, DNS_TYPE_A, "www.example.com");
1✔
652
        ASSERT_NOT_NULL(rr);
1✔
653
        rr->a.in_addr.s_addr = htobe32(0x7f01a8c0);
1✔
654
        ASSERT_TRUE(dns_answer_contains(ret_answer, rr));
1✔
655
        dns_resource_record_unref(rr);
1✔
656

657
        rr = dns_resource_record_new_full(DNS_CLASS_IN, DNS_TYPE_A, "www.example.com");
1✔
658
        ASSERT_NOT_NULL(rr);
1✔
659
        rr->a.in_addr.s_addr = htobe32(0xc0a8017f);
1✔
660
        ASSERT_FALSE(dns_answer_contains(ret_answer, rr));
1✔
661
        dns_resource_record_unref(rr);
1✔
662
}
1✔
663

664
TEST(dns_cache_lookup_retains_multiple_answers_from_one_response) {
1✔
665
        _cleanup_(dns_cache_unrefp) DnsCache cache = new_cache();
1✔
666
        _cleanup_(put_args_unrefp) PutArgs put_args = mk_put_args();
2✔
667
        _cleanup_(dns_answer_unrefp) DnsAnswer *ret_answer = NULL;
1✔
668
        _cleanup_(dns_packet_unrefp) DnsPacket *ret_full_packet = NULL;
1✔
669
        _cleanup_(dns_resource_key_unrefp) DnsResourceKey *key = NULL;
1✔
670
        DnsResourceRecord *rr = NULL;
1✔
671
        int query_flags, ret_rcode;
1✔
672
        uint64_t ret_query_flags;
1✔
673

674
        put_args.key = dns_resource_key_new(DNS_CLASS_IN, DNS_TYPE_A, "www.example.com");
1✔
675
        ASSERT_NOT_NULL(put_args.key);
1✔
676
        put_args.rcode = DNS_RCODE_SUCCESS;
1✔
677
        answer_add_a(&put_args, put_args.key, 0xc0a8017f, 3600, DNS_ANSWER_CACHEABLE);
1✔
678
        answer_add_a(&put_args, put_args.key, 0x7f01a8cc, 3600, DNS_ANSWER_CACHEABLE);
1✔
679
        cache_put(&cache, &put_args);
1✔
680

681
        ASSERT_EQ(dns_cache_size(&cache), 1u);
1✔
682

683
        key = dns_resource_key_new(DNS_CLASS_IN, DNS_TYPE_A, "www.example.com");
1✔
684
        ASSERT_NOT_NULL(key);
1✔
685
        query_flags = 0;
1✔
686
        ASSERT_OK_POSITIVE(dns_cache_lookup(&cache, key, query_flags, &ret_rcode, &ret_answer, &ret_full_packet, &ret_query_flags, NULL));
1✔
687

688
        ASSERT_EQ(cache.n_hit, 1u);
1✔
689
        ASSERT_EQ(cache.n_miss, 0u);
1✔
690

691
        ASSERT_EQ(ret_rcode, DNS_RCODE_SUCCESS);
1✔
692
        ASSERT_EQ(ret_query_flags, SD_RESOLVED_CONFIDENTIAL);
1✔
693

694
        ASSERT_EQ(dns_answer_size(ret_answer), 2u);
2✔
695

696
        rr = dns_resource_record_new_full(DNS_CLASS_IN, DNS_TYPE_A, "www.example.com");
1✔
697
        ASSERT_NOT_NULL(rr);
1✔
698
        rr->a.in_addr.s_addr = htobe32(0x7f01a8cc);
1✔
699
        ASSERT_TRUE(dns_answer_contains(ret_answer, rr));
1✔
700
        dns_resource_record_unref(rr);
1✔
701

702
        rr = dns_resource_record_new_full(DNS_CLASS_IN, DNS_TYPE_A, "www.example.com");
1✔
703
        ASSERT_NOT_NULL(rr);
1✔
704
        rr->a.in_addr.s_addr = htobe32(0xc0a8017f);
1✔
705
        ASSERT_TRUE(dns_answer_contains(ret_answer, rr));
1✔
706
        dns_resource_record_unref(rr);
1✔
707
}
1✔
708

709
TEST(dns_cache_lookup_nxdomain) {
1✔
710
        _cleanup_(dns_cache_unrefp) DnsCache cache = new_cache();
1✔
711
        _cleanup_(put_args_unrefp) PutArgs put_args = mk_put_args();
2✔
712
        _cleanup_(dns_answer_unrefp) DnsAnswer *ret_answer = NULL;
1✔
713
        _cleanup_(dns_packet_unrefp) DnsPacket *ret_full_packet = NULL;
1✔
714
        _cleanup_(dns_resource_record_unrefp) DnsResourceRecord *rr = NULL;
1✔
715
        _cleanup_(dns_resource_key_unrefp) DnsResourceKey *key = NULL;
1✔
716
        int query_flags, ret_rcode;
1✔
717
        uint64_t ret_query_flags;
1✔
718

719
        put_args.key = dns_resource_key_new(DNS_CLASS_IN, DNS_TYPE_A, "www.example.com");
1✔
720
        ASSERT_NOT_NULL(put_args.key);
1✔
721
        put_args.rcode = DNS_RCODE_NXDOMAIN;
1✔
722
        dns_answer_add_soa(put_args.answer, "example.com", 3600, 0);
1✔
723
        cache_put(&cache, &put_args);
1✔
724

725
        ASSERT_EQ(dns_cache_size(&cache), 1u);
1✔
726

727
        key = dns_resource_key_new(DNS_CLASS_IN, DNS_TYPE_A, "www.example.com");
1✔
728
        ASSERT_NOT_NULL(key);
1✔
729
        query_flags = 0;
1✔
730
        ASSERT_OK_POSITIVE(dns_cache_lookup(&cache, key, query_flags, &ret_rcode, &ret_answer, &ret_full_packet, &ret_query_flags, NULL));
1✔
731

732
        ASSERT_EQ(cache.n_hit, 1u);
1✔
733
        ASSERT_EQ(cache.n_miss, 0u);
1✔
734

735
        ASSERT_EQ(ret_rcode, DNS_RCODE_NXDOMAIN);
1✔
736
        ASSERT_EQ(ret_query_flags, (SD_RESOLVED_AUTHENTICATED | SD_RESOLVED_CONFIDENTIAL));
1✔
737

738
        ASSERT_EQ(dns_answer_size(ret_answer), 1u);
2✔
739

740
        rr = dns_resource_record_new_full(DNS_CLASS_IN, DNS_TYPE_SOA, "example.com");
1✔
741
        ASSERT_NOT_NULL(rr);
1✔
742
        rr->soa.mname = checked_strdup("example.com");
1✔
743
        rr->soa.rname = checked_strdup("root.example.com");
1✔
744
        rr->soa.serial = 1;
1✔
745
        rr->soa.refresh = 1;
1✔
746
        rr->soa.retry = 1;
1✔
747
        rr->soa.expire = 1;
1✔
748
        rr->soa.minimum = 3600;
1✔
749
        ASSERT_TRUE(dns_answer_contains(ret_answer, rr));
1✔
750
}
1✔
751

752
TEST(dns_cache_lookup_any_always_misses) {
1✔
753
        _cleanup_(dns_cache_unrefp) DnsCache cache = new_cache();
1✔
754
        _cleanup_(put_args_unrefp) PutArgs put_args = mk_put_args();
2✔
755
        _cleanup_(dns_answer_unrefp) DnsAnswer *ret_answer = NULL;
1✔
756
        _cleanup_(dns_packet_unrefp) DnsPacket *ret_full_packet = NULL;
1✔
757
        _cleanup_(dns_resource_key_unrefp) DnsResourceKey *key = NULL;
×
758
        int query_flags, ret_rcode;
1✔
759
        uint64_t ret_query_flags;
1✔
760

761
        put_args.key = dns_resource_key_new(DNS_CLASS_IN, DNS_TYPE_A, "www.example.com");
1✔
762
        ASSERT_NOT_NULL(put_args.key);
1✔
763
        put_args.rcode = DNS_RCODE_SUCCESS;
1✔
764
        answer_add_a(&put_args, put_args.key, 0xc0a8017f, 3600, DNS_ANSWER_CACHEABLE);
1✔
765
        cache_put(&cache, &put_args);
1✔
766

767
        ASSERT_EQ(dns_cache_size(&cache), 1u);
1✔
768

769
        key = dns_resource_key_new(DNS_CLASS_IN, DNS_TYPE_ANY, "www.example.com");
1✔
770
        ASSERT_NOT_NULL(key);
1✔
771
        query_flags = 0;
1✔
772
        ASSERT_FALSE(dns_cache_lookup(&cache, key, query_flags, &ret_rcode, &ret_answer, &ret_full_packet, &ret_query_flags, NULL));
1✔
773

774
        ASSERT_EQ(cache.n_hit, 0u);
1✔
775
        ASSERT_EQ(cache.n_miss, 1u);
1✔
776

777
        ASSERT_EQ(ret_rcode, DNS_RCODE_SUCCESS);
1✔
778
        ASSERT_EQ(ret_query_flags, 0u);
1✔
779

780
        ASSERT_EQ(dns_answer_size(ret_answer), 0u);
1✔
781
}
1✔
782

783
TEST(dns_cache_lookup_mdns_multiple_shared_responses_are_cached) {
1✔
784
        _cleanup_(dns_cache_unrefp) DnsCache cache = new_cache();
1✔
785
        _cleanup_(put_args_unrefp) PutArgs args1 = mk_put_args(), args2 = mk_put_args();
2✔
786
        _cleanup_(dns_answer_unrefp) DnsAnswer *ret_answer = NULL;
1✔
787
        _cleanup_(dns_packet_unrefp) DnsPacket *ret_full_packet = NULL;
1✔
788
        DnsResourceKey *key = NULL;
1✔
789
        DnsResourceRecord *rr = NULL;
1✔
790
        int query_flags, ret_rcode;
1✔
791
        uint64_t ret_query_flags;
1✔
792

793
        args1.protocol = DNS_PROTOCOL_MDNS;
1✔
794
        args1.rcode = DNS_RCODE_SUCCESS;
1✔
795
        key = dns_resource_key_new(DNS_CLASS_IN, DNS_TYPE_A, "example.com");
1✔
796
        ASSERT_NOT_NULL(key);
1✔
797
        answer_add_a(&args1, key, 0xc0a8017f, 3600, DNS_ANSWER_CACHEABLE | DNS_ANSWER_SHARED_OWNER);
1✔
798
        ASSERT_OK(cache_put(&cache, &args1));
1✔
799
        dns_resource_key_unref(key);
1✔
800

801
        args2.protocol = DNS_PROTOCOL_MDNS;
1✔
802
        args2.rcode = DNS_RCODE_SUCCESS;
1✔
803
        key = dns_resource_key_new(DNS_CLASS_IN, DNS_TYPE_A, "example.com");
1✔
804
        ASSERT_NOT_NULL(key);
1✔
805
        answer_add_a(&args2, key, 0x7f01a8cc, 3600, DNS_ANSWER_CACHEABLE | DNS_ANSWER_SHARED_OWNER);
1✔
806
        ASSERT_OK(cache_put(&cache, &args2));
1✔
807
        dns_resource_key_unref(key);
1✔
808

809
        ASSERT_FALSE(dns_cache_is_empty(&cache));
1✔
810
        ASSERT_EQ(dns_cache_size(&cache), 1u);
1✔
811

812
        key = dns_resource_key_new(DNS_CLASS_IN, DNS_TYPE_A, "example.com");
1✔
813
        ASSERT_NOT_NULL(key);
1✔
814
        query_flags = 0;
1✔
815
        ASSERT_OK_POSITIVE(dns_cache_lookup(&cache, key, query_flags, &ret_rcode, &ret_answer, &ret_full_packet, &ret_query_flags, NULL));
1✔
816
        dns_resource_key_unref(key);
1✔
817

818
        ASSERT_EQ(cache.n_hit, 1u);
1✔
819
        ASSERT_EQ(cache.n_miss, 0u);
1✔
820

821
        ASSERT_EQ(ret_rcode, DNS_RCODE_SUCCESS);
1✔
822
        ASSERT_EQ(ret_query_flags, SD_RESOLVED_CONFIDENTIAL);
1✔
823

824
        ASSERT_EQ(dns_answer_size(ret_answer), 2u);
2✔
825

826
        rr = dns_resource_record_new_full(DNS_CLASS_IN, DNS_TYPE_A, "example.com");
1✔
827
        ASSERT_NOT_NULL(rr);
1✔
828
        rr->a.in_addr.s_addr = htobe32(0xc0a8017f);
1✔
829
        ASSERT_TRUE(dns_answer_contains(ret_answer, rr));
1✔
830
        dns_resource_record_unref(rr);
1✔
831

832
        rr = dns_resource_record_new_full(DNS_CLASS_IN, DNS_TYPE_A, "example.com");
1✔
833
        ASSERT_NOT_NULL(rr);
1✔
834
        rr->a.in_addr.s_addr = htobe32(0x7f01a8cc);
1✔
835
        ASSERT_TRUE(dns_answer_contains(ret_answer, rr));
1✔
836
        dns_resource_record_unref(rr);
1✔
837
}
1✔
838

839
TEST(dns_cache_lookup_mdns_multiple_unshared_responses_are_not_cached) {
1✔
840
        _cleanup_(dns_cache_unrefp) DnsCache cache = new_cache();
1✔
841
        _cleanup_(put_args_unrefp) PutArgs args1 = mk_put_args(), args2 = mk_put_args();
2✔
842
        _cleanup_(dns_answer_unrefp) DnsAnswer *ret_answer = NULL;
1✔
843
        _cleanup_(dns_packet_unrefp) DnsPacket *ret_full_packet = NULL;
1✔
844
        DnsResourceKey *key = NULL;
1✔
845
        DnsResourceRecord *rr = NULL;
1✔
846
        int query_flags, ret_rcode;
1✔
847
        uint64_t ret_query_flags;
1✔
848

849
        args1.protocol = DNS_PROTOCOL_MDNS;
1✔
850
        args1.rcode = DNS_RCODE_SUCCESS;
1✔
851
        key = dns_resource_key_new(DNS_CLASS_IN, DNS_TYPE_A, "example.com");
1✔
852
        ASSERT_NOT_NULL(key);
1✔
853
        answer_add_a(&args1, key, 0xc0a8017f, 3600, DNS_ANSWER_CACHEABLE);
1✔
854
        ASSERT_OK(cache_put(&cache, &args1));
1✔
855
        dns_resource_key_unref(key);
1✔
856

857
        args2.protocol = DNS_PROTOCOL_MDNS;
1✔
858
        args2.rcode = DNS_RCODE_SUCCESS;
1✔
859
        key = dns_resource_key_new(DNS_CLASS_IN, DNS_TYPE_A, "example.com");
1✔
860
        ASSERT_NOT_NULL(key);
1✔
861
        answer_add_a(&args2, key, 0x7f01a8cc, 3600, DNS_ANSWER_CACHEABLE);
1✔
862
        ASSERT_OK(cache_put(&cache, &args2));
1✔
863
        dns_resource_key_unref(key);
1✔
864

865
        ASSERT_FALSE(dns_cache_is_empty(&cache));
1✔
866
        ASSERT_EQ(dns_cache_size(&cache), 1u);
1✔
867

868
        key = dns_resource_key_new(DNS_CLASS_IN, DNS_TYPE_A, "example.com");
1✔
869
        ASSERT_NOT_NULL(key);
1✔
870
        query_flags = 0;
1✔
871
        ASSERT_OK_POSITIVE(dns_cache_lookup(&cache, key, query_flags, &ret_rcode, &ret_answer, &ret_full_packet, &ret_query_flags, NULL));
1✔
872
        dns_resource_key_unref(key);
1✔
873

874
        ASSERT_EQ(cache.n_hit, 1u);
1✔
875
        ASSERT_EQ(cache.n_miss, 0u);
1✔
876

877
        ASSERT_EQ(ret_rcode, DNS_RCODE_SUCCESS);
1✔
878
        ASSERT_EQ(ret_query_flags, SD_RESOLVED_CONFIDENTIAL);
1✔
879

880
        ASSERT_EQ(dns_answer_size(ret_answer), 1u);
2✔
881

882
        rr = dns_resource_record_new_full(DNS_CLASS_IN, DNS_TYPE_A, "example.com");
1✔
883
        ASSERT_NOT_NULL(rr);
1✔
884
        rr->a.in_addr.s_addr = htobe32(0xc0a8017f);
1✔
885
        ASSERT_FALSE(dns_answer_contains(ret_answer, rr));
1✔
886
        dns_resource_record_unref(rr);
1✔
887

888
        rr = dns_resource_record_new_full(DNS_CLASS_IN, DNS_TYPE_A, "example.com");
1✔
889
        ASSERT_NOT_NULL(rr);
1✔
890
        rr->a.in_addr.s_addr = htobe32(0x7f01a8cc);
1✔
891
        ASSERT_TRUE(dns_answer_contains(ret_answer, rr));
1✔
892
        dns_resource_record_unref(rr);
1✔
893
}
1✔
894

895
/* ================================================================
896
 * dns_cache_prune(), dns_cache_expiry_in_one_second()
897
 * ================================================================ */
898

899
TEST(dns_cache_prune) {
1✔
900
        _cleanup_(dns_cache_unrefp) DnsCache cache = new_cache();
1✔
901
        _cleanup_(put_args_unrefp) PutArgs put_args = mk_put_args();
2✔
902
        DnsResourceKey *key = NULL;
1✔
903

904
        key = dns_resource_key_new(DNS_CLASS_IN, DNS_TYPE_A, "ns1.example.com");
1✔
905
        ASSERT_NOT_NULL(key);
1✔
906
        answer_add_a(&put_args, key, 0xc0a8017f, 1, DNS_ANSWER_CACHEABLE);
1✔
907
        dns_resource_key_unref(key);
1✔
908

909
        key = dns_resource_key_new(DNS_CLASS_IN, DNS_TYPE_A, "ns2.example.com");
1✔
910
        ASSERT_NOT_NULL(key);
1✔
911
        answer_add_a(&put_args, key, 0x7f01a8cc, 3, DNS_ANSWER_CACHEABLE);
1✔
912
        dns_resource_key_unref(key);
1✔
913

914
        cache_put(&cache, &put_args);
1✔
915

916
        dns_cache_prune(&cache);
1✔
917
        ASSERT_EQ(dns_cache_size(&cache), 2u);
1✔
918
        ASSERT_TRUE(dns_cache_expiry_in_one_second(&cache, now(CLOCK_BOOTTIME)));
1✔
919

920
        sleep(2);
1✔
921

922
        dns_cache_prune(&cache);
1✔
923
        ASSERT_EQ(dns_cache_size(&cache), 1u);
1✔
924
        ASSERT_TRUE(dns_cache_expiry_in_one_second(&cache, now(CLOCK_BOOTTIME)));
1✔
925

926
        sleep(2);
1✔
927

928
        dns_cache_prune(&cache);
1✔
929
        ASSERT_TRUE(dns_cache_is_empty(&cache));
1✔
930
        ASSERT_FALSE(dns_cache_expiry_in_one_second(&cache, now(CLOCK_BOOTTIME)));
1✔
931
}
1✔
932

933
/* ================================================================
934
 * dns_cache_check_conflicts()
935
 * ================================================================ */
936

937
TEST(dns_cache_check_conflicts_same_key_and_owner) {
1✔
938
        _cleanup_(dns_cache_unrefp) DnsCache cache = new_cache();
1✔
939
        _cleanup_(put_args_unrefp) PutArgs put_args = mk_put_args();
2✔
940
        _cleanup_(dns_resource_record_unrefp) DnsResourceRecord *rr = NULL;
1✔
941

942
        put_args.key = dns_resource_key_new(DNS_CLASS_IN, DNS_TYPE_A, "ns1.example.com");
1✔
943
        ASSERT_NOT_NULL(put_args.key);
1✔
944
        answer_add_a(&put_args, put_args.key, 0xc0a8017f, 3600, DNS_ANSWER_CACHEABLE);
1✔
945
        cache_put(&cache, &put_args);
1✔
946

947
        union in_addr_union owner_addr = { .in.s_addr = htobe32(0x01020304) };
1✔
948

949
        rr = dns_resource_record_new_full(DNS_CLASS_IN, DNS_TYPE_A, "ns1.example.com");
1✔
950
        ASSERT_NOT_NULL(rr);
1✔
951
        ASSERT_FALSE(dns_cache_check_conflicts(&cache, rr, AF_INET, &owner_addr));
1✔
952
}
1✔
953

954
TEST(dns_cache_check_conflicts_same_key_different_owner) {
1✔
955
        _cleanup_(dns_cache_unrefp) DnsCache cache = new_cache();
1✔
956
        _cleanup_(put_args_unrefp) PutArgs put_args = mk_put_args();
2✔
957
        _cleanup_(dns_resource_record_unrefp) DnsResourceRecord *rr = NULL;
1✔
958

959
        put_args.key = dns_resource_key_new(DNS_CLASS_IN, DNS_TYPE_A, "ns1.example.com");
1✔
960
        ASSERT_NOT_NULL(put_args.key);
1✔
961
        answer_add_a(&put_args, put_args.key, 0xc0a8017f, 3600, DNS_ANSWER_CACHEABLE);
1✔
962
        cache_put(&cache, &put_args);
1✔
963

964
        union in_addr_union owner_addr = { .in.s_addr = htobe32(0x01020305) };
1✔
965

966
        rr = dns_resource_record_new_full(DNS_CLASS_IN, DNS_TYPE_A, "ns1.example.com");
1✔
967
        ASSERT_NOT_NULL(rr);
1✔
968
        ASSERT_TRUE(dns_cache_check_conflicts(&cache, rr, AF_INET, &owner_addr));
1✔
969
}
1✔
970

971
TEST(dns_cache_check_conflicts_different_key) {
1✔
972
        _cleanup_(dns_cache_unrefp) DnsCache cache = new_cache();
1✔
973
        _cleanup_(put_args_unrefp) PutArgs put_args = mk_put_args();
2✔
974
        _cleanup_(dns_resource_record_unrefp) DnsResourceRecord *rr = NULL;
1✔
975

976
        put_args.key = dns_resource_key_new(DNS_CLASS_IN, DNS_TYPE_A, "ns2.example.com");
1✔
977
        ASSERT_NOT_NULL(put_args.key);
1✔
978
        answer_add_a(&put_args, put_args.key, 0xc0a8017f, 3600, DNS_ANSWER_CACHEABLE);
1✔
979
        cache_put(&cache, &put_args);
1✔
980

981
        union in_addr_union owner_addr = { .in.s_addr = htobe32(0x01020305) };
1✔
982

983
        rr = dns_resource_record_new_full(DNS_CLASS_IN, DNS_TYPE_A, "ns1.example.com");
1✔
984
        ASSERT_NOT_NULL(rr);
1✔
985
        ASSERT_FALSE(dns_cache_check_conflicts(&cache, rr, AF_INET, &owner_addr));
1✔
986
}
1✔
987

988
/* ================================================================
989
 * dns_cache_export_shared_to_packet()
990
 * ================================================================ */
991

992
TEST(dns_cache_export_shared_to_packet) {
1✔
993
        _cleanup_(dns_cache_unrefp) DnsCache cache = new_cache();
1✔
994
        _cleanup_(put_args_unrefp) PutArgs args1 = mk_put_args(), args2 = mk_put_args();
2✔
995
        _cleanup_(dns_packet_unrefp) DnsPacket *packet = NULL;
1✔
996
        DnsResourceKey *key = NULL;
1✔
997

998
        args1.protocol = DNS_PROTOCOL_MDNS;
1✔
999
        args1.rcode = DNS_RCODE_SUCCESS;
1✔
1000

1001
        key = dns_resource_key_new(DNS_CLASS_IN, DNS_TYPE_A, "shared.example.com");
1✔
1002
        ASSERT_NOT_NULL(key);
1✔
1003
        answer_add_a(&args1, key, 0xc0a8017f, 3600, DNS_ANSWER_CACHEABLE | DNS_ANSWER_SHARED_OWNER);
1✔
1004
        dns_resource_key_unref(key);
1✔
1005

1006
        key = dns_resource_key_new(DNS_CLASS_IN, DNS_TYPE_A, "unshared.example.com");
1✔
1007
        ASSERT_NOT_NULL(key);
1✔
1008
        answer_add_a(&args1, key, 0xa87f01c0, 2400, DNS_ANSWER_CACHEABLE);
1✔
1009
        dns_resource_key_unref(key);
1✔
1010

1011
        cache_put(&cache, &args1);
1✔
1012

1013
        args2.protocol = DNS_PROTOCOL_DNS;
1✔
1014
        args2.key = dns_resource_key_new(DNS_CLASS_IN, DNS_TYPE_A, "dns.example.com");
1✔
1015
        ASSERT_NOT_NULL(args2.key);
1✔
1016
        args2.rcode = DNS_RCODE_SUCCESS;
1✔
1017
        answer_add_a(&args2, args2.key, 0xa9fe0100, 2400, DNS_ANSWER_CACHEABLE);
1✔
1018
        cache_put(&cache, &args2);
1✔
1019

1020
        dns_packet_new(&packet, DNS_PROTOCOL_MDNS, 0, DNS_PACKET_SIZE_MAX);
1✔
1021
        ASSERT_NOT_NULL(packet);
1✔
1022
        ASSERT_OK(dns_cache_export_shared_to_packet(&cache, packet, 0, 50));
1✔
1023

1024
        const uint8_t data[] = {
1✔
1025
                        0x00, 0x00,     0x00, 0x00,
1026
                        0x00, 0x00,     0x00, 0x01,     0x00, 0x00,     0x00, 0x00,
1027

1028
        /* name */      0x06, 's', 'h', 'a', 'r', 'e', 'd',
1029
                        0x07, 'e', 'x', 'a', 'm', 'p', 'l', 'e',
1030
                        0x03, 'c', 'o', 'm',
1031
                        0x00,
1032
        /* A */         0x00, 0x01,
1033
        /* IN */        0x00, 0x01,
1034
        /* ttl */       0x00, 0x00, 0x0e, 0x10,
1035
        /* rdata */     0x00, 0x04,
1036
        /* ip */        0xc0, 0xa8, 0x01, 0x7f
1037
        };
1038

1039
        ASSERT_EQ(packet->size, sizeof(data));
1✔
1040
        ASSERT_EQ(memcmp(DNS_PACKET_DATA(packet), data, sizeof(data)), 0);
2✔
1041
}
1✔
1042

1043
TEST(dns_cache_export_shared_to_packet_multi) {
1✔
1044
        _cleanup_(dns_cache_unrefp) DnsCache cache = new_cache();
1✔
1045
        _cleanup_(put_args_unrefp) PutArgs put_args = mk_put_args();
2✔
1046
        _cleanup_(dns_packet_unrefp) DnsPacket *packet = NULL;
1✔
1047
        DnsResourceKey *key = NULL;
1✔
1048

1049
        put_args.protocol = DNS_PROTOCOL_MDNS;
1✔
1050
        put_args.rcode = DNS_RCODE_SUCCESS;
1✔
1051

1052
        key = dns_resource_key_new(DNS_CLASS_IN, DNS_TYPE_A, "shared1.example.com");
1✔
1053
        ASSERT_NOT_NULL(key);
1✔
1054
        answer_add_a(&put_args, key, 0xc0a8017f, 3600, DNS_ANSWER_CACHEABLE | DNS_ANSWER_SHARED_OWNER);
1✔
1055
        dns_resource_key_unref(key);
1✔
1056

1057
        key = dns_resource_key_new(DNS_CLASS_IN, DNS_TYPE_A, "unshared.example.com");
1✔
1058
        ASSERT_NOT_NULL(key);
1✔
1059
        answer_add_a(&put_args, key, 0xa87f01c0, 2400, DNS_ANSWER_CACHEABLE);
1✔
1060
        dns_resource_key_unref(key);
1✔
1061

1062
        key = dns_resource_key_new(DNS_CLASS_IN, DNS_TYPE_A, "shared2.example.com");
1✔
1063
        ASSERT_NOT_NULL(key);
1✔
1064
        answer_add_a(&put_args, key, 0x7f01a8cc, 1800, DNS_ANSWER_CACHEABLE | DNS_ANSWER_SHARED_OWNER);
1✔
1065
        dns_resource_key_unref(key);
1✔
1066

1067
        cache_put(&cache, &put_args);
1✔
1068

1069
        dns_packet_new(&packet, DNS_PROTOCOL_MDNS, 0, DNS_PACKET_SIZE_MAX);
1✔
1070
        ASSERT_NOT_NULL(packet);
1✔
1071
        ASSERT_OK(dns_cache_export_shared_to_packet(&cache, packet, 0, 1));
1✔
1072

1073
        const uint8_t data1[] = {
1✔
1074
                        0x00, 0x00,     0x00, 0x00,
1075
                        0x00, 0x00,     0x00, 0x01,     0x00, 0x00,     0x00, 0x00,
1076

1077
        /* name */      0x07, 's', 'h', 'a', 'r', 'e', 'd', '1',
1078
                        0x07, 'e', 'x', 'a', 'm', 'p', 'l', 'e',
1079
                        0x03, 'c', 'o', 'm',
1080
                        0x00,
1081
        /* A */         0x00, 0x01,
1082
        /* IN */        0x00, 0x01,
1083
        /* ttl */       0x00, 0x00, 0x0e, 0x10,
1084
        /* rdata */     0x00, 0x04,
1085
        /* ip */        0xc0, 0xa8, 0x01, 0x7f
1086
        };
1087

1088
        const uint8_t data2[] = {
1✔
1089
                        0x00, 0x00,     0x00, 0x00,
1090
                        0x00, 0x00,     0x00, 0x01,     0x00, 0x00,     0x00, 0x00,
1091

1092
        /* name */      0x07, 's', 'h', 'a', 'r', 'e', 'd', '2',
1093
                        0x07, 'e', 'x', 'a', 'm', 'p', 'l', 'e',
1094
                        0x03, 'c', 'o', 'm',
1095
                        0x00,
1096
        /* A */         0x00, 0x01,
1097
        /* IN */        0x00, 0x01,
1098
        /* ttl */       0x00, 0x00, 0x07, 0x08,
1099
        /* rdata */     0x00, 0x04,
1100
        /* ip */        0x7f, 0x01, 0xa8, 0xcc
1101
        };
1102

1103
        size_t size1 = sizeof(data1), size2 = sizeof(data2);
1✔
1104

1105
        /* cache key order is not deterministic; the packets could come out in either order */
1106

1107
        if (memcmp(DNS_PACKET_DATA(packet), data1, size1) == 0) {
2✔
UNCOV
1108
                ASSERT_EQ(packet->size, size1);
×
UNCOV
1109
                ASSERT_EQ(memcmp(DNS_PACKET_DATA(packet), data1, size1), 0);
×
1110

UNCOV
1111
                ASSERT_EQ(packet->more->size, size2);
×
UNCOV
1112
                ASSERT_EQ(memcmp(DNS_PACKET_DATA(packet->more), data2, size2), 0);
×
1113
        } else {
1114
                ASSERT_EQ(packet->size, size2);
1✔
1115
                ASSERT_EQ(memcmp(DNS_PACKET_DATA(packet), data2, size2), 0);
2✔
1116

1117
                ASSERT_EQ(packet->more->size, size1);
1✔
1118
                ASSERT_EQ(memcmp(DNS_PACKET_DATA(packet->more), data1, size1), 0);
2✔
1119
        }
1120

1121
        ASSERT_NULL(packet->more->more);
1✔
1122
}
1✔
1123

1124
/* ================================================================
1125
 * dns_cache_dump()
1126
 * ================================================================ */
1127

1128
static int cmpstring(const void *a, const void *b) {
1✔
1129
        ASSERT_NOT_NULL(a);
1✔
1130
        ASSERT_NOT_NULL(b);
1✔
1131

1132
        return strcmp(*(const char **)a, *(const char **)b);
1✔
1133
}
1134

1135
static void check_dump_contents(FILE *f, const char **expected, size_t n) {
2✔
1136
        char *actual[n];
2✔
1137
        rewind(f);
2✔
1138

1139
        for (size_t i = 0; i < n; i++) {
5✔
1140
                size_t length = read_line(f, 1024, &actual[i]);
3✔
1141
                ASSERT_GT(length, 0u);
3✔
1142
        }
1143

1144
        qsort(actual, n, sizeof(char *), cmpstring);
2✔
1145

1146
        for (size_t i = 0; i < n; i++)
5✔
1147
                ASSERT_STREQ(actual[i], expected[i]);
3✔
1148

1149
        for (size_t i = 0; i < n; i++)
5✔
1150
                free(actual[i]);
3✔
1151
}
2✔
1152

1153
TEST(dns_cache_dump_single_a) {
1✔
1154
        _cleanup_(dns_cache_unrefp) DnsCache cache = new_cache();
1✔
1155
        _cleanup_(put_args_unrefp) PutArgs put_args = mk_put_args();
2✔
1156

1157
        put_args.key = dns_resource_key_new(DNS_CLASS_IN, DNS_TYPE_A, "www.example.com");
1✔
1158
        ASSERT_NOT_NULL(put_args.key);
1✔
1159
        put_args.rcode = DNS_RCODE_SUCCESS;
1✔
1160
        answer_add_a(&put_args, put_args.key, 0xc0a8017f, 3600, DNS_ANSWER_CACHEABLE);
1✔
1161
        cache_put(&cache, &put_args);
1✔
1162

1163
        ASSERT_EQ(dns_cache_size(&cache), 1u);
1✔
1164

1165
        _cleanup_(unlink_tempfilep) char p[] = "/tmp/dns-cache-dump-single-a-XXXXXX";
×
1166
        _cleanup_fclose_ FILE *f = NULL;
1✔
1167
        fmkostemp_safe(p, "r+", &f);
1✔
1168
        dns_cache_dump(&cache, f);
1✔
1169

1170
        const char *expected[] = {
1✔
1171
                "\twww.example.com IN A 192.168.1.127"
1172
        };
1173
        check_dump_contents(f, expected, 1);
1✔
1174
}
1✔
1175

1176
TEST(dns_cache_dump_a_with_cname) {
1✔
1177
        _cleanup_(dns_cache_unrefp) DnsCache cache = new_cache();
1✔
1178
        _cleanup_(put_args_unrefp) PutArgs put_args = mk_put_args();
2✔
1179
        _cleanup_(dns_resource_key_unrefp) DnsResourceKey *key = NULL;
1✔
1180

1181
        put_args.key = dns_resource_key_new(DNS_CLASS_IN, DNS_TYPE_A, "www.example.com");
1✔
1182
        ASSERT_NOT_NULL(put_args.key);
1✔
1183
        put_args.rcode = DNS_RCODE_SUCCESS;
1✔
1184

1185
        key = dns_resource_key_new(DNS_CLASS_IN, DNS_TYPE_CNAME, "www.example.com");
1✔
1186
        ASSERT_NOT_NULL(key);
1✔
1187
        answer_add_cname(&put_args, key, "example.com", 3600, DNS_ANSWER_CACHEABLE);
1✔
1188

1189
        dns_resource_key_unref(key);
1✔
1190
        key = dns_resource_key_new(DNS_CLASS_IN, DNS_TYPE_A, "example.com");
1✔
1191
        ASSERT_NOT_NULL(key);
1✔
1192
        answer_add_a(&put_args, key, 0xc0a8017f, 3600, DNS_ANSWER_CACHEABLE);
1✔
1193

1194
        cache_put(&cache, &put_args);
1✔
1195

1196
        ASSERT_EQ(dns_cache_size(&cache), 2u);
1✔
1197

1198
        _cleanup_(unlink_tempfilep) char p[] = "/tmp/dns-cache-dump-a-with-cname-XXXXXX";
×
1199
        _cleanup_fclose_ FILE *f = NULL;
1✔
1200
        fmkostemp_safe(p, "r+", &f);
1✔
1201
        dns_cache_dump(&cache, f);
1✔
1202

1203
        const char *expected[] = {
1✔
1204
                "\texample.com IN A 192.168.1.127",
1205
                "\twww.example.com IN CNAME example.com"
1206
        };
1207
        check_dump_contents(f, expected, 2);
1✔
1208
}
1✔
1209

1210
/* ================================================================
1211
 * dns_cache_dump_to_json()
1212
 * ================================================================ */
1213

1214
TEST(dns_cache_dump_json_basic) {
1✔
1215
        _cleanup_(dns_cache_unrefp) DnsCache cache = new_cache();
1✔
1216
        _cleanup_(put_args_unrefp) PutArgs put_args = mk_put_args();
1✔
1217
        _cleanup_(sd_json_variant_unrefp) sd_json_variant *json = NULL, *expected = NULL;
2✔
1218
        sd_json_variant *item = NULL, *rr = NULL;
1✔
1219
        _cleanup_free_ char *str = calloc(256, sizeof(char));
1✔
1220

1221
        ASSERT_NOT_NULL(str);
1✔
1222

1223
        put_args.key = dns_resource_key_new(DNS_CLASS_IN, DNS_TYPE_A, "www.example.com");
1✔
1224
        ASSERT_NOT_NULL(put_args.key);
1✔
1225
        put_args.rcode = DNS_RCODE_SUCCESS;
1✔
1226
        answer_add_a(&put_args, put_args.key, 0xc0a8017f, 3600, DNS_ANSWER_CACHEABLE);
1✔
1227
        cache_put(&cache, &put_args);
1✔
1228

1229
        ASSERT_EQ(dns_cache_size(&cache), 1u);
1✔
1230

1231
        ASSERT_OK(dns_cache_dump_to_json(&cache, &json));
1✔
1232
        ASSERT_NOT_NULL(json);
1✔
1233

1234
        ASSERT_TRUE(sd_json_variant_is_array(json));
1✔
1235
        ASSERT_EQ(sd_json_variant_elements(json), 1u);
1✔
1236

1237
        item = BY_IDX(json, 0);
1✔
1238
        ASSERT_NOT_NULL(item);
1✔
1239

1240
        sprintf(str, "{ \"class\": %d, \"type\": %d, \"name\": \"www.example.com\" }", DNS_CLASS_IN, DNS_TYPE_A);
1✔
1241
        ASSERT_OK(sd_json_parse(str, 0, &expected, NULL, NULL));
1✔
1242
        ASSERT_TRUE(sd_json_variant_equal(BY_KEY(item, "key"), expected));
1✔
1243

1244
        ASSERT_TRUE(sd_json_variant_is_array(BY_KEY(item, "rrs")));
1✔
1245
        ASSERT_EQ(sd_json_variant_elements(BY_KEY(item, "rrs")), 1u);
1✔
1246

1247
        rr = BY_KEY(BY_IDX(BY_KEY(item, "rrs"), 0), "rr");
1✔
1248
        ASSERT_NOT_NULL(rr);
1✔
1249
        ASSERT_TRUE(sd_json_variant_equal(BY_KEY(rr, "key"), expected));
1✔
1250

1251
        sd_json_variant_unref(expected);
1✔
1252

1253
        sprintf(str, "[192, 168, 1, 127]");
1✔
1254
        ASSERT_OK(sd_json_parse(str, 0, &expected, NULL, NULL));
1✔
1255
        ASSERT_TRUE(sd_json_variant_equal(BY_KEY(rr, "address"), expected));
1✔
1256

1257
        ASSERT_TRUE(sd_json_variant_is_string(BY_KEY(BY_IDX(BY_KEY(item, "rrs"), 0), "raw")));
1✔
1258
        ASSERT_TRUE(sd_json_variant_is_integer(BY_KEY(item, "until")));
1✔
1259
}
1✔
1260

1261
DEFINE_TEST_MAIN(LOG_DEBUG);
1✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc