Merge branch 'v4-0-test' of ssh://git.samba.org/data/git/samba into v4-0-wsgi
[nivanova/samba-autobuild/.git] / source4 / torture / smb2 / oplocks.c
1 /*
2    Unix SMB/CIFS implementation.
3
4    test suite for SMB2 oplocks
5
6    Copyright (C) Stefan Metzmacher 2008
7
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.
12
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.
17
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/>.
20 */
21
22 #include "includes.h"
23 #include "librpc/gen_ndr/security.h"
24 #include "libcli/smb2/smb2.h"
25 #include "libcli/smb2/smb2_calls.h"
26 #include "torture/torture.h"
27 #include "torture/smb2/proto.h"
28 #include "param/param.h"
29
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)
36
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)
44
45 static struct {
46         struct smb2_handle handle;
47         uint8_t level;
48         struct smb2_break br;
49         int count;
50         int failures;
51 } break_info;
52
53 static void torture_oplock_break_callback(struct smb2_request *req)
54 {
55         NTSTATUS status;
56         struct smb2_break br;
57
58         ZERO_STRUCT(br);
59         status = smb2_break_recv(req, &break_info.br);
60         if (!NT_STATUS_IS_OK(status)) {
61                 break_info.failures++;
62         }
63
64         return;
65 }
66
67 /* a oplock break request handler */
68 static bool torture_oplock_handler(struct smb2_transport *transport,
69                                    const struct smb2_handle *handle,
70                                    uint8_t level, void *private_data)
71 {
72         struct smb2_tree *tree = private_data;
73         const char *name;
74         struct smb2_request *req;
75
76         break_info.handle       = *handle;
77         break_info.level        = level;
78         break_info.count++;
79
80         switch (level) {
81         case SMB2_OPLOCK_LEVEL_II:
82                 name = "level II";
83                 break;
84         case SMB2_OPLOCK_LEVEL_NONE:
85                 name = "none";
86                 break;
87         default:
88                 name = "unknown";
89                 break_info.failures++;
90         }
91         printf("Acking to %s [0x%02X] in oplock handler\n",
92                 name, level);
93
94         ZERO_STRUCT(break_info.br);
95         break_info.br.in.file.handle    = *handle;
96         break_info.br.in.oplock_level   = level;
97         break_info.br.in.reserved       = 0;
98         break_info.br.in.reserved2      = 0;
99
100         req = smb2_break_send(tree, &break_info.br);
101         req->async.fn = torture_oplock_break_callback;
102         req->async.private_data = NULL;
103
104         return true;
105 }
106
107 bool torture_smb2_oplock_batch1(struct torture_context *tctx,
108                                 struct smb2_tree *tree)
109 {
110         TALLOC_CTX *mem_ctx = talloc_new(tctx);
111         struct smb2_handle h1, h2;
112         struct smb2_create io;
113         NTSTATUS status;
114         const char *fname = "oplock.dat";
115         bool ret = true;
116
117         tree->session->transport->oplock.handler        = torture_oplock_handler;
118         tree->session->transport->oplock.private_data   = tree;
119
120         smb2_util_unlink(tree, fname);
121
122         ZERO_STRUCT(break_info);
123
124         ZERO_STRUCT(io);
125         io.in.security_flags            = 0x00;
126         io.in.oplock_level              = SMB2_OPLOCK_LEVEL_BATCH;
127         io.in.impersonation_level       = NTCREATEX_IMPERSONATION_IMPERSONATION;
128         io.in.create_flags              = 0x00000000;
129         io.in.reserved                  = 0x00000000;
130         io.in.desired_access            = SEC_RIGHTS_FILE_ALL;
131         io.in.file_attributes           = FILE_ATTRIBUTE_NORMAL;
132         io.in.share_access              = NTCREATEX_SHARE_ACCESS_READ |
133                                           NTCREATEX_SHARE_ACCESS_WRITE |
134                                           NTCREATEX_SHARE_ACCESS_DELETE;
135         io.in.create_disposition        = NTCREATEX_DISP_OPEN_IF;
136         io.in.create_options            = NTCREATEX_OPTIONS_SEQUENTIAL_ONLY |
137                                           NTCREATEX_OPTIONS_ASYNC_ALERT |
138                                           NTCREATEX_OPTIONS_NON_DIRECTORY_FILE |
139                                           0x00200000;
140         io.in.fname                     = fname;
141
142         status = smb2_create(tree, mem_ctx, &io);
143         CHECK_STATUS(status, NT_STATUS_OK);
144         CHECK_VAL(io.out.oplock_level, SMB2_OPLOCK_LEVEL_BATCH);
145         /*CHECK_VAL(io.out.reserved, 0);*/
146         CHECK_VAL(io.out.create_action, NTCREATEX_ACTION_CREATED);
147         CHECK_VAL(io.out.alloc_size, 0);
148         CHECK_VAL(io.out.size, 0);
149         CHECK_VAL(io.out.file_attr, FILE_ATTRIBUTE_ARCHIVE);
150         CHECK_VAL(io.out.reserved2, 0);
151         CHECK_VAL(break_info.count, 0);
152
153         h1 = io.out.file.handle;
154
155         ZERO_STRUCT(io.in.blobs);
156         status = smb2_create(tree, mem_ctx, &io);
157         CHECK_VAL(break_info.count, 1);
158         CHECK_VAL(break_info.failures, 0);
159         CHECK_VAL(break_info.level, SMB2_OPLOCK_LEVEL_II);
160         CHECK_STATUS(status, NT_STATUS_OK);
161         CHECK_VAL(io.out.oplock_level, SMB2_OPLOCK_LEVEL_II);
162         /*CHECK_VAL(io.out.reserved, 0);*/
163         CHECK_VAL(io.out.create_action, NTCREATEX_ACTION_EXISTED);
164         CHECK_VAL(io.out.alloc_size, 0);
165         CHECK_VAL(io.out.size, 0);
166         CHECK_VAL(io.out.file_attr, FILE_ATTRIBUTE_ARCHIVE);
167         CHECK_VAL(io.out.reserved2, 0);
168
169         h2 = io.out.file.handle;
170
171 done:
172         talloc_free(mem_ctx);
173
174         smb2_util_close(tree, h1);
175         smb2_util_close(tree, h2);
176         smb2_util_unlink(tree, fname);
177         return ret;
178 }