2 Unix SMB/CIFS implementation.
4 test suite for SMB2 session setups
6 Copyright (C) Michael Adam 2012
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>.
23 #include "libcli/smb2/smb2.h"
24 #include "libcli/smb2/smb2_calls.h"
25 #include "torture/torture.h"
26 #include "torture/smb2/proto.h"
27 #include "../libcli/smb/smbXcli_base.h"
28 #include "lib/cmdline/popt_common.h"
30 #define CHECK_VAL(v, correct) do { \
31 if ((v) != (correct)) { \
32 torture_result(tctx, TORTURE_FAIL, "(%s): wrong value for %s got 0x%x - should be 0x%x\n", \
33 __location__, #v, (int)v, (int)correct); \
37 #define CHECK_STATUS(status, correct) do { \
38 if (!NT_STATUS_EQUAL(status, correct)) { \
39 torture_result(tctx, TORTURE_FAIL, __location__": Incorrect status %s - should be %s", \
40 nt_errstr(status), nt_errstr(correct)); \
45 #define CHECK_CREATED(__io, __created, __attribute) \
47 CHECK_VAL((__io)->out.create_action, NTCREATEX_ACTION_ ## __created); \
48 CHECK_VAL((__io)->out.alloc_size, 0); \
49 CHECK_VAL((__io)->out.size, 0); \
50 CHECK_VAL((__io)->out.file_attr, (__attribute)); \
51 CHECK_VAL((__io)->out.reserved2, 0); \
56 * basic test for doing a session reconnect
58 bool test_session_reconnect(struct torture_context
*tctx
, struct smb2_tree
*tree
)
61 TALLOC_CTX
*mem_ctx
= talloc_new(tctx
);
63 struct smb2_handle _h1
;
64 struct smb2_handle
*h1
= NULL
;
65 struct smb2_handle _h2
;
66 struct smb2_handle
*h2
= NULL
;
67 struct smb2_create io1
, io2
;
68 uint64_t previous_session_id
;
70 struct smb2_tree
*tree2
;
71 union smb_fileinfo qfinfo
;
73 /* Add some random component to the file name. */
74 snprintf(fname
, 256, "session_reconnect_%s.dat",
75 generate_random_str(tctx
, 8));
77 smb2_util_unlink(tree
, fname
);
79 smb2_oplock_create_share(&io1
, fname
,
80 smb2_util_share_access(""),
81 smb2_util_oplock_level("b"));
83 status
= smb2_create(tree
, mem_ctx
, &io1
);
84 CHECK_STATUS(status
, NT_STATUS_OK
);
85 _h1
= io1
.out
.file
.handle
;
87 CHECK_CREATED(&io1
, CREATED
, FILE_ATTRIBUTE_ARCHIVE
);
88 CHECK_VAL(io1
.out
.oplock_level
, smb2_util_oplock_level("b"));
90 /* disconnect, reconnect and then do durable reopen */
91 previous_session_id
= smb2cli_session_current_id(tree
->session
->smbXcli
);
93 if (!torture_smb2_connection_ext(tctx
, previous_session_id
, &tree2
)) {
94 torture_warning(tctx
, "session reconnect failed\n");
99 /* try to access the file via the old handle */
102 qfinfo
.generic
.level
= RAW_FILEINFO_POSITION_INFORMATION
;
103 qfinfo
.generic
.in
.file
.handle
= _h1
;
104 status
= smb2_getinfo_file(tree
, mem_ctx
, &qfinfo
);
105 CHECK_STATUS(status
, NT_STATUS_USER_SESSION_DELETED
);
108 smb2_oplock_create_share(&io2
, fname
,
109 smb2_util_share_access(""),
110 smb2_util_oplock_level("b"));
112 status
= smb2_create(tree2
, mem_ctx
, &io2
);
113 CHECK_STATUS(status
, NT_STATUS_OK
);
114 CHECK_CREATED(&io2
, EXISTED
, FILE_ATTRIBUTE_ARCHIVE
);
115 CHECK_VAL(io2
.out
.oplock_level
, smb2_util_oplock_level("b"));
116 _h2
= io2
.out
.file
.handle
;
121 smb2_util_close(tree
, *h1
);
124 smb2_util_close(tree2
, *h2
);
127 smb2_util_unlink(tree2
, fname
);
132 talloc_free(mem_ctx
);
137 bool test_session_reauth(struct torture_context
*tctx
, struct smb2_tree
*tree
)
140 TALLOC_CTX
*mem_ctx
= talloc_new(tctx
);
142 struct smb2_handle _h1
;
143 struct smb2_handle
*h1
= NULL
;
144 struct smb2_create io1
;
146 union smb_fileinfo qfinfo
;
148 /* Add some random component to the file name. */
149 snprintf(fname
, 256, "session_reconnect_%s.dat",
150 generate_random_str(tctx
, 8));
152 smb2_util_unlink(tree
, fname
);
154 smb2_oplock_create_share(&io1
, fname
,
155 smb2_util_share_access(""),
156 smb2_util_oplock_level("b"));
158 status
= smb2_create(tree
, mem_ctx
, &io1
);
159 CHECK_STATUS(status
, NT_STATUS_OK
);
160 _h1
= io1
.out
.file
.handle
;
162 CHECK_CREATED(&io1
, CREATED
, FILE_ATTRIBUTE_ARCHIVE
);
163 CHECK_VAL(io1
.out
.oplock_level
, smb2_util_oplock_level("b"));
165 status
= smb2_session_setup_spnego(tree
->session
,
167 0 /* previous_session_id */);
168 CHECK_STATUS(status
, NT_STATUS_OK
);
170 /* try to access the file via the old handle */
173 qfinfo
.generic
.level
= RAW_FILEINFO_POSITION_INFORMATION
;
174 qfinfo
.generic
.in
.file
.handle
= _h1
;
175 status
= smb2_getinfo_file(tree
, mem_ctx
, &qfinfo
);
176 CHECK_STATUS(status
, NT_STATUS_OK
);
180 smb2_util_close(tree
, *h1
);
183 smb2_util_unlink(tree
, fname
);
187 talloc_free(mem_ctx
);
192 struct torture_suite
*torture_smb2_session_init(void)
194 struct torture_suite
*suite
=
195 torture_suite_create(talloc_autofree_context(), "session");
197 torture_suite_add_1smb2_test(suite
, "reconnect", test_session_reconnect
);
198 torture_suite_add_1smb2_test(suite
, "reauth", test_session_reauth
);
200 suite
->description
= talloc_strdup(suite
, "SMB2-SESSION tests");