5a55a43ceb0c9caf8d4b38253572f536a260f996
[ira/wip.git] / nsswitch / libwbclient / tests / wbclient.c
1 /*
2    Unix SMB/CIFS implementation.
3    SMB torture tester
4    Copyright (C) Guenther Deschner 2009
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_library_details(struct torture_context *tctx)
47 {
48         struct wbcLibraryDetails *details;
49
50         torture_assert_wbc_ok(tctx, wbcLibraryDetails(&details),
51                 "wbcLibraryDetails failed");
52         torture_assert(tctx, details,
53                 "wbcLibraryDetails returned NULL pointer");
54
55         wbcFreeMemory(details);
56
57         return true;
58 }
59
60 static bool test_wbc_interface_details(struct torture_context *tctx)
61 {
62         struct wbcInterfaceDetails *details;
63
64         torture_assert_wbc_ok(tctx, wbcInterfaceDetails(&details),
65                 "wbcInterfaceDetails failed");
66         torture_assert(tctx, details,
67                 "wbcInterfaceDetails returned NULL pointer");
68
69         wbcFreeMemory(details);
70
71         return true;
72 }
73
74 static bool test_wbc_sidtypestring(struct torture_context *tctx)
75 {
76         torture_assert_str_equal(tctx, wbcSidTypeString(WBC_SID_NAME_USE_NONE),
77                                  "SID_NONE", "SID_NONE failed");
78         torture_assert_str_equal(tctx, wbcSidTypeString(WBC_SID_NAME_USER),
79                                  "SID_USER", "SID_USER failed");
80         torture_assert_str_equal(tctx, wbcSidTypeString(WBC_SID_NAME_DOM_GRP),
81                                  "SID_DOM_GROUP", "SID_DOM_GROUP failed");
82         torture_assert_str_equal(tctx, wbcSidTypeString(WBC_SID_NAME_DOMAIN),
83                                  "SID_DOMAIN", "SID_DOMAIN failed");
84         torture_assert_str_equal(tctx, wbcSidTypeString(WBC_SID_NAME_ALIAS),
85                                  "SID_ALIAS", "SID_ALIAS failed");
86         torture_assert_str_equal(tctx, wbcSidTypeString(WBC_SID_NAME_WKN_GRP),
87                                  "SID_WKN_GROUP", "SID_WKN_GROUP failed");
88         torture_assert_str_equal(tctx, wbcSidTypeString(WBC_SID_NAME_DELETED),
89                                  "SID_DELETED", "SID_DELETED failed");
90         torture_assert_str_equal(tctx, wbcSidTypeString(WBC_SID_NAME_INVALID),
91                                  "SID_INVALID", "SID_INVALID failed");
92         torture_assert_str_equal(tctx, wbcSidTypeString(WBC_SID_NAME_UNKNOWN),
93                                  "SID_UNKNOWN", "SID_UNKNOWN failed");
94         torture_assert_str_equal(tctx, wbcSidTypeString(WBC_SID_NAME_COMPUTER),
95                                  "SID_COMPUTER", "SID_COMPUTER failed");
96         return true;
97 }
98
99 static bool test_wbc_sidtostring(struct torture_context *tctx)
100 {
101         struct wbcDomainSid sid;
102         const char *sid_string = "S-1-5-32";
103         char *sid_string2;
104
105         torture_assert_wbc_ok(tctx, wbcStringToSid(sid_string, &sid),
106                 "wbcStringToSid failed");
107         torture_assert_wbc_ok(tctx, wbcSidToString(&sid, &sid_string2),
108                 "wbcSidToString failed");
109         torture_assert_str_equal(tctx, sid_string, sid_string2,
110                 "sid strings differ");
111
112         return true;
113 }
114
115 static bool test_wbc_guidtostring(struct torture_context *tctx)
116 {
117         struct wbcGuid guid;
118         const char *guid_string = "f7cf07b4-1487-45c7-824d-8b18cc580811";
119         char *guid_string2;
120
121         torture_assert_wbc_ok(tctx, wbcStringToGuid(guid_string, &guid),
122                 "wbcStringToGuid failed");
123         torture_assert_wbc_ok(tctx, wbcGuidToString(&guid, &guid_string2),
124                 "wbcGuidToString failed");
125         torture_assert_str_equal(tctx, guid_string, guid_string2,
126                 "guid strings differ");
127
128         return true;
129 }
130
131 static bool test_wbc_domain_info(struct torture_context *tctx)
132 {
133         const char *domain_name = NULL;
134         struct wbcDomainInfo *info;
135         struct wbcInterfaceDetails *details;
136
137         torture_assert_wbc_ok(tctx, wbcInterfaceDetails(&details),
138                 "wbcInterfaceDetails failed");
139
140         domain_name = talloc_strdup(tctx, details->netbios_domain);
141         wbcFreeMemory(details);
142
143         torture_assert_wbc_ok(tctx, wbcDomainInfo(domain_name, &info),
144                 "wbcDomainInfo failed");
145         torture_assert(tctx, info,
146                 "wbcDomainInfo returned NULL pointer");
147
148         return true;
149 }
150
151 static bool test_wbc_users(struct torture_context *tctx)
152 {
153         const char *domain_name = NULL;
154         uint32_t num_users;
155         const char **users;
156         int i;
157         struct wbcInterfaceDetails *details;
158
159         torture_assert_wbc_ok(tctx, wbcInterfaceDetails(&details),
160                 "wbcInterfaceDetails failed");
161
162         domain_name = talloc_strdup(tctx, details->netbios_domain);
163         wbcFreeMemory(details);
164
165         torture_assert_wbc_ok(tctx, wbcListUsers(domain_name, &num_users, &users),
166                 "wbcListUsers failed");
167         torture_assert(tctx, !(num_users > 0 && !users),
168                 "wbcListUsers returned invalid results");
169
170         for (i=0; i < MIN(num_users,100); i++) {
171
172                 struct wbcDomainSid sid, *sids;
173                 enum wbcSidType name_type;
174                 char *domain;
175                 char *name;
176                 uint32_t num_sids;
177
178                 torture_assert_wbc_ok(tctx, wbcLookupName(domain_name, users[i], &sid, &name_type),
179                         "wbcLookupName failed");
180                 torture_assert_int_equal(tctx, name_type, WBC_SID_NAME_USER,
181                         "wbcLookupName expected WBC_SID_NAME_USER");
182                 torture_assert_wbc_ok(tctx, wbcLookupSid(&sid, &domain, &name, &name_type),
183                         "wbcLookupSid failed");
184                 torture_assert_int_equal(tctx, name_type, WBC_SID_NAME_USER,
185                         "wbcLookupSid expected WBC_SID_NAME_USER");
186                 torture_assert(tctx, name,
187                         "wbcLookupSid returned no name");
188                 torture_assert_wbc_ok(tctx, wbcLookupUserSids(&sid, true, &num_sids, &sids),
189                         "wbcLookupUserSids failed");
190         }
191
192         return true;
193 }
194
195 static bool test_wbc_groups(struct torture_context *tctx)
196 {
197         const char *domain_name = NULL;
198         uint32_t num_groups;
199         const char **groups;
200         int i;
201         struct wbcInterfaceDetails *details;
202
203         torture_assert_wbc_ok(tctx, wbcInterfaceDetails(&details),
204                 "wbcInterfaceDetails failed");
205
206         domain_name = talloc_strdup(tctx, details->netbios_domain);
207         wbcFreeMemory(details);
208
209         torture_assert_wbc_ok(tctx, wbcListGroups(domain_name, &num_groups, &groups),
210                 "wbcListGroups failed");
211         torture_assert(tctx, !(num_groups > 0 && !groups),
212                 "wbcListGroups returned invalid results");
213
214         for (i=0; i < MIN(num_groups,100); i++) {
215
216                 struct wbcDomainSid sid;
217                 enum wbcSidType name_type;
218                 char *domain;
219                 char *name;
220
221                 torture_assert_wbc_ok(tctx, wbcLookupName(domain_name, groups[i], &sid, &name_type),
222                         "wbcLookupName failed");
223                 torture_assert_wbc_ok(tctx, wbcLookupSid(&sid, &domain, &name, &name_type),
224                         "wbcLookupSid failed");
225                 torture_assert(tctx, name,
226                         "wbcLookupSid returned no name");
227         }
228
229         return true;
230 }
231
232 static bool test_wbc_trusts(struct torture_context *tctx)
233 {
234         struct wbcDomainInfo *domains;
235         size_t num_domains;
236         int i;
237
238         torture_assert_wbc_ok(tctx, wbcListTrusts(&domains, &num_domains),
239                 "wbcListTrusts failed");
240         torture_assert(tctx, !(num_domains > 0 && !domains),
241                 "wbcListTrusts returned invalid results");
242
243         for (i=0; i < MIN(num_domains,100); i++) {
244
245                 struct wbcAuthErrorInfo *error;
246                 /*
247                 struct wbcDomainSid sid;
248                 enum wbcSidType name_type;
249                 char *domain;
250                 char *name;
251                 */
252                 torture_assert_wbc_ok(tctx, wbcCheckTrustCredentials(domains[i].short_name, &error),
253                         "wbcCheckTrustCredentials failed");
254                 /*
255                 torture_assert_wbc_ok(tctx, wbcLookupName(domains[i].short_name, NULL, &sid, &name_type),
256                         "wbcLookupName failed");
257                 torture_assert_int_equal(tctx, name_type, WBC_SID_NAME_DOMAIN,
258                         "wbcLookupName expected WBC_SID_NAME_DOMAIN");
259                 torture_assert_wbc_ok(tctx, wbcLookupSid(&sid, &domain, &name, &name_type),
260                         "wbcLookupSid failed");
261                 torture_assert_int_equal(tctx, name_type, WBC_SID_NAME_DOMAIN,
262                         "wbcLookupSid expected WBC_SID_NAME_DOMAIN");
263                 torture_assert(tctx, name,
264                         "wbcLookupSid returned no name");
265                 */
266         }
267
268         return true;
269 }
270
271
272
273 struct torture_suite *torture_wbclient(void)
274 {
275         struct torture_suite *suite = torture_suite_create(talloc_autofree_context(), "WBCLIENT");
276
277         torture_suite_add_simple_test(suite, "wbcPing", test_wbc_ping);
278         torture_suite_add_simple_test(suite, "wbcLibraryDetails", test_wbc_library_details);
279         torture_suite_add_simple_test(suite, "wbcInterfaceDetails", test_wbc_interface_details);
280         torture_suite_add_simple_test(suite, "wbcSidTypeString", test_wbc_sidtypestring);
281         torture_suite_add_simple_test(suite, "wbcSidToString", test_wbc_sidtostring);
282         torture_suite_add_simple_test(suite, "wbcGuidToString", test_wbc_guidtostring);
283         torture_suite_add_simple_test(suite, "wbcDomainInfo", test_wbc_domain_info);
284         torture_suite_add_simple_test(suite, "wbcListUsers", test_wbc_users);
285         torture_suite_add_simple_test(suite, "wbcListGroups", test_wbc_groups);
286         torture_suite_add_simple_test(suite, "wbcListTrusts", test_wbc_trusts);
287
288         return suite;
289 }