2 * Unix SMB/CIFS implementation.
3 * libsmbconf - Samba configuration library: testsuite
4 * Copyright (C) Michael Adam 2008
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.
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.
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/>.
21 #include "lib/cmdline/cmdline.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"
27 static void print_strings(const char *prefix
,
29 const char * const *strings
)
37 for (count
= 0; count
< num_strings
; count
++) {
38 printf("%s%s\n", prefix
, strings
[count
]);
42 static bool test_get_includes(struct smbconf_ctx
*ctx
)
46 uint32_t num_includes
= 0;
47 char **includes
= NULL
;
48 TALLOC_CTX
*mem_ctx
= talloc_stackframe();
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
));
58 printf("got %u includes%s\n", num_includes
,
59 (num_includes
> 0) ? ":" : ".");
60 print_strings("", num_includes
, (const char * const *)includes
);
62 printf("OK: get_includes\n");
70 static bool test_set_get_includes(struct smbconf_ctx
*ctx
)
75 const char *set_includes
[] = {
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();
84 printf("TEST: set_get_includes\n");
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",
93 err
= smbconf_get_global_includes(ctx
, mem_ctx
, &get_num_includes
,
95 if (!SBC_ERROR_IS_OK(err
)) {
96 printf("FAIL: get_set_includes (getting includes) - %s\n",
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
);
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
);
113 print_strings("* ", get_num_includes
,
114 (const char * const *)get_includes
);
115 printf("FAIL: get_set_includes - data mismatch:\n");
120 printf("OK: set_includes\n");
124 talloc_free(mem_ctx
);
128 static bool test_delete_includes(struct smbconf_ctx
*ctx
)
132 const char *set_includes
[] = {
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();
140 printf("TEST: delete_includes\n");
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
));
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
));
156 err
= smbconf_get_global_includes(ctx
, mem_ctx
, &get_num_includes
,
158 if (!SBC_ERROR_IS_OK(err
)) {
159 printf("FAIL: delete_includes (getting includes) - %s\n",
160 sbcErrorString(err
));
164 if (get_num_includes
!= 0) {
165 printf("FAIL: delete_includes (not empty after delete)\n");
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
));
176 printf("OK: delete_includes\n");
180 talloc_free(mem_ctx
);
184 static bool create_conf_file(const char *filename
)
188 printf("TEST: creating file\n");
189 f
= fopen(filename
, "w");
191 printf("failure: failed to open %s for writing: %s\n",
192 filename
, strerror(errno
));
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");
203 printf("OK: create file\n");
207 static bool torture_smbconf_txt(void)
211 const char *filename
= "/tmp/smb.conf.smbconf_testsuite";
212 struct smbconf_ctx
*conf_ctx
= NULL
;
213 TALLOC_CTX
*mem_ctx
= talloc_stackframe();
215 printf("test: text backend\n");
217 if (!create_conf_file(filename
)) {
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
));
229 printf("OK: init\n");
231 ret
&= test_get_includes(conf_ctx
);
233 smbconf_shutdown(conf_ctx
);
235 printf("TEST: unlink file\n");
236 if (unlink(filename
) != 0) {
237 printf("OK: unlink failed: %s\n", strerror(errno
));
241 printf("OK: unlink file\n");
244 printf("%s: text backend\n", ret
? "success" : "failure");
245 talloc_free(mem_ctx
);
249 static bool torture_smbconf_reg(void)
253 struct smbconf_ctx
*conf_ctx
= NULL
;
254 TALLOC_CTX
*mem_ctx
= talloc_stackframe();
256 printf("test: registry backend\n");
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
));
265 printf("OK: init\n");
267 ret
&= test_get_includes(conf_ctx
);
268 ret
&= test_set_get_includes(conf_ctx
);
269 ret
&= test_delete_includes(conf_ctx
);
271 smbconf_shutdown(conf_ctx
);
274 printf("%s: registry backend\n", ret
? "success" : "failure");
275 talloc_free(mem_ctx
);
279 static bool torture_smbconf(void)
282 ret
&= torture_smbconf_txt();
284 ret
&= torture_smbconf_reg();
288 int main(int argc
, const char **argv
)
292 TALLOC_CTX
*mem_ctx
= talloc_stackframe();
295 struct poptOption long_options
[] = {
303 ret
= samba_cmdline_init(mem_ctx
,
304 SAMBA_CMDLINE_CONFIG_CLIENT
,
305 true /* require_smbconf */);
311 pc
= samba_popt_get_context(getprogname(),
321 while ((opt
= poptGetNextOpt(pc
)) != -1) {
323 case POPT_ERROR_BADOPT
:
324 fprintf(stderr
, "\nInvalid option %s: %s\n\n",
325 poptBadOption(pc
, 0), poptStrerror(opt
));
326 poptPrintUsage(pc
, stderr
, 0);
333 ret
= torture_smbconf();
336 talloc_free(mem_ctx
);