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/>.
22 #include "system/filesys.h"
23 #include "torture/proto.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"
39 #define TESTDB "/tmp/idmap_test.tdb"
41 #define DOM_SID1 "S-1-5-21-1234-5678-9012"
42 #define DOM_SID2 "S-1-5-21-0123-5678-9012"
43 #define DOM_SID3 "S-1-5-21-0012-5678-9012"
44 #define DOM_SID4 "S-1-5-21-0001-5678-9012"
45 #define DOM_SID5 "S-1-5-21-2345-5678-9012"
46 #define DOM_SID6 "S-1-5-21-3456-5678-9012"
48 /* overwrite some winbind internal functions */
49 struct winbindd_domain *find_domain_from_name(const char *domain_name)
54 bool get_global_winbindd_state_offline(void) {
58 bool winbindd_use_idmap_cache(void) {
62 bool idmap_is_online(void)
67 NTSTATUS idmap_backends_sid_to_unixid(const char *domain, struct id_map *id)
72 NTSTATUS idmap_backends_unixid_to_sid(const char *domname, struct id_map *id)
77 static bool open_db(struct idmap_tdb_common_context *ctx)
88 ctx->db = db_open(ctx, TESTDB, 0, TDB_DEFAULT,
89 O_RDWR | O_CREAT | O_EXCL, 0600,
92 if(dbwrap_transaction_start(ctx->db) != 0) {
93 DEBUG(0, ("Failed to start transaction!\n"));
97 status = dbwrap_store_uint32(ctx->db, ctx->hwmkey_uid, LOW_ID);
98 if(!NT_STATUS_IS_OK(status)) {
99 dbwrap_transaction_cancel(ctx->db);
103 status = dbwrap_store_uint32(ctx->db, ctx->hwmkey_gid, LOW_ID);
104 if(!NT_STATUS_IS_OK(status)) {
105 dbwrap_transaction_cancel(ctx->db);
109 if(dbwrap_transaction_commit(ctx->db) != 0) {
110 DEBUG(0, ("Failed to commit transaction!\n"));
117 static struct idmap_tdb_common_context *createcontext(TALLOC_CTX *memctx)
119 struct idmap_tdb_common_context *ret;
121 ret = talloc_zero(memctx, struct idmap_tdb_common_context);
122 ret->rw_ops = talloc_zero(ret, struct idmap_rw_ops);
124 ret->max_id = HIGH_ID;
125 ret->hwmkey_uid = HWM_USER;
126 ret->hwmkey_gid = HWM_GROUP;
128 ret->rw_ops->get_new_id = idmap_tdb_common_get_new_id;
129 ret->rw_ops->set_mapping = idmap_tdb_common_set_mapping;
136 static struct idmap_domain *createdomain(TALLOC_CTX *memctx)
138 struct idmap_domain *dom;
140 dom = talloc_zero(memctx, struct idmap_domain);
142 dom->low_id = LOW_ID;
143 dom->high_id = HIGH_ID;
144 dom->read_only = false;
145 dom->methods = talloc_zero(dom, struct idmap_methods);
146 dom->methods->sids_to_unixids = idmap_tdb_common_sids_to_unixids;
147 dom->methods->unixids_to_sids = idmap_tdb_common_unixids_to_sids;
148 dom->methods->allocate_id = idmap_tdb_common_get_new_id;
153 static bool test_getnewid1(TALLOC_CTX *memctx, struct idmap_domain *dom)
158 id.type = ID_TYPE_UID;
160 status = idmap_tdb_common_get_new_id(dom, &id);
162 if(!NT_STATUS_IS_OK(status)) {
163 DEBUG(0, ("test_getnewid1: Could not allocate id!\n"));
168 DEBUG(0, ("test_getnewid1: Allocate returned "
173 if(id.id > HIGH_ID || id.id < LOW_ID) {
174 DEBUG(0, ("test_getnewid1: Allocate returned "
175 "out of range id!\n"));
179 DEBUG(0, ("test_getnewid1: PASSED!\n"));
184 static bool test_getnewid2(TALLOC_CTX *memctx, struct idmap_domain *dom)
190 id.type = ID_TYPE_UID;
192 status = idmap_tdb_common_get_new_id(dom, &id);
194 if(!NT_STATUS_IS_OK(status)) {
195 DEBUG(0, ("test_getnewid2: Could not allocate id!\n"));
200 DEBUG(0, ("test_getnewid2: Allocate returned "
205 if(id.id > HIGH_ID || id.id < LOW_ID) {
206 DEBUG(0, ("test_getnewid2: Allocate returned "
207 "out of range id!\n"));
211 /* how many ids are left? */
213 left = HIGH_ID - id.id;
215 /* consume them all */
216 for(i = 0; i<left; i++) {
218 status = idmap_tdb_common_get_new_id(dom, &id);
220 if(!NT_STATUS_IS_OK(status)) {
221 DEBUG(0, ("test_getnewid2: Allocate returned "
222 "error %s\n", nt_errstr(status)));
226 if(id.id > HIGH_ID) {
227 DEBUG(0, ("test_getnewid2: Allocate returned "
228 "out of range id (%d)!\n", id.id));
233 /* one more must fail */
234 status = idmap_tdb_common_get_new_id(dom, &id);
236 if(NT_STATUS_IS_OK(status)) {
237 DEBUG(0, ("test_getnewid2: Could allocate id (%d) from "
238 "depleted pool!\n", id.id));
242 DEBUG(0, ("test_getnewid2: PASSED!\n"));
247 static bool test_setmap1(TALLOC_CTX *memctx, struct idmap_domain *dom)
254 /* test for correct return code with invalid data */
256 status = idmap_tdb_common_set_mapping(dom, NULL);
257 if(!NT_STATUS_EQUAL(status, NT_STATUS_INVALID_PARAMETER)) {
258 DEBUG(0, ("test_setmap1: bad parameter handling!\n"));
262 status = idmap_tdb_common_set_mapping(dom, &map);
263 if(!NT_STATUS_EQUAL(status, NT_STATUS_INVALID_PARAMETER)) {
264 DEBUG(0, ("test_setmap1: bad parameter handling!\n"));
268 map.sid = dom_sid_parse_talloc(memctx, DOM_SID1 "-100");
270 map.xid.type = ID_TYPE_NOT_SPECIFIED;
273 status = idmap_tdb_common_set_mapping(dom, &map);
274 if(!NT_STATUS_EQUAL(status, NT_STATUS_INVALID_PARAMETER)) {
275 DEBUG(0, ("test_setmap1: bad parameter handling!\n"));
279 /* now the good ones */
280 map.xid.type = ID_TYPE_UID;
283 status = idmap_tdb_common_get_new_id(dom, &(map.xid));
284 if(!NT_STATUS_IS_OK(status)) {
285 DEBUG(0, ("test_setmap1: get_new_uid failed!\n"));
289 status = idmap_tdb_common_set_mapping(dom, &map);
290 if(!NT_STATUS_IS_OK(status)) {
291 DEBUG(0, ("test_setmap1: setting UID mapping failed!\n"));
295 /* try to set the same mapping again as group (must fail) */
297 map.xid.type = ID_TYPE_GID;
298 status = idmap_tdb_common_set_mapping(dom, &map);
299 if(NT_STATUS_IS_OK(status)) {
300 DEBUG(0, ("test_setmap1: could create map for "
301 "group and user!\n"));
305 /* now a group with a different SID*/
308 map.sid = dom_sid_parse_talloc(memctx, DOM_SID1 "-101");
310 status = idmap_tdb_common_get_new_id(dom, &(map.xid));
311 if(!NT_STATUS_IS_OK(status)) {
312 DEBUG(0, ("test_setmap1: get_new_gid failed!\n"));
316 status = idmap_tdb_common_set_mapping(dom, &map);
317 if(!NT_STATUS_IS_OK(status)) {
318 DEBUG(0, ("test_setmap1: setting GID mapping failed!\n"));
321 DEBUG(0, ("test_setmap1: PASSED!\n"));
326 static bool test_sid2unixid1(TALLOC_CTX *memctx, struct idmap_domain *dom)
328 NTSTATUS status1, status2, status3;
331 /* check for correct dealing with bad parameters */
332 status1 = idmap_tdb_common_sid_to_unixid(NULL, &map);
333 status2 = idmap_tdb_common_sid_to_unixid(dom, NULL);
334 status3 = idmap_tdb_common_sid_to_unixid(NULL, NULL);
336 if(!NT_STATUS_EQUAL(NT_STATUS_INVALID_PARAMETER, status1) ||
337 !NT_STATUS_EQUAL(NT_STATUS_INVALID_PARAMETER, status2) ||
338 !NT_STATUS_EQUAL(NT_STATUS_INVALID_PARAMETER, status3)) {
339 DEBUG(0, ("test_setmap1: bad parameter handling!\n"));
343 DEBUG(0, ("test_unixid2sid1: PASSED!\n"));
348 static bool test_sid2unixid2(TALLOC_CTX *memctx, struct idmap_domain *dom)
351 struct id_map uid_map, gid_map, test_map;
354 ZERO_STRUCT(uid_map);
355 ZERO_STRUCT(gid_map);
357 /* create two mappings for a UID and GID */
361 uid_map.sid = dom_sid_parse_talloc(memctx, DOM_SID2 "-1000");
362 uid_map.xid.type = ID_TYPE_UID;
364 gid_map.sid = dom_sid_parse_talloc(memctx, DOM_SID2 "-1001");
365 gid_map.xid.type = ID_TYPE_GID;
367 status = idmap_tdb_common_new_mapping(dom, &uid_map);
368 if(!NT_STATUS_IS_OK(status)) {
369 DEBUG(0, ("test_sid2unixid1: could not create uid map!\n"));
373 status = idmap_tdb_common_new_mapping(dom, &gid_map);
374 if(!NT_STATUS_IS_OK(status)) {
375 DEBUG(0, ("test_sid2unixid1: could not create gid map!\n"));
379 /* now read them back */
380 ZERO_STRUCT(test_map);
381 test_map.sid = uid_map.sid;
383 status = idmap_tdb_common_sid_to_unixid(dom, &test_map);
384 if(!NT_STATUS_IS_OK(status)) {
385 DEBUG(0, ("test_sid2unixid1: sid2unixid failed for uid!\n"));
389 if(test_map.xid.id!=uid_map.xid.id) {
390 DEBUG(0, ("test_sid2unixid1: sid2unixid returned wrong uid!\n"));
394 test_map.sid = gid_map.sid;
396 status = idmap_tdb_common_sid_to_unixid(dom, &test_map);
397 if(!NT_STATUS_IS_OK(status)) {
398 DEBUG(0, ("test_sid2unixid1: sid2unixid failed for gid!\n"));
402 if(test_map.xid.id!=gid_map.xid.id) {
403 DEBUG(0, ("test_sid2unixid1: sid2unixid returned wrong gid!\n"));
408 * Go through the same tests again once to see if trying to recreate
409 * a mapping that was already created will work or not
416 DEBUG(0, ("test_sid2unixid1: PASSED!\n"));
421 static bool test_sids2unixids1(TALLOC_CTX *memctx, struct idmap_domain *dom)
424 struct id_map uid_map, gid_map, **test_maps;
426 ZERO_STRUCT(uid_map);
427 ZERO_STRUCT(gid_map);
429 /* create two mappings for a UID and GID */
431 uid_map.sid = dom_sid_parse_talloc(memctx, DOM_SID4 "-1000");
432 uid_map.xid.type = ID_TYPE_UID;
434 gid_map.sid = dom_sid_parse_talloc(memctx, DOM_SID4 "-1001");
435 gid_map.xid.type = ID_TYPE_GID;
437 status = idmap_tdb_common_new_mapping(dom, &uid_map);
438 if(!NT_STATUS_IS_OK(status)) {
439 DEBUG(0, ("test_sids2unixids1: could not create uid map!\n"));
443 status = idmap_tdb_common_new_mapping(dom, &gid_map);
444 if(!NT_STATUS_IS_OK(status)) {
445 DEBUG(0, ("test_sids2unixids1: could not create gid map!\n"));
449 /* now read them back */
450 test_maps = talloc_zero_array(memctx, struct id_map*, 3);
452 test_maps[0] = talloc(test_maps, struct id_map);
453 test_maps[1] = talloc(test_maps, struct id_map);
456 test_maps[0]->sid = talloc(test_maps, struct dom_sid);
457 test_maps[1]->sid = talloc(test_maps, struct dom_sid);
458 sid_copy(test_maps[0]->sid, uid_map.sid);
459 sid_copy(test_maps[1]->sid, gid_map.sid);
461 status = idmap_tdb_common_sids_to_unixids(dom, test_maps);
462 if(!NT_STATUS_IS_OK(status)) {
463 DEBUG(0, ("test_sids2sunixids1: sids2unixids failed!\n"));
464 talloc_free(test_maps);
468 if(test_maps[0]->xid.id!=uid_map.xid.id ||
469 test_maps[1]->xid.id!=gid_map.xid.id ) {
470 DEBUG(0, ("test_sids2unixids1: sid2unixid returned wrong xid!\n"));
471 talloc_free(test_maps);
475 DEBUG(0, ("test_sids2unixids1: PASSED!\n"));
477 talloc_free(test_maps);
482 static bool test_sids2unixids2(TALLOC_CTX *memctx, struct idmap_domain *dom)
485 struct id_map **test_maps;
488 test_maps = talloc_zero_array(memctx, struct id_map*, 3);
490 test_maps[0] = talloc(test_maps, struct id_map);
491 test_maps[1] = talloc(test_maps, struct id_map);
494 /* ask for two new mappings for a UID and GID */
495 test_maps[0]->sid = dom_sid_parse_talloc(test_maps, DOM_SID4 "-1003");
496 test_maps[0]->xid.type = ID_TYPE_UID;
497 test_maps[1]->sid = dom_sid_parse_talloc(test_maps, DOM_SID4 "-1004");
498 test_maps[1]->xid.type = ID_TYPE_GID;
500 status = idmap_tdb_common_sids_to_unixids(dom, test_maps);
501 if(!NT_STATUS_IS_OK(status)) {
502 DEBUG(0, ("test_sids2sunixids2: sids2unixids "
503 "failed (%s)!\n", nt_errstr(status)));
504 talloc_free(test_maps);
508 if(test_maps[0]->xid.id == 0 || test_maps[1]->xid.id == 0) {
509 DEBUG(0, ("test_sids2sunixids2: sids2unixids "
510 "returned zero ids!\n"));
511 talloc_free(test_maps);
515 save = test_maps[1]->xid;
517 /* ask for a known and a new mapping at the same time */
518 talloc_free(test_maps);
519 test_maps = talloc_zero_array(memctx, struct id_map*, 3);
520 test_maps[0] = talloc(test_maps, struct id_map);
521 test_maps[1] = talloc(test_maps, struct id_map);
524 test_maps[0]->sid = dom_sid_parse_talloc(test_maps, DOM_SID4 "-1004");
525 test_maps[0]->xid.type = ID_TYPE_GID;
526 test_maps[1]->sid = dom_sid_parse_talloc(test_maps, DOM_SID4 "-1005");
527 test_maps[1]->xid.type = ID_TYPE_UID;
529 status = idmap_tdb_common_sids_to_unixids(dom, test_maps);
530 if(!NT_STATUS_IS_OK(status)) {
531 DEBUG(0, ("test_sids2sunixids2: sids2unixids (2) "
532 "failed (%s)!\n", nt_errstr(status)));
533 talloc_free(test_maps);
537 if(test_maps[0]->xid.type != save.type ||
538 test_maps[0]->xid.id != save.id) {
539 DEBUG(0, ("test_sids2sunixids2: second lookup returned "
540 "different value!\n"));
541 talloc_free(test_maps);
545 if(test_maps[1]->xid.id == 0) {
546 DEBUG(0, ("test_sids2sunixids2: sids2unixids "
547 "returned zero id for mixed mapping request!\n"));
548 talloc_free(test_maps);
552 DEBUG(0, ("test_sids2unixids2: PASSED!\n"));
554 talloc_free(test_maps);
559 static bool test_sids2unixids3(TALLOC_CTX *memctx, struct idmap_domain *dom)
562 struct id_map **test_maps;
566 * check the mapping states:
567 * NONE_MAPPED, SOME_UNMAPPED, OK (all mapped)
569 * use the ids created by test_sids2unixids1
570 * need to make dom read-only
573 dom->read_only = true;
575 test_maps = talloc_zero_array(memctx, struct id_map*, 3);
577 test_maps[0] = talloc(test_maps, struct id_map);
578 test_maps[1] = talloc(test_maps, struct id_map);
581 /* NONE_MAPPED first */
582 test_maps[0]->sid = talloc(test_maps, struct dom_sid);
583 test_maps[1]->sid = talloc(test_maps, struct dom_sid);
584 test_maps[0]->sid = dom_sid_parse_talloc(test_maps,
586 test_maps[0]->xid.type = ID_TYPE_UID;
588 test_maps[1]->sid = dom_sid_parse_talloc(test_maps,
590 test_maps[1]->xid.type = ID_TYPE_GID;
592 status = idmap_tdb_common_sids_to_unixids(dom, test_maps);
593 if(!NT_STATUS_EQUAL(status, NT_STATUS_NONE_MAPPED)) {
594 DEBUG(0, ("test_sids2unixids3: incorrect status "
595 "(%s), expected NT_STATUS_NONE_MAPPED!\n",
602 test_maps[0]->sid = talloc(test_maps, struct dom_sid);
603 test_maps[1]->sid = talloc(test_maps, struct dom_sid);
604 test_maps[0]->sid = dom_sid_parse_talloc(test_maps,
606 test_maps[0]->xid.type = ID_TYPE_UID;
607 test_maps[1]->sid = dom_sid_parse_talloc(test_maps,
609 test_maps[1]->xid.type = ID_TYPE_GID;
611 status = idmap_tdb_common_sids_to_unixids(dom, test_maps);
612 if(!NT_STATUS_EQUAL(status, STATUS_SOME_UNMAPPED)) {
613 DEBUG(0, ("test_sids2unixids3: incorrect status "
614 "(%s), expected STATUS_SOME_UNMAPPED!\n",
621 test_maps[0]->sid = talloc(test_maps, struct dom_sid);
622 test_maps[1]->sid = talloc(test_maps, struct dom_sid);
623 test_maps[0]->sid = dom_sid_parse_talloc(test_maps,
625 test_maps[1]->sid = dom_sid_parse_talloc(test_maps,
628 status = idmap_tdb_common_sids_to_unixids(dom, test_maps);
629 if(!NT_STATUS_IS_OK(status)) {
630 DEBUG(0, ("test_sids2unixids3: incorrect status "
631 "(%s), expected NT_STATUS_OK!\n",
637 DEBUG(0, ("test_sids2unixids3: PASSED!\n"));
640 talloc_free(test_maps);
641 dom->read_only = false;
645 static bool test_unixid2sid1(TALLOC_CTX *memctx, struct idmap_domain *dom)
647 NTSTATUS status1, status2, status3;
650 /* check for correct dealing with bad parameters */
651 status1 = idmap_tdb_common_unixid_to_sid(NULL, &map);
652 status2 = idmap_tdb_common_unixid_to_sid(dom, NULL);
653 status3 = idmap_tdb_common_unixid_to_sid(NULL, NULL);
655 if(!NT_STATUS_EQUAL(NT_STATUS_INVALID_PARAMETER, status1) ||
656 !NT_STATUS_EQUAL(NT_STATUS_INVALID_PARAMETER, status2) ||
657 !NT_STATUS_EQUAL(NT_STATUS_INVALID_PARAMETER, status3)) {
658 DEBUG(0, ("test_setmap1: bad parameter handling!\n"));
662 DEBUG(0, ("test_unixid2sid1: PASSED!\n"));
667 static bool test_unixid2sid2(TALLOC_CTX *memctx, struct idmap_domain *dom)
673 /* ask for mapping that is outside of the range */
674 map = talloc(memctx, struct id_map);
675 map->sid = talloc(map, struct dom_sid);
677 map->xid.type = ID_TYPE_UID;
678 map->xid.id = HIGH_ID + 1;
680 status = idmap_tdb_common_unixid_to_sid(dom, map);
681 if(NT_STATUS_IS_OK(status)) {
682 DEBUG(0, ("test_unixid2sid2: unixid2sid returned "
683 "out-of-range result\n"));
688 DEBUG(0, ("test_unixid2sid2: PASSED!\n"));
695 static bool test_unixid2sid3(TALLOC_CTX *memctx, struct idmap_domain *dom)
698 struct id_map uid_map, gid_map, test_map;
699 struct dom_sid testsid;
701 ZERO_STRUCT(uid_map);
702 ZERO_STRUCT(gid_map);
704 /* create two mappings for a UID and GID */
705 uid_map.sid = dom_sid_parse_talloc(memctx, DOM_SID3 "-1000");
706 uid_map.xid.type = ID_TYPE_UID;
708 gid_map.sid = dom_sid_parse_talloc(memctx, DOM_SID3 "-1001");
709 gid_map.xid.type = ID_TYPE_GID;
711 status = idmap_tdb_common_new_mapping(dom, &uid_map);
712 if(!NT_STATUS_IS_OK(status)) {
713 DEBUG(0, ("test_unixid2sid3: could not create uid map!\n"));
717 status = idmap_tdb_common_new_mapping(dom, &gid_map);
718 if(!NT_STATUS_IS_OK(status)) {
719 DEBUG(0, ("test_unixid2sid3: could not create gid map!\n"));
723 /* now read them back */
724 ZERO_STRUCT(test_map);
725 test_map.xid.id = uid_map.xid.id;
726 test_map.xid.type = ID_TYPE_UID;
727 test_map.sid = &testsid;
729 status = idmap_tdb_common_unixid_to_sid(dom, &test_map);
730 if(!NT_STATUS_IS_OK(status)) {
731 DEBUG(0, ("test_unixid2sid3: unixid2sid failed for uid!\n"));
735 if(test_map.xid.type!=uid_map.xid.type) {
736 DEBUG(0, ("test_unixid2sid3: unixid2sid returned wrong type!\n"));
740 if(!dom_sid_equal(test_map.sid, uid_map.sid)) {
741 DEBUG(0, ("test_unixid2sid3: unixid2sid returned wrong SID!\n"));
745 ZERO_STRUCT(test_map);
746 test_map.xid.id = gid_map.xid.id;
747 test_map.xid.type = ID_TYPE_GID;
748 test_map.sid = &testsid;
750 status = idmap_tdb_common_unixid_to_sid(dom, &test_map);
751 if(!NT_STATUS_IS_OK(status)) {
752 DEBUG(0, ("test_unixid2sid3: unixid2sid failed for gid!\n"));
756 if(test_map.xid.type!=gid_map.xid.type) {
757 DEBUG(0, ("test_unixid2sid3: unixid2sid returned wrong type!\n"));
761 if(!dom_sid_equal(test_map.sid,gid_map.sid)) {
762 DEBUG(0, ("test_unixid2sid3: unixid2sid returned wrong SID!\n"));
766 DEBUG(0, ("test_unixid2sid3: PASSED!\n"));
771 static bool test_unixids2sids1(TALLOC_CTX *memctx, struct idmap_domain *dom)
774 struct id_map uid_map, gid_map, **test_maps;
776 ZERO_STRUCT(uid_map);
777 ZERO_STRUCT(gid_map);
779 /* create two mappings for a UID and GID */
781 uid_map.sid = dom_sid_parse_talloc(memctx, DOM_SID5 "-1000");
782 uid_map.xid.type = ID_TYPE_UID;
784 gid_map.sid = dom_sid_parse_talloc(memctx, DOM_SID5 "-1001");
785 gid_map.xid.type = ID_TYPE_GID;
787 status = idmap_tdb_common_new_mapping(dom, &uid_map);
788 if(!NT_STATUS_IS_OK(status)) {
789 DEBUG(0, ("test_unixids2sids1: could not create uid map!\n"));
793 status = idmap_tdb_common_new_mapping(dom, &gid_map);
794 if(!NT_STATUS_IS_OK(status)) {
795 DEBUG(0, ("test_unixids2sids1: could not create gid map!\n"));
799 /* now read them back */
800 test_maps = talloc_zero_array(memctx, struct id_map*, 3);
802 test_maps[0] = talloc(test_maps, struct id_map);
803 test_maps[1] = talloc(test_maps, struct id_map);
806 test_maps[0]->sid = talloc(test_maps, struct dom_sid);
807 test_maps[1]->sid = talloc(test_maps, struct dom_sid);
808 test_maps[0]->xid.id = uid_map.xid.id;
809 test_maps[0]->xid.type = ID_TYPE_UID;
810 test_maps[1]->xid.id = gid_map.xid.id;
811 test_maps[1]->xid.type = ID_TYPE_GID;
813 status = idmap_tdb_common_unixids_to_sids(dom, test_maps);
814 if(!NT_STATUS_IS_OK(status)) {
815 DEBUG(0, ("test_unixids2sids1: unixids2sids failed!\n"));
816 talloc_free(test_maps);
820 if(!dom_sid_equal(test_maps[0]->sid, uid_map.sid) ||
821 !dom_sid_equal(test_maps[1]->sid, gid_map.sid) ) {
822 DEBUG(0, ("test_unixids2sids1: unixids2sids returned wrong sid!\n"));
823 talloc_free(test_maps);
827 DEBUG(0, ("test_unixids2sids1: PASSED!\n"));
829 talloc_free(test_maps);
834 static bool test_unixids2sids2(TALLOC_CTX *memctx, struct idmap_domain *dom)
837 struct id_map **test_maps;
840 test_maps = talloc_zero_array(memctx, struct id_map*, 3);
842 test_maps[0] = talloc(test_maps, struct id_map);
843 test_maps[1] = talloc(test_maps, struct id_map);
846 /* ask for two unknown mappings for a UID and GID */
847 test_maps[0]->sid = talloc(test_maps, struct dom_sid);
848 test_maps[1]->sid = talloc(test_maps, struct dom_sid);
849 test_maps[0]->xid.id = HIGH_ID - 1;
850 test_maps[0]->xid.type = ID_TYPE_UID;
851 test_maps[1]->xid.id = HIGH_ID - 1;
852 test_maps[1]->xid.type = ID_TYPE_GID;
854 status = idmap_tdb_common_unixids_to_sids(dom, test_maps);
855 if(NT_STATUS_IS_OK(status)) {
856 DEBUG(0, ("test_unixids2sids2: unixids2sids succeeded "
862 DEBUG(0, ("test_unixids2sids2: PASSED!\n"));
865 talloc_free(test_maps);
870 static bool test_unixids2sids3(TALLOC_CTX *memctx, struct idmap_domain *dom)
873 struct id_map uid_map, gid_map, **test_maps;
876 ZERO_STRUCT(uid_map);
877 ZERO_STRUCT(gid_map);
879 /* create two mappings for a UID and GID */
880 uid_map.sid = dom_sid_parse_talloc(memctx, DOM_SID6 "-1000");
881 uid_map.xid.type = ID_TYPE_UID;
883 gid_map.sid = dom_sid_parse_talloc(memctx, DOM_SID6 "-1001");
884 gid_map.xid.type = ID_TYPE_GID;
886 status = idmap_tdb_common_new_mapping(dom, &uid_map);
887 if(!NT_STATUS_IS_OK(status)) {
888 DEBUG(0, ("test_unixids2sids3: could not create uid map!\n"));
892 status = idmap_tdb_common_new_mapping(dom, &gid_map);
893 if(!NT_STATUS_IS_OK(status)) {
894 DEBUG(0, ("test_unixids2sids3: could not create gid map!\n"));
899 * check the mapping states:
900 * NONE_MAPPED, SOME_UNMAPPED, OK (all mapped)
902 test_maps = talloc_zero_array(memctx, struct id_map*, 3);
904 test_maps[0] = talloc(test_maps, struct id_map);
905 test_maps[1] = talloc(test_maps, struct id_map);
908 /* NONE_MAPPED first */
909 test_maps[0]->sid = talloc(test_maps, struct dom_sid);
910 test_maps[1]->sid = talloc(test_maps, struct dom_sid);
912 test_maps[0]->xid.id = HIGH_ID - 1;
913 test_maps[0]->xid.type = ID_TYPE_UID;
915 test_maps[1]->xid.id = HIGH_ID - 1;
916 test_maps[1]->xid.type = ID_TYPE_GID;
918 status = idmap_tdb_common_unixids_to_sids(dom, test_maps);
919 if(!NT_STATUS_EQUAL(status, NT_STATUS_NONE_MAPPED)) {
920 DEBUG(0, ("test_unixids2sids3: incorrect status "
921 "(%s), expected NT_STATUS_NONE_MAPPED!\n",
928 test_maps[0]->sid = talloc(test_maps, struct dom_sid);
929 test_maps[1]->sid = talloc(test_maps, struct dom_sid);
930 test_maps[0]->xid = uid_map.xid;
931 test_maps[1]->xid.id = HIGH_ID - 1;
932 test_maps[1]->xid.type = ID_TYPE_GID;
934 status = idmap_tdb_common_unixids_to_sids(dom, test_maps);
935 if(!NT_STATUS_EQUAL(status, STATUS_SOME_UNMAPPED)) {
936 DEBUG(0, ("test_unixids2sids3: incorrect status "
937 "(%s), expected STATUS_SOME_UNMAPPED!\n",
944 test_maps[0]->sid = talloc(test_maps, struct dom_sid);
945 test_maps[1]->sid = talloc(test_maps, struct dom_sid);
946 test_maps[0]->xid = uid_map.xid;
947 test_maps[1]->xid = gid_map.xid;
949 status = idmap_tdb_common_unixids_to_sids(dom, test_maps);
950 if(!NT_STATUS_IS_OK(status)) {
951 DEBUG(0, ("test_unixids2sids3: incorrect status "
952 "(%s), expected NT_STATUS_OK!\n",
958 DEBUG(0, ("test_unixids2sids3: PASSED!\n"));
961 talloc_free(test_maps);
965 #define CHECKRESULT(r) if(!r) {return r;}
967 bool run_idmap_tdb_common_test(int dummy)
970 struct idmap_tdb_common_context *ctx;
971 struct idmap_domain *dom;
973 TALLOC_CTX *memctx = talloc_new(NULL);
974 TALLOC_CTX *stack = talloc_stackframe();
976 ctx = createcontext(memctx);
978 dom = createdomain(memctx);
980 dom->private_data = ctx;
982 /* test a single allocation from pool (no mapping) */
983 result = test_getnewid1(memctx, dom);
986 /* test idmap_tdb_common_set_mapping */
987 result = test_setmap1(memctx, dom);
990 /* test idmap_tdb_common_sid_to_unixid */
991 result = test_sid2unixid1(memctx, dom);
993 result = test_sid2unixid2(memctx, dom);
996 /* test idmap_tdb_common_sids_to_unixids */
997 result = test_sids2unixids1(memctx, dom);
999 result = test_sids2unixids2(memctx, dom);
1000 CHECKRESULT(result);
1001 result = test_sids2unixids3(memctx, dom);
1002 CHECKRESULT(result);
1004 /* test idmap_tdb_common_unixid_to_sid */
1005 result = test_unixid2sid1(memctx, dom);
1006 CHECKRESULT(result);
1007 result = test_unixid2sid2(memctx, dom);
1008 CHECKRESULT(result);
1009 result = test_unixid2sid3(memctx, dom);
1010 CHECKRESULT(result);
1012 /* test idmap_tdb_common_unixids_to_sids */
1013 result = test_unixids2sids1(memctx, dom);
1014 CHECKRESULT(result);
1015 result = test_unixids2sids2(memctx, dom);
1016 CHECKRESULT(result);
1017 result = test_unixids2sids3(memctx, dom);
1018 CHECKRESULT(result);
1020 /* test filling up the range */
1021 result = test_getnewid2(memctx, dom);
1022 CHECKRESULT(result);
1024 talloc_free(memctx);