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/>.
22 static void print_strings(const char *prefix,
23 uint32_t num_strings, const char **strings)
31 for (count = 0; count < num_strings; count++) {
32 printf("%s%s\n", prefix, strings[count]);
36 static bool test_get_includes(struct smbconf_ctx *ctx)
40 uint32_t num_includes = 0;
41 char **includes = NULL;
42 TALLOC_CTX *mem_ctx = talloc_stackframe();
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("FAIL: get_includes - %s\n", win_errstr(werr));
52 printf("got %u includes%s\n", num_includes,
53 (num_includes > 0) ? ":" : ".");
54 print_strings("", num_includes, (const char **)includes);
56 printf("OK: get_includes\n");
64 static bool test_set_get_includes(struct smbconf_ctx *ctx)
69 const char *set_includes[] = {
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();
78 printf("TEST: set_get_includes\n");
80 werr = smbconf_set_global_includes(ctx, set_num_includes, set_includes);
81 if (!W_ERROR_IS_OK(werr)) {
82 printf("FAIL: get_set_includes (setting includes) - %s\n",
87 werr = smbconf_get_global_includes(ctx, mem_ctx, &get_num_includes,
89 if (!W_ERROR_IS_OK(werr)) {
90 printf("FAIL: get_set_includes (getting includes) - %s\n",
95 if (get_num_includes != set_num_includes) {
96 printf("FAIL: get_set_includes - set %d includes, got %d\n",
97 set_num_includes, get_num_includes);
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);
106 print_strings("* ", get_num_includes,
107 (const char **)get_includes);
108 printf("FAIL: get_set_includes - data mismatch:\n");
113 printf("OK: set_includes\n");
117 talloc_free(mem_ctx);
121 static bool test_delete_includes(struct smbconf_ctx *ctx)
125 const char *set_includes[] = {
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();
133 printf("TEST: delete_includes\n");
135 werr = smbconf_set_global_includes(ctx, set_num_includes, set_includes);
136 if (!W_ERROR_IS_OK(werr)) {
137 printf("FAIL: delete_includes (setting includes) - %s\n",
142 werr = smbconf_delete_global_includes(ctx);
143 if (!W_ERROR_IS_OK(werr)) {
144 printf("FAIL: delete_includes (deleting includes) - %s\n",
149 werr = smbconf_get_global_includes(ctx, mem_ctx, &get_num_includes,
151 if (!W_ERROR_IS_OK(werr)) {
152 printf("FAIL: delete_includes (getting includes) - %s\n",
157 if (get_num_includes != 0) {
158 printf("FAIL: delete_includes (not empty after delete)\n");
162 werr = smbconf_delete_global_includes(ctx);
163 if (!W_ERROR_IS_OK(werr)) {
164 printf("FAIL: delete_includes (delete empty includes) - "
165 "%s\n", win_errstr(werr));
169 printf("OK: delete_includes\n");
176 static bool create_conf_file(const char *filename)
180 printf("TEST: creating file\n");
181 f = sys_fopen(filename, "w");
183 printf("failure: failed to open %s for writing: %s\n",
184 filename, strerror(errno));
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");
195 printf("OK: create file\n");
199 static bool torture_smbconf_txt(void)
203 const char *filename = "/tmp/smb.conf.smbconf_testsuite";
204 struct smbconf_ctx *conf_ctx = NULL;
205 TALLOC_CTX *mem_ctx = talloc_stackframe();
207 printf("test: text backend\n");
209 if (!create_conf_file(filename)) {
214 printf("TEST: init\n");
215 werr = smbconf_init_txt(mem_ctx, &conf_ctx, filename);
216 if (!W_ERROR_IS_OK(werr)) {
217 printf("FAIL: text backend\[ failed: %s\n", win_errstr(werr));
221 printf("OK: init\n");
223 ret &= test_get_includes(conf_ctx);
225 smbconf_shutdown(conf_ctx);
227 printf("TEST: unlink file\n");
228 if (unlink(filename) != 0) {
229 printf("OK: unlink failed: %s\n", strerror(errno));
233 printf("OK: unlink file\n");
236 printf("%s: text backend\n", ret ? "success" : "failure");
237 talloc_free(mem_ctx);
241 static bool torture_smbconf_reg(void)
245 struct smbconf_ctx *conf_ctx = NULL;
246 TALLOC_CTX *mem_ctx = talloc_stackframe();
248 printf("test: registry backend\n");
250 printf("TEST: init\n");
251 werr = smbconf_init_reg(mem_ctx, &conf_ctx, NULL);
252 if (!W_ERROR_IS_OK(werr)) {
253 printf("FAIL: init failed: %s\n", win_errstr(werr));
257 printf("OK: init\n");
259 ret &= test_get_includes(conf_ctx);
260 ret &= test_set_get_includes(conf_ctx);
261 ret &= test_delete_includes(conf_ctx);
263 smbconf_shutdown(conf_ctx);
266 printf("%s: registry backend\n", ret ? "success" : "failure");
267 talloc_free(mem_ctx);
271 static bool torture_smbconf(void)
274 ret &= torture_smbconf_txt();
276 ret &= torture_smbconf_reg();
280 int main(int argc, const char **argv)
284 TALLOC_CTX *mem_ctx = talloc_stackframe();
286 struct poptOption long_options[] = {
295 pc = poptGetContext("smbconftort", argc, (const char **)argv,
298 while(poptGetNextOpt(pc) != -1) { }
302 ret = lp_load(get_dyn_CONFIGFILE(),
303 true, /* globals_only */
304 false, /* save_defaults */
306 true /* initialize globals */);
309 printf("failure: error loading the configuration\n");
313 ret = torture_smbconf();
316 talloc_free(mem_ctx);