ctdb-scripts: Extend NFS .check files with service_check_cmd variable
[Samba.git] / source3 / lib / smbconf / testsuite.c
blob1e8d0fe27eea5c6be24fa2854656c6d906d23adc
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"
21 #include "popt_common.h"
22 #include "lib/smbconf/smbconf.h"
23 #include "lib/smbconf/smbconf_init.h"
24 #include "lib/smbconf/smbconf_reg.h"
25 #include "lib/smbconf/smbconf_txt.h"
27 static void print_strings(const char *prefix,
28 uint32_t num_strings,
29 const char * const *strings)
31 uint32_t count;
33 if (prefix == NULL) {
34 prefix = "";
37 for (count = 0; count < num_strings; count++) {
38 printf("%s%s\n", prefix, strings[count]);
42 static bool test_get_includes(struct smbconf_ctx *ctx)
44 sbcErr err;
45 bool ret = false;
46 uint32_t num_includes = 0;
47 char **includes = NULL;
48 TALLOC_CTX *mem_ctx = talloc_stackframe();
50 printf("TEST: get_includes\n");
51 err = smbconf_get_global_includes(ctx, mem_ctx,
52 &num_includes, &includes);
53 if (!SBC_ERROR_IS_OK(err)) {
54 printf("FAIL: get_includes - %s\n", sbcErrorString(err));
55 goto done;
58 printf("got %u includes%s\n", num_includes,
59 (num_includes > 0) ? ":" : ".");
60 print_strings("", num_includes, (const char * const *)includes);
62 printf("OK: get_includes\n");
63 ret = true;
65 done:
66 talloc_free(mem_ctx);
67 return ret;
70 static bool test_set_get_includes(struct smbconf_ctx *ctx)
72 sbcErr err;
73 uint32_t count;
74 bool ret = false;
75 const char *set_includes[] = {
76 "/path/to/include1",
77 "/path/to/include2"
79 uint32_t set_num_includes = 2;
80 char **get_includes = NULL;
81 uint32_t get_num_includes = 0;
82 TALLOC_CTX *mem_ctx = talloc_stackframe();
84 printf("TEST: set_get_includes\n");
86 err = smbconf_set_global_includes(ctx, set_num_includes, set_includes);
87 if (!SBC_ERROR_IS_OK(err)) {
88 printf("FAIL: get_set_includes (setting includes) - %s\n",
89 sbcErrorString(err));
90 goto done;
93 err = smbconf_get_global_includes(ctx, mem_ctx, &get_num_includes,
94 &get_includes);
95 if (!SBC_ERROR_IS_OK(err)) {
96 printf("FAIL: get_set_includes (getting includes) - %s\n",
97 sbcErrorString(err));
98 goto done;
101 if (get_num_includes != set_num_includes) {
102 printf("FAIL: get_set_includes - set %d includes, got %d\n",
103 set_num_includes, get_num_includes);
104 goto done;
107 for (count = 0; count < get_num_includes; count++) {
108 if (!strequal(set_includes[count], get_includes[count])) {
109 printf("expected: \n");
110 print_strings("* ", set_num_includes,
111 (const char * const *)set_includes);
112 printf("got: \n");
113 print_strings("* ", get_num_includes,
114 (const char * const *)get_includes);
115 printf("FAIL: get_set_includes - data mismatch:\n");
116 goto done;
120 printf("OK: set_includes\n");
121 ret = true;
123 done:
124 talloc_free(mem_ctx);
125 return ret;
128 static bool test_delete_includes(struct smbconf_ctx *ctx)
130 sbcErr err;
131 bool ret = false;
132 const char *set_includes[] = {
133 "/path/to/include",
135 uint32_t set_num_includes = 1;
136 char **get_includes = NULL;
137 uint32_t get_num_includes = 0;
138 TALLOC_CTX *mem_ctx = talloc_stackframe();
140 printf("TEST: delete_includes\n");
142 err = smbconf_set_global_includes(ctx, set_num_includes, set_includes);
143 if (!SBC_ERROR_IS_OK(err)) {
144 printf("FAIL: delete_includes (setting includes) - %s\n",
145 sbcErrorString(err));
146 goto done;
149 err = smbconf_delete_global_includes(ctx);
150 if (!SBC_ERROR_IS_OK(err)) {
151 printf("FAIL: delete_includes (deleting includes) - %s\n",
152 sbcErrorString(err));
153 goto done;
156 err = smbconf_get_global_includes(ctx, mem_ctx, &get_num_includes,
157 &get_includes);
158 if (!SBC_ERROR_IS_OK(err)) {
159 printf("FAIL: delete_includes (getting includes) - %s\n",
160 sbcErrorString(err));
161 goto done;
164 if (get_num_includes != 0) {
165 printf("FAIL: delete_includes (not empty after delete)\n");
166 goto done;
169 err = smbconf_delete_global_includes(ctx);
170 if (!SBC_ERROR_IS_OK(err)) {
171 printf("FAIL: delete_includes (delete empty includes) - "
172 "%s\n", sbcErrorString(err));
173 goto done;
176 printf("OK: delete_includes\n");
177 ret = true;
179 done:
180 talloc_free(mem_ctx);
181 return ret;
184 static bool create_conf_file(const char *filename)
186 FILE *f;
188 printf("TEST: creating file\n");
189 f = fopen(filename, "w");
190 if (!f) {
191 printf("failure: failed to open %s for writing: %s\n",
192 filename, strerror(errno));
193 return false;
196 fprintf(f, "[global]\n");
197 fprintf(f, "\tserver string = smbconf testsuite\n");
198 fprintf(f, "\tworkgroup = SAMBA\n");
199 fprintf(f, "\tsecurity = user\n");
201 fclose(f);
203 printf("OK: create file\n");
204 return true;
207 static bool torture_smbconf_txt(void)
209 sbcErr err;
210 bool ret = true;
211 const char *filename = "/tmp/smb.conf.smbconf_testsuite";
212 struct smbconf_ctx *conf_ctx = NULL;
213 TALLOC_CTX *mem_ctx = talloc_stackframe();
215 printf("test: text backend\n");
217 if (!create_conf_file(filename)) {
218 ret = false;
219 goto done;
222 printf("TEST: init\n");
223 err = smbconf_init_txt(mem_ctx, &conf_ctx, filename);
224 if (!SBC_ERROR_IS_OK(err)) {
225 printf("FAIL: text backend failed: %s\n", sbcErrorString(err));
226 ret = false;
227 goto done;
229 printf("OK: init\n");
231 ret &= test_get_includes(conf_ctx);
233 smbconf_shutdown(conf_ctx);
235 printf("TEST: unlink file\n");
236 if (unlink(filename) != 0) {
237 printf("OK: unlink failed: %s\n", strerror(errno));
238 ret = false;
239 goto done;
241 printf("OK: unlink file\n");
243 done:
244 printf("%s: text backend\n", ret ? "success" : "failure");
245 talloc_free(mem_ctx);
246 return ret;
249 static bool torture_smbconf_reg(void)
251 sbcErr err;
252 bool ret = true;
253 struct smbconf_ctx *conf_ctx = NULL;
254 TALLOC_CTX *mem_ctx = talloc_stackframe();
256 printf("test: registry backend\n");
258 printf("TEST: init\n");
259 err = smbconf_init_reg(mem_ctx, &conf_ctx, NULL);
260 if (!SBC_ERROR_IS_OK(err)) {
261 printf("FAIL: init failed: %s\n", sbcErrorString(err));
262 ret = false;
263 goto done;
265 printf("OK: init\n");
267 ret &= test_get_includes(conf_ctx);
268 ret &= test_set_get_includes(conf_ctx);
269 ret &= test_delete_includes(conf_ctx);
271 smbconf_shutdown(conf_ctx);
273 done:
274 printf("%s: registry backend\n", ret ? "success" : "failure");
275 talloc_free(mem_ctx);
276 return ret;
279 static bool torture_smbconf(void)
281 bool ret = true;
282 ret &= torture_smbconf_txt();
283 printf("\n");
284 ret &= torture_smbconf_reg();
285 return ret;
288 int main(int argc, const char **argv)
290 bool ret;
291 poptContext pc;
292 TALLOC_CTX *mem_ctx = talloc_stackframe();
294 struct poptOption long_options[] = {
295 POPT_COMMON_SAMBA
296 {0, 0, 0, 0}
299 smb_init_locale();
300 setup_logging(argv[0], DEBUG_STDERR);
302 /* parse options */
303 pc = poptGetContext("smbconftort", argc, (const char **)argv,
304 long_options, 0);
306 while(poptGetNextOpt(pc) != -1) { }
308 poptFreeContext(pc);
310 ret = lp_load_global(get_dyn_CONFIGFILE());
311 if (!ret) {
312 printf("failure: error loading the configuration\n");
313 goto done;
316 ret = torture_smbconf();
318 done:
319 talloc_free(mem_ctx);
320 return ret ? 0 : -1;