libsmbconf: testsuite: fix handling of error message/overall status
[Samba.git] / source / lib / smbconf / testsuite.c
blob2e833d9e2632dad40f1bfe9d8859056eaf9ba5de
1 /*
2 * Unix SMB/CIFS implementation.
3 * libsmbconf - Samba configuration library: testsuite
4 * Copyright (C) Michael Adam 2008
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 3 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, see <http://www.gnu.org/licenses/>.
20 #include "includes.h"
22 static void print_strings(const char *prefix,
23 uint32_t num_strings, const char **strings)
25 uint32_t count;
27 if (prefix == NULL) {
28 prefix = "";
31 for (count = 0; count < num_strings; count++) {
32 printf("%s%s\n", prefix, strings[count]);
36 static bool test_get_includes(struct smbconf_ctx *ctx)
38 WERROR werr;
39 bool ret = false;
40 uint32_t num_includes = 0;
41 char **includes = NULL;
42 TALLOC_CTX *mem_ctx = talloc_stackframe();
44 printf("test: get_includes\n");
45 werr = smbconf_get_global_includes(ctx, mem_ctx,
46 &num_includes, &includes);
47 if (!W_ERROR_IS_OK(werr)) {
48 printf("failure: get_includes - %s\n", dos_errstr(werr));
49 goto done;
52 printf("got %u includes%s\n", num_includes,
53 (num_includes > 0) ? ":" : ".");
54 print_strings("", num_includes, (const char **)includes);
56 printf("success: get_includes\n");
57 ret = true;
59 done:
60 TALLOC_FREE(mem_ctx);
61 return ret;
64 static bool test_set_get_includes(struct smbconf_ctx *ctx)
66 WERROR werr;
67 uint32_t count;
68 bool ret = false;
69 const char *set_includes[] = {
70 "/path/to/include1",
71 "/path/to/include2"
73 uint32_t set_num_includes = 2;
74 char **get_includes = NULL;
75 uint32_t get_num_includes = 0;
76 TALLOC_CTX *mem_ctx = talloc_stackframe();
78 printf("test: set_get_includes\n");
80 werr = smbconf_set_global_includes(ctx, set_num_includes, set_includes);
81 if (!W_ERROR_IS_OK(werr)) {
82 printf("failure: get_set_includes (setting includes) - %s\n",
83 dos_errstr(werr));
84 goto done;
87 werr = smbconf_get_global_includes(ctx, mem_ctx, &get_num_includes,
88 &get_includes);
89 if (!W_ERROR_IS_OK(werr)) {
90 printf("failure: get_set_includes (getting includes) - %s\n",
91 dos_errstr(werr));
92 goto done;
95 if (get_num_includes != set_num_includes) {
96 printf("failure: get_set_includes - set %d includes, got %d\n",
97 set_num_includes, get_num_includes);
98 goto done;
101 for (count = 0; count < get_num_includes; count++) {
102 if (!strequal(set_includes[count], get_includes[count])) {
103 printf("expected: \n");
104 print_strings("* ", set_num_includes, set_includes);
105 printf("got: \n");
106 print_strings("* ", get_num_includes,
107 (const char **)get_includes);
108 printf("failure: get_set_includes - data mismatch:\n");
109 goto done;
113 printf("success: set_includes\n");
114 ret = true;
116 done:
117 TALLOC_FREE(mem_ctx);
118 return ret;
121 static bool torture_smbconf_txt(void)
123 WERROR werr;
124 bool ret = true;
125 struct smbconf_ctx *conf_ctx = NULL;
126 TALLOC_CTX *mem_ctx = talloc_stackframe();
128 printf("test: text backend\n");
130 printf("test: init\n");
131 werr = smbconf_init_txt_simple(mem_ctx, &conf_ctx, NULL, true);
132 if (!W_ERROR_IS_OK(werr)) {
133 printf("failure: init failed: %s\n", dos_errstr(werr));
134 ret = false;
135 goto done;
137 printf("success: init\n");
139 ret &= test_get_includes(conf_ctx);
141 smbconf_shutdown(conf_ctx);
143 printf("%s: text backend\n", ret ? "success" : "failure");
145 done:
146 TALLOC_FREE(mem_ctx);
147 return ret;
150 static bool torture_smbconf_reg(void)
152 WERROR werr;
153 bool ret = true;
154 struct smbconf_ctx *conf_ctx = NULL;
155 TALLOC_CTX *mem_ctx = talloc_stackframe();
157 printf("test: registry backend\n");
159 printf("test: init\n");
160 werr = smbconf_init_reg(mem_ctx, &conf_ctx, NULL);
161 if (!W_ERROR_IS_OK(werr)) {
162 printf("failure: init failed: %s\n", dos_errstr(werr));
163 ret = false;
164 goto done;
166 printf("success: init\n");
168 ret &= test_get_includes(conf_ctx);
169 ret &= test_set_get_includes(conf_ctx);
171 smbconf_shutdown(conf_ctx);
173 printf("%s: registry backend\n", ret ? "success" : "failure");
175 done:
176 TALLOC_FREE(mem_ctx);
177 return ret;
180 static bool torture_smbconf(void)
182 bool ret = true;
183 ret &= torture_smbconf_txt();
184 printf("\n");
185 ret &= torture_smbconf_reg();
186 return ret;
189 int main(int argc, const char **argv)
191 bool ret;
192 poptContext pc;
193 TALLOC_CTX *mem_ctx = talloc_stackframe();
195 struct poptOption long_options[] = {
196 POPT_COMMON_SAMBA
197 {0, 0, 0, 0}
200 load_case_tables();
201 dbf = x_stderr;
203 /* parse options */
204 pc = poptGetContext("smbconftort", argc, (const char **)argv,
205 long_options, 0);
207 while(poptGetNextOpt(pc) != -1) { }
209 poptFreeContext(pc);
211 ret = lp_load(get_dyn_CONFIGFILE(),
212 true, /* globals_only */
213 false, /* save_defaults */
214 false, /* add_ipc */
215 true /* initialize globals */);
217 if (!ret) {
218 printf("failure: error loading the configuration\n");
219 goto done;
222 ret = torture_smbconf();
224 done:
225 TALLOC_FREE(mem_ctx);
226 return ret ? 0 : -1;