s4: popt: Global replace of cmdline_credentials -> popt_get_cmdline_credentials().
[Samba.git] / source4 / torture / libsmbclient / libsmbclient.c
blobf6cd8102c4d5eb4c5b07d49165c0db78eac1a7ac
1 /*
2 Unix SMB/CIFS implementation.
3 SMB torture tester
4 Copyright (C) Guenther Deschner 2010
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.
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.
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/>.
20 #include "includes.h"
21 #include "torture/smbtorture.h"
22 #include "auth/credentials/credentials.h"
23 #include "lib/cmdline/popt_common.h"
24 #include <libsmbclient.h>
25 #include "torture/libsmbclient/proto.h"
27 bool torture_libsmbclient_init_context(struct torture_context *tctx,
28 SMBCCTX **ctx_p)
30 SMBCCTX *ctx;
32 ctx = smbc_new_context();
33 torture_assert(tctx, ctx, "failed to get new context");
34 torture_assert(tctx, smbc_init_context(ctx), "failed to init context");
36 smbc_setDebug(ctx, DEBUGLEVEL);
37 smbc_setOptionDebugToStderr(ctx, 1);
39 /* yes, libsmbclient API frees the username when freeing the context, so
40 * have to pass malloced data here */
41 smbc_setUser(ctx, strdup(cli_credentials_get_username(
42 popt_get_cmdline_credentials())));
44 *ctx_p = ctx;
46 return true;
49 static bool torture_libsmbclient_version(struct torture_context *tctx)
51 torture_comment(tctx, "Testing smbc_version\n");
53 torture_assert(tctx, smbc_version(), "failed to get version");
55 return true;
58 static bool torture_libsmbclient_initialize(struct torture_context *tctx)
60 SMBCCTX *ctx;
62 torture_comment(tctx, "Testing smbc_new_context\n");
64 ctx = smbc_new_context();
65 torture_assert(tctx, ctx, "failed to get new context");
67 torture_comment(tctx, "Testing smbc_init_context\n");
69 torture_assert(tctx, smbc_init_context(ctx), "failed to init context");
71 smbc_free_context(ctx, 1);
73 return true;
76 static bool test_opendir(struct torture_context *tctx,
77 SMBCCTX *ctx,
78 const char *fname,
79 bool expect_success)
81 int handle, ret;
83 torture_comment(tctx, "Testing smbc_opendir(%s)\n", fname);
85 handle = smbc_opendir(fname);
86 if (!expect_success) {
87 return true;
89 if (handle < 0) {
90 torture_fail(tctx, talloc_asprintf(tctx, "failed to obain file handle for '%s'", fname));
93 ret = smbc_closedir(handle);
94 torture_assert_int_equal(tctx, ret, 0,
95 talloc_asprintf(tctx, "failed to close file handle for '%s'", fname));
97 return true;
100 static bool torture_libsmbclient_opendir(struct torture_context *tctx)
102 int i;
103 SMBCCTX *ctx;
104 bool ret = true;
105 const char *bad_urls[] = {
107 NULL,
108 "smb",
109 "smb:",
110 "smb:/",
111 "smb:///",
112 "bms://",
113 ":",
114 ":/",
115 "://",
116 ":///",
117 "/",
118 "//",
119 "///"
121 const char *good_urls[] = {
122 "smb://",
123 "smb://WORKGROUP",
124 "smb://WORKGROUP/"
127 torture_assert(tctx, torture_libsmbclient_init_context(tctx, &ctx), "");
128 smbc_set_context(ctx);
130 for (i=0; i < ARRAY_SIZE(bad_urls); i++) {
131 ret &= test_opendir(tctx, ctx, bad_urls[i], false);
133 for (i=0; i < ARRAY_SIZE(good_urls); i++) {
134 ret &= test_opendir(tctx, ctx, good_urls[i], true);
137 smbc_free_context(ctx, 1);
139 return ret;
142 /* note the strdup for string options on smbc_set calls. I think libsmbclient is
143 * really doing something wrong here: in smbc_free_context libsmbclient just
144 * calls free() on the string options so it assumes the callers have malloced
145 * them before setting them via smbc_set calls. */
147 #define TEST_OPTION_INT(option, val) \
148 torture_comment(tctx, "Testing smbc_set" #option "\n");\
149 smbc_set ##option(ctx, val);\
150 torture_comment(tctx, "Testing smbc_get" #option "\n");\
151 torture_assert_int_equal(tctx, smbc_get ##option(ctx), val, "failed " #option);
153 #define TEST_OPTION_STRING(option, val) \
154 torture_comment(tctx, "Testing smbc_set" #option "\n");\
155 smbc_set ##option(ctx, strdup(val));\
156 torture_comment(tctx, "Testing smbc_get" #option "\n");\
157 torture_assert_str_equal(tctx, smbc_get ##option(ctx), val, "failed " #option);
159 bool torture_libsmbclient_configuration(struct torture_context *tctx)
161 SMBCCTX *ctx;
163 ctx = smbc_new_context();
164 torture_assert(tctx, ctx, "failed to get new context");
165 torture_assert(tctx, smbc_init_context(ctx), "failed to init context");
167 TEST_OPTION_INT(Debug, DEBUGLEVEL);
168 TEST_OPTION_STRING(NetbiosName, "torture_netbios");
169 TEST_OPTION_STRING(Workgroup, "torture_workgroup");
170 TEST_OPTION_STRING(User, "torture_user");
171 TEST_OPTION_INT(Timeout, 12345);
173 smbc_free_context(ctx, 1);
175 return true;
178 bool torture_libsmbclient_options(struct torture_context *tctx)
180 SMBCCTX *ctx;
182 ctx = smbc_new_context();
183 torture_assert(tctx, ctx, "failed to get new context");
184 torture_assert(tctx, smbc_init_context(ctx), "failed to init context");
186 TEST_OPTION_INT(OptionDebugToStderr, true);
187 TEST_OPTION_INT(OptionFullTimeNames, true);
188 TEST_OPTION_INT(OptionOpenShareMode, SMBC_SHAREMODE_DENY_ALL);
189 /* FIXME: OptionUserData */
190 TEST_OPTION_INT(OptionSmbEncryptionLevel, SMBC_ENCRYPTLEVEL_REQUEST);
191 TEST_OPTION_INT(OptionCaseSensitive, false);
192 TEST_OPTION_INT(OptionBrowseMaxLmbCount, 2);
193 TEST_OPTION_INT(OptionUrlEncodeReaddirEntries, true);
194 TEST_OPTION_INT(OptionOneSharePerServer, true);
195 TEST_OPTION_INT(OptionUseKerberos, false);
196 TEST_OPTION_INT(OptionFallbackAfterKerberos, false);
197 TEST_OPTION_INT(OptionNoAutoAnonymousLogin, true);
198 TEST_OPTION_INT(OptionUseCCache, true);
200 smbc_free_context(ctx, 1);
202 return true;
205 NTSTATUS torture_libsmbclient_init(TALLOC_CTX *ctx)
207 struct torture_suite *suite;
209 suite = torture_suite_create(ctx, "libsmbclient");
211 torture_suite_add_simple_test(suite, "version", torture_libsmbclient_version);
212 torture_suite_add_simple_test(suite, "initialize", torture_libsmbclient_initialize);
213 torture_suite_add_simple_test(suite, "configuration", torture_libsmbclient_configuration);
214 torture_suite_add_simple_test(suite, "options", torture_libsmbclient_options);
215 torture_suite_add_simple_test(suite, "opendir", torture_libsmbclient_opendir);
217 suite->description = talloc_strdup(suite, "libsmbclient interface tests");
219 torture_register_suite(ctx, suite);
221 return NT_STATUS_OK;