parse-name-test.c revision 127808
1178476Sjb/* 2178476Sjb * Copyright (c) 2002 Kungliga Tekniska H�gskolan 3178476Sjb * (Royal Institute of Technology, Stockholm, Sweden). 4178476Sjb * All rights reserved. 5178476Sjb * 6178476Sjb * Redistribution and use in source and binary forms, with or without 7178476Sjb * modification, are permitted provided that the following conditions 8178476Sjb * are met: 9178476Sjb * 10178476Sjb * 1. Redistributions of source code must retain the above copyright 11178476Sjb * notice, this list of conditions and the following disclaimer. 12178476Sjb * 13178476Sjb * 2. Redistributions in binary form must reproduce the above copyright 14178476Sjb * notice, this list of conditions and the following disclaimer in the 15178476Sjb * documentation and/or other materials provided with the distribution. 16178476Sjb * 17178476Sjb * 3. Neither the name of KTH nor the names of its contributors may be 18178476Sjb * used to endorse or promote products derived from this software without 19178476Sjb * specific prior written permission. 20178476Sjb * 21178476Sjb * THIS SOFTWARE IS PROVIDED BY KTH AND ITS CONTRIBUTORS ``AS IS'' AND ANY 22178476Sjb * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23178476Sjb * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 24178476Sjb * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL KTH OR ITS CONTRIBUTORS BE 25178476Sjb * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 26178476Sjb * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 27178476Sjb * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR 28178476Sjb * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 29178476Sjb * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 30178476Sjb * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 31178476Sjb * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ 32178476Sjb 33178476Sjb#include "krb5_locl.h" 34178476Sjb 35178476SjbRCSID("$Id: parse-name-test.c,v 1.3.4.1 2004/03/22 19:27:36 joda Exp $"); 36178476Sjb 37178476Sjbenum { MAX_COMPONENTS = 3 }; 38178476Sjb 39178476Sjbstatic struct testcase { 40178476Sjb const char *input_string; 41178476Sjb const char *output_string; 42178476Sjb krb5_realm realm; 43178476Sjb unsigned ncomponents; 44178476Sjb char *comp_val[MAX_COMPONENTS]; 45178476Sjb int realmp; 46178476Sjb} tests[] = { 47178476Sjb {"", "@", "", 1, {""}, FALSE}, 48178476Sjb {"a", "a@", "", 1, {"a"}, FALSE}, 49178476Sjb {"\\n", "\\n@", "", 1, {"\n"}, FALSE}, 50178476Sjb {"\\ ", "\\ @", "", 1, {" "}, FALSE}, 51178476Sjb {"\\t", "\\t@", "", 1, {"\t"}, FALSE}, 52178476Sjb {"\\b", "\\b@", "", 1, {"\b"}, FALSE}, 53178476Sjb {"\\\\", "\\\\@", "", 1, {"\\"}, FALSE}, 54178476Sjb {"\\/", "\\/@", "", 1, {"/"}, FALSE}, 55178476Sjb {"\\@", "\\@@", "", 1, {"@"}, FALSE}, 56178476Sjb {"@", "@", "", 1, {""}, TRUE}, 57178476Sjb {"a/b", "a/b@", "", 2, {"a", "b"}, FALSE}, 58178476Sjb {"a/", "a/@", "", 2, {"a", ""}, FALSE}, 59178476Sjb {"a\\//\\/", "a\\//\\/@", "", 2, {"a/", "/"}, FALSE}, 60178476Sjb {"/a", "/a@", "", 2, {"", "a"}, FALSE}, 61178476Sjb {"\\@@\\@", "\\@@\\@", "@", 1, {"@"}, TRUE}, 62178476Sjb {"a/b/c", "a/b/c@", "", 3, {"a", "b", "c"}, FALSE}, 63178476Sjb {NULL, NULL, "", 0, { NULL }, FALSE}}; 64178476Sjb 65178476Sjbint 66178476Sjbmain(int argc, char **argv) 67178476Sjb{ 68 struct testcase *t; 69 krb5_context context; 70 krb5_error_code ret; 71 int val = 0; 72 73 ret = krb5_init_context (&context); 74 if (ret) 75 errx (1, "krb5_init_context failed: %d", ret); 76 77 /* to enable realm-less principal name above */ 78 79 krb5_set_default_realm(context, ""); 80 81 for (t = tests; t->input_string; ++t) { 82 krb5_principal princ; 83 int i, j; 84 char name_buf[1024]; 85 char *s; 86 87 ret = krb5_parse_name(context, t->input_string, &princ); 88 if (ret) 89 krb5_err (context, 1, ret, "krb5_parse_name %s", 90 t->input_string); 91 if (strcmp (t->realm, princ->realm) != 0) { 92 printf ("wrong realm (\"%s\" should be \"%s\")" 93 " for \"%s\"\n", 94 princ->realm, t->realm, 95 t->input_string); 96 val = 1; 97 } 98 99 if (t->ncomponents != princ->name.name_string.len) { 100 printf ("wrong number of components (%u should be %u)" 101 " for \"%s\"\n", 102 princ->name.name_string.len, t->ncomponents, 103 t->input_string); 104 val = 1; 105 } else { 106 for (i = 0; i < t->ncomponents; ++i) { 107 if (strcmp(t->comp_val[i], 108 princ->name.name_string.val[i]) != 0) { 109 printf ("bad component %d (\"%s\" should be \"%s\")" 110 " for \"%s\"\n", 111 i, 112 princ->name.name_string.val[i], 113 t->comp_val[i], 114 t->input_string); 115 val = 1; 116 } 117 } 118 } 119 for (j = 0; j < strlen(t->output_string); ++j) { 120 ret = krb5_unparse_name_fixed(context, princ, 121 name_buf, j); 122 if (ret != ERANGE) { 123 printf ("unparse_name %s with length %d should have failed\n", 124 t->input_string, j); 125 val = 1; 126 break; 127 } 128 } 129 ret = krb5_unparse_name_fixed(context, princ, 130 name_buf, sizeof(name_buf)); 131 if (ret) 132 krb5_err (context, 1, ret, "krb5_unparse_name_fixed"); 133 134 if (strcmp (t->output_string, name_buf) != 0) { 135 printf ("failed comparing the re-parsed" 136 " (\"%s\" should be \"%s\")\n", 137 name_buf, t->output_string); 138 val = 1; 139 } 140 141 ret = krb5_unparse_name(context, princ, &s); 142 if (ret) 143 krb5_err (context, 1, ret, "krb5_unparse_name"); 144 145 if (strcmp (t->output_string, s) != 0) { 146 printf ("failed comparing the re-parsed" 147 " (\"%s\" should be \"%s\"\n", 148 s, t->output_string); 149 val = 1; 150 } 151 free(s); 152 153 if (!t->realmp) { 154 for (j = 0; j < strlen(t->input_string); ++j) { 155 ret = krb5_unparse_name_fixed_short(context, princ, 156 name_buf, j); 157 if (ret != ERANGE) { 158 printf ("unparse_name_short %s with length %d" 159 " should have failed\n", 160 t->input_string, j); 161 val = 1; 162 break; 163 } 164 } 165 ret = krb5_unparse_name_fixed_short(context, princ, 166 name_buf, sizeof(name_buf)); 167 if (ret) 168 krb5_err (context, 1, ret, "krb5_unparse_name_fixed"); 169 170 if (strcmp (t->input_string, name_buf) != 0) { 171 printf ("failed comparing the re-parsed" 172 " (\"%s\" should be \"%s\")\n", 173 name_buf, t->input_string); 174 val = 1; 175 } 176 177 ret = krb5_unparse_name_short(context, princ, &s); 178 if (ret) 179 krb5_err (context, 1, ret, "krb5_unparse_name_short"); 180 181 if (strcmp (t->input_string, s) != 0) { 182 printf ("failed comparing the re-parsed" 183 " (\"%s\" should be \"%s\"\n", 184 s, t->input_string); 185 val = 1; 186 } 187 free(s); 188 } 189 krb5_free_principal (context, princ); 190 } 191 return val; 192} 193