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