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 NTSTATUS
idmap_test_tdb_db_init(struct idmap_domain
*dom
)
115 struct idmap_tdb_common_context
*ret
;
117 DBG_DEBUG("called for domain '%s'\n", dom
->name
);
119 ret
= talloc_zero(dom
, struct idmap_tdb_common_context
);
121 return NT_STATUS_NO_MEMORY
;
123 ret
->rw_ops
= talloc_zero(ret
, struct idmap_rw_ops
);
124 if (ret
->rw_ops
== NULL
) {
126 return NT_STATUS_NO_MEMORY
;
129 ret
->max_id
= HIGH_ID
;
130 ret
->hwmkey_uid
= HWM_USER
;
131 ret
->hwmkey_gid
= HWM_GROUP
;
133 ret
->rw_ops
->get_new_id
= idmap_tdb_common_get_new_id
;
134 ret
->rw_ops
->set_mapping
= idmap_tdb_common_set_mapping
;
138 return NT_STATUS_INTERNAL_ERROR
;
141 dom
->private_data
= ret
;
146 static struct idmap_domain
*createdomain(TALLOC_CTX
*memctx
)
148 struct idmap_domain
*dom
;
149 struct idmap_methods
*m
;
151 dom
= talloc_zero(memctx
, struct idmap_domain
);
153 dom
->low_id
= LOW_ID
;
154 dom
->high_id
= HIGH_ID
;
155 dom
->read_only
= false;
156 m
= talloc_zero(dom
, struct idmap_methods
);
157 *m
= (struct idmap_methods
) {
158 .init
= idmap_test_tdb_db_init
,
159 .sids_to_unixids
= idmap_tdb_common_sids_to_unixids
,
160 .unixids_to_sids
= idmap_tdb_common_unixids_to_sids
,
161 .allocate_id
= idmap_tdb_common_get_new_id
,
168 static bool test_getnewid1(TALLOC_CTX
*memctx
, struct idmap_domain
*dom
)
173 id
.type
= ID_TYPE_UID
;
175 status
= idmap_tdb_common_get_new_id(dom
, &id
);
177 if(!NT_STATUS_IS_OK(status
)) {
178 DEBUG(0, ("test_getnewid1: Could not allocate id!\n"));
183 DEBUG(0, ("test_getnewid1: Allocate returned "
188 if(id
.id
> HIGH_ID
|| id
.id
< LOW_ID
) {
189 DEBUG(0, ("test_getnewid1: Allocate returned "
190 "out of range id!\n"));
194 DEBUG(0, ("test_getnewid1: PASSED!\n"));
199 static bool test_getnewid2(TALLOC_CTX
*memctx
, struct idmap_domain
*dom
)
205 id
.type
= ID_TYPE_UID
;
207 status
= idmap_tdb_common_get_new_id(dom
, &id
);
209 if(!NT_STATUS_IS_OK(status
)) {
210 DEBUG(0, ("test_getnewid2: Could not allocate id!\n"));
215 DEBUG(0, ("test_getnewid2: Allocate returned "
220 if(id
.id
> HIGH_ID
|| id
.id
< LOW_ID
) {
221 DEBUG(0, ("test_getnewid2: Allocate returned "
222 "out of range id!\n"));
226 /* how many ids are left? */
228 left
= HIGH_ID
- id
.id
;
230 /* consume them all */
231 for(i
= 0; i
<left
; i
++) {
233 status
= idmap_tdb_common_get_new_id(dom
, &id
);
235 if(!NT_STATUS_IS_OK(status
)) {
236 DEBUG(0, ("test_getnewid2: Allocate returned "
237 "error %s\n", nt_errstr(status
)));
241 if(id
.id
> HIGH_ID
) {
242 DEBUG(0, ("test_getnewid2: Allocate returned "
243 "out of range id (%d)!\n", id
.id
));
248 /* one more must fail */
249 status
= idmap_tdb_common_get_new_id(dom
, &id
);
251 if(NT_STATUS_IS_OK(status
)) {
252 DEBUG(0, ("test_getnewid2: Could allocate id (%d) from "
253 "depleted pool!\n", id
.id
));
257 DEBUG(0, ("test_getnewid2: PASSED!\n"));
262 static bool test_setmap1(TALLOC_CTX
*memctx
, struct idmap_domain
*dom
)
269 /* test for correct return code with invalid data */
271 status
= idmap_tdb_common_set_mapping(dom
, NULL
);
272 if(!NT_STATUS_EQUAL(status
, NT_STATUS_INVALID_PARAMETER
)) {
273 DEBUG(0, ("test_setmap1: bad parameter handling!\n"));
277 status
= idmap_tdb_common_set_mapping(dom
, &map
);
278 if(!NT_STATUS_EQUAL(status
, NT_STATUS_INVALID_PARAMETER
)) {
279 DEBUG(0, ("test_setmap1: bad parameter handling!\n"));
283 map
.sid
= dom_sid_parse_talloc(memctx
, DOM_SID1
"-100");
285 map
.xid
.type
= ID_TYPE_NOT_SPECIFIED
;
288 status
= idmap_tdb_common_set_mapping(dom
, &map
);
289 if(!NT_STATUS_EQUAL(status
, NT_STATUS_INVALID_PARAMETER
)) {
290 DEBUG(0, ("test_setmap1: bad parameter handling!\n"));
294 /* now the good ones */
295 map
.xid
.type
= ID_TYPE_UID
;
298 status
= idmap_tdb_common_get_new_id(dom
, &(map
.xid
));
299 if(!NT_STATUS_IS_OK(status
)) {
300 DEBUG(0, ("test_setmap1: get_new_uid failed!\n"));
304 status
= idmap_tdb_common_set_mapping(dom
, &map
);
305 if(!NT_STATUS_IS_OK(status
)) {
306 DEBUG(0, ("test_setmap1: setting UID mapping failed!\n"));
310 /* try to set the same mapping again as group (must fail) */
312 map
.xid
.type
= ID_TYPE_GID
;
313 status
= idmap_tdb_common_set_mapping(dom
, &map
);
314 if(NT_STATUS_IS_OK(status
)) {
315 DEBUG(0, ("test_setmap1: could create map for "
316 "group and user!\n"));
320 /* now a group with a different SID*/
323 map
.sid
= dom_sid_parse_talloc(memctx
, DOM_SID1
"-101");
325 status
= idmap_tdb_common_get_new_id(dom
, &(map
.xid
));
326 if(!NT_STATUS_IS_OK(status
)) {
327 DEBUG(0, ("test_setmap1: get_new_gid failed!\n"));
331 status
= idmap_tdb_common_set_mapping(dom
, &map
);
332 if(!NT_STATUS_IS_OK(status
)) {
333 DEBUG(0, ("test_setmap1: setting GID mapping failed!\n"));
336 DEBUG(0, ("test_setmap1: PASSED!\n"));
341 static bool test_sid2unixid1(TALLOC_CTX
*memctx
, struct idmap_domain
*dom
)
343 NTSTATUS status1
, status2
, status3
;
346 /* check for correct dealing with bad parameters */
347 status1
= idmap_tdb_common_sid_to_unixid(NULL
, &map
);
348 status2
= idmap_tdb_common_sid_to_unixid(dom
, NULL
);
349 status3
= idmap_tdb_common_sid_to_unixid(NULL
, NULL
);
351 if(!NT_STATUS_EQUAL(NT_STATUS_INVALID_PARAMETER
, status1
) ||
352 !NT_STATUS_EQUAL(NT_STATUS_INVALID_PARAMETER
, status2
) ||
353 !NT_STATUS_EQUAL(NT_STATUS_INVALID_PARAMETER
, status3
)) {
354 DEBUG(0, ("test_setmap1: bad parameter handling!\n"));
358 DEBUG(0, ("test_unixid2sid1: PASSED!\n"));
363 static bool test_sid2unixid2(TALLOC_CTX
*memctx
, struct idmap_domain
*dom
)
366 struct id_map uid_map
, gid_map
, test_map
;
369 ZERO_STRUCT(uid_map
);
370 ZERO_STRUCT(gid_map
);
372 /* create two mappings for a UID and GID */
376 uid_map
.sid
= dom_sid_parse_talloc(memctx
, DOM_SID2
"-1000");
377 uid_map
.xid
.type
= ID_TYPE_UID
;
379 gid_map
.sid
= dom_sid_parse_talloc(memctx
, DOM_SID2
"-1001");
380 gid_map
.xid
.type
= ID_TYPE_GID
;
382 status
= idmap_tdb_common_new_mapping(dom
, &uid_map
);
383 if(!NT_STATUS_IS_OK(status
)) {
384 DEBUG(0, ("test_sid2unixid1: could not create uid map!\n"));
388 status
= idmap_tdb_common_new_mapping(dom
, &gid_map
);
389 if(!NT_STATUS_IS_OK(status
)) {
390 DEBUG(0, ("test_sid2unixid1: could not create gid map!\n"));
394 /* now read them back */
395 ZERO_STRUCT(test_map
);
396 test_map
.sid
= uid_map
.sid
;
398 status
= idmap_tdb_common_sid_to_unixid(dom
, &test_map
);
399 if(!NT_STATUS_IS_OK(status
)) {
400 DEBUG(0, ("test_sid2unixid1: sid2unixid failed for uid!\n"));
404 if(test_map
.xid
.id
!=uid_map
.xid
.id
) {
405 DEBUG(0, ("test_sid2unixid1: sid2unixid returned wrong uid!\n"));
409 test_map
.sid
= gid_map
.sid
;
411 status
= idmap_tdb_common_sid_to_unixid(dom
, &test_map
);
412 if(!NT_STATUS_IS_OK(status
)) {
413 DEBUG(0, ("test_sid2unixid1: sid2unixid failed for gid!\n"));
417 if(test_map
.xid
.id
!=gid_map
.xid
.id
) {
418 DEBUG(0, ("test_sid2unixid1: sid2unixid returned wrong gid!\n"));
423 * Go through the same tests again once to see if trying to recreate
424 * a mapping that was already created will work or not
431 DEBUG(0, ("test_sid2unixid1: PASSED!\n"));
436 static bool test_sids2unixids1(TALLOC_CTX
*memctx
, struct idmap_domain
*dom
)
439 struct id_map uid_map
, gid_map
, **test_maps
;
441 ZERO_STRUCT(uid_map
);
442 ZERO_STRUCT(gid_map
);
444 /* create two mappings for a UID and GID */
446 uid_map
.sid
= dom_sid_parse_talloc(memctx
, DOM_SID4
"-1000");
447 uid_map
.xid
.type
= ID_TYPE_UID
;
449 gid_map
.sid
= dom_sid_parse_talloc(memctx
, DOM_SID4
"-1001");
450 gid_map
.xid
.type
= ID_TYPE_GID
;
452 status
= idmap_tdb_common_new_mapping(dom
, &uid_map
);
453 if(!NT_STATUS_IS_OK(status
)) {
454 DEBUG(0, ("test_sids2unixids1: could not create uid map!\n"));
458 status
= idmap_tdb_common_new_mapping(dom
, &gid_map
);
459 if(!NT_STATUS_IS_OK(status
)) {
460 DEBUG(0, ("test_sids2unixids1: could not create gid map!\n"));
464 /* now read them back */
465 test_maps
= talloc_zero_array(memctx
, struct id_map
*, 3);
467 test_maps
[0] = talloc(test_maps
, struct id_map
);
468 test_maps
[1] = talloc(test_maps
, struct id_map
);
471 test_maps
[0]->sid
= talloc(test_maps
, struct dom_sid
);
472 test_maps
[1]->sid
= talloc(test_maps
, struct dom_sid
);
473 sid_copy(test_maps
[0]->sid
, uid_map
.sid
);
474 sid_copy(test_maps
[1]->sid
, gid_map
.sid
);
476 status
= idmap_tdb_common_sids_to_unixids(dom
, test_maps
);
477 if(!NT_STATUS_IS_OK(status
)) {
478 DEBUG(0, ("test_sids2sunixids1: sids2unixids failed!\n"));
479 talloc_free(test_maps
);
483 if(test_maps
[0]->xid
.id
!=uid_map
.xid
.id
||
484 test_maps
[1]->xid
.id
!=gid_map
.xid
.id
) {
485 DEBUG(0, ("test_sids2unixids1: sid2unixid returned wrong xid!\n"));
486 talloc_free(test_maps
);
490 DEBUG(0, ("test_sids2unixids1: PASSED!\n"));
492 talloc_free(test_maps
);
497 static bool test_sids2unixids2(TALLOC_CTX
*memctx
, struct idmap_domain
*dom
)
500 struct id_map
**test_maps
;
503 test_maps
= talloc_zero_array(memctx
, struct id_map
*, 3);
505 test_maps
[0] = talloc(test_maps
, struct id_map
);
506 test_maps
[1] = talloc(test_maps
, struct id_map
);
509 /* ask for two new mappings for a UID and GID */
510 test_maps
[0]->sid
= dom_sid_parse_talloc(test_maps
, DOM_SID4
"-1003");
511 test_maps
[0]->xid
.type
= ID_TYPE_UID
;
512 test_maps
[1]->sid
= dom_sid_parse_talloc(test_maps
, DOM_SID4
"-1004");
513 test_maps
[1]->xid
.type
= ID_TYPE_GID
;
515 status
= idmap_tdb_common_sids_to_unixids(dom
, test_maps
);
516 if(!NT_STATUS_IS_OK(status
)) {
517 DEBUG(0, ("test_sids2sunixids2: sids2unixids "
518 "failed (%s)!\n", nt_errstr(status
)));
519 talloc_free(test_maps
);
523 if(test_maps
[0]->xid
.id
== 0 || test_maps
[1]->xid
.id
== 0) {
524 DEBUG(0, ("test_sids2sunixids2: sids2unixids "
525 "returned zero ids!\n"));
526 talloc_free(test_maps
);
530 save
= test_maps
[1]->xid
;
532 /* ask for a known and a new mapping at the same time */
533 talloc_free(test_maps
);
534 test_maps
= talloc_zero_array(memctx
, struct id_map
*, 3);
535 test_maps
[0] = talloc(test_maps
, struct id_map
);
536 test_maps
[1] = talloc(test_maps
, struct id_map
);
539 test_maps
[0]->sid
= dom_sid_parse_talloc(test_maps
, DOM_SID4
"-1004");
540 test_maps
[0]->xid
.type
= ID_TYPE_GID
;
541 test_maps
[1]->sid
= dom_sid_parse_talloc(test_maps
, DOM_SID4
"-1005");
542 test_maps
[1]->xid
.type
= ID_TYPE_UID
;
544 status
= idmap_tdb_common_sids_to_unixids(dom
, test_maps
);
545 if(!NT_STATUS_IS_OK(status
)) {
546 DEBUG(0, ("test_sids2sunixids2: sids2unixids (2) "
547 "failed (%s)!\n", nt_errstr(status
)));
548 talloc_free(test_maps
);
552 if(test_maps
[0]->xid
.type
!= save
.type
||
553 test_maps
[0]->xid
.id
!= save
.id
) {
554 DEBUG(0, ("test_sids2sunixids2: second lookup returned "
555 "different value!\n"));
556 talloc_free(test_maps
);
560 if(test_maps
[1]->xid
.id
== 0) {
561 DEBUG(0, ("test_sids2sunixids2: sids2unixids "
562 "returned zero id for mixed mapping request!\n"));
563 talloc_free(test_maps
);
567 DEBUG(0, ("test_sids2unixids2: PASSED!\n"));
569 talloc_free(test_maps
);
574 static bool test_sids2unixids3(TALLOC_CTX
*memctx
, struct idmap_domain
*dom
)
577 struct id_map
**test_maps
;
581 * check the mapping states:
582 * NONE_MAPPED, SOME_UNMAPPED, OK (all mapped)
584 * use the ids created by test_sids2unixids1
585 * need to make dom read-only
588 dom
->read_only
= true;
590 test_maps
= talloc_zero_array(memctx
, struct id_map
*, 3);
592 test_maps
[0] = talloc(test_maps
, struct id_map
);
593 test_maps
[1] = talloc(test_maps
, struct id_map
);
596 /* NONE_MAPPED first */
597 test_maps
[0]->sid
= talloc(test_maps
, struct dom_sid
);
598 test_maps
[1]->sid
= talloc(test_maps
, struct dom_sid
);
599 test_maps
[0]->sid
= dom_sid_parse_talloc(test_maps
,
601 test_maps
[0]->xid
.type
= ID_TYPE_UID
;
603 test_maps
[1]->sid
= dom_sid_parse_talloc(test_maps
,
605 test_maps
[1]->xid
.type
= ID_TYPE_GID
;
607 status
= idmap_tdb_common_sids_to_unixids(dom
, test_maps
);
608 if(!NT_STATUS_EQUAL(status
, NT_STATUS_NONE_MAPPED
)) {
609 DEBUG(0, ("test_sids2unixids3: incorrect status "
610 "(%s), expected NT_STATUS_NONE_MAPPED!\n",
617 test_maps
[0]->sid
= talloc(test_maps
, struct dom_sid
);
618 test_maps
[1]->sid
= talloc(test_maps
, struct dom_sid
);
619 test_maps
[0]->sid
= dom_sid_parse_talloc(test_maps
,
621 test_maps
[0]->xid
.type
= ID_TYPE_UID
;
622 test_maps
[1]->sid
= dom_sid_parse_talloc(test_maps
,
624 test_maps
[1]->xid
.type
= ID_TYPE_GID
;
626 status
= idmap_tdb_common_sids_to_unixids(dom
, test_maps
);
627 if(!NT_STATUS_EQUAL(status
, STATUS_SOME_UNMAPPED
)) {
628 DEBUG(0, ("test_sids2unixids3: incorrect status "
629 "(%s), expected STATUS_SOME_UNMAPPED!\n",
636 test_maps
[0]->sid
= talloc(test_maps
, struct dom_sid
);
637 test_maps
[1]->sid
= talloc(test_maps
, struct dom_sid
);
638 test_maps
[0]->sid
= dom_sid_parse_talloc(test_maps
,
640 test_maps
[1]->sid
= dom_sid_parse_talloc(test_maps
,
643 status
= idmap_tdb_common_sids_to_unixids(dom
, test_maps
);
644 if(!NT_STATUS_IS_OK(status
)) {
645 DEBUG(0, ("test_sids2unixids3: incorrect status "
646 "(%s), expected NT_STATUS_OK!\n",
652 DEBUG(0, ("test_sids2unixids3: PASSED!\n"));
655 talloc_free(test_maps
);
656 dom
->read_only
= false;
660 static bool test_unixid2sid1(TALLOC_CTX
*memctx
, struct idmap_domain
*dom
)
662 NTSTATUS status1
, status2
, status3
;
665 /* check for correct dealing with bad parameters */
666 status1
= idmap_tdb_common_unixid_to_sid(NULL
, &map
);
667 status2
= idmap_tdb_common_unixid_to_sid(dom
, NULL
);
668 status3
= idmap_tdb_common_unixid_to_sid(NULL
, NULL
);
670 if(!NT_STATUS_EQUAL(NT_STATUS_INVALID_PARAMETER
, status1
) ||
671 !NT_STATUS_EQUAL(NT_STATUS_INVALID_PARAMETER
, status2
) ||
672 !NT_STATUS_EQUAL(NT_STATUS_INVALID_PARAMETER
, status3
)) {
673 DEBUG(0, ("test_setmap1: bad parameter handling!\n"));
677 DEBUG(0, ("test_unixid2sid1: PASSED!\n"));
682 static bool test_unixid2sid2(TALLOC_CTX
*memctx
, struct idmap_domain
*dom
)
688 /* ask for mapping that is outside of the range */
689 map
= talloc(memctx
, struct id_map
);
690 map
->sid
= talloc(map
, struct dom_sid
);
692 map
->xid
.type
= ID_TYPE_UID
;
693 map
->xid
.id
= HIGH_ID
+ 1;
695 status
= idmap_tdb_common_unixid_to_sid(dom
, map
);
696 if(NT_STATUS_IS_OK(status
)) {
697 DEBUG(0, ("test_unixid2sid2: unixid2sid returned "
698 "out-of-range result\n"));
703 DEBUG(0, ("test_unixid2sid2: PASSED!\n"));
710 static bool test_unixid2sid3(TALLOC_CTX
*memctx
, struct idmap_domain
*dom
)
713 struct id_map uid_map
, gid_map
, test_map
;
714 struct dom_sid testsid
;
716 ZERO_STRUCT(uid_map
);
717 ZERO_STRUCT(gid_map
);
719 /* create two mappings for a UID and GID */
720 uid_map
.sid
= dom_sid_parse_talloc(memctx
, DOM_SID3
"-1000");
721 uid_map
.xid
.type
= ID_TYPE_UID
;
723 gid_map
.sid
= dom_sid_parse_talloc(memctx
, DOM_SID3
"-1001");
724 gid_map
.xid
.type
= ID_TYPE_GID
;
726 status
= idmap_tdb_common_new_mapping(dom
, &uid_map
);
727 if(!NT_STATUS_IS_OK(status
)) {
728 DEBUG(0, ("test_unixid2sid3: could not create uid map!\n"));
732 status
= idmap_tdb_common_new_mapping(dom
, &gid_map
);
733 if(!NT_STATUS_IS_OK(status
)) {
734 DEBUG(0, ("test_unixid2sid3: could not create gid map!\n"));
738 /* now read them back */
739 ZERO_STRUCT(test_map
);
740 test_map
.xid
.id
= uid_map
.xid
.id
;
741 test_map
.xid
.type
= ID_TYPE_UID
;
742 test_map
.sid
= &testsid
;
744 status
= idmap_tdb_common_unixid_to_sid(dom
, &test_map
);
745 if(!NT_STATUS_IS_OK(status
)) {
746 DEBUG(0, ("test_unixid2sid3: unixid2sid failed for uid!\n"));
750 if(test_map
.xid
.type
!=uid_map
.xid
.type
) {
751 DEBUG(0, ("test_unixid2sid3: unixid2sid returned wrong type!\n"));
755 if(!dom_sid_equal(test_map
.sid
, uid_map
.sid
)) {
756 DEBUG(0, ("test_unixid2sid3: unixid2sid returned wrong SID!\n"));
760 ZERO_STRUCT(test_map
);
761 test_map
.xid
.id
= gid_map
.xid
.id
;
762 test_map
.xid
.type
= ID_TYPE_GID
;
763 test_map
.sid
= &testsid
;
765 status
= idmap_tdb_common_unixid_to_sid(dom
, &test_map
);
766 if(!NT_STATUS_IS_OK(status
)) {
767 DEBUG(0, ("test_unixid2sid3: unixid2sid failed for gid!\n"));
771 if(test_map
.xid
.type
!=gid_map
.xid
.type
) {
772 DEBUG(0, ("test_unixid2sid3: unixid2sid returned wrong type!\n"));
776 if(!dom_sid_equal(test_map
.sid
,gid_map
.sid
)) {
777 DEBUG(0, ("test_unixid2sid3: unixid2sid returned wrong SID!\n"));
781 DEBUG(0, ("test_unixid2sid3: PASSED!\n"));
786 static bool test_unixids2sids1(TALLOC_CTX
*memctx
, struct idmap_domain
*dom
)
789 struct id_map uid_map
, gid_map
, **test_maps
;
791 ZERO_STRUCT(uid_map
);
792 ZERO_STRUCT(gid_map
);
794 /* create two mappings for a UID and GID */
796 uid_map
.sid
= dom_sid_parse_talloc(memctx
, DOM_SID5
"-1000");
797 uid_map
.xid
.type
= ID_TYPE_UID
;
799 gid_map
.sid
= dom_sid_parse_talloc(memctx
, DOM_SID5
"-1001");
800 gid_map
.xid
.type
= ID_TYPE_GID
;
802 status
= idmap_tdb_common_new_mapping(dom
, &uid_map
);
803 if(!NT_STATUS_IS_OK(status
)) {
804 DEBUG(0, ("test_unixids2sids1: could not create uid map!\n"));
808 status
= idmap_tdb_common_new_mapping(dom
, &gid_map
);
809 if(!NT_STATUS_IS_OK(status
)) {
810 DEBUG(0, ("test_unixids2sids1: could not create gid map!\n"));
814 /* now read them back */
815 test_maps
= talloc_zero_array(memctx
, struct id_map
*, 3);
817 test_maps
[0] = talloc(test_maps
, struct id_map
);
818 test_maps
[1] = talloc(test_maps
, struct id_map
);
821 test_maps
[0]->sid
= talloc(test_maps
, struct dom_sid
);
822 test_maps
[1]->sid
= talloc(test_maps
, struct dom_sid
);
823 test_maps
[0]->xid
.id
= uid_map
.xid
.id
;
824 test_maps
[0]->xid
.type
= ID_TYPE_UID
;
825 test_maps
[1]->xid
.id
= gid_map
.xid
.id
;
826 test_maps
[1]->xid
.type
= ID_TYPE_GID
;
828 status
= idmap_tdb_common_unixids_to_sids(dom
, test_maps
);
829 if(!NT_STATUS_IS_OK(status
)) {
830 DEBUG(0, ("test_unixids2sids1: unixids2sids failed!\n"));
831 talloc_free(test_maps
);
835 if(!dom_sid_equal(test_maps
[0]->sid
, uid_map
.sid
) ||
836 !dom_sid_equal(test_maps
[1]->sid
, gid_map
.sid
) ) {
837 DEBUG(0, ("test_unixids2sids1: unixids2sids returned wrong sid!\n"));
838 talloc_free(test_maps
);
842 DEBUG(0, ("test_unixids2sids1: PASSED!\n"));
844 talloc_free(test_maps
);
849 static bool test_unixids2sids2(TALLOC_CTX
*memctx
, struct idmap_domain
*dom
)
852 struct id_map
**test_maps
;
855 test_maps
= talloc_zero_array(memctx
, struct id_map
*, 3);
857 test_maps
[0] = talloc(test_maps
, struct id_map
);
858 test_maps
[1] = talloc(test_maps
, struct id_map
);
861 /* ask for two unknown mappings for a UID and GID */
862 test_maps
[0]->sid
= talloc(test_maps
, struct dom_sid
);
863 test_maps
[1]->sid
= talloc(test_maps
, struct dom_sid
);
864 test_maps
[0]->xid
.id
= HIGH_ID
- 1;
865 test_maps
[0]->xid
.type
= ID_TYPE_UID
;
866 test_maps
[1]->xid
.id
= HIGH_ID
- 1;
867 test_maps
[1]->xid
.type
= ID_TYPE_GID
;
869 status
= idmap_tdb_common_unixids_to_sids(dom
, test_maps
);
870 if(NT_STATUS_IS_OK(status
)) {
871 DEBUG(0, ("test_unixids2sids2: unixids2sids succeeded "
877 DEBUG(0, ("test_unixids2sids2: PASSED!\n"));
880 talloc_free(test_maps
);
885 static bool test_unixids2sids3(TALLOC_CTX
*memctx
, struct idmap_domain
*dom
)
888 struct id_map uid_map
, gid_map
, **test_maps
;
891 ZERO_STRUCT(uid_map
);
892 ZERO_STRUCT(gid_map
);
894 /* create two mappings for a UID and GID */
895 uid_map
.sid
= dom_sid_parse_talloc(memctx
, DOM_SID6
"-1000");
896 uid_map
.xid
.type
= ID_TYPE_UID
;
898 gid_map
.sid
= dom_sid_parse_talloc(memctx
, DOM_SID6
"-1001");
899 gid_map
.xid
.type
= ID_TYPE_GID
;
901 status
= idmap_tdb_common_new_mapping(dom
, &uid_map
);
902 if(!NT_STATUS_IS_OK(status
)) {
903 DEBUG(0, ("test_unixids2sids3: could not create uid map!\n"));
907 status
= idmap_tdb_common_new_mapping(dom
, &gid_map
);
908 if(!NT_STATUS_IS_OK(status
)) {
909 DEBUG(0, ("test_unixids2sids3: could not create gid map!\n"));
914 * check the mapping states:
915 * NONE_MAPPED, SOME_UNMAPPED, OK (all mapped)
917 test_maps
= talloc_zero_array(memctx
, struct id_map
*, 3);
919 test_maps
[0] = talloc(test_maps
, struct id_map
);
920 test_maps
[1] = talloc(test_maps
, struct id_map
);
923 /* NONE_MAPPED first */
924 test_maps
[0]->sid
= talloc(test_maps
, struct dom_sid
);
925 test_maps
[1]->sid
= talloc(test_maps
, struct dom_sid
);
927 test_maps
[0]->xid
.id
= HIGH_ID
- 1;
928 test_maps
[0]->xid
.type
= ID_TYPE_UID
;
930 test_maps
[1]->xid
.id
= HIGH_ID
- 1;
931 test_maps
[1]->xid
.type
= ID_TYPE_GID
;
933 status
= idmap_tdb_common_unixids_to_sids(dom
, test_maps
);
934 if(!NT_STATUS_EQUAL(status
, NT_STATUS_NONE_MAPPED
)) {
935 DEBUG(0, ("test_unixids2sids3: incorrect status "
936 "(%s), expected NT_STATUS_NONE_MAPPED!\n",
943 test_maps
[0]->sid
= talloc(test_maps
, struct dom_sid
);
944 test_maps
[1]->sid
= talloc(test_maps
, struct dom_sid
);
945 test_maps
[0]->xid
= uid_map
.xid
;
946 test_maps
[1]->xid
.id
= HIGH_ID
- 1;
947 test_maps
[1]->xid
.type
= ID_TYPE_GID
;
949 status
= idmap_tdb_common_unixids_to_sids(dom
, test_maps
);
950 if(!NT_STATUS_EQUAL(status
, STATUS_SOME_UNMAPPED
)) {
951 DEBUG(0, ("test_unixids2sids3: incorrect status "
952 "(%s), expected STATUS_SOME_UNMAPPED!\n",
959 test_maps
[0]->sid
= talloc(test_maps
, struct dom_sid
);
960 test_maps
[1]->sid
= talloc(test_maps
, struct dom_sid
);
961 test_maps
[0]->xid
= uid_map
.xid
;
962 test_maps
[1]->xid
= gid_map
.xid
;
964 status
= idmap_tdb_common_unixids_to_sids(dom
, test_maps
);
965 if(!NT_STATUS_IS_OK(status
)) {
966 DEBUG(0, ("test_unixids2sids3: incorrect status "
967 "(%s), expected NT_STATUS_OK!\n",
973 DEBUG(0, ("test_unixids2sids3: PASSED!\n"));
976 talloc_free(test_maps
);
980 #define CHECKRESULT(r) if(!r) {return r;}
982 bool run_idmap_tdb_common_test(int dummy
)
985 struct idmap_domain
*dom
;
986 TALLOC_CTX
*stack
= talloc_stackframe();
987 TALLOC_CTX
*memctx
= talloc_new(stack
);
990 dom
= createdomain(memctx
);
995 status
= dom
->methods
->init(dom
);
996 if (!NT_STATUS_IS_OK(status
)) {
1000 /* test a single allocation from pool (no mapping) */
1001 result
= test_getnewid1(memctx
, dom
);
1002 CHECKRESULT(result
);
1004 /* test idmap_tdb_common_set_mapping */
1005 result
= test_setmap1(memctx
, dom
);
1006 CHECKRESULT(result
);
1008 /* test idmap_tdb_common_sid_to_unixid */
1009 result
= test_sid2unixid1(memctx
, dom
);
1010 CHECKRESULT(result
);
1011 result
= test_sid2unixid2(memctx
, dom
);
1012 CHECKRESULT(result
);
1014 /* test idmap_tdb_common_sids_to_unixids */
1015 result
= test_sids2unixids1(memctx
, dom
);
1016 CHECKRESULT(result
);
1017 result
= test_sids2unixids2(memctx
, dom
);
1018 CHECKRESULT(result
);
1019 result
= test_sids2unixids3(memctx
, dom
);
1020 CHECKRESULT(result
);
1022 /* test idmap_tdb_common_unixid_to_sid */
1023 result
= test_unixid2sid1(memctx
, dom
);
1024 CHECKRESULT(result
);
1025 result
= test_unixid2sid2(memctx
, dom
);
1026 CHECKRESULT(result
);
1027 result
= test_unixid2sid3(memctx
, dom
);
1028 CHECKRESULT(result
);
1030 /* test idmap_tdb_common_unixids_to_sids */
1031 result
= test_unixids2sids1(memctx
, dom
);
1032 CHECKRESULT(result
);
1033 result
= test_unixids2sids2(memctx
, dom
);
1034 CHECKRESULT(result
);
1035 result
= test_unixids2sids3(memctx
, dom
);
1036 CHECKRESULT(result
);
1038 /* test filling up the range */
1039 result
= test_getnewid2(memctx
, dom
);
1040 CHECKRESULT(result
);