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 "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"
27 static void print_strings(const char *prefix
,
28 uint32_t num_strings
, const char **strings
)
36 for (count
= 0; count
< num_strings
; count
++) {
37 printf("%s%s\n", prefix
, strings
[count
]);
41 static bool test_get_includes(struct smbconf_ctx
*ctx
)
45 uint32_t num_includes
= 0;
46 char **includes
= NULL
;
47 TALLOC_CTX
*mem_ctx
= talloc_stackframe();
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
));
57 printf("got %u includes%s\n", num_includes
,
58 (num_includes
> 0) ? ":" : ".");
59 print_strings("", num_includes
, (const char **)includes
);
61 printf("OK: get_includes\n");
69 static bool test_set_get_includes(struct smbconf_ctx
*ctx
)
74 const char *set_includes
[] = {
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();
83 printf("TEST: set_get_includes\n");
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",
92 err
= smbconf_get_global_includes(ctx
, mem_ctx
, &get_num_includes
,
94 if (!SBC_ERROR_IS_OK(err
)) {
95 printf("FAIL: get_set_includes (getting includes) - %s\n",
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
);
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
);
111 print_strings("* ", get_num_includes
,
112 (const char **)get_includes
);
113 printf("FAIL: get_set_includes - data mismatch:\n");
118 printf("OK: set_includes\n");
122 talloc_free(mem_ctx
);
126 static bool test_delete_includes(struct smbconf_ctx
*ctx
)
130 const char *set_includes
[] = {
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();
138 printf("TEST: delete_includes\n");
140 err
= smbconf_set_global_includes(ctx
, set_num_includes
, set_includes
);
141 if (!SBC_ERROR_IS_OK(err
)) {
142 printf("FAIL: delete_includes (setting includes) - %s\n",
143 sbcErrorString(err
));
147 err
= smbconf_delete_global_includes(ctx
);
148 if (!SBC_ERROR_IS_OK(err
)) {
149 printf("FAIL: delete_includes (deleting includes) - %s\n",
150 sbcErrorString(err
));
154 err
= smbconf_get_global_includes(ctx
, mem_ctx
, &get_num_includes
,
156 if (!SBC_ERROR_IS_OK(err
)) {
157 printf("FAIL: delete_includes (getting includes) - %s\n",
158 sbcErrorString(err
));
162 if (get_num_includes
!= 0) {
163 printf("FAIL: delete_includes (not empty after delete)\n");
167 err
= smbconf_delete_global_includes(ctx
);
168 if (!SBC_ERROR_IS_OK(err
)) {
169 printf("FAIL: delete_includes (delete empty includes) - "
170 "%s\n", sbcErrorString(err
));
174 printf("OK: delete_includes\n");
181 static bool create_conf_file(const char *filename
)
185 printf("TEST: creating file\n");
186 f
= sys_fopen(filename
, "w");
188 printf("failure: failed to open %s for writing: %s\n",
189 filename
, strerror(errno
));
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");
200 printf("OK: create file\n");
204 static bool torture_smbconf_txt(void)
208 const char *filename
= "/tmp/smb.conf.smbconf_testsuite";
209 struct smbconf_ctx
*conf_ctx
= NULL
;
210 TALLOC_CTX
*mem_ctx
= talloc_stackframe();
212 printf("test: text backend\n");
214 if (!create_conf_file(filename
)) {
219 printf("TEST: init\n");
220 err
= smbconf_init_txt(mem_ctx
, &conf_ctx
, filename
);
221 if (!SBC_ERROR_IS_OK(err
)) {
222 printf("FAIL: text backend failed: %s\n", sbcErrorString(err
));
226 printf("OK: init\n");
228 ret
&= test_get_includes(conf_ctx
);
230 smbconf_shutdown(conf_ctx
);
232 printf("TEST: unlink file\n");
233 if (unlink(filename
) != 0) {
234 printf("OK: unlink failed: %s\n", strerror(errno
));
238 printf("OK: unlink file\n");
241 printf("%s: text backend\n", ret
? "success" : "failure");
242 talloc_free(mem_ctx
);
246 static bool torture_smbconf_reg(void)
250 struct smbconf_ctx
*conf_ctx
= NULL
;
251 TALLOC_CTX
*mem_ctx
= talloc_stackframe();
253 printf("test: registry backend\n");
255 printf("TEST: init\n");
256 err
= smbconf_init_reg(mem_ctx
, &conf_ctx
, NULL
);
257 if (!SBC_ERROR_IS_OK(err
)) {
258 printf("FAIL: init failed: %s\n", sbcErrorString(err
));
262 printf("OK: init\n");
264 ret
&= test_get_includes(conf_ctx
);
265 ret
&= test_set_get_includes(conf_ctx
);
266 ret
&= test_delete_includes(conf_ctx
);
268 smbconf_shutdown(conf_ctx
);
271 printf("%s: registry backend\n", ret
? "success" : "failure");
272 talloc_free(mem_ctx
);
276 static bool torture_smbconf(void)
279 ret
&= torture_smbconf_txt();
281 ret
&= torture_smbconf_reg();
285 int main(int argc
, const char **argv
)
289 TALLOC_CTX
*mem_ctx
= talloc_stackframe();
291 struct poptOption long_options
[] = {
297 setup_logging(argv
[0], DEBUG_STDERR
);
300 pc
= poptGetContext("smbconftort", argc
, (const char **)argv
,
303 while(poptGetNextOpt(pc
) != -1) { }
307 ret
= lp_load_global(get_dyn_CONFIGFILE());
309 printf("failure: error loading the configuration\n");
313 ret
= torture_smbconf();
316 talloc_free(mem_ctx
);