1 /*
2  * Copyright (c) 2002 Kungliga Tekniska H�gskolan
3  * (Royal Institute of Technology, Stockholm, Sweden).
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  *
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  *
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  *
17  * 3. Neither the name of KTH nor the names of its contributors may be
18  *    used to endorse or promote products derived from this software without
19  *    specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY KTH AND ITS CONTRIBUTORS ``AS IS'' AND ANY
22  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
24  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL KTH OR ITS CONTRIBUTORS BE
25  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
28  * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
29  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
30  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
31  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */
32 
33 #include "krb5_locl.h"
34 
35 RCSID("$Id: parse-name-test.c,v 1.3.4.1 2004/03/22 19:27:36 joda Exp $");
36 
37 enum { MAX_COMPONENTS = 3 };
38 
39 static struct testcase {
40     const char *input_string;
41     const char *output_string;
42     krb5_realm realm;
43     unsigned ncomponents;
44     char *comp_val[MAX_COMPONENTS];
45     int realmp;
46 } tests[] = {
47     {"", "@", "", 1, {""}, FALSE},
48     {"a", "a@", "", 1, {"a"}, FALSE},
49     {"\\n", "\\n@", "", 1, {"\n"}, FALSE},
50     {"\\ ", "\\ @", "", 1, {" "}, FALSE},
51     {"\\t", "\\t@", "", 1, {"\t"}, FALSE},
52     {"\\b", "\\b@", "", 1, {"\b"}, FALSE},
53     {"\\\\", "\\\\@", "", 1, {"\\"}, FALSE},
54     {"\\/", "\\/@", "", 1, {"/"}, FALSE},
55     {"\\@", "\\@@", "", 1, {"@"}, FALSE},
56     {"@", "@", "", 1, {""}, TRUE},
57     {"a/b", "a/b@", "", 2, {"a", "b"}, FALSE},
58     {"a/", "a/@", "", 2, {"a", ""}, FALSE},
59     {"a\\//\\/", "a\\//\\/@", "", 2, {"a/", "/"}, FALSE},
60     {"/a", "/a@", "", 2, {"", "a"}, FALSE},
61     {"\\@@\\@", "\\@@\\@", "@", 1, {"@"}, TRUE},
62     {"a/b/c", "a/b/c@", "", 3, {"a", "b", "c"}, FALSE},
63     {NULL, NULL, "", 0, { NULL }, FALSE}};
64 
65 int
66 main(int argc, char **argv)
67 {
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