xref: /minix3/crypto/external/bsd/heimdal/dist/lib/krb5/test_config.c (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
1*0a6a1f1dSLionel Sambuc /*	$NetBSD: test_config.c,v 1.1.1.2 2014/04/24 12:45:51 pettai Exp $	*/
2ebfedea0SLionel Sambuc 
3ebfedea0SLionel Sambuc /*
4ebfedea0SLionel Sambuc  * Copyright (c) 2003 Kungliga Tekniska Högskolan
5ebfedea0SLionel Sambuc  * (Royal Institute of Technology, Stockholm, Sweden).
6ebfedea0SLionel Sambuc  * All rights reserved.
7ebfedea0SLionel Sambuc  *
8ebfedea0SLionel Sambuc  * Redistribution and use in source and binary forms, with or without
9ebfedea0SLionel Sambuc  * modification, are permitted provided that the following conditions
10ebfedea0SLionel Sambuc  * are met:
11ebfedea0SLionel Sambuc  *
12ebfedea0SLionel Sambuc  * 1. Redistributions of source code must retain the above copyright
13ebfedea0SLionel Sambuc  *    notice, this list of conditions and the following disclaimer.
14ebfedea0SLionel Sambuc  *
15ebfedea0SLionel Sambuc  * 2. Redistributions in binary form must reproduce the above copyright
16ebfedea0SLionel Sambuc  *    notice, this list of conditions and the following disclaimer in the
17ebfedea0SLionel Sambuc  *    documentation and/or other materials provided with the distribution.
18ebfedea0SLionel Sambuc  *
19ebfedea0SLionel Sambuc  * 3. Neither the name of KTH nor the names of its contributors may be
20ebfedea0SLionel Sambuc  *    used to endorse or promote products derived from this software without
21ebfedea0SLionel Sambuc  *    specific prior written permission.
22ebfedea0SLionel Sambuc  *
23ebfedea0SLionel Sambuc  * THIS SOFTWARE IS PROVIDED BY KTH AND ITS CONTRIBUTORS ``AS IS'' AND ANY
24ebfedea0SLionel Sambuc  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25ebfedea0SLionel Sambuc  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
26ebfedea0SLionel Sambuc  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL KTH OR ITS CONTRIBUTORS BE
27ebfedea0SLionel Sambuc  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
28ebfedea0SLionel Sambuc  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
29ebfedea0SLionel Sambuc  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
30ebfedea0SLionel Sambuc  * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
31ebfedea0SLionel Sambuc  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
32ebfedea0SLionel Sambuc  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
33ebfedea0SLionel Sambuc  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34ebfedea0SLionel Sambuc  */
35ebfedea0SLionel Sambuc 
36ebfedea0SLionel Sambuc #include "krb5_locl.h"
37ebfedea0SLionel Sambuc #include <err.h>
38ebfedea0SLionel Sambuc 
39ebfedea0SLionel Sambuc static int
check_config_file(krb5_context context,char * filelist,char ** res,int def)40ebfedea0SLionel Sambuc check_config_file(krb5_context context, char *filelist, char **res, int def)
41ebfedea0SLionel Sambuc {
42ebfedea0SLionel Sambuc     krb5_error_code ret;
43ebfedea0SLionel Sambuc     char **pp;
44ebfedea0SLionel Sambuc     int i;
45ebfedea0SLionel Sambuc 
46ebfedea0SLionel Sambuc     pp = NULL;
47ebfedea0SLionel Sambuc 
48ebfedea0SLionel Sambuc     if (def)
49ebfedea0SLionel Sambuc 	ret = krb5_prepend_config_files_default(filelist, &pp);
50ebfedea0SLionel Sambuc     else
51ebfedea0SLionel Sambuc 	ret = krb5_prepend_config_files(filelist, NULL, &pp);
52ebfedea0SLionel Sambuc 
53ebfedea0SLionel Sambuc     if (ret)
54ebfedea0SLionel Sambuc 	krb5_err(context, 1, ret, "prepend_config_files");
55ebfedea0SLionel Sambuc 
56ebfedea0SLionel Sambuc     for (i = 0; res[i] && pp[i]; i++)
57ebfedea0SLionel Sambuc 	if (strcmp(pp[i], res[i]) != 0)
58ebfedea0SLionel Sambuc 	    krb5_errx(context, 1, "'%s' != '%s'", pp[i], res[i]);
59ebfedea0SLionel Sambuc 
60ebfedea0SLionel Sambuc     if (res[i] != NULL)
61ebfedea0SLionel Sambuc 	krb5_errx(context, 1, "pp ended before res list");
62ebfedea0SLionel Sambuc 
63ebfedea0SLionel Sambuc     if (def) {
64ebfedea0SLionel Sambuc 	char **deflist;
65ebfedea0SLionel Sambuc 	int j;
66ebfedea0SLionel Sambuc 
67ebfedea0SLionel Sambuc 	ret = krb5_get_default_config_files(&deflist);
68ebfedea0SLionel Sambuc 	if (ret)
69ebfedea0SLionel Sambuc 	    krb5_err(context, 1, ret, "get_default_config_files");
70ebfedea0SLionel Sambuc 
71ebfedea0SLionel Sambuc 	for (j = 0 ; pp[i] && deflist[j]; i++, j++)
72ebfedea0SLionel Sambuc 	    if (strcmp(pp[i], deflist[j]) != 0)
73ebfedea0SLionel Sambuc 		krb5_errx(context, 1, "'%s' != '%s'", pp[i], deflist[j]);
74ebfedea0SLionel Sambuc 
75ebfedea0SLionel Sambuc 	if (deflist[j] != NULL)
76ebfedea0SLionel Sambuc 	    krb5_errx(context, 1, "pp ended before def list");
77ebfedea0SLionel Sambuc 	krb5_free_config_files(deflist);
78ebfedea0SLionel Sambuc     }
79ebfedea0SLionel Sambuc 
80ebfedea0SLionel Sambuc     if (pp[i] != NULL)
81ebfedea0SLionel Sambuc 	krb5_errx(context, 1, "pp ended after res (and def) list");
82ebfedea0SLionel Sambuc 
83ebfedea0SLionel Sambuc     krb5_free_config_files(pp);
84ebfedea0SLionel Sambuc 
85ebfedea0SLionel Sambuc     return 0;
86ebfedea0SLionel Sambuc }
87ebfedea0SLionel Sambuc 
88ebfedea0SLionel Sambuc char *list0[] =  { "/tmp/foo", NULL };
89ebfedea0SLionel Sambuc char *list1[] =  { "/tmp/foo", "/tmp/foo/bar", NULL };
90ebfedea0SLionel Sambuc char *list2[] =  { "", NULL };
91ebfedea0SLionel Sambuc 
92ebfedea0SLionel Sambuc struct {
93ebfedea0SLionel Sambuc     char *fl;
94ebfedea0SLionel Sambuc     char **res;
95ebfedea0SLionel Sambuc } test[] = {
96ebfedea0SLionel Sambuc     { "/tmp/foo", NULL },
97ebfedea0SLionel Sambuc     { "/tmp/foo" PATH_SEP "/tmp/foo/bar", NULL },
98ebfedea0SLionel Sambuc     { "", NULL }
99ebfedea0SLionel Sambuc };
100ebfedea0SLionel Sambuc 
101ebfedea0SLionel Sambuc static void
check_config_files(void)102ebfedea0SLionel Sambuc check_config_files(void)
103ebfedea0SLionel Sambuc {
104ebfedea0SLionel Sambuc     krb5_context context;
105ebfedea0SLionel Sambuc     krb5_error_code ret;
106ebfedea0SLionel Sambuc     int i;
107ebfedea0SLionel Sambuc 
108ebfedea0SLionel Sambuc     ret = krb5_init_context(&context);
109ebfedea0SLionel Sambuc     if (ret)
110ebfedea0SLionel Sambuc 	errx(1, "krb5_init_context %d", ret);
111ebfedea0SLionel Sambuc 
112ebfedea0SLionel Sambuc     test[0].res = list0;
113ebfedea0SLionel Sambuc     test[1].res = list1;
114ebfedea0SLionel Sambuc     test[2].res = list2;
115ebfedea0SLionel Sambuc 
116ebfedea0SLionel Sambuc     for (i = 0; i < sizeof(test)/sizeof(*test); i++) {
117ebfedea0SLionel Sambuc 	check_config_file(context, test[i].fl, test[i].res, 0);
118ebfedea0SLionel Sambuc 	check_config_file(context, test[i].fl, test[i].res, 1);
119ebfedea0SLionel Sambuc     }
120ebfedea0SLionel Sambuc 
121ebfedea0SLionel Sambuc     krb5_free_context(context);
122ebfedea0SLionel Sambuc }
123ebfedea0SLionel Sambuc 
124ebfedea0SLionel Sambuc const char *config_string_result0[] = {
125ebfedea0SLionel Sambuc     "A", "B", "C", "D", NULL
126ebfedea0SLionel Sambuc };
127ebfedea0SLionel Sambuc 
128ebfedea0SLionel Sambuc const char *config_string_result1[] = {
129ebfedea0SLionel Sambuc     "A", "B", "C D", NULL
130ebfedea0SLionel Sambuc };
131ebfedea0SLionel Sambuc 
132ebfedea0SLionel Sambuc const char *config_string_result2[] = {
133ebfedea0SLionel Sambuc     "A", "B", "", NULL
134ebfedea0SLionel Sambuc };
135ebfedea0SLionel Sambuc 
136ebfedea0SLionel Sambuc const char *config_string_result3[] = {
137ebfedea0SLionel Sambuc     "A B;C: D", NULL
138ebfedea0SLionel Sambuc };
139ebfedea0SLionel Sambuc 
140ebfedea0SLionel Sambuc const char *config_string_result4[] = {
141ebfedea0SLionel Sambuc     "\"\"", "", "\"\"", NULL
142ebfedea0SLionel Sambuc };
143ebfedea0SLionel Sambuc 
144ebfedea0SLionel Sambuc const char *config_string_result5[] = {
145ebfedea0SLionel Sambuc     "A\"BQd", NULL
146ebfedea0SLionel Sambuc };
147ebfedea0SLionel Sambuc 
148ebfedea0SLionel Sambuc const char *config_string_result6[] = {
149ebfedea0SLionel Sambuc     "efgh\"", "ABC", NULL
150ebfedea0SLionel Sambuc };
151ebfedea0SLionel Sambuc 
152ebfedea0SLionel Sambuc const char *config_string_result7[] = {
153ebfedea0SLionel Sambuc     "SnapeKills\\", "Dumbledore", NULL
154ebfedea0SLionel Sambuc };
155ebfedea0SLionel Sambuc 
156ebfedea0SLionel Sambuc const char *config_string_result8[] = {
157ebfedea0SLionel Sambuc     "\"TownOf Sandwich: Massachusetts\"Oldest", "Town", "In", "Cape Cod", NULL
158ebfedea0SLionel Sambuc };
159ebfedea0SLionel Sambuc 
160ebfedea0SLionel Sambuc const char *config_string_result9[] = {
161ebfedea0SLionel Sambuc     "\"Begins and\"ends", "In", "One", "String", NULL
162ebfedea0SLionel Sambuc };
163ebfedea0SLionel Sambuc 
164ebfedea0SLionel Sambuc const char *config_string_result10[] = {
165ebfedea0SLionel Sambuc     "Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:",
166ebfedea0SLionel Sambuc     "1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.",
167ebfedea0SLionel Sambuc     "2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.",
168ebfedea0SLionel Sambuc     "3. Neither the name of the Institute nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.",
169ebfedea0SLionel Sambuc     "THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.",
170ebfedea0SLionel Sambuc     "Why do we test with such long strings? Because some people have config files",
171ebfedea0SLionel Sambuc     "That", "look", "Like this.", NULL
172ebfedea0SLionel Sambuc };
173ebfedea0SLionel Sambuc 
174ebfedea0SLionel Sambuc const struct {
175ebfedea0SLionel Sambuc     const char * name;
176ebfedea0SLionel Sambuc     const char ** expected;
177ebfedea0SLionel Sambuc } config_strings_tests[] = {
178ebfedea0SLionel Sambuc     { "foo", config_string_result0 },
179ebfedea0SLionel Sambuc     { "bar", config_string_result1 },
180ebfedea0SLionel Sambuc     { "baz", config_string_result2 },
181ebfedea0SLionel Sambuc     { "quux", config_string_result3 },
182ebfedea0SLionel Sambuc     { "questionable", config_string_result4 },
183ebfedea0SLionel Sambuc     { "mismatch1", config_string_result5 },
184ebfedea0SLionel Sambuc     { "mismatch2", config_string_result6 },
185ebfedea0SLionel Sambuc     { "internal1", config_string_result7 },
186ebfedea0SLionel Sambuc     { "internal2", config_string_result8 },
187ebfedea0SLionel Sambuc     { "internal3", config_string_result9 },
188ebfedea0SLionel Sambuc     { "longer_strings", config_string_result10 }
189ebfedea0SLionel Sambuc };
190ebfedea0SLionel Sambuc 
191ebfedea0SLionel Sambuc static void
check_escaped_strings(void)192ebfedea0SLionel Sambuc check_escaped_strings(void)
193ebfedea0SLionel Sambuc {
194ebfedea0SLionel Sambuc     krb5_context context;
195ebfedea0SLionel Sambuc     krb5_config_section *c = NULL;
196ebfedea0SLionel Sambuc     krb5_error_code ret;
197ebfedea0SLionel Sambuc     int i;
198ebfedea0SLionel Sambuc 
199ebfedea0SLionel Sambuc     ret = krb5_init_context(&context);
200ebfedea0SLionel Sambuc     if (ret)
201ebfedea0SLionel Sambuc         errx(1, "krb5_init_context %d", ret);
202ebfedea0SLionel Sambuc 
203ebfedea0SLionel Sambuc     ret = krb5_config_parse_file(context, "test_config_strings.out", &c);
204ebfedea0SLionel Sambuc     if (ret)
205ebfedea0SLionel Sambuc         krb5_errx(context, 1, "krb5_config_parse_file()");
206ebfedea0SLionel Sambuc 
207ebfedea0SLionel Sambuc     for (i=0; i < sizeof(config_strings_tests)/sizeof(config_strings_tests[0]); i++) {
208ebfedea0SLionel Sambuc         char **ps;
209ebfedea0SLionel Sambuc         const char **s;
210ebfedea0SLionel Sambuc         const char **e;
211ebfedea0SLionel Sambuc 
212ebfedea0SLionel Sambuc         ps = krb5_config_get_strings(context, c, "escapes", config_strings_tests[i].name,
213ebfedea0SLionel Sambuc                                      NULL);
214ebfedea0SLionel Sambuc         if (ps == NULL)
215ebfedea0SLionel Sambuc             errx(1, "Failed to read string value %s", config_strings_tests[i].name);
216ebfedea0SLionel Sambuc 
217ebfedea0SLionel Sambuc         e = config_strings_tests[i].expected;
218ebfedea0SLionel Sambuc 
219ebfedea0SLionel Sambuc         for (s = (const char **)ps; *s && *e; s++, e++) {
220ebfedea0SLionel Sambuc             if (strcmp(*s, *e))
221ebfedea0SLionel Sambuc                 errx(1,
222ebfedea0SLionel Sambuc                      "Unexpected configuration string at value [%s].\n"
223ebfedea0SLionel Sambuc                      "Actual=[%s]\n"
224ebfedea0SLionel Sambuc                      "Expected=[%s]\n",
225ebfedea0SLionel Sambuc                      config_strings_tests[i].name, *s, *e);
226ebfedea0SLionel Sambuc         }
227ebfedea0SLionel Sambuc 
228ebfedea0SLionel Sambuc         if (*s || *e)
229ebfedea0SLionel Sambuc             errx(1, "Configuation string list for value [%s] has incorrect length.",
230ebfedea0SLionel Sambuc 		 config_strings_tests[i].name);
231ebfedea0SLionel Sambuc 
232ebfedea0SLionel Sambuc         krb5_config_free_strings(ps);
233ebfedea0SLionel Sambuc     }
234ebfedea0SLionel Sambuc 
235ebfedea0SLionel Sambuc     ret = krb5_config_file_free(context, c);
236ebfedea0SLionel Sambuc     if (ret)
237ebfedea0SLionel Sambuc         krb5_errx(context, 1, "krb5_config_file_free()");
238ebfedea0SLionel Sambuc 
239ebfedea0SLionel Sambuc     krb5_free_context(context);
240ebfedea0SLionel Sambuc }
241ebfedea0SLionel Sambuc 
242ebfedea0SLionel Sambuc int
main(int argc,char ** argv)243ebfedea0SLionel Sambuc main(int argc, char **argv)
244ebfedea0SLionel Sambuc {
245ebfedea0SLionel Sambuc     check_config_files();
246ebfedea0SLionel Sambuc     check_escaped_strings();
247ebfedea0SLionel Sambuc     return 0;
248ebfedea0SLionel Sambuc }
249