2 Unix SMB/CIFS implementation.
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/>.
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,
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(cmdline_credentials)));
48 static bool torture_libsmbclient_version(struct torture_context *tctx)
50 torture_comment(tctx, "Testing smbc_version\n");
52 torture_assert(tctx, smbc_version(), "failed to get version");
57 static bool torture_libsmbclient_initialize(struct torture_context *tctx)
61 torture_comment(tctx, "Testing smbc_new_context\n");
63 ctx = smbc_new_context();
64 torture_assert(tctx, ctx, "failed to get new context");
66 torture_comment(tctx, "Testing smbc_init_context\n");
68 torture_assert(tctx, smbc_init_context(ctx), "failed to init context");
70 smbc_free_context(ctx, 1);
75 static bool test_opendir(struct torture_context *tctx,
82 torture_comment(tctx, "Testing smbc_opendir(%s)\n", fname);
84 handle = smbc_opendir(fname);
85 if (!expect_success) {
89 torture_fail(tctx, talloc_asprintf(tctx, "failed to obain file handle for '%s'", fname));
92 ret = smbc_closedir(handle);
93 torture_assert_int_equal(tctx, ret, 0,
94 talloc_asprintf(tctx, "failed to close file handle for '%s'", fname));
99 static bool torture_libsmbclient_opendir(struct torture_context *tctx)
104 const char *bad_urls[] = {
120 const char *good_urls[] = {
126 torture_assert(tctx, torture_libsmbclient_init_context(tctx, &ctx), "");
127 smbc_set_context(ctx);
129 for (i=0; i < ARRAY_SIZE(bad_urls); i++) {
130 ret &= test_opendir(tctx, ctx, bad_urls[i], false);
132 for (i=0; i < ARRAY_SIZE(good_urls); i++) {
133 ret &= test_opendir(tctx, ctx, good_urls[i], true);
136 smbc_free_context(ctx, 1);
141 /* note the strdup for string options on smbc_set calls. I think libsmbclient is
142 * really doing something wrong here: in smbc_free_context libsmbclient just
143 * calls free() on the string options so it assumes the callers have malloced
144 * them before setting them via smbc_set calls. */
146 #define TEST_OPTION_INT(option, val) \
147 torture_comment(tctx, "Testing smbc_set" #option "\n");\
148 smbc_set ##option(ctx, val);\
149 torture_comment(tctx, "Testing smbc_get" #option "\n");\
150 torture_assert_int_equal(tctx, smbc_get ##option(ctx), val, "failed " #option);
152 #define TEST_OPTION_STRING(option, val) \
153 torture_comment(tctx, "Testing smbc_set" #option "\n");\
154 smbc_set ##option(ctx, strdup(val));\
155 torture_comment(tctx, "Testing smbc_get" #option "\n");\
156 torture_assert_str_equal(tctx, smbc_get ##option(ctx), val, "failed " #option);
158 bool torture_libsmbclient_configuration(struct torture_context *tctx)
162 ctx = smbc_new_context();
163 torture_assert(tctx, ctx, "failed to get new context");
164 torture_assert(tctx, smbc_init_context(ctx), "failed to init context");
166 TEST_OPTION_INT(Debug, DEBUGLEVEL);
167 TEST_OPTION_STRING(NetbiosName, "torture_netbios");
168 TEST_OPTION_STRING(Workgroup, "torture_workgroup");
169 TEST_OPTION_STRING(User, "torture_user");
170 TEST_OPTION_INT(Timeout, 12345);
172 smbc_free_context(ctx, 1);
177 bool torture_libsmbclient_options(struct torture_context *tctx)
181 ctx = smbc_new_context();
182 torture_assert(tctx, ctx, "failed to get new context");
183 torture_assert(tctx, smbc_init_context(ctx), "failed to init context");
185 TEST_OPTION_INT(OptionDebugToStderr, true);
186 TEST_OPTION_INT(OptionFullTimeNames, true);
187 TEST_OPTION_INT(OptionOpenShareMode, SMBC_SHAREMODE_DENY_ALL);
188 /* FIXME: OptionUserData */
189 TEST_OPTION_INT(OptionSmbEncryptionLevel, SMBC_ENCRYPTLEVEL_REQUEST);
190 TEST_OPTION_INT(OptionCaseSensitive, false);
191 TEST_OPTION_INT(OptionBrowseMaxLmbCount, 2);
192 TEST_OPTION_INT(OptionUrlEncodeReaddirEntries, true);
193 TEST_OPTION_INT(OptionOneSharePerServer, true);
194 TEST_OPTION_INT(OptionUseKerberos, false);
195 TEST_OPTION_INT(OptionFallbackAfterKerberos, false);
196 TEST_OPTION_INT(OptionNoAutoAnonymousLogin, true);
197 TEST_OPTION_INT(OptionUseCCache, true);
199 smbc_free_context(ctx, 1);
204 NTSTATUS torture_libsmbclient_init(void)
206 struct torture_suite *suite;
208 suite = torture_suite_create(talloc_autofree_context(), "libsmbclient");
210 torture_suite_add_simple_test(suite, "version", torture_libsmbclient_version);
211 torture_suite_add_simple_test(suite, "initialize", torture_libsmbclient_initialize);
212 torture_suite_add_simple_test(suite, "configuration", torture_libsmbclient_configuration);
213 torture_suite_add_simple_test(suite, "options", torture_libsmbclient_options);
214 torture_suite_add_simple_test(suite, "opendir", torture_libsmbclient_opendir);
216 suite->description = talloc_strdup(suite, "libsmbclient interface tests");
218 torture_register_suite(suite);