2 * Copyright (c) 2013, Olivier MATZ <zer0@droids-corp.org>
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are met:
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.
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.
31 #include <sys/param.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>
42 #include "test_dotconfig.h"
44 #define LOG(level, fmt, args...) \
45 CFZY_LOG("test_dotconfig", level, fmt, ##args)
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
54 static void print_error(const char *in, int err)
56 printf("Unexpected return value when parsing: <%s>\n", in);
59 printf("Test returned success, but should not\n");
61 case CANNOT_PARSE_CONFTREE:
62 printf("Cannot parse configuration tree\n");
64 case CANNOT_EVAL_CONFTREE:
65 printf("Cannot evalulate configuration tree\n");
67 case CANNOT_GENERATE_DOTCONFIG:
68 printf("Cannot generate dotconfig file\n");
70 case CANNOT_GENERATE_C_HDR:
71 printf("Cannot generate C header file\n");
73 case CANNOT_READ_DOTCONFIG:
74 printf("Cannot read dotconfig file\n");
77 printf("Invalid error %d\n", err);
82 static int test_one_dotconfig(struct cfzy_conftree *conftree,
83 const char *filename, int expected)
87 printf("-- Test dotconfig <%s>\n", filename);
89 /* XXX we should reset config */
91 ret = cfzy_dotconfig_read(conftree, filename);
93 err = CANNOT_READ_DOTCONFIG;
97 /* this is the expected result, return success */
101 print_error(filename, err);
105 int test_dotconfig(const char *progname)
107 struct cfzy_conftree *conftree = NULL;
109 char filename[PATH_MAX];
112 cfzy_log_set_default_level(CFZY_LOG_DEBUG);
114 /* progdir is the directory where confizery-test is located */
115 progdir = strdup(progname);
118 /* an example config */
119 snprintf(filename, sizeof(filename),
120 "%s/../../src/confizery-test/test-configs/"
121 "conftree.cfzy", progdir);
123 conftree = cfzy_conftree_new(filename);
124 if (conftree == NULL) {
125 LOG(DEBUG, "cannot parse configuration tree\n");
129 if (cfzy_conftree_evaluate(conftree) < 0) {
130 LOG(DEBUG, "cannot eval conftree\n");
134 /* write the dotconfig file */
135 ret = cfzy_dotconfig_write(conftree, "/tmp/dotconfig");
137 LOG(DEBUG, "cannot write dotconfig file\n");
141 /* write the autoconf.h file */
142 ret = cfzy_c_hdr_write(conftree, "/tmp/autoconf.h");
144 LOG(DEBUG, "cannot write autoconf.h file\n");
148 /* write the config/foo/bar.h files */
149 ret = cfzy_multi_c_hdr_write(conftree, "/tmp/config");
151 LOG(DEBUG, "cannot write multiple headers file\n");
155 /* test a valid config */
156 if (test_one_dotconfig(conftree, "/tmp/dotconfig", SUCCESS) < 0)
159 snprintf(filename, sizeof(filename),
160 "%s/../../src/confizery-test/test-configs/"
161 "dotconfig", progdir);
162 if (test_one_dotconfig(conftree, filename, SUCCESS) < 0)
165 /* invalid configs */
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)
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)
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)
185 cfzy_conftree_free(conftree);
190 if (conftree != NULL)
191 cfzy_conftree_free(conftree);