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