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 static bool open_db(struct idmap_tdb_common_context
*ctx
)
70 db_path
= talloc_asprintf(talloc_tos(), "%s/idmap_test.tdb",
73 DEBUG(0, ("Out of memory!\n"));
77 ctx
->db
= db_open(ctx
, db_path
, 0, TDB_DEFAULT
,
78 O_RDWR
| O_CREAT
, 0600,
79 DBWRAP_LOCK_ORDER_1
, DBWRAP_FLAG_NONE
);
82 DEBUG(0, ("Failed to open database: %s\n", strerror(errno
)));
86 if(dbwrap_transaction_start(ctx
->db
) != 0) {
87 DEBUG(0, ("Failed to start transaction!\n"));
91 status
= dbwrap_store_uint32_bystring(ctx
->db
, ctx
->hwmkey_uid
,
93 if(!NT_STATUS_IS_OK(status
)) {
94 dbwrap_transaction_cancel(ctx
->db
);
98 status
= dbwrap_store_uint32_bystring(ctx
->db
, ctx
->hwmkey_gid
,
100 if(!NT_STATUS_IS_OK(status
)) {
101 dbwrap_transaction_cancel(ctx
->db
);
105 if(dbwrap_transaction_commit(ctx
->db
) != 0) {
106 DEBUG(0, ("Failed to commit transaction!\n"));
113 static struct idmap_tdb_common_context
*createcontext(TALLOC_CTX
*memctx
)
115 struct idmap_tdb_common_context
*ret
;
117 ret
= talloc_zero(memctx
, struct idmap_tdb_common_context
);
118 ret
->rw_ops
= talloc_zero(ret
, struct idmap_rw_ops
);
120 ret
->max_id
= HIGH_ID
;
121 ret
->hwmkey_uid
= HWM_USER
;
122 ret
->hwmkey_gid
= HWM_GROUP
;
124 ret
->rw_ops
->get_new_id
= idmap_tdb_common_get_new_id
;
125 ret
->rw_ops
->set_mapping
= idmap_tdb_common_set_mapping
;
134 static struct idmap_domain
*createdomain(TALLOC_CTX
*memctx
)
136 struct idmap_domain
*dom
;
138 dom
= talloc_zero(memctx
, struct idmap_domain
);
140 dom
->low_id
= LOW_ID
;
141 dom
->high_id
= HIGH_ID
;
142 dom
->read_only
= false;
143 dom
->methods
= talloc_zero(dom
, struct idmap_methods
);
144 dom
->methods
->sids_to_unixids
= idmap_tdb_common_sids_to_unixids
;
145 dom
->methods
->unixids_to_sids
= idmap_tdb_common_unixids_to_sids
;
146 dom
->methods
->allocate_id
= idmap_tdb_common_get_new_id
;
151 static bool test_getnewid1(TALLOC_CTX
*memctx
, struct idmap_domain
*dom
)
156 id
.type
= ID_TYPE_UID
;
158 status
= idmap_tdb_common_get_new_id(dom
, &id
);
160 if(!NT_STATUS_IS_OK(status
)) {
161 DEBUG(0, ("test_getnewid1: Could not allocate id!\n"));
166 DEBUG(0, ("test_getnewid1: Allocate returned "
171 if(id
.id
> HIGH_ID
|| id
.id
< LOW_ID
) {
172 DEBUG(0, ("test_getnewid1: Allocate returned "
173 "out of range id!\n"));
177 DEBUG(0, ("test_getnewid1: PASSED!\n"));
182 static bool test_getnewid2(TALLOC_CTX
*memctx
, struct idmap_domain
*dom
)
188 id
.type
= ID_TYPE_UID
;
190 status
= idmap_tdb_common_get_new_id(dom
, &id
);
192 if(!NT_STATUS_IS_OK(status
)) {
193 DEBUG(0, ("test_getnewid2: Could not allocate id!\n"));
198 DEBUG(0, ("test_getnewid2: Allocate returned "
203 if(id
.id
> HIGH_ID
|| id
.id
< LOW_ID
) {
204 DEBUG(0, ("test_getnewid2: Allocate returned "
205 "out of range id!\n"));
209 /* how many ids are left? */
211 left
= HIGH_ID
- id
.id
;
213 /* consume them all */
214 for(i
= 0; i
<left
; i
++) {
216 status
= idmap_tdb_common_get_new_id(dom
, &id
);
218 if(!NT_STATUS_IS_OK(status
)) {
219 DEBUG(0, ("test_getnewid2: Allocate returned "
220 "error %s\n", nt_errstr(status
)));
224 if(id
.id
> HIGH_ID
) {
225 DEBUG(0, ("test_getnewid2: Allocate returned "
226 "out of range id (%d)!\n", id
.id
));
231 /* one more must fail */
232 status
= idmap_tdb_common_get_new_id(dom
, &id
);
234 if(NT_STATUS_IS_OK(status
)) {
235 DEBUG(0, ("test_getnewid2: Could allocate id (%d) from "
236 "depleted pool!\n", id
.id
));
240 DEBUG(0, ("test_getnewid2: PASSED!\n"));
245 static bool test_setmap1(TALLOC_CTX
*memctx
, struct idmap_domain
*dom
)
252 /* test for correct return code with invalid data */
254 status
= idmap_tdb_common_set_mapping(dom
, NULL
);
255 if(!NT_STATUS_EQUAL(status
, NT_STATUS_INVALID_PARAMETER
)) {
256 DEBUG(0, ("test_setmap1: bad parameter handling!\n"));
260 status
= idmap_tdb_common_set_mapping(dom
, &map
);
261 if(!NT_STATUS_EQUAL(status
, NT_STATUS_INVALID_PARAMETER
)) {
262 DEBUG(0, ("test_setmap1: bad parameter handling!\n"));
266 map
.sid
= dom_sid_parse_talloc(memctx
, DOM_SID1
"-100");
268 map
.xid
.type
= ID_TYPE_NOT_SPECIFIED
;
271 status
= idmap_tdb_common_set_mapping(dom
, &map
);
272 if(!NT_STATUS_EQUAL(status
, NT_STATUS_INVALID_PARAMETER
)) {
273 DEBUG(0, ("test_setmap1: bad parameter handling!\n"));
277 /* now the good ones */
278 map
.xid
.type
= ID_TYPE_UID
;
281 status
= idmap_tdb_common_get_new_id(dom
, &(map
.xid
));
282 if(!NT_STATUS_IS_OK(status
)) {
283 DEBUG(0, ("test_setmap1: get_new_uid failed!\n"));
287 status
= idmap_tdb_common_set_mapping(dom
, &map
);
288 if(!NT_STATUS_IS_OK(status
)) {
289 DEBUG(0, ("test_setmap1: setting UID mapping failed!\n"));
293 /* try to set the same mapping again as group (must fail) */
295 map
.xid
.type
= ID_TYPE_GID
;
296 status
= idmap_tdb_common_set_mapping(dom
, &map
);
297 if(NT_STATUS_IS_OK(status
)) {
298 DEBUG(0, ("test_setmap1: could create map for "
299 "group and user!\n"));
303 /* now a group with a different SID*/
306 map
.sid
= dom_sid_parse_talloc(memctx
, DOM_SID1
"-101");
308 status
= idmap_tdb_common_get_new_id(dom
, &(map
.xid
));
309 if(!NT_STATUS_IS_OK(status
)) {
310 DEBUG(0, ("test_setmap1: get_new_gid failed!\n"));
314 status
= idmap_tdb_common_set_mapping(dom
, &map
);
315 if(!NT_STATUS_IS_OK(status
)) {
316 DEBUG(0, ("test_setmap1: setting GID mapping failed!\n"));
319 DEBUG(0, ("test_setmap1: PASSED!\n"));
324 static bool test_sid2unixid1(TALLOC_CTX
*memctx
, struct idmap_domain
*dom
)
326 NTSTATUS status1
, status2
, status3
;
329 /* check for correct dealing with bad parameters */
330 status1
= idmap_tdb_common_sid_to_unixid(NULL
, &map
);
331 status2
= idmap_tdb_common_sid_to_unixid(dom
, NULL
);
332 status3
= idmap_tdb_common_sid_to_unixid(NULL
, NULL
);
334 if(!NT_STATUS_EQUAL(NT_STATUS_INVALID_PARAMETER
, status1
) ||
335 !NT_STATUS_EQUAL(NT_STATUS_INVALID_PARAMETER
, status2
) ||
336 !NT_STATUS_EQUAL(NT_STATUS_INVALID_PARAMETER
, status3
)) {
337 DEBUG(0, ("test_setmap1: bad parameter handling!\n"));
341 DEBUG(0, ("test_unixid2sid1: PASSED!\n"));
346 static bool test_sid2unixid2(TALLOC_CTX
*memctx
, struct idmap_domain
*dom
)
349 struct id_map uid_map
, gid_map
, test_map
;
352 ZERO_STRUCT(uid_map
);
353 ZERO_STRUCT(gid_map
);
355 /* create two mappings for a UID and GID */
359 uid_map
.sid
= dom_sid_parse_talloc(memctx
, DOM_SID2
"-1000");
360 uid_map
.xid
.type
= ID_TYPE_UID
;
362 gid_map
.sid
= dom_sid_parse_talloc(memctx
, DOM_SID2
"-1001");
363 gid_map
.xid
.type
= ID_TYPE_GID
;
365 status
= idmap_tdb_common_new_mapping(dom
, &uid_map
);
366 if(!NT_STATUS_IS_OK(status
)) {
367 DEBUG(0, ("test_sid2unixid1: could not create uid map!\n"));
371 status
= idmap_tdb_common_new_mapping(dom
, &gid_map
);
372 if(!NT_STATUS_IS_OK(status
)) {
373 DEBUG(0, ("test_sid2unixid1: could not create gid map!\n"));
377 /* now read them back */
378 ZERO_STRUCT(test_map
);
379 test_map
.sid
= uid_map
.sid
;
381 status
= idmap_tdb_common_sid_to_unixid(dom
, &test_map
);
382 if(!NT_STATUS_IS_OK(status
)) {
383 DEBUG(0, ("test_sid2unixid1: sid2unixid failed for uid!\n"));
387 if(test_map
.xid
.id
!=uid_map
.xid
.id
) {
388 DEBUG(0, ("test_sid2unixid1: sid2unixid returned wrong uid!\n"));
392 test_map
.sid
= gid_map
.sid
;
394 status
= idmap_tdb_common_sid_to_unixid(dom
, &test_map
);
395 if(!NT_STATUS_IS_OK(status
)) {
396 DEBUG(0, ("test_sid2unixid1: sid2unixid failed for gid!\n"));
400 if(test_map
.xid
.id
!=gid_map
.xid
.id
) {
401 DEBUG(0, ("test_sid2unixid1: sid2unixid returned wrong gid!\n"));
406 * Go through the same tests again once to see if trying to recreate
407 * a mapping that was already created will work or not
414 DEBUG(0, ("test_sid2unixid1: PASSED!\n"));
419 static bool test_sids2unixids1(TALLOC_CTX
*memctx
, struct idmap_domain
*dom
)
422 struct id_map uid_map
, gid_map
, **test_maps
;
424 ZERO_STRUCT(uid_map
);
425 ZERO_STRUCT(gid_map
);
427 /* create two mappings for a UID and GID */
429 uid_map
.sid
= dom_sid_parse_talloc(memctx
, DOM_SID4
"-1000");
430 uid_map
.xid
.type
= ID_TYPE_UID
;
432 gid_map
.sid
= dom_sid_parse_talloc(memctx
, DOM_SID4
"-1001");
433 gid_map
.xid
.type
= ID_TYPE_GID
;
435 status
= idmap_tdb_common_new_mapping(dom
, &uid_map
);
436 if(!NT_STATUS_IS_OK(status
)) {
437 DEBUG(0, ("test_sids2unixids1: could not create uid map!\n"));
441 status
= idmap_tdb_common_new_mapping(dom
, &gid_map
);
442 if(!NT_STATUS_IS_OK(status
)) {
443 DEBUG(0, ("test_sids2unixids1: could not create gid map!\n"));
447 /* now read them back */
448 test_maps
= talloc_zero_array(memctx
, struct id_map
*, 3);
450 test_maps
[0] = talloc(test_maps
, struct id_map
);
451 test_maps
[1] = talloc(test_maps
, struct id_map
);
454 test_maps
[0]->sid
= talloc(test_maps
, struct dom_sid
);
455 test_maps
[1]->sid
= talloc(test_maps
, struct dom_sid
);
456 sid_copy(test_maps
[0]->sid
, uid_map
.sid
);
457 sid_copy(test_maps
[1]->sid
, gid_map
.sid
);
459 status
= idmap_tdb_common_sids_to_unixids(dom
, test_maps
);
460 if(!NT_STATUS_IS_OK(status
)) {
461 DEBUG(0, ("test_sids2sunixids1: sids2unixids failed!\n"));
462 talloc_free(test_maps
);
466 if(test_maps
[0]->xid
.id
!=uid_map
.xid
.id
||
467 test_maps
[1]->xid
.id
!=gid_map
.xid
.id
) {
468 DEBUG(0, ("test_sids2unixids1: sid2unixid returned wrong xid!\n"));
469 talloc_free(test_maps
);
473 DEBUG(0, ("test_sids2unixids1: PASSED!\n"));
475 talloc_free(test_maps
);
480 static bool test_sids2unixids2(TALLOC_CTX
*memctx
, struct idmap_domain
*dom
)
483 struct id_map
**test_maps
;
486 test_maps
= talloc_zero_array(memctx
, struct id_map
*, 3);
488 test_maps
[0] = talloc(test_maps
, struct id_map
);
489 test_maps
[1] = talloc(test_maps
, struct id_map
);
492 /* ask for two new mappings for a UID and GID */
493 test_maps
[0]->sid
= dom_sid_parse_talloc(test_maps
, DOM_SID4
"-1003");
494 test_maps
[0]->xid
.type
= ID_TYPE_UID
;
495 test_maps
[1]->sid
= dom_sid_parse_talloc(test_maps
, DOM_SID4
"-1004");
496 test_maps
[1]->xid
.type
= ID_TYPE_GID
;
498 status
= idmap_tdb_common_sids_to_unixids(dom
, test_maps
);
499 if(!NT_STATUS_IS_OK(status
)) {
500 DEBUG(0, ("test_sids2sunixids2: sids2unixids "
501 "failed (%s)!\n", nt_errstr(status
)));
502 talloc_free(test_maps
);
506 if(test_maps
[0]->xid
.id
== 0 || test_maps
[1]->xid
.id
== 0) {
507 DEBUG(0, ("test_sids2sunixids2: sids2unixids "
508 "returned zero ids!\n"));
509 talloc_free(test_maps
);
513 save
= test_maps
[1]->xid
;
515 /* ask for a known and a new mapping at the same time */
516 talloc_free(test_maps
);
517 test_maps
= talloc_zero_array(memctx
, struct id_map
*, 3);
518 test_maps
[0] = talloc(test_maps
, struct id_map
);
519 test_maps
[1] = talloc(test_maps
, struct id_map
);
522 test_maps
[0]->sid
= dom_sid_parse_talloc(test_maps
, DOM_SID4
"-1004");
523 test_maps
[0]->xid
.type
= ID_TYPE_GID
;
524 test_maps
[1]->sid
= dom_sid_parse_talloc(test_maps
, DOM_SID4
"-1005");
525 test_maps
[1]->xid
.type
= ID_TYPE_UID
;
527 status
= idmap_tdb_common_sids_to_unixids(dom
, test_maps
);
528 if(!NT_STATUS_IS_OK(status
)) {
529 DEBUG(0, ("test_sids2sunixids2: sids2unixids (2) "
530 "failed (%s)!\n", nt_errstr(status
)));
531 talloc_free(test_maps
);
535 if(test_maps
[0]->xid
.type
!= save
.type
||
536 test_maps
[0]->xid
.id
!= save
.id
) {
537 DEBUG(0, ("test_sids2sunixids2: second lookup returned "
538 "different value!\n"));
539 talloc_free(test_maps
);
543 if(test_maps
[1]->xid
.id
== 0) {
544 DEBUG(0, ("test_sids2sunixids2: sids2unixids "
545 "returned zero id for mixed mapping request!\n"));
546 talloc_free(test_maps
);
550 DEBUG(0, ("test_sids2unixids2: PASSED!\n"));
552 talloc_free(test_maps
);
557 static bool test_sids2unixids3(TALLOC_CTX
*memctx
, struct idmap_domain
*dom
)
560 struct id_map
**test_maps
;
564 * check the mapping states:
565 * NONE_MAPPED, SOME_UNMAPPED, OK (all mapped)
567 * use the ids created by test_sids2unixids1
568 * need to make dom read-only
571 dom
->read_only
= true;
573 test_maps
= talloc_zero_array(memctx
, struct id_map
*, 3);
575 test_maps
[0] = talloc(test_maps
, struct id_map
);
576 test_maps
[1] = talloc(test_maps
, struct id_map
);
579 /* NONE_MAPPED first */
580 test_maps
[0]->sid
= talloc(test_maps
, struct dom_sid
);
581 test_maps
[1]->sid
= talloc(test_maps
, struct dom_sid
);
582 test_maps
[0]->sid
= dom_sid_parse_talloc(test_maps
,
584 test_maps
[0]->xid
.type
= ID_TYPE_UID
;
586 test_maps
[1]->sid
= dom_sid_parse_talloc(test_maps
,
588 test_maps
[1]->xid
.type
= ID_TYPE_GID
;
590 status
= idmap_tdb_common_sids_to_unixids(dom
, test_maps
);
591 if(!NT_STATUS_EQUAL(status
, NT_STATUS_NONE_MAPPED
)) {
592 DEBUG(0, ("test_sids2unixids3: incorrect status "
593 "(%s), expected NT_STATUS_NONE_MAPPED!\n",
600 test_maps
[0]->sid
= talloc(test_maps
, struct dom_sid
);
601 test_maps
[1]->sid
= talloc(test_maps
, struct dom_sid
);
602 test_maps
[0]->sid
= dom_sid_parse_talloc(test_maps
,
604 test_maps
[0]->xid
.type
= ID_TYPE_UID
;
605 test_maps
[1]->sid
= dom_sid_parse_talloc(test_maps
,
607 test_maps
[1]->xid
.type
= ID_TYPE_GID
;
609 status
= idmap_tdb_common_sids_to_unixids(dom
, test_maps
);
610 if(!NT_STATUS_EQUAL(status
, STATUS_SOME_UNMAPPED
)) {
611 DEBUG(0, ("test_sids2unixids3: incorrect status "
612 "(%s), expected STATUS_SOME_UNMAPPED!\n",
619 test_maps
[0]->sid
= talloc(test_maps
, struct dom_sid
);
620 test_maps
[1]->sid
= talloc(test_maps
, struct dom_sid
);
621 test_maps
[0]->sid
= dom_sid_parse_talloc(test_maps
,
623 test_maps
[1]->sid
= dom_sid_parse_talloc(test_maps
,
626 status
= idmap_tdb_common_sids_to_unixids(dom
, test_maps
);
627 if(!NT_STATUS_IS_OK(status
)) {
628 DEBUG(0, ("test_sids2unixids3: incorrect status "
629 "(%s), expected NT_STATUS_OK!\n",
635 DEBUG(0, ("test_sids2unixids3: PASSED!\n"));
638 talloc_free(test_maps
);
639 dom
->read_only
= false;
643 static bool test_unixid2sid1(TALLOC_CTX
*memctx
, struct idmap_domain
*dom
)
645 NTSTATUS status1
, status2
, status3
;
648 /* check for correct dealing with bad parameters */
649 status1
= idmap_tdb_common_unixid_to_sid(NULL
, &map
);
650 status2
= idmap_tdb_common_unixid_to_sid(dom
, NULL
);
651 status3
= idmap_tdb_common_unixid_to_sid(NULL
, NULL
);
653 if(!NT_STATUS_EQUAL(NT_STATUS_INVALID_PARAMETER
, status1
) ||
654 !NT_STATUS_EQUAL(NT_STATUS_INVALID_PARAMETER
, status2
) ||
655 !NT_STATUS_EQUAL(NT_STATUS_INVALID_PARAMETER
, status3
)) {
656 DEBUG(0, ("test_setmap1: bad parameter handling!\n"));
660 DEBUG(0, ("test_unixid2sid1: PASSED!\n"));
665 static bool test_unixid2sid2(TALLOC_CTX
*memctx
, struct idmap_domain
*dom
)
671 /* ask for mapping that is outside of the range */
672 map
= talloc(memctx
, struct id_map
);
673 map
->sid
= talloc(map
, struct dom_sid
);
675 map
->xid
.type
= ID_TYPE_UID
;
676 map
->xid
.id
= HIGH_ID
+ 1;
678 status
= idmap_tdb_common_unixid_to_sid(dom
, map
);
679 if(NT_STATUS_IS_OK(status
)) {
680 DEBUG(0, ("test_unixid2sid2: unixid2sid returned "
681 "out-of-range result\n"));
686 DEBUG(0, ("test_unixid2sid2: PASSED!\n"));
693 static bool test_unixid2sid3(TALLOC_CTX
*memctx
, struct idmap_domain
*dom
)
696 struct id_map uid_map
, gid_map
, test_map
;
697 struct dom_sid testsid
;
699 ZERO_STRUCT(uid_map
);
700 ZERO_STRUCT(gid_map
);
702 /* create two mappings for a UID and GID */
703 uid_map
.sid
= dom_sid_parse_talloc(memctx
, DOM_SID3
"-1000");
704 uid_map
.xid
.type
= ID_TYPE_UID
;
706 gid_map
.sid
= dom_sid_parse_talloc(memctx
, DOM_SID3
"-1001");
707 gid_map
.xid
.type
= ID_TYPE_GID
;
709 status
= idmap_tdb_common_new_mapping(dom
, &uid_map
);
710 if(!NT_STATUS_IS_OK(status
)) {
711 DEBUG(0, ("test_unixid2sid3: could not create uid map!\n"));
715 status
= idmap_tdb_common_new_mapping(dom
, &gid_map
);
716 if(!NT_STATUS_IS_OK(status
)) {
717 DEBUG(0, ("test_unixid2sid3: could not create gid map!\n"));
721 /* now read them back */
722 ZERO_STRUCT(test_map
);
723 test_map
.xid
.id
= uid_map
.xid
.id
;
724 test_map
.xid
.type
= ID_TYPE_UID
;
725 test_map
.sid
= &testsid
;
727 status
= idmap_tdb_common_unixid_to_sid(dom
, &test_map
);
728 if(!NT_STATUS_IS_OK(status
)) {
729 DEBUG(0, ("test_unixid2sid3: unixid2sid failed for uid!\n"));
733 if(test_map
.xid
.type
!=uid_map
.xid
.type
) {
734 DEBUG(0, ("test_unixid2sid3: unixid2sid returned wrong type!\n"));
738 if(!dom_sid_equal(test_map
.sid
, uid_map
.sid
)) {
739 DEBUG(0, ("test_unixid2sid3: unixid2sid returned wrong SID!\n"));
743 ZERO_STRUCT(test_map
);
744 test_map
.xid
.id
= gid_map
.xid
.id
;
745 test_map
.xid
.type
= ID_TYPE_GID
;
746 test_map
.sid
= &testsid
;
748 status
= idmap_tdb_common_unixid_to_sid(dom
, &test_map
);
749 if(!NT_STATUS_IS_OK(status
)) {
750 DEBUG(0, ("test_unixid2sid3: unixid2sid failed for gid!\n"));
754 if(test_map
.xid
.type
!=gid_map
.xid
.type
) {
755 DEBUG(0, ("test_unixid2sid3: unixid2sid returned wrong type!\n"));
759 if(!dom_sid_equal(test_map
.sid
,gid_map
.sid
)) {
760 DEBUG(0, ("test_unixid2sid3: unixid2sid returned wrong SID!\n"));
764 DEBUG(0, ("test_unixid2sid3: PASSED!\n"));
769 static bool test_unixids2sids1(TALLOC_CTX
*memctx
, struct idmap_domain
*dom
)
772 struct id_map uid_map
, gid_map
, **test_maps
;
774 ZERO_STRUCT(uid_map
);
775 ZERO_STRUCT(gid_map
);
777 /* create two mappings for a UID and GID */
779 uid_map
.sid
= dom_sid_parse_talloc(memctx
, DOM_SID5
"-1000");
780 uid_map
.xid
.type
= ID_TYPE_UID
;
782 gid_map
.sid
= dom_sid_parse_talloc(memctx
, DOM_SID5
"-1001");
783 gid_map
.xid
.type
= ID_TYPE_GID
;
785 status
= idmap_tdb_common_new_mapping(dom
, &uid_map
);
786 if(!NT_STATUS_IS_OK(status
)) {
787 DEBUG(0, ("test_unixids2sids1: could not create uid map!\n"));
791 status
= idmap_tdb_common_new_mapping(dom
, &gid_map
);
792 if(!NT_STATUS_IS_OK(status
)) {
793 DEBUG(0, ("test_unixids2sids1: could not create gid map!\n"));
797 /* now read them back */
798 test_maps
= talloc_zero_array(memctx
, struct id_map
*, 3);
800 test_maps
[0] = talloc(test_maps
, struct id_map
);
801 test_maps
[1] = talloc(test_maps
, struct id_map
);
804 test_maps
[0]->sid
= talloc(test_maps
, struct dom_sid
);
805 test_maps
[1]->sid
= talloc(test_maps
, struct dom_sid
);
806 test_maps
[0]->xid
.id
= uid_map
.xid
.id
;
807 test_maps
[0]->xid
.type
= ID_TYPE_UID
;
808 test_maps
[1]->xid
.id
= gid_map
.xid
.id
;
809 test_maps
[1]->xid
.type
= ID_TYPE_GID
;
811 status
= idmap_tdb_common_unixids_to_sids(dom
, test_maps
);
812 if(!NT_STATUS_IS_OK(status
)) {
813 DEBUG(0, ("test_unixids2sids1: unixids2sids failed!\n"));
814 talloc_free(test_maps
);
818 if(!dom_sid_equal(test_maps
[0]->sid
, uid_map
.sid
) ||
819 !dom_sid_equal(test_maps
[1]->sid
, gid_map
.sid
) ) {
820 DEBUG(0, ("test_unixids2sids1: unixids2sids returned wrong sid!\n"));
821 talloc_free(test_maps
);
825 DEBUG(0, ("test_unixids2sids1: PASSED!\n"));
827 talloc_free(test_maps
);
832 static bool test_unixids2sids2(TALLOC_CTX
*memctx
, struct idmap_domain
*dom
)
835 struct id_map
**test_maps
;
838 test_maps
= talloc_zero_array(memctx
, struct id_map
*, 3);
840 test_maps
[0] = talloc(test_maps
, struct id_map
);
841 test_maps
[1] = talloc(test_maps
, struct id_map
);
844 /* ask for two unknown mappings for a UID and GID */
845 test_maps
[0]->sid
= talloc(test_maps
, struct dom_sid
);
846 test_maps
[1]->sid
= talloc(test_maps
, struct dom_sid
);
847 test_maps
[0]->xid
.id
= HIGH_ID
- 1;
848 test_maps
[0]->xid
.type
= ID_TYPE_UID
;
849 test_maps
[1]->xid
.id
= HIGH_ID
- 1;
850 test_maps
[1]->xid
.type
= ID_TYPE_GID
;
852 status
= idmap_tdb_common_unixids_to_sids(dom
, test_maps
);
853 if(NT_STATUS_IS_OK(status
)) {
854 DEBUG(0, ("test_unixids2sids2: unixids2sids succeeded "
860 DEBUG(0, ("test_unixids2sids2: PASSED!\n"));
863 talloc_free(test_maps
);
868 static bool test_unixids2sids3(TALLOC_CTX
*memctx
, struct idmap_domain
*dom
)
871 struct id_map uid_map
, gid_map
, **test_maps
;
874 ZERO_STRUCT(uid_map
);
875 ZERO_STRUCT(gid_map
);
877 /* create two mappings for a UID and GID */
878 uid_map
.sid
= dom_sid_parse_talloc(memctx
, DOM_SID6
"-1000");
879 uid_map
.xid
.type
= ID_TYPE_UID
;
881 gid_map
.sid
= dom_sid_parse_talloc(memctx
, DOM_SID6
"-1001");
882 gid_map
.xid
.type
= ID_TYPE_GID
;
884 status
= idmap_tdb_common_new_mapping(dom
, &uid_map
);
885 if(!NT_STATUS_IS_OK(status
)) {
886 DEBUG(0, ("test_unixids2sids3: could not create uid map!\n"));
890 status
= idmap_tdb_common_new_mapping(dom
, &gid_map
);
891 if(!NT_STATUS_IS_OK(status
)) {
892 DEBUG(0, ("test_unixids2sids3: could not create gid map!\n"));
897 * check the mapping states:
898 * NONE_MAPPED, SOME_UNMAPPED, OK (all mapped)
900 test_maps
= talloc_zero_array(memctx
, struct id_map
*, 3);
902 test_maps
[0] = talloc(test_maps
, struct id_map
);
903 test_maps
[1] = talloc(test_maps
, struct id_map
);
906 /* NONE_MAPPED first */
907 test_maps
[0]->sid
= talloc(test_maps
, struct dom_sid
);
908 test_maps
[1]->sid
= talloc(test_maps
, struct dom_sid
);
910 test_maps
[0]->xid
.id
= HIGH_ID
- 1;
911 test_maps
[0]->xid
.type
= ID_TYPE_UID
;
913 test_maps
[1]->xid
.id
= HIGH_ID
- 1;
914 test_maps
[1]->xid
.type
= ID_TYPE_GID
;
916 status
= idmap_tdb_common_unixids_to_sids(dom
, test_maps
);
917 if(!NT_STATUS_EQUAL(status
, NT_STATUS_NONE_MAPPED
)) {
918 DEBUG(0, ("test_unixids2sids3: incorrect status "
919 "(%s), expected NT_STATUS_NONE_MAPPED!\n",
926 test_maps
[0]->sid
= talloc(test_maps
, struct dom_sid
);
927 test_maps
[1]->sid
= talloc(test_maps
, struct dom_sid
);
928 test_maps
[0]->xid
= uid_map
.xid
;
929 test_maps
[1]->xid
.id
= HIGH_ID
- 1;
930 test_maps
[1]->xid
.type
= ID_TYPE_GID
;
932 status
= idmap_tdb_common_unixids_to_sids(dom
, test_maps
);
933 if(!NT_STATUS_EQUAL(status
, STATUS_SOME_UNMAPPED
)) {
934 DEBUG(0, ("test_unixids2sids3: incorrect status "
935 "(%s), expected STATUS_SOME_UNMAPPED!\n",
942 test_maps
[0]->sid
= talloc(test_maps
, struct dom_sid
);
943 test_maps
[1]->sid
= talloc(test_maps
, struct dom_sid
);
944 test_maps
[0]->xid
= uid_map
.xid
;
945 test_maps
[1]->xid
= gid_map
.xid
;
947 status
= idmap_tdb_common_unixids_to_sids(dom
, test_maps
);
948 if(!NT_STATUS_IS_OK(status
)) {
949 DEBUG(0, ("test_unixids2sids3: incorrect status "
950 "(%s), expected NT_STATUS_OK!\n",
956 DEBUG(0, ("test_unixids2sids3: PASSED!\n"));
959 talloc_free(test_maps
);
963 #define CHECKRESULT(r) if(!r) {return r;}
965 bool run_idmap_tdb_common_test(int dummy
)
968 struct idmap_tdb_common_context
*ctx
;
969 struct idmap_domain
*dom
;
971 TALLOC_CTX
*memctx
= talloc_new(NULL
);
972 TALLOC_CTX
*stack
= talloc_stackframe();
974 ctx
= createcontext(memctx
);
979 dom
= createdomain(memctx
);
981 dom
->private_data
= ctx
;
983 /* test a single allocation from pool (no mapping) */
984 result
= test_getnewid1(memctx
, dom
);
987 /* test idmap_tdb_common_set_mapping */
988 result
= test_setmap1(memctx
, dom
);
991 /* test idmap_tdb_common_sid_to_unixid */
992 result
= test_sid2unixid1(memctx
, dom
);
994 result
= test_sid2unixid2(memctx
, dom
);
997 /* test idmap_tdb_common_sids_to_unixids */
998 result
= test_sids2unixids1(memctx
, dom
);
1000 result
= test_sids2unixids2(memctx
, dom
);
1001 CHECKRESULT(result
);
1002 result
= test_sids2unixids3(memctx
, dom
);
1003 CHECKRESULT(result
);
1005 /* test idmap_tdb_common_unixid_to_sid */
1006 result
= test_unixid2sid1(memctx
, dom
);
1007 CHECKRESULT(result
);
1008 result
= test_unixid2sid2(memctx
, dom
);
1009 CHECKRESULT(result
);
1010 result
= test_unixid2sid3(memctx
, dom
);
1011 CHECKRESULT(result
);
1013 /* test idmap_tdb_common_unixids_to_sids */
1014 result
= test_unixids2sids1(memctx
, dom
);
1015 CHECKRESULT(result
);
1016 result
= test_unixids2sids2(memctx
, dom
);
1017 CHECKRESULT(result
);
1018 result
= test_unixids2sids3(memctx
, dom
);
1019 CHECKRESULT(result
);
1021 /* test filling up the range */
1022 result
= test_getnewid2(memctx
, dom
);
1023 CHECKRESULT(result
);
1025 talloc_free(memctx
);