1*712b2f30Ssthen /*
2*712b2f30Ssthen * testcode/unitanchor.c - unit test for trust anchor storage.
3*712b2f30Ssthen *
4*712b2f30Ssthen * Copyright (c) 2007, NLnet Labs. All rights reserved.
5*712b2f30Ssthen *
6*712b2f30Ssthen * This software is open source.
7*712b2f30Ssthen *
8*712b2f30Ssthen * Redistribution and use in source and binary forms, with or without
9*712b2f30Ssthen * modification, are permitted provided that the following conditions
10*712b2f30Ssthen * are met:
11*712b2f30Ssthen *
12*712b2f30Ssthen * Redistributions of source code must retain the above copyright notice,
13*712b2f30Ssthen * this list of conditions and the following disclaimer.
14*712b2f30Ssthen *
15*712b2f30Ssthen * Redistributions in binary form must reproduce the above copyright notice,
16*712b2f30Ssthen * this list of conditions and the following disclaimer in the documentation
17*712b2f30Ssthen * and/or other materials provided with the distribution.
18*712b2f30Ssthen *
19*712b2f30Ssthen * Neither the name of the NLNET LABS nor the names of its contributors may
20*712b2f30Ssthen * be used to endorse or promote products derived from this software without
21*712b2f30Ssthen * specific prior written permission.
22*712b2f30Ssthen *
23*712b2f30Ssthen * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24*712b2f30Ssthen * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25*712b2f30Ssthen * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
26*712b2f30Ssthen * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
27*712b2f30Ssthen * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
28*712b2f30Ssthen * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
29*712b2f30Ssthen * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
30*712b2f30Ssthen * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
31*712b2f30Ssthen * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
32*712b2f30Ssthen * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
33*712b2f30Ssthen * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34*712b2f30Ssthen *
35*712b2f30Ssthen */
36*712b2f30Ssthen /**
37*712b2f30Ssthen * \file
38*712b2f30Ssthen * Calls trust anchor unit tests. Exits with code 1 on a failure.
39*712b2f30Ssthen */
40*712b2f30Ssthen
41*712b2f30Ssthen #include "config.h"
42*712b2f30Ssthen #include "util/log.h"
43*712b2f30Ssthen #include "util/data/dname.h"
44*712b2f30Ssthen #include "testcode/unitmain.h"
45*712b2f30Ssthen #include "validator/val_anchor.h"
46*712b2f30Ssthen #include "sldns/sbuffer.h"
47*712b2f30Ssthen #include "sldns/rrdef.h"
48*712b2f30Ssthen
49*712b2f30Ssthen /** test empty set */
50*712b2f30Ssthen static void
test_anchor_empty(struct val_anchors * a)51*712b2f30Ssthen test_anchor_empty(struct val_anchors* a)
52*712b2f30Ssthen {
53*712b2f30Ssthen uint16_t c = LDNS_RR_CLASS_IN;
54*712b2f30Ssthen unit_assert(anchors_lookup(a, (uint8_t*)"\000", 1, c) == NULL);
55*712b2f30Ssthen unit_assert(anchors_lookup(a, (uint8_t*)"\003com\000", 5, c) == NULL);
56*712b2f30Ssthen unit_assert(anchors_lookup(a,
57*712b2f30Ssthen (uint8_t*)"\007example\003com\000", 11, c) == NULL);
58*712b2f30Ssthen unit_assert(anchors_lookup(a, (uint8_t*)"\002nl\000", 4, c) == NULL);
59*712b2f30Ssthen unit_assert(anchors_lookup(a,
60*712b2f30Ssthen (uint8_t*)"\004labs\002nl\000", 9, c) == NULL);
61*712b2f30Ssthen unit_assert(anchors_lookup(a,
62*712b2f30Ssthen (uint8_t*)"\004fabs\002nl\000", 9, c) == NULL);
63*712b2f30Ssthen }
64*712b2f30Ssthen
65*712b2f30Ssthen /** test set of one anchor */
66*712b2f30Ssthen static void
test_anchor_one(sldns_buffer * buff,struct val_anchors * a)67*712b2f30Ssthen test_anchor_one(sldns_buffer* buff, struct val_anchors* a)
68*712b2f30Ssthen {
69*712b2f30Ssthen struct trust_anchor* ta;
70*712b2f30Ssthen uint16_t c = LDNS_RR_CLASS_IN;
71*712b2f30Ssthen unit_assert(anchor_store_str(a, buff,
72*712b2f30Ssthen "nl. DS 42860 5 1 14D739EB566D2B1A5E216A0BA4D17FA9B038BE4A"));
73*712b2f30Ssthen unit_assert(anchors_lookup(a, (uint8_t*)"\000", 1, c) == NULL);
74*712b2f30Ssthen unit_assert(anchors_lookup(a, (uint8_t*)"\003com\000", 5, c) == NULL);
75*712b2f30Ssthen unit_assert(anchors_lookup(a,
76*712b2f30Ssthen (uint8_t*)"\007example\003com\000", 11, c) == NULL);
77*712b2f30Ssthen
78*712b2f30Ssthen unit_assert((ta=anchors_lookup(a,
79*712b2f30Ssthen (uint8_t*)"\002nl\000", 4, c)) != NULL);
80*712b2f30Ssthen lock_basic_unlock(&ta->lock);
81*712b2f30Ssthen
82*712b2f30Ssthen unit_assert((ta=anchors_lookup(a,
83*712b2f30Ssthen (uint8_t*)"\004labs\002nl\000", 9, c)) != NULL);
84*712b2f30Ssthen lock_basic_unlock(&ta->lock);
85*712b2f30Ssthen
86*712b2f30Ssthen unit_assert((ta=anchors_lookup(a,
87*712b2f30Ssthen (uint8_t*)"\004fabs\002nl\000", 9, c)) != NULL);
88*712b2f30Ssthen lock_basic_unlock(&ta->lock);
89*712b2f30Ssthen
90*712b2f30Ssthen unit_assert(anchors_lookup(a, (uint8_t*)"\002oo\000", 4, c) == NULL);
91*712b2f30Ssthen }
92*712b2f30Ssthen
93*712b2f30Ssthen /** test with several anchors */
94*712b2f30Ssthen static void
test_anchors(sldns_buffer * buff,struct val_anchors * a)95*712b2f30Ssthen test_anchors(sldns_buffer* buff, struct val_anchors* a)
96*712b2f30Ssthen {
97*712b2f30Ssthen struct trust_anchor* ta;
98*712b2f30Ssthen uint16_t c = LDNS_RR_CLASS_IN;
99*712b2f30Ssthen unit_assert(anchor_store_str(a, buff,
100*712b2f30Ssthen "labs.nl. DS 42860 5 1 14D739EB566D2B1A5E216A0BA4D17FA9B038BE4A"));
101*712b2f30Ssthen unit_assert(anchors_lookup(a, (uint8_t*)"\000", 1, c) == NULL);
102*712b2f30Ssthen unit_assert(anchors_lookup(a, (uint8_t*)"\003com\000", 5, c) == NULL);
103*712b2f30Ssthen unit_assert(anchors_lookup(a,
104*712b2f30Ssthen (uint8_t*)"\007example\003com\000", 11, c) == NULL);
105*712b2f30Ssthen
106*712b2f30Ssthen unit_assert(ta = anchors_lookup(a, (uint8_t*)"\002nl\000", 4, c));
107*712b2f30Ssthen unit_assert(query_dname_compare(ta->name, (uint8_t*)"\002nl\000")==0);
108*712b2f30Ssthen lock_basic_unlock(&ta->lock);
109*712b2f30Ssthen
110*712b2f30Ssthen unit_assert(ta = anchors_lookup(a,
111*712b2f30Ssthen (uint8_t*)"\004labs\002nl\000", 9, c));
112*712b2f30Ssthen unit_assert(query_dname_compare(ta->name,
113*712b2f30Ssthen (uint8_t*)"\004labs\002nl\000") == 0);
114*712b2f30Ssthen lock_basic_unlock(&ta->lock);
115*712b2f30Ssthen
116*712b2f30Ssthen unit_assert(ta = anchors_lookup(a,
117*712b2f30Ssthen (uint8_t*)"\004fabs\002nl\000", 9, c));
118*712b2f30Ssthen unit_assert(query_dname_compare(ta->name,
119*712b2f30Ssthen (uint8_t*)"\002nl\000") == 0);
120*712b2f30Ssthen lock_basic_unlock(&ta->lock);
121*712b2f30Ssthen
122*712b2f30Ssthen unit_assert(anchors_lookup(a, (uint8_t*)"\002oo\000", 4, c) == NULL);
123*712b2f30Ssthen }
124*712b2f30Ssthen
anchors_test(void)125*712b2f30Ssthen void anchors_test(void)
126*712b2f30Ssthen {
127*712b2f30Ssthen sldns_buffer* buff = sldns_buffer_new(65800);
128*712b2f30Ssthen struct val_anchors* a;
129*712b2f30Ssthen unit_show_feature("trust anchor store");
130*712b2f30Ssthen unit_assert(a = anchors_create());
131*712b2f30Ssthen sldns_buffer_flip(buff);
132*712b2f30Ssthen test_anchor_empty(a);
133*712b2f30Ssthen test_anchor_one(buff, a);
134*712b2f30Ssthen test_anchors(buff, a);
135*712b2f30Ssthen anchors_delete(a);
136*712b2f30Ssthen sldns_buffer_free(buff);
137*712b2f30Ssthen }
138