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