initial revision
[ucgine.git] / tools / cfzy / cfzy-test / test_dotconfig.c
1 /*
2  * Copyright (c) 2013, Olivier MATZ <zer0@droids-corp.org>
3  * All rights reserved.
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are met:
6  *
7  *     * Redistributions of source code must retain the above copyright
8  *       notice, this list of conditions and the following disclaimer.
9  *     * Redistributions in binary form must reproduce the above copyright
10  *       notice, this list of conditions and the following disclaimer in the
11  *       documentation and/or other materials provided with the distribution.
12  *     * Neither the name of the University of California, Berkeley nor the
13  *       names of its contributors may be used to endorse or promote products
14  *       derived from this software without specific prior written permission.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND ANY
17  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19  * DISCLAIMED. IN NO EVENT SHALL THE REGENTS AND CONTRIBUTORS BE LIABLE FOR ANY
20  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  */
27
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <string.h>
31 #include <sys/param.h>
32 #include <libgen.h>
33
34 #include <cfzy_log.h>
35 #include <cfzy_list.h>
36 #include <cfzy_htable.h>
37 #include <cfzy_confnode.h>
38 #include <cfzy_conftree.h>
39 #include <cfzy_dotconfig.h>
40 #include <cfzy_c_hdr.h>
41
42 #include "test_dotconfig.h"
43
44 #define LOG(level, fmt, args...)                                \
45         CFZY_LOG("test_dotconfig", level, fmt, ##args)
46
47 #define SUCCESS                      0
48 #define CANNOT_PARSE_CONFTREE       -1
49 #define CANNOT_EVAL_CONFTREE        -2
50 #define CANNOT_GENERATE_DOTCONFIG   -3
51 #define CANNOT_GENERATE_C_HDR       -4
52 #define CANNOT_READ_DOTCONFIG       -5
53
54 static void print_error(const char *in, int err)
55 {
56         printf("Unexpected return value when parsing: <%s>\n", in);
57         switch (err) {
58                 case SUCCESS:
59                         printf("Test returned success, but should not\n");
60                         break;
61                 case CANNOT_PARSE_CONFTREE:
62                         printf("Cannot parse configuration tree\n");
63                         break;
64                 case CANNOT_EVAL_CONFTREE:
65                         printf("Cannot evalulate configuration tree\n");
66                         break;
67                 case CANNOT_GENERATE_DOTCONFIG:
68                         printf("Cannot generate dotconfig file\n");
69                         break;
70                 case CANNOT_GENERATE_C_HDR:
71                         printf("Cannot generate C header file\n");
72                         break;
73                 case CANNOT_READ_DOTCONFIG:
74                         printf("Cannot read dotconfig file\n");
75                         break;
76                 default:
77                         printf("Invalid error %d\n", err);
78                         break;
79         }
80 }
81
82 static int test_one_dotconfig(struct cfzy_conftree *conftree,
83                               const char *filename, int expected)
84 {
85         int ret, err;
86
87         printf("-- Test dotconfig <%s>\n", filename);
88
89         /* XXX we should reset config */
90
91         ret = cfzy_dotconfig_read(conftree, filename);
92         if (ret < 0)
93                 err = CANNOT_READ_DOTCONFIG;
94         else
95                 err = SUCCESS;
96
97         /* this is the expected result, return success */
98         if (err == expected)
99                 return 0;
100
101         print_error(filename, err);
102         return -1;
103 }
104
105 int test_dotconfig(const char *progname)
106 {
107         struct cfzy_conftree *conftree = NULL;
108         int ret;
109         char filename[PATH_MAX];
110         char *progdir;
111
112         cfzy_log_set_default_level(CFZY_LOG_DEBUG);
113
114         /* progdir is the directory where confizery-test is located */
115         progdir = strdup(progname);
116         dirname(progdir);
117
118         /* an example config */
119         snprintf(filename, sizeof(filename),
120                  "%s/../../src/confizery-test/test-configs/"
121                  "conftree.cfzy", progdir);
122
123         conftree = cfzy_conftree_new(filename);
124         if (conftree == NULL) {
125                 LOG(DEBUG, "cannot parse configuration tree\n");
126                 goto fail;
127         }
128
129         if (cfzy_conftree_evaluate(conftree) < 0) {
130                 LOG(DEBUG, "cannot eval conftree\n");
131                 goto fail;
132         }
133
134         /* write the dotconfig file */
135         ret = cfzy_dotconfig_write(conftree, "/tmp/dotconfig");
136         if (ret < 0) {
137                 LOG(DEBUG, "cannot write dotconfig file\n");
138                 goto fail;
139         }
140
141         /* write the autoconf.h file */
142         ret = cfzy_c_hdr_write(conftree, "/tmp/autoconf.h");
143         if (ret < 0) {
144                 LOG(DEBUG, "cannot write autoconf.h file\n");
145                 goto fail;
146         }
147
148         /* write the config/foo/bar.h files */
149         ret = cfzy_multi_c_hdr_write(conftree, "/tmp/config");
150         if (ret < 0) {
151                 LOG(DEBUG, "cannot write multiple headers file\n");
152                 goto fail;
153         }
154
155         /* test a valid config */
156         if (test_one_dotconfig(conftree, "/tmp/dotconfig", SUCCESS) < 0)
157                 goto fail;
158
159         snprintf(filename, sizeof(filename),
160                  "%s/../../src/confizery-test/test-configs/"
161                  "dotconfig", progdir);
162         if (test_one_dotconfig(conftree, filename, SUCCESS) < 0)
163                 goto fail;
164
165         /* invalid configs */
166
167         snprintf(filename, sizeof(filename),
168                  "%s/../../src/confizery-test/invalid-configs/"
169                  "dotconfig-bad-val", progdir);
170         if (test_one_dotconfig(conftree, filename, CANNOT_READ_DOTCONFIG) < 0)
171                 goto fail;
172
173         snprintf(filename, sizeof(filename),
174                  "%s/../../src/confizery-test/invalid-configs/"
175                  "dotconfig-bad-val2", progdir);
176         if (test_one_dotconfig(conftree, filename, CANNOT_READ_DOTCONFIG) < 0)
177                 goto fail;
178
179         snprintf(filename, sizeof(filename),
180                  "%s/../../src/confizery-test/invalid-configs/"
181                  "dotconfig-bad-val3", progdir);
182         if (test_one_dotconfig(conftree, filename, CANNOT_READ_DOTCONFIG) < 0)
183                 goto fail;
184
185         cfzy_conftree_free(conftree);
186         free(progdir);
187         return 0;
188
189  fail:
190         if (conftree != NULL)
191                 cfzy_conftree_free(conftree);
192         free(progdir);
193         return -1;
194 }