libsmbconf: testsuite: refactor printing of string lists out.
[kai/samba.git] / source / lib / smbconf / testsuite.c
1 /*
2  *  Unix SMB/CIFS implementation.
3  *  libsmbconf - Samba configuration library: testsuite
4  *  Copyright (C) Michael Adam 2008
5  *
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.
10  *
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.
15  *
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/>.
18  */
19
20 #include "includes.h"
21
22 static void print_strings(const char *prefix,
23                           uint32_t num_strings, const char **strings)
24 {
25         uint32_t count;
26
27         if (prefix == NULL) {
28                 prefix = "";
29         }
30
31         for (count = 0; count < num_strings; count++) {
32                 printf("%s%s\n", prefix, strings[count]);
33         }
34 }
35
36 static bool test_get_includes(struct smbconf_ctx *ctx)
37 {
38         WERROR werr;
39         bool ret = false;
40         uint32_t num_includes = 0;
41         char **includes = NULL;
42         TALLOC_CTX *mem_ctx = talloc_stackframe();
43
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;
50         }
51
52         printf("got %u includes%s\n", num_includes,
53                (num_includes > 0) ? ":" : ".");
54         print_strings("", num_includes, (const char **)includes);
55
56         printf("success: get_includes\n");
57         ret = true;
58
59 done:
60         TALLOC_FREE(mem_ctx);
61         return ret;
62 }
63
64 static bool torture_smbconf_txt(void)
65 {
66         WERROR werr;
67         bool ret = true;
68         struct smbconf_ctx *conf_ctx = NULL;
69         TALLOC_CTX *mem_ctx = talloc_stackframe();
70
71         printf("test: text backend\n");
72
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;
79         }
80         printf("success: init\n");
81
82         ret &= test_get_includes(conf_ctx);
83
84         smbconf_shutdown(conf_ctx);
85
86         ret = true;
87
88         printf("success: text backend\n");
89
90 done:
91         TALLOC_FREE(mem_ctx);
92         return ret;
93 }
94
95 static bool torture_smbconf_reg(void)
96 {
97         WERROR werr;
98         bool ret = true;
99         struct smbconf_ctx *conf_ctx = NULL;
100         TALLOC_CTX *mem_ctx = talloc_stackframe();
101
102         printf("test: registry backend\n");
103
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;
110         }
111         printf("success: init\n");
112
113         ret &= test_get_includes(conf_ctx);
114
115         smbconf_shutdown(conf_ctx);
116
117         ret = true;
118
119         printf("success: registry backend\n");
120
121 done:
122         TALLOC_FREE(mem_ctx);
123         return ret;
124 }
125
126 static bool torture_smbconf(void)
127 {
128         bool ret = true;
129         ret &= torture_smbconf_txt();
130         printf("\n");
131         ret &= torture_smbconf_reg();
132         return ret;
133 }
134
135 int main(int argc, const char **argv)
136 {
137         bool ret;
138         poptContext pc;
139         TALLOC_CTX *mem_ctx = talloc_stackframe();
140
141         struct poptOption long_options[] = {
142                 POPT_COMMON_SAMBA
143                 {0, 0, 0, 0}
144         };
145
146         load_case_tables();
147         dbf = x_stderr;
148
149         /* parse options */
150         pc = poptGetContext("smbconftort", argc, (const char **)argv,
151                             long_options, 0);
152
153         while(poptGetNextOpt(pc) != -1) { }
154
155         poptFreeContext(pc);
156
157         ret = lp_load(get_dyn_CONFIGFILE(),
158                       true,  /* globals_only */
159                       false, /* save_defaults */
160                       false, /* add_ipc */
161                       true   /* initialize globals */);
162
163         if (!ret) {
164                 printf("failure: error loading the configuration\n");
165                 goto done;
166         }
167
168         ret = torture_smbconf();
169
170 done:
171         TALLOC_FREE(mem_ctx);
172         return ret ? 0 : -1;
173 }