s4:torture/smb2: add simple smb2.session.reauth test
[Samba.git] / source4 / torture / smb2 / session.c
blob3a0ef080aa3618a795db4bbc11323e0224b26216
1 /*
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/>.
22 #include "includes.h"
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); \
34 ret = false; \
35 }} while (0)
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)); \
41 ret = false; \
42 goto done; \
43 }} while (0)
45 #define CHECK_CREATED(__io, __created, __attribute) \
46 do { \
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); \
52 } while(0)
55 /**
56 * basic test for doing a session reconnect
58 bool test_session_reconnect(struct torture_context *tctx, struct smb2_tree *tree)
60 NTSTATUS status;
61 TALLOC_CTX *mem_ctx = talloc_new(tctx);
62 char fname[256];
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;
69 bool ret = true;
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;
86 h1 = &_h1;
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");
95 ret = false;
96 goto done;
99 smb2_oplock_create_share(&io2, fname,
100 smb2_util_share_access(""),
101 smb2_util_oplock_level("b"));
103 status = smb2_create(tree2, mem_ctx, &io2);
104 CHECK_STATUS(status, NT_STATUS_OK);
105 CHECK_CREATED(&io2, EXISTED, FILE_ATTRIBUTE_ARCHIVE);
106 CHECK_VAL(io2.out.oplock_level, smb2_util_oplock_level("b"));
107 _h2 = io2.out.file.handle;
108 h2 = &_h2;
110 /* try to access the file via the old handle */
112 ZERO_STRUCT(qfinfo);
113 qfinfo.generic.level = RAW_FILEINFO_POSITION_INFORMATION;
114 qfinfo.generic.in.file.handle = _h1;
115 status = smb2_getinfo_file(tree, mem_ctx, &qfinfo);
116 CHECK_STATUS(status, NT_STATUS_USER_SESSION_DELETED);
118 done:
119 if (h2 != NULL) {
120 smb2_util_close(tree2, *h2);
123 smb2_util_unlink(tree2, fname);
125 talloc_free(tree);
126 talloc_free(tree2);
128 talloc_free(mem_ctx);
130 return ret;
133 bool test_session_reauth(struct torture_context *tctx, struct smb2_tree *tree)
135 NTSTATUS status;
136 TALLOC_CTX *mem_ctx = talloc_new(tctx);
137 char fname[256];
138 struct smb2_handle _h1;
139 struct smb2_handle *h1 = NULL;
140 struct smb2_create io1;
141 bool ret = true;
142 union smb_fileinfo qfinfo;
144 /* Add some random component to the file name. */
145 snprintf(fname, 256, "session_reconnect_%s.dat",
146 generate_random_str(tctx, 8));
148 smb2_util_unlink(tree, fname);
150 smb2_oplock_create_share(&io1, fname,
151 smb2_util_share_access(""),
152 smb2_util_oplock_level("b"));
154 status = smb2_create(tree, mem_ctx, &io1);
155 CHECK_STATUS(status, NT_STATUS_OK);
156 _h1 = io1.out.file.handle;
157 h1 = &_h1;
158 CHECK_CREATED(&io1, CREATED, FILE_ATTRIBUTE_ARCHIVE);
159 CHECK_VAL(io1.out.oplock_level, smb2_util_oplock_level("b"));
161 status = smb2_session_setup_spnego(tree->session,
162 cmdline_credentials,
163 0 /* previous_session_id */);
164 CHECK_STATUS(status, NT_STATUS_OK);
166 /* try to access the file via the old handle */
168 ZERO_STRUCT(qfinfo);
169 qfinfo.generic.level = RAW_FILEINFO_POSITION_INFORMATION;
170 qfinfo.generic.in.file.handle = _h1;
171 status = smb2_getinfo_file(tree, mem_ctx, &qfinfo);
172 CHECK_STATUS(status, NT_STATUS_OK);
174 done:
175 if (h1 != NULL) {
176 smb2_util_close(tree, *h1);
179 smb2_util_unlink(tree, fname);
181 talloc_free(tree);
183 talloc_free(mem_ctx);
185 return ret;
188 struct torture_suite *torture_smb2_session_init(void)
190 struct torture_suite *suite =
191 torture_suite_create(talloc_autofree_context(), "session");
193 torture_suite_add_1smb2_test(suite, "reconnect", test_session_reconnect);
194 torture_suite_add_1smb2_test(suite, "reauth", test_session_reauth);
196 suite->description = talloc_strdup(suite, "SMB2-SESSION tests");
198 return suite;