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_bystring(ctx
->db
, ctx
->hwmkey_uid
,
108 if(!NT_STATUS_IS_OK(status
)) {
109 dbwrap_transaction_cancel(ctx
->db
);
113 status
= dbwrap_store_uint32_bystring(ctx
->db
, ctx
->hwmkey_gid
,
115 if(!NT_STATUS_IS_OK(status
)) {
116 dbwrap_transaction_cancel(ctx
->db
);
120 if(dbwrap_transaction_commit(ctx
->db
) != 0) {
121 DEBUG(0, ("Failed to commit transaction!\n"));
128 static struct idmap_tdb_common_context
*createcontext(TALLOC_CTX
*memctx
)
130 struct idmap_tdb_common_context
*ret
;
132 ret
= talloc_zero(memctx
, struct idmap_tdb_common_context
);
133 ret
->rw_ops
= talloc_zero(ret
, struct idmap_rw_ops
);
135 ret
->max_id
= HIGH_ID
;
136 ret
->hwmkey_uid
= HWM_USER
;
137 ret
->hwmkey_gid
= HWM_GROUP
;
139 ret
->rw_ops
->get_new_id
= idmap_tdb_common_get_new_id
;
140 ret
->rw_ops
->set_mapping
= idmap_tdb_common_set_mapping
;
149 static struct idmap_domain
*createdomain(TALLOC_CTX
*memctx
)
151 struct idmap_domain
*dom
;
153 dom
= talloc_zero(memctx
, struct idmap_domain
);
155 dom
->low_id
= LOW_ID
;
156 dom
->high_id
= HIGH_ID
;
157 dom
->read_only
= false;
158 dom
->methods
= talloc_zero(dom
, struct idmap_methods
);
159 dom
->methods
->sids_to_unixids
= idmap_tdb_common_sids_to_unixids
;
160 dom
->methods
->unixids_to_sids
= idmap_tdb_common_unixids_to_sids
;
161 dom
->methods
->allocate_id
= idmap_tdb_common_get_new_id
;
166 static bool test_getnewid1(TALLOC_CTX
*memctx
, struct idmap_domain
*dom
)
171 id
.type
= ID_TYPE_UID
;
173 status
= idmap_tdb_common_get_new_id(dom
, &id
);
175 if(!NT_STATUS_IS_OK(status
)) {
176 DEBUG(0, ("test_getnewid1: Could not allocate id!\n"));
181 DEBUG(0, ("test_getnewid1: Allocate returned "
186 if(id
.id
> HIGH_ID
|| id
.id
< LOW_ID
) {
187 DEBUG(0, ("test_getnewid1: Allocate returned "
188 "out of range id!\n"));
192 DEBUG(0, ("test_getnewid1: PASSED!\n"));
197 static bool test_getnewid2(TALLOC_CTX
*memctx
, struct idmap_domain
*dom
)
203 id
.type
= ID_TYPE_UID
;
205 status
= idmap_tdb_common_get_new_id(dom
, &id
);
207 if(!NT_STATUS_IS_OK(status
)) {
208 DEBUG(0, ("test_getnewid2: Could not allocate id!\n"));
213 DEBUG(0, ("test_getnewid2: Allocate returned "
218 if(id
.id
> HIGH_ID
|| id
.id
< LOW_ID
) {
219 DEBUG(0, ("test_getnewid2: Allocate returned "
220 "out of range id!\n"));
224 /* how many ids are left? */
226 left
= HIGH_ID
- id
.id
;
228 /* consume them all */
229 for(i
= 0; i
<left
; i
++) {
231 status
= idmap_tdb_common_get_new_id(dom
, &id
);
233 if(!NT_STATUS_IS_OK(status
)) {
234 DEBUG(0, ("test_getnewid2: Allocate returned "
235 "error %s\n", nt_errstr(status
)));
239 if(id
.id
> HIGH_ID
) {
240 DEBUG(0, ("test_getnewid2: Allocate returned "
241 "out of range id (%d)!\n", id
.id
));
246 /* one more must fail */
247 status
= idmap_tdb_common_get_new_id(dom
, &id
);
249 if(NT_STATUS_IS_OK(status
)) {
250 DEBUG(0, ("test_getnewid2: Could allocate id (%d) from "
251 "depleted pool!\n", id
.id
));
255 DEBUG(0, ("test_getnewid2: PASSED!\n"));
260 static bool test_setmap1(TALLOC_CTX
*memctx
, struct idmap_domain
*dom
)
267 /* test for correct return code with invalid data */
269 status
= idmap_tdb_common_set_mapping(dom
, NULL
);
270 if(!NT_STATUS_EQUAL(status
, NT_STATUS_INVALID_PARAMETER
)) {
271 DEBUG(0, ("test_setmap1: bad parameter handling!\n"));
275 status
= idmap_tdb_common_set_mapping(dom
, &map
);
276 if(!NT_STATUS_EQUAL(status
, NT_STATUS_INVALID_PARAMETER
)) {
277 DEBUG(0, ("test_setmap1: bad parameter handling!\n"));
281 map
.sid
= dom_sid_parse_talloc(memctx
, DOM_SID1
"-100");
283 map
.xid
.type
= ID_TYPE_NOT_SPECIFIED
;
286 status
= idmap_tdb_common_set_mapping(dom
, &map
);
287 if(!NT_STATUS_EQUAL(status
, NT_STATUS_INVALID_PARAMETER
)) {
288 DEBUG(0, ("test_setmap1: bad parameter handling!\n"));
292 /* now the good ones */
293 map
.xid
.type
= ID_TYPE_UID
;
296 status
= idmap_tdb_common_get_new_id(dom
, &(map
.xid
));
297 if(!NT_STATUS_IS_OK(status
)) {
298 DEBUG(0, ("test_setmap1: get_new_uid failed!\n"));
302 status
= idmap_tdb_common_set_mapping(dom
, &map
);
303 if(!NT_STATUS_IS_OK(status
)) {
304 DEBUG(0, ("test_setmap1: setting UID mapping failed!\n"));
308 /* try to set the same mapping again as group (must fail) */
310 map
.xid
.type
= ID_TYPE_GID
;
311 status
= idmap_tdb_common_set_mapping(dom
, &map
);
312 if(NT_STATUS_IS_OK(status
)) {
313 DEBUG(0, ("test_setmap1: could create map for "
314 "group and user!\n"));
318 /* now a group with a different SID*/
321 map
.sid
= dom_sid_parse_talloc(memctx
, DOM_SID1
"-101");
323 status
= idmap_tdb_common_get_new_id(dom
, &(map
.xid
));
324 if(!NT_STATUS_IS_OK(status
)) {
325 DEBUG(0, ("test_setmap1: get_new_gid failed!\n"));
329 status
= idmap_tdb_common_set_mapping(dom
, &map
);
330 if(!NT_STATUS_IS_OK(status
)) {
331 DEBUG(0, ("test_setmap1: setting GID mapping failed!\n"));
334 DEBUG(0, ("test_setmap1: PASSED!\n"));
339 static bool test_sid2unixid1(TALLOC_CTX
*memctx
, struct idmap_domain
*dom
)
341 NTSTATUS status1
, status2
, status3
;
344 /* check for correct dealing with bad parameters */
345 status1
= idmap_tdb_common_sid_to_unixid(NULL
, &map
);
346 status2
= idmap_tdb_common_sid_to_unixid(dom
, NULL
);
347 status3
= idmap_tdb_common_sid_to_unixid(NULL
, NULL
);
349 if(!NT_STATUS_EQUAL(NT_STATUS_INVALID_PARAMETER
, status1
) ||
350 !NT_STATUS_EQUAL(NT_STATUS_INVALID_PARAMETER
, status2
) ||
351 !NT_STATUS_EQUAL(NT_STATUS_INVALID_PARAMETER
, status3
)) {
352 DEBUG(0, ("test_setmap1: bad parameter handling!\n"));
356 DEBUG(0, ("test_unixid2sid1: PASSED!\n"));
361 static bool test_sid2unixid2(TALLOC_CTX
*memctx
, struct idmap_domain
*dom
)
364 struct id_map uid_map
, gid_map
, test_map
;
367 ZERO_STRUCT(uid_map
);
368 ZERO_STRUCT(gid_map
);
370 /* create two mappings for a UID and GID */
374 uid_map
.sid
= dom_sid_parse_talloc(memctx
, DOM_SID2
"-1000");
375 uid_map
.xid
.type
= ID_TYPE_UID
;
377 gid_map
.sid
= dom_sid_parse_talloc(memctx
, DOM_SID2
"-1001");
378 gid_map
.xid
.type
= ID_TYPE_GID
;
380 status
= idmap_tdb_common_new_mapping(dom
, &uid_map
);
381 if(!NT_STATUS_IS_OK(status
)) {
382 DEBUG(0, ("test_sid2unixid1: could not create uid map!\n"));
386 status
= idmap_tdb_common_new_mapping(dom
, &gid_map
);
387 if(!NT_STATUS_IS_OK(status
)) {
388 DEBUG(0, ("test_sid2unixid1: could not create gid map!\n"));
392 /* now read them back */
393 ZERO_STRUCT(test_map
);
394 test_map
.sid
= uid_map
.sid
;
396 status
= idmap_tdb_common_sid_to_unixid(dom
, &test_map
);
397 if(!NT_STATUS_IS_OK(status
)) {
398 DEBUG(0, ("test_sid2unixid1: sid2unixid failed for uid!\n"));
402 if(test_map
.xid
.id
!=uid_map
.xid
.id
) {
403 DEBUG(0, ("test_sid2unixid1: sid2unixid returned wrong uid!\n"));
407 test_map
.sid
= gid_map
.sid
;
409 status
= idmap_tdb_common_sid_to_unixid(dom
, &test_map
);
410 if(!NT_STATUS_IS_OK(status
)) {
411 DEBUG(0, ("test_sid2unixid1: sid2unixid failed for gid!\n"));
415 if(test_map
.xid
.id
!=gid_map
.xid
.id
) {
416 DEBUG(0, ("test_sid2unixid1: sid2unixid returned wrong gid!\n"));
421 * Go through the same tests again once to see if trying to recreate
422 * a mapping that was already created will work or not
429 DEBUG(0, ("test_sid2unixid1: PASSED!\n"));
434 static bool test_sids2unixids1(TALLOC_CTX
*memctx
, struct idmap_domain
*dom
)
437 struct id_map uid_map
, gid_map
, **test_maps
;
439 ZERO_STRUCT(uid_map
);
440 ZERO_STRUCT(gid_map
);
442 /* create two mappings for a UID and GID */
444 uid_map
.sid
= dom_sid_parse_talloc(memctx
, DOM_SID4
"-1000");
445 uid_map
.xid
.type
= ID_TYPE_UID
;
447 gid_map
.sid
= dom_sid_parse_talloc(memctx
, DOM_SID4
"-1001");
448 gid_map
.xid
.type
= ID_TYPE_GID
;
450 status
= idmap_tdb_common_new_mapping(dom
, &uid_map
);
451 if(!NT_STATUS_IS_OK(status
)) {
452 DEBUG(0, ("test_sids2unixids1: could not create uid map!\n"));
456 status
= idmap_tdb_common_new_mapping(dom
, &gid_map
);
457 if(!NT_STATUS_IS_OK(status
)) {
458 DEBUG(0, ("test_sids2unixids1: could not create gid map!\n"));
462 /* now read them back */
463 test_maps
= talloc_zero_array(memctx
, struct id_map
*, 3);
465 test_maps
[0] = talloc(test_maps
, struct id_map
);
466 test_maps
[1] = talloc(test_maps
, struct id_map
);
469 test_maps
[0]->sid
= talloc(test_maps
, struct dom_sid
);
470 test_maps
[1]->sid
= talloc(test_maps
, struct dom_sid
);
471 sid_copy(test_maps
[0]->sid
, uid_map
.sid
);
472 sid_copy(test_maps
[1]->sid
, gid_map
.sid
);
474 status
= idmap_tdb_common_sids_to_unixids(dom
, test_maps
);
475 if(!NT_STATUS_IS_OK(status
)) {
476 DEBUG(0, ("test_sids2sunixids1: sids2unixids failed!\n"));
477 talloc_free(test_maps
);
481 if(test_maps
[0]->xid
.id
!=uid_map
.xid
.id
||
482 test_maps
[1]->xid
.id
!=gid_map
.xid
.id
) {
483 DEBUG(0, ("test_sids2unixids1: sid2unixid returned wrong xid!\n"));
484 talloc_free(test_maps
);
488 DEBUG(0, ("test_sids2unixids1: PASSED!\n"));
490 talloc_free(test_maps
);
495 static bool test_sids2unixids2(TALLOC_CTX
*memctx
, struct idmap_domain
*dom
)
498 struct id_map
**test_maps
;
501 test_maps
= talloc_zero_array(memctx
, struct id_map
*, 3);
503 test_maps
[0] = talloc(test_maps
, struct id_map
);
504 test_maps
[1] = talloc(test_maps
, struct id_map
);
507 /* ask for two new mappings for a UID and GID */
508 test_maps
[0]->sid
= dom_sid_parse_talloc(test_maps
, DOM_SID4
"-1003");
509 test_maps
[0]->xid
.type
= ID_TYPE_UID
;
510 test_maps
[1]->sid
= dom_sid_parse_talloc(test_maps
, DOM_SID4
"-1004");
511 test_maps
[1]->xid
.type
= ID_TYPE_GID
;
513 status
= idmap_tdb_common_sids_to_unixids(dom
, test_maps
);
514 if(!NT_STATUS_IS_OK(status
)) {
515 DEBUG(0, ("test_sids2sunixids2: sids2unixids "
516 "failed (%s)!\n", nt_errstr(status
)));
517 talloc_free(test_maps
);
521 if(test_maps
[0]->xid
.id
== 0 || test_maps
[1]->xid
.id
== 0) {
522 DEBUG(0, ("test_sids2sunixids2: sids2unixids "
523 "returned zero ids!\n"));
524 talloc_free(test_maps
);
528 save
= test_maps
[1]->xid
;
530 /* ask for a known and a new mapping at the same time */
531 talloc_free(test_maps
);
532 test_maps
= talloc_zero_array(memctx
, struct id_map
*, 3);
533 test_maps
[0] = talloc(test_maps
, struct id_map
);
534 test_maps
[1] = talloc(test_maps
, struct id_map
);
537 test_maps
[0]->sid
= dom_sid_parse_talloc(test_maps
, DOM_SID4
"-1004");
538 test_maps
[0]->xid
.type
= ID_TYPE_GID
;
539 test_maps
[1]->sid
= dom_sid_parse_talloc(test_maps
, DOM_SID4
"-1005");
540 test_maps
[1]->xid
.type
= ID_TYPE_UID
;
542 status
= idmap_tdb_common_sids_to_unixids(dom
, test_maps
);
543 if(!NT_STATUS_IS_OK(status
)) {
544 DEBUG(0, ("test_sids2sunixids2: sids2unixids (2) "
545 "failed (%s)!\n", nt_errstr(status
)));
546 talloc_free(test_maps
);
550 if(test_maps
[0]->xid
.type
!= save
.type
||
551 test_maps
[0]->xid
.id
!= save
.id
) {
552 DEBUG(0, ("test_sids2sunixids2: second lookup returned "
553 "different value!\n"));
554 talloc_free(test_maps
);
558 if(test_maps
[1]->xid
.id
== 0) {
559 DEBUG(0, ("test_sids2sunixids2: sids2unixids "
560 "returned zero id for mixed mapping request!\n"));
561 talloc_free(test_maps
);
565 DEBUG(0, ("test_sids2unixids2: PASSED!\n"));
567 talloc_free(test_maps
);
572 static bool test_sids2unixids3(TALLOC_CTX
*memctx
, struct idmap_domain
*dom
)
575 struct id_map
**test_maps
;
579 * check the mapping states:
580 * NONE_MAPPED, SOME_UNMAPPED, OK (all mapped)
582 * use the ids created by test_sids2unixids1
583 * need to make dom read-only
586 dom
->read_only
= true;
588 test_maps
= talloc_zero_array(memctx
, struct id_map
*, 3);
590 test_maps
[0] = talloc(test_maps
, struct id_map
);
591 test_maps
[1] = talloc(test_maps
, struct id_map
);
594 /* NONE_MAPPED first */
595 test_maps
[0]->sid
= talloc(test_maps
, struct dom_sid
);
596 test_maps
[1]->sid
= talloc(test_maps
, struct dom_sid
);
597 test_maps
[0]->sid
= dom_sid_parse_talloc(test_maps
,
599 test_maps
[0]->xid
.type
= ID_TYPE_UID
;
601 test_maps
[1]->sid
= dom_sid_parse_talloc(test_maps
,
603 test_maps
[1]->xid
.type
= ID_TYPE_GID
;
605 status
= idmap_tdb_common_sids_to_unixids(dom
, test_maps
);
606 if(!NT_STATUS_EQUAL(status
, NT_STATUS_NONE_MAPPED
)) {
607 DEBUG(0, ("test_sids2unixids3: incorrect status "
608 "(%s), expected NT_STATUS_NONE_MAPPED!\n",
615 test_maps
[0]->sid
= talloc(test_maps
, struct dom_sid
);
616 test_maps
[1]->sid
= talloc(test_maps
, struct dom_sid
);
617 test_maps
[0]->sid
= dom_sid_parse_talloc(test_maps
,
619 test_maps
[0]->xid
.type
= ID_TYPE_UID
;
620 test_maps
[1]->sid
= dom_sid_parse_talloc(test_maps
,
622 test_maps
[1]->xid
.type
= ID_TYPE_GID
;
624 status
= idmap_tdb_common_sids_to_unixids(dom
, test_maps
);
625 if(!NT_STATUS_EQUAL(status
, STATUS_SOME_UNMAPPED
)) {
626 DEBUG(0, ("test_sids2unixids3: incorrect status "
627 "(%s), expected STATUS_SOME_UNMAPPED!\n",
634 test_maps
[0]->sid
= talloc(test_maps
, struct dom_sid
);
635 test_maps
[1]->sid
= talloc(test_maps
, struct dom_sid
);
636 test_maps
[0]->sid
= dom_sid_parse_talloc(test_maps
,
638 test_maps
[1]->sid
= dom_sid_parse_talloc(test_maps
,
641 status
= idmap_tdb_common_sids_to_unixids(dom
, test_maps
);
642 if(!NT_STATUS_IS_OK(status
)) {
643 DEBUG(0, ("test_sids2unixids3: incorrect status "
644 "(%s), expected NT_STATUS_OK!\n",
650 DEBUG(0, ("test_sids2unixids3: PASSED!\n"));
653 talloc_free(test_maps
);
654 dom
->read_only
= false;
658 static bool test_unixid2sid1(TALLOC_CTX
*memctx
, struct idmap_domain
*dom
)
660 NTSTATUS status1
, status2
, status3
;
663 /* check for correct dealing with bad parameters */
664 status1
= idmap_tdb_common_unixid_to_sid(NULL
, &map
);
665 status2
= idmap_tdb_common_unixid_to_sid(dom
, NULL
);
666 status3
= idmap_tdb_common_unixid_to_sid(NULL
, NULL
);
668 if(!NT_STATUS_EQUAL(NT_STATUS_INVALID_PARAMETER
, status1
) ||
669 !NT_STATUS_EQUAL(NT_STATUS_INVALID_PARAMETER
, status2
) ||
670 !NT_STATUS_EQUAL(NT_STATUS_INVALID_PARAMETER
, status3
)) {
671 DEBUG(0, ("test_setmap1: bad parameter handling!\n"));
675 DEBUG(0, ("test_unixid2sid1: PASSED!\n"));
680 static bool test_unixid2sid2(TALLOC_CTX
*memctx
, struct idmap_domain
*dom
)
686 /* ask for mapping that is outside of the range */
687 map
= talloc(memctx
, struct id_map
);
688 map
->sid
= talloc(map
, struct dom_sid
);
690 map
->xid
.type
= ID_TYPE_UID
;
691 map
->xid
.id
= HIGH_ID
+ 1;
693 status
= idmap_tdb_common_unixid_to_sid(dom
, map
);
694 if(NT_STATUS_IS_OK(status
)) {
695 DEBUG(0, ("test_unixid2sid2: unixid2sid returned "
696 "out-of-range result\n"));
701 DEBUG(0, ("test_unixid2sid2: PASSED!\n"));
708 static bool test_unixid2sid3(TALLOC_CTX
*memctx
, struct idmap_domain
*dom
)
711 struct id_map uid_map
, gid_map
, test_map
;
712 struct dom_sid testsid
;
714 ZERO_STRUCT(uid_map
);
715 ZERO_STRUCT(gid_map
);
717 /* create two mappings for a UID and GID */
718 uid_map
.sid
= dom_sid_parse_talloc(memctx
, DOM_SID3
"-1000");
719 uid_map
.xid
.type
= ID_TYPE_UID
;
721 gid_map
.sid
= dom_sid_parse_talloc(memctx
, DOM_SID3
"-1001");
722 gid_map
.xid
.type
= ID_TYPE_GID
;
724 status
= idmap_tdb_common_new_mapping(dom
, &uid_map
);
725 if(!NT_STATUS_IS_OK(status
)) {
726 DEBUG(0, ("test_unixid2sid3: could not create uid map!\n"));
730 status
= idmap_tdb_common_new_mapping(dom
, &gid_map
);
731 if(!NT_STATUS_IS_OK(status
)) {
732 DEBUG(0, ("test_unixid2sid3: could not create gid map!\n"));
736 /* now read them back */
737 ZERO_STRUCT(test_map
);
738 test_map
.xid
.id
= uid_map
.xid
.id
;
739 test_map
.xid
.type
= ID_TYPE_UID
;
740 test_map
.sid
= &testsid
;
742 status
= idmap_tdb_common_unixid_to_sid(dom
, &test_map
);
743 if(!NT_STATUS_IS_OK(status
)) {
744 DEBUG(0, ("test_unixid2sid3: unixid2sid failed for uid!\n"));
748 if(test_map
.xid
.type
!=uid_map
.xid
.type
) {
749 DEBUG(0, ("test_unixid2sid3: unixid2sid returned wrong type!\n"));
753 if(!dom_sid_equal(test_map
.sid
, uid_map
.sid
)) {
754 DEBUG(0, ("test_unixid2sid3: unixid2sid returned wrong SID!\n"));
758 ZERO_STRUCT(test_map
);
759 test_map
.xid
.id
= gid_map
.xid
.id
;
760 test_map
.xid
.type
= ID_TYPE_GID
;
761 test_map
.sid
= &testsid
;
763 status
= idmap_tdb_common_unixid_to_sid(dom
, &test_map
);
764 if(!NT_STATUS_IS_OK(status
)) {
765 DEBUG(0, ("test_unixid2sid3: unixid2sid failed for gid!\n"));
769 if(test_map
.xid
.type
!=gid_map
.xid
.type
) {
770 DEBUG(0, ("test_unixid2sid3: unixid2sid returned wrong type!\n"));
774 if(!dom_sid_equal(test_map
.sid
,gid_map
.sid
)) {
775 DEBUG(0, ("test_unixid2sid3: unixid2sid returned wrong SID!\n"));
779 DEBUG(0, ("test_unixid2sid3: PASSED!\n"));
784 static bool test_unixids2sids1(TALLOC_CTX
*memctx
, struct idmap_domain
*dom
)
787 struct id_map uid_map
, gid_map
, **test_maps
;
789 ZERO_STRUCT(uid_map
);
790 ZERO_STRUCT(gid_map
);
792 /* create two mappings for a UID and GID */
794 uid_map
.sid
= dom_sid_parse_talloc(memctx
, DOM_SID5
"-1000");
795 uid_map
.xid
.type
= ID_TYPE_UID
;
797 gid_map
.sid
= dom_sid_parse_talloc(memctx
, DOM_SID5
"-1001");
798 gid_map
.xid
.type
= ID_TYPE_GID
;
800 status
= idmap_tdb_common_new_mapping(dom
, &uid_map
);
801 if(!NT_STATUS_IS_OK(status
)) {
802 DEBUG(0, ("test_unixids2sids1: could not create uid map!\n"));
806 status
= idmap_tdb_common_new_mapping(dom
, &gid_map
);
807 if(!NT_STATUS_IS_OK(status
)) {
808 DEBUG(0, ("test_unixids2sids1: could not create gid map!\n"));
812 /* now read them back */
813 test_maps
= talloc_zero_array(memctx
, struct id_map
*, 3);
815 test_maps
[0] = talloc(test_maps
, struct id_map
);
816 test_maps
[1] = talloc(test_maps
, struct id_map
);
819 test_maps
[0]->sid
= talloc(test_maps
, struct dom_sid
);
820 test_maps
[1]->sid
= talloc(test_maps
, struct dom_sid
);
821 test_maps
[0]->xid
.id
= uid_map
.xid
.id
;
822 test_maps
[0]->xid
.type
= ID_TYPE_UID
;
823 test_maps
[1]->xid
.id
= gid_map
.xid
.id
;
824 test_maps
[1]->xid
.type
= ID_TYPE_GID
;
826 status
= idmap_tdb_common_unixids_to_sids(dom
, test_maps
);
827 if(!NT_STATUS_IS_OK(status
)) {
828 DEBUG(0, ("test_unixids2sids1: unixids2sids failed!\n"));
829 talloc_free(test_maps
);
833 if(!dom_sid_equal(test_maps
[0]->sid
, uid_map
.sid
) ||
834 !dom_sid_equal(test_maps
[1]->sid
, gid_map
.sid
) ) {
835 DEBUG(0, ("test_unixids2sids1: unixids2sids returned wrong sid!\n"));
836 talloc_free(test_maps
);
840 DEBUG(0, ("test_unixids2sids1: PASSED!\n"));
842 talloc_free(test_maps
);
847 static bool test_unixids2sids2(TALLOC_CTX
*memctx
, struct idmap_domain
*dom
)
850 struct id_map
**test_maps
;
853 test_maps
= talloc_zero_array(memctx
, struct id_map
*, 3);
855 test_maps
[0] = talloc(test_maps
, struct id_map
);
856 test_maps
[1] = talloc(test_maps
, struct id_map
);
859 /* ask for two unknown mappings for a UID and GID */
860 test_maps
[0]->sid
= talloc(test_maps
, struct dom_sid
);
861 test_maps
[1]->sid
= talloc(test_maps
, struct dom_sid
);
862 test_maps
[0]->xid
.id
= HIGH_ID
- 1;
863 test_maps
[0]->xid
.type
= ID_TYPE_UID
;
864 test_maps
[1]->xid
.id
= HIGH_ID
- 1;
865 test_maps
[1]->xid
.type
= ID_TYPE_GID
;
867 status
= idmap_tdb_common_unixids_to_sids(dom
, test_maps
);
868 if(NT_STATUS_IS_OK(status
)) {
869 DEBUG(0, ("test_unixids2sids2: unixids2sids succeeded "
875 DEBUG(0, ("test_unixids2sids2: PASSED!\n"));
878 talloc_free(test_maps
);
883 static bool test_unixids2sids3(TALLOC_CTX
*memctx
, struct idmap_domain
*dom
)
886 struct id_map uid_map
, gid_map
, **test_maps
;
889 ZERO_STRUCT(uid_map
);
890 ZERO_STRUCT(gid_map
);
892 /* create two mappings for a UID and GID */
893 uid_map
.sid
= dom_sid_parse_talloc(memctx
, DOM_SID6
"-1000");
894 uid_map
.xid
.type
= ID_TYPE_UID
;
896 gid_map
.sid
= dom_sid_parse_talloc(memctx
, DOM_SID6
"-1001");
897 gid_map
.xid
.type
= ID_TYPE_GID
;
899 status
= idmap_tdb_common_new_mapping(dom
, &uid_map
);
900 if(!NT_STATUS_IS_OK(status
)) {
901 DEBUG(0, ("test_unixids2sids3: could not create uid map!\n"));
905 status
= idmap_tdb_common_new_mapping(dom
, &gid_map
);
906 if(!NT_STATUS_IS_OK(status
)) {
907 DEBUG(0, ("test_unixids2sids3: could not create gid map!\n"));
912 * check the mapping states:
913 * NONE_MAPPED, SOME_UNMAPPED, OK (all mapped)
915 test_maps
= talloc_zero_array(memctx
, struct id_map
*, 3);
917 test_maps
[0] = talloc(test_maps
, struct id_map
);
918 test_maps
[1] = talloc(test_maps
, struct id_map
);
921 /* NONE_MAPPED first */
922 test_maps
[0]->sid
= talloc(test_maps
, struct dom_sid
);
923 test_maps
[1]->sid
= talloc(test_maps
, struct dom_sid
);
925 test_maps
[0]->xid
.id
= HIGH_ID
- 1;
926 test_maps
[0]->xid
.type
= ID_TYPE_UID
;
928 test_maps
[1]->xid
.id
= HIGH_ID
- 1;
929 test_maps
[1]->xid
.type
= ID_TYPE_GID
;
931 status
= idmap_tdb_common_unixids_to_sids(dom
, test_maps
);
932 if(!NT_STATUS_EQUAL(status
, NT_STATUS_NONE_MAPPED
)) {
933 DEBUG(0, ("test_unixids2sids3: incorrect status "
934 "(%s), expected NT_STATUS_NONE_MAPPED!\n",
941 test_maps
[0]->sid
= talloc(test_maps
, struct dom_sid
);
942 test_maps
[1]->sid
= talloc(test_maps
, struct dom_sid
);
943 test_maps
[0]->xid
= uid_map
.xid
;
944 test_maps
[1]->xid
.id
= HIGH_ID
- 1;
945 test_maps
[1]->xid
.type
= ID_TYPE_GID
;
947 status
= idmap_tdb_common_unixids_to_sids(dom
, test_maps
);
948 if(!NT_STATUS_EQUAL(status
, STATUS_SOME_UNMAPPED
)) {
949 DEBUG(0, ("test_unixids2sids3: incorrect status "
950 "(%s), expected STATUS_SOME_UNMAPPED!\n",
957 test_maps
[0]->sid
= talloc(test_maps
, struct dom_sid
);
958 test_maps
[1]->sid
= talloc(test_maps
, struct dom_sid
);
959 test_maps
[0]->xid
= uid_map
.xid
;
960 test_maps
[1]->xid
= gid_map
.xid
;
962 status
= idmap_tdb_common_unixids_to_sids(dom
, test_maps
);
963 if(!NT_STATUS_IS_OK(status
)) {
964 DEBUG(0, ("test_unixids2sids3: incorrect status "
965 "(%s), expected NT_STATUS_OK!\n",
971 DEBUG(0, ("test_unixids2sids3: PASSED!\n"));
974 talloc_free(test_maps
);
978 #define CHECKRESULT(r) if(!r) {return r;}
980 bool run_idmap_tdb_common_test(int dummy
)
983 struct idmap_tdb_common_context
*ctx
;
984 struct idmap_domain
*dom
;
986 TALLOC_CTX
*memctx
= talloc_new(NULL
);
987 TALLOC_CTX
*stack
= talloc_stackframe();
989 ctx
= createcontext(memctx
);
994 dom
= createdomain(memctx
);
996 dom
->private_data
= ctx
;
998 /* test a single allocation from pool (no mapping) */
999 result
= test_getnewid1(memctx
, dom
);
1000 CHECKRESULT(result
);
1002 /* test idmap_tdb_common_set_mapping */
1003 result
= test_setmap1(memctx
, dom
);
1004 CHECKRESULT(result
);
1006 /* test idmap_tdb_common_sid_to_unixid */
1007 result
= test_sid2unixid1(memctx
, dom
);
1008 CHECKRESULT(result
);
1009 result
= test_sid2unixid2(memctx
, dom
);
1010 CHECKRESULT(result
);
1012 /* test idmap_tdb_common_sids_to_unixids */
1013 result
= test_sids2unixids1(memctx
, dom
);
1014 CHECKRESULT(result
);
1015 result
= test_sids2unixids2(memctx
, dom
);
1016 CHECKRESULT(result
);
1017 result
= test_sids2unixids3(memctx
, dom
);
1018 CHECKRESULT(result
);
1020 /* test idmap_tdb_common_unixid_to_sid */
1021 result
= test_unixid2sid1(memctx
, dom
);
1022 CHECKRESULT(result
);
1023 result
= test_unixid2sid2(memctx
, dom
);
1024 CHECKRESULT(result
);
1025 result
= test_unixid2sid3(memctx
, dom
);
1026 CHECKRESULT(result
);
1028 /* test idmap_tdb_common_unixids_to_sids */
1029 result
= test_unixids2sids1(memctx
, dom
);
1030 CHECKRESULT(result
);
1031 result
= test_unixids2sids2(memctx
, dom
);
1032 CHECKRESULT(result
);
1033 result
= test_unixids2sids3(memctx
, dom
);
1034 CHECKRESULT(result
);
1036 /* test filling up the range */
1037 result
= test_getnewid2(memctx
, dom
);
1038 CHECKRESULT(result
);
1040 talloc_free(memctx
);