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