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 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
)
52 bool get_global_winbindd_state_offline(void) {
56 bool winbindd_use_idmap_cache(void) {
60 bool idmap_is_online(void)
65 NTSTATUS
idmap_backends_unixid_to_sid(const char *domname
, struct id_map
*id
)
70 static bool open_db(struct idmap_tdb_common_context
*ctx
)
80 db_path
= talloc_asprintf(talloc_tos(), "%s/idmap_test.tdb",
83 DEBUG(0, ("Out of memory!\n"));
87 ctx
->db
= db_open(ctx
, db_path
, 0, TDB_DEFAULT
,
88 O_RDWR
| O_CREAT
, 0600,
92 DEBUG(0, ("Failed to open database: %s\n", strerror(errno
)));
96 if(dbwrap_transaction_start(ctx
->db
) != 0) {
97 DEBUG(0, ("Failed to start transaction!\n"));
101 status
= dbwrap_store_uint32_bystring(ctx
->db
, ctx
->hwmkey_uid
,
103 if(!NT_STATUS_IS_OK(status
)) {
104 dbwrap_transaction_cancel(ctx
->db
);
108 status
= dbwrap_store_uint32_bystring(ctx
->db
, ctx
->hwmkey_gid
,
110 if(!NT_STATUS_IS_OK(status
)) {
111 dbwrap_transaction_cancel(ctx
->db
);
115 if(dbwrap_transaction_commit(ctx
->db
) != 0) {
116 DEBUG(0, ("Failed to commit transaction!\n"));
123 static struct idmap_tdb_common_context
*createcontext(TALLOC_CTX
*memctx
)
125 struct idmap_tdb_common_context
*ret
;
127 ret
= talloc_zero(memctx
, struct idmap_tdb_common_context
);
128 ret
->rw_ops
= talloc_zero(ret
, struct idmap_rw_ops
);
130 ret
->max_id
= HIGH_ID
;
131 ret
->hwmkey_uid
= HWM_USER
;
132 ret
->hwmkey_gid
= HWM_GROUP
;
134 ret
->rw_ops
->get_new_id
= idmap_tdb_common_get_new_id
;
135 ret
->rw_ops
->set_mapping
= idmap_tdb_common_set_mapping
;
144 static struct idmap_domain
*createdomain(TALLOC_CTX
*memctx
)
146 struct idmap_domain
*dom
;
148 dom
= talloc_zero(memctx
, struct idmap_domain
);
150 dom
->low_id
= LOW_ID
;
151 dom
->high_id
= HIGH_ID
;
152 dom
->read_only
= false;
153 dom
->methods
= talloc_zero(dom
, struct idmap_methods
);
154 dom
->methods
->sids_to_unixids
= idmap_tdb_common_sids_to_unixids
;
155 dom
->methods
->unixids_to_sids
= idmap_tdb_common_unixids_to_sids
;
156 dom
->methods
->allocate_id
= idmap_tdb_common_get_new_id
;
161 static bool test_getnewid1(TALLOC_CTX
*memctx
, struct idmap_domain
*dom
)
166 id
.type
= ID_TYPE_UID
;
168 status
= idmap_tdb_common_get_new_id(dom
, &id
);
170 if(!NT_STATUS_IS_OK(status
)) {
171 DEBUG(0, ("test_getnewid1: Could not allocate id!\n"));
176 DEBUG(0, ("test_getnewid1: Allocate returned "
181 if(id
.id
> HIGH_ID
|| id
.id
< LOW_ID
) {
182 DEBUG(0, ("test_getnewid1: Allocate returned "
183 "out of range id!\n"));
187 DEBUG(0, ("test_getnewid1: PASSED!\n"));
192 static bool test_getnewid2(TALLOC_CTX
*memctx
, struct idmap_domain
*dom
)
198 id
.type
= ID_TYPE_UID
;
200 status
= idmap_tdb_common_get_new_id(dom
, &id
);
202 if(!NT_STATUS_IS_OK(status
)) {
203 DEBUG(0, ("test_getnewid2: Could not allocate id!\n"));
208 DEBUG(0, ("test_getnewid2: Allocate returned "
213 if(id
.id
> HIGH_ID
|| id
.id
< LOW_ID
) {
214 DEBUG(0, ("test_getnewid2: Allocate returned "
215 "out of range id!\n"));
219 /* how many ids are left? */
221 left
= HIGH_ID
- id
.id
;
223 /* consume them all */
224 for(i
= 0; i
<left
; i
++) {
226 status
= idmap_tdb_common_get_new_id(dom
, &id
);
228 if(!NT_STATUS_IS_OK(status
)) {
229 DEBUG(0, ("test_getnewid2: Allocate returned "
230 "error %s\n", nt_errstr(status
)));
234 if(id
.id
> HIGH_ID
) {
235 DEBUG(0, ("test_getnewid2: Allocate returned "
236 "out of range id (%d)!\n", id
.id
));
241 /* one more must fail */
242 status
= idmap_tdb_common_get_new_id(dom
, &id
);
244 if(NT_STATUS_IS_OK(status
)) {
245 DEBUG(0, ("test_getnewid2: Could allocate id (%d) from "
246 "depleted pool!\n", id
.id
));
250 DEBUG(0, ("test_getnewid2: PASSED!\n"));
255 static bool test_setmap1(TALLOC_CTX
*memctx
, struct idmap_domain
*dom
)
262 /* test for correct return code with invalid data */
264 status
= idmap_tdb_common_set_mapping(dom
, NULL
);
265 if(!NT_STATUS_EQUAL(status
, NT_STATUS_INVALID_PARAMETER
)) {
266 DEBUG(0, ("test_setmap1: bad parameter handling!\n"));
270 status
= idmap_tdb_common_set_mapping(dom
, &map
);
271 if(!NT_STATUS_EQUAL(status
, NT_STATUS_INVALID_PARAMETER
)) {
272 DEBUG(0, ("test_setmap1: bad parameter handling!\n"));
276 map
.sid
= dom_sid_parse_talloc(memctx
, DOM_SID1
"-100");
278 map
.xid
.type
= ID_TYPE_NOT_SPECIFIED
;
281 status
= idmap_tdb_common_set_mapping(dom
, &map
);
282 if(!NT_STATUS_EQUAL(status
, NT_STATUS_INVALID_PARAMETER
)) {
283 DEBUG(0, ("test_setmap1: bad parameter handling!\n"));
287 /* now the good ones */
288 map
.xid
.type
= ID_TYPE_UID
;
291 status
= idmap_tdb_common_get_new_id(dom
, &(map
.xid
));
292 if(!NT_STATUS_IS_OK(status
)) {
293 DEBUG(0, ("test_setmap1: get_new_uid failed!\n"));
297 status
= idmap_tdb_common_set_mapping(dom
, &map
);
298 if(!NT_STATUS_IS_OK(status
)) {
299 DEBUG(0, ("test_setmap1: setting UID mapping failed!\n"));
303 /* try to set the same mapping again as group (must fail) */
305 map
.xid
.type
= ID_TYPE_GID
;
306 status
= idmap_tdb_common_set_mapping(dom
, &map
);
307 if(NT_STATUS_IS_OK(status
)) {
308 DEBUG(0, ("test_setmap1: could create map for "
309 "group and user!\n"));
313 /* now a group with a different SID*/
316 map
.sid
= dom_sid_parse_talloc(memctx
, DOM_SID1
"-101");
318 status
= idmap_tdb_common_get_new_id(dom
, &(map
.xid
));
319 if(!NT_STATUS_IS_OK(status
)) {
320 DEBUG(0, ("test_setmap1: get_new_gid failed!\n"));
324 status
= idmap_tdb_common_set_mapping(dom
, &map
);
325 if(!NT_STATUS_IS_OK(status
)) {
326 DEBUG(0, ("test_setmap1: setting GID mapping failed!\n"));
329 DEBUG(0, ("test_setmap1: PASSED!\n"));
334 static bool test_sid2unixid1(TALLOC_CTX
*memctx
, struct idmap_domain
*dom
)
336 NTSTATUS status1
, status2
, status3
;
339 /* check for correct dealing with bad parameters */
340 status1
= idmap_tdb_common_sid_to_unixid(NULL
, &map
);
341 status2
= idmap_tdb_common_sid_to_unixid(dom
, NULL
);
342 status3
= idmap_tdb_common_sid_to_unixid(NULL
, NULL
);
344 if(!NT_STATUS_EQUAL(NT_STATUS_INVALID_PARAMETER
, status1
) ||
345 !NT_STATUS_EQUAL(NT_STATUS_INVALID_PARAMETER
, status2
) ||
346 !NT_STATUS_EQUAL(NT_STATUS_INVALID_PARAMETER
, status3
)) {
347 DEBUG(0, ("test_setmap1: bad parameter handling!\n"));
351 DEBUG(0, ("test_unixid2sid1: PASSED!\n"));
356 static bool test_sid2unixid2(TALLOC_CTX
*memctx
, struct idmap_domain
*dom
)
359 struct id_map uid_map
, gid_map
, test_map
;
362 ZERO_STRUCT(uid_map
);
363 ZERO_STRUCT(gid_map
);
365 /* create two mappings for a UID and GID */
369 uid_map
.sid
= dom_sid_parse_talloc(memctx
, DOM_SID2
"-1000");
370 uid_map
.xid
.type
= ID_TYPE_UID
;
372 gid_map
.sid
= dom_sid_parse_talloc(memctx
, DOM_SID2
"-1001");
373 gid_map
.xid
.type
= ID_TYPE_GID
;
375 status
= idmap_tdb_common_new_mapping(dom
, &uid_map
);
376 if(!NT_STATUS_IS_OK(status
)) {
377 DEBUG(0, ("test_sid2unixid1: could not create uid map!\n"));
381 status
= idmap_tdb_common_new_mapping(dom
, &gid_map
);
382 if(!NT_STATUS_IS_OK(status
)) {
383 DEBUG(0, ("test_sid2unixid1: could not create gid map!\n"));
387 /* now read them back */
388 ZERO_STRUCT(test_map
);
389 test_map
.sid
= uid_map
.sid
;
391 status
= idmap_tdb_common_sid_to_unixid(dom
, &test_map
);
392 if(!NT_STATUS_IS_OK(status
)) {
393 DEBUG(0, ("test_sid2unixid1: sid2unixid failed for uid!\n"));
397 if(test_map
.xid
.id
!=uid_map
.xid
.id
) {
398 DEBUG(0, ("test_sid2unixid1: sid2unixid returned wrong uid!\n"));
402 test_map
.sid
= gid_map
.sid
;
404 status
= idmap_tdb_common_sid_to_unixid(dom
, &test_map
);
405 if(!NT_STATUS_IS_OK(status
)) {
406 DEBUG(0, ("test_sid2unixid1: sid2unixid failed for gid!\n"));
410 if(test_map
.xid
.id
!=gid_map
.xid
.id
) {
411 DEBUG(0, ("test_sid2unixid1: sid2unixid returned wrong gid!\n"));
416 * Go through the same tests again once to see if trying to recreate
417 * a mapping that was already created will work or not
424 DEBUG(0, ("test_sid2unixid1: PASSED!\n"));
429 static bool test_sids2unixids1(TALLOC_CTX
*memctx
, struct idmap_domain
*dom
)
432 struct id_map uid_map
, gid_map
, **test_maps
;
434 ZERO_STRUCT(uid_map
);
435 ZERO_STRUCT(gid_map
);
437 /* create two mappings for a UID and GID */
439 uid_map
.sid
= dom_sid_parse_talloc(memctx
, DOM_SID4
"-1000");
440 uid_map
.xid
.type
= ID_TYPE_UID
;
442 gid_map
.sid
= dom_sid_parse_talloc(memctx
, DOM_SID4
"-1001");
443 gid_map
.xid
.type
= ID_TYPE_GID
;
445 status
= idmap_tdb_common_new_mapping(dom
, &uid_map
);
446 if(!NT_STATUS_IS_OK(status
)) {
447 DEBUG(0, ("test_sids2unixids1: could not create uid map!\n"));
451 status
= idmap_tdb_common_new_mapping(dom
, &gid_map
);
452 if(!NT_STATUS_IS_OK(status
)) {
453 DEBUG(0, ("test_sids2unixids1: could not create gid map!\n"));
457 /* now read them back */
458 test_maps
= talloc_zero_array(memctx
, struct id_map
*, 3);
460 test_maps
[0] = talloc(test_maps
, struct id_map
);
461 test_maps
[1] = talloc(test_maps
, struct id_map
);
464 test_maps
[0]->sid
= talloc(test_maps
, struct dom_sid
);
465 test_maps
[1]->sid
= talloc(test_maps
, struct dom_sid
);
466 sid_copy(test_maps
[0]->sid
, uid_map
.sid
);
467 sid_copy(test_maps
[1]->sid
, gid_map
.sid
);
469 status
= idmap_tdb_common_sids_to_unixids(dom
, test_maps
);
470 if(!NT_STATUS_IS_OK(status
)) {
471 DEBUG(0, ("test_sids2sunixids1: sids2unixids failed!\n"));
472 talloc_free(test_maps
);
476 if(test_maps
[0]->xid
.id
!=uid_map
.xid
.id
||
477 test_maps
[1]->xid
.id
!=gid_map
.xid
.id
) {
478 DEBUG(0, ("test_sids2unixids1: sid2unixid returned wrong xid!\n"));
479 talloc_free(test_maps
);
483 DEBUG(0, ("test_sids2unixids1: PASSED!\n"));
485 talloc_free(test_maps
);
490 static bool test_sids2unixids2(TALLOC_CTX
*memctx
, struct idmap_domain
*dom
)
493 struct id_map
**test_maps
;
496 test_maps
= talloc_zero_array(memctx
, struct id_map
*, 3);
498 test_maps
[0] = talloc(test_maps
, struct id_map
);
499 test_maps
[1] = talloc(test_maps
, struct id_map
);
502 /* ask for two new mappings for a UID and GID */
503 test_maps
[0]->sid
= dom_sid_parse_talloc(test_maps
, DOM_SID4
"-1003");
504 test_maps
[0]->xid
.type
= ID_TYPE_UID
;
505 test_maps
[1]->sid
= dom_sid_parse_talloc(test_maps
, DOM_SID4
"-1004");
506 test_maps
[1]->xid
.type
= ID_TYPE_GID
;
508 status
= idmap_tdb_common_sids_to_unixids(dom
, test_maps
);
509 if(!NT_STATUS_IS_OK(status
)) {
510 DEBUG(0, ("test_sids2sunixids2: sids2unixids "
511 "failed (%s)!\n", nt_errstr(status
)));
512 talloc_free(test_maps
);
516 if(test_maps
[0]->xid
.id
== 0 || test_maps
[1]->xid
.id
== 0) {
517 DEBUG(0, ("test_sids2sunixids2: sids2unixids "
518 "returned zero ids!\n"));
519 talloc_free(test_maps
);
523 save
= test_maps
[1]->xid
;
525 /* ask for a known and a new mapping at the same time */
526 talloc_free(test_maps
);
527 test_maps
= talloc_zero_array(memctx
, struct id_map
*, 3);
528 test_maps
[0] = talloc(test_maps
, struct id_map
);
529 test_maps
[1] = talloc(test_maps
, struct id_map
);
532 test_maps
[0]->sid
= dom_sid_parse_talloc(test_maps
, DOM_SID4
"-1004");
533 test_maps
[0]->xid
.type
= ID_TYPE_GID
;
534 test_maps
[1]->sid
= dom_sid_parse_talloc(test_maps
, DOM_SID4
"-1005");
535 test_maps
[1]->xid
.type
= ID_TYPE_UID
;
537 status
= idmap_tdb_common_sids_to_unixids(dom
, test_maps
);
538 if(!NT_STATUS_IS_OK(status
)) {
539 DEBUG(0, ("test_sids2sunixids2: sids2unixids (2) "
540 "failed (%s)!\n", nt_errstr(status
)));
541 talloc_free(test_maps
);
545 if(test_maps
[0]->xid
.type
!= save
.type
||
546 test_maps
[0]->xid
.id
!= save
.id
) {
547 DEBUG(0, ("test_sids2sunixids2: second lookup returned "
548 "different value!\n"));
549 talloc_free(test_maps
);
553 if(test_maps
[1]->xid
.id
== 0) {
554 DEBUG(0, ("test_sids2sunixids2: sids2unixids "
555 "returned zero id for mixed mapping request!\n"));
556 talloc_free(test_maps
);
560 DEBUG(0, ("test_sids2unixids2: PASSED!\n"));
562 talloc_free(test_maps
);
567 static bool test_sids2unixids3(TALLOC_CTX
*memctx
, struct idmap_domain
*dom
)
570 struct id_map
**test_maps
;
574 * check the mapping states:
575 * NONE_MAPPED, SOME_UNMAPPED, OK (all mapped)
577 * use the ids created by test_sids2unixids1
578 * need to make dom read-only
581 dom
->read_only
= true;
583 test_maps
= talloc_zero_array(memctx
, struct id_map
*, 3);
585 test_maps
[0] = talloc(test_maps
, struct id_map
);
586 test_maps
[1] = talloc(test_maps
, struct id_map
);
589 /* NONE_MAPPED first */
590 test_maps
[0]->sid
= talloc(test_maps
, struct dom_sid
);
591 test_maps
[1]->sid
= talloc(test_maps
, struct dom_sid
);
592 test_maps
[0]->sid
= dom_sid_parse_talloc(test_maps
,
594 test_maps
[0]->xid
.type
= ID_TYPE_UID
;
596 test_maps
[1]->sid
= dom_sid_parse_talloc(test_maps
,
598 test_maps
[1]->xid
.type
= ID_TYPE_GID
;
600 status
= idmap_tdb_common_sids_to_unixids(dom
, test_maps
);
601 if(!NT_STATUS_EQUAL(status
, NT_STATUS_NONE_MAPPED
)) {
602 DEBUG(0, ("test_sids2unixids3: incorrect status "
603 "(%s), expected NT_STATUS_NONE_MAPPED!\n",
610 test_maps
[0]->sid
= talloc(test_maps
, struct dom_sid
);
611 test_maps
[1]->sid
= talloc(test_maps
, struct dom_sid
);
612 test_maps
[0]->sid
= dom_sid_parse_talloc(test_maps
,
614 test_maps
[0]->xid
.type
= ID_TYPE_UID
;
615 test_maps
[1]->sid
= dom_sid_parse_talloc(test_maps
,
617 test_maps
[1]->xid
.type
= ID_TYPE_GID
;
619 status
= idmap_tdb_common_sids_to_unixids(dom
, test_maps
);
620 if(!NT_STATUS_EQUAL(status
, STATUS_SOME_UNMAPPED
)) {
621 DEBUG(0, ("test_sids2unixids3: incorrect status "
622 "(%s), expected STATUS_SOME_UNMAPPED!\n",
629 test_maps
[0]->sid
= talloc(test_maps
, struct dom_sid
);
630 test_maps
[1]->sid
= talloc(test_maps
, struct dom_sid
);
631 test_maps
[0]->sid
= dom_sid_parse_talloc(test_maps
,
633 test_maps
[1]->sid
= dom_sid_parse_talloc(test_maps
,
636 status
= idmap_tdb_common_sids_to_unixids(dom
, test_maps
);
637 if(!NT_STATUS_IS_OK(status
)) {
638 DEBUG(0, ("test_sids2unixids3: incorrect status "
639 "(%s), expected NT_STATUS_OK!\n",
645 DEBUG(0, ("test_sids2unixids3: PASSED!\n"));
648 talloc_free(test_maps
);
649 dom
->read_only
= false;
653 static bool test_unixid2sid1(TALLOC_CTX
*memctx
, struct idmap_domain
*dom
)
655 NTSTATUS status1
, status2
, status3
;
658 /* check for correct dealing with bad parameters */
659 status1
= idmap_tdb_common_unixid_to_sid(NULL
, &map
);
660 status2
= idmap_tdb_common_unixid_to_sid(dom
, NULL
);
661 status3
= idmap_tdb_common_unixid_to_sid(NULL
, NULL
);
663 if(!NT_STATUS_EQUAL(NT_STATUS_INVALID_PARAMETER
, status1
) ||
664 !NT_STATUS_EQUAL(NT_STATUS_INVALID_PARAMETER
, status2
) ||
665 !NT_STATUS_EQUAL(NT_STATUS_INVALID_PARAMETER
, status3
)) {
666 DEBUG(0, ("test_setmap1: bad parameter handling!\n"));
670 DEBUG(0, ("test_unixid2sid1: PASSED!\n"));
675 static bool test_unixid2sid2(TALLOC_CTX
*memctx
, struct idmap_domain
*dom
)
681 /* ask for mapping that is outside of the range */
682 map
= talloc(memctx
, struct id_map
);
683 map
->sid
= talloc(map
, struct dom_sid
);
685 map
->xid
.type
= ID_TYPE_UID
;
686 map
->xid
.id
= HIGH_ID
+ 1;
688 status
= idmap_tdb_common_unixid_to_sid(dom
, map
);
689 if(NT_STATUS_IS_OK(status
)) {
690 DEBUG(0, ("test_unixid2sid2: unixid2sid returned "
691 "out-of-range result\n"));
696 DEBUG(0, ("test_unixid2sid2: PASSED!\n"));
703 static bool test_unixid2sid3(TALLOC_CTX
*memctx
, struct idmap_domain
*dom
)
706 struct id_map uid_map
, gid_map
, test_map
;
707 struct dom_sid testsid
;
709 ZERO_STRUCT(uid_map
);
710 ZERO_STRUCT(gid_map
);
712 /* create two mappings for a UID and GID */
713 uid_map
.sid
= dom_sid_parse_talloc(memctx
, DOM_SID3
"-1000");
714 uid_map
.xid
.type
= ID_TYPE_UID
;
716 gid_map
.sid
= dom_sid_parse_talloc(memctx
, DOM_SID3
"-1001");
717 gid_map
.xid
.type
= ID_TYPE_GID
;
719 status
= idmap_tdb_common_new_mapping(dom
, &uid_map
);
720 if(!NT_STATUS_IS_OK(status
)) {
721 DEBUG(0, ("test_unixid2sid3: could not create uid map!\n"));
725 status
= idmap_tdb_common_new_mapping(dom
, &gid_map
);
726 if(!NT_STATUS_IS_OK(status
)) {
727 DEBUG(0, ("test_unixid2sid3: could not create gid map!\n"));
731 /* now read them back */
732 ZERO_STRUCT(test_map
);
733 test_map
.xid
.id
= uid_map
.xid
.id
;
734 test_map
.xid
.type
= ID_TYPE_UID
;
735 test_map
.sid
= &testsid
;
737 status
= idmap_tdb_common_unixid_to_sid(dom
, &test_map
);
738 if(!NT_STATUS_IS_OK(status
)) {
739 DEBUG(0, ("test_unixid2sid3: unixid2sid failed for uid!\n"));
743 if(test_map
.xid
.type
!=uid_map
.xid
.type
) {
744 DEBUG(0, ("test_unixid2sid3: unixid2sid returned wrong type!\n"));
748 if(!dom_sid_equal(test_map
.sid
, uid_map
.sid
)) {
749 DEBUG(0, ("test_unixid2sid3: unixid2sid returned wrong SID!\n"));
753 ZERO_STRUCT(test_map
);
754 test_map
.xid
.id
= gid_map
.xid
.id
;
755 test_map
.xid
.type
= ID_TYPE_GID
;
756 test_map
.sid
= &testsid
;
758 status
= idmap_tdb_common_unixid_to_sid(dom
, &test_map
);
759 if(!NT_STATUS_IS_OK(status
)) {
760 DEBUG(0, ("test_unixid2sid3: unixid2sid failed for gid!\n"));
764 if(test_map
.xid
.type
!=gid_map
.xid
.type
) {
765 DEBUG(0, ("test_unixid2sid3: unixid2sid returned wrong type!\n"));
769 if(!dom_sid_equal(test_map
.sid
,gid_map
.sid
)) {
770 DEBUG(0, ("test_unixid2sid3: unixid2sid returned wrong SID!\n"));
774 DEBUG(0, ("test_unixid2sid3: PASSED!\n"));
779 static bool test_unixids2sids1(TALLOC_CTX
*memctx
, struct idmap_domain
*dom
)
782 struct id_map uid_map
, gid_map
, **test_maps
;
784 ZERO_STRUCT(uid_map
);
785 ZERO_STRUCT(gid_map
);
787 /* create two mappings for a UID and GID */
789 uid_map
.sid
= dom_sid_parse_talloc(memctx
, DOM_SID5
"-1000");
790 uid_map
.xid
.type
= ID_TYPE_UID
;
792 gid_map
.sid
= dom_sid_parse_talloc(memctx
, DOM_SID5
"-1001");
793 gid_map
.xid
.type
= ID_TYPE_GID
;
795 status
= idmap_tdb_common_new_mapping(dom
, &uid_map
);
796 if(!NT_STATUS_IS_OK(status
)) {
797 DEBUG(0, ("test_unixids2sids1: could not create uid map!\n"));
801 status
= idmap_tdb_common_new_mapping(dom
, &gid_map
);
802 if(!NT_STATUS_IS_OK(status
)) {
803 DEBUG(0, ("test_unixids2sids1: could not create gid map!\n"));
807 /* now read them back */
808 test_maps
= talloc_zero_array(memctx
, struct id_map
*, 3);
810 test_maps
[0] = talloc(test_maps
, struct id_map
);
811 test_maps
[1] = talloc(test_maps
, struct id_map
);
814 test_maps
[0]->sid
= talloc(test_maps
, struct dom_sid
);
815 test_maps
[1]->sid
= talloc(test_maps
, struct dom_sid
);
816 test_maps
[0]->xid
.id
= uid_map
.xid
.id
;
817 test_maps
[0]->xid
.type
= ID_TYPE_UID
;
818 test_maps
[1]->xid
.id
= gid_map
.xid
.id
;
819 test_maps
[1]->xid
.type
= ID_TYPE_GID
;
821 status
= idmap_tdb_common_unixids_to_sids(dom
, test_maps
);
822 if(!NT_STATUS_IS_OK(status
)) {
823 DEBUG(0, ("test_unixids2sids1: unixids2sids failed!\n"));
824 talloc_free(test_maps
);
828 if(!dom_sid_equal(test_maps
[0]->sid
, uid_map
.sid
) ||
829 !dom_sid_equal(test_maps
[1]->sid
, gid_map
.sid
) ) {
830 DEBUG(0, ("test_unixids2sids1: unixids2sids returned wrong sid!\n"));
831 talloc_free(test_maps
);
835 DEBUG(0, ("test_unixids2sids1: PASSED!\n"));
837 talloc_free(test_maps
);
842 static bool test_unixids2sids2(TALLOC_CTX
*memctx
, struct idmap_domain
*dom
)
845 struct id_map
**test_maps
;
848 test_maps
= talloc_zero_array(memctx
, struct id_map
*, 3);
850 test_maps
[0] = talloc(test_maps
, struct id_map
);
851 test_maps
[1] = talloc(test_maps
, struct id_map
);
854 /* ask for two unknown mappings for a UID and GID */
855 test_maps
[0]->sid
= talloc(test_maps
, struct dom_sid
);
856 test_maps
[1]->sid
= talloc(test_maps
, struct dom_sid
);
857 test_maps
[0]->xid
.id
= HIGH_ID
- 1;
858 test_maps
[0]->xid
.type
= ID_TYPE_UID
;
859 test_maps
[1]->xid
.id
= HIGH_ID
- 1;
860 test_maps
[1]->xid
.type
= ID_TYPE_GID
;
862 status
= idmap_tdb_common_unixids_to_sids(dom
, test_maps
);
863 if(NT_STATUS_IS_OK(status
)) {
864 DEBUG(0, ("test_unixids2sids2: unixids2sids succeeded "
870 DEBUG(0, ("test_unixids2sids2: PASSED!\n"));
873 talloc_free(test_maps
);
878 static bool test_unixids2sids3(TALLOC_CTX
*memctx
, struct idmap_domain
*dom
)
881 struct id_map uid_map
, gid_map
, **test_maps
;
884 ZERO_STRUCT(uid_map
);
885 ZERO_STRUCT(gid_map
);
887 /* create two mappings for a UID and GID */
888 uid_map
.sid
= dom_sid_parse_talloc(memctx
, DOM_SID6
"-1000");
889 uid_map
.xid
.type
= ID_TYPE_UID
;
891 gid_map
.sid
= dom_sid_parse_talloc(memctx
, DOM_SID6
"-1001");
892 gid_map
.xid
.type
= ID_TYPE_GID
;
894 status
= idmap_tdb_common_new_mapping(dom
, &uid_map
);
895 if(!NT_STATUS_IS_OK(status
)) {
896 DEBUG(0, ("test_unixids2sids3: could not create uid map!\n"));
900 status
= idmap_tdb_common_new_mapping(dom
, &gid_map
);
901 if(!NT_STATUS_IS_OK(status
)) {
902 DEBUG(0, ("test_unixids2sids3: could not create gid map!\n"));
907 * check the mapping states:
908 * NONE_MAPPED, SOME_UNMAPPED, OK (all mapped)
910 test_maps
= talloc_zero_array(memctx
, struct id_map
*, 3);
912 test_maps
[0] = talloc(test_maps
, struct id_map
);
913 test_maps
[1] = talloc(test_maps
, struct id_map
);
916 /* NONE_MAPPED first */
917 test_maps
[0]->sid
= talloc(test_maps
, struct dom_sid
);
918 test_maps
[1]->sid
= talloc(test_maps
, struct dom_sid
);
920 test_maps
[0]->xid
.id
= HIGH_ID
- 1;
921 test_maps
[0]->xid
.type
= ID_TYPE_UID
;
923 test_maps
[1]->xid
.id
= HIGH_ID
- 1;
924 test_maps
[1]->xid
.type
= ID_TYPE_GID
;
926 status
= idmap_tdb_common_unixids_to_sids(dom
, test_maps
);
927 if(!NT_STATUS_EQUAL(status
, NT_STATUS_NONE_MAPPED
)) {
928 DEBUG(0, ("test_unixids2sids3: incorrect status "
929 "(%s), expected NT_STATUS_NONE_MAPPED!\n",
936 test_maps
[0]->sid
= talloc(test_maps
, struct dom_sid
);
937 test_maps
[1]->sid
= talloc(test_maps
, struct dom_sid
);
938 test_maps
[0]->xid
= uid_map
.xid
;
939 test_maps
[1]->xid
.id
= HIGH_ID
- 1;
940 test_maps
[1]->xid
.type
= ID_TYPE_GID
;
942 status
= idmap_tdb_common_unixids_to_sids(dom
, test_maps
);
943 if(!NT_STATUS_EQUAL(status
, STATUS_SOME_UNMAPPED
)) {
944 DEBUG(0, ("test_unixids2sids3: incorrect status "
945 "(%s), expected STATUS_SOME_UNMAPPED!\n",
952 test_maps
[0]->sid
= talloc(test_maps
, struct dom_sid
);
953 test_maps
[1]->sid
= talloc(test_maps
, struct dom_sid
);
954 test_maps
[0]->xid
= uid_map
.xid
;
955 test_maps
[1]->xid
= gid_map
.xid
;
957 status
= idmap_tdb_common_unixids_to_sids(dom
, test_maps
);
958 if(!NT_STATUS_IS_OK(status
)) {
959 DEBUG(0, ("test_unixids2sids3: incorrect status "
960 "(%s), expected NT_STATUS_OK!\n",
966 DEBUG(0, ("test_unixids2sids3: PASSED!\n"));
969 talloc_free(test_maps
);
973 #define CHECKRESULT(r) if(!r) {return r;}
975 bool run_idmap_tdb_common_test(int dummy
)
978 struct idmap_tdb_common_context
*ctx
;
979 struct idmap_domain
*dom
;
981 TALLOC_CTX
*memctx
= talloc_new(NULL
);
982 TALLOC_CTX
*stack
= talloc_stackframe();
984 ctx
= createcontext(memctx
);
989 dom
= createdomain(memctx
);
991 dom
->private_data
= ctx
;
993 /* test a single allocation from pool (no mapping) */
994 result
= test_getnewid1(memctx
, dom
);
997 /* test idmap_tdb_common_set_mapping */
998 result
= test_setmap1(memctx
, dom
);
1001 /* test idmap_tdb_common_sid_to_unixid */
1002 result
= test_sid2unixid1(memctx
, dom
);
1003 CHECKRESULT(result
);
1004 result
= test_sid2unixid2(memctx
, dom
);
1005 CHECKRESULT(result
);
1007 /* test idmap_tdb_common_sids_to_unixids */
1008 result
= test_sids2unixids1(memctx
, dom
);
1009 CHECKRESULT(result
);
1010 result
= test_sids2unixids2(memctx
, dom
);
1011 CHECKRESULT(result
);
1012 result
= test_sids2unixids3(memctx
, dom
);
1013 CHECKRESULT(result
);
1015 /* test idmap_tdb_common_unixid_to_sid */
1016 result
= test_unixid2sid1(memctx
, dom
);
1017 CHECKRESULT(result
);
1018 result
= test_unixid2sid2(memctx
, dom
);
1019 CHECKRESULT(result
);
1020 result
= test_unixid2sid3(memctx
, dom
);
1021 CHECKRESULT(result
);
1023 /* test idmap_tdb_common_unixids_to_sids */
1024 result
= test_unixids2sids1(memctx
, dom
);
1025 CHECKRESULT(result
);
1026 result
= test_unixids2sids2(memctx
, dom
);
1027 CHECKRESULT(result
);
1028 result
= test_unixids2sids3(memctx
, dom
);
1029 CHECKRESULT(result
);
1031 /* test filling up the range */
1032 result
= test_getnewid2(memctx
, dom
);
1033 CHECKRESULT(result
);
1035 talloc_free(memctx
);