libsmbconf: testsuite: refactor printing of string lists out.
[Samba.git] / source / lib / smbconf / testsuite.c
blobe718e734e39e6baaa323019cc18af98a61980b61
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 torture_smbconf_txt(void)
66 WERROR werr;
67 bool ret = true;
68 struct smbconf_ctx *conf_ctx = NULL;
69 TALLOC_CTX *mem_ctx = talloc_stackframe();
71 printf("test: text backend\n");
73 printf("test: init\n");
74 werr = smbconf_init_txt_simple(mem_ctx, &conf_ctx, NULL, true);
75 if (!W_ERROR_IS_OK(werr)) {
76 printf("failure: init failed: %s\n", dos_errstr(werr));
77 ret = false;
78 goto done;
80 printf("success: init\n");
82 ret &= test_get_includes(conf_ctx);
84 smbconf_shutdown(conf_ctx);
86 ret = true;
88 printf("success: text backend\n");
90 done:
91 TALLOC_FREE(mem_ctx);
92 return ret;
95 static bool torture_smbconf_reg(void)
97 WERROR werr;
98 bool ret = true;
99 struct smbconf_ctx *conf_ctx = NULL;
100 TALLOC_CTX *mem_ctx = talloc_stackframe();
102 printf("test: registry backend\n");
104 printf("test: init\n");
105 werr = smbconf_init_reg(mem_ctx, &conf_ctx, NULL);
106 if (!W_ERROR_IS_OK(werr)) {
107 printf("failure: init failed: %s\n", dos_errstr(werr));
108 ret = false;
109 goto done;
111 printf("success: init\n");
113 ret &= test_get_includes(conf_ctx);
115 smbconf_shutdown(conf_ctx);
117 ret = true;
119 printf("success: registry backend\n");
121 done:
122 TALLOC_FREE(mem_ctx);
123 return ret;
126 static bool torture_smbconf(void)
128 bool ret = true;
129 ret &= torture_smbconf_txt();
130 printf("\n");
131 ret &= torture_smbconf_reg();
132 return ret;
135 int main(int argc, const char **argv)
137 bool ret;
138 poptContext pc;
139 TALLOC_CTX *mem_ctx = talloc_stackframe();
141 struct poptOption long_options[] = {
142 POPT_COMMON_SAMBA
143 {0, 0, 0, 0}
146 load_case_tables();
147 dbf = x_stderr;
149 /* parse options */
150 pc = poptGetContext("smbconftort", argc, (const char **)argv,
151 long_options, 0);
153 while(poptGetNextOpt(pc) != -1) { }
155 poptFreeContext(pc);
157 ret = lp_load(get_dyn_CONFIGFILE(),
158 true, /* globals_only */
159 false, /* save_defaults */
160 false, /* add_ipc */
161 true /* initialize globals */);
163 if (!ret) {
164 printf("failure: error loading the configuration\n");
165 goto done;
168 ret = torture_smbconf();
170 done:
171 TALLOC_FREE(mem_ctx);
172 return ret ? 0 : -1;