e0b436962e04bd9d5e9d6678ddf86de9bf3ddc6b
[ira/wip.git] / nsswitch / libwbclient / tests / wbclient.c
1 /*
2    Unix SMB/CIFS implementation.
3    SMB torture tester
4    Copyright (C) Guenther Deschner 2009-2010
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 "nsswitch/libwbclient/wbclient.h"
22 #include "torture/smbtorture.h"
23 #include "torture/winbind/proto.h"
24
25 #define WBC_ERROR_EQUAL(x,y) (x == y)
26
27 #define torture_assert_wbc_equal(torture_ctx, got, expected, cmt) \
28         do { wbcErr __got = got, __expected = expected; \
29         if (!WBC_ERROR_EQUAL(__got, __expected)) { \
30                 torture_result(torture_ctx, TORTURE_FAIL, __location__": "#got" was %s, expected %s: %s", wbcErrorString(__got), wbcErrorString(__expected), cmt); \
31                 return false; \
32         } \
33         } while (0)
34
35 #define torture_assert_wbc_ok(torture_ctx,expr,cmt) \
36                 torture_assert_wbc_equal(torture_ctx,expr,WBC_ERR_SUCCESS,cmt)
37
38 static bool test_wbc_ping(struct torture_context *tctx)
39 {
40         torture_assert_wbc_ok(tctx, wbcPing(),
41                 "wbcPing failed");
42
43         return true;
44 }
45
46 static bool test_wbc_pingdc(struct torture_context *tctx)
47 {
48         torture_assert_wbc_equal(tctx, wbcPingDc("random_string", NULL), WBC_ERR_NOT_IMPLEMENTED,
49                 "wbcPingDc failed");
50         torture_assert_wbc_ok(tctx, wbcPingDc(NULL, NULL),
51                 "wbcPingDc failed");
52
53         return true;
54 }
55
56 static bool test_wbc_library_details(struct torture_context *tctx)
57 {
58         struct wbcLibraryDetails *details;
59
60         torture_assert_wbc_ok(tctx, wbcLibraryDetails(&details),
61                 "wbcLibraryDetails failed");
62         torture_assert(tctx, details,
63                 "wbcLibraryDetails returned NULL pointer");
64
65         wbcFreeMemory(details);
66
67         return true;
68 }
69
70 static bool test_wbc_interface_details(struct torture_context *tctx)
71 {
72         struct wbcInterfaceDetails *details;
73
74         torture_assert_wbc_ok(tctx, wbcInterfaceDetails(&details),
75                 "wbcInterfaceDetails failed");
76         torture_assert(tctx, details,
77                 "wbcInterfaceDetails returned NULL pointer");
78
79         wbcFreeMemory(details);
80
81         return true;
82 }
83
84 static bool test_wbc_sidtypestring(struct torture_context *tctx)
85 {
86         torture_assert_str_equal(tctx, wbcSidTypeString(WBC_SID_NAME_USE_NONE),
87                                  "SID_NONE", "SID_NONE failed");
88         torture_assert_str_equal(tctx, wbcSidTypeString(WBC_SID_NAME_USER),
89                                  "SID_USER", "SID_USER failed");
90         torture_assert_str_equal(tctx, wbcSidTypeString(WBC_SID_NAME_DOM_GRP),
91                                  "SID_DOM_GROUP", "SID_DOM_GROUP failed");
92         torture_assert_str_equal(tctx, wbcSidTypeString(WBC_SID_NAME_DOMAIN),
93                                  "SID_DOMAIN", "SID_DOMAIN failed");
94         torture_assert_str_equal(tctx, wbcSidTypeString(WBC_SID_NAME_ALIAS),
95                                  "SID_ALIAS", "SID_ALIAS failed");
96         torture_assert_str_equal(tctx, wbcSidTypeString(WBC_SID_NAME_WKN_GRP),
97                                  "SID_WKN_GROUP", "SID_WKN_GROUP failed");
98         torture_assert_str_equal(tctx, wbcSidTypeString(WBC_SID_NAME_DELETED),
99                                  "SID_DELETED", "SID_DELETED failed");
100         torture_assert_str_equal(tctx, wbcSidTypeString(WBC_SID_NAME_INVALID),
101                                  "SID_INVALID", "SID_INVALID failed");
102         torture_assert_str_equal(tctx, wbcSidTypeString(WBC_SID_NAME_UNKNOWN),
103                                  "SID_UNKNOWN", "SID_UNKNOWN failed");
104         torture_assert_str_equal(tctx, wbcSidTypeString(WBC_SID_NAME_COMPUTER),
105                                  "SID_COMPUTER", "SID_COMPUTER failed");
106         return true;
107 }
108
109 static bool test_wbc_sidtostring(struct torture_context *tctx)
110 {
111         struct wbcDomainSid sid;
112         const char *sid_string = "S-1-5-32";
113         char *sid_string2;
114
115         torture_assert_wbc_ok(tctx, wbcStringToSid(sid_string, &sid),
116                 "wbcStringToSid failed");
117         torture_assert_wbc_ok(tctx, wbcSidToString(&sid, &sid_string2),
118                 "wbcSidToString failed");
119         torture_assert_str_equal(tctx, sid_string, sid_string2,
120                 "sid strings differ");
121
122         return true;
123 }
124
125 static bool test_wbc_guidtostring(struct torture_context *tctx)
126 {
127         struct wbcGuid guid;
128         const char *guid_string = "f7cf07b4-1487-45c7-824d-8b18cc580811";
129         char *guid_string2;
130
131         torture_assert_wbc_ok(tctx, wbcStringToGuid(guid_string, &guid),
132                 "wbcStringToGuid failed");
133         torture_assert_wbc_ok(tctx, wbcGuidToString(&guid, &guid_string2),
134                 "wbcGuidToString failed");
135         torture_assert_str_equal(tctx, guid_string, guid_string2,
136                 "guid strings differ");
137
138         return true;
139 }
140
141 static bool test_wbc_domain_info(struct torture_context *tctx)
142 {
143         const char *domain_name = NULL;
144         struct wbcDomainInfo *info;
145         struct wbcInterfaceDetails *details;
146
147         torture_assert_wbc_ok(tctx, wbcInterfaceDetails(&details),
148                 "wbcInterfaceDetails failed");
149
150         domain_name = talloc_strdup(tctx, details->netbios_domain);
151         wbcFreeMemory(details);
152
153         torture_assert_wbc_ok(tctx, wbcDomainInfo(domain_name, &info),
154                 "wbcDomainInfo failed");
155         torture_assert(tctx, info,
156                 "wbcDomainInfo returned NULL pointer");
157
158         return true;
159 }
160
161 static bool test_wbc_users(struct torture_context *tctx)
162 {
163         const char *domain_name = NULL;
164         uint32_t num_users;
165         const char **users;
166         int i;
167         struct wbcInterfaceDetails *details;
168
169         torture_assert_wbc_ok(tctx, wbcInterfaceDetails(&details),
170                 "wbcInterfaceDetails failed");
171
172         domain_name = talloc_strdup(tctx, details->netbios_domain);
173         wbcFreeMemory(details);
174
175         torture_assert_wbc_ok(tctx, wbcListUsers(domain_name, &num_users, &users),
176                 "wbcListUsers failed");
177         torture_assert(tctx, !(num_users > 0 && !users),
178                 "wbcListUsers returned invalid results");
179
180         for (i=0; i < MIN(num_users,100); i++) {
181
182                 struct wbcDomainSid sid, *sids;
183                 enum wbcSidType name_type;
184                 char *domain;
185                 char *name;
186                 uint32_t num_sids;
187
188                 torture_assert_wbc_ok(tctx, wbcLookupName(domain_name, users[i], &sid, &name_type),
189                         "wbcLookupName failed");
190                 torture_assert_int_equal(tctx, name_type, WBC_SID_NAME_USER,
191                         "wbcLookupName expected WBC_SID_NAME_USER");
192                 torture_assert_wbc_ok(tctx, wbcLookupSid(&sid, &domain, &name, &name_type),
193                         "wbcLookupSid failed");
194                 torture_assert_int_equal(tctx, name_type, WBC_SID_NAME_USER,
195                         "wbcLookupSid expected WBC_SID_NAME_USER");
196                 torture_assert(tctx, name,
197                         "wbcLookupSid returned no name");
198                 torture_assert_wbc_ok(tctx, wbcLookupUserSids(&sid, true, &num_sids, &sids),
199                         "wbcLookupUserSids failed");
200         }
201
202         return true;
203 }
204
205 static bool test_wbc_groups(struct torture_context *tctx)
206 {
207         const char *domain_name = NULL;
208         uint32_t num_groups;
209         const char **groups;
210         int i;
211         struct wbcInterfaceDetails *details;
212
213         torture_assert_wbc_ok(tctx, wbcInterfaceDetails(&details),
214                 "wbcInterfaceDetails failed");
215
216         domain_name = talloc_strdup(tctx, details->netbios_domain);
217         wbcFreeMemory(details);
218
219         torture_assert_wbc_ok(tctx, wbcListGroups(domain_name, &num_groups, &groups),
220                 "wbcListGroups failed");
221         torture_assert(tctx, !(num_groups > 0 && !groups),
222                 "wbcListGroups returned invalid results");
223
224         for (i=0; i < MIN(num_groups,100); i++) {
225
226                 struct wbcDomainSid sid;
227                 enum wbcSidType name_type;
228                 char *domain;
229                 char *name;
230
231                 torture_assert_wbc_ok(tctx, wbcLookupName(domain_name, groups[i], &sid, &name_type),
232                         "wbcLookupName failed");
233                 torture_assert_wbc_ok(tctx, wbcLookupSid(&sid, &domain, &name, &name_type),
234                         "wbcLookupSid failed");
235                 torture_assert(tctx, name,
236                         "wbcLookupSid returned no name");
237         }
238
239         return true;
240 }
241
242 static bool test_wbc_trusts(struct torture_context *tctx)
243 {
244         struct wbcDomainInfo *domains;
245         size_t num_domains;
246         int i;
247
248         torture_assert_wbc_ok(tctx, wbcListTrusts(&domains, &num_domains),
249                 "wbcListTrusts failed");
250         torture_assert(tctx, !(num_domains > 0 && !domains),
251                 "wbcListTrusts returned invalid results");
252
253         for (i=0; i < MIN(num_domains,100); i++) {
254
255                 struct wbcAuthErrorInfo *error;
256                 /*
257                 struct wbcDomainSid sid;
258                 enum wbcSidType name_type;
259                 char *domain;
260                 char *name;
261                 */
262                 torture_assert_wbc_ok(tctx, wbcCheckTrustCredentials(domains[i].short_name, &error),
263                         "wbcCheckTrustCredentials failed");
264                 /*
265                 torture_assert_wbc_ok(tctx, wbcLookupName(domains[i].short_name, NULL, &sid, &name_type),
266                         "wbcLookupName failed");
267                 torture_assert_int_equal(tctx, name_type, WBC_SID_NAME_DOMAIN,
268                         "wbcLookupName expected WBC_SID_NAME_DOMAIN");
269                 torture_assert_wbc_ok(tctx, wbcLookupSid(&sid, &domain, &name, &name_type),
270                         "wbcLookupSid failed");
271                 torture_assert_int_equal(tctx, name_type, WBC_SID_NAME_DOMAIN,
272                         "wbcLookupSid expected WBC_SID_NAME_DOMAIN");
273                 torture_assert(tctx, name,
274                         "wbcLookupSid returned no name");
275                 */
276         }
277
278         return true;
279 }
280
281 static bool test_wbc_lookupdc(struct torture_context *tctx)
282 {
283         const char *domain_name = NULL;
284         struct wbcInterfaceDetails *details;
285         struct wbcDomainControllerInfo *dc_info;
286
287         torture_assert_wbc_ok(tctx, wbcInterfaceDetails(&details),
288                 "wbcInterfaceDetails failed");
289
290         domain_name = talloc_strdup(tctx, details->netbios_domain);
291         wbcFreeMemory(details);
292
293         torture_assert_wbc_ok(tctx, wbcLookupDomainController(domain_name, 0, &dc_info),
294                 "wbcLookupDomainController failed");
295
296         return true;
297 }
298
299 static bool test_wbc_lookupdcex(struct torture_context *tctx)
300 {
301         const char *domain_name = NULL;
302         struct wbcInterfaceDetails *details;
303         struct wbcDomainControllerInfoEx *dc_info;
304
305         torture_assert_wbc_ok(tctx, wbcInterfaceDetails(&details),
306                 "wbcInterfaceDetails failed");
307
308         domain_name = talloc_strdup(tctx, details->netbios_domain);
309         wbcFreeMemory(details);
310
311         torture_assert_wbc_ok(tctx, wbcLookupDomainControllerEx(domain_name, NULL, NULL, 0, &dc_info),
312                 "wbcLookupDomainControllerEx failed");
313
314         return true;
315 }
316
317
318 struct torture_suite *torture_wbclient(void)
319 {
320         struct torture_suite *suite = torture_suite_create(talloc_autofree_context(), "WBCLIENT");
321
322         torture_suite_add_simple_test(suite, "wbcPing", test_wbc_ping);
323         torture_suite_add_simple_test(suite, "wbcPingDc", test_wbc_pingdc);
324         torture_suite_add_simple_test(suite, "wbcLibraryDetails", test_wbc_library_details);
325         torture_suite_add_simple_test(suite, "wbcInterfaceDetails", test_wbc_interface_details);
326         torture_suite_add_simple_test(suite, "wbcSidTypeString", test_wbc_sidtypestring);
327         torture_suite_add_simple_test(suite, "wbcSidToString", test_wbc_sidtostring);
328         torture_suite_add_simple_test(suite, "wbcGuidToString", test_wbc_guidtostring);
329         torture_suite_add_simple_test(suite, "wbcDomainInfo", test_wbc_domain_info);
330         torture_suite_add_simple_test(suite, "wbcListUsers", test_wbc_users);
331         torture_suite_add_simple_test(suite, "wbcListGroups", test_wbc_groups);
332         torture_suite_add_simple_test(suite, "wbcListTrusts", test_wbc_trusts);
333         torture_suite_add_simple_test(suite, "wbcLookupDomainController", test_wbc_lookupdc);
334         torture_suite_add_simple_test(suite, "wbcLookupDomainControllerEx", test_wbc_lookupdcex);
335
336         return suite;
337 }