Brian Waters | 13d9601 | 2017-12-08 16:53:31 -0600 | [diff] [blame] | 1 | /********************************************************************************************************* |
| 2 | * Software License Agreement (BSD License) * |
| 3 | * Author: Sebastien Decugis <sdecugis@freediameter.net> * |
| 4 | * * |
| 5 | * Copyright (c) 2011, WIDE Project and NICT * |
| 6 | * All rights reserved. * |
| 7 | * * |
| 8 | * Redistribution and use of this software in source and binary forms, with or without modification, are * |
| 9 | * permitted provided that the following conditions are met: * |
| 10 | * * |
| 11 | * * Redistributions of source code must retain the above * |
| 12 | * copyright notice, this list of conditions and the * |
| 13 | * following disclaimer. * |
| 14 | * * |
| 15 | * * Redistributions in binary form must reproduce the above * |
| 16 | * copyright notice, this list of conditions and the * |
| 17 | * following disclaimer in the documentation and/or other * |
| 18 | * materials provided with the distribution. * |
| 19 | * * |
| 20 | * * Neither the name of the WIDE Project or NICT nor the * |
| 21 | * names of its contributors may be used to endorse or * |
| 22 | * promote products derived from this software without * |
| 23 | * specific prior written permission of WIDE Project and * |
| 24 | * NICT. * |
| 25 | * * |
| 26 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED * |
| 27 | * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A * |
| 28 | * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR * |
| 29 | * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT * |
| 30 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * |
| 31 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR * |
| 32 | * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF * |
| 33 | * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * |
| 34 | *********************************************************************************************************/ |
| 35 | |
| 36 | #include "tests.h" |
| 37 | |
| 38 | #define TEST_STR (os0_t)"This is my test string (with extra unused data)" |
| 39 | |
| 40 | /* The following string contains UTF-8 encoded characters (Chinese characters) */ |
| 41 | #define TEST_IDN_UTF8 "freeDiameter.中国" |
| 42 | #define TEST_IDN_CONV "freeDiameter.xn--fiqs8s" |
| 43 | |
| 44 | /* Main test routine */ |
| 45 | int main(int argc, char *argv[]) |
| 46 | { |
| 47 | /* First, initialize the daemon modules */ |
| 48 | INIT_FD(); |
| 49 | |
| 50 | /* Check the hash function */ |
| 51 | { |
| 52 | uint8_t buf[30]; |
| 53 | |
| 54 | uint32_t hash = fd_os_hash(TEST_STR, CONSTSTRLEN(TEST_STR)); /* reference value */ |
| 55 | |
| 56 | /* Check that a hash of a substring / surstring is different */ |
| 57 | CHECK( 1, hash != fd_os_hash(TEST_STR, CONSTSTRLEN(TEST_STR) - 1) ? 1 : 0 ); |
| 58 | CHECK( 1, hash != fd_os_hash(TEST_STR, CONSTSTRLEN(TEST_STR) + 1) ? 1 : 0 ); |
| 59 | |
| 60 | /* Check alignment of the string is not important */ |
| 61 | memcpy(buf + 4, TEST_STR, CONSTSTRLEN(TEST_STR)); |
| 62 | CHECK( hash, fd_os_hash(buf + 4, CONSTSTRLEN(TEST_STR)) ); |
| 63 | |
| 64 | memcpy(buf + 3, TEST_STR, CONSTSTRLEN(TEST_STR)); |
| 65 | CHECK( hash, fd_os_hash(buf + 3, CONSTSTRLEN(TEST_STR)) ); |
| 66 | |
| 67 | memcpy(buf + 2, TEST_STR, CONSTSTRLEN(TEST_STR)); |
| 68 | CHECK( hash, fd_os_hash(buf + 2, CONSTSTRLEN(TEST_STR)) ); |
| 69 | |
| 70 | memcpy(buf + 1, TEST_STR, CONSTSTRLEN(TEST_STR)); |
| 71 | CHECK( hash, fd_os_hash(buf + 1, CONSTSTRLEN(TEST_STR)) ); |
| 72 | } |
| 73 | |
| 74 | /* Check the Diameter Identity functions */ |
| 75 | { |
| 76 | char * res; |
| 77 | size_t len=0; |
| 78 | |
| 79 | /* A valid ASCII domain name */ |
| 80 | res = TEST_IDN_CONV; |
| 81 | CHECK( 0, fd_os_validate_DiameterIdentity(&res, &len, 1) ); |
| 82 | CHECK( 0, strcasecmp(res, TEST_IDN_CONV) ); /* the function does not change a valid DN */ |
| 83 | CHECK( 0, fd_os_validate_DiameterIdentity(&res, &len, 0) ); |
| 84 | CHECK( 0, strcasecmp(res, TEST_IDN_CONV) ); |
| 85 | CHECK( CONSTSTRLEN(TEST_IDN_CONV), len ); |
| 86 | free(res); |
| 87 | |
| 88 | /* Now, an invalid string */ |
| 89 | res = TEST_IDN_UTF8; |
| 90 | len = 0; |
| 91 | |
| 92 | #ifdef DIAMID_IDNA_IGNORE |
| 93 | |
| 94 | /* The UTF-8 chars are considered valid */ |
| 95 | CHECK( 1, fd_os_is_valid_DiameterIdentity((os0_t)TEST_IDN_UTF8, CONSTSTRLEN(TEST_IDN_UTF8) ) ); |
| 96 | |
| 97 | /* The string should be passed unmodified */ |
| 98 | CHECK( 0, fd_os_validate_DiameterIdentity(&res, &len, 1) ); |
| 99 | CHECK( 0, strcasecmp(res, TEST_IDN_UTF8) ); |
| 100 | CHECK( 0, fd_os_cmp(res, len, TEST_IDN_UTF8, CONSTSTRLEN(TEST_IDN_UTF8)) ); |
| 101 | CHECK( 0, fd_os_almostcasesrch(res, len, TEST_IDN_UTF8, CONSTSTRLEN(TEST_IDN_UTF8), NULL) ); |
| 102 | CHECK( 0, fd_os_validate_DiameterIdentity(&res, &len, 0) ); |
| 103 | CHECK( 0, strcasecmp(res, TEST_IDN_UTF8) ); |
| 104 | CHECK( CONSTSTRLEN(TEST_IDN_UTF8), len ); |
| 105 | free(res); |
| 106 | |
| 107 | #else /* DIAMID_IDNA_IGNORE */ |
| 108 | |
| 109 | /* The UTF-8 chars are recognized as invalid DiameterIdentity */ |
| 110 | CHECK( 0, fd_os_is_valid_DiameterIdentity((os0_t)TEST_IDN_UTF8, CONSTSTRLEN(TEST_IDN_UTF8) )); |
| 111 | |
| 112 | # ifdef DIAMID_IDNA_REJECT |
| 113 | |
| 114 | /* The string must be rejected */ |
| 115 | CHECK( EINVAL, fd_os_validate_DiameterIdentity(&res, &len, 1) ); |
| 116 | |
| 117 | # else /* DIAMID_IDNA_REJECT */ |
| 118 | |
| 119 | /* The string should be transformed into TEST_IDN_CONV */ |
| 120 | CHECK( 0, fd_os_validate_DiameterIdentity(&res, &len, 1) ); |
| 121 | CHECK( 0, strcasecmp(res, TEST_IDN_CONV) ); |
| 122 | CHECK( CONSTSTRLEN(TEST_IDN_CONV), len ); |
| 123 | free(res); |
| 124 | |
| 125 | # endif /* DIAMID_IDNA_REJECT */ |
| 126 | #endif /* DIAMID_IDNA_IGNORE */ |
| 127 | |
| 128 | } |
| 129 | |
| 130 | { |
| 131 | /* test fd_os_cmp and fd_os_almostcasesrch and that they are compatible */ |
| 132 | char *t1 = "a"; |
| 133 | char *t2 = "b"; |
| 134 | char *t3 = "C"; |
| 135 | char *t4 = "d"; |
| 136 | char *t5 = "aa"; |
| 137 | char *t6 = "aB"; |
| 138 | char *t7 = "Ac"; |
| 139 | char *t8 = "aD"; |
| 140 | char *t9 = "AAA"; |
| 141 | |
| 142 | char *t5b = "Aa"; |
| 143 | char *t6b = "ab"; |
| 144 | |
| 145 | /* First, create a list with all the elements in order given by fd_os_cmp */ |
| 146 | char *t[] = { t1, t2, t3, t4, t5, t6,t7, t8, t9 }; |
| 147 | int i; |
| 148 | struct fd_list *li, l = FD_LIST_INITIALIZER(l); |
| 149 | for (i = 0; i < sizeof(t) / sizeof(t[0]); i++) { |
| 150 | /* insert t[i] */ |
| 151 | struct fd_list *n = malloc(sizeof(struct fd_list)); |
| 152 | CHECK( 1, n ? 1 : 0 ); |
| 153 | fd_list_init(n, t[i]); |
| 154 | for (li = l.next; li != &l; li = li->next) { |
| 155 | if ( fd_os_cmp(t[i], strlen(t[i]), li->o, strlen(li->o)) < 0 ) |
| 156 | break; |
| 157 | } |
| 158 | fd_list_insert_before(li, n); |
| 159 | } |
| 160 | /* in principle the result is: [ "C", "a", "b", "d", "Ac", "aB", "aD", "aa", "AAA" ] */ |
| 161 | |
| 162 | /* Since there is no equal value in the list (even case-insensitive), check that the order is valid also for the caseinsensitive variant */ |
| 163 | for (li = l.next; li != l.prev; li = li->next) { |
| 164 | CHECK( 1, fd_os_almostcasesrch(li->o, strlen(li->o), li->next->o, strlen(li->next->o), NULL) < 0 ? 1 : 0 ); |
| 165 | } |
| 166 | |
| 167 | /* Now check that we can case-insentively find t5b and t6b to be equal to t5 and t6 resp. (this is how we use it in the daemon) */ |
| 168 | for (li = l.next; li != &l; li = li->next) { |
| 169 | int cont, cmp; |
| 170 | cmp = fd_os_almostcasesrch(t5b, strlen(t5b), li->o, strlen(li->o), &cont); |
| 171 | TRACE_DEBUG(FULL, "Comp '%s' : %d, %d", (char *)li->o, cmp, cont); |
| 172 | if (cmp == 0) |
| 173 | break; |
| 174 | if (!cont) |
| 175 | break; |
| 176 | } |
| 177 | CHECK( li->o, t5 ); |
| 178 | |
| 179 | for (li = l.next; li != &l; li = li->next) { |
| 180 | int cont, cmp; |
| 181 | cmp = fd_os_almostcasesrch(t6b, strlen(t6b), li->o, strlen(li->o), &cont); |
| 182 | TRACE_DEBUG(FULL, "Comp '%s' : %d, %d", (char *)li->o, cmp, cont); |
| 183 | if (cmp == 0) |
| 184 | break; |
| 185 | if (!cont) |
| 186 | break; |
| 187 | } |
| 188 | CHECK( li->o, t6 ); |
| 189 | |
| 190 | |
| 191 | /* done */ |
| 192 | while (!FD_IS_LIST_EMPTY(&l)) { |
| 193 | li = l.next; |
| 194 | fd_list_unlink(li); |
| 195 | free(li); |
| 196 | } |
| 197 | } |
| 198 | |
| 199 | /* That's all for the tests yet */ |
| 200 | PASSTEST(); |
| 201 | } |
| 202 | |