idmap: Remove "domname" from idmap_backends_unixid_to_sid
[Samba.git] / source3 / torture / test_idmap_tdb_common.c
blobdd736ad40c20dd408984a4d5d2e0b2b6dca02d81
1 /*
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/>.
21 #include "includes.h"
22 #include "system/filesys.h"
23 #include "torture/proto.h"
24 #include "idmap.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"
36 #define LOW_ID 100
37 #define HIGH_ID 199
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)
49 return NULL;
52 bool get_global_winbindd_state_offline(void) {
53 return false;
56 bool winbindd_use_idmap_cache(void) {
57 return false;
60 bool idmap_is_online(void)
62 return true;
65 NTSTATUS idmap_backends_unixid_to_sid(struct id_map *id)
67 return NT_STATUS_OK;
70 static bool open_db(struct idmap_tdb_common_context *ctx)
72 NTSTATUS status;
73 char *db_path;
75 if(ctx->db) {
76 /* already open */
77 return true;
80 db_path = talloc_asprintf(talloc_tos(), "%s/idmap_test.tdb",
81 lp_private_dir());
82 if(!db_path) {
83 DEBUG(0, ("Out of memory!\n"));
84 return false;
87 ctx->db = db_open(ctx, db_path, 0, TDB_DEFAULT,
88 O_RDWR | O_CREAT, 0600,
89 DBWRAP_LOCK_ORDER_1, DBWRAP_FLAG_NONE);
91 if(!ctx->db) {
92 DEBUG(0, ("Failed to open database: %s\n", strerror(errno)));
93 return false;
96 if(dbwrap_transaction_start(ctx->db) != 0) {
97 DEBUG(0, ("Failed to start transaction!\n"));
98 return false;
101 status = dbwrap_store_uint32_bystring(ctx->db, ctx->hwmkey_uid,
102 LOW_ID);
103 if(!NT_STATUS_IS_OK(status)) {
104 dbwrap_transaction_cancel(ctx->db);
105 return false;
108 status = dbwrap_store_uint32_bystring(ctx->db, ctx->hwmkey_gid,
109 LOW_ID);
110 if(!NT_STATUS_IS_OK(status)) {
111 dbwrap_transaction_cancel(ctx->db);
112 return false;
115 if(dbwrap_transaction_commit(ctx->db) != 0) {
116 DEBUG(0, ("Failed to commit transaction!\n"));
117 return false;
120 return true;
123 static struct idmap_tdb_common_context *createcontext(TALLOC_CTX *memctx)
125 struct idmap_tdb_common_context *ret;
127 ret = talloc_zero(memctx, struct idmap_tdb_common_context);
128 ret->rw_ops = talloc_zero(ret, struct idmap_rw_ops);
130 ret->max_id = HIGH_ID;
131 ret->hwmkey_uid = HWM_USER;
132 ret->hwmkey_gid = HWM_GROUP;
134 ret->rw_ops->get_new_id = idmap_tdb_common_get_new_id;
135 ret->rw_ops->set_mapping = idmap_tdb_common_set_mapping;
137 if (!open_db(ret)) {
138 return NULL;
141 return ret;
144 static struct idmap_domain *createdomain(TALLOC_CTX *memctx)
146 struct idmap_domain *dom;
148 dom = talloc_zero(memctx, struct idmap_domain);
149 dom->name = "*";
150 dom->low_id = LOW_ID;
151 dom->high_id = HIGH_ID;
152 dom->read_only = false;
153 dom->methods = talloc_zero(dom, struct idmap_methods);
154 dom->methods->sids_to_unixids = idmap_tdb_common_sids_to_unixids;
155 dom->methods->unixids_to_sids = idmap_tdb_common_unixids_to_sids;
156 dom->methods->allocate_id = idmap_tdb_common_get_new_id;
158 return dom;
161 static bool test_getnewid1(TALLOC_CTX *memctx, struct idmap_domain *dom)
163 NTSTATUS status;
164 struct unixid id;
166 id.type = ID_TYPE_UID;
168 status = idmap_tdb_common_get_new_id(dom, &id);
170 if(!NT_STATUS_IS_OK(status)) {
171 DEBUG(0, ("test_getnewid1: Could not allocate id!\n"));
172 return false;
175 if(id.id == 0) {
176 DEBUG(0, ("test_getnewid1: Allocate returned "
177 "empty id!\n"));
178 return false;
181 if(id.id > HIGH_ID || id.id < LOW_ID) {
182 DEBUG(0, ("test_getnewid1: Allocate returned "
183 "out of range id!\n"));
184 return false;
187 DEBUG(0, ("test_getnewid1: PASSED!\n"));
189 return true;
192 static bool test_getnewid2(TALLOC_CTX *memctx, struct idmap_domain *dom)
194 NTSTATUS status;
195 struct unixid id;
196 int i, left;
198 id.type = ID_TYPE_UID;
200 status = idmap_tdb_common_get_new_id(dom, &id);
202 if(!NT_STATUS_IS_OK(status)) {
203 DEBUG(0, ("test_getnewid2: Could not allocate id!\n"));
204 return false;
207 if(id.id == 0) {
208 DEBUG(0, ("test_getnewid2: Allocate returned "
209 "empty id!\n"));
210 return false;
213 if(id.id > HIGH_ID || id.id < LOW_ID) {
214 DEBUG(0, ("test_getnewid2: Allocate returned "
215 "out of range id!\n"));
216 return false;
219 /* how many ids are left? */
221 left = HIGH_ID - id.id;
223 /* consume them all */
224 for(i = 0; i<left; i++) {
226 status = idmap_tdb_common_get_new_id(dom, &id);
228 if(!NT_STATUS_IS_OK(status)) {
229 DEBUG(0, ("test_getnewid2: Allocate returned "
230 "error %s\n", nt_errstr(status)));
231 return false;
234 if(id.id > HIGH_ID) {
235 DEBUG(0, ("test_getnewid2: Allocate returned "
236 "out of range id (%d)!\n", id.id));
237 return false;
241 /* one more must fail */
242 status = idmap_tdb_common_get_new_id(dom, &id);
244 if(NT_STATUS_IS_OK(status)) {
245 DEBUG(0, ("test_getnewid2: Could allocate id (%d) from "
246 "depleted pool!\n", id.id));
247 return false;
250 DEBUG(0, ("test_getnewid2: PASSED!\n"));
252 return true;
255 static bool test_setmap1(TALLOC_CTX *memctx, struct idmap_domain *dom)
257 NTSTATUS status;
258 struct id_map map;
260 ZERO_STRUCT(map);
262 /* test for correct return code with invalid data */
264 status = idmap_tdb_common_set_mapping(dom, NULL);
265 if(!NT_STATUS_EQUAL(status, NT_STATUS_INVALID_PARAMETER)) {
266 DEBUG(0, ("test_setmap1: bad parameter handling!\n"));
267 return false;
270 status = idmap_tdb_common_set_mapping(dom, &map);
271 if(!NT_STATUS_EQUAL(status, NT_STATUS_INVALID_PARAMETER)) {
272 DEBUG(0, ("test_setmap1: bad parameter handling!\n"));
273 return false;
276 map.sid = dom_sid_parse_talloc(memctx, DOM_SID1 "-100");
278 map.xid.type = ID_TYPE_NOT_SPECIFIED;
279 map.xid.id = 4711;
281 status = idmap_tdb_common_set_mapping(dom, &map);
282 if(!NT_STATUS_EQUAL(status, NT_STATUS_INVALID_PARAMETER)) {
283 DEBUG(0, ("test_setmap1: bad parameter handling!\n"));
284 return false;
287 /* now the good ones */
288 map.xid.type = ID_TYPE_UID;
289 map.xid.id = 0;
291 status = idmap_tdb_common_get_new_id(dom, &(map.xid));
292 if(!NT_STATUS_IS_OK(status)) {
293 DEBUG(0, ("test_setmap1: get_new_uid failed!\n"));
294 return false;
297 status = idmap_tdb_common_set_mapping(dom, &map);
298 if(!NT_STATUS_IS_OK(status)) {
299 DEBUG(0, ("test_setmap1: setting UID mapping failed!\n"));
300 return false;
303 /* try to set the same mapping again as group (must fail) */
305 map.xid.type = ID_TYPE_GID;
306 status = idmap_tdb_common_set_mapping(dom, &map);
307 if(NT_STATUS_IS_OK(status)) {
308 DEBUG(0, ("test_setmap1: could create map for "
309 "group and user!\n"));
310 return false;
313 /* now a group with a different SID*/
314 map.xid.id = 0;
316 map.sid = dom_sid_parse_talloc(memctx, DOM_SID1 "-101");
318 status = idmap_tdb_common_get_new_id(dom, &(map.xid));
319 if(!NT_STATUS_IS_OK(status)) {
320 DEBUG(0, ("test_setmap1: get_new_gid failed!\n"));
321 return false;
324 status = idmap_tdb_common_set_mapping(dom, &map);
325 if(!NT_STATUS_IS_OK(status)) {
326 DEBUG(0, ("test_setmap1: setting GID mapping failed!\n"));
327 return false;
329 DEBUG(0, ("test_setmap1: PASSED!\n"));
331 return true;
334 static bool test_sid2unixid1(TALLOC_CTX *memctx, struct idmap_domain *dom)
336 NTSTATUS status1, status2, status3;
337 struct id_map map;
339 /* check for correct dealing with bad parameters */
340 status1 = idmap_tdb_common_sid_to_unixid(NULL, &map);
341 status2 = idmap_tdb_common_sid_to_unixid(dom, NULL);
342 status3 = idmap_tdb_common_sid_to_unixid(NULL, NULL);
344 if(!NT_STATUS_EQUAL(NT_STATUS_INVALID_PARAMETER, status1) ||
345 !NT_STATUS_EQUAL(NT_STATUS_INVALID_PARAMETER, status2) ||
346 !NT_STATUS_EQUAL(NT_STATUS_INVALID_PARAMETER, status3)) {
347 DEBUG(0, ("test_setmap1: bad parameter handling!\n"));
348 return false;
351 DEBUG(0, ("test_unixid2sid1: PASSED!\n"));
353 return true;
356 static bool test_sid2unixid2(TALLOC_CTX *memctx, struct idmap_domain *dom)
358 NTSTATUS status;
359 struct id_map uid_map, gid_map, test_map;
360 bool doagain = true;
362 ZERO_STRUCT(uid_map);
363 ZERO_STRUCT(gid_map);
365 /* create two mappings for a UID and GID */
367 again:
369 uid_map.sid = dom_sid_parse_talloc(memctx, DOM_SID2 "-1000");
370 uid_map.xid.type = ID_TYPE_UID;
372 gid_map.sid = dom_sid_parse_talloc(memctx, DOM_SID2 "-1001");
373 gid_map.xid.type = ID_TYPE_GID;
375 status = idmap_tdb_common_new_mapping(dom, &uid_map);
376 if(!NT_STATUS_IS_OK(status)) {
377 DEBUG(0, ("test_sid2unixid1: could not create uid map!\n"));
378 return false;
381 status = idmap_tdb_common_new_mapping(dom, &gid_map);
382 if(!NT_STATUS_IS_OK(status)) {
383 DEBUG(0, ("test_sid2unixid1: could not create gid map!\n"));
384 return false;
387 /* now read them back */
388 ZERO_STRUCT(test_map);
389 test_map.sid = uid_map.sid;
391 status = idmap_tdb_common_sid_to_unixid(dom, &test_map);
392 if(!NT_STATUS_IS_OK(status)) {
393 DEBUG(0, ("test_sid2unixid1: sid2unixid failed for uid!\n"));
394 return false;
397 if(test_map.xid.id!=uid_map.xid.id) {
398 DEBUG(0, ("test_sid2unixid1: sid2unixid returned wrong uid!\n"));
399 return false;
402 test_map.sid = gid_map.sid;
404 status = idmap_tdb_common_sid_to_unixid(dom, &test_map);
405 if(!NT_STATUS_IS_OK(status)) {
406 DEBUG(0, ("test_sid2unixid1: sid2unixid failed for gid!\n"));
407 return false;
410 if(test_map.xid.id!=gid_map.xid.id) {
411 DEBUG(0, ("test_sid2unixid1: sid2unixid returned wrong gid!\n"));
412 return false;
416 * Go through the same tests again once to see if trying to recreate
417 * a mapping that was already created will work or not
419 if(doagain) {
420 doagain = false;
421 goto again;
424 DEBUG(0, ("test_sid2unixid1: PASSED!\n"));
426 return true;
429 static bool test_sids2unixids1(TALLOC_CTX *memctx, struct idmap_domain *dom)
431 NTSTATUS status;
432 struct id_map uid_map, gid_map, **test_maps;
434 ZERO_STRUCT(uid_map);
435 ZERO_STRUCT(gid_map);
437 /* create two mappings for a UID and GID */
439 uid_map.sid = dom_sid_parse_talloc(memctx, DOM_SID4 "-1000");
440 uid_map.xid.type = ID_TYPE_UID;
442 gid_map.sid = dom_sid_parse_talloc(memctx, DOM_SID4 "-1001");
443 gid_map.xid.type = ID_TYPE_GID;
445 status = idmap_tdb_common_new_mapping(dom, &uid_map);
446 if(!NT_STATUS_IS_OK(status)) {
447 DEBUG(0, ("test_sids2unixids1: could not create uid map!\n"));
448 return false;
451 status = idmap_tdb_common_new_mapping(dom, &gid_map);
452 if(!NT_STATUS_IS_OK(status)) {
453 DEBUG(0, ("test_sids2unixids1: could not create gid map!\n"));
454 return false;
457 /* now read them back */
458 test_maps = talloc_zero_array(memctx, struct id_map*, 3);
460 test_maps[0] = talloc(test_maps, struct id_map);
461 test_maps[1] = talloc(test_maps, struct id_map);
462 test_maps[2] = NULL;
464 test_maps[0]->sid = talloc(test_maps, struct dom_sid);
465 test_maps[1]->sid = talloc(test_maps, struct dom_sid);
466 sid_copy(test_maps[0]->sid, uid_map.sid);
467 sid_copy(test_maps[1]->sid, gid_map.sid);
469 status = idmap_tdb_common_sids_to_unixids(dom, test_maps);
470 if(!NT_STATUS_IS_OK(status)) {
471 DEBUG(0, ("test_sids2sunixids1: sids2unixids failed!\n"));
472 talloc_free(test_maps);
473 return false;
476 if(test_maps[0]->xid.id!=uid_map.xid.id ||
477 test_maps[1]->xid.id!=gid_map.xid.id ) {
478 DEBUG(0, ("test_sids2unixids1: sid2unixid returned wrong xid!\n"));
479 talloc_free(test_maps);
480 return false;
483 DEBUG(0, ("test_sids2unixids1: PASSED!\n"));
485 talloc_free(test_maps);
487 return true;
490 static bool test_sids2unixids2(TALLOC_CTX *memctx, struct idmap_domain *dom)
492 NTSTATUS status;
493 struct id_map **test_maps;
494 struct unixid save;
496 test_maps = talloc_zero_array(memctx, struct id_map*, 3);
498 test_maps[0] = talloc(test_maps, struct id_map);
499 test_maps[1] = talloc(test_maps, struct id_map);
500 test_maps[2] = NULL;
502 /* ask for two new mappings for a UID and GID */
503 test_maps[0]->sid = dom_sid_parse_talloc(test_maps, DOM_SID4 "-1003");
504 test_maps[0]->xid.type = ID_TYPE_UID;
505 test_maps[1]->sid = dom_sid_parse_talloc(test_maps, DOM_SID4 "-1004");
506 test_maps[1]->xid.type = ID_TYPE_GID;
508 status = idmap_tdb_common_sids_to_unixids(dom, test_maps);
509 if(!NT_STATUS_IS_OK(status)) {
510 DEBUG(0, ("test_sids2sunixids2: sids2unixids "
511 "failed (%s)!\n", nt_errstr(status)));
512 talloc_free(test_maps);
513 return false;
516 if(test_maps[0]->xid.id == 0 || test_maps[1]->xid.id == 0) {
517 DEBUG(0, ("test_sids2sunixids2: sids2unixids "
518 "returned zero ids!\n"));
519 talloc_free(test_maps);
520 return false;
523 save = test_maps[1]->xid;
525 /* ask for a known and a new mapping at the same time */
526 talloc_free(test_maps);
527 test_maps = talloc_zero_array(memctx, struct id_map*, 3);
528 test_maps[0] = talloc(test_maps, struct id_map);
529 test_maps[1] = talloc(test_maps, struct id_map);
530 test_maps[2] = NULL;
532 test_maps[0]->sid = dom_sid_parse_talloc(test_maps, DOM_SID4 "-1004");
533 test_maps[0]->xid.type = ID_TYPE_GID;
534 test_maps[1]->sid = dom_sid_parse_talloc(test_maps, DOM_SID4 "-1005");
535 test_maps[1]->xid.type = ID_TYPE_UID;
537 status = idmap_tdb_common_sids_to_unixids(dom, test_maps);
538 if(!NT_STATUS_IS_OK(status)) {
539 DEBUG(0, ("test_sids2sunixids2: sids2unixids (2) "
540 "failed (%s)!\n", nt_errstr(status)));
541 talloc_free(test_maps);
542 return false;
545 if(test_maps[0]->xid.type != save.type ||
546 test_maps[0]->xid.id != save.id) {
547 DEBUG(0, ("test_sids2sunixids2: second lookup returned "
548 "different value!\n"));
549 talloc_free(test_maps);
550 return false;
553 if(test_maps[1]->xid.id == 0) {
554 DEBUG(0, ("test_sids2sunixids2: sids2unixids "
555 "returned zero id for mixed mapping request!\n"));
556 talloc_free(test_maps);
557 return false;
560 DEBUG(0, ("test_sids2unixids2: PASSED!\n"));
562 talloc_free(test_maps);
564 return true;
567 static bool test_sids2unixids3(TALLOC_CTX *memctx, struct idmap_domain *dom)
569 NTSTATUS status;
570 struct id_map **test_maps;
571 bool retval = true;
574 * check the mapping states:
575 * NONE_MAPPED, SOME_UNMAPPED, OK (all mapped)
577 * use the ids created by test_sids2unixids1
578 * need to make dom read-only
581 dom->read_only = true;
583 test_maps = talloc_zero_array(memctx, struct id_map*, 3);
585 test_maps[0] = talloc(test_maps, struct id_map);
586 test_maps[1] = talloc(test_maps, struct id_map);
587 test_maps[2] = NULL;
589 /* NONE_MAPPED first */
590 test_maps[0]->sid = talloc(test_maps, struct dom_sid);
591 test_maps[1]->sid = talloc(test_maps, struct dom_sid);
592 test_maps[0]->sid = dom_sid_parse_talloc(test_maps,
593 "S-1-5-21-1-2-3-4");
594 test_maps[0]->xid.type = ID_TYPE_UID;
596 test_maps[1]->sid = dom_sid_parse_talloc(test_maps,
597 "S-1-5-21-1-2-3-5");
598 test_maps[1]->xid.type = ID_TYPE_GID;
600 status = idmap_tdb_common_sids_to_unixids(dom, test_maps);
601 if(!NT_STATUS_EQUAL(status, NT_STATUS_NONE_MAPPED)) {
602 DEBUG(0, ("test_sids2unixids3: incorrect status "
603 "(%s), expected NT_STATUS_NONE_MAPPED!\n",
604 nt_errstr(status)));
605 retval = false;
606 goto out;
609 /* SOME_UNMAPPED */
610 test_maps[0]->sid = talloc(test_maps, struct dom_sid);
611 test_maps[1]->sid = talloc(test_maps, struct dom_sid);
612 test_maps[0]->sid = dom_sid_parse_talloc(test_maps,
613 DOM_SID4 "-1000");
614 test_maps[0]->xid.type = ID_TYPE_UID;
615 test_maps[1]->sid = dom_sid_parse_talloc(test_maps,
616 "S-1-5-21-1-2-3-5");
617 test_maps[1]->xid.type = ID_TYPE_GID;
619 status = idmap_tdb_common_sids_to_unixids(dom, test_maps);
620 if(!NT_STATUS_EQUAL(status, STATUS_SOME_UNMAPPED)) {
621 DEBUG(0, ("test_sids2unixids3: incorrect status "
622 "(%s), expected STATUS_SOME_UNMAPPED!\n",
623 nt_errstr(status)));
624 retval = false;
625 goto out;
628 /* OK */
629 test_maps[0]->sid = talloc(test_maps, struct dom_sid);
630 test_maps[1]->sid = talloc(test_maps, struct dom_sid);
631 test_maps[0]->sid = dom_sid_parse_talloc(test_maps,
632 DOM_SID4 "-1001");
633 test_maps[1]->sid = dom_sid_parse_talloc(test_maps,
634 DOM_SID4 "-1000");
636 status = idmap_tdb_common_sids_to_unixids(dom, test_maps);
637 if(!NT_STATUS_IS_OK(status)) {
638 DEBUG(0, ("test_sids2unixids3: incorrect status "
639 "(%s), expected NT_STATUS_OK!\n",
640 nt_errstr(status)));
641 retval = false;
642 goto out;
645 DEBUG(0, ("test_sids2unixids3: PASSED!\n"));
647 out:
648 talloc_free(test_maps);
649 dom->read_only = false;
650 return retval;
653 static bool test_unixid2sid1(TALLOC_CTX *memctx, struct idmap_domain *dom)
655 NTSTATUS status1, status2, status3;
656 struct id_map map;
658 /* check for correct dealing with bad parameters */
659 status1 = idmap_tdb_common_unixid_to_sid(NULL, &map);
660 status2 = idmap_tdb_common_unixid_to_sid(dom, NULL);
661 status3 = idmap_tdb_common_unixid_to_sid(NULL, NULL);
663 if(!NT_STATUS_EQUAL(NT_STATUS_INVALID_PARAMETER, status1) ||
664 !NT_STATUS_EQUAL(NT_STATUS_INVALID_PARAMETER, status2) ||
665 !NT_STATUS_EQUAL(NT_STATUS_INVALID_PARAMETER, status3)) {
666 DEBUG(0, ("test_setmap1: bad parameter handling!\n"));
667 return false;
670 DEBUG(0, ("test_unixid2sid1: PASSED!\n"));
672 return true;
675 static bool test_unixid2sid2(TALLOC_CTX *memctx, struct idmap_domain *dom)
677 NTSTATUS status;
678 struct id_map *map;
679 bool retval = true;
681 /* ask for mapping that is outside of the range */
682 map = talloc(memctx, struct id_map);
683 map->sid = talloc(map, struct dom_sid);
685 map->xid.type = ID_TYPE_UID;
686 map->xid.id = HIGH_ID + 1;
688 status = idmap_tdb_common_unixid_to_sid(dom, map);
689 if(NT_STATUS_IS_OK(status)) {
690 DEBUG(0, ("test_unixid2sid2: unixid2sid returned "
691 "out-of-range result\n"));
692 retval = false;
693 goto out;
696 DEBUG(0, ("test_unixid2sid2: PASSED!\n"));
697 out:
698 talloc_free(map);
699 return retval;
703 static bool test_unixid2sid3(TALLOC_CTX *memctx, struct idmap_domain *dom)
705 NTSTATUS status;
706 struct id_map uid_map, gid_map, test_map;
707 struct dom_sid testsid;
709 ZERO_STRUCT(uid_map);
710 ZERO_STRUCT(gid_map);
712 /* create two mappings for a UID and GID */
713 uid_map.sid = dom_sid_parse_talloc(memctx, DOM_SID3 "-1000");
714 uid_map.xid.type = ID_TYPE_UID;
716 gid_map.sid = dom_sid_parse_talloc(memctx, DOM_SID3 "-1001");
717 gid_map.xid.type = ID_TYPE_GID;
719 status = idmap_tdb_common_new_mapping(dom, &uid_map);
720 if(!NT_STATUS_IS_OK(status)) {
721 DEBUG(0, ("test_unixid2sid3: could not create uid map!\n"));
722 return false;
725 status = idmap_tdb_common_new_mapping(dom, &gid_map);
726 if(!NT_STATUS_IS_OK(status)) {
727 DEBUG(0, ("test_unixid2sid3: could not create gid map!\n"));
728 return false;
731 /* now read them back */
732 ZERO_STRUCT(test_map);
733 test_map.xid.id = uid_map.xid.id;
734 test_map.xid.type = ID_TYPE_UID;
735 test_map.sid = &testsid;
737 status = idmap_tdb_common_unixid_to_sid(dom, &test_map);
738 if(!NT_STATUS_IS_OK(status)) {
739 DEBUG(0, ("test_unixid2sid3: unixid2sid failed for uid!\n"));
740 return false;
743 if(test_map.xid.type!=uid_map.xid.type) {
744 DEBUG(0, ("test_unixid2sid3: unixid2sid returned wrong type!\n"));
745 return false;
748 if(!dom_sid_equal(test_map.sid, uid_map.sid)) {
749 DEBUG(0, ("test_unixid2sid3: unixid2sid returned wrong SID!\n"));
750 return false;
753 ZERO_STRUCT(test_map);
754 test_map.xid.id = gid_map.xid.id;
755 test_map.xid.type = ID_TYPE_GID;
756 test_map.sid = &testsid;
758 status = idmap_tdb_common_unixid_to_sid(dom, &test_map);
759 if(!NT_STATUS_IS_OK(status)) {
760 DEBUG(0, ("test_unixid2sid3: unixid2sid failed for gid!\n"));
761 return false;
764 if(test_map.xid.type!=gid_map.xid.type) {
765 DEBUG(0, ("test_unixid2sid3: unixid2sid returned wrong type!\n"));
766 return false;
769 if(!dom_sid_equal(test_map.sid,gid_map.sid)) {
770 DEBUG(0, ("test_unixid2sid3: unixid2sid returned wrong SID!\n"));
771 return false;
774 DEBUG(0, ("test_unixid2sid3: PASSED!\n"));
776 return true;
779 static bool test_unixids2sids1(TALLOC_CTX *memctx, struct idmap_domain *dom)
781 NTSTATUS status;
782 struct id_map uid_map, gid_map, **test_maps;
784 ZERO_STRUCT(uid_map);
785 ZERO_STRUCT(gid_map);
787 /* create two mappings for a UID and GID */
789 uid_map.sid = dom_sid_parse_talloc(memctx, DOM_SID5 "-1000");
790 uid_map.xid.type = ID_TYPE_UID;
792 gid_map.sid = dom_sid_parse_talloc(memctx, DOM_SID5 "-1001");
793 gid_map.xid.type = ID_TYPE_GID;
795 status = idmap_tdb_common_new_mapping(dom, &uid_map);
796 if(!NT_STATUS_IS_OK(status)) {
797 DEBUG(0, ("test_unixids2sids1: could not create uid map!\n"));
798 return false;
801 status = idmap_tdb_common_new_mapping(dom, &gid_map);
802 if(!NT_STATUS_IS_OK(status)) {
803 DEBUG(0, ("test_unixids2sids1: could not create gid map!\n"));
804 return false;
807 /* now read them back */
808 test_maps = talloc_zero_array(memctx, struct id_map*, 3);
810 test_maps[0] = talloc(test_maps, struct id_map);
811 test_maps[1] = talloc(test_maps, struct id_map);
812 test_maps[2] = NULL;
814 test_maps[0]->sid = talloc(test_maps, struct dom_sid);
815 test_maps[1]->sid = talloc(test_maps, struct dom_sid);
816 test_maps[0]->xid.id = uid_map.xid.id;
817 test_maps[0]->xid.type = ID_TYPE_UID;
818 test_maps[1]->xid.id = gid_map.xid.id;
819 test_maps[1]->xid.type = ID_TYPE_GID;
821 status = idmap_tdb_common_unixids_to_sids(dom, test_maps);
822 if(!NT_STATUS_IS_OK(status)) {
823 DEBUG(0, ("test_unixids2sids1: unixids2sids failed!\n"));
824 talloc_free(test_maps);
825 return false;
828 if(!dom_sid_equal(test_maps[0]->sid, uid_map.sid) ||
829 !dom_sid_equal(test_maps[1]->sid, gid_map.sid) ) {
830 DEBUG(0, ("test_unixids2sids1: unixids2sids returned wrong sid!\n"));
831 talloc_free(test_maps);
832 return false;
835 DEBUG(0, ("test_unixids2sids1: PASSED!\n"));
837 talloc_free(test_maps);
839 return true;
842 static bool test_unixids2sids2(TALLOC_CTX *memctx, struct idmap_domain *dom)
844 NTSTATUS status;
845 struct id_map **test_maps;
846 bool retval = true;
848 test_maps = talloc_zero_array(memctx, struct id_map*, 3);
850 test_maps[0] = talloc(test_maps, struct id_map);
851 test_maps[1] = talloc(test_maps, struct id_map);
852 test_maps[2] = NULL;
854 /* ask for two unknown mappings for a UID and GID */
855 test_maps[0]->sid = talloc(test_maps, struct dom_sid);
856 test_maps[1]->sid = talloc(test_maps, struct dom_sid);
857 test_maps[0]->xid.id = HIGH_ID - 1;
858 test_maps[0]->xid.type = ID_TYPE_UID;
859 test_maps[1]->xid.id = HIGH_ID - 1;
860 test_maps[1]->xid.type = ID_TYPE_GID;
862 status = idmap_tdb_common_unixids_to_sids(dom, test_maps);
863 if(NT_STATUS_IS_OK(status)) {
864 DEBUG(0, ("test_unixids2sids2: unixids2sids succeeded "
865 "unexpectedly!\n"));
866 retval = false;
867 goto out;
870 DEBUG(0, ("test_unixids2sids2: PASSED!\n"));
872 out:
873 talloc_free(test_maps);
875 return retval;;
878 static bool test_unixids2sids3(TALLOC_CTX *memctx, struct idmap_domain *dom)
880 NTSTATUS status;
881 struct id_map uid_map, gid_map, **test_maps;
882 bool retval = true;
884 ZERO_STRUCT(uid_map);
885 ZERO_STRUCT(gid_map);
887 /* create two mappings for a UID and GID */
888 uid_map.sid = dom_sid_parse_talloc(memctx, DOM_SID6 "-1000");
889 uid_map.xid.type = ID_TYPE_UID;
891 gid_map.sid = dom_sid_parse_talloc(memctx, DOM_SID6 "-1001");
892 gid_map.xid.type = ID_TYPE_GID;
894 status = idmap_tdb_common_new_mapping(dom, &uid_map);
895 if(!NT_STATUS_IS_OK(status)) {
896 DEBUG(0, ("test_unixids2sids3: could not create uid map!\n"));
897 return false;
900 status = idmap_tdb_common_new_mapping(dom, &gid_map);
901 if(!NT_STATUS_IS_OK(status)) {
902 DEBUG(0, ("test_unixids2sids3: could not create gid map!\n"));
903 return false;
907 * check the mapping states:
908 * NONE_MAPPED, SOME_UNMAPPED, OK (all mapped)
910 test_maps = talloc_zero_array(memctx, struct id_map*, 3);
912 test_maps[0] = talloc(test_maps, struct id_map);
913 test_maps[1] = talloc(test_maps, struct id_map);
914 test_maps[2] = NULL;
916 /* NONE_MAPPED first */
917 test_maps[0]->sid = talloc(test_maps, struct dom_sid);
918 test_maps[1]->sid = talloc(test_maps, struct dom_sid);
920 test_maps[0]->xid.id = HIGH_ID - 1;
921 test_maps[0]->xid.type = ID_TYPE_UID;
923 test_maps[1]->xid.id = HIGH_ID - 1;
924 test_maps[1]->xid.type = ID_TYPE_GID;
926 status = idmap_tdb_common_unixids_to_sids(dom, test_maps);
927 if(!NT_STATUS_EQUAL(status, NT_STATUS_NONE_MAPPED)) {
928 DEBUG(0, ("test_unixids2sids3: incorrect status "
929 "(%s), expected NT_STATUS_NONE_MAPPED!\n",
930 nt_errstr(status)));
931 retval = false;
932 goto out;
935 /* SOME_UNMAPPED */
936 test_maps[0]->sid = talloc(test_maps, struct dom_sid);
937 test_maps[1]->sid = talloc(test_maps, struct dom_sid);
938 test_maps[0]->xid = uid_map.xid;
939 test_maps[1]->xid.id = HIGH_ID - 1;
940 test_maps[1]->xid.type = ID_TYPE_GID;
942 status = idmap_tdb_common_unixids_to_sids(dom, test_maps);
943 if(!NT_STATUS_EQUAL(status, STATUS_SOME_UNMAPPED)) {
944 DEBUG(0, ("test_unixids2sids3: incorrect status "
945 "(%s), expected STATUS_SOME_UNMAPPED!\n",
946 nt_errstr(status)));
947 retval = false;
948 goto out;
951 /* OK */
952 test_maps[0]->sid = talloc(test_maps, struct dom_sid);
953 test_maps[1]->sid = talloc(test_maps, struct dom_sid);
954 test_maps[0]->xid = uid_map.xid;
955 test_maps[1]->xid = gid_map.xid;
957 status = idmap_tdb_common_unixids_to_sids(dom, test_maps);
958 if(!NT_STATUS_IS_OK(status)) {
959 DEBUG(0, ("test_unixids2sids3: incorrect status "
960 "(%s), expected NT_STATUS_OK!\n",
961 nt_errstr(status)));
962 retval = false;
963 goto out;
966 DEBUG(0, ("test_unixids2sids3: PASSED!\n"));
968 out:
969 talloc_free(test_maps);
970 return retval;
973 #define CHECKRESULT(r) if(!r) {return r;}
975 bool run_idmap_tdb_common_test(int dummy)
977 bool result;
978 struct idmap_tdb_common_context *ctx;
979 struct idmap_domain *dom;
981 TALLOC_CTX *memctx = talloc_new(NULL);
982 TALLOC_CTX *stack = talloc_stackframe();
984 ctx = createcontext(memctx);
985 if(!ctx) {
986 return false;
989 dom = createdomain(memctx);
991 dom->private_data = ctx;
993 /* test a single allocation from pool (no mapping) */
994 result = test_getnewid1(memctx, dom);
995 CHECKRESULT(result);
997 /* test idmap_tdb_common_set_mapping */
998 result = test_setmap1(memctx, dom);
999 CHECKRESULT(result);
1001 /* test idmap_tdb_common_sid_to_unixid */
1002 result = test_sid2unixid1(memctx, dom);
1003 CHECKRESULT(result);
1004 result = test_sid2unixid2(memctx, dom);
1005 CHECKRESULT(result);
1007 /* test idmap_tdb_common_sids_to_unixids */
1008 result = test_sids2unixids1(memctx, dom);
1009 CHECKRESULT(result);
1010 result = test_sids2unixids2(memctx, dom);
1011 CHECKRESULT(result);
1012 result = test_sids2unixids3(memctx, dom);
1013 CHECKRESULT(result);
1015 /* test idmap_tdb_common_unixid_to_sid */
1016 result = test_unixid2sid1(memctx, dom);
1017 CHECKRESULT(result);
1018 result = test_unixid2sid2(memctx, dom);
1019 CHECKRESULT(result);
1020 result = test_unixid2sid3(memctx, dom);
1021 CHECKRESULT(result);
1023 /* test idmap_tdb_common_unixids_to_sids */
1024 result = test_unixids2sids1(memctx, dom);
1025 CHECKRESULT(result);
1026 result = test_unixids2sids2(memctx, dom);
1027 CHECKRESULT(result);
1028 result = test_unixids2sids3(memctx, dom);
1029 CHECKRESULT(result);
1031 /* test filling up the range */
1032 result = test_getnewid2(memctx, dom);
1033 CHECKRESULT(result);
1035 talloc_free(memctx);
1036 talloc_free(stack);
1038 return true;