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_sid_to_unixid(const char *domain
, struct id_map
*id
)
70 NTSTATUS
idmap_backends_unixid_to_sid(const char *domname
, struct id_map
*id
)
75 static bool open_db(struct idmap_tdb_common_context
*ctx
)
85 db_path
= talloc_asprintf(talloc_tos(), "%s/idmap_test.tdb",
88 DEBUG(0, ("Out of memory!\n"));
92 ctx
->db
= db_open(ctx
, db_path
, 0, TDB_DEFAULT
| TDB_CLEAR_IF_FIRST
,
93 O_RDWR
| O_CREAT
, 0600,
97 DEBUG(0, ("Failed to open database: %s\n", strerror(errno
)));
101 if(dbwrap_transaction_start(ctx
->db
) != 0) {
102 DEBUG(0, ("Failed to start transaction!\n"));
106 status
= dbwrap_store_uint32(ctx
->db
, ctx
->hwmkey_uid
, LOW_ID
);
107 if(!NT_STATUS_IS_OK(status
)) {
108 dbwrap_transaction_cancel(ctx
->db
);
112 status
= dbwrap_store_uint32(ctx
->db
, ctx
->hwmkey_gid
, LOW_ID
);
113 if(!NT_STATUS_IS_OK(status
)) {
114 dbwrap_transaction_cancel(ctx
->db
);
118 if(dbwrap_transaction_commit(ctx
->db
) != 0) {
119 DEBUG(0, ("Failed to commit transaction!\n"));
126 static struct idmap_tdb_common_context
*createcontext(TALLOC_CTX
*memctx
)
128 struct idmap_tdb_common_context
*ret
;
130 ret
= talloc_zero(memctx
, struct idmap_tdb_common_context
);
131 ret
->rw_ops
= talloc_zero(ret
, struct idmap_rw_ops
);
133 ret
->max_id
= HIGH_ID
;
134 ret
->hwmkey_uid
= HWM_USER
;
135 ret
->hwmkey_gid
= HWM_GROUP
;
137 ret
->rw_ops
->get_new_id
= idmap_tdb_common_get_new_id
;
138 ret
->rw_ops
->set_mapping
= idmap_tdb_common_set_mapping
;
147 static struct idmap_domain
*createdomain(TALLOC_CTX
*memctx
)
149 struct idmap_domain
*dom
;
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 dom
->methods
= talloc_zero(dom
, struct idmap_methods
);
157 dom
->methods
->sids_to_unixids
= idmap_tdb_common_sids_to_unixids
;
158 dom
->methods
->unixids_to_sids
= idmap_tdb_common_unixids_to_sids
;
159 dom
->methods
->allocate_id
= idmap_tdb_common_get_new_id
;
164 static bool test_getnewid1(TALLOC_CTX
*memctx
, struct idmap_domain
*dom
)
169 id
.type
= ID_TYPE_UID
;
171 status
= idmap_tdb_common_get_new_id(dom
, &id
);
173 if(!NT_STATUS_IS_OK(status
)) {
174 DEBUG(0, ("test_getnewid1: Could not allocate id!\n"));
179 DEBUG(0, ("test_getnewid1: Allocate returned "
184 if(id
.id
> HIGH_ID
|| id
.id
< LOW_ID
) {
185 DEBUG(0, ("test_getnewid1: Allocate returned "
186 "out of range id!\n"));
190 DEBUG(0, ("test_getnewid1: PASSED!\n"));
195 static bool test_getnewid2(TALLOC_CTX
*memctx
, struct idmap_domain
*dom
)
201 id
.type
= ID_TYPE_UID
;
203 status
= idmap_tdb_common_get_new_id(dom
, &id
);
205 if(!NT_STATUS_IS_OK(status
)) {
206 DEBUG(0, ("test_getnewid2: Could not allocate id!\n"));
211 DEBUG(0, ("test_getnewid2: Allocate returned "
216 if(id
.id
> HIGH_ID
|| id
.id
< LOW_ID
) {
217 DEBUG(0, ("test_getnewid2: Allocate returned "
218 "out of range id!\n"));
222 /* how many ids are left? */
224 left
= HIGH_ID
- id
.id
;
226 /* consume them all */
227 for(i
= 0; i
<left
; i
++) {
229 status
= idmap_tdb_common_get_new_id(dom
, &id
);
231 if(!NT_STATUS_IS_OK(status
)) {
232 DEBUG(0, ("test_getnewid2: Allocate returned "
233 "error %s\n", nt_errstr(status
)));
237 if(id
.id
> HIGH_ID
) {
238 DEBUG(0, ("test_getnewid2: Allocate returned "
239 "out of range id (%d)!\n", id
.id
));
244 /* one more must fail */
245 status
= idmap_tdb_common_get_new_id(dom
, &id
);
247 if(NT_STATUS_IS_OK(status
)) {
248 DEBUG(0, ("test_getnewid2: Could allocate id (%d) from "
249 "depleted pool!\n", id
.id
));
253 DEBUG(0, ("test_getnewid2: PASSED!\n"));
258 static bool test_setmap1(TALLOC_CTX
*memctx
, struct idmap_domain
*dom
)
265 /* test for correct return code with invalid data */
267 status
= idmap_tdb_common_set_mapping(dom
, NULL
);
268 if(!NT_STATUS_EQUAL(status
, NT_STATUS_INVALID_PARAMETER
)) {
269 DEBUG(0, ("test_setmap1: bad parameter handling!\n"));
273 status
= idmap_tdb_common_set_mapping(dom
, &map
);
274 if(!NT_STATUS_EQUAL(status
, NT_STATUS_INVALID_PARAMETER
)) {
275 DEBUG(0, ("test_setmap1: bad parameter handling!\n"));
279 map
.sid
= dom_sid_parse_talloc(memctx
, DOM_SID1
"-100");
281 map
.xid
.type
= ID_TYPE_NOT_SPECIFIED
;
284 status
= idmap_tdb_common_set_mapping(dom
, &map
);
285 if(!NT_STATUS_EQUAL(status
, NT_STATUS_INVALID_PARAMETER
)) {
286 DEBUG(0, ("test_setmap1: bad parameter handling!\n"));
290 /* now the good ones */
291 map
.xid
.type
= ID_TYPE_UID
;
294 status
= idmap_tdb_common_get_new_id(dom
, &(map
.xid
));
295 if(!NT_STATUS_IS_OK(status
)) {
296 DEBUG(0, ("test_setmap1: get_new_uid failed!\n"));
300 status
= idmap_tdb_common_set_mapping(dom
, &map
);
301 if(!NT_STATUS_IS_OK(status
)) {
302 DEBUG(0, ("test_setmap1: setting UID mapping failed!\n"));
306 /* try to set the same mapping again as group (must fail) */
308 map
.xid
.type
= ID_TYPE_GID
;
309 status
= idmap_tdb_common_set_mapping(dom
, &map
);
310 if(NT_STATUS_IS_OK(status
)) {
311 DEBUG(0, ("test_setmap1: could create map for "
312 "group and user!\n"));
316 /* now a group with a different SID*/
319 map
.sid
= dom_sid_parse_talloc(memctx
, DOM_SID1
"-101");
321 status
= idmap_tdb_common_get_new_id(dom
, &(map
.xid
));
322 if(!NT_STATUS_IS_OK(status
)) {
323 DEBUG(0, ("test_setmap1: get_new_gid failed!\n"));
327 status
= idmap_tdb_common_set_mapping(dom
, &map
);
328 if(!NT_STATUS_IS_OK(status
)) {
329 DEBUG(0, ("test_setmap1: setting GID mapping failed!\n"));
332 DEBUG(0, ("test_setmap1: PASSED!\n"));
337 static bool test_sid2unixid1(TALLOC_CTX
*memctx
, struct idmap_domain
*dom
)
339 NTSTATUS status1
, status2
, status3
;
342 /* check for correct dealing with bad parameters */
343 status1
= idmap_tdb_common_sid_to_unixid(NULL
, &map
);
344 status2
= idmap_tdb_common_sid_to_unixid(dom
, NULL
);
345 status3
= idmap_tdb_common_sid_to_unixid(NULL
, NULL
);
347 if(!NT_STATUS_EQUAL(NT_STATUS_INVALID_PARAMETER
, status1
) ||
348 !NT_STATUS_EQUAL(NT_STATUS_INVALID_PARAMETER
, status2
) ||
349 !NT_STATUS_EQUAL(NT_STATUS_INVALID_PARAMETER
, status3
)) {
350 DEBUG(0, ("test_setmap1: bad parameter handling!\n"));
354 DEBUG(0, ("test_unixid2sid1: PASSED!\n"));
359 static bool test_sid2unixid2(TALLOC_CTX
*memctx
, struct idmap_domain
*dom
)
362 struct id_map uid_map
, gid_map
, test_map
;
365 ZERO_STRUCT(uid_map
);
366 ZERO_STRUCT(gid_map
);
368 /* create two mappings for a UID and GID */
372 uid_map
.sid
= dom_sid_parse_talloc(memctx
, DOM_SID2
"-1000");
373 uid_map
.xid
.type
= ID_TYPE_UID
;
375 gid_map
.sid
= dom_sid_parse_talloc(memctx
, DOM_SID2
"-1001");
376 gid_map
.xid
.type
= ID_TYPE_GID
;
378 status
= idmap_tdb_common_new_mapping(dom
, &uid_map
);
379 if(!NT_STATUS_IS_OK(status
)) {
380 DEBUG(0, ("test_sid2unixid1: could not create uid map!\n"));
384 status
= idmap_tdb_common_new_mapping(dom
, &gid_map
);
385 if(!NT_STATUS_IS_OK(status
)) {
386 DEBUG(0, ("test_sid2unixid1: could not create gid map!\n"));
390 /* now read them back */
391 ZERO_STRUCT(test_map
);
392 test_map
.sid
= uid_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 uid!\n"));
400 if(test_map
.xid
.id
!=uid_map
.xid
.id
) {
401 DEBUG(0, ("test_sid2unixid1: sid2unixid returned wrong uid!\n"));
405 test_map
.sid
= gid_map
.sid
;
407 status
= idmap_tdb_common_sid_to_unixid(dom
, &test_map
);
408 if(!NT_STATUS_IS_OK(status
)) {
409 DEBUG(0, ("test_sid2unixid1: sid2unixid failed for gid!\n"));
413 if(test_map
.xid
.id
!=gid_map
.xid
.id
) {
414 DEBUG(0, ("test_sid2unixid1: sid2unixid returned wrong gid!\n"));
419 * Go through the same tests again once to see if trying to recreate
420 * a mapping that was already created will work or not
427 DEBUG(0, ("test_sid2unixid1: PASSED!\n"));
432 static bool test_sids2unixids1(TALLOC_CTX
*memctx
, struct idmap_domain
*dom
)
435 struct id_map uid_map
, gid_map
, **test_maps
;
437 ZERO_STRUCT(uid_map
);
438 ZERO_STRUCT(gid_map
);
440 /* create two mappings for a UID and GID */
442 uid_map
.sid
= dom_sid_parse_talloc(memctx
, DOM_SID4
"-1000");
443 uid_map
.xid
.type
= ID_TYPE_UID
;
445 gid_map
.sid
= dom_sid_parse_talloc(memctx
, DOM_SID4
"-1001");
446 gid_map
.xid
.type
= ID_TYPE_GID
;
448 status
= idmap_tdb_common_new_mapping(dom
, &uid_map
);
449 if(!NT_STATUS_IS_OK(status
)) {
450 DEBUG(0, ("test_sids2unixids1: could not create uid map!\n"));
454 status
= idmap_tdb_common_new_mapping(dom
, &gid_map
);
455 if(!NT_STATUS_IS_OK(status
)) {
456 DEBUG(0, ("test_sids2unixids1: could not create gid map!\n"));
460 /* now read them back */
461 test_maps
= talloc_zero_array(memctx
, struct id_map
*, 3);
463 test_maps
[0] = talloc(test_maps
, struct id_map
);
464 test_maps
[1] = talloc(test_maps
, struct id_map
);
467 test_maps
[0]->sid
= talloc(test_maps
, struct dom_sid
);
468 test_maps
[1]->sid
= talloc(test_maps
, struct dom_sid
);
469 sid_copy(test_maps
[0]->sid
, uid_map
.sid
);
470 sid_copy(test_maps
[1]->sid
, gid_map
.sid
);
472 status
= idmap_tdb_common_sids_to_unixids(dom
, test_maps
);
473 if(!NT_STATUS_IS_OK(status
)) {
474 DEBUG(0, ("test_sids2sunixids1: sids2unixids failed!\n"));
475 talloc_free(test_maps
);
479 if(test_maps
[0]->xid
.id
!=uid_map
.xid
.id
||
480 test_maps
[1]->xid
.id
!=gid_map
.xid
.id
) {
481 DEBUG(0, ("test_sids2unixids1: sid2unixid returned wrong xid!\n"));
482 talloc_free(test_maps
);
486 DEBUG(0, ("test_sids2unixids1: PASSED!\n"));
488 talloc_free(test_maps
);
493 static bool test_sids2unixids2(TALLOC_CTX
*memctx
, struct idmap_domain
*dom
)
496 struct id_map
**test_maps
;
499 test_maps
= talloc_zero_array(memctx
, struct id_map
*, 3);
501 test_maps
[0] = talloc(test_maps
, struct id_map
);
502 test_maps
[1] = talloc(test_maps
, struct id_map
);
505 /* ask for two new mappings for a UID and GID */
506 test_maps
[0]->sid
= dom_sid_parse_talloc(test_maps
, DOM_SID4
"-1003");
507 test_maps
[0]->xid
.type
= ID_TYPE_UID
;
508 test_maps
[1]->sid
= dom_sid_parse_talloc(test_maps
, DOM_SID4
"-1004");
509 test_maps
[1]->xid
.type
= ID_TYPE_GID
;
511 status
= idmap_tdb_common_sids_to_unixids(dom
, test_maps
);
512 if(!NT_STATUS_IS_OK(status
)) {
513 DEBUG(0, ("test_sids2sunixids2: sids2unixids "
514 "failed (%s)!\n", nt_errstr(status
)));
515 talloc_free(test_maps
);
519 if(test_maps
[0]->xid
.id
== 0 || test_maps
[1]->xid
.id
== 0) {
520 DEBUG(0, ("test_sids2sunixids2: sids2unixids "
521 "returned zero ids!\n"));
522 talloc_free(test_maps
);
526 save
= test_maps
[1]->xid
;
528 /* ask for a known and a new mapping at the same time */
529 talloc_free(test_maps
);
530 test_maps
= talloc_zero_array(memctx
, struct id_map
*, 3);
531 test_maps
[0] = talloc(test_maps
, struct id_map
);
532 test_maps
[1] = talloc(test_maps
, struct id_map
);
535 test_maps
[0]->sid
= dom_sid_parse_talloc(test_maps
, DOM_SID4
"-1004");
536 test_maps
[0]->xid
.type
= ID_TYPE_GID
;
537 test_maps
[1]->sid
= dom_sid_parse_talloc(test_maps
, DOM_SID4
"-1005");
538 test_maps
[1]->xid
.type
= ID_TYPE_UID
;
540 status
= idmap_tdb_common_sids_to_unixids(dom
, test_maps
);
541 if(!NT_STATUS_IS_OK(status
)) {
542 DEBUG(0, ("test_sids2sunixids2: sids2unixids (2) "
543 "failed (%s)!\n", nt_errstr(status
)));
544 talloc_free(test_maps
);
548 if(test_maps
[0]->xid
.type
!= save
.type
||
549 test_maps
[0]->xid
.id
!= save
.id
) {
550 DEBUG(0, ("test_sids2sunixids2: second lookup returned "
551 "different value!\n"));
552 talloc_free(test_maps
);
556 if(test_maps
[1]->xid
.id
== 0) {
557 DEBUG(0, ("test_sids2sunixids2: sids2unixids "
558 "returned zero id for mixed mapping request!\n"));
559 talloc_free(test_maps
);
563 DEBUG(0, ("test_sids2unixids2: PASSED!\n"));
565 talloc_free(test_maps
);
570 static bool test_sids2unixids3(TALLOC_CTX
*memctx
, struct idmap_domain
*dom
)
573 struct id_map
**test_maps
;
577 * check the mapping states:
578 * NONE_MAPPED, SOME_UNMAPPED, OK (all mapped)
580 * use the ids created by test_sids2unixids1
581 * need to make dom read-only
584 dom
->read_only
= true;
586 test_maps
= talloc_zero_array(memctx
, struct id_map
*, 3);
588 test_maps
[0] = talloc(test_maps
, struct id_map
);
589 test_maps
[1] = talloc(test_maps
, struct id_map
);
592 /* NONE_MAPPED first */
593 test_maps
[0]->sid
= talloc(test_maps
, struct dom_sid
);
594 test_maps
[1]->sid
= talloc(test_maps
, struct dom_sid
);
595 test_maps
[0]->sid
= dom_sid_parse_talloc(test_maps
,
597 test_maps
[0]->xid
.type
= ID_TYPE_UID
;
599 test_maps
[1]->sid
= dom_sid_parse_talloc(test_maps
,
601 test_maps
[1]->xid
.type
= ID_TYPE_GID
;
603 status
= idmap_tdb_common_sids_to_unixids(dom
, test_maps
);
604 if(!NT_STATUS_EQUAL(status
, NT_STATUS_NONE_MAPPED
)) {
605 DEBUG(0, ("test_sids2unixids3: incorrect status "
606 "(%s), expected NT_STATUS_NONE_MAPPED!\n",
613 test_maps
[0]->sid
= talloc(test_maps
, struct dom_sid
);
614 test_maps
[1]->sid
= talloc(test_maps
, struct dom_sid
);
615 test_maps
[0]->sid
= dom_sid_parse_talloc(test_maps
,
617 test_maps
[0]->xid
.type
= ID_TYPE_UID
;
618 test_maps
[1]->sid
= dom_sid_parse_talloc(test_maps
,
620 test_maps
[1]->xid
.type
= ID_TYPE_GID
;
622 status
= idmap_tdb_common_sids_to_unixids(dom
, test_maps
);
623 if(!NT_STATUS_EQUAL(status
, STATUS_SOME_UNMAPPED
)) {
624 DEBUG(0, ("test_sids2unixids3: incorrect status "
625 "(%s), expected STATUS_SOME_UNMAPPED!\n",
632 test_maps
[0]->sid
= talloc(test_maps
, struct dom_sid
);
633 test_maps
[1]->sid
= talloc(test_maps
, struct dom_sid
);
634 test_maps
[0]->sid
= dom_sid_parse_talloc(test_maps
,
636 test_maps
[1]->sid
= dom_sid_parse_talloc(test_maps
,
639 status
= idmap_tdb_common_sids_to_unixids(dom
, test_maps
);
640 if(!NT_STATUS_IS_OK(status
)) {
641 DEBUG(0, ("test_sids2unixids3: incorrect status "
642 "(%s), expected NT_STATUS_OK!\n",
648 DEBUG(0, ("test_sids2unixids3: PASSED!\n"));
651 talloc_free(test_maps
);
652 dom
->read_only
= false;
656 static bool test_unixid2sid1(TALLOC_CTX
*memctx
, struct idmap_domain
*dom
)
658 NTSTATUS status1
, status2
, status3
;
661 /* check for correct dealing with bad parameters */
662 status1
= idmap_tdb_common_unixid_to_sid(NULL
, &map
);
663 status2
= idmap_tdb_common_unixid_to_sid(dom
, NULL
);
664 status3
= idmap_tdb_common_unixid_to_sid(NULL
, NULL
);
666 if(!NT_STATUS_EQUAL(NT_STATUS_INVALID_PARAMETER
, status1
) ||
667 !NT_STATUS_EQUAL(NT_STATUS_INVALID_PARAMETER
, status2
) ||
668 !NT_STATUS_EQUAL(NT_STATUS_INVALID_PARAMETER
, status3
)) {
669 DEBUG(0, ("test_setmap1: bad parameter handling!\n"));
673 DEBUG(0, ("test_unixid2sid1: PASSED!\n"));
678 static bool test_unixid2sid2(TALLOC_CTX
*memctx
, struct idmap_domain
*dom
)
684 /* ask for mapping that is outside of the range */
685 map
= talloc(memctx
, struct id_map
);
686 map
->sid
= talloc(map
, struct dom_sid
);
688 map
->xid
.type
= ID_TYPE_UID
;
689 map
->xid
.id
= HIGH_ID
+ 1;
691 status
= idmap_tdb_common_unixid_to_sid(dom
, map
);
692 if(NT_STATUS_IS_OK(status
)) {
693 DEBUG(0, ("test_unixid2sid2: unixid2sid returned "
694 "out-of-range result\n"));
699 DEBUG(0, ("test_unixid2sid2: PASSED!\n"));
706 static bool test_unixid2sid3(TALLOC_CTX
*memctx
, struct idmap_domain
*dom
)
709 struct id_map uid_map
, gid_map
, test_map
;
710 struct dom_sid testsid
;
712 ZERO_STRUCT(uid_map
);
713 ZERO_STRUCT(gid_map
);
715 /* create two mappings for a UID and GID */
716 uid_map
.sid
= dom_sid_parse_talloc(memctx
, DOM_SID3
"-1000");
717 uid_map
.xid
.type
= ID_TYPE_UID
;
719 gid_map
.sid
= dom_sid_parse_talloc(memctx
, DOM_SID3
"-1001");
720 gid_map
.xid
.type
= ID_TYPE_GID
;
722 status
= idmap_tdb_common_new_mapping(dom
, &uid_map
);
723 if(!NT_STATUS_IS_OK(status
)) {
724 DEBUG(0, ("test_unixid2sid3: could not create uid map!\n"));
728 status
= idmap_tdb_common_new_mapping(dom
, &gid_map
);
729 if(!NT_STATUS_IS_OK(status
)) {
730 DEBUG(0, ("test_unixid2sid3: could not create gid map!\n"));
734 /* now read them back */
735 ZERO_STRUCT(test_map
);
736 test_map
.xid
.id
= uid_map
.xid
.id
;
737 test_map
.xid
.type
= ID_TYPE_UID
;
738 test_map
.sid
= &testsid
;
740 status
= idmap_tdb_common_unixid_to_sid(dom
, &test_map
);
741 if(!NT_STATUS_IS_OK(status
)) {
742 DEBUG(0, ("test_unixid2sid3: unixid2sid failed for uid!\n"));
746 if(test_map
.xid
.type
!=uid_map
.xid
.type
) {
747 DEBUG(0, ("test_unixid2sid3: unixid2sid returned wrong type!\n"));
751 if(!dom_sid_equal(test_map
.sid
, uid_map
.sid
)) {
752 DEBUG(0, ("test_unixid2sid3: unixid2sid returned wrong SID!\n"));
756 ZERO_STRUCT(test_map
);
757 test_map
.xid
.id
= gid_map
.xid
.id
;
758 test_map
.xid
.type
= ID_TYPE_GID
;
759 test_map
.sid
= &testsid
;
761 status
= idmap_tdb_common_unixid_to_sid(dom
, &test_map
);
762 if(!NT_STATUS_IS_OK(status
)) {
763 DEBUG(0, ("test_unixid2sid3: unixid2sid failed for gid!\n"));
767 if(test_map
.xid
.type
!=gid_map
.xid
.type
) {
768 DEBUG(0, ("test_unixid2sid3: unixid2sid returned wrong type!\n"));
772 if(!dom_sid_equal(test_map
.sid
,gid_map
.sid
)) {
773 DEBUG(0, ("test_unixid2sid3: unixid2sid returned wrong SID!\n"));
777 DEBUG(0, ("test_unixid2sid3: PASSED!\n"));
782 static bool test_unixids2sids1(TALLOC_CTX
*memctx
, struct idmap_domain
*dom
)
785 struct id_map uid_map
, gid_map
, **test_maps
;
787 ZERO_STRUCT(uid_map
);
788 ZERO_STRUCT(gid_map
);
790 /* create two mappings for a UID and GID */
792 uid_map
.sid
= dom_sid_parse_talloc(memctx
, DOM_SID5
"-1000");
793 uid_map
.xid
.type
= ID_TYPE_UID
;
795 gid_map
.sid
= dom_sid_parse_talloc(memctx
, DOM_SID5
"-1001");
796 gid_map
.xid
.type
= ID_TYPE_GID
;
798 status
= idmap_tdb_common_new_mapping(dom
, &uid_map
);
799 if(!NT_STATUS_IS_OK(status
)) {
800 DEBUG(0, ("test_unixids2sids1: could not create uid map!\n"));
804 status
= idmap_tdb_common_new_mapping(dom
, &gid_map
);
805 if(!NT_STATUS_IS_OK(status
)) {
806 DEBUG(0, ("test_unixids2sids1: could not create gid map!\n"));
810 /* now read them back */
811 test_maps
= talloc_zero_array(memctx
, struct id_map
*, 3);
813 test_maps
[0] = talloc(test_maps
, struct id_map
);
814 test_maps
[1] = talloc(test_maps
, struct id_map
);
817 test_maps
[0]->sid
= talloc(test_maps
, struct dom_sid
);
818 test_maps
[1]->sid
= talloc(test_maps
, struct dom_sid
);
819 test_maps
[0]->xid
.id
= uid_map
.xid
.id
;
820 test_maps
[0]->xid
.type
= ID_TYPE_UID
;
821 test_maps
[1]->xid
.id
= gid_map
.xid
.id
;
822 test_maps
[1]->xid
.type
= ID_TYPE_GID
;
824 status
= idmap_tdb_common_unixids_to_sids(dom
, test_maps
);
825 if(!NT_STATUS_IS_OK(status
)) {
826 DEBUG(0, ("test_unixids2sids1: unixids2sids failed!\n"));
827 talloc_free(test_maps
);
831 if(!dom_sid_equal(test_maps
[0]->sid
, uid_map
.sid
) ||
832 !dom_sid_equal(test_maps
[1]->sid
, gid_map
.sid
) ) {
833 DEBUG(0, ("test_unixids2sids1: unixids2sids returned wrong sid!\n"));
834 talloc_free(test_maps
);
838 DEBUG(0, ("test_unixids2sids1: PASSED!\n"));
840 talloc_free(test_maps
);
845 static bool test_unixids2sids2(TALLOC_CTX
*memctx
, struct idmap_domain
*dom
)
848 struct id_map
**test_maps
;
851 test_maps
= talloc_zero_array(memctx
, struct id_map
*, 3);
853 test_maps
[0] = talloc(test_maps
, struct id_map
);
854 test_maps
[1] = talloc(test_maps
, struct id_map
);
857 /* ask for two unknown mappings for a UID and GID */
858 test_maps
[0]->sid
= talloc(test_maps
, struct dom_sid
);
859 test_maps
[1]->sid
= talloc(test_maps
, struct dom_sid
);
860 test_maps
[0]->xid
.id
= HIGH_ID
- 1;
861 test_maps
[0]->xid
.type
= ID_TYPE_UID
;
862 test_maps
[1]->xid
.id
= HIGH_ID
- 1;
863 test_maps
[1]->xid
.type
= ID_TYPE_GID
;
865 status
= idmap_tdb_common_unixids_to_sids(dom
, test_maps
);
866 if(NT_STATUS_IS_OK(status
)) {
867 DEBUG(0, ("test_unixids2sids2: unixids2sids succeeded "
873 DEBUG(0, ("test_unixids2sids2: PASSED!\n"));
876 talloc_free(test_maps
);
881 static bool test_unixids2sids3(TALLOC_CTX
*memctx
, struct idmap_domain
*dom
)
884 struct id_map uid_map
, gid_map
, **test_maps
;
887 ZERO_STRUCT(uid_map
);
888 ZERO_STRUCT(gid_map
);
890 /* create two mappings for a UID and GID */
891 uid_map
.sid
= dom_sid_parse_talloc(memctx
, DOM_SID6
"-1000");
892 uid_map
.xid
.type
= ID_TYPE_UID
;
894 gid_map
.sid
= dom_sid_parse_talloc(memctx
, DOM_SID6
"-1001");
895 gid_map
.xid
.type
= ID_TYPE_GID
;
897 status
= idmap_tdb_common_new_mapping(dom
, &uid_map
);
898 if(!NT_STATUS_IS_OK(status
)) {
899 DEBUG(0, ("test_unixids2sids3: could not create uid map!\n"));
903 status
= idmap_tdb_common_new_mapping(dom
, &gid_map
);
904 if(!NT_STATUS_IS_OK(status
)) {
905 DEBUG(0, ("test_unixids2sids3: could not create gid map!\n"));
910 * check the mapping states:
911 * NONE_MAPPED, SOME_UNMAPPED, OK (all mapped)
913 test_maps
= talloc_zero_array(memctx
, struct id_map
*, 3);
915 test_maps
[0] = talloc(test_maps
, struct id_map
);
916 test_maps
[1] = talloc(test_maps
, struct id_map
);
919 /* NONE_MAPPED first */
920 test_maps
[0]->sid
= talloc(test_maps
, struct dom_sid
);
921 test_maps
[1]->sid
= talloc(test_maps
, struct dom_sid
);
923 test_maps
[0]->xid
.id
= HIGH_ID
- 1;
924 test_maps
[0]->xid
.type
= ID_TYPE_UID
;
926 test_maps
[1]->xid
.id
= HIGH_ID
- 1;
927 test_maps
[1]->xid
.type
= ID_TYPE_GID
;
929 status
= idmap_tdb_common_unixids_to_sids(dom
, test_maps
);
930 if(!NT_STATUS_EQUAL(status
, NT_STATUS_NONE_MAPPED
)) {
931 DEBUG(0, ("test_unixids2sids3: incorrect status "
932 "(%s), expected NT_STATUS_NONE_MAPPED!\n",
939 test_maps
[0]->sid
= talloc(test_maps
, struct dom_sid
);
940 test_maps
[1]->sid
= talloc(test_maps
, struct dom_sid
);
941 test_maps
[0]->xid
= uid_map
.xid
;
942 test_maps
[1]->xid
.id
= HIGH_ID
- 1;
943 test_maps
[1]->xid
.type
= ID_TYPE_GID
;
945 status
= idmap_tdb_common_unixids_to_sids(dom
, test_maps
);
946 if(!NT_STATUS_EQUAL(status
, STATUS_SOME_UNMAPPED
)) {
947 DEBUG(0, ("test_unixids2sids3: incorrect status "
948 "(%s), expected STATUS_SOME_UNMAPPED!\n",
955 test_maps
[0]->sid
= talloc(test_maps
, struct dom_sid
);
956 test_maps
[1]->sid
= talloc(test_maps
, struct dom_sid
);
957 test_maps
[0]->xid
= uid_map
.xid
;
958 test_maps
[1]->xid
= gid_map
.xid
;
960 status
= idmap_tdb_common_unixids_to_sids(dom
, test_maps
);
961 if(!NT_STATUS_IS_OK(status
)) {
962 DEBUG(0, ("test_unixids2sids3: incorrect status "
963 "(%s), expected NT_STATUS_OK!\n",
969 DEBUG(0, ("test_unixids2sids3: PASSED!\n"));
972 talloc_free(test_maps
);
976 #define CHECKRESULT(r) if(!r) {return r;}
978 bool run_idmap_tdb_common_test(int dummy
)
981 struct idmap_tdb_common_context
*ctx
;
982 struct idmap_domain
*dom
;
984 TALLOC_CTX
*memctx
= talloc_new(NULL
);
985 TALLOC_CTX
*stack
= talloc_stackframe();
987 ctx
= createcontext(memctx
);
992 dom
= createdomain(memctx
);
994 dom
->private_data
= ctx
;
996 /* test a single allocation from pool (no mapping) */
997 result
= test_getnewid1(memctx
, dom
);
1000 /* test idmap_tdb_common_set_mapping */
1001 result
= test_setmap1(memctx
, dom
);
1002 CHECKRESULT(result
);
1004 /* test idmap_tdb_common_sid_to_unixid */
1005 result
= test_sid2unixid1(memctx
, dom
);
1006 CHECKRESULT(result
);
1007 result
= test_sid2unixid2(memctx
, dom
);
1008 CHECKRESULT(result
);
1010 /* test idmap_tdb_common_sids_to_unixids */
1011 result
= test_sids2unixids1(memctx
, dom
);
1012 CHECKRESULT(result
);
1013 result
= test_sids2unixids2(memctx
, dom
);
1014 CHECKRESULT(result
);
1015 result
= test_sids2unixids3(memctx
, dom
);
1016 CHECKRESULT(result
);
1018 /* test idmap_tdb_common_unixid_to_sid */
1019 result
= test_unixid2sid1(memctx
, dom
);
1020 CHECKRESULT(result
);
1021 result
= test_unixid2sid2(memctx
, dom
);
1022 CHECKRESULT(result
);
1023 result
= test_unixid2sid3(memctx
, dom
);
1024 CHECKRESULT(result
);
1026 /* test idmap_tdb_common_unixids_to_sids */
1027 result
= test_unixids2sids1(memctx
, dom
);
1028 CHECKRESULT(result
);
1029 result
= test_unixids2sids2(memctx
, dom
);
1030 CHECKRESULT(result
);
1031 result
= test_unixids2sids3(memctx
, dom
);
1032 CHECKRESULT(result
);
1034 /* test filling up the range */
1035 result
= test_getnewid2(memctx
, dom
);
1036 CHECKRESULT(result
);
1038 talloc_free(memctx
);