libnet: Add NULL checks to py_net_finddc
[Samba.git] / source3 / torture / test_idmap_tdb_common.c
blob5ecb978990ddd4c4e2498f678f5d391b7a99115a
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 static bool open_db(struct idmap_tdb_common_context *ctx)
62 NTSTATUS status;
63 char *db_path;
65 if(ctx->db) {
66 /* already open */
67 return true;
70 db_path = talloc_asprintf(talloc_tos(), "%s/idmap_test.tdb",
71 lp_private_dir());
72 if(!db_path) {
73 DEBUG(0, ("Out of memory!\n"));
74 return false;
77 ctx->db = db_open(ctx, db_path, 0, TDB_DEFAULT,
78 O_RDWR | O_CREAT, 0600,
79 DBWRAP_LOCK_ORDER_1, DBWRAP_FLAG_NONE);
81 if(!ctx->db) {
82 DEBUG(0, ("Failed to open database: %s\n", strerror(errno)));
83 return false;
86 if(dbwrap_transaction_start(ctx->db) != 0) {
87 DEBUG(0, ("Failed to start transaction!\n"));
88 return false;
91 status = dbwrap_store_uint32_bystring(ctx->db, ctx->hwmkey_uid,
92 LOW_ID);
93 if(!NT_STATUS_IS_OK(status)) {
94 dbwrap_transaction_cancel(ctx->db);
95 return false;
98 status = dbwrap_store_uint32_bystring(ctx->db, ctx->hwmkey_gid,
99 LOW_ID);
100 if(!NT_STATUS_IS_OK(status)) {
101 dbwrap_transaction_cancel(ctx->db);
102 return false;
105 if(dbwrap_transaction_commit(ctx->db) != 0) {
106 DEBUG(0, ("Failed to commit transaction!\n"));
107 return false;
110 return true;
113 static struct idmap_tdb_common_context *createcontext(TALLOC_CTX *memctx)
115 struct idmap_tdb_common_context *ret;
117 ret = talloc_zero(memctx, struct idmap_tdb_common_context);
118 ret->rw_ops = talloc_zero(ret, struct idmap_rw_ops);
120 ret->max_id = HIGH_ID;
121 ret->hwmkey_uid = HWM_USER;
122 ret->hwmkey_gid = HWM_GROUP;
124 ret->rw_ops->get_new_id = idmap_tdb_common_get_new_id;
125 ret->rw_ops->set_mapping = idmap_tdb_common_set_mapping;
127 if (!open_db(ret)) {
128 return NULL;
131 return ret;
134 static struct idmap_domain *createdomain(TALLOC_CTX *memctx)
136 struct idmap_domain *dom;
138 dom = talloc_zero(memctx, struct idmap_domain);
139 dom->name = "*";
140 dom->low_id = LOW_ID;
141 dom->high_id = HIGH_ID;
142 dom->read_only = false;
143 dom->methods = talloc_zero(dom, struct idmap_methods);
144 dom->methods->sids_to_unixids = idmap_tdb_common_sids_to_unixids;
145 dom->methods->unixids_to_sids = idmap_tdb_common_unixids_to_sids;
146 dom->methods->allocate_id = idmap_tdb_common_get_new_id;
148 return dom;
151 static bool test_getnewid1(TALLOC_CTX *memctx, struct idmap_domain *dom)
153 NTSTATUS status;
154 struct unixid id;
156 id.type = ID_TYPE_UID;
158 status = idmap_tdb_common_get_new_id(dom, &id);
160 if(!NT_STATUS_IS_OK(status)) {
161 DEBUG(0, ("test_getnewid1: Could not allocate id!\n"));
162 return false;
165 if(id.id == 0) {
166 DEBUG(0, ("test_getnewid1: Allocate returned "
167 "empty id!\n"));
168 return false;
171 if(id.id > HIGH_ID || id.id < LOW_ID) {
172 DEBUG(0, ("test_getnewid1: Allocate returned "
173 "out of range id!\n"));
174 return false;
177 DEBUG(0, ("test_getnewid1: PASSED!\n"));
179 return true;
182 static bool test_getnewid2(TALLOC_CTX *memctx, struct idmap_domain *dom)
184 NTSTATUS status;
185 struct unixid id;
186 int i, left;
188 id.type = ID_TYPE_UID;
190 status = idmap_tdb_common_get_new_id(dom, &id);
192 if(!NT_STATUS_IS_OK(status)) {
193 DEBUG(0, ("test_getnewid2: Could not allocate id!\n"));
194 return false;
197 if(id.id == 0) {
198 DEBUG(0, ("test_getnewid2: Allocate returned "
199 "empty id!\n"));
200 return false;
203 if(id.id > HIGH_ID || id.id < LOW_ID) {
204 DEBUG(0, ("test_getnewid2: Allocate returned "
205 "out of range id!\n"));
206 return false;
209 /* how many ids are left? */
211 left = HIGH_ID - id.id;
213 /* consume them all */
214 for(i = 0; i<left; i++) {
216 status = idmap_tdb_common_get_new_id(dom, &id);
218 if(!NT_STATUS_IS_OK(status)) {
219 DEBUG(0, ("test_getnewid2: Allocate returned "
220 "error %s\n", nt_errstr(status)));
221 return false;
224 if(id.id > HIGH_ID) {
225 DEBUG(0, ("test_getnewid2: Allocate returned "
226 "out of range id (%d)!\n", id.id));
227 return false;
231 /* one more must fail */
232 status = idmap_tdb_common_get_new_id(dom, &id);
234 if(NT_STATUS_IS_OK(status)) {
235 DEBUG(0, ("test_getnewid2: Could allocate id (%d) from "
236 "depleted pool!\n", id.id));
237 return false;
240 DEBUG(0, ("test_getnewid2: PASSED!\n"));
242 return true;
245 static bool test_setmap1(TALLOC_CTX *memctx, struct idmap_domain *dom)
247 NTSTATUS status;
248 struct id_map map;
250 ZERO_STRUCT(map);
252 /* test for correct return code with invalid data */
254 status = idmap_tdb_common_set_mapping(dom, NULL);
255 if(!NT_STATUS_EQUAL(status, NT_STATUS_INVALID_PARAMETER)) {
256 DEBUG(0, ("test_setmap1: bad parameter handling!\n"));
257 return false;
260 status = idmap_tdb_common_set_mapping(dom, &map);
261 if(!NT_STATUS_EQUAL(status, NT_STATUS_INVALID_PARAMETER)) {
262 DEBUG(0, ("test_setmap1: bad parameter handling!\n"));
263 return false;
266 map.sid = dom_sid_parse_talloc(memctx, DOM_SID1 "-100");
268 map.xid.type = ID_TYPE_NOT_SPECIFIED;
269 map.xid.id = 4711;
271 status = idmap_tdb_common_set_mapping(dom, &map);
272 if(!NT_STATUS_EQUAL(status, NT_STATUS_INVALID_PARAMETER)) {
273 DEBUG(0, ("test_setmap1: bad parameter handling!\n"));
274 return false;
277 /* now the good ones */
278 map.xid.type = ID_TYPE_UID;
279 map.xid.id = 0;
281 status = idmap_tdb_common_get_new_id(dom, &(map.xid));
282 if(!NT_STATUS_IS_OK(status)) {
283 DEBUG(0, ("test_setmap1: get_new_uid failed!\n"));
284 return false;
287 status = idmap_tdb_common_set_mapping(dom, &map);
288 if(!NT_STATUS_IS_OK(status)) {
289 DEBUG(0, ("test_setmap1: setting UID mapping failed!\n"));
290 return false;
293 /* try to set the same mapping again as group (must fail) */
295 map.xid.type = ID_TYPE_GID;
296 status = idmap_tdb_common_set_mapping(dom, &map);
297 if(NT_STATUS_IS_OK(status)) {
298 DEBUG(0, ("test_setmap1: could create map for "
299 "group and user!\n"));
300 return false;
303 /* now a group with a different SID*/
304 map.xid.id = 0;
306 map.sid = dom_sid_parse_talloc(memctx, DOM_SID1 "-101");
308 status = idmap_tdb_common_get_new_id(dom, &(map.xid));
309 if(!NT_STATUS_IS_OK(status)) {
310 DEBUG(0, ("test_setmap1: get_new_gid failed!\n"));
311 return false;
314 status = idmap_tdb_common_set_mapping(dom, &map);
315 if(!NT_STATUS_IS_OK(status)) {
316 DEBUG(0, ("test_setmap1: setting GID mapping failed!\n"));
317 return false;
319 DEBUG(0, ("test_setmap1: PASSED!\n"));
321 return true;
324 static bool test_sid2unixid1(TALLOC_CTX *memctx, struct idmap_domain *dom)
326 NTSTATUS status1, status2, status3;
327 struct id_map map;
329 /* check for correct dealing with bad parameters */
330 status1 = idmap_tdb_common_sid_to_unixid(NULL, &map);
331 status2 = idmap_tdb_common_sid_to_unixid(dom, NULL);
332 status3 = idmap_tdb_common_sid_to_unixid(NULL, NULL);
334 if(!NT_STATUS_EQUAL(NT_STATUS_INVALID_PARAMETER, status1) ||
335 !NT_STATUS_EQUAL(NT_STATUS_INVALID_PARAMETER, status2) ||
336 !NT_STATUS_EQUAL(NT_STATUS_INVALID_PARAMETER, status3)) {
337 DEBUG(0, ("test_setmap1: bad parameter handling!\n"));
338 return false;
341 DEBUG(0, ("test_unixid2sid1: PASSED!\n"));
343 return true;
346 static bool test_sid2unixid2(TALLOC_CTX *memctx, struct idmap_domain *dom)
348 NTSTATUS status;
349 struct id_map uid_map, gid_map, test_map;
350 bool doagain = true;
352 ZERO_STRUCT(uid_map);
353 ZERO_STRUCT(gid_map);
355 /* create two mappings for a UID and GID */
357 again:
359 uid_map.sid = dom_sid_parse_talloc(memctx, DOM_SID2 "-1000");
360 uid_map.xid.type = ID_TYPE_UID;
362 gid_map.sid = dom_sid_parse_talloc(memctx, DOM_SID2 "-1001");
363 gid_map.xid.type = ID_TYPE_GID;
365 status = idmap_tdb_common_new_mapping(dom, &uid_map);
366 if(!NT_STATUS_IS_OK(status)) {
367 DEBUG(0, ("test_sid2unixid1: could not create uid map!\n"));
368 return false;
371 status = idmap_tdb_common_new_mapping(dom, &gid_map);
372 if(!NT_STATUS_IS_OK(status)) {
373 DEBUG(0, ("test_sid2unixid1: could not create gid map!\n"));
374 return false;
377 /* now read them back */
378 ZERO_STRUCT(test_map);
379 test_map.sid = uid_map.sid;
381 status = idmap_tdb_common_sid_to_unixid(dom, &test_map);
382 if(!NT_STATUS_IS_OK(status)) {
383 DEBUG(0, ("test_sid2unixid1: sid2unixid failed for uid!\n"));
384 return false;
387 if(test_map.xid.id!=uid_map.xid.id) {
388 DEBUG(0, ("test_sid2unixid1: sid2unixid returned wrong uid!\n"));
389 return false;
392 test_map.sid = gid_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 gid!\n"));
397 return false;
400 if(test_map.xid.id!=gid_map.xid.id) {
401 DEBUG(0, ("test_sid2unixid1: sid2unixid returned wrong gid!\n"));
402 return false;
406 * Go through the same tests again once to see if trying to recreate
407 * a mapping that was already created will work or not
409 if(doagain) {
410 doagain = false;
411 goto again;
414 DEBUG(0, ("test_sid2unixid1: PASSED!\n"));
416 return true;
419 static bool test_sids2unixids1(TALLOC_CTX *memctx, struct idmap_domain *dom)
421 NTSTATUS status;
422 struct id_map uid_map, gid_map, **test_maps;
424 ZERO_STRUCT(uid_map);
425 ZERO_STRUCT(gid_map);
427 /* create two mappings for a UID and GID */
429 uid_map.sid = dom_sid_parse_talloc(memctx, DOM_SID4 "-1000");
430 uid_map.xid.type = ID_TYPE_UID;
432 gid_map.sid = dom_sid_parse_talloc(memctx, DOM_SID4 "-1001");
433 gid_map.xid.type = ID_TYPE_GID;
435 status = idmap_tdb_common_new_mapping(dom, &uid_map);
436 if(!NT_STATUS_IS_OK(status)) {
437 DEBUG(0, ("test_sids2unixids1: could not create uid map!\n"));
438 return false;
441 status = idmap_tdb_common_new_mapping(dom, &gid_map);
442 if(!NT_STATUS_IS_OK(status)) {
443 DEBUG(0, ("test_sids2unixids1: could not create gid map!\n"));
444 return false;
447 /* now read them back */
448 test_maps = talloc_zero_array(memctx, struct id_map*, 3);
450 test_maps[0] = talloc(test_maps, struct id_map);
451 test_maps[1] = talloc(test_maps, struct id_map);
452 test_maps[2] = NULL;
454 test_maps[0]->sid = talloc(test_maps, struct dom_sid);
455 test_maps[1]->sid = talloc(test_maps, struct dom_sid);
456 sid_copy(test_maps[0]->sid, uid_map.sid);
457 sid_copy(test_maps[1]->sid, gid_map.sid);
459 status = idmap_tdb_common_sids_to_unixids(dom, test_maps);
460 if(!NT_STATUS_IS_OK(status)) {
461 DEBUG(0, ("test_sids2sunixids1: sids2unixids failed!\n"));
462 talloc_free(test_maps);
463 return false;
466 if(test_maps[0]->xid.id!=uid_map.xid.id ||
467 test_maps[1]->xid.id!=gid_map.xid.id ) {
468 DEBUG(0, ("test_sids2unixids1: sid2unixid returned wrong xid!\n"));
469 talloc_free(test_maps);
470 return false;
473 DEBUG(0, ("test_sids2unixids1: PASSED!\n"));
475 talloc_free(test_maps);
477 return true;
480 static bool test_sids2unixids2(TALLOC_CTX *memctx, struct idmap_domain *dom)
482 NTSTATUS status;
483 struct id_map **test_maps;
484 struct unixid save;
486 test_maps = talloc_zero_array(memctx, struct id_map*, 3);
488 test_maps[0] = talloc(test_maps, struct id_map);
489 test_maps[1] = talloc(test_maps, struct id_map);
490 test_maps[2] = NULL;
492 /* ask for two new mappings for a UID and GID */
493 test_maps[0]->sid = dom_sid_parse_talloc(test_maps, DOM_SID4 "-1003");
494 test_maps[0]->xid.type = ID_TYPE_UID;
495 test_maps[1]->sid = dom_sid_parse_talloc(test_maps, DOM_SID4 "-1004");
496 test_maps[1]->xid.type = ID_TYPE_GID;
498 status = idmap_tdb_common_sids_to_unixids(dom, test_maps);
499 if(!NT_STATUS_IS_OK(status)) {
500 DEBUG(0, ("test_sids2sunixids2: sids2unixids "
501 "failed (%s)!\n", nt_errstr(status)));
502 talloc_free(test_maps);
503 return false;
506 if(test_maps[0]->xid.id == 0 || test_maps[1]->xid.id == 0) {
507 DEBUG(0, ("test_sids2sunixids2: sids2unixids "
508 "returned zero ids!\n"));
509 talloc_free(test_maps);
510 return false;
513 save = test_maps[1]->xid;
515 /* ask for a known and a new mapping at the same time */
516 talloc_free(test_maps);
517 test_maps = talloc_zero_array(memctx, struct id_map*, 3);
518 test_maps[0] = talloc(test_maps, struct id_map);
519 test_maps[1] = talloc(test_maps, struct id_map);
520 test_maps[2] = NULL;
522 test_maps[0]->sid = dom_sid_parse_talloc(test_maps, DOM_SID4 "-1004");
523 test_maps[0]->xid.type = ID_TYPE_GID;
524 test_maps[1]->sid = dom_sid_parse_talloc(test_maps, DOM_SID4 "-1005");
525 test_maps[1]->xid.type = ID_TYPE_UID;
527 status = idmap_tdb_common_sids_to_unixids(dom, test_maps);
528 if(!NT_STATUS_IS_OK(status)) {
529 DEBUG(0, ("test_sids2sunixids2: sids2unixids (2) "
530 "failed (%s)!\n", nt_errstr(status)));
531 talloc_free(test_maps);
532 return false;
535 if(test_maps[0]->xid.type != save.type ||
536 test_maps[0]->xid.id != save.id) {
537 DEBUG(0, ("test_sids2sunixids2: second lookup returned "
538 "different value!\n"));
539 talloc_free(test_maps);
540 return false;
543 if(test_maps[1]->xid.id == 0) {
544 DEBUG(0, ("test_sids2sunixids2: sids2unixids "
545 "returned zero id for mixed mapping request!\n"));
546 talloc_free(test_maps);
547 return false;
550 DEBUG(0, ("test_sids2unixids2: PASSED!\n"));
552 talloc_free(test_maps);
554 return true;
557 static bool test_sids2unixids3(TALLOC_CTX *memctx, struct idmap_domain *dom)
559 NTSTATUS status;
560 struct id_map **test_maps;
561 bool retval = true;
564 * check the mapping states:
565 * NONE_MAPPED, SOME_UNMAPPED, OK (all mapped)
567 * use the ids created by test_sids2unixids1
568 * need to make dom read-only
571 dom->read_only = true;
573 test_maps = talloc_zero_array(memctx, struct id_map*, 3);
575 test_maps[0] = talloc(test_maps, struct id_map);
576 test_maps[1] = talloc(test_maps, struct id_map);
577 test_maps[2] = NULL;
579 /* NONE_MAPPED first */
580 test_maps[0]->sid = talloc(test_maps, struct dom_sid);
581 test_maps[1]->sid = talloc(test_maps, struct dom_sid);
582 test_maps[0]->sid = dom_sid_parse_talloc(test_maps,
583 "S-1-5-21-1-2-3-4");
584 test_maps[0]->xid.type = ID_TYPE_UID;
586 test_maps[1]->sid = dom_sid_parse_talloc(test_maps,
587 "S-1-5-21-1-2-3-5");
588 test_maps[1]->xid.type = ID_TYPE_GID;
590 status = idmap_tdb_common_sids_to_unixids(dom, test_maps);
591 if(!NT_STATUS_EQUAL(status, NT_STATUS_NONE_MAPPED)) {
592 DEBUG(0, ("test_sids2unixids3: incorrect status "
593 "(%s), expected NT_STATUS_NONE_MAPPED!\n",
594 nt_errstr(status)));
595 retval = false;
596 goto out;
599 /* SOME_UNMAPPED */
600 test_maps[0]->sid = talloc(test_maps, struct dom_sid);
601 test_maps[1]->sid = talloc(test_maps, struct dom_sid);
602 test_maps[0]->sid = dom_sid_parse_talloc(test_maps,
603 DOM_SID4 "-1000");
604 test_maps[0]->xid.type = ID_TYPE_UID;
605 test_maps[1]->sid = dom_sid_parse_talloc(test_maps,
606 "S-1-5-21-1-2-3-5");
607 test_maps[1]->xid.type = ID_TYPE_GID;
609 status = idmap_tdb_common_sids_to_unixids(dom, test_maps);
610 if(!NT_STATUS_EQUAL(status, STATUS_SOME_UNMAPPED)) {
611 DEBUG(0, ("test_sids2unixids3: incorrect status "
612 "(%s), expected STATUS_SOME_UNMAPPED!\n",
613 nt_errstr(status)));
614 retval = false;
615 goto out;
618 /* OK */
619 test_maps[0]->sid = talloc(test_maps, struct dom_sid);
620 test_maps[1]->sid = talloc(test_maps, struct dom_sid);
621 test_maps[0]->sid = dom_sid_parse_talloc(test_maps,
622 DOM_SID4 "-1001");
623 test_maps[1]->sid = dom_sid_parse_talloc(test_maps,
624 DOM_SID4 "-1000");
626 status = idmap_tdb_common_sids_to_unixids(dom, test_maps);
627 if(!NT_STATUS_IS_OK(status)) {
628 DEBUG(0, ("test_sids2unixids3: incorrect status "
629 "(%s), expected NT_STATUS_OK!\n",
630 nt_errstr(status)));
631 retval = false;
632 goto out;
635 DEBUG(0, ("test_sids2unixids3: PASSED!\n"));
637 out:
638 talloc_free(test_maps);
639 dom->read_only = false;
640 return retval;
643 static bool test_unixid2sid1(TALLOC_CTX *memctx, struct idmap_domain *dom)
645 NTSTATUS status1, status2, status3;
646 struct id_map map;
648 /* check for correct dealing with bad parameters */
649 status1 = idmap_tdb_common_unixid_to_sid(NULL, &map);
650 status2 = idmap_tdb_common_unixid_to_sid(dom, NULL);
651 status3 = idmap_tdb_common_unixid_to_sid(NULL, NULL);
653 if(!NT_STATUS_EQUAL(NT_STATUS_INVALID_PARAMETER, status1) ||
654 !NT_STATUS_EQUAL(NT_STATUS_INVALID_PARAMETER, status2) ||
655 !NT_STATUS_EQUAL(NT_STATUS_INVALID_PARAMETER, status3)) {
656 DEBUG(0, ("test_setmap1: bad parameter handling!\n"));
657 return false;
660 DEBUG(0, ("test_unixid2sid1: PASSED!\n"));
662 return true;
665 static bool test_unixid2sid2(TALLOC_CTX *memctx, struct idmap_domain *dom)
667 NTSTATUS status;
668 struct id_map *map;
669 bool retval = true;
671 /* ask for mapping that is outside of the range */
672 map = talloc(memctx, struct id_map);
673 map->sid = talloc(map, struct dom_sid);
675 map->xid.type = ID_TYPE_UID;
676 map->xid.id = HIGH_ID + 1;
678 status = idmap_tdb_common_unixid_to_sid(dom, map);
679 if(NT_STATUS_IS_OK(status)) {
680 DEBUG(0, ("test_unixid2sid2: unixid2sid returned "
681 "out-of-range result\n"));
682 retval = false;
683 goto out;
686 DEBUG(0, ("test_unixid2sid2: PASSED!\n"));
687 out:
688 talloc_free(map);
689 return retval;
693 static bool test_unixid2sid3(TALLOC_CTX *memctx, struct idmap_domain *dom)
695 NTSTATUS status;
696 struct id_map uid_map, gid_map, test_map;
697 struct dom_sid testsid;
699 ZERO_STRUCT(uid_map);
700 ZERO_STRUCT(gid_map);
702 /* create two mappings for a UID and GID */
703 uid_map.sid = dom_sid_parse_talloc(memctx, DOM_SID3 "-1000");
704 uid_map.xid.type = ID_TYPE_UID;
706 gid_map.sid = dom_sid_parse_talloc(memctx, DOM_SID3 "-1001");
707 gid_map.xid.type = ID_TYPE_GID;
709 status = idmap_tdb_common_new_mapping(dom, &uid_map);
710 if(!NT_STATUS_IS_OK(status)) {
711 DEBUG(0, ("test_unixid2sid3: could not create uid map!\n"));
712 return false;
715 status = idmap_tdb_common_new_mapping(dom, &gid_map);
716 if(!NT_STATUS_IS_OK(status)) {
717 DEBUG(0, ("test_unixid2sid3: could not create gid map!\n"));
718 return false;
721 /* now read them back */
722 ZERO_STRUCT(test_map);
723 test_map.xid.id = uid_map.xid.id;
724 test_map.xid.type = ID_TYPE_UID;
725 test_map.sid = &testsid;
727 status = idmap_tdb_common_unixid_to_sid(dom, &test_map);
728 if(!NT_STATUS_IS_OK(status)) {
729 DEBUG(0, ("test_unixid2sid3: unixid2sid failed for uid!\n"));
730 return false;
733 if(test_map.xid.type!=uid_map.xid.type) {
734 DEBUG(0, ("test_unixid2sid3: unixid2sid returned wrong type!\n"));
735 return false;
738 if(!dom_sid_equal(test_map.sid, uid_map.sid)) {
739 DEBUG(0, ("test_unixid2sid3: unixid2sid returned wrong SID!\n"));
740 return false;
743 ZERO_STRUCT(test_map);
744 test_map.xid.id = gid_map.xid.id;
745 test_map.xid.type = ID_TYPE_GID;
746 test_map.sid = &testsid;
748 status = idmap_tdb_common_unixid_to_sid(dom, &test_map);
749 if(!NT_STATUS_IS_OK(status)) {
750 DEBUG(0, ("test_unixid2sid3: unixid2sid failed for gid!\n"));
751 return false;
754 if(test_map.xid.type!=gid_map.xid.type) {
755 DEBUG(0, ("test_unixid2sid3: unixid2sid returned wrong type!\n"));
756 return false;
759 if(!dom_sid_equal(test_map.sid,gid_map.sid)) {
760 DEBUG(0, ("test_unixid2sid3: unixid2sid returned wrong SID!\n"));
761 return false;
764 DEBUG(0, ("test_unixid2sid3: PASSED!\n"));
766 return true;
769 static bool test_unixids2sids1(TALLOC_CTX *memctx, struct idmap_domain *dom)
771 NTSTATUS status;
772 struct id_map uid_map, gid_map, **test_maps;
774 ZERO_STRUCT(uid_map);
775 ZERO_STRUCT(gid_map);
777 /* create two mappings for a UID and GID */
779 uid_map.sid = dom_sid_parse_talloc(memctx, DOM_SID5 "-1000");
780 uid_map.xid.type = ID_TYPE_UID;
782 gid_map.sid = dom_sid_parse_talloc(memctx, DOM_SID5 "-1001");
783 gid_map.xid.type = ID_TYPE_GID;
785 status = idmap_tdb_common_new_mapping(dom, &uid_map);
786 if(!NT_STATUS_IS_OK(status)) {
787 DEBUG(0, ("test_unixids2sids1: could not create uid map!\n"));
788 return false;
791 status = idmap_tdb_common_new_mapping(dom, &gid_map);
792 if(!NT_STATUS_IS_OK(status)) {
793 DEBUG(0, ("test_unixids2sids1: could not create gid map!\n"));
794 return false;
797 /* now read them back */
798 test_maps = talloc_zero_array(memctx, struct id_map*, 3);
800 test_maps[0] = talloc(test_maps, struct id_map);
801 test_maps[1] = talloc(test_maps, struct id_map);
802 test_maps[2] = NULL;
804 test_maps[0]->sid = talloc(test_maps, struct dom_sid);
805 test_maps[1]->sid = talloc(test_maps, struct dom_sid);
806 test_maps[0]->xid.id = uid_map.xid.id;
807 test_maps[0]->xid.type = ID_TYPE_UID;
808 test_maps[1]->xid.id = gid_map.xid.id;
809 test_maps[1]->xid.type = ID_TYPE_GID;
811 status = idmap_tdb_common_unixids_to_sids(dom, test_maps);
812 if(!NT_STATUS_IS_OK(status)) {
813 DEBUG(0, ("test_unixids2sids1: unixids2sids failed!\n"));
814 talloc_free(test_maps);
815 return false;
818 if(!dom_sid_equal(test_maps[0]->sid, uid_map.sid) ||
819 !dom_sid_equal(test_maps[1]->sid, gid_map.sid) ) {
820 DEBUG(0, ("test_unixids2sids1: unixids2sids returned wrong sid!\n"));
821 talloc_free(test_maps);
822 return false;
825 DEBUG(0, ("test_unixids2sids1: PASSED!\n"));
827 talloc_free(test_maps);
829 return true;
832 static bool test_unixids2sids2(TALLOC_CTX *memctx, struct idmap_domain *dom)
834 NTSTATUS status;
835 struct id_map **test_maps;
836 bool retval = true;
838 test_maps = talloc_zero_array(memctx, struct id_map*, 3);
840 test_maps[0] = talloc(test_maps, struct id_map);
841 test_maps[1] = talloc(test_maps, struct id_map);
842 test_maps[2] = NULL;
844 /* ask for two unknown mappings for a UID and GID */
845 test_maps[0]->sid = talloc(test_maps, struct dom_sid);
846 test_maps[1]->sid = talloc(test_maps, struct dom_sid);
847 test_maps[0]->xid.id = HIGH_ID - 1;
848 test_maps[0]->xid.type = ID_TYPE_UID;
849 test_maps[1]->xid.id = HIGH_ID - 1;
850 test_maps[1]->xid.type = ID_TYPE_GID;
852 status = idmap_tdb_common_unixids_to_sids(dom, test_maps);
853 if(NT_STATUS_IS_OK(status)) {
854 DEBUG(0, ("test_unixids2sids2: unixids2sids succeeded "
855 "unexpectedly!\n"));
856 retval = false;
857 goto out;
860 DEBUG(0, ("test_unixids2sids2: PASSED!\n"));
862 out:
863 talloc_free(test_maps);
865 return retval;;
868 static bool test_unixids2sids3(TALLOC_CTX *memctx, struct idmap_domain *dom)
870 NTSTATUS status;
871 struct id_map uid_map, gid_map, **test_maps;
872 bool retval = true;
874 ZERO_STRUCT(uid_map);
875 ZERO_STRUCT(gid_map);
877 /* create two mappings for a UID and GID */
878 uid_map.sid = dom_sid_parse_talloc(memctx, DOM_SID6 "-1000");
879 uid_map.xid.type = ID_TYPE_UID;
881 gid_map.sid = dom_sid_parse_talloc(memctx, DOM_SID6 "-1001");
882 gid_map.xid.type = ID_TYPE_GID;
884 status = idmap_tdb_common_new_mapping(dom, &uid_map);
885 if(!NT_STATUS_IS_OK(status)) {
886 DEBUG(0, ("test_unixids2sids3: could not create uid map!\n"));
887 return false;
890 status = idmap_tdb_common_new_mapping(dom, &gid_map);
891 if(!NT_STATUS_IS_OK(status)) {
892 DEBUG(0, ("test_unixids2sids3: could not create gid map!\n"));
893 return false;
897 * check the mapping states:
898 * NONE_MAPPED, SOME_UNMAPPED, OK (all mapped)
900 test_maps = talloc_zero_array(memctx, struct id_map*, 3);
902 test_maps[0] = talloc(test_maps, struct id_map);
903 test_maps[1] = talloc(test_maps, struct id_map);
904 test_maps[2] = NULL;
906 /* NONE_MAPPED first */
907 test_maps[0]->sid = talloc(test_maps, struct dom_sid);
908 test_maps[1]->sid = talloc(test_maps, struct dom_sid);
910 test_maps[0]->xid.id = HIGH_ID - 1;
911 test_maps[0]->xid.type = ID_TYPE_UID;
913 test_maps[1]->xid.id = HIGH_ID - 1;
914 test_maps[1]->xid.type = ID_TYPE_GID;
916 status = idmap_tdb_common_unixids_to_sids(dom, test_maps);
917 if(!NT_STATUS_EQUAL(status, NT_STATUS_NONE_MAPPED)) {
918 DEBUG(0, ("test_unixids2sids3: incorrect status "
919 "(%s), expected NT_STATUS_NONE_MAPPED!\n",
920 nt_errstr(status)));
921 retval = false;
922 goto out;
925 /* SOME_UNMAPPED */
926 test_maps[0]->sid = talloc(test_maps, struct dom_sid);
927 test_maps[1]->sid = talloc(test_maps, struct dom_sid);
928 test_maps[0]->xid = uid_map.xid;
929 test_maps[1]->xid.id = HIGH_ID - 1;
930 test_maps[1]->xid.type = ID_TYPE_GID;
932 status = idmap_tdb_common_unixids_to_sids(dom, test_maps);
933 if(!NT_STATUS_EQUAL(status, STATUS_SOME_UNMAPPED)) {
934 DEBUG(0, ("test_unixids2sids3: incorrect status "
935 "(%s), expected STATUS_SOME_UNMAPPED!\n",
936 nt_errstr(status)));
937 retval = false;
938 goto out;
941 /* OK */
942 test_maps[0]->sid = talloc(test_maps, struct dom_sid);
943 test_maps[1]->sid = talloc(test_maps, struct dom_sid);
944 test_maps[0]->xid = uid_map.xid;
945 test_maps[1]->xid = gid_map.xid;
947 status = idmap_tdb_common_unixids_to_sids(dom, test_maps);
948 if(!NT_STATUS_IS_OK(status)) {
949 DEBUG(0, ("test_unixids2sids3: incorrect status "
950 "(%s), expected NT_STATUS_OK!\n",
951 nt_errstr(status)));
952 retval = false;
953 goto out;
956 DEBUG(0, ("test_unixids2sids3: PASSED!\n"));
958 out:
959 talloc_free(test_maps);
960 return retval;
963 #define CHECKRESULT(r) if(!r) {return r;}
965 bool run_idmap_tdb_common_test(int dummy)
967 bool result;
968 struct idmap_tdb_common_context *ctx;
969 struct idmap_domain *dom;
971 TALLOC_CTX *memctx = talloc_new(NULL);
972 TALLOC_CTX *stack = talloc_stackframe();
974 ctx = createcontext(memctx);
975 if(!ctx) {
976 return false;
979 dom = createdomain(memctx);
981 dom->private_data = ctx;
983 /* test a single allocation from pool (no mapping) */
984 result = test_getnewid1(memctx, dom);
985 CHECKRESULT(result);
987 /* test idmap_tdb_common_set_mapping */
988 result = test_setmap1(memctx, dom);
989 CHECKRESULT(result);
991 /* test idmap_tdb_common_sid_to_unixid */
992 result = test_sid2unixid1(memctx, dom);
993 CHECKRESULT(result);
994 result = test_sid2unixid2(memctx, dom);
995 CHECKRESULT(result);
997 /* test idmap_tdb_common_sids_to_unixids */
998 result = test_sids2unixids1(memctx, dom);
999 CHECKRESULT(result);
1000 result = test_sids2unixids2(memctx, dom);
1001 CHECKRESULT(result);
1002 result = test_sids2unixids3(memctx, dom);
1003 CHECKRESULT(result);
1005 /* test idmap_tdb_common_unixid_to_sid */
1006 result = test_unixid2sid1(memctx, dom);
1007 CHECKRESULT(result);
1008 result = test_unixid2sid2(memctx, dom);
1009 CHECKRESULT(result);
1010 result = test_unixid2sid3(memctx, dom);
1011 CHECKRESULT(result);
1013 /* test idmap_tdb_common_unixids_to_sids */
1014 result = test_unixids2sids1(memctx, dom);
1015 CHECKRESULT(result);
1016 result = test_unixids2sids2(memctx, dom);
1017 CHECKRESULT(result);
1018 result = test_unixids2sids3(memctx, dom);
1019 CHECKRESULT(result);
1021 /* test filling up the range */
1022 result = test_getnewid2(memctx, dom);
1023 CHECKRESULT(result);
1025 talloc_free(memctx);
1026 talloc_free(stack);
1028 return true;