1178825Sdfr/*
2233294Sstas * Copyright (c) 2003 Kungliga Tekniska H��gskolan
3233294Sstas * (Royal Institute of Technology, Stockholm, Sweden).
4233294Sstas * All rights reserved.
5178825Sdfr *
6233294Sstas * Redistribution and use in source and binary forms, with or without
7233294Sstas * modification, are permitted provided that the following conditions
8233294Sstas * are met:
9178825Sdfr *
10233294Sstas * 1. Redistributions of source code must retain the above copyright
11233294Sstas *    notice, this list of conditions and the following disclaimer.
12178825Sdfr *
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.
16178825Sdfr *
17178825Sdfr * 3. Neither the name of KTH nor the names of its contributors may be
18178825Sdfr *    used to endorse or promote products derived from this software without
19178825Sdfr *    specific prior written permission.
20178825Sdfr *
21178825Sdfr * THIS SOFTWARE IS PROVIDED BY KTH AND ITS CONTRIBUTORS ``AS IS'' AND ANY
22178825Sdfr * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23178825Sdfr * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
24178825Sdfr * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL KTH OR ITS CONTRIBUTORS BE
25178825Sdfr * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26178825Sdfr * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27178825Sdfr * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
28178825Sdfr * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
29178825Sdfr * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
30178825Sdfr * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
31178825Sdfr * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32178825Sdfr */
33178825Sdfr
34178825Sdfr#include "krb5_locl.h"
35178825Sdfr#include <err.h>
36178825Sdfr
37178825Sdfrstatic int
38178825Sdfrcheck_config_file(krb5_context context, char *filelist, char **res, int def)
39178825Sdfr{
40178825Sdfr    krb5_error_code ret;
41178825Sdfr    char **pp;
42178825Sdfr    int i;
43178825Sdfr
44178825Sdfr    pp = NULL;
45178825Sdfr
46178825Sdfr    if (def)
47178825Sdfr	ret = krb5_prepend_config_files_default(filelist, &pp);
48178825Sdfr    else
49178825Sdfr	ret = krb5_prepend_config_files(filelist, NULL, &pp);
50233294Sstas
51178825Sdfr    if (ret)
52178825Sdfr	krb5_err(context, 1, ret, "prepend_config_files");
53233294Sstas
54178825Sdfr    for (i = 0; res[i] && pp[i]; i++)
55178825Sdfr	if (strcmp(pp[i], res[i]) != 0)
56178825Sdfr	    krb5_errx(context, 1, "'%s' != '%s'", pp[i], res[i]);
57233294Sstas
58178825Sdfr    if (res[i] != NULL)
59178825Sdfr	krb5_errx(context, 1, "pp ended before res list");
60233294Sstas
61178825Sdfr    if (def) {
62178825Sdfr	char **deflist;
63178825Sdfr	int j;
64233294Sstas
65178825Sdfr	ret = krb5_get_default_config_files(&deflist);
66178825Sdfr	if (ret)
67178825Sdfr	    krb5_err(context, 1, ret, "get_default_config_files");
68233294Sstas
69178825Sdfr	for (j = 0 ; pp[i] && deflist[j]; i++, j++)
70178825Sdfr	    if (strcmp(pp[i], deflist[j]) != 0)
71178825Sdfr		krb5_errx(context, 1, "'%s' != '%s'", pp[i], deflist[j]);
72233294Sstas
73178825Sdfr	if (deflist[j] != NULL)
74178825Sdfr	    krb5_errx(context, 1, "pp ended before def list");
75178825Sdfr	krb5_free_config_files(deflist);
76178825Sdfr    }
77233294Sstas
78178825Sdfr    if (pp[i] != NULL)
79178825Sdfr	krb5_errx(context, 1, "pp ended after res (and def) list");
80233294Sstas
81178825Sdfr    krb5_free_config_files(pp);
82233294Sstas
83178825Sdfr    return 0;
84178825Sdfr}
85178825Sdfr
86178825Sdfrchar *list0[] =  { "/tmp/foo", NULL };
87178825Sdfrchar *list1[] =  { "/tmp/foo", "/tmp/foo/bar", NULL };
88178825Sdfrchar *list2[] =  { "", NULL };
89178825Sdfr
90178825Sdfrstruct {
91178825Sdfr    char *fl;
92178825Sdfr    char **res;
93178825Sdfr} test[] = {
94178825Sdfr    { "/tmp/foo", NULL },
95233294Sstas    { "/tmp/foo" PATH_SEP "/tmp/foo/bar", NULL },
96178825Sdfr    { "", NULL }
97178825Sdfr};
98178825Sdfr
99233294Sstasstatic void
100233294Sstascheck_config_files(void)
101178825Sdfr{
102178825Sdfr    krb5_context context;
103178825Sdfr    krb5_error_code ret;
104178825Sdfr    int i;
105178825Sdfr
106178825Sdfr    ret = krb5_init_context(&context);
107178825Sdfr    if (ret)
108178825Sdfr	errx(1, "krb5_init_context %d", ret);
109178825Sdfr
110178825Sdfr    test[0].res = list0;
111178825Sdfr    test[1].res = list1;
112178825Sdfr    test[2].res = list2;
113178825Sdfr
114178825Sdfr    for (i = 0; i < sizeof(test)/sizeof(*test); i++) {
115178825Sdfr	check_config_file(context, test[i].fl, test[i].res, 0);
116178825Sdfr	check_config_file(context, test[i].fl, test[i].res, 1);
117178825Sdfr    }
118178825Sdfr
119178825Sdfr    krb5_free_context(context);
120233294Sstas}
121178825Sdfr
122233294Sstasconst char *config_string_result0[] = {
123233294Sstas    "A", "B", "C", "D", NULL
124233294Sstas};
125233294Sstas
126233294Sstasconst char *config_string_result1[] = {
127233294Sstas    "A", "B", "C D", NULL
128233294Sstas};
129233294Sstas
130233294Sstasconst char *config_string_result2[] = {
131233294Sstas    "A", "B", "", NULL
132233294Sstas};
133233294Sstas
134233294Sstasconst char *config_string_result3[] = {
135233294Sstas    "A B;C: D", NULL
136233294Sstas};
137233294Sstas
138233294Sstasconst char *config_string_result4[] = {
139233294Sstas    "\"\"", "", "\"\"", NULL
140233294Sstas};
141233294Sstas
142233294Sstasconst char *config_string_result5[] = {
143233294Sstas    "A\"BQd", NULL
144233294Sstas};
145233294Sstas
146233294Sstasconst char *config_string_result6[] = {
147233294Sstas    "efgh\"", "ABC", NULL
148233294Sstas};
149233294Sstas
150233294Sstasconst char *config_string_result7[] = {
151233294Sstas    "SnapeKills\\", "Dumbledore", NULL
152233294Sstas};
153233294Sstas
154233294Sstasconst char *config_string_result8[] = {
155233294Sstas    "\"TownOf Sandwich: Massachusetts\"Oldest", "Town", "In", "Cape Cod", NULL
156233294Sstas};
157233294Sstas
158233294Sstasconst char *config_string_result9[] = {
159233294Sstas    "\"Begins and\"ends", "In", "One", "String", NULL
160233294Sstas};
161233294Sstas
162233294Sstasconst char *config_string_result10[] = {
163233294Sstas    "Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:",
164233294Sstas    "1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.",
165233294Sstas    "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.",
166233294Sstas    "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.",
167233294Sstas    "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.",
168233294Sstas    "Why do we test with such long strings? Because some people have config files",
169233294Sstas    "That", "look", "Like this.", NULL
170233294Sstas};
171233294Sstas
172233294Sstasconst struct {
173233294Sstas    const char * name;
174233294Sstas    const char ** expected;
175233294Sstas} config_strings_tests[] = {
176233294Sstas    { "foo", config_string_result0 },
177233294Sstas    { "bar", config_string_result1 },
178233294Sstas    { "baz", config_string_result2 },
179233294Sstas    { "quux", config_string_result3 },
180233294Sstas    { "questionable", config_string_result4 },
181233294Sstas    { "mismatch1", config_string_result5 },
182233294Sstas    { "mismatch2", config_string_result6 },
183233294Sstas    { "internal1", config_string_result7 },
184233294Sstas    { "internal2", config_string_result8 },
185233294Sstas    { "internal3", config_string_result9 },
186233294Sstas    { "longer_strings", config_string_result10 }
187233294Sstas};
188233294Sstas
189233294Sstasstatic void
190233294Sstascheck_escaped_strings(void)
191233294Sstas{
192233294Sstas    krb5_context context;
193233294Sstas    krb5_config_section *c = NULL;
194233294Sstas    krb5_error_code ret;
195233294Sstas    int i;
196233294Sstas
197233294Sstas    ret = krb5_init_context(&context);
198233294Sstas    if (ret)
199233294Sstas        errx(1, "krb5_init_context %d", ret);
200233294Sstas
201233294Sstas    ret = krb5_config_parse_file(context, "test_config_strings.out", &c);
202233294Sstas    if (ret)
203233294Sstas        krb5_errx(context, 1, "krb5_config_parse_file()");
204233294Sstas
205233294Sstas    for (i=0; i < sizeof(config_strings_tests)/sizeof(config_strings_tests[0]); i++) {
206233294Sstas        char **ps;
207233294Sstas        const char **s;
208233294Sstas        const char **e;
209233294Sstas
210233294Sstas        ps = krb5_config_get_strings(context, c, "escapes", config_strings_tests[i].name,
211233294Sstas                                     NULL);
212233294Sstas        if (ps == NULL)
213233294Sstas            errx(1, "Failed to read string value %s", config_strings_tests[i].name);
214233294Sstas
215233294Sstas        e = config_strings_tests[i].expected;
216233294Sstas
217233294Sstas        for (s = (const char **)ps; *s && *e; s++, e++) {
218233294Sstas            if (strcmp(*s, *e))
219233294Sstas                errx(1,
220233294Sstas                     "Unexpected configuration string at value [%s].\n"
221233294Sstas                     "Actual=[%s]\n"
222233294Sstas                     "Expected=[%s]\n",
223233294Sstas                     config_strings_tests[i].name, *s, *e);
224233294Sstas        }
225233294Sstas
226233294Sstas        if (*s || *e)
227233294Sstas            errx(1, "Configuation string list for value [%s] has incorrect length.",
228233294Sstas		 config_strings_tests[i].name);
229233294Sstas
230233294Sstas        krb5_config_free_strings(ps);
231233294Sstas    }
232233294Sstas
233233294Sstas    ret = krb5_config_file_free(context, c);
234233294Sstas    if (ret)
235233294Sstas        krb5_errx(context, 1, "krb5_config_file_free()");
236233294Sstas
237233294Sstas    krb5_free_context(context);
238233294Sstas}
239233294Sstas
240233294Sstasint
241233294Sstasmain(int argc, char **argv)
242233294Sstas{
243233294Sstas    check_config_files();
244233294Sstas    check_escaped_strings();
245178825Sdfr    return 0;
246178825Sdfr}
247