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 static bool open_db(struct idmap_tdb_common_context
*ctx
)
75 db_path
= talloc_asprintf(talloc_tos(), "%s/idmap_test.tdb",
78 DEBUG(0, ("Out of memory!\n"));
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
);
87 DEBUG(0, ("Failed to open database: %s\n", strerror(errno
)));
91 if(dbwrap_transaction_start(ctx
->db
) != 0) {
92 DEBUG(0, ("Failed to start transaction!\n"));
96 status
= dbwrap_store_uint32_bystring(ctx
->db
, ctx
->hwmkey_uid
,
98 if(!NT_STATUS_IS_OK(status
)) {
99 dbwrap_transaction_cancel(ctx
->db
);
103 status
= dbwrap_store_uint32_bystring(ctx
->db
, ctx
->hwmkey_gid
,
105 if(!NT_STATUS_IS_OK(status
)) {
106 dbwrap_transaction_cancel(ctx
->db
);
110 if(dbwrap_transaction_commit(ctx
->db
) != 0) {
111 DEBUG(0, ("Failed to commit transaction!\n"));
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
;
139 static struct idmap_domain
*createdomain(TALLOC_CTX
*memctx
)
141 struct idmap_domain
*dom
;
143 dom
= talloc_zero(memctx
, struct idmap_domain
);
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
;
156 static bool test_getnewid1(TALLOC_CTX
*memctx
, struct idmap_domain
*dom
)
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"));
171 DEBUG(0, ("test_getnewid1: Allocate returned "
176 if(id
.id
> HIGH_ID
|| id
.id
< LOW_ID
) {
177 DEBUG(0, ("test_getnewid1: Allocate returned "
178 "out of range id!\n"));
182 DEBUG(0, ("test_getnewid1: PASSED!\n"));
187 static bool test_getnewid2(TALLOC_CTX
*memctx
, struct idmap_domain
*dom
)
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"));
203 DEBUG(0, ("test_getnewid2: Allocate returned "
208 if(id
.id
> HIGH_ID
|| id
.id
< LOW_ID
) {
209 DEBUG(0, ("test_getnewid2: Allocate returned "
210 "out of range id!\n"));
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
)));
229 if(id
.id
> HIGH_ID
) {
230 DEBUG(0, ("test_getnewid2: Allocate returned "
231 "out of range id (%d)!\n", id
.id
));
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
));
245 DEBUG(0, ("test_getnewid2: PASSED!\n"));
250 static bool test_setmap1(TALLOC_CTX
*memctx
, struct idmap_domain
*dom
)
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"));
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"));
271 map
.sid
= dom_sid_parse_talloc(memctx
, DOM_SID1
"-100");
273 map
.xid
.type
= ID_TYPE_NOT_SPECIFIED
;
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"));
282 /* now the good ones */
283 map
.xid
.type
= ID_TYPE_UID
;
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"));
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"));
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"));
308 /* now a group with a different SID*/
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"));
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"));
324 DEBUG(0, ("test_setmap1: PASSED!\n"));
329 static bool test_sid2unixid1(TALLOC_CTX
*memctx
, struct idmap_domain
*dom
)
331 NTSTATUS status1
, status2
, status3
;
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"));
346 DEBUG(0, ("test_unixid2sid1: PASSED!\n"));
351 static bool test_sid2unixid2(TALLOC_CTX
*memctx
, struct idmap_domain
*dom
)
354 struct id_map uid_map
, gid_map
, test_map
;
357 ZERO_STRUCT(uid_map
);
358 ZERO_STRUCT(gid_map
);
360 /* create two mappings for a UID and GID */
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"));
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"));
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"));
392 if(test_map
.xid
.id
!=uid_map
.xid
.id
) {
393 DEBUG(0, ("test_sid2unixid1: sid2unixid returned wrong uid!\n"));
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"));
405 if(test_map
.xid
.id
!=gid_map
.xid
.id
) {
406 DEBUG(0, ("test_sid2unixid1: sid2unixid returned wrong gid!\n"));
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
419 DEBUG(0, ("test_sid2unixid1: PASSED!\n"));
424 static bool test_sids2unixids1(TALLOC_CTX
*memctx
, struct idmap_domain
*dom
)
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"));
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"));
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
);
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
);
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
);
478 DEBUG(0, ("test_sids2unixids1: PASSED!\n"));
480 talloc_free(test_maps
);
485 static bool test_sids2unixids2(TALLOC_CTX
*memctx
, struct idmap_domain
*dom
)
488 struct id_map
**test_maps
;
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
);
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
);
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
);
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
);
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
);
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
);
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
);
555 DEBUG(0, ("test_sids2unixids2: PASSED!\n"));
557 talloc_free(test_maps
);
562 static bool test_sids2unixids3(TALLOC_CTX
*memctx
, struct idmap_domain
*dom
)
565 struct id_map
**test_maps
;
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
);
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
,
589 test_maps
[0]->xid
.type
= ID_TYPE_UID
;
591 test_maps
[1]->sid
= dom_sid_parse_talloc(test_maps
,
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",
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
,
609 test_maps
[0]->xid
.type
= ID_TYPE_UID
;
610 test_maps
[1]->sid
= dom_sid_parse_talloc(test_maps
,
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",
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
,
628 test_maps
[1]->sid
= dom_sid_parse_talloc(test_maps
,
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",
640 DEBUG(0, ("test_sids2unixids3: PASSED!\n"));
643 talloc_free(test_maps
);
644 dom
->read_only
= false;
648 static bool test_unixid2sid1(TALLOC_CTX
*memctx
, struct idmap_domain
*dom
)
650 NTSTATUS status1
, status2
, status3
;
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"));
665 DEBUG(0, ("test_unixid2sid1: PASSED!\n"));
670 static bool test_unixid2sid2(TALLOC_CTX
*memctx
, struct idmap_domain
*dom
)
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"));
691 DEBUG(0, ("test_unixid2sid2: PASSED!\n"));
698 static bool test_unixid2sid3(TALLOC_CTX
*memctx
, struct idmap_domain
*dom
)
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"));
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"));
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"));
738 if(test_map
.xid
.type
!=uid_map
.xid
.type
) {
739 DEBUG(0, ("test_unixid2sid3: unixid2sid returned wrong type!\n"));
743 if(!dom_sid_equal(test_map
.sid
, uid_map
.sid
)) {
744 DEBUG(0, ("test_unixid2sid3: unixid2sid returned wrong SID!\n"));
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"));
759 if(test_map
.xid
.type
!=gid_map
.xid
.type
) {
760 DEBUG(0, ("test_unixid2sid3: unixid2sid returned wrong type!\n"));
764 if(!dom_sid_equal(test_map
.sid
,gid_map
.sid
)) {
765 DEBUG(0, ("test_unixid2sid3: unixid2sid returned wrong SID!\n"));
769 DEBUG(0, ("test_unixid2sid3: PASSED!\n"));
774 static bool test_unixids2sids1(TALLOC_CTX
*memctx
, struct idmap_domain
*dom
)
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"));
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"));
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
);
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
);
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
);
830 DEBUG(0, ("test_unixids2sids1: PASSED!\n"));
832 talloc_free(test_maps
);
837 static bool test_unixids2sids2(TALLOC_CTX
*memctx
, struct idmap_domain
*dom
)
840 struct id_map
**test_maps
;
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
);
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 "
865 DEBUG(0, ("test_unixids2sids2: PASSED!\n"));
868 talloc_free(test_maps
);
873 static bool test_unixids2sids3(TALLOC_CTX
*memctx
, struct idmap_domain
*dom
)
876 struct id_map uid_map
, gid_map
, **test_maps
;
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"));
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"));
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
);
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",
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",
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",
961 DEBUG(0, ("test_unixids2sids3: PASSED!\n"));
964 talloc_free(test_maps
);
968 #define CHECKRESULT(r) if(!r) {return r;}
970 bool run_idmap_tdb_common_test(int dummy
)
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
);
984 dom
= createdomain(memctx
);
986 dom
->private_data
= ctx
;
988 /* test a single allocation from pool (no mapping) */
989 result
= test_getnewid1(memctx
, dom
);
992 /* test idmap_tdb_common_set_mapping */
993 result
= test_setmap1(memctx
, dom
);
996 /* test idmap_tdb_common_sid_to_unixid */
997 result
= test_sid2unixid1(memctx
, dom
);
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
);