lib/winbind_util: Add winbind_xid_to_sid for --without-winbind
[samba.git] / source3 / torture / test_idmap_cache.c
1 /*
2  * Unix SMB/CIFS implementation.
3  * Test dbwrap_watch API
4  * Copyright (C) Volker Lendecke 2017
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18  */
19
20 #include "includes.h"
21 #include "torture/proto.h"
22 #include "lib/idmap_cache.h"
23 #include "librpc/gen_ndr/idmap.h"
24 #include "libcli/security/dom_sid.h"
25
26 bool run_local_idmap_cache1(int dummy)
27 {
28         struct dom_sid sid, found_sid;
29         struct unixid xid, found_xid;
30         bool ret = false;
31         bool expired = false;
32
33         xid = (struct unixid) { .id = 1234, .type = ID_TYPE_UID };
34         dom_sid_parse("S-1-5-21-2864185242-3846410404-2398417794-1235", &sid);
35         idmap_cache_set_sid2unixid(&sid, &xid);
36
37         ret = idmap_cache_find_sid2unixid(&sid, &found_xid, &expired);
38         if (!ret) {
39                 fprintf(stderr, "idmap_cache_find_sid2unixid failed\n");
40                 goto done;
41         }
42         if (expired) {
43                 fprintf(stderr,
44                         "idmap_cache_find_sid2unixid returned an expired "
45                         "value\n");
46                 goto done;
47         }
48         if ((xid.type != found_xid.type) || (xid.id != found_xid.id)) {
49                 fprintf(stderr,
50                         "idmap_cache_find_sid2unixid returned wrong "
51                         "values\n");
52                 goto done;
53         }
54
55         ret = idmap_cache_find_xid2sid(&xid, &found_sid, &expired);
56         if (!ret) {
57                 fprintf(stderr, "idmap_cache_find_xid2sid failed\n");
58                 goto done;
59         }
60         if (expired) {
61                 fprintf(stderr,
62                         "idmap_cache_find_xid2sid returned an expired "
63                         "value\n");
64                 goto done;
65         }
66         if (!dom_sid_equal(&sid, &found_sid)) {
67                 fprintf(stderr,
68                         "idmap_cache_find_xid2sid returned wrong sid\n");
69                 goto done;
70         }
71
72         xid.type = ID_TYPE_GID;
73
74         ret = idmap_cache_find_xid2sid(&xid, &found_sid, &expired);
75         if (ret) {
76                 fprintf(stderr,
77                         "idmap_cache_find_xid2sid found a GID where it "
78                         "should not\n");
79                 goto done;
80         }
81
82         idmap_cache_del_sid(&sid);
83
84         xid.type = ID_TYPE_UID;
85         ret = idmap_cache_find_xid2sid(&xid, &found_sid, &expired);
86         if (ret) {
87                 fprintf(stderr,
88                         "idmap_cache_find_xid2sid found a UID where it "
89                         "should not\n");
90                 goto done;
91         }
92
93         /*
94          * Test that negative mappings can also be cached
95          */
96         sid = (struct dom_sid) {0};
97         xid = (struct unixid) { .id = 1234, .type = ID_TYPE_UID };
98         idmap_cache_set_sid2unixid(&sid, &xid);
99
100         ret = idmap_cache_find_xid2sid(&xid, &found_sid, &expired);
101         if (!ret) {
102                 fprintf(stderr,
103                         "idmap_cache_find_xid2sid failed to find "
104                         "negative mapping\n");
105                 goto done;
106         }
107         if (expired) {
108                 fprintf(stderr,
109                         "idmap_cache_find_xid2sid returned an expired "
110                         "value\n");
111                 goto done;
112         }
113         if (!dom_sid_equal(&sid, &found_sid)) {
114                 fprintf(stderr,
115                         "idmap_cache_find_xid2sid returned wrong sid\n");
116                 goto done;
117         }
118
119         ret = true;
120 done:
121         return ret;
122 }