s4-smbtorture: add very basic libwbclient testsuite.
[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_domain_info(struct torture_context *tctx)
100 {
101         const char *domain_name = NULL;
102         struct wbcDomainInfo *info;
103         struct wbcInterfaceDetails *details;
104
105         torture_assert_wbc_ok(tctx, wbcInterfaceDetails(&details),
106                 "wbcInterfaceDetails failed");
107
108         domain_name = talloc_strdup(tctx, details->netbios_domain);
109         wbcFreeMemory(details);
110
111         torture_assert_wbc_ok(tctx, wbcDomainInfo(domain_name, &info),
112                 "wbcDomainInfo failed");
113         torture_assert(tctx, info,
114                 "wbcDomainInfo returned NULL pointer");
115
116         return true;
117 }
118
119 static bool test_wbc_users(struct torture_context *tctx)
120 {
121         const char *domain_name = NULL;
122         uint32_t num_users;
123         const char **users;
124         int i;
125         struct wbcInterfaceDetails *details;
126
127         torture_assert_wbc_ok(tctx, wbcInterfaceDetails(&details),
128                 "wbcInterfaceDetails failed");
129
130         domain_name = talloc_strdup(tctx, details->netbios_domain);
131         wbcFreeMemory(details);
132
133         torture_assert_wbc_ok(tctx, wbcListUsers(domain_name, &num_users, &users),
134                 "wbcListUsers failed");
135         torture_assert(tctx, !(num_users > 0 && !users),
136                 "wbcListUsers returned invalid results");
137
138         for (i=0; i < MIN(num_users,100); i++) {
139
140                 struct wbcDomainSid sid;
141                 enum wbcSidType name_type;
142                 char *domain;
143                 char *name;
144
145                 torture_assert_wbc_ok(tctx, wbcLookupName(domain_name, users[i], &sid, &name_type),
146                         "wbcLookupName failed");
147                 torture_assert_int_equal(tctx, name_type, WBC_SID_NAME_USER,
148                         "wbcLookupName expected WBC_SID_NAME_USER");
149                 torture_assert_wbc_ok(tctx, wbcLookupSid(&sid, &domain, &name, &name_type),
150                         "wbcLookupSid failed");
151                 torture_assert_int_equal(tctx, name_type, WBC_SID_NAME_USER,
152                         "wbcLookupSid expected WBC_SID_NAME_USER");
153                 torture_assert(tctx, name,
154                         "wbcLookupSid returned no name");
155         }
156
157         return true;
158 }
159
160 static bool test_wbc_groups(struct torture_context *tctx)
161 {
162         const char *domain_name = NULL;
163         uint32_t num_groups;
164         const char **groups;
165         int i;
166         struct wbcInterfaceDetails *details;
167
168         torture_assert_wbc_ok(tctx, wbcInterfaceDetails(&details),
169                 "wbcInterfaceDetails failed");
170
171         domain_name = talloc_strdup(tctx, details->netbios_domain);
172         wbcFreeMemory(details);
173
174         torture_assert_wbc_ok(tctx, wbcListGroups(domain_name, &num_groups, &groups),
175                 "wbcListGroups failed");
176         torture_assert(tctx, !(num_groups > 0 && !groups),
177                 "wbcListGroups returned invalid results");
178
179         for (i=0; i < MIN(num_groups,100); i++) {
180
181                 struct wbcDomainSid sid;
182                 enum wbcSidType name_type;
183                 char *domain;
184                 char *name;
185
186                 torture_assert_wbc_ok(tctx, wbcLookupName(domain_name, groups[i], &sid, &name_type),
187                         "wbcLookupName failed");
188                 torture_assert_wbc_ok(tctx, wbcLookupSid(&sid, &domain, &name, &name_type),
189                         "wbcLookupSid failed");
190                 torture_assert(tctx, name,
191                         "wbcLookupSid returned no name");
192         }
193
194         return true;
195 }
196
197 static bool test_wbc_trusts(struct torture_context *tctx)
198 {
199         struct wbcDomainInfo *domains;
200         size_t num_domains;
201         int i;
202
203         torture_assert_wbc_ok(tctx, wbcListTrusts(&domains, &num_domains),
204                 "wbcListTrusts failed");
205         torture_assert(tctx, !(num_domains > 0 && !domains),
206                 "wbcListTrusts returned invalid results");
207
208         for (i=0; i < MIN(num_domains,100); i++) {
209
210                 struct wbcAuthErrorInfo *error;
211                 /*
212                 struct wbcDomainSid sid;
213                 enum wbcSidType name_type;
214                 char *domain;
215                 char *name;
216                 */
217                 torture_assert_wbc_ok(tctx, wbcCheckTrustCredentials(domains[i].short_name, &error),
218                         "wbcCheckTrustCredentials failed");
219                 /*
220                 torture_assert_wbc_ok(tctx, wbcLookupName(domains[i].short_name, NULL, &sid, &name_type),
221                         "wbcLookupName failed");
222                 torture_assert_int_equal(tctx, name_type, WBC_SID_NAME_DOMAIN,
223                         "wbcLookupName expected WBC_SID_NAME_DOMAIN");
224                 torture_assert_wbc_ok(tctx, wbcLookupSid(&sid, &domain, &name, &name_type),
225                         "wbcLookupSid failed");
226                 torture_assert_int_equal(tctx, name_type, WBC_SID_NAME_DOMAIN,
227                         "wbcLookupSid expected WBC_SID_NAME_DOMAIN");
228                 torture_assert(tctx, name,
229                         "wbcLookupSid returned no name");
230                 */
231         }
232
233         return true;
234 }
235
236
237
238 struct torture_suite *torture_wbclient(void)
239 {
240         struct torture_suite *suite = torture_suite_create(talloc_autofree_context(), "WBCLIENT");
241
242         torture_suite_add_simple_test(suite, "wbcPing", test_wbc_ping);
243         torture_suite_add_simple_test(suite, "wbcLibraryDetails", test_wbc_library_details);
244         torture_suite_add_simple_test(suite, "wbcInterfaceDetails", test_wbc_interface_details);
245         torture_suite_add_simple_test(suite, "wbcSidTypeString", test_wbc_sidtypestring);
246         torture_suite_add_simple_test(suite, "wbcDomainInfo", test_wbc_domain_info);
247         torture_suite_add_simple_test(suite, "wbcListUsers", test_wbc_users);
248         torture_suite_add_simple_test(suite, "wbcListGroups", test_wbc_groups);
249         torture_suite_add_simple_test(suite, "wbcListTrusts", test_wbc_trusts);
250
251         return suite;
252 }