80754dd20c3cc564fe55ecade2d98d491cf88321
[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, const char **strings)
29 {
30         uint32_t count;
31
32         if (prefix == NULL) {
33                 prefix = "";
34         }
35
36         for (count = 0; count < num_strings; count++) {
37                 printf("%s%s\n", prefix, strings[count]);
38         }
39 }
40
41 static bool test_get_includes(struct smbconf_ctx *ctx)
42 {
43         WERROR werr;
44         bool ret = false;
45         uint32_t num_includes = 0;
46         char **includes = NULL;
47         TALLOC_CTX *mem_ctx = talloc_stackframe();
48
49         printf("TEST: get_includes\n");
50         werr = smbconf_get_global_includes(ctx, mem_ctx,
51                                            &num_includes, &includes);
52         if (!W_ERROR_IS_OK(werr)) {
53                 printf("FAIL: get_includes - %s\n", win_errstr(werr));
54                 goto done;
55         }
56
57         printf("got %u includes%s\n", num_includes,
58                (num_includes > 0) ? ":" : ".");
59         print_strings("", num_includes, (const char **)includes);
60
61         printf("OK: get_includes\n");
62         ret = true;
63
64 done:
65         talloc_free(mem_ctx);
66         return ret;
67 }
68
69 static bool test_set_get_includes(struct smbconf_ctx *ctx)
70 {
71         WERROR werr;
72         uint32_t count;
73         bool ret = false;
74         const char *set_includes[] = {
75                 "/path/to/include1",
76                 "/path/to/include2"
77         };
78         uint32_t set_num_includes = 2;
79         char **get_includes = NULL;
80         uint32_t get_num_includes = 0;
81         TALLOC_CTX *mem_ctx = talloc_stackframe();
82
83         printf("TEST: set_get_includes\n");
84
85         werr = smbconf_set_global_includes(ctx, set_num_includes, set_includes);
86         if (!W_ERROR_IS_OK(werr)) {
87                 printf("FAIL: get_set_includes (setting includes) - %s\n",
88                        win_errstr(werr));
89                 goto done;
90         }
91
92         werr = smbconf_get_global_includes(ctx, mem_ctx, &get_num_includes,
93                                            &get_includes);
94         if (!W_ERROR_IS_OK(werr)) {
95                 printf("FAIL: get_set_includes (getting includes) - %s\n",
96                        win_errstr(werr));
97                 goto done;
98         }
99
100         if (get_num_includes != set_num_includes) {
101                 printf("FAIL: get_set_includes - set %d includes, got %d\n",
102                        set_num_includes, get_num_includes);
103                 goto done;
104         }
105
106         for (count = 0; count < get_num_includes; count++) {
107                 if (!strequal(set_includes[count], get_includes[count])) {
108                         printf("expected: \n");
109                         print_strings("* ", set_num_includes, set_includes);
110                         printf("got: \n");
111                         print_strings("* ", get_num_includes,
112                                       (const char **)get_includes);
113                         printf("FAIL: get_set_includes - data mismatch:\n");
114                         goto done;
115                 }
116         }
117
118         printf("OK: set_includes\n");
119         ret = true;
120
121 done:
122         talloc_free(mem_ctx);
123         return ret;
124 }
125
126 static bool test_delete_includes(struct smbconf_ctx *ctx)
127 {
128         WERROR werr;
129         bool ret = false;
130         const char *set_includes[] = {
131                 "/path/to/include",
132         };
133         uint32_t set_num_includes = 1;
134         char **get_includes = NULL;
135         uint32_t get_num_includes = 0;
136         TALLOC_CTX *mem_ctx = talloc_stackframe();
137
138         printf("TEST: delete_includes\n");
139
140         werr = smbconf_set_global_includes(ctx, set_num_includes, set_includes);
141         if (!W_ERROR_IS_OK(werr)) {
142                 printf("FAIL: delete_includes (setting includes) - %s\n",
143                        win_errstr(werr));
144                 goto done;
145         }
146
147         werr = smbconf_delete_global_includes(ctx);
148         if (!W_ERROR_IS_OK(werr)) {
149                 printf("FAIL: delete_includes (deleting includes) - %s\n",
150                        win_errstr(werr));
151                 goto done;
152         }
153
154         werr = smbconf_get_global_includes(ctx, mem_ctx, &get_num_includes,
155                                            &get_includes);
156         if (!W_ERROR_IS_OK(werr)) {
157                 printf("FAIL: delete_includes (getting includes) - %s\n",
158                        win_errstr(werr));
159                 goto done;
160         }
161
162         if (get_num_includes != 0) {
163                 printf("FAIL: delete_includes (not empty after delete)\n");
164                 goto done;
165         }
166
167         werr = smbconf_delete_global_includes(ctx);
168         if (!W_ERROR_IS_OK(werr)) {
169                 printf("FAIL: delete_includes (delete empty includes) - "
170                        "%s\n", win_errstr(werr));
171                 goto done;
172         }
173
174         printf("OK: delete_includes\n");
175         ret = true;
176
177 done:
178         return ret;
179 }
180
181 static bool create_conf_file(const char *filename)
182 {
183         FILE *f;
184
185         printf("TEST: creating file\n");
186         f = sys_fopen(filename, "w");
187         if (!f) {
188                 printf("failure: failed to open %s for writing: %s\n",
189                        filename, strerror(errno));
190                 return false;
191         }
192
193         fprintf(f, "[global]\n");
194         fprintf(f, "\tserver string = smbconf testsuite\n");
195         fprintf(f, "\tworkgroup = SAMBA\n");
196         fprintf(f, "\tsecurity = user\n");
197
198         fclose(f);
199
200         printf("OK: create file\n");
201         return true;
202 }
203
204 static bool torture_smbconf_txt(void)
205 {
206         WERROR werr;
207         bool ret = true;
208         const char *filename = "/tmp/smb.conf.smbconf_testsuite";
209         struct smbconf_ctx *conf_ctx = NULL;
210         TALLOC_CTX *mem_ctx = talloc_stackframe();
211
212         printf("test: text backend\n");
213
214         if (!create_conf_file(filename)) {
215                 ret = false;
216                 goto done;
217         }
218
219         printf("TEST: init\n");
220         werr = smbconf_init_txt(mem_ctx, &conf_ctx, filename);
221         if (!W_ERROR_IS_OK(werr)) {
222                 printf("FAIL: text backend failed: %s\n", win_errstr(werr));
223                 ret = false;
224                 goto done;
225         }
226         printf("OK: init\n");
227
228         ret &= test_get_includes(conf_ctx);
229
230         smbconf_shutdown(conf_ctx);
231
232         printf("TEST: unlink file\n");
233         if (unlink(filename) != 0) {
234                 printf("OK: unlink failed: %s\n", strerror(errno));
235                 ret = false;
236                 goto done;
237         }
238         printf("OK: unlink file\n");
239
240 done:
241         printf("%s: text backend\n", ret ? "success" : "failure");
242         talloc_free(mem_ctx);
243         return ret;
244 }
245
246 static bool torture_smbconf_reg(void)
247 {
248         WERROR werr;
249         bool ret = true;
250         struct smbconf_ctx *conf_ctx = NULL;
251         TALLOC_CTX *mem_ctx = talloc_stackframe();
252
253         printf("test: registry backend\n");
254
255         printf("TEST: init\n");
256         werr = smbconf_init_reg(mem_ctx, &conf_ctx, NULL);
257         if (!W_ERROR_IS_OK(werr)) {
258                 printf("FAIL: init failed: %s\n", win_errstr(werr));
259                 ret = false;
260                 goto done;
261         }
262         printf("OK: init\n");
263
264         ret &= test_get_includes(conf_ctx);
265         ret &= test_set_get_includes(conf_ctx);
266         ret &= test_delete_includes(conf_ctx);
267
268         smbconf_shutdown(conf_ctx);
269
270 done:
271         printf("%s: registry backend\n", ret ? "success" : "failure");
272         talloc_free(mem_ctx);
273         return ret;
274 }
275
276 static bool torture_smbconf(void)
277 {
278         bool ret = true;
279         ret &= torture_smbconf_txt();
280         printf("\n");
281         ret &= torture_smbconf_reg();
282         return ret;
283 }
284
285 int main(int argc, const char **argv)
286 {
287         bool ret;
288         poptContext pc;
289         TALLOC_CTX *mem_ctx = talloc_stackframe();
290
291         struct poptOption long_options[] = {
292                 POPT_COMMON_SAMBA
293                 {0, 0, 0, 0}
294         };
295
296         load_case_tables();
297         setup_logging(argv[0], DEBUG_STDERR);
298
299         /* parse options */
300         pc = poptGetContext("smbconftort", argc, (const char **)argv,
301                             long_options, 0);
302
303         while(poptGetNextOpt(pc) != -1) { }
304
305         poptFreeContext(pc);
306
307         ret = lp_load(get_dyn_CONFIGFILE(),
308                       true,  /* globals_only */
309                       false, /* save_defaults */
310                       false, /* add_ipc */
311                       true   /* initialize globals */);
312
313         if (!ret) {
314                 printf("failure: error loading the configuration\n");
315                 goto done;
316         }
317
318         ret = torture_smbconf();
319
320 done:
321         talloc_free(mem_ctx);
322         return ret ? 0 : -1;
323 }