source: trunk/server/source4/torture/libsmbclient/libsmbclient.c

Last change on this file was 745, checked in by Silvan Scherrer, 13 years ago

Samba Server: updated trunk to 3.6.0

File size: 6.4 KB
Line 
1/*
2 Unix SMB/CIFS implementation.
3 SMB torture tester
4 Copyright (C) Guenther Deschner 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 "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"
26
27bool torture_libsmbclient_init_context(struct torture_context *tctx,
28 SMBCCTX **ctx_p)
29{
30 SMBCCTX *ctx;
31
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");
35
36 smbc_setDebug(ctx, DEBUGLEVEL);
37 smbc_setOptionDebugToStderr(ctx, 1);
38
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)));
42
43 *ctx_p = ctx;
44
45 return true;
46}
47
48static bool torture_libsmbclient_version(struct torture_context *tctx)
49{
50 torture_comment(tctx, "Testing smbc_version\n");
51
52 torture_assert(tctx, smbc_version(), "failed to get version");
53
54 return true;
55}
56
57static bool torture_libsmbclient_initialize(struct torture_context *tctx)
58{
59 SMBCCTX *ctx;
60
61 torture_comment(tctx, "Testing smbc_new_context\n");
62
63 ctx = smbc_new_context();
64 torture_assert(tctx, ctx, "failed to get new context");
65
66 torture_comment(tctx, "Testing smbc_init_context\n");
67
68 torture_assert(tctx, smbc_init_context(ctx), "failed to init context");
69
70 smbc_free_context(ctx, 1);
71
72 return true;
73}
74
75static bool test_opendir(struct torture_context *tctx,
76 SMBCCTX *ctx,
77 const char *fname,
78 bool expect_success)
79{
80 int handle, ret;
81
82 torture_comment(tctx, "Testing smbc_opendir(%s)\n", fname);
83
84 handle = smbc_opendir(fname);
85 if (!expect_success) {
86 return true;
87 }
88 if (handle < 0) {
89 torture_fail(tctx, talloc_asprintf(tctx, "failed to obain file handle for '%s'", fname));
90 }
91
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));
95
96 return true;
97}
98
99static bool torture_libsmbclient_opendir(struct torture_context *tctx)
100{
101 int i;
102 SMBCCTX *ctx;
103 bool ret = true;
104 const char *bad_urls[] = {
105 "",
106 NULL,
107 "smb",
108 "smb:",
109 "smb:/",
110 "smb:///",
111 "bms://",
112 ":",
113 ":/",
114 "://",
115 ":///",
116 "/",
117 "//",
118 "///"
119 };
120 const char *good_urls[] = {
121 "smb://",
122 "smb://WORKGROUP",
123 "smb://WORKGROUP/"
124 };
125
126 torture_assert(tctx, torture_libsmbclient_init_context(tctx, &ctx), "");
127 smbc_set_context(ctx);
128
129 for (i=0; i < ARRAY_SIZE(bad_urls); i++) {
130 ret &= test_opendir(tctx, ctx, bad_urls[i], false);
131 }
132 for (i=0; i < ARRAY_SIZE(good_urls); i++) {
133 ret &= test_opendir(tctx, ctx, good_urls[i], true);
134 }
135
136 smbc_free_context(ctx, 1);
137
138 return ret;
139}
140
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. */
145
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);
151
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);
157
158bool torture_libsmbclient_configuration(struct torture_context *tctx)
159{
160 SMBCCTX *ctx;
161
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");
165
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);
171
172 smbc_free_context(ctx, 1);
173
174 return true;
175}
176
177bool torture_libsmbclient_options(struct torture_context *tctx)
178{
179 SMBCCTX *ctx;
180
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");
184
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);
198
199 smbc_free_context(ctx, 1);
200
201 return true;
202}
203
204NTSTATUS torture_libsmbclient_init(void)
205{
206 struct torture_suite *suite;
207
208 suite = torture_suite_create(talloc_autofree_context(), "libsmbclient");
209
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);
215
216 suite->description = talloc_strdup(suite, "libsmbclient interface tests");
217
218 torture_register_suite(suite);
219
220 return NT_STATUS_OK;
221}
Note: See TracBrowser for help on using the repository browser.