b003876f59281ebf7f909a4bd7123368462a2c78
[metze/samba/wip.git] / source3 / torture / test_oplock_cancel.c
1 /*
2    Unix SMB/CIFS implementation.
3    Test cleanup behaviour
4    Copyright (C) Volker Lendecke 2011
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 "locking/proto.h"
22 #include "torture/proto.h"
23 #include "system/filesys.h"
24 #include "system/select.h"
25 #include "libsmb/libsmb.h"
26 #include "libcli/smb/smbXcli_base.h"
27 #include "libcli/security/security.h"
28 #include "lib/util/tevent_ntstatus.h"
29
30 struct create_cancel_state {
31         uint8_t dummy;
32 };
33
34 static void create_cancel_done(struct tevent_req *subreq);
35
36 static struct tevent_req *create_cancel_send(
37         TALLOC_CTX *mem_ctx, struct tevent_context *ev,
38         struct cli_state *cli, const char *fname)
39 {
40         struct tevent_req *req, *subreq;
41         struct create_cancel_state *state;
42
43         req = tevent_req_create(mem_ctx, &state, struct create_cancel_state);
44         if (req == NULL) {
45                 return NULL;
46         }
47
48         subreq = cli_ntcreate_send(
49                 mem_ctx, ev, cli, fname, 0, FILE_GENERIC_READ,
50                 FILE_ATTRIBUTE_NORMAL, FILE_SHARE_READ|FILE_SHARE_WRITE,
51                 FILE_OPEN_IF, 0, SMB2_IMPERSONATION_IMPERSONATION, 0);
52         if (tevent_req_nomem(subreq, req)) {
53                 return tevent_req_post(req, ev);
54         }
55         if (!tevent_req_cancel(subreq)) {
56                 tevent_req_oom(req);
57                 return tevent_req_post(req, ev);
58         }
59         tevent_req_set_callback(subreq, create_cancel_done, req);
60         return req;
61 }
62
63 static void create_cancel_done(struct tevent_req *subreq)
64 {
65         struct tevent_req *req = tevent_req_callback_data(
66                 subreq, struct tevent_req);
67         NTSTATUS status;
68
69         status = cli_ntcreate_recv(subreq, NULL, NULL);
70         TALLOC_FREE(subreq);
71         if (!NT_STATUS_EQUAL(status, NT_STATUS_CANCELLED)) {
72                 if (NT_STATUS_IS_OK(status)) {
73                         status = NT_STATUS_UNSUCCESSFUL;
74                 }
75                 tevent_req_nterror(req, status);
76                 return;
77         }
78         tevent_req_done(req);
79 }
80
81 static NTSTATUS create_cancel_recv(struct tevent_req *req)
82 {
83         return tevent_req_simple_recv_ntstatus(req);
84 }
85
86 static NTSTATUS create_cancel(struct cli_state *cli, const char *fname)
87 {
88         TALLOC_CTX *frame = talloc_stackframe();
89         struct tevent_context *ev;
90         struct tevent_req *req;
91         NTSTATUS status = NT_STATUS_NO_MEMORY;
92
93         ev = samba_tevent_context_init(frame);
94         if (ev == NULL) {
95                 goto fail;
96         }
97         req = create_cancel_send(frame, ev, cli, fname);
98         if (req == NULL) {
99                 goto fail;
100         }
101         if (!tevent_req_poll_ntstatus(req, ev, &status)) {
102                 goto fail;
103         }
104         status = create_cancel_recv(req);
105  fail:
106         TALLOC_FREE(frame);
107         return status;
108 }
109
110 bool run_oplock_cancel(int dummy)
111 {
112         struct cli_state *cli1, *cli2;
113         const char *fname = "oplock-cancel";
114         uint16_t fnum1;
115         NTSTATUS status;
116         /*
117          * Currently this test seems to work only
118          * with SMB2/3 and only against Samba.
119          *
120          * TODO: we should change our server
121          * to ignore cancel for SMB2 Create
122          * and behave like Windows.
123          */
124         int flags = CLI_FULL_CONNECTION_DISABLE_SMB1;
125
126         if (!torture_open_connection_flags(&cli1, 0, flags)) {
127                 return false;
128         }
129         cli1->use_oplocks = true;
130
131         if (!torture_open_connection_flags(&cli2, 0, flags)) {
132                 return false;
133         }
134         cli2->use_oplocks = true;
135
136         status = cli_ntcreate(
137                 cli1, fname, 0, FILE_GENERIC_READ, FILE_ATTRIBUTE_NORMAL,
138                 FILE_SHARE_READ|FILE_SHARE_WRITE, FILE_OPEN_IF, 0, 0,
139                 &fnum1, NULL);
140         if (!NT_STATUS_IS_OK(status)) {
141                 d_printf("cli_ntcreate failed: %s\n", nt_errstr(status));
142                 return false;
143         }
144
145         status = create_cancel(cli2, fname);
146         if (!NT_STATUS_IS_OK(status)) {
147                 d_printf("create_cancel failed: %s\n", nt_errstr(status));
148                 return false;
149         }
150
151         TALLOC_FREE(cli1);
152
153         /*
154          * Give cli1's smbd time to inform cli2's smbd
155          */
156         smb_msleep(5000);
157
158         status = cli_unlink(cli2, fname,
159                             FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
160         if (!NT_STATUS_IS_OK(status)) {
161                 d_printf("cli_unlink failed: %s\n", nt_errstr(status));
162                 return false;
163         }
164
165         return true;
166 }