s3:smbd: change blocking.c to use fsp_fnum_dbg() for fsp->fnum logging.
[Samba/gebeck_regimport.git] / source3 / torture / test_idmap_tdb_common.c
blob7e7c5dd2b6d9f7970ebd93dcdc92713fc94efc67
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_sid_to_unixid(const char *domain, struct id_map *id)
67 return NT_STATUS_OK;
70 NTSTATUS idmap_backends_unixid_to_sid(const char *domname, struct id_map *id)
72 return NT_STATUS_OK;
75 static bool open_db(struct idmap_tdb_common_context *ctx)
77 NTSTATUS status;
78 char *db_path;
80 if(ctx->db) {
81 /* already open */
82 return true;
85 db_path = talloc_asprintf(talloc_tos(), "%s/idmap_test.tdb",
86 lp_private_dir());
87 if(!db_path) {
88 DEBUG(0, ("Out of memory!\n"));
89 return false;
92 ctx->db = db_open(ctx, db_path, 0, TDB_DEFAULT | TDB_CLEAR_IF_FIRST,
93 O_RDWR | O_CREAT, 0600,
94 DBWRAP_LOCK_ORDER_1);
96 if(!ctx->db) {
97 DEBUG(0, ("Failed to open database: %s\n", strerror(errno)));
98 return false;
101 if(dbwrap_transaction_start(ctx->db) != 0) {
102 DEBUG(0, ("Failed to start transaction!\n"));
103 return false;
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);
109 return false;
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);
115 return false;
118 if(dbwrap_transaction_commit(ctx->db) != 0) {
119 DEBUG(0, ("Failed to commit transaction!\n"));
120 return false;
123 return true;
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;
140 if (!open_db(ret)) {
141 return NULL;
144 return ret;
147 static struct idmap_domain *createdomain(TALLOC_CTX *memctx)
149 struct idmap_domain *dom;
151 dom = talloc_zero(memctx, struct idmap_domain);
152 dom->name = "*";
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;
161 return dom;
164 static bool test_getnewid1(TALLOC_CTX *memctx, struct idmap_domain *dom)
166 NTSTATUS status;
167 struct unixid id;
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"));
175 return false;
178 if(id.id == 0) {
179 DEBUG(0, ("test_getnewid1: Allocate returned "
180 "empty id!\n"));
181 return false;
184 if(id.id > HIGH_ID || id.id < LOW_ID) {
185 DEBUG(0, ("test_getnewid1: Allocate returned "
186 "out of range id!\n"));
187 return false;
190 DEBUG(0, ("test_getnewid1: PASSED!\n"));
192 return true;
195 static bool test_getnewid2(TALLOC_CTX *memctx, struct idmap_domain *dom)
197 NTSTATUS status;
198 struct unixid id;
199 int i, left;
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"));
207 return false;
210 if(id.id == 0) {
211 DEBUG(0, ("test_getnewid2: Allocate returned "
212 "empty id!\n"));
213 return false;
216 if(id.id > HIGH_ID || id.id < LOW_ID) {
217 DEBUG(0, ("test_getnewid2: Allocate returned "
218 "out of range id!\n"));
219 return false;
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)));
234 return false;
237 if(id.id > HIGH_ID) {
238 DEBUG(0, ("test_getnewid2: Allocate returned "
239 "out of range id (%d)!\n", id.id));
240 return false;
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));
250 return false;
253 DEBUG(0, ("test_getnewid2: PASSED!\n"));
255 return true;
258 static bool test_setmap1(TALLOC_CTX *memctx, struct idmap_domain *dom)
260 NTSTATUS status;
261 struct id_map map;
263 ZERO_STRUCT(map);
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"));
270 return false;
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"));
276 return false;
279 map.sid = dom_sid_parse_talloc(memctx, DOM_SID1 "-100");
281 map.xid.type = ID_TYPE_NOT_SPECIFIED;
282 map.xid.id = 4711;
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"));
287 return false;
290 /* now the good ones */
291 map.xid.type = ID_TYPE_UID;
292 map.xid.id = 0;
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"));
297 return false;
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"));
303 return false;
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"));
313 return false;
316 /* now a group with a different SID*/
317 map.xid.id = 0;
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"));
324 return false;
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"));
330 return false;
332 DEBUG(0, ("test_setmap1: PASSED!\n"));
334 return true;
337 static bool test_sid2unixid1(TALLOC_CTX *memctx, struct idmap_domain *dom)
339 NTSTATUS status1, status2, status3;
340 struct id_map map;
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"));
351 return false;
354 DEBUG(0, ("test_unixid2sid1: PASSED!\n"));
356 return true;
359 static bool test_sid2unixid2(TALLOC_CTX *memctx, struct idmap_domain *dom)
361 NTSTATUS status;
362 struct id_map uid_map, gid_map, test_map;
363 bool doagain = true;
365 ZERO_STRUCT(uid_map);
366 ZERO_STRUCT(gid_map);
368 /* create two mappings for a UID and GID */
370 again:
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"));
381 return false;
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"));
387 return false;
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"));
397 return false;
400 if(test_map.xid.id!=uid_map.xid.id) {
401 DEBUG(0, ("test_sid2unixid1: sid2unixid returned wrong uid!\n"));
402 return false;
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"));
410 return false;
413 if(test_map.xid.id!=gid_map.xid.id) {
414 DEBUG(0, ("test_sid2unixid1: sid2unixid returned wrong gid!\n"));
415 return false;
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
422 if(doagain) {
423 doagain = false;
424 goto again;
427 DEBUG(0, ("test_sid2unixid1: PASSED!\n"));
429 return true;
432 static bool test_sids2unixids1(TALLOC_CTX *memctx, struct idmap_domain *dom)
434 NTSTATUS status;
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"));
451 return false;
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"));
457 return false;
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);
465 test_maps[2] = NULL;
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);
476 return false;
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);
483 return false;
486 DEBUG(0, ("test_sids2unixids1: PASSED!\n"));
488 talloc_free(test_maps);
490 return true;
493 static bool test_sids2unixids2(TALLOC_CTX *memctx, struct idmap_domain *dom)
495 NTSTATUS status;
496 struct id_map **test_maps;
497 struct unixid save;
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);
503 test_maps[2] = NULL;
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);
516 return false;
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);
523 return false;
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);
533 test_maps[2] = NULL;
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);
545 return false;
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);
553 return false;
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);
560 return false;
563 DEBUG(0, ("test_sids2unixids2: PASSED!\n"));
565 talloc_free(test_maps);
567 return true;
570 static bool test_sids2unixids3(TALLOC_CTX *memctx, struct idmap_domain *dom)
572 NTSTATUS status;
573 struct id_map **test_maps;
574 bool retval = true;
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);
590 test_maps[2] = NULL;
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,
596 "S-1-5-21-1-2-3-4");
597 test_maps[0]->xid.type = ID_TYPE_UID;
599 test_maps[1]->sid = dom_sid_parse_talloc(test_maps,
600 "S-1-5-21-1-2-3-5");
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",
607 nt_errstr(status)));
608 retval = false;
609 goto out;
612 /* SOME_UNMAPPED */
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,
616 DOM_SID4 "-1000");
617 test_maps[0]->xid.type = ID_TYPE_UID;
618 test_maps[1]->sid = dom_sid_parse_talloc(test_maps,
619 "S-1-5-21-1-2-3-5");
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",
626 nt_errstr(status)));
627 retval = false;
628 goto out;
631 /* OK */
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,
635 DOM_SID4 "-1001");
636 test_maps[1]->sid = dom_sid_parse_talloc(test_maps,
637 DOM_SID4 "-1000");
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",
643 nt_errstr(status)));
644 retval = false;
645 goto out;
648 DEBUG(0, ("test_sids2unixids3: PASSED!\n"));
650 out:
651 talloc_free(test_maps);
652 dom->read_only = false;
653 return retval;
656 static bool test_unixid2sid1(TALLOC_CTX *memctx, struct idmap_domain *dom)
658 NTSTATUS status1, status2, status3;
659 struct id_map map;
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"));
670 return false;
673 DEBUG(0, ("test_unixid2sid1: PASSED!\n"));
675 return true;
678 static bool test_unixid2sid2(TALLOC_CTX *memctx, struct idmap_domain *dom)
680 NTSTATUS status;
681 struct id_map *map;
682 bool retval = true;
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"));
695 retval = false;
696 goto out;
699 DEBUG(0, ("test_unixid2sid2: PASSED!\n"));
700 out:
701 talloc_free(map);
702 return retval;
706 static bool test_unixid2sid3(TALLOC_CTX *memctx, struct idmap_domain *dom)
708 NTSTATUS status;
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"));
725 return false;
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"));
731 return false;
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"));
743 return false;
746 if(test_map.xid.type!=uid_map.xid.type) {
747 DEBUG(0, ("test_unixid2sid3: unixid2sid returned wrong type!\n"));
748 return false;
751 if(!dom_sid_equal(test_map.sid, uid_map.sid)) {
752 DEBUG(0, ("test_unixid2sid3: unixid2sid returned wrong SID!\n"));
753 return false;
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"));
764 return false;
767 if(test_map.xid.type!=gid_map.xid.type) {
768 DEBUG(0, ("test_unixid2sid3: unixid2sid returned wrong type!\n"));
769 return false;
772 if(!dom_sid_equal(test_map.sid,gid_map.sid)) {
773 DEBUG(0, ("test_unixid2sid3: unixid2sid returned wrong SID!\n"));
774 return false;
777 DEBUG(0, ("test_unixid2sid3: PASSED!\n"));
779 return true;
782 static bool test_unixids2sids1(TALLOC_CTX *memctx, struct idmap_domain *dom)
784 NTSTATUS status;
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"));
801 return false;
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"));
807 return false;
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);
815 test_maps[2] = NULL;
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);
828 return false;
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);
835 return false;
838 DEBUG(0, ("test_unixids2sids1: PASSED!\n"));
840 talloc_free(test_maps);
842 return true;
845 static bool test_unixids2sids2(TALLOC_CTX *memctx, struct idmap_domain *dom)
847 NTSTATUS status;
848 struct id_map **test_maps;
849 bool retval = true;
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);
855 test_maps[2] = NULL;
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 "
868 "unexpectedly!\n"));
869 retval = false;
870 goto out;
873 DEBUG(0, ("test_unixids2sids2: PASSED!\n"));
875 out:
876 talloc_free(test_maps);
878 return retval;;
881 static bool test_unixids2sids3(TALLOC_CTX *memctx, struct idmap_domain *dom)
883 NTSTATUS status;
884 struct id_map uid_map, gid_map, **test_maps;
885 bool retval = true;
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"));
900 return false;
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"));
906 return false;
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);
917 test_maps[2] = NULL;
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",
933 nt_errstr(status)));
934 retval = false;
935 goto out;
938 /* SOME_UNMAPPED */
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",
949 nt_errstr(status)));
950 retval = false;
951 goto out;
954 /* OK */
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",
964 nt_errstr(status)));
965 retval = false;
966 goto out;
969 DEBUG(0, ("test_unixids2sids3: PASSED!\n"));
971 out:
972 talloc_free(test_maps);
973 return retval;
976 #define CHECKRESULT(r) if(!r) {return r;}
978 bool run_idmap_tdb_common_test(int dummy)
980 bool result;
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);
988 if(!ctx) {
989 return false;
992 dom = createdomain(memctx);
994 dom->private_data = ctx;
996 /* test a single allocation from pool (no mapping) */
997 result = test_getnewid1(memctx, dom);
998 CHECKRESULT(result);
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);
1039 talloc_free(stack);
1041 return true;