parse-name-test.c revision 233294
1223013Sdim/* 2223013Sdim * Copyright (c) 2002 Kungliga Tekniska H��gskolan 3223013Sdim * (Royal Institute of Technology, Stockholm, Sweden). 4223013Sdim * All rights reserved. 5223013Sdim * 6223013Sdim * Redistribution and use in source and binary forms, with or without 7223013Sdim * modification, are permitted provided that the following conditions 8223013Sdim * are met: 9223013Sdim * 10223013Sdim * 1. Redistributions of source code must retain the above copyright 11223013Sdim * notice, this list of conditions and the following disclaimer. 12223013Sdim * 13223013Sdim * 2. Redistributions in binary form must reproduce the above copyright 14223013Sdim * notice, this list of conditions and the following disclaimer in the 15223013Sdim * documentation and/or other materials provided with the distribution. 16223013Sdim * 17223013Sdim * 3. Neither the name of KTH nor the names of its contributors may be 18223013Sdim * used to endorse or promote products derived from this software without 19223013Sdim * specific prior written permission. 20223013Sdim * 21223013Sdim * THIS SOFTWARE IS PROVIDED BY KTH AND ITS CONTRIBUTORS ``AS IS'' AND ANY 22223013Sdim * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23223013Sdim * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 24223013Sdim * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL KTH OR ITS CONTRIBUTORS BE 25223013Sdim * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 26223013Sdim * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 27223013Sdim * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR 28223013Sdim * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 29223013Sdim * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 30223013Sdim * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 31223013Sdim * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ 32223013Sdim 33223013Sdim#include "krb5_locl.h" 34223013Sdim#include <err.h> 35223013Sdim 36223013Sdimenum { MAX_COMPONENTS = 3 }; 37223013Sdim 38223013Sdimstatic struct testcase { 39223013Sdim const char *input_string; 40223013Sdim const char *output_string; 41223013Sdim krb5_realm realm; 42223013Sdim unsigned ncomponents; 43223013Sdim char *comp_val[MAX_COMPONENTS]; 44223013Sdim int realmp; 45223013Sdim} tests[] = { 46223013Sdim {"", "@", "", 1, {""}, FALSE}, 47223013Sdim {"a", "a@", "", 1, {"a"}, FALSE}, 48223013Sdim {"\\n", "\\n@", "", 1, {"\n"}, FALSE}, 49223013Sdim {"\\ ", "\\ @", "", 1, {" "}, FALSE}, 50223013Sdim {"\\t", "\\t@", "", 1, {"\t"}, FALSE}, 51223013Sdim {"\\b", "\\b@", "", 1, {"\b"}, FALSE}, 52245431Sdim {"\\\\", "\\\\@", "", 1, {"\\"}, FALSE}, 53223013Sdim {"\\/", "\\/@", "", 1, {"/"}, FALSE}, 54223013Sdim {"\\@", "\\@@", "", 1, {"@"}, FALSE}, 55223013Sdim {"@", "@", "", 1, {""}, TRUE}, 56223013Sdim {"a/b", "a/b@", "", 2, {"a", "b"}, FALSE}, 57223013Sdim {"a/", "a/@", "", 2, {"a", ""}, FALSE}, 58223013Sdim {"a\\//\\/", "a\\//\\/@", "", 2, {"a/", "/"}, FALSE}, 59224145Sdim {"/a", "/a@", "", 2, {"", "a"}, FALSE}, 60223013Sdim {"\\@@\\@", "\\@@\\@", "@", 1, {"@"}, TRUE}, 61223013Sdim {"a/b/c", "a/b/c@", "", 3, {"a", "b", "c"}, FALSE}, 62223013Sdim {NULL, NULL, "", 0, { NULL }, FALSE}}; 63223013Sdim 64223013Sdimint 65223013Sdimmain(int argc, char **argv) 66223013Sdim{ 67223013Sdim struct testcase *t; 68223013Sdim krb5_context context; 69235633Sdim krb5_error_code ret; 70235633Sdim int val = 0; 71235633Sdim 72223013Sdim ret = krb5_init_context (&context); 73223013Sdim if (ret) 74223013Sdim errx (1, "krb5_init_context failed: %d", ret); 75223013Sdim 76245431Sdim /* to enable realm-less principal name above */ 77245431Sdim 78223013Sdim krb5_set_default_realm(context, ""); 79223013Sdim 80223013Sdim for (t = tests; t->input_string; ++t) { 81223013Sdim krb5_principal princ; 82223013Sdim int i, j; 83235633Sdim char name_buf[1024]; 84235633Sdim char *s; 85235633Sdim 86223013Sdim ret = krb5_parse_name(context, t->input_string, &princ); 87223013Sdim if (ret) 88223013Sdim krb5_err (context, 1, ret, "krb5_parse_name %s", 89223013Sdim t->input_string); 90223013Sdim if (strcmp (t->realm, princ->realm) != 0) { 91223013Sdim printf ("wrong realm (\"%s\" should be \"%s\")" 92223013Sdim " for \"%s\"\n", 93223013Sdim princ->realm, t->realm, 94223013Sdim t->input_string); 95223013Sdim val = 1; 96223013Sdim } 97223013Sdim 98223013Sdim if (t->ncomponents != princ->name.name_string.len) { 99223013Sdim printf ("wrong number of components (%u should be %u)" 100223013Sdim " for \"%s\"\n", 101223013Sdim princ->name.name_string.len, t->ncomponents, 102223013Sdim t->input_string); 103223013Sdim val = 1; 104223013Sdim } else { 105223013Sdim for (i = 0; i < t->ncomponents; ++i) { 106223013Sdim if (strcmp(t->comp_val[i], 107223013Sdim princ->name.name_string.val[i]) != 0) { 108223013Sdim printf ("bad component %d (\"%s\" should be \"%s\")" 109223013Sdim " for \"%s\"\n", 110223013Sdim i, 111223013Sdim princ->name.name_string.val[i], 112223013Sdim t->comp_val[i], 113223013Sdim t->input_string); 114223013Sdim val = 1; 115223013Sdim } 116223013Sdim } 117223013Sdim } 118223013Sdim for (j = 0; j < strlen(t->output_string); ++j) { 119223013Sdim ret = krb5_unparse_name_fixed(context, princ, 120223013Sdim name_buf, j); 121223013Sdim if (ret != ERANGE) { 122223013Sdim printf ("unparse_name %s with length %d should have failed\n", 123223013Sdim t->input_string, j); 124245431Sdim val = 1; 125223013Sdim break; 126223013Sdim } 127223013Sdim } 128245431Sdim ret = krb5_unparse_name_fixed(context, princ, 129223013Sdim name_buf, sizeof(name_buf)); 130245431Sdim if (ret) 131223013Sdim krb5_err (context, 1, ret, "krb5_unparse_name_fixed"); 132223013Sdim 133223013Sdim if (strcmp (t->output_string, name_buf) != 0) { 134223013Sdim printf ("failed comparing the re-parsed" 135223013Sdim " (\"%s\" should be \"%s\")\n", 136223013Sdim name_buf, t->output_string); 137223013Sdim val = 1; 138223013Sdim } 139223013Sdim 140223013Sdim ret = krb5_unparse_name(context, princ, &s); 141223013Sdim if (ret) 142223013Sdim krb5_err (context, 1, ret, "krb5_unparse_name"); 143 144 if (strcmp (t->output_string, s) != 0) { 145 printf ("failed comparing the re-parsed" 146 " (\"%s\" should be \"%s\"\n", 147 s, t->output_string); 148 val = 1; 149 } 150 free(s); 151 152 if (!t->realmp) { 153 for (j = 0; j < strlen(t->input_string); ++j) { 154 ret = krb5_unparse_name_fixed_short(context, princ, 155 name_buf, j); 156 if (ret != ERANGE) { 157 printf ("unparse_name_short %s with length %d" 158 " should have failed\n", 159 t->input_string, j); 160 val = 1; 161 break; 162 } 163 } 164 ret = krb5_unparse_name_fixed_short(context, princ, 165 name_buf, sizeof(name_buf)); 166 if (ret) 167 krb5_err (context, 1, ret, "krb5_unparse_name_fixed"); 168 169 if (strcmp (t->input_string, name_buf) != 0) { 170 printf ("failed comparing the re-parsed" 171 " (\"%s\" should be \"%s\")\n", 172 name_buf, t->input_string); 173 val = 1; 174 } 175 176 ret = krb5_unparse_name_short(context, princ, &s); 177 if (ret) 178 krb5_err (context, 1, ret, "krb5_unparse_name_short"); 179 180 if (strcmp (t->input_string, s) != 0) { 181 printf ("failed comparing the re-parsed" 182 " (\"%s\" should be \"%s\"\n", 183 s, t->input_string); 184 val = 1; 185 } 186 free(s); 187 } 188 krb5_free_principal (context, princ); 189 } 190 krb5_free_context(context); 191 return val; 192} 193