s3: Fix a typo
[Samba/gebeck_regimport.git] / source3 / torture / test_idmap_tdb_common.c
blobd08166017a92858752b1ff7281159b2b95e474c6
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_bystring(ctx->db, ctx->hwmkey_uid,
107 LOW_ID);
108 if(!NT_STATUS_IS_OK(status)) {
109 dbwrap_transaction_cancel(ctx->db);
110 return false;
113 status = dbwrap_store_uint32_bystring(ctx->db, ctx->hwmkey_gid,
114 LOW_ID);
115 if(!NT_STATUS_IS_OK(status)) {
116 dbwrap_transaction_cancel(ctx->db);
117 return false;
120 if(dbwrap_transaction_commit(ctx->db) != 0) {
121 DEBUG(0, ("Failed to commit transaction!\n"));
122 return false;
125 return true;
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;
142 if (!open_db(ret)) {
143 return NULL;
146 return ret;
149 static struct idmap_domain *createdomain(TALLOC_CTX *memctx)
151 struct idmap_domain *dom;
153 dom = talloc_zero(memctx, struct idmap_domain);
154 dom->name = "*";
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;
163 return dom;
166 static bool test_getnewid1(TALLOC_CTX *memctx, struct idmap_domain *dom)
168 NTSTATUS status;
169 struct unixid id;
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"));
177 return false;
180 if(id.id == 0) {
181 DEBUG(0, ("test_getnewid1: Allocate returned "
182 "empty id!\n"));
183 return false;
186 if(id.id > HIGH_ID || id.id < LOW_ID) {
187 DEBUG(0, ("test_getnewid1: Allocate returned "
188 "out of range id!\n"));
189 return false;
192 DEBUG(0, ("test_getnewid1: PASSED!\n"));
194 return true;
197 static bool test_getnewid2(TALLOC_CTX *memctx, struct idmap_domain *dom)
199 NTSTATUS status;
200 struct unixid id;
201 int i, left;
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"));
209 return false;
212 if(id.id == 0) {
213 DEBUG(0, ("test_getnewid2: Allocate returned "
214 "empty id!\n"));
215 return false;
218 if(id.id > HIGH_ID || id.id < LOW_ID) {
219 DEBUG(0, ("test_getnewid2: Allocate returned "
220 "out of range id!\n"));
221 return false;
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)));
236 return false;
239 if(id.id > HIGH_ID) {
240 DEBUG(0, ("test_getnewid2: Allocate returned "
241 "out of range id (%d)!\n", id.id));
242 return false;
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));
252 return false;
255 DEBUG(0, ("test_getnewid2: PASSED!\n"));
257 return true;
260 static bool test_setmap1(TALLOC_CTX *memctx, struct idmap_domain *dom)
262 NTSTATUS status;
263 struct id_map map;
265 ZERO_STRUCT(map);
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"));
272 return false;
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"));
278 return false;
281 map.sid = dom_sid_parse_talloc(memctx, DOM_SID1 "-100");
283 map.xid.type = ID_TYPE_NOT_SPECIFIED;
284 map.xid.id = 4711;
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"));
289 return false;
292 /* now the good ones */
293 map.xid.type = ID_TYPE_UID;
294 map.xid.id = 0;
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"));
299 return false;
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"));
305 return false;
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"));
315 return false;
318 /* now a group with a different SID*/
319 map.xid.id = 0;
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"));
326 return false;
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"));
332 return false;
334 DEBUG(0, ("test_setmap1: PASSED!\n"));
336 return true;
339 static bool test_sid2unixid1(TALLOC_CTX *memctx, struct idmap_domain *dom)
341 NTSTATUS status1, status2, status3;
342 struct id_map map;
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"));
353 return false;
356 DEBUG(0, ("test_unixid2sid1: PASSED!\n"));
358 return true;
361 static bool test_sid2unixid2(TALLOC_CTX *memctx, struct idmap_domain *dom)
363 NTSTATUS status;
364 struct id_map uid_map, gid_map, test_map;
365 bool doagain = true;
367 ZERO_STRUCT(uid_map);
368 ZERO_STRUCT(gid_map);
370 /* create two mappings for a UID and GID */
372 again:
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"));
383 return false;
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"));
389 return false;
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"));
399 return false;
402 if(test_map.xid.id!=uid_map.xid.id) {
403 DEBUG(0, ("test_sid2unixid1: sid2unixid returned wrong uid!\n"));
404 return false;
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"));
412 return false;
415 if(test_map.xid.id!=gid_map.xid.id) {
416 DEBUG(0, ("test_sid2unixid1: sid2unixid returned wrong gid!\n"));
417 return false;
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
424 if(doagain) {
425 doagain = false;
426 goto again;
429 DEBUG(0, ("test_sid2unixid1: PASSED!\n"));
431 return true;
434 static bool test_sids2unixids1(TALLOC_CTX *memctx, struct idmap_domain *dom)
436 NTSTATUS status;
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"));
453 return false;
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"));
459 return false;
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);
467 test_maps[2] = NULL;
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);
478 return false;
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);
485 return false;
488 DEBUG(0, ("test_sids2unixids1: PASSED!\n"));
490 talloc_free(test_maps);
492 return true;
495 static bool test_sids2unixids2(TALLOC_CTX *memctx, struct idmap_domain *dom)
497 NTSTATUS status;
498 struct id_map **test_maps;
499 struct unixid save;
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);
505 test_maps[2] = NULL;
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);
518 return false;
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);
525 return false;
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);
535 test_maps[2] = NULL;
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);
547 return false;
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);
555 return false;
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);
562 return false;
565 DEBUG(0, ("test_sids2unixids2: PASSED!\n"));
567 talloc_free(test_maps);
569 return true;
572 static bool test_sids2unixids3(TALLOC_CTX *memctx, struct idmap_domain *dom)
574 NTSTATUS status;
575 struct id_map **test_maps;
576 bool retval = true;
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);
592 test_maps[2] = NULL;
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,
598 "S-1-5-21-1-2-3-4");
599 test_maps[0]->xid.type = ID_TYPE_UID;
601 test_maps[1]->sid = dom_sid_parse_talloc(test_maps,
602 "S-1-5-21-1-2-3-5");
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",
609 nt_errstr(status)));
610 retval = false;
611 goto out;
614 /* SOME_UNMAPPED */
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,
618 DOM_SID4 "-1000");
619 test_maps[0]->xid.type = ID_TYPE_UID;
620 test_maps[1]->sid = dom_sid_parse_talloc(test_maps,
621 "S-1-5-21-1-2-3-5");
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",
628 nt_errstr(status)));
629 retval = false;
630 goto out;
633 /* OK */
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,
637 DOM_SID4 "-1001");
638 test_maps[1]->sid = dom_sid_parse_talloc(test_maps,
639 DOM_SID4 "-1000");
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",
645 nt_errstr(status)));
646 retval = false;
647 goto out;
650 DEBUG(0, ("test_sids2unixids3: PASSED!\n"));
652 out:
653 talloc_free(test_maps);
654 dom->read_only = false;
655 return retval;
658 static bool test_unixid2sid1(TALLOC_CTX *memctx, struct idmap_domain *dom)
660 NTSTATUS status1, status2, status3;
661 struct id_map map;
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"));
672 return false;
675 DEBUG(0, ("test_unixid2sid1: PASSED!\n"));
677 return true;
680 static bool test_unixid2sid2(TALLOC_CTX *memctx, struct idmap_domain *dom)
682 NTSTATUS status;
683 struct id_map *map;
684 bool retval = true;
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"));
697 retval = false;
698 goto out;
701 DEBUG(0, ("test_unixid2sid2: PASSED!\n"));
702 out:
703 talloc_free(map);
704 return retval;
708 static bool test_unixid2sid3(TALLOC_CTX *memctx, struct idmap_domain *dom)
710 NTSTATUS status;
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"));
727 return false;
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"));
733 return false;
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"));
745 return false;
748 if(test_map.xid.type!=uid_map.xid.type) {
749 DEBUG(0, ("test_unixid2sid3: unixid2sid returned wrong type!\n"));
750 return false;
753 if(!dom_sid_equal(test_map.sid, uid_map.sid)) {
754 DEBUG(0, ("test_unixid2sid3: unixid2sid returned wrong SID!\n"));
755 return false;
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"));
766 return false;
769 if(test_map.xid.type!=gid_map.xid.type) {
770 DEBUG(0, ("test_unixid2sid3: unixid2sid returned wrong type!\n"));
771 return false;
774 if(!dom_sid_equal(test_map.sid,gid_map.sid)) {
775 DEBUG(0, ("test_unixid2sid3: unixid2sid returned wrong SID!\n"));
776 return false;
779 DEBUG(0, ("test_unixid2sid3: PASSED!\n"));
781 return true;
784 static bool test_unixids2sids1(TALLOC_CTX *memctx, struct idmap_domain *dom)
786 NTSTATUS status;
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"));
803 return false;
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"));
809 return false;
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);
817 test_maps[2] = NULL;
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);
830 return false;
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);
837 return false;
840 DEBUG(0, ("test_unixids2sids1: PASSED!\n"));
842 talloc_free(test_maps);
844 return true;
847 static bool test_unixids2sids2(TALLOC_CTX *memctx, struct idmap_domain *dom)
849 NTSTATUS status;
850 struct id_map **test_maps;
851 bool retval = true;
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);
857 test_maps[2] = NULL;
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 "
870 "unexpectedly!\n"));
871 retval = false;
872 goto out;
875 DEBUG(0, ("test_unixids2sids2: PASSED!\n"));
877 out:
878 talloc_free(test_maps);
880 return retval;;
883 static bool test_unixids2sids3(TALLOC_CTX *memctx, struct idmap_domain *dom)
885 NTSTATUS status;
886 struct id_map uid_map, gid_map, **test_maps;
887 bool retval = true;
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"));
902 return false;
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"));
908 return false;
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);
919 test_maps[2] = NULL;
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",
935 nt_errstr(status)));
936 retval = false;
937 goto out;
940 /* SOME_UNMAPPED */
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",
951 nt_errstr(status)));
952 retval = false;
953 goto out;
956 /* OK */
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",
966 nt_errstr(status)));
967 retval = false;
968 goto out;
971 DEBUG(0, ("test_unixids2sids3: PASSED!\n"));
973 out:
974 talloc_free(test_maps);
975 return retval;
978 #define CHECKRESULT(r) if(!r) {return r;}
980 bool run_idmap_tdb_common_test(int dummy)
982 bool result;
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);
990 if(!ctx) {
991 return false;
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);
1041 talloc_free(stack);
1043 return true;