1102644Snectar/* 2233294Sstas * Copyright (c) 2002 Kungliga Tekniska H��gskolan 3233294Sstas * (Royal Institute of Technology, Stockholm, Sweden). 4233294Sstas * All rights reserved. 5102644Snectar * 6233294Sstas * Redistribution and use in source and binary forms, with or without 7233294Sstas * modification, are permitted provided that the following conditions 8233294Sstas * are met: 9102644Snectar * 10233294Sstas * 1. Redistributions of source code must retain the above copyright 11233294Sstas * notice, this list of conditions and the following disclaimer. 12102644Snectar * 13233294Sstas * 2. Redistributions in binary form must reproduce the above copyright 14233294Sstas * notice, this list of conditions and the following disclaimer in the 15233294Sstas * documentation and/or other materials provided with the distribution. 16102644Snectar * 17102644Snectar * 3. Neither the name of KTH nor the names of its contributors may be 18102644Snectar * used to endorse or promote products derived from this software without 19102644Snectar * specific prior written permission. 20102644Snectar * 21102644Snectar * THIS SOFTWARE IS PROVIDED BY KTH AND ITS CONTRIBUTORS ``AS IS'' AND ANY 22102644Snectar * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23102644Snectar * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 24102644Snectar * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL KTH OR ITS CONTRIBUTORS BE 25102644Snectar * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 26102644Snectar * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 27102644Snectar * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR 28102644Snectar * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 29102644Snectar * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 30102644Snectar * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 31102644Snectar * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ 32102644Snectar 33102644Snectar#include "krb5_locl.h" 34178825Sdfr#include <err.h> 35102644Snectar 36102644Snectarenum { MAX_COMPONENTS = 3 }; 37102644Snectar 38102644Snectarstatic struct testcase { 39102644Snectar const char *input_string; 40102644Snectar const char *output_string; 41102644Snectar krb5_realm realm; 42102644Snectar unsigned ncomponents; 43102644Snectar char *comp_val[MAX_COMPONENTS]; 44102644Snectar int realmp; 45102644Snectar} tests[] = { 46102644Snectar {"", "@", "", 1, {""}, FALSE}, 47102644Snectar {"a", "a@", "", 1, {"a"}, FALSE}, 48102644Snectar {"\\n", "\\n@", "", 1, {"\n"}, FALSE}, 49102644Snectar {"\\ ", "\\ @", "", 1, {" "}, FALSE}, 50102644Snectar {"\\t", "\\t@", "", 1, {"\t"}, FALSE}, 51102644Snectar {"\\b", "\\b@", "", 1, {"\b"}, FALSE}, 52102644Snectar {"\\\\", "\\\\@", "", 1, {"\\"}, FALSE}, 53102644Snectar {"\\/", "\\/@", "", 1, {"/"}, FALSE}, 54102644Snectar {"\\@", "\\@@", "", 1, {"@"}, FALSE}, 55102644Snectar {"@", "@", "", 1, {""}, TRUE}, 56102644Snectar {"a/b", "a/b@", "", 2, {"a", "b"}, FALSE}, 57102644Snectar {"a/", "a/@", "", 2, {"a", ""}, FALSE}, 58102644Snectar {"a\\//\\/", "a\\//\\/@", "", 2, {"a/", "/"}, FALSE}, 59102644Snectar {"/a", "/a@", "", 2, {"", "a"}, FALSE}, 60102644Snectar {"\\@@\\@", "\\@@\\@", "@", 1, {"@"}, TRUE}, 61102644Snectar {"a/b/c", "a/b/c@", "", 3, {"a", "b", "c"}, FALSE}, 62127808Snectar {NULL, NULL, "", 0, { NULL }, FALSE}}; 63102644Snectar 64233294Sstasint 65102644Snectarmain(int argc, char **argv) 66102644Snectar{ 67102644Snectar struct testcase *t; 68102644Snectar krb5_context context; 69102644Snectar krb5_error_code ret; 70102644Snectar int val = 0; 71102644Snectar 72102644Snectar ret = krb5_init_context (&context); 73102644Snectar if (ret) 74102644Snectar errx (1, "krb5_init_context failed: %d", ret); 75102644Snectar 76102644Snectar /* to enable realm-less principal name above */ 77102644Snectar 78102644Snectar krb5_set_default_realm(context, ""); 79102644Snectar 80102644Snectar for (t = tests; t->input_string; ++t) { 81102644Snectar krb5_principal princ; 82102644Snectar int i, j; 83102644Snectar char name_buf[1024]; 84102644Snectar char *s; 85102644Snectar 86102644Snectar ret = krb5_parse_name(context, t->input_string, &princ); 87102644Snectar if (ret) 88102644Snectar krb5_err (context, 1, ret, "krb5_parse_name %s", 89102644Snectar t->input_string); 90102644Snectar if (strcmp (t->realm, princ->realm) != 0) { 91102644Snectar printf ("wrong realm (\"%s\" should be \"%s\")" 92102644Snectar " for \"%s\"\n", 93102644Snectar princ->realm, t->realm, 94102644Snectar t->input_string); 95102644Snectar val = 1; 96102644Snectar } 97102644Snectar 98102644Snectar if (t->ncomponents != princ->name.name_string.len) { 99102644Snectar printf ("wrong number of components (%u should be %u)" 100102644Snectar " for \"%s\"\n", 101102644Snectar princ->name.name_string.len, t->ncomponents, 102102644Snectar t->input_string); 103102644Snectar val = 1; 104102644Snectar } else { 105102644Snectar for (i = 0; i < t->ncomponents; ++i) { 106102644Snectar if (strcmp(t->comp_val[i], 107102644Snectar princ->name.name_string.val[i]) != 0) { 108102644Snectar printf ("bad component %d (\"%s\" should be \"%s\")" 109102644Snectar " for \"%s\"\n", 110102644Snectar i, 111102644Snectar princ->name.name_string.val[i], 112102644Snectar t->comp_val[i], 113102644Snectar t->input_string); 114102644Snectar val = 1; 115102644Snectar } 116102644Snectar } 117102644Snectar } 118102644Snectar for (j = 0; j < strlen(t->output_string); ++j) { 119102644Snectar ret = krb5_unparse_name_fixed(context, princ, 120102644Snectar name_buf, j); 121102644Snectar if (ret != ERANGE) { 122102644Snectar printf ("unparse_name %s with length %d should have failed\n", 123102644Snectar t->input_string, j); 124102644Snectar val = 1; 125102644Snectar break; 126102644Snectar } 127102644Snectar } 128102644Snectar ret = krb5_unparse_name_fixed(context, princ, 129102644Snectar name_buf, sizeof(name_buf)); 130102644Snectar if (ret) 131102644Snectar krb5_err (context, 1, ret, "krb5_unparse_name_fixed"); 132102644Snectar 133102644Snectar if (strcmp (t->output_string, name_buf) != 0) { 134102644Snectar printf ("failed comparing the re-parsed" 135102644Snectar " (\"%s\" should be \"%s\")\n", 136102644Snectar name_buf, t->output_string); 137102644Snectar val = 1; 138102644Snectar } 139102644Snectar 140102644Snectar ret = krb5_unparse_name(context, princ, &s); 141102644Snectar if (ret) 142102644Snectar krb5_err (context, 1, ret, "krb5_unparse_name"); 143102644Snectar 144102644Snectar if (strcmp (t->output_string, s) != 0) { 145102644Snectar printf ("failed comparing the re-parsed" 146102644Snectar " (\"%s\" should be \"%s\"\n", 147102644Snectar s, t->output_string); 148102644Snectar val = 1; 149102644Snectar } 150102644Snectar free(s); 151102644Snectar 152102644Snectar if (!t->realmp) { 153102644Snectar for (j = 0; j < strlen(t->input_string); ++j) { 154102644Snectar ret = krb5_unparse_name_fixed_short(context, princ, 155102644Snectar name_buf, j); 156102644Snectar if (ret != ERANGE) { 157102644Snectar printf ("unparse_name_short %s with length %d" 158102644Snectar " should have failed\n", 159102644Snectar t->input_string, j); 160102644Snectar val = 1; 161102644Snectar break; 162102644Snectar } 163102644Snectar } 164102644Snectar ret = krb5_unparse_name_fixed_short(context, princ, 165102644Snectar name_buf, sizeof(name_buf)); 166102644Snectar if (ret) 167102644Snectar krb5_err (context, 1, ret, "krb5_unparse_name_fixed"); 168102644Snectar 169102644Snectar if (strcmp (t->input_string, name_buf) != 0) { 170102644Snectar printf ("failed comparing the re-parsed" 171102644Snectar " (\"%s\" should be \"%s\")\n", 172102644Snectar name_buf, t->input_string); 173102644Snectar val = 1; 174102644Snectar } 175102644Snectar 176102644Snectar ret = krb5_unparse_name_short(context, princ, &s); 177102644Snectar if (ret) 178102644Snectar krb5_err (context, 1, ret, "krb5_unparse_name_short"); 179102644Snectar 180102644Snectar if (strcmp (t->input_string, s) != 0) { 181102644Snectar printf ("failed comparing the re-parsed" 182102644Snectar " (\"%s\" should be \"%s\"\n", 183102644Snectar s, t->input_string); 184102644Snectar val = 1; 185102644Snectar } 186102644Snectar free(s); 187102644Snectar } 188102644Snectar krb5_free_principal (context, princ); 189102644Snectar } 190178825Sdfr krb5_free_context(context); 191102644Snectar return val; 192102644Snectar} 193