1 /* $NetBSD: test-normalize.c,v 1.1.1.1 2011/04/13 18:16:00 elric Exp $ */ 2 3 /* 4 * Copyright (c) 2004 Kungliga Tekniska Högskolan 5 * (Royal Institute of Technology, Stockholm, Sweden). 6 * All rights reserved. 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 * 1. Redistributions of source code must retain the above copyright 13 * notice, this list of conditions and the following disclaimer. 14 * 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 19 * 3. Neither the name of the Institute nor the names of its contributors 20 * may be used to endorse or promote products derived from this software 21 * without specific prior written permission. 22 * 23 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND 24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 26 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE 27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 33 * SUCH DAMAGE. 34 */ 35 36 #ifdef HAVE_CONFIG_H 37 #include <config.h> 38 #endif 39 #include <stdio.h> 40 #include <stdlib.h> 41 #include <string.h> 42 #include <err.h> 43 44 #include <krb5/roken.h> 45 46 #include "windlocl.h" 47 #include "normalize_table.h" 48 49 static size_t 50 parse_vector(char *buf, uint32_t *v) 51 { 52 char *last; 53 unsigned ret = 0; 54 const char *n; 55 unsigned u; 56 57 for(n = strtok_r(buf, " ", &last); 58 n != NULL; 59 n = strtok_r(NULL, " ", &last)) { 60 if (ret >= MAX_LENGTH_CANON) { 61 errx(1, "increase MAX_LENGTH_CANON"); 62 } 63 if (sscanf(n, "%x", &u) != 1) { 64 errx(1, "failed to parse hex: %s", n); 65 } 66 v[ret] = u; 67 ++ret; 68 } 69 return ret; 70 } 71 72 static void 73 dump_vector(const char * msg, uint32_t * v, size_t len) 74 { 75 size_t i; 76 77 printf("%s: (%d) ", msg, (int)len); 78 for (i=0; i < len; i++) { 79 printf("%s%x", (i > 0? " ":""), v[i]); 80 } 81 printf("\n"); 82 } 83 84 static int 85 test(char *buf, unsigned lineno) 86 { 87 char *last; 88 char *c; 89 uint32_t in[MAX_LENGTH_CANON]; 90 size_t in_len; 91 uint32_t out[MAX_LENGTH_CANON]; 92 size_t out_len; 93 uint32_t *tmp; 94 size_t norm_len; 95 int ret; 96 97 c = strtok_r(buf, ";", &last); 98 if (c == NULL) 99 return 0; 100 101 in_len = parse_vector(c, in); 102 if (strtok_r(NULL, ";", &last) == NULL) 103 return 0; 104 if (strtok_r(NULL, ";", &last) == NULL) 105 return 0; 106 c = strtok_r(NULL, ";", &last); 107 if (c == NULL) 108 return 0; 109 out_len = parse_vector(c, out); 110 if (strtok_r(NULL, ";", &last) == NULL) 111 return 0; 112 c = last; 113 114 norm_len = MAX_LENGTH_CANON; 115 tmp = malloc(norm_len * sizeof(uint32_t)); 116 if (tmp == NULL && norm_len != 0) 117 err(1, "malloc"); 118 ret = _wind_stringprep_normalize(in, in_len, tmp, &norm_len); 119 if (ret) { 120 printf("wind_stringprep_normalize %s failed\n", c); 121 free(tmp); 122 return 1; 123 } 124 if (out_len != norm_len) { 125 printf("%u: wrong out len (%s)\n", lineno, c); 126 dump_vector("Expected", out, out_len); 127 dump_vector("Received", tmp, norm_len); 128 free(tmp); 129 return 1; 130 } 131 if (memcmp(out, tmp, out_len * sizeof(uint32_t)) != 0) { 132 printf("%u: wrong out data (%s)\n", lineno, c); 133 dump_vector("Expected", out, out_len); 134 dump_vector("Received", tmp, norm_len); 135 free(tmp); 136 return 1; 137 } 138 free(tmp); 139 return 0; 140 } 141 142 int 143 main(int argc, char **argv) 144 { 145 FILE *f; 146 char buf[1024]; 147 char filename[256] = "NormalizationTest.txt"; 148 unsigned failures = 0; 149 unsigned lineno = 0; 150 151 if (argc > 2) 152 errx(1, "usage: %s [file]", argv[0]); 153 else if (argc == 2) 154 strlcpy(filename, argv[1], sizeof(filename)); 155 156 f = fopen(filename, "r"); 157 if (f == NULL) { 158 const char *srcdir = getenv("srcdir"); 159 if (srcdir != NULL) { 160 char longname[256]; 161 snprintf(longname, sizeof(longname), "%s/%s", srcdir, filename); 162 f = fopen(longname, "r"); 163 } 164 if (f == NULL) 165 err(1, "open %s", filename); 166 } 167 while (fgets(buf, sizeof(buf), f) != NULL) { 168 lineno++; 169 if (buf[0] == '#') 170 continue; 171 if (buf[0] == '@') { 172 continue; 173 } 174 failures += test(buf, lineno); 175 } 176 fclose(f); 177 return failures != 0; 178 } 179