lib: load_case_tables() -> smb_init_locale()
[kai/samba-autobuild/.git] / source3 / 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 #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"
26
27 static void print_strings(const char *prefix,
28                           uint32_t num_strings,
29                           const char * const *strings)
30 {
31         uint32_t count;
32
33         if (prefix == NULL) {
34                 prefix = "";
35         }
36
37         for (count = 0; count < num_strings; count++) {
38                 printf("%s%s\n", prefix, strings[count]);
39         }
40 }
41
42 static bool test_get_includes(struct smbconf_ctx *ctx)
43 {
44         sbcErr err;
45         bool ret = false;
46         uint32_t num_includes = 0;
47         char **includes = NULL;
48         TALLOC_CTX *mem_ctx = talloc_stackframe();
49
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;
56         }
57
58         printf("got %u includes%s\n", num_includes,
59                (num_includes > 0) ? ":" : ".");
60         print_strings("", num_includes, (const char * const *)includes);
61
62         printf("OK: get_includes\n");
63         ret = true;
64
65 done:
66         talloc_free(mem_ctx);
67         return ret;
68 }
69
70 static bool test_set_get_includes(struct smbconf_ctx *ctx)
71 {
72         sbcErr err;
73         uint32_t count;
74         bool ret = false;
75         const char *set_includes[] = {
76                 "/path/to/include1",
77                 "/path/to/include2"
78         };
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();
83
84         printf("TEST: set_get_includes\n");
85
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;
91         }
92
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;
99         }
100
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;
105         }
106
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;
117                 }
118         }
119
120         printf("OK: set_includes\n");
121         ret = true;
122
123 done:
124         talloc_free(mem_ctx);
125         return ret;
126 }
127
128 static bool test_delete_includes(struct smbconf_ctx *ctx)
129 {
130         sbcErr err;
131         bool ret = false;
132         const char *set_includes[] = {
133                 "/path/to/include",
134         };
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();
139
140         printf("TEST: delete_includes\n");
141
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;
147         }
148
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;
154         }
155
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;
162         }
163
164         if (get_num_includes != 0) {
165                 printf("FAIL: delete_includes (not empty after delete)\n");
166                 goto done;
167         }
168
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;
174         }
175
176         printf("OK: delete_includes\n");
177         ret = true;
178
179 done:
180         talloc_free(mem_ctx);
181         return ret;
182 }
183
184 static bool create_conf_file(const char *filename)
185 {
186         FILE *f;
187
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;
194         }
195
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");
200
201         fclose(f);
202
203         printf("OK: create file\n");
204         return true;
205 }
206
207 static bool torture_smbconf_txt(void)
208 {
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();
214
215         printf("test: text backend\n");
216
217         if (!create_conf_file(filename)) {
218                 ret = false;
219                 goto done;
220         }
221
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;
228         }
229         printf("OK: init\n");
230
231         ret &= test_get_includes(conf_ctx);
232
233         smbconf_shutdown(conf_ctx);
234
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;
240         }
241         printf("OK: unlink file\n");
242
243 done:
244         printf("%s: text backend\n", ret ? "success" : "failure");
245         talloc_free(mem_ctx);
246         return ret;
247 }
248
249 static bool torture_smbconf_reg(void)
250 {
251         sbcErr err;
252         bool ret = true;
253         struct smbconf_ctx *conf_ctx = NULL;
254         TALLOC_CTX *mem_ctx = talloc_stackframe();
255
256         printf("test: registry backend\n");
257
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;
264         }
265         printf("OK: init\n");
266
267         ret &= test_get_includes(conf_ctx);
268         ret &= test_set_get_includes(conf_ctx);
269         ret &= test_delete_includes(conf_ctx);
270
271         smbconf_shutdown(conf_ctx);
272
273 done:
274         printf("%s: registry backend\n", ret ? "success" : "failure");
275         talloc_free(mem_ctx);
276         return ret;
277 }
278
279 static bool torture_smbconf(void)
280 {
281         bool ret = true;
282         ret &= torture_smbconf_txt();
283         printf("\n");
284         ret &= torture_smbconf_reg();
285         return ret;
286 }
287
288 int main(int argc, const char **argv)
289 {
290         bool ret;
291         poptContext pc;
292         TALLOC_CTX *mem_ctx = talloc_stackframe();
293
294         struct poptOption long_options[] = {
295                 POPT_COMMON_SAMBA
296                 {0, 0, 0, 0}
297         };
298
299         smb_init_locale();
300         setup_logging(argv[0], DEBUG_STDERR);
301
302         /* parse options */
303         pc = poptGetContext("smbconftort", argc, (const char **)argv,
304                             long_options, 0);
305
306         while(poptGetNextOpt(pc) != -1) { }
307
308         poptFreeContext(pc);
309
310         ret = lp_load_global(get_dyn_CONFIGFILE());
311         if (!ret) {
312                 printf("failure: error loading the configuration\n");
313                 goto done;
314         }
315
316         ret = torture_smbconf();
317
318 done:
319         talloc_free(mem_ctx);
320         return ret ? 0 : -1;
321 }