s3:smb2_negprot: set the 'remote_proto' value
[Samba/gebeck_regimport.git] / source3 / lib / smbconf / testsuite.c
blob79083614eeebb0856c29861870b41b79f6b0dba5
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 talloc_free(mem_ctx);
179 return ret;
182 static bool create_conf_file(const char *filename)
184 FILE *f;
186 printf("TEST: creating file\n");
187 f = fopen(filename, "w");
188 if (!f) {
189 printf("failure: failed to open %s for writing: %s\n",
190 filename, strerror(errno));
191 return false;
194 fprintf(f, "[global]\n");
195 fprintf(f, "\tserver string = smbconf testsuite\n");
196 fprintf(f, "\tworkgroup = SAMBA\n");
197 fprintf(f, "\tsecurity = user\n");
199 fclose(f);
201 printf("OK: create file\n");
202 return true;
205 static bool torture_smbconf_txt(void)
207 sbcErr err;
208 bool ret = true;
209 const char *filename = "/tmp/smb.conf.smbconf_testsuite";
210 struct smbconf_ctx *conf_ctx = NULL;
211 TALLOC_CTX *mem_ctx = talloc_stackframe();
213 printf("test: text backend\n");
215 if (!create_conf_file(filename)) {
216 ret = false;
217 goto done;
220 printf("TEST: init\n");
221 err = smbconf_init_txt(mem_ctx, &conf_ctx, filename);
222 if (!SBC_ERROR_IS_OK(err)) {
223 printf("FAIL: text backend failed: %s\n", sbcErrorString(err));
224 ret = false;
225 goto done;
227 printf("OK: init\n");
229 ret &= test_get_includes(conf_ctx);
231 smbconf_shutdown(conf_ctx);
233 printf("TEST: unlink file\n");
234 if (unlink(filename) != 0) {
235 printf("OK: unlink failed: %s\n", strerror(errno));
236 ret = false;
237 goto done;
239 printf("OK: unlink file\n");
241 done:
242 printf("%s: text backend\n", ret ? "success" : "failure");
243 talloc_free(mem_ctx);
244 return ret;
247 static bool torture_smbconf_reg(void)
249 sbcErr err;
250 bool ret = true;
251 struct smbconf_ctx *conf_ctx = NULL;
252 TALLOC_CTX *mem_ctx = talloc_stackframe();
254 printf("test: registry backend\n");
256 printf("TEST: init\n");
257 err = smbconf_init_reg(mem_ctx, &conf_ctx, NULL);
258 if (!SBC_ERROR_IS_OK(err)) {
259 printf("FAIL: init failed: %s\n", sbcErrorString(err));
260 ret = false;
261 goto done;
263 printf("OK: init\n");
265 ret &= test_get_includes(conf_ctx);
266 ret &= test_set_get_includes(conf_ctx);
267 ret &= test_delete_includes(conf_ctx);
269 smbconf_shutdown(conf_ctx);
271 done:
272 printf("%s: registry backend\n", ret ? "success" : "failure");
273 talloc_free(mem_ctx);
274 return ret;
277 static bool torture_smbconf(void)
279 bool ret = true;
280 ret &= torture_smbconf_txt();
281 printf("\n");
282 ret &= torture_smbconf_reg();
283 return ret;
286 int main(int argc, const char **argv)
288 bool ret;
289 poptContext pc;
290 TALLOC_CTX *mem_ctx = talloc_stackframe();
292 struct poptOption long_options[] = {
293 POPT_COMMON_SAMBA
294 {0, 0, 0, 0}
297 load_case_tables();
298 setup_logging(argv[0], DEBUG_STDERR);
300 /* parse options */
301 pc = poptGetContext("smbconftort", argc, (const char **)argv,
302 long_options, 0);
304 while(poptGetNextOpt(pc) != -1) { }
306 poptFreeContext(pc);
308 ret = lp_load_global(get_dyn_CONFIGFILE());
309 if (!ret) {
310 printf("failure: error loading the configuration\n");
311 goto done;
314 ret = torture_smbconf();
316 done:
317 talloc_free(mem_ctx);
318 return ret ? 0 : -1;