593839d88f6e6720c2581aa4470c34088995b8fc
[ddiss/samba.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         sbcErr err;
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         err = smbconf_get_global_includes(ctx, mem_ctx,
51                                           &num_includes, &includes);
52         if (!SBC_ERROR_IS_OK(err)) {
53                 printf("FAIL: get_includes - %s\n", sbcErrorString(err));
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         sbcErr err;
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         err = smbconf_set_global_includes(ctx, set_num_includes, set_includes);
86         if (!SBC_ERROR_IS_OK(err)) {
87                 printf("FAIL: get_set_includes (setting includes) - %s\n",
88                        sbcErrorString(err));
89                 goto done;
90         }
91
92         err = smbconf_get_global_includes(ctx, mem_ctx, &get_num_includes,
93                                           &get_includes);
94         if (!SBC_ERROR_IS_OK(err)) {
95                 printf("FAIL: get_set_includes (getting includes) - %s\n",
96                        sbcErrorString(err));
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         sbcErr err;
130         bool ret = false;
131         const char *set_includes[] = {
132                 "/path/to/include",
133         };
134         uint32_t set_num_includes = 1;
135         char **get_includes = NULL;
136         uint32_t get_num_includes = 0;
137         TALLOC_CTX *mem_ctx = talloc_stackframe();
138
139         printf("TEST: delete_includes\n");
140
141         err = smbconf_set_global_includes(ctx, set_num_includes, set_includes);
142         if (!SBC_ERROR_IS_OK(err)) {
143                 printf("FAIL: delete_includes (setting includes) - %s\n",
144                        sbcErrorString(err));
145                 goto done;
146         }
147
148         werr = smbconf_delete_global_includes(ctx);
149         if (!W_ERROR_IS_OK(werr)) {
150                 printf("FAIL: delete_includes (deleting includes) - %s\n",
151                        win_errstr(werr));
152                 goto done;
153         }
154
155         err = smbconf_get_global_includes(ctx, mem_ctx, &get_num_includes,
156                                           &get_includes);
157         if (!SBC_ERROR_IS_OK(err)) {
158                 printf("FAIL: delete_includes (getting includes) - %s\n",
159                        sbcErrorString(err));
160                 goto done;
161         }
162
163         if (get_num_includes != 0) {
164                 printf("FAIL: delete_includes (not empty after delete)\n");
165                 goto done;
166         }
167
168         werr = smbconf_delete_global_includes(ctx);
169         if (!W_ERROR_IS_OK(werr)) {
170                 printf("FAIL: delete_includes (delete empty includes) - "
171                        "%s\n", win_errstr(werr));
172                 goto done;
173         }
174
175         printf("OK: delete_includes\n");
176         ret = true;
177
178 done:
179         return ret;
180 }
181
182 static bool create_conf_file(const char *filename)
183 {
184         FILE *f;
185
186         printf("TEST: creating file\n");
187         f = sys_fopen(filename, "w");
188         if (!f) {
189                 printf("failure: failed to open %s for writing: %s\n",
190                        filename, strerror(errno));
191                 return false;
192         }
193
194         fprintf(f, "[global]\n");
195         fprintf(f, "\tserver string = smbconf testsuite\n");
196         fprintf(f, "\tworkgroup = SAMBA\n");
197         fprintf(f, "\tsecurity = user\n");
198
199         fclose(f);
200
201         printf("OK: create file\n");
202         return true;
203 }
204
205 static bool torture_smbconf_txt(void)
206 {
207         sbcErr err;
208         bool ret = true;
209         const char *filename = "/tmp/smb.conf.smbconf_testsuite";
210         struct smbconf_ctx *conf_ctx = NULL;
211         TALLOC_CTX *mem_ctx = talloc_stackframe();
212
213         printf("test: text backend\n");
214
215         if (!create_conf_file(filename)) {
216                 ret = false;
217                 goto done;
218         }
219
220         printf("TEST: init\n");
221         err = smbconf_init_txt(mem_ctx, &conf_ctx, filename);
222         if (!SBC_ERROR_IS_OK(err)) {
223                 printf("FAIL: text backend failed: %s\n", sbcErrorString(err));
224                 ret = false;
225                 goto done;
226         }
227         printf("OK: init\n");
228
229         ret &= test_get_includes(conf_ctx);
230
231         smbconf_shutdown(conf_ctx);
232
233         printf("TEST: unlink file\n");
234         if (unlink(filename) != 0) {
235                 printf("OK: unlink failed: %s\n", strerror(errno));
236                 ret = false;
237                 goto done;
238         }
239         printf("OK: unlink file\n");
240
241 done:
242         printf("%s: text backend\n", ret ? "success" : "failure");
243         talloc_free(mem_ctx);
244         return ret;
245 }
246
247 static bool torture_smbconf_reg(void)
248 {
249         sbcErr err;
250         bool ret = true;
251         struct smbconf_ctx *conf_ctx = NULL;
252         TALLOC_CTX *mem_ctx = talloc_stackframe();
253
254         printf("test: registry backend\n");
255
256         printf("TEST: init\n");
257         err = smbconf_init_reg(mem_ctx, &conf_ctx, NULL);
258         if (!SBC_ERROR_IS_OK(err)) {
259                 printf("FAIL: init failed: %s\n", sbcErrorString(err));
260                 ret = false;
261                 goto done;
262         }
263         printf("OK: init\n");
264
265         ret &= test_get_includes(conf_ctx);
266         ret &= test_set_get_includes(conf_ctx);
267         ret &= test_delete_includes(conf_ctx);
268
269         smbconf_shutdown(conf_ctx);
270
271 done:
272         printf("%s: registry backend\n", ret ? "success" : "failure");
273         talloc_free(mem_ctx);
274         return ret;
275 }
276
277 static bool torture_smbconf(void)
278 {
279         bool ret = true;
280         ret &= torture_smbconf_txt();
281         printf("\n");
282         ret &= torture_smbconf_reg();
283         return ret;
284 }
285
286 int main(int argc, const char **argv)
287 {
288         bool ret;
289         poptContext pc;
290         TALLOC_CTX *mem_ctx = talloc_stackframe();
291
292         struct poptOption long_options[] = {
293                 POPT_COMMON_SAMBA
294                 {0, 0, 0, 0}
295         };
296
297         load_case_tables();
298         setup_logging(argv[0], DEBUG_STDERR);
299
300         /* parse options */
301         pc = poptGetContext("smbconftort", argc, (const char **)argv,
302                             long_options, 0);
303
304         while(poptGetNextOpt(pc) != -1) { }
305
306         poptFreeContext(pc);
307
308         ret = lp_load(get_dyn_CONFIGFILE(),
309                       true,  /* globals_only */
310                       false, /* save_defaults */
311                       false, /* add_ipc */
312                       true   /* initialize globals */);
313
314         if (!ret) {
315                 printf("failure: error loading the configuration\n");
316                 goto done;
317         }
318
319         ret = torture_smbconf();
320
321 done:
322         talloc_free(mem_ctx);
323         return ret ? 0 : -1;
324 }