torture: include config.h before any glibc headers
[Samba.git] / source4 / torture / local / fsrvp_state.c
blob3a08543b734b97030af438301999844f0c76d967
1 /*
2 Test suite for FSRVP server state
4 Copyright (C) David Disseldorp 2012-2015
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 <unistd.h>
23 #include "librpc/gen_ndr/security.h"
24 #include "lib/param/param.h"
25 #include "lib/util/dlinklist.h"
26 #include "libcli/resolve/resolve.h"
27 #include "librpc/gen_ndr/ndr_fsrvp.h"
28 #include "librpc/gen_ndr/ndr_fsrvp_c.h"
29 #include "source3/rpc_server/fss/srv_fss_private.h"
30 #include "torture/torture.h"
31 #include "torture/local/proto.h"
33 static bool test_fsrvp_state_empty(struct torture_context *tctx)
35 NTSTATUS status;
36 struct fss_global fss_global;
37 struct stat sbuf;
38 char db_dir[] = "fsrvp_torture_XXXXXX";
39 char *db_path = talloc_asprintf(NULL, "%s/%s",
40 mkdtemp(db_dir), FSS_DB_NAME);
42 memset(&fss_global, 0, sizeof(fss_global));
43 fss_global.mem_ctx = talloc_new(NULL);
44 fss_global.db_path = db_path;
46 status = fss_state_store(fss_global.mem_ctx, fss_global.sc_sets,
47 fss_global.sc_sets_count, fss_global.db_path);
48 torture_assert_ntstatus_ok(tctx, status,
49 "failed to store empty fss state");
51 torture_assert_int_equal(tctx, stat(fss_global.db_path, &sbuf), 0,
52 "failed to stat fss state tdb");
53 talloc_free(fss_global.mem_ctx);
55 memset(&fss_global, 0, sizeof(fss_global));
56 fss_global.mem_ctx = talloc_new(NULL);
57 fss_global.db_path = db_path;
59 status = fss_state_retrieve(fss_global.mem_ctx, &fss_global.sc_sets,
60 &fss_global.sc_sets_count,
61 fss_global.db_path);
62 torture_assert_ntstatus_ok(tctx, status,
63 "failed to retrieve empty fss state");
64 torture_assert_int_equal(tctx, fss_global.sc_sets_count, 0,
65 "sc_sets_count set when it should be zero");
66 talloc_free(fss_global.mem_ctx);
67 unlink(db_path);
68 rmdir(db_dir);
69 talloc_free(db_path);
71 return true;
74 static bool test_fsrvp_state_sc_set(struct torture_context *tctx,
75 TALLOC_CTX *mem_ctx,
76 struct fss_sc_set **sc_set_out)
78 struct fss_sc_set *sc_set;
80 sc_set = talloc_zero(mem_ctx, struct fss_sc_set);
81 sc_set->id = GUID_random();
82 sc_set->id_str = GUID_string(sc_set, &sc_set->id);
83 sc_set->state = FSS_SC_COMMITED;
84 sc_set->context = FSRVP_CTX_FILE_SHARE_BACKUP;
85 *sc_set_out = sc_set;
87 return true;
90 static bool test_fsrvp_state_sc(struct torture_context *tctx,
91 TALLOC_CTX *mem_ctx,
92 struct fss_sc **sc_out)
94 struct fss_sc *sc;
96 sc = talloc_zero(mem_ctx, struct fss_sc);
97 sc->id = GUID_random();
98 sc->id_str = GUID_string(sc, &sc->id);
99 sc->volume_name = talloc_strdup(sc, "/this/is/a/path");
100 /* keep snap path NULL, i.e. not yet commited */
101 sc->create_ts = time(NULL);
102 *sc_out = sc;
104 return true;
107 static bool test_fsrvp_state_smap(struct torture_context *tctx,
108 TALLOC_CTX *mem_ctx,
109 const char *base_share_name,
110 const char *sc_share_name,
111 struct fss_sc_smap **smap_out)
113 struct fss_sc_smap *smap;
115 smap = talloc_zero(mem_ctx, struct fss_sc_smap);
116 smap->share_name = talloc_strdup(mem_ctx, base_share_name);
117 smap->sc_share_name = talloc_strdup(mem_ctx, sc_share_name);
118 smap->sc_share_comment = talloc_strdup(mem_ctx, "test sc share comment");
119 smap->is_exposed = false;
120 *smap_out = smap;
122 return true;
125 static bool test_fsrvp_state_smap_compare(struct torture_context *tctx,
126 struct fss_sc_smap *smap_1,
127 struct fss_sc_smap *smap_2)
129 /* already confirmed by caller */
130 torture_assert_str_equal(tctx, smap_1->sc_share_name,
131 smap_2->sc_share_name,
132 "smap sc share name strings differ");
134 torture_assert_str_equal(tctx, smap_1->share_name,
135 smap_2->share_name,
136 "smap share name strings differ");
138 torture_assert_str_equal(tctx, smap_1->sc_share_comment,
139 smap_2->sc_share_comment,
140 "smap sc share comment strings differ");
142 torture_assert(tctx, (smap_1->is_exposed == smap_2->is_exposed),
143 "smap exposure settings differ");
145 return true;
148 static bool test_fsrvp_state_sc_compare(struct torture_context *tctx,
149 struct fss_sc *sc_1,
150 struct fss_sc *sc_2)
152 struct fss_sc_smap *smap_1;
153 struct fss_sc_smap *smap_2;
154 bool ok;
156 /* should have already been confirmed by the caller */
157 torture_assert(tctx, GUID_equal(&sc_1->id, &sc_2->id),
158 "sc guids differ");
160 torture_assert_str_equal(tctx, sc_1->volume_name, sc_2->volume_name,
161 "sc volume_name strings differ");
163 /* may be null, assert_str_eq handles null ptrs safely */
164 torture_assert_str_equal(tctx, sc_1->sc_path, sc_2->sc_path,
165 "sc path strings differ");
167 torture_assert(tctx, difftime(sc_1->create_ts, sc_2->create_ts) == 0,
168 "sc create timestamps differ");
170 torture_assert_int_equal(tctx, sc_1->smaps_count, sc_2->smaps_count,
171 "sc smaps counts differ");
173 for (smap_1 = sc_1->smaps; smap_1; smap_1 = smap_1->next) {
174 bool matched = false;
175 for (smap_2 = sc_2->smaps; smap_2; smap_2 = smap_2->next) {
176 if (strcmp(smap_1->sc_share_name,
177 smap_2->sc_share_name) == 0) {
178 matched = true;
179 ok = test_fsrvp_state_smap_compare(tctx,
180 smap_1,
181 smap_2);
182 torture_assert(tctx, ok, "");
183 break;
186 torture_assert(tctx, matched, "no match for smap");
189 return true;
192 static bool test_fsrvp_state_sc_set_compare(struct torture_context *tctx,
193 struct fss_sc_set *sc_set_1,
194 struct fss_sc_set *sc_set_2)
196 struct fss_sc *sc_1;
197 struct fss_sc *sc_2;
198 bool ok;
200 /* should have already been confirmed by the caller */
201 torture_assert(tctx, GUID_equal(&sc_set_1->id, &sc_set_2->id),
202 "sc_set guids differ");
204 torture_assert_str_equal(tctx, sc_set_1->id_str, sc_set_2->id_str,
205 "sc_set guid strings differ");
207 torture_assert_int_equal(tctx, sc_set_1->state, sc_set_2->state,
208 "sc_set state enums differ");
210 torture_assert_int_equal(tctx, sc_set_1->context, sc_set_2->context,
211 "sc_set contexts differ");
213 torture_assert_int_equal(tctx, sc_set_1->scs_count, sc_set_2->scs_count,
214 "sc_set sc counts differ");
216 for (sc_1 = sc_set_1->scs; sc_1; sc_1 = sc_1->next) {
217 bool matched = false;
218 for (sc_2 = sc_set_2->scs; sc_2; sc_2 = sc_2->next) {
219 if (GUID_equal(&sc_1->id, &sc_2->id)) {
220 matched = true;
221 ok = test_fsrvp_state_sc_compare(tctx, sc_1,
222 sc_2);
223 torture_assert(tctx, ok, "");
224 break;
227 torture_assert(tctx, matched, "no match for sc");
229 return true;
232 static bool test_fsrvp_state_compare(struct torture_context *tctx,
233 struct fss_global *fss_1,
234 struct fss_global *fss_2)
236 struct fss_sc_set *sc_set_1;
237 struct fss_sc_set *sc_set_2;
238 bool ok;
240 torture_assert_int_equal(tctx, fss_1->sc_sets_count,
241 fss_2->sc_sets_count,
242 "sc_sets_count differ");
244 for (sc_set_1 = fss_1->sc_sets; sc_set_1; sc_set_1 = sc_set_1->next) {
245 bool matched = false;
246 for (sc_set_2 = fss_2->sc_sets;
247 sc_set_2;
248 sc_set_2 = sc_set_2->next) {
249 if (GUID_equal(&sc_set_1->id, &sc_set_2->id)) {
250 matched = true;
251 ok = test_fsrvp_state_sc_set_compare(tctx,
252 sc_set_1,
253 sc_set_2);
254 torture_assert(tctx, ok, "");
255 break;
258 torture_assert(tctx, matched, "no match for sc_set");
261 return true;
265 * test a simple heirarchy of:
268 * sc_set
270 * sc
272 * smap
274 static bool test_fsrvp_state_single(struct torture_context *tctx)
276 NTSTATUS status;
277 bool ok;
278 struct fss_global fss_gs;
279 struct fss_global fss_gr;
280 struct fss_sc_set *sc_set;
281 struct fss_sc *sc;
282 struct fss_sc_smap *smap;
283 char db_dir[] = "fsrvp_torture_XXXXXX";
284 char *db_path = talloc_asprintf(NULL, "%s/%s",
285 mkdtemp(db_dir), FSS_DB_NAME);
287 memset(&fss_gs, 0, sizeof(fss_gs));
288 fss_gs.mem_ctx = talloc_new(NULL);
289 fss_gs.db_path = db_path;
291 ok = test_fsrvp_state_sc_set(tctx, fss_gs.mem_ctx, &sc_set);
292 torture_assert(tctx, ok, "failed to create sc set");
294 /* use parent as mem ctx */
295 ok = test_fsrvp_state_sc(tctx, sc_set, &sc);
296 torture_assert(tctx, ok, "failed to create sc");
298 ok = test_fsrvp_state_smap(tctx, sc, "base_share", "sc_share", &smap);
299 torture_assert(tctx, ok, "failed to create smap");
301 DLIST_ADD_END(fss_gs.sc_sets, sc_set, struct fss_sc_set *);
302 fss_gs.sc_sets_count++;
303 DLIST_ADD_END(sc_set->scs, sc, struct fss_sc *);
304 sc_set->scs_count++;
305 sc->sc_set = sc_set;
306 DLIST_ADD_END(sc->smaps, smap, struct fss_sc_smap *);
307 sc->smaps_count++;
309 status = fss_state_store(fss_gs.mem_ctx, fss_gs.sc_sets,
310 fss_gs.sc_sets_count, fss_gs.db_path);
311 torture_assert_ntstatus_ok(tctx, status,
312 "failed to store fss state");
314 memset(&fss_gr, 0, sizeof(fss_gr));
315 fss_gr.mem_ctx = talloc_new(NULL);
316 fss_gr.db_path = db_path;
318 status = fss_state_retrieve(fss_gr.mem_ctx, &fss_gr.sc_sets,
319 &fss_gr.sc_sets_count, fss_gr.db_path);
320 torture_assert_ntstatus_ok(tctx, status,
321 "failed to retrieve fss state");
323 ok = test_fsrvp_state_compare(tctx, &fss_gs, &fss_gr);
324 torture_assert(tctx, ok,
325 "stored and retrieved state comparison failed");
327 talloc_free(fss_gs.mem_ctx);
328 talloc_free(fss_gr.mem_ctx);
329 unlink(db_path);
330 rmdir(db_dir);
331 talloc_free(db_path);
333 return true;
337 * test a complex heirarchy of:
339 * /\
340 * / \
341 * sc_set_a sc_set_b
342 * / \
343 * sc_aa sc_ab
344 * | | \
345 * smap_aaa | \
346 * | \
347 * smap_aba smap_abb
349 static bool test_fsrvp_state_multi(struct torture_context *tctx)
351 NTSTATUS status;
352 bool ok;
353 struct fss_global fss_gs;
354 struct fss_global fss_gr;
355 struct fss_sc_set *sc_set_a;
356 struct fss_sc_set *sc_set_b;
357 struct fss_sc *sc_aa;
358 struct fss_sc *sc_ab;
359 struct fss_sc_smap *smap_aaa;
360 struct fss_sc_smap *smap_aba;
361 struct fss_sc_smap *smap_abb;
362 char db_dir[] = "fsrvp_torture_XXXXXX";
363 char *db_path = talloc_asprintf(NULL, "%s/%s",
364 mkdtemp(db_dir), FSS_DB_NAME);
366 memset(&fss_gs, 0, sizeof(fss_gs));
367 fss_gs.mem_ctx = talloc_new(NULL);
368 fss_gs.db_path = db_path;
370 ok = test_fsrvp_state_sc_set(tctx, fss_gs.mem_ctx, &sc_set_a);
371 torture_assert(tctx, ok, "failed to create sc set");
373 ok = test_fsrvp_state_sc_set(tctx, fss_gs.mem_ctx, &sc_set_b);
374 torture_assert(tctx, ok, "failed to create sc set");
376 /* use parent as mem ctx */
377 ok = test_fsrvp_state_sc(tctx, sc_set_a, &sc_aa);
378 torture_assert(tctx, ok, "failed to create sc");
380 ok = test_fsrvp_state_sc(tctx, sc_set_a, &sc_ab);
381 torture_assert(tctx, ok, "failed to create sc");
383 ok = test_fsrvp_state_smap(tctx, sc_ab, "share_aa", "sc_share_aaa",
384 &smap_aaa);
385 torture_assert(tctx, ok, "failed to create smap");
387 ok = test_fsrvp_state_smap(tctx, sc_ab, "share_ab", "sc_share_aba",
388 &smap_aba);
389 torture_assert(tctx, ok, "failed to create smap");
391 ok = test_fsrvp_state_smap(tctx, sc_ab, "share_ab", "sc_share_abb",
392 &smap_abb);
393 torture_assert(tctx, ok, "failed to create smap");
395 DLIST_ADD_END(fss_gs.sc_sets, sc_set_a, struct fss_sc_set *);
396 fss_gs.sc_sets_count++;
397 DLIST_ADD_END(fss_gs.sc_sets, sc_set_b, struct fss_sc_set *);
398 fss_gs.sc_sets_count++;
400 DLIST_ADD_END(sc_set_a->scs, sc_aa, struct fss_sc *);
401 sc_set_a->scs_count++;
402 sc_aa->sc_set = sc_set_a;
403 DLIST_ADD_END(sc_set_a->scs, sc_ab, struct fss_sc *);
404 sc_set_a->scs_count++;
405 sc_ab->sc_set = sc_set_a;
407 DLIST_ADD_END(sc_aa->smaps, smap_aaa, struct fss_sc_smap *);
408 sc_aa->smaps_count++;
409 DLIST_ADD_END(sc_ab->smaps, smap_aba, struct fss_sc_smap *);
410 sc_ab->smaps_count++;
411 DLIST_ADD_END(sc_ab->smaps, smap_abb, struct fss_sc_smap *);
412 sc_ab->smaps_count++;
414 status = fss_state_store(fss_gs.mem_ctx, fss_gs.sc_sets,
415 fss_gs.sc_sets_count, fss_gs.db_path);
416 torture_assert_ntstatus_ok(tctx, status,
417 "failed to store fss state");
419 memset(&fss_gr, 0, sizeof(fss_gr));
420 fss_gr.mem_ctx = talloc_new(NULL);
421 fss_gr.db_path = db_path;
422 status = fss_state_retrieve(fss_gr.mem_ctx, &fss_gr.sc_sets,
423 &fss_gr.sc_sets_count, fss_gr.db_path);
424 torture_assert_ntstatus_ok(tctx, status,
425 "failed to retrieve fss state");
427 ok = test_fsrvp_state_compare(tctx, &fss_gs, &fss_gr);
428 torture_assert(tctx, ok,
429 "stored and retrieved state comparison failed");
431 talloc_free(fss_gs.mem_ctx);
432 talloc_free(fss_gr.mem_ctx);
433 unlink(db_path);
434 rmdir(db_dir);
435 talloc_free(db_path);
437 return true;
440 static bool test_fsrvp_state_none(struct torture_context *tctx)
442 NTSTATUS status;
443 struct fss_global fss_global;
444 char db_dir[] = "fsrvp_torture_XXXXXX";
445 char *db_path = talloc_asprintf(NULL, "%s/%s",
446 mkdtemp(db_dir), FSS_DB_NAME);
448 memset(&fss_global, 0, sizeof(fss_global));
449 fss_global.mem_ctx = talloc_new(NULL);
450 fss_global.db_path = db_path;
452 status = fss_state_retrieve(fss_global.mem_ctx, &fss_global.sc_sets,
453 &fss_global.sc_sets_count,
454 fss_global.db_path);
455 torture_assert_ntstatus_ok(tctx, status,
456 "failed to retrieve fss state");
457 torture_assert_int_equal(tctx, fss_global.sc_sets_count, 0,
458 "sc_sets_count set when it should be zero");
459 talloc_free(fss_global.mem_ctx);
460 unlink(db_path);
461 rmdir(db_dir);
462 talloc_free(db_path);
464 return true;
467 struct torture_suite *torture_local_fsrvp(TALLOC_CTX *mem_ctx)
469 struct torture_suite *suite = torture_suite_create(mem_ctx,
470 "fsrvp_state");
472 /* dbwrap uses talloc_tos(), hence we need a stackframe :( */
473 talloc_stackframe();
475 torture_suite_add_simple_test(suite,
476 "state_empty",
477 test_fsrvp_state_empty);
479 torture_suite_add_simple_test(suite,
480 "state_single",
481 test_fsrvp_state_single);
483 torture_suite_add_simple_test(suite,
484 "state_multi",
485 test_fsrvp_state_multi);
487 torture_suite_add_simple_test(suite,
488 "state_none",
489 test_fsrvp_state_none);
491 return suite;