ctdb-tool: Remove old ctdb tool
[Samba.git] / source3 / torture / test_idmap_tdb_common.c
blob897d2173002509c25f0f9fcf5ccdd4f8f815f1c8
1 /*
2 Unix SMB/CIFS implementation.
3 IDMAP TDB common code tester
5 Copyright (C) Christian Ambach 2012
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>.
21 #include "includes.h"
22 #include "system/filesys.h"
23 #include "torture/proto.h"
24 #include "idmap.h"
25 #include "winbindd/idmap_rw.h"
26 #include "winbindd/idmap_tdb_common.h"
27 #include "winbindd/winbindd.h"
28 #include "winbindd/winbindd_proto.h"
29 #include "dbwrap/dbwrap.h"
30 #include "dbwrap/dbwrap_open.h"
31 #include "../libcli/security/dom_sid.h"
33 #define HWM_GROUP "GROUP HWM"
34 #define HWM_USER "USER HWM"
36 #define LOW_ID 100
37 #define HIGH_ID 199
39 #define DOM_SID1 "S-1-5-21-1234-5678-9012"
40 #define DOM_SID2 "S-1-5-21-0123-5678-9012"
41 #define DOM_SID3 "S-1-5-21-0012-5678-9012"
42 #define DOM_SID4 "S-1-5-21-0001-5678-9012"
43 #define DOM_SID5 "S-1-5-21-2345-5678-9012"
44 #define DOM_SID6 "S-1-5-21-3456-5678-9012"
46 /* overwrite some winbind internal functions */
47 struct winbindd_domain *find_domain_from_name(const char *domain_name)
49 return NULL;
52 bool get_global_winbindd_state_offline(void) {
53 return false;
56 bool winbindd_use_idmap_cache(void) {
57 return false;
60 bool idmap_is_online(void)
62 return true;
65 static bool open_db(struct idmap_tdb_common_context *ctx)
67 NTSTATUS status;
68 char *db_path;
70 if(ctx->db) {
71 /* already open */
72 return true;
75 db_path = talloc_asprintf(talloc_tos(), "%s/idmap_test.tdb",
76 lp_private_dir());
77 if(!db_path) {
78 DEBUG(0, ("Out of memory!\n"));
79 return false;
82 ctx->db = db_open(ctx, db_path, 0, TDB_DEFAULT,
83 O_RDWR | O_CREAT, 0600,
84 DBWRAP_LOCK_ORDER_1, DBWRAP_FLAG_NONE);
86 if(!ctx->db) {
87 DEBUG(0, ("Failed to open database: %s\n", strerror(errno)));
88 return false;
91 if(dbwrap_transaction_start(ctx->db) != 0) {
92 DEBUG(0, ("Failed to start transaction!\n"));
93 return false;
96 status = dbwrap_store_uint32_bystring(ctx->db, ctx->hwmkey_uid,
97 LOW_ID);
98 if(!NT_STATUS_IS_OK(status)) {
99 dbwrap_transaction_cancel(ctx->db);
100 return false;
103 status = dbwrap_store_uint32_bystring(ctx->db, ctx->hwmkey_gid,
104 LOW_ID);
105 if(!NT_STATUS_IS_OK(status)) {
106 dbwrap_transaction_cancel(ctx->db);
107 return false;
110 if(dbwrap_transaction_commit(ctx->db) != 0) {
111 DEBUG(0, ("Failed to commit transaction!\n"));
112 return false;
115 return true;
118 static struct idmap_tdb_common_context *createcontext(TALLOC_CTX *memctx)
120 struct idmap_tdb_common_context *ret;
122 ret = talloc_zero(memctx, struct idmap_tdb_common_context);
123 ret->rw_ops = talloc_zero(ret, struct idmap_rw_ops);
125 ret->max_id = HIGH_ID;
126 ret->hwmkey_uid = HWM_USER;
127 ret->hwmkey_gid = HWM_GROUP;
129 ret->rw_ops->get_new_id = idmap_tdb_common_get_new_id;
130 ret->rw_ops->set_mapping = idmap_tdb_common_set_mapping;
132 if (!open_db(ret)) {
133 return NULL;
136 return ret;
139 static struct idmap_domain *createdomain(TALLOC_CTX *memctx)
141 struct idmap_domain *dom;
143 dom = talloc_zero(memctx, struct idmap_domain);
144 dom->name = "*";
145 dom->low_id = LOW_ID;
146 dom->high_id = HIGH_ID;
147 dom->read_only = false;
148 dom->methods = talloc_zero(dom, struct idmap_methods);
149 dom->methods->sids_to_unixids = idmap_tdb_common_sids_to_unixids;
150 dom->methods->unixids_to_sids = idmap_tdb_common_unixids_to_sids;
151 dom->methods->allocate_id = idmap_tdb_common_get_new_id;
153 return dom;
156 static bool test_getnewid1(TALLOC_CTX *memctx, struct idmap_domain *dom)
158 NTSTATUS status;
159 struct unixid id;
161 id.type = ID_TYPE_UID;
163 status = idmap_tdb_common_get_new_id(dom, &id);
165 if(!NT_STATUS_IS_OK(status)) {
166 DEBUG(0, ("test_getnewid1: Could not allocate id!\n"));
167 return false;
170 if(id.id == 0) {
171 DEBUG(0, ("test_getnewid1: Allocate returned "
172 "empty id!\n"));
173 return false;
176 if(id.id > HIGH_ID || id.id < LOW_ID) {
177 DEBUG(0, ("test_getnewid1: Allocate returned "
178 "out of range id!\n"));
179 return false;
182 DEBUG(0, ("test_getnewid1: PASSED!\n"));
184 return true;
187 static bool test_getnewid2(TALLOC_CTX *memctx, struct idmap_domain *dom)
189 NTSTATUS status;
190 struct unixid id;
191 int i, left;
193 id.type = ID_TYPE_UID;
195 status = idmap_tdb_common_get_new_id(dom, &id);
197 if(!NT_STATUS_IS_OK(status)) {
198 DEBUG(0, ("test_getnewid2: Could not allocate id!\n"));
199 return false;
202 if(id.id == 0) {
203 DEBUG(0, ("test_getnewid2: Allocate returned "
204 "empty id!\n"));
205 return false;
208 if(id.id > HIGH_ID || id.id < LOW_ID) {
209 DEBUG(0, ("test_getnewid2: Allocate returned "
210 "out of range id!\n"));
211 return false;
214 /* how many ids are left? */
216 left = HIGH_ID - id.id;
218 /* consume them all */
219 for(i = 0; i<left; i++) {
221 status = idmap_tdb_common_get_new_id(dom, &id);
223 if(!NT_STATUS_IS_OK(status)) {
224 DEBUG(0, ("test_getnewid2: Allocate returned "
225 "error %s\n", nt_errstr(status)));
226 return false;
229 if(id.id > HIGH_ID) {
230 DEBUG(0, ("test_getnewid2: Allocate returned "
231 "out of range id (%d)!\n", id.id));
232 return false;
236 /* one more must fail */
237 status = idmap_tdb_common_get_new_id(dom, &id);
239 if(NT_STATUS_IS_OK(status)) {
240 DEBUG(0, ("test_getnewid2: Could allocate id (%d) from "
241 "depleted pool!\n", id.id));
242 return false;
245 DEBUG(0, ("test_getnewid2: PASSED!\n"));
247 return true;
250 static bool test_setmap1(TALLOC_CTX *memctx, struct idmap_domain *dom)
252 NTSTATUS status;
253 struct id_map map;
255 ZERO_STRUCT(map);
257 /* test for correct return code with invalid data */
259 status = idmap_tdb_common_set_mapping(dom, NULL);
260 if(!NT_STATUS_EQUAL(status, NT_STATUS_INVALID_PARAMETER)) {
261 DEBUG(0, ("test_setmap1: bad parameter handling!\n"));
262 return false;
265 status = idmap_tdb_common_set_mapping(dom, &map);
266 if(!NT_STATUS_EQUAL(status, NT_STATUS_INVALID_PARAMETER)) {
267 DEBUG(0, ("test_setmap1: bad parameter handling!\n"));
268 return false;
271 map.sid = dom_sid_parse_talloc(memctx, DOM_SID1 "-100");
273 map.xid.type = ID_TYPE_NOT_SPECIFIED;
274 map.xid.id = 4711;
276 status = idmap_tdb_common_set_mapping(dom, &map);
277 if(!NT_STATUS_EQUAL(status, NT_STATUS_INVALID_PARAMETER)) {
278 DEBUG(0, ("test_setmap1: bad parameter handling!\n"));
279 return false;
282 /* now the good ones */
283 map.xid.type = ID_TYPE_UID;
284 map.xid.id = 0;
286 status = idmap_tdb_common_get_new_id(dom, &(map.xid));
287 if(!NT_STATUS_IS_OK(status)) {
288 DEBUG(0, ("test_setmap1: get_new_uid failed!\n"));
289 return false;
292 status = idmap_tdb_common_set_mapping(dom, &map);
293 if(!NT_STATUS_IS_OK(status)) {
294 DEBUG(0, ("test_setmap1: setting UID mapping failed!\n"));
295 return false;
298 /* try to set the same mapping again as group (must fail) */
300 map.xid.type = ID_TYPE_GID;
301 status = idmap_tdb_common_set_mapping(dom, &map);
302 if(NT_STATUS_IS_OK(status)) {
303 DEBUG(0, ("test_setmap1: could create map for "
304 "group and user!\n"));
305 return false;
308 /* now a group with a different SID*/
309 map.xid.id = 0;
311 map.sid = dom_sid_parse_talloc(memctx, DOM_SID1 "-101");
313 status = idmap_tdb_common_get_new_id(dom, &(map.xid));
314 if(!NT_STATUS_IS_OK(status)) {
315 DEBUG(0, ("test_setmap1: get_new_gid failed!\n"));
316 return false;
319 status = idmap_tdb_common_set_mapping(dom, &map);
320 if(!NT_STATUS_IS_OK(status)) {
321 DEBUG(0, ("test_setmap1: setting GID mapping failed!\n"));
322 return false;
324 DEBUG(0, ("test_setmap1: PASSED!\n"));
326 return true;
329 static bool test_sid2unixid1(TALLOC_CTX *memctx, struct idmap_domain *dom)
331 NTSTATUS status1, status2, status3;
332 struct id_map map;
334 /* check for correct dealing with bad parameters */
335 status1 = idmap_tdb_common_sid_to_unixid(NULL, &map);
336 status2 = idmap_tdb_common_sid_to_unixid(dom, NULL);
337 status3 = idmap_tdb_common_sid_to_unixid(NULL, NULL);
339 if(!NT_STATUS_EQUAL(NT_STATUS_INVALID_PARAMETER, status1) ||
340 !NT_STATUS_EQUAL(NT_STATUS_INVALID_PARAMETER, status2) ||
341 !NT_STATUS_EQUAL(NT_STATUS_INVALID_PARAMETER, status3)) {
342 DEBUG(0, ("test_setmap1: bad parameter handling!\n"));
343 return false;
346 DEBUG(0, ("test_unixid2sid1: PASSED!\n"));
348 return true;
351 static bool test_sid2unixid2(TALLOC_CTX *memctx, struct idmap_domain *dom)
353 NTSTATUS status;
354 struct id_map uid_map, gid_map, test_map;
355 bool doagain = true;
357 ZERO_STRUCT(uid_map);
358 ZERO_STRUCT(gid_map);
360 /* create two mappings for a UID and GID */
362 again:
364 uid_map.sid = dom_sid_parse_talloc(memctx, DOM_SID2 "-1000");
365 uid_map.xid.type = ID_TYPE_UID;
367 gid_map.sid = dom_sid_parse_talloc(memctx, DOM_SID2 "-1001");
368 gid_map.xid.type = ID_TYPE_GID;
370 status = idmap_tdb_common_new_mapping(dom, &uid_map);
371 if(!NT_STATUS_IS_OK(status)) {
372 DEBUG(0, ("test_sid2unixid1: could not create uid map!\n"));
373 return false;
376 status = idmap_tdb_common_new_mapping(dom, &gid_map);
377 if(!NT_STATUS_IS_OK(status)) {
378 DEBUG(0, ("test_sid2unixid1: could not create gid map!\n"));
379 return false;
382 /* now read them back */
383 ZERO_STRUCT(test_map);
384 test_map.sid = uid_map.sid;
386 status = idmap_tdb_common_sid_to_unixid(dom, &test_map);
387 if(!NT_STATUS_IS_OK(status)) {
388 DEBUG(0, ("test_sid2unixid1: sid2unixid failed for uid!\n"));
389 return false;
392 if(test_map.xid.id!=uid_map.xid.id) {
393 DEBUG(0, ("test_sid2unixid1: sid2unixid returned wrong uid!\n"));
394 return false;
397 test_map.sid = gid_map.sid;
399 status = idmap_tdb_common_sid_to_unixid(dom, &test_map);
400 if(!NT_STATUS_IS_OK(status)) {
401 DEBUG(0, ("test_sid2unixid1: sid2unixid failed for gid!\n"));
402 return false;
405 if(test_map.xid.id!=gid_map.xid.id) {
406 DEBUG(0, ("test_sid2unixid1: sid2unixid returned wrong gid!\n"));
407 return false;
411 * Go through the same tests again once to see if trying to recreate
412 * a mapping that was already created will work or not
414 if(doagain) {
415 doagain = false;
416 goto again;
419 DEBUG(0, ("test_sid2unixid1: PASSED!\n"));
421 return true;
424 static bool test_sids2unixids1(TALLOC_CTX *memctx, struct idmap_domain *dom)
426 NTSTATUS status;
427 struct id_map uid_map, gid_map, **test_maps;
429 ZERO_STRUCT(uid_map);
430 ZERO_STRUCT(gid_map);
432 /* create two mappings for a UID and GID */
434 uid_map.sid = dom_sid_parse_talloc(memctx, DOM_SID4 "-1000");
435 uid_map.xid.type = ID_TYPE_UID;
437 gid_map.sid = dom_sid_parse_talloc(memctx, DOM_SID4 "-1001");
438 gid_map.xid.type = ID_TYPE_GID;
440 status = idmap_tdb_common_new_mapping(dom, &uid_map);
441 if(!NT_STATUS_IS_OK(status)) {
442 DEBUG(0, ("test_sids2unixids1: could not create uid map!\n"));
443 return false;
446 status = idmap_tdb_common_new_mapping(dom, &gid_map);
447 if(!NT_STATUS_IS_OK(status)) {
448 DEBUG(0, ("test_sids2unixids1: could not create gid map!\n"));
449 return false;
452 /* now read them back */
453 test_maps = talloc_zero_array(memctx, struct id_map*, 3);
455 test_maps[0] = talloc(test_maps, struct id_map);
456 test_maps[1] = talloc(test_maps, struct id_map);
457 test_maps[2] = NULL;
459 test_maps[0]->sid = talloc(test_maps, struct dom_sid);
460 test_maps[1]->sid = talloc(test_maps, struct dom_sid);
461 sid_copy(test_maps[0]->sid, uid_map.sid);
462 sid_copy(test_maps[1]->sid, gid_map.sid);
464 status = idmap_tdb_common_sids_to_unixids(dom, test_maps);
465 if(!NT_STATUS_IS_OK(status)) {
466 DEBUG(0, ("test_sids2sunixids1: sids2unixids failed!\n"));
467 talloc_free(test_maps);
468 return false;
471 if(test_maps[0]->xid.id!=uid_map.xid.id ||
472 test_maps[1]->xid.id!=gid_map.xid.id ) {
473 DEBUG(0, ("test_sids2unixids1: sid2unixid returned wrong xid!\n"));
474 talloc_free(test_maps);
475 return false;
478 DEBUG(0, ("test_sids2unixids1: PASSED!\n"));
480 talloc_free(test_maps);
482 return true;
485 static bool test_sids2unixids2(TALLOC_CTX *memctx, struct idmap_domain *dom)
487 NTSTATUS status;
488 struct id_map **test_maps;
489 struct unixid save;
491 test_maps = talloc_zero_array(memctx, struct id_map*, 3);
493 test_maps[0] = talloc(test_maps, struct id_map);
494 test_maps[1] = talloc(test_maps, struct id_map);
495 test_maps[2] = NULL;
497 /* ask for two new mappings for a UID and GID */
498 test_maps[0]->sid = dom_sid_parse_talloc(test_maps, DOM_SID4 "-1003");
499 test_maps[0]->xid.type = ID_TYPE_UID;
500 test_maps[1]->sid = dom_sid_parse_talloc(test_maps, DOM_SID4 "-1004");
501 test_maps[1]->xid.type = ID_TYPE_GID;
503 status = idmap_tdb_common_sids_to_unixids(dom, test_maps);
504 if(!NT_STATUS_IS_OK(status)) {
505 DEBUG(0, ("test_sids2sunixids2: sids2unixids "
506 "failed (%s)!\n", nt_errstr(status)));
507 talloc_free(test_maps);
508 return false;
511 if(test_maps[0]->xid.id == 0 || test_maps[1]->xid.id == 0) {
512 DEBUG(0, ("test_sids2sunixids2: sids2unixids "
513 "returned zero ids!\n"));
514 talloc_free(test_maps);
515 return false;
518 save = test_maps[1]->xid;
520 /* ask for a known and a new mapping at the same time */
521 talloc_free(test_maps);
522 test_maps = talloc_zero_array(memctx, struct id_map*, 3);
523 test_maps[0] = talloc(test_maps, struct id_map);
524 test_maps[1] = talloc(test_maps, struct id_map);
525 test_maps[2] = NULL;
527 test_maps[0]->sid = dom_sid_parse_talloc(test_maps, DOM_SID4 "-1004");
528 test_maps[0]->xid.type = ID_TYPE_GID;
529 test_maps[1]->sid = dom_sid_parse_talloc(test_maps, DOM_SID4 "-1005");
530 test_maps[1]->xid.type = ID_TYPE_UID;
532 status = idmap_tdb_common_sids_to_unixids(dom, test_maps);
533 if(!NT_STATUS_IS_OK(status)) {
534 DEBUG(0, ("test_sids2sunixids2: sids2unixids (2) "
535 "failed (%s)!\n", nt_errstr(status)));
536 talloc_free(test_maps);
537 return false;
540 if(test_maps[0]->xid.type != save.type ||
541 test_maps[0]->xid.id != save.id) {
542 DEBUG(0, ("test_sids2sunixids2: second lookup returned "
543 "different value!\n"));
544 talloc_free(test_maps);
545 return false;
548 if(test_maps[1]->xid.id == 0) {
549 DEBUG(0, ("test_sids2sunixids2: sids2unixids "
550 "returned zero id for mixed mapping request!\n"));
551 talloc_free(test_maps);
552 return false;
555 DEBUG(0, ("test_sids2unixids2: PASSED!\n"));
557 talloc_free(test_maps);
559 return true;
562 static bool test_sids2unixids3(TALLOC_CTX *memctx, struct idmap_domain *dom)
564 NTSTATUS status;
565 struct id_map **test_maps;
566 bool retval = true;
569 * check the mapping states:
570 * NONE_MAPPED, SOME_UNMAPPED, OK (all mapped)
572 * use the ids created by test_sids2unixids1
573 * need to make dom read-only
576 dom->read_only = true;
578 test_maps = talloc_zero_array(memctx, struct id_map*, 3);
580 test_maps[0] = talloc(test_maps, struct id_map);
581 test_maps[1] = talloc(test_maps, struct id_map);
582 test_maps[2] = NULL;
584 /* NONE_MAPPED first */
585 test_maps[0]->sid = talloc(test_maps, struct dom_sid);
586 test_maps[1]->sid = talloc(test_maps, struct dom_sid);
587 test_maps[0]->sid = dom_sid_parse_talloc(test_maps,
588 "S-1-5-21-1-2-3-4");
589 test_maps[0]->xid.type = ID_TYPE_UID;
591 test_maps[1]->sid = dom_sid_parse_talloc(test_maps,
592 "S-1-5-21-1-2-3-5");
593 test_maps[1]->xid.type = ID_TYPE_GID;
595 status = idmap_tdb_common_sids_to_unixids(dom, test_maps);
596 if(!NT_STATUS_EQUAL(status, NT_STATUS_NONE_MAPPED)) {
597 DEBUG(0, ("test_sids2unixids3: incorrect status "
598 "(%s), expected NT_STATUS_NONE_MAPPED!\n",
599 nt_errstr(status)));
600 retval = false;
601 goto out;
604 /* SOME_UNMAPPED */
605 test_maps[0]->sid = talloc(test_maps, struct dom_sid);
606 test_maps[1]->sid = talloc(test_maps, struct dom_sid);
607 test_maps[0]->sid = dom_sid_parse_talloc(test_maps,
608 DOM_SID4 "-1000");
609 test_maps[0]->xid.type = ID_TYPE_UID;
610 test_maps[1]->sid = dom_sid_parse_talloc(test_maps,
611 "S-1-5-21-1-2-3-5");
612 test_maps[1]->xid.type = ID_TYPE_GID;
614 status = idmap_tdb_common_sids_to_unixids(dom, test_maps);
615 if(!NT_STATUS_EQUAL(status, STATUS_SOME_UNMAPPED)) {
616 DEBUG(0, ("test_sids2unixids3: incorrect status "
617 "(%s), expected STATUS_SOME_UNMAPPED!\n",
618 nt_errstr(status)));
619 retval = false;
620 goto out;
623 /* OK */
624 test_maps[0]->sid = talloc(test_maps, struct dom_sid);
625 test_maps[1]->sid = talloc(test_maps, struct dom_sid);
626 test_maps[0]->sid = dom_sid_parse_talloc(test_maps,
627 DOM_SID4 "-1001");
628 test_maps[1]->sid = dom_sid_parse_talloc(test_maps,
629 DOM_SID4 "-1000");
631 status = idmap_tdb_common_sids_to_unixids(dom, test_maps);
632 if(!NT_STATUS_IS_OK(status)) {
633 DEBUG(0, ("test_sids2unixids3: incorrect status "
634 "(%s), expected NT_STATUS_OK!\n",
635 nt_errstr(status)));
636 retval = false;
637 goto out;
640 DEBUG(0, ("test_sids2unixids3: PASSED!\n"));
642 out:
643 talloc_free(test_maps);
644 dom->read_only = false;
645 return retval;
648 static bool test_unixid2sid1(TALLOC_CTX *memctx, struct idmap_domain *dom)
650 NTSTATUS status1, status2, status3;
651 struct id_map map;
653 /* check for correct dealing with bad parameters */
654 status1 = idmap_tdb_common_unixid_to_sid(NULL, &map);
655 status2 = idmap_tdb_common_unixid_to_sid(dom, NULL);
656 status3 = idmap_tdb_common_unixid_to_sid(NULL, NULL);
658 if(!NT_STATUS_EQUAL(NT_STATUS_INVALID_PARAMETER, status1) ||
659 !NT_STATUS_EQUAL(NT_STATUS_INVALID_PARAMETER, status2) ||
660 !NT_STATUS_EQUAL(NT_STATUS_INVALID_PARAMETER, status3)) {
661 DEBUG(0, ("test_setmap1: bad parameter handling!\n"));
662 return false;
665 DEBUG(0, ("test_unixid2sid1: PASSED!\n"));
667 return true;
670 static bool test_unixid2sid2(TALLOC_CTX *memctx, struct idmap_domain *dom)
672 NTSTATUS status;
673 struct id_map *map;
674 bool retval = true;
676 /* ask for mapping that is outside of the range */
677 map = talloc(memctx, struct id_map);
678 map->sid = talloc(map, struct dom_sid);
680 map->xid.type = ID_TYPE_UID;
681 map->xid.id = HIGH_ID + 1;
683 status = idmap_tdb_common_unixid_to_sid(dom, map);
684 if(NT_STATUS_IS_OK(status)) {
685 DEBUG(0, ("test_unixid2sid2: unixid2sid returned "
686 "out-of-range result\n"));
687 retval = false;
688 goto out;
691 DEBUG(0, ("test_unixid2sid2: PASSED!\n"));
692 out:
693 talloc_free(map);
694 return retval;
698 static bool test_unixid2sid3(TALLOC_CTX *memctx, struct idmap_domain *dom)
700 NTSTATUS status;
701 struct id_map uid_map, gid_map, test_map;
702 struct dom_sid testsid;
704 ZERO_STRUCT(uid_map);
705 ZERO_STRUCT(gid_map);
707 /* create two mappings for a UID and GID */
708 uid_map.sid = dom_sid_parse_talloc(memctx, DOM_SID3 "-1000");
709 uid_map.xid.type = ID_TYPE_UID;
711 gid_map.sid = dom_sid_parse_talloc(memctx, DOM_SID3 "-1001");
712 gid_map.xid.type = ID_TYPE_GID;
714 status = idmap_tdb_common_new_mapping(dom, &uid_map);
715 if(!NT_STATUS_IS_OK(status)) {
716 DEBUG(0, ("test_unixid2sid3: could not create uid map!\n"));
717 return false;
720 status = idmap_tdb_common_new_mapping(dom, &gid_map);
721 if(!NT_STATUS_IS_OK(status)) {
722 DEBUG(0, ("test_unixid2sid3: could not create gid map!\n"));
723 return false;
726 /* now read them back */
727 ZERO_STRUCT(test_map);
728 test_map.xid.id = uid_map.xid.id;
729 test_map.xid.type = ID_TYPE_UID;
730 test_map.sid = &testsid;
732 status = idmap_tdb_common_unixid_to_sid(dom, &test_map);
733 if(!NT_STATUS_IS_OK(status)) {
734 DEBUG(0, ("test_unixid2sid3: unixid2sid failed for uid!\n"));
735 return false;
738 if(test_map.xid.type!=uid_map.xid.type) {
739 DEBUG(0, ("test_unixid2sid3: unixid2sid returned wrong type!\n"));
740 return false;
743 if(!dom_sid_equal(test_map.sid, uid_map.sid)) {
744 DEBUG(0, ("test_unixid2sid3: unixid2sid returned wrong SID!\n"));
745 return false;
748 ZERO_STRUCT(test_map);
749 test_map.xid.id = gid_map.xid.id;
750 test_map.xid.type = ID_TYPE_GID;
751 test_map.sid = &testsid;
753 status = idmap_tdb_common_unixid_to_sid(dom, &test_map);
754 if(!NT_STATUS_IS_OK(status)) {
755 DEBUG(0, ("test_unixid2sid3: unixid2sid failed for gid!\n"));
756 return false;
759 if(test_map.xid.type!=gid_map.xid.type) {
760 DEBUG(0, ("test_unixid2sid3: unixid2sid returned wrong type!\n"));
761 return false;
764 if(!dom_sid_equal(test_map.sid,gid_map.sid)) {
765 DEBUG(0, ("test_unixid2sid3: unixid2sid returned wrong SID!\n"));
766 return false;
769 DEBUG(0, ("test_unixid2sid3: PASSED!\n"));
771 return true;
774 static bool test_unixids2sids1(TALLOC_CTX *memctx, struct idmap_domain *dom)
776 NTSTATUS status;
777 struct id_map uid_map, gid_map, **test_maps;
779 ZERO_STRUCT(uid_map);
780 ZERO_STRUCT(gid_map);
782 /* create two mappings for a UID and GID */
784 uid_map.sid = dom_sid_parse_talloc(memctx, DOM_SID5 "-1000");
785 uid_map.xid.type = ID_TYPE_UID;
787 gid_map.sid = dom_sid_parse_talloc(memctx, DOM_SID5 "-1001");
788 gid_map.xid.type = ID_TYPE_GID;
790 status = idmap_tdb_common_new_mapping(dom, &uid_map);
791 if(!NT_STATUS_IS_OK(status)) {
792 DEBUG(0, ("test_unixids2sids1: could not create uid map!\n"));
793 return false;
796 status = idmap_tdb_common_new_mapping(dom, &gid_map);
797 if(!NT_STATUS_IS_OK(status)) {
798 DEBUG(0, ("test_unixids2sids1: could not create gid map!\n"));
799 return false;
802 /* now read them back */
803 test_maps = talloc_zero_array(memctx, struct id_map*, 3);
805 test_maps[0] = talloc(test_maps, struct id_map);
806 test_maps[1] = talloc(test_maps, struct id_map);
807 test_maps[2] = NULL;
809 test_maps[0]->sid = talloc(test_maps, struct dom_sid);
810 test_maps[1]->sid = talloc(test_maps, struct dom_sid);
811 test_maps[0]->xid.id = uid_map.xid.id;
812 test_maps[0]->xid.type = ID_TYPE_UID;
813 test_maps[1]->xid.id = gid_map.xid.id;
814 test_maps[1]->xid.type = ID_TYPE_GID;
816 status = idmap_tdb_common_unixids_to_sids(dom, test_maps);
817 if(!NT_STATUS_IS_OK(status)) {
818 DEBUG(0, ("test_unixids2sids1: unixids2sids failed!\n"));
819 talloc_free(test_maps);
820 return false;
823 if(!dom_sid_equal(test_maps[0]->sid, uid_map.sid) ||
824 !dom_sid_equal(test_maps[1]->sid, gid_map.sid) ) {
825 DEBUG(0, ("test_unixids2sids1: unixids2sids returned wrong sid!\n"));
826 talloc_free(test_maps);
827 return false;
830 DEBUG(0, ("test_unixids2sids1: PASSED!\n"));
832 talloc_free(test_maps);
834 return true;
837 static bool test_unixids2sids2(TALLOC_CTX *memctx, struct idmap_domain *dom)
839 NTSTATUS status;
840 struct id_map **test_maps;
841 bool retval = true;
843 test_maps = talloc_zero_array(memctx, struct id_map*, 3);
845 test_maps[0] = talloc(test_maps, struct id_map);
846 test_maps[1] = talloc(test_maps, struct id_map);
847 test_maps[2] = NULL;
849 /* ask for two unknown mappings for a UID and GID */
850 test_maps[0]->sid = talloc(test_maps, struct dom_sid);
851 test_maps[1]->sid = talloc(test_maps, struct dom_sid);
852 test_maps[0]->xid.id = HIGH_ID - 1;
853 test_maps[0]->xid.type = ID_TYPE_UID;
854 test_maps[1]->xid.id = HIGH_ID - 1;
855 test_maps[1]->xid.type = ID_TYPE_GID;
857 status = idmap_tdb_common_unixids_to_sids(dom, test_maps);
858 if(NT_STATUS_IS_OK(status)) {
859 DEBUG(0, ("test_unixids2sids2: unixids2sids succeeded "
860 "unexpectedly!\n"));
861 retval = false;
862 goto out;
865 DEBUG(0, ("test_unixids2sids2: PASSED!\n"));
867 out:
868 talloc_free(test_maps);
870 return retval;;
873 static bool test_unixids2sids3(TALLOC_CTX *memctx, struct idmap_domain *dom)
875 NTSTATUS status;
876 struct id_map uid_map, gid_map, **test_maps;
877 bool retval = true;
879 ZERO_STRUCT(uid_map);
880 ZERO_STRUCT(gid_map);
882 /* create two mappings for a UID and GID */
883 uid_map.sid = dom_sid_parse_talloc(memctx, DOM_SID6 "-1000");
884 uid_map.xid.type = ID_TYPE_UID;
886 gid_map.sid = dom_sid_parse_talloc(memctx, DOM_SID6 "-1001");
887 gid_map.xid.type = ID_TYPE_GID;
889 status = idmap_tdb_common_new_mapping(dom, &uid_map);
890 if(!NT_STATUS_IS_OK(status)) {
891 DEBUG(0, ("test_unixids2sids3: could not create uid map!\n"));
892 return false;
895 status = idmap_tdb_common_new_mapping(dom, &gid_map);
896 if(!NT_STATUS_IS_OK(status)) {
897 DEBUG(0, ("test_unixids2sids3: could not create gid map!\n"));
898 return false;
902 * check the mapping states:
903 * NONE_MAPPED, SOME_UNMAPPED, OK (all mapped)
905 test_maps = talloc_zero_array(memctx, struct id_map*, 3);
907 test_maps[0] = talloc(test_maps, struct id_map);
908 test_maps[1] = talloc(test_maps, struct id_map);
909 test_maps[2] = NULL;
911 /* NONE_MAPPED first */
912 test_maps[0]->sid = talloc(test_maps, struct dom_sid);
913 test_maps[1]->sid = talloc(test_maps, struct dom_sid);
915 test_maps[0]->xid.id = HIGH_ID - 1;
916 test_maps[0]->xid.type = ID_TYPE_UID;
918 test_maps[1]->xid.id = HIGH_ID - 1;
919 test_maps[1]->xid.type = ID_TYPE_GID;
921 status = idmap_tdb_common_unixids_to_sids(dom, test_maps);
922 if(!NT_STATUS_EQUAL(status, NT_STATUS_NONE_MAPPED)) {
923 DEBUG(0, ("test_unixids2sids3: incorrect status "
924 "(%s), expected NT_STATUS_NONE_MAPPED!\n",
925 nt_errstr(status)));
926 retval = false;
927 goto out;
930 /* SOME_UNMAPPED */
931 test_maps[0]->sid = talloc(test_maps, struct dom_sid);
932 test_maps[1]->sid = talloc(test_maps, struct dom_sid);
933 test_maps[0]->xid = uid_map.xid;
934 test_maps[1]->xid.id = HIGH_ID - 1;
935 test_maps[1]->xid.type = ID_TYPE_GID;
937 status = idmap_tdb_common_unixids_to_sids(dom, test_maps);
938 if(!NT_STATUS_EQUAL(status, STATUS_SOME_UNMAPPED)) {
939 DEBUG(0, ("test_unixids2sids3: incorrect status "
940 "(%s), expected STATUS_SOME_UNMAPPED!\n",
941 nt_errstr(status)));
942 retval = false;
943 goto out;
946 /* OK */
947 test_maps[0]->sid = talloc(test_maps, struct dom_sid);
948 test_maps[1]->sid = talloc(test_maps, struct dom_sid);
949 test_maps[0]->xid = uid_map.xid;
950 test_maps[1]->xid = gid_map.xid;
952 status = idmap_tdb_common_unixids_to_sids(dom, test_maps);
953 if(!NT_STATUS_IS_OK(status)) {
954 DEBUG(0, ("test_unixids2sids3: incorrect status "
955 "(%s), expected NT_STATUS_OK!\n",
956 nt_errstr(status)));
957 retval = false;
958 goto out;
961 DEBUG(0, ("test_unixids2sids3: PASSED!\n"));
963 out:
964 talloc_free(test_maps);
965 return retval;
968 #define CHECKRESULT(r) if(!r) {return r;}
970 bool run_idmap_tdb_common_test(int dummy)
972 bool result;
973 struct idmap_tdb_common_context *ctx;
974 struct idmap_domain *dom;
976 TALLOC_CTX *memctx = talloc_new(NULL);
977 TALLOC_CTX *stack = talloc_stackframe();
979 ctx = createcontext(memctx);
980 if(!ctx) {
981 return false;
984 dom = createdomain(memctx);
986 dom->private_data = ctx;
988 /* test a single allocation from pool (no mapping) */
989 result = test_getnewid1(memctx, dom);
990 CHECKRESULT(result);
992 /* test idmap_tdb_common_set_mapping */
993 result = test_setmap1(memctx, dom);
994 CHECKRESULT(result);
996 /* test idmap_tdb_common_sid_to_unixid */
997 result = test_sid2unixid1(memctx, dom);
998 CHECKRESULT(result);
999 result = test_sid2unixid2(memctx, dom);
1000 CHECKRESULT(result);
1002 /* test idmap_tdb_common_sids_to_unixids */
1003 result = test_sids2unixids1(memctx, dom);
1004 CHECKRESULT(result);
1005 result = test_sids2unixids2(memctx, dom);
1006 CHECKRESULT(result);
1007 result = test_sids2unixids3(memctx, dom);
1008 CHECKRESULT(result);
1010 /* test idmap_tdb_common_unixid_to_sid */
1011 result = test_unixid2sid1(memctx, dom);
1012 CHECKRESULT(result);
1013 result = test_unixid2sid2(memctx, dom);
1014 CHECKRESULT(result);
1015 result = test_unixid2sid3(memctx, dom);
1016 CHECKRESULT(result);
1018 /* test idmap_tdb_common_unixids_to_sids */
1019 result = test_unixids2sids1(memctx, dom);
1020 CHECKRESULT(result);
1021 result = test_unixids2sids2(memctx, dom);
1022 CHECKRESULT(result);
1023 result = test_unixids2sids3(memctx, dom);
1024 CHECKRESULT(result);
1026 /* test filling up the range */
1027 result = test_getnewid2(memctx, dom);
1028 CHECKRESULT(result);
1030 talloc_free(memctx);
1031 talloc_free(stack);
1033 return true;