torture3: Test namemap_cache
[nivanova/samba-autobuild/.git] / source3 / torture / test_namemap_cache.c
1 /*
2  * Unix SMB/CIFS implementation.
3  * namemap_cache.c
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/namemap_cache.h"
23 #include "libcli/security/dom_sid.h"
24
25 static const struct dom_sid domsid = {
26         1, 4, {0,0,0,0,0,5}, {21, 123, 456, 789}
27 };
28
29 static void namemap_cache1_fn1(const char *domain, const char *name,
30                                enum lsa_SidType type, time_t timeout,
31                                void *private_data)
32 {
33         bool *p_ok = private_data;
34         bool ok;
35
36         ok = strequal(domain, "nt authority");
37         ok &= strequal(name, "network");
38         ok &= (type == SID_NAME_WKN_GRP);
39
40         *p_ok = ok;
41 }
42
43 static void namemap_cache1_fn2(const struct dom_sid *sid,
44                                enum lsa_SidType type, time_t timeout,
45                                void *private_data)
46 {
47         bool *p_ok = private_data;
48         bool ok;
49
50         ok = dom_sid_equal(sid, &global_sid_Network);
51         ok &= (type == SID_NAME_WKN_GRP);
52
53         *p_ok = ok;
54 }
55
56 static void namemap_cache1_fn3(const char *domain, const char *name,
57                                enum lsa_SidType type, time_t timeout,
58                                void *private_data)
59 {
60         bool *p_ok = private_data;
61         bool ok;
62
63         ok = strequal(domain, "");
64         ok &= strequal(name, "everyone");
65         ok &= (type == SID_NAME_WKN_GRP);
66
67         *p_ok = ok;
68 }
69
70 static void namemap_cache1_fn4(const struct dom_sid *sid,
71                                enum lsa_SidType type, time_t timeout,
72                                void *private_data)
73 {
74         bool *p_ok = private_data;
75         bool ok;
76
77         ok = dom_sid_equal(sid, &global_sid_World);
78         ok &= (type == SID_NAME_WKN_GRP);
79
80         *p_ok = ok;
81 }
82
83 static void namemap_cache1_fn5(const char *domain, const char *name,
84                                enum lsa_SidType type, time_t timeout,
85                                void *private_data)
86 {
87         bool *p_ok = private_data;
88         bool ok;
89
90         ok = strequal(domain, "samba-dom");
91         ok &= strequal(name, "");
92         ok &= (type == SID_NAME_DOMAIN);
93
94         *p_ok = ok;
95 }
96
97 static void namemap_cache1_fn6(const struct dom_sid *sid,
98                                enum lsa_SidType type, time_t timeout,
99                                void *private_data)
100 {
101         bool *p_ok = private_data;
102         bool ok;
103
104         ok = dom_sid_equal(sid, &domsid);
105         ok &= (type == SID_NAME_DOMAIN);
106
107         *p_ok = ok;
108 }
109
110 bool run_local_namemap_cache1(int dummy)
111 {
112         bool found;
113         bool ok;
114
115         ok = gencache_set("SID2NAME/S-1-5-2", "invalid", time(NULL)+60);
116         if (!ok) {
117                 fprintf(stderr, "gencache_set failed\n");
118                 return false;
119         }
120
121         ok = namemap_cache_find_sid(&global_sid_Network, namemap_cache1_fn1,
122                                     &found);
123         if (ok) {
124                 fprintf(stderr, "namemap_cache_find_sid parsed valid value\n");
125                 return false;
126         }
127
128         ok = namemap_cache_set_sid2name(&global_sid_Network,
129                                         "NT Authority", "Network",
130                                         SID_NAME_WKN_GRP,
131                                         time(NULL) + 60);
132         if (!ok) {
133                 fprintf(stderr, "namemap_cache_set_sid2name failed\n");
134                 return false;
135         }
136
137         ok = namemap_cache_find_sid(&global_sid_Network, namemap_cache1_fn1,
138                                     &found);
139         if (!ok) {
140                 fprintf(stderr, "namecache_find_sid failed\n");
141                 return false;
142         }
143         if (!found) {
144                 fprintf(stderr, "wrong values found\n");
145                 return false;
146         }
147
148         ok = namemap_cache_set_name2sid("NT Authority", "Network",
149                                         &global_sid_Network,
150                                         SID_NAME_WKN_GRP,
151                                         time(NULL) + 60);
152         if (!ok) {
153                 fprintf(stderr, "namemap_cache_set_name2sid failed\n");
154                 return false;
155         }
156
157         ok = namemap_cache_find_name("nt authority", "network",
158                                      namemap_cache1_fn2, &found);
159         if (!ok) {
160                 fprintf(stderr, "namecache_find_name failed\n");
161                 return false;
162         }
163         if (!found) {
164                 fprintf(stderr, "wrong values found\n");
165                 return false;
166         }
167
168         ok = namemap_cache_find_name("foo", "bar", namemap_cache1_fn2, &found);
169         if (ok) {
170                 fprintf(stderr,
171                         "namemap_cache_find_name succeeded unexpectedly\n");
172                 return false;
173         }
174
175         /*
176          * Test "" domain name
177          */
178
179         ok = namemap_cache_set_sid2name(&global_sid_World, "", "Everyone",
180                                         SID_NAME_WKN_GRP,
181                                         time(NULL) + 60);
182         if (!ok) {
183                 fprintf(stderr, "namemap_cache_set_sid2name failed\n");
184                 return false;
185         }
186
187         ok = namemap_cache_find_sid(&global_sid_World, namemap_cache1_fn3,
188                                     &found);
189         if (!ok) {
190                 fprintf(stderr, "namecache_find_sid failed\n");
191                 return false;
192         }
193         if (!found) {
194                 fprintf(stderr, "wrong values found\n");
195                 return false;
196         }
197
198         ok = namemap_cache_set_name2sid("", "Everyone",
199                                         &global_sid_World, SID_NAME_WKN_GRP,
200                                         time(NULL) + 60);
201         if (!ok) {
202                 fprintf(stderr, "namemap_cache_set failed\n");
203                 return false;
204         }
205
206         ok = namemap_cache_find_name("", "everyone",
207                                      namemap_cache1_fn4, &found);
208         if (!ok) {
209                 fprintf(stderr, "namecache_find_name failed\n");
210                 return false;
211         }
212         if (!found) {
213                 fprintf(stderr, "wrong values found\n");
214                 return false;
215         }
216
217         /*
218          * Test domain only
219          */
220
221         ok = namemap_cache_set_sid2name(&domsid, "SAMBA-DOM", "",
222                                         SID_NAME_DOMAIN,
223                                         time(NULL) + 60);
224         if (!ok) {
225                 fprintf(stderr, "namemap_cache_set failed\n");
226                 return false;
227         }
228
229         ok = namemap_cache_find_sid(&domsid, namemap_cache1_fn5,
230                                     &found);
231         if (!ok) {
232                 fprintf(stderr, "namecache_find_sid failed\n");
233                 return false;
234         }
235         if (!found) {
236                 fprintf(stderr, "wrong values found\n");
237                 return false;
238         }
239
240         ok = namemap_cache_set_name2sid("SAMBA-DOM", "",
241                                         &domsid, SID_NAME_DOMAIN,
242                                         time(NULL) + 60);
243         if (!ok) {
244                 fprintf(stderr, "namemap_cache_set failed\n");
245                 return false;
246         }
247
248         ok = namemap_cache_find_name("samba-dom", "",
249                                      namemap_cache1_fn6, &found);
250         if (!ok) {
251                 fprintf(stderr, "namecache_find_name failed\n");
252                 return false;
253         }
254         if (!found) {
255                 fprintf(stderr, "wrong values found\n");
256                 return false;
257         }
258
259         gencache_stabilize();
260
261         return true;
262 }