s4-kcc: correctly populate the neighbor object when taking information from repsTo
[Samba/gebeck_regimport.git] / source3 / lib / smbconf / testsuite.c
blob9e679362a325186a7bcb1e5b7e6c37ac46b0d2aa
1 /*
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/>.
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"
27 static void print_strings(const char *prefix,
28 uint32_t num_strings, const char **strings)
30 uint32_t count;
32 if (prefix == NULL) {
33 prefix = "";
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)
43 sbcErr err;
44 bool ret = false;
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));
54 goto done;
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");
62 ret = true;
64 done:
65 talloc_free(mem_ctx);
66 return ret;
69 static bool test_set_get_includes(struct smbconf_ctx *ctx)
71 sbcErr err;
72 uint32_t count;
73 bool ret = false;
74 const char *set_includes[] = {
75 "/path/to/include1",
76 "/path/to/include2"
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",
88 sbcErrorString(err));
89 goto done;
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;
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;
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;
118 printf("OK: set_includes\n");
119 ret = true;
121 done:
122 talloc_free(mem_ctx);
123 return ret;
126 static bool test_delete_includes(struct smbconf_ctx *ctx)
128 sbcErr err;
129 bool ret = false;
130 const char *set_includes[] = {
131 "/path/to/include",
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));
144 goto done;
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));
151 goto done;
154 err = smbconf_get_global_includes(ctx, mem_ctx, &get_num_includes,
155 &get_includes);
156 if (!SBC_ERROR_IS_OK(err)) {
157 printf("FAIL: delete_includes (getting includes) - %s\n",
158 sbcErrorString(err));
159 goto done;
162 if (get_num_includes != 0) {
163 printf("FAIL: delete_includes (not empty after delete)\n");
164 goto done;
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));
171 goto done;
174 printf("OK: delete_includes\n");
175 ret = true;
177 done:
178 return ret;
181 static bool create_conf_file(const char *filename)
183 FILE *f;
185 printf("TEST: creating file\n");
186 f = sys_fopen(filename, "w");
187 if (!f) {
188 printf("failure: failed to open %s for writing: %s\n",
189 filename, strerror(errno));
190 return false;
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");
198 fclose(f);
200 printf("OK: create file\n");
201 return true;
204 static bool torture_smbconf_txt(void)
206 sbcErr err;
207 bool ret = true;
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)) {
215 ret = false;
216 goto done;
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));
223 ret = false;
224 goto done;
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));
235 ret = false;
236 goto done;
238 printf("OK: unlink file\n");
240 done:
241 printf("%s: text backend\n", ret ? "success" : "failure");
242 talloc_free(mem_ctx);
243 return ret;
246 static bool torture_smbconf_reg(void)
248 sbcErr err;
249 bool ret = true;
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));
259 ret = false;
260 goto done;
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);
270 done:
271 printf("%s: registry backend\n", ret ? "success" : "failure");
272 talloc_free(mem_ctx);
273 return ret;
276 static bool torture_smbconf(void)
278 bool ret = true;
279 ret &= torture_smbconf_txt();
280 printf("\n");
281 ret &= torture_smbconf_reg();
282 return ret;
285 int main(int argc, const char **argv)
287 bool ret;
288 poptContext pc;
289 TALLOC_CTX *mem_ctx = talloc_stackframe();
291 struct poptOption long_options[] = {
292 POPT_COMMON_SAMBA
293 {0, 0, 0, 0}
296 load_case_tables();
297 setup_logging(argv[0], DEBUG_STDERR);
299 /* parse options */
300 pc = poptGetContext("smbconftort", argc, (const char **)argv,
301 long_options, 0);
303 while(poptGetNextOpt(pc) != -1) { }
305 poptFreeContext(pc);
307 ret = lp_load_global(get_dyn_CONFIGFILE());
308 if (!ret) {
309 printf("failure: error loading the configuration\n");
310 goto done;
313 ret = torture_smbconf();
315 done:
316 talloc_free(mem_ctx);
317 return ret ? 0 : -1;