s4-dns: Add support for BIND 9.10
[Samba.git] / source3 / torture / test_oplock_cancel.c
blobaa4218a1126e42bff17dc39400deff394383f447
1 /*
2 Unix SMB/CIFS implementation.
3 Test cleanup behaviour
4 Copyright (C) Volker Lendecke 2011
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.
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.
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/>.
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"
30 struct create_cancel_state {
31 uint8_t dummy;
34 static void create_cancel_done(struct tevent_req *subreq);
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)
40 struct tevent_req *req, *subreq;
41 struct create_cancel_state *state;
43 req = tevent_req_create(mem_ctx, &state, struct create_cancel_state);
44 if (req == NULL) {
45 return NULL;
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, 0);
52 if (tevent_req_nomem(subreq, req)) {
53 return tevent_req_post(req, ev);
55 if (!tevent_req_cancel(subreq)) {
56 tevent_req_oom(req);
57 return tevent_req_post(req, ev);
59 tevent_req_set_callback(subreq, create_cancel_done, req);
60 return req;
63 static void create_cancel_done(struct tevent_req *subreq)
65 struct tevent_req *req = tevent_req_callback_data(
66 subreq, struct tevent_req);
67 NTSTATUS status;
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;
75 tevent_req_nterror(req, status);
76 return;
78 tevent_req_done(req);
81 static NTSTATUS create_cancel_recv(struct tevent_req *req)
83 return tevent_req_simple_recv_ntstatus(req);
86 static NTSTATUS create_cancel(struct cli_state *cli, const char *fname)
88 TALLOC_CTX *frame = talloc_stackframe();
89 struct tevent_context *ev;
90 struct tevent_req *req;
91 NTSTATUS status = NT_STATUS_NO_MEMORY;
93 ev = samba_tevent_context_init(frame);
94 if (ev == NULL) {
95 goto fail;
97 req = create_cancel_send(frame, ev, cli, fname);
98 if (req == NULL) {
99 goto fail;
101 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
102 goto fail;
104 status = create_cancel_recv(req);
105 fail:
106 TALLOC_FREE(frame);
107 return status;
110 bool run_oplock_cancel(int dummy)
112 struct cli_state *cli1, *cli2;
113 const char *fname = "oplock-cancel";
114 uint16_t fnum1;
115 NTSTATUS status;
117 lp_set_cmdline("client max protocol", "smb3");
119 if (!torture_open_connection(&cli1, 0)) {
120 return false;
122 cli1->use_oplocks = true;
124 if (!torture_open_connection(&cli2, 0)) {
125 return false;
127 cli2->use_oplocks = true;
129 status = cli_ntcreate(
130 cli1, fname, 0, FILE_GENERIC_READ, FILE_ATTRIBUTE_NORMAL,
131 FILE_SHARE_READ|FILE_SHARE_WRITE, FILE_OPEN_IF, 0, 0,
132 &fnum1, NULL);
133 if (!NT_STATUS_IS_OK(status)) {
134 d_printf("cli_ntcreate failed: %s\n", nt_errstr(status));
135 return false;
138 status = create_cancel(cli2, fname);
139 if (!NT_STATUS_IS_OK(status)) {
140 d_printf("create_cancel failed: %s\n", nt_errstr(status));
141 return false;
144 TALLOC_FREE(cli1);
147 * Give cli1's smbd time to inform cli2's smbd
149 smb_msleep(5000);
151 status = cli_unlink(cli2, fname,
152 FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
153 if (!NT_STATUS_IS_OK(status)) {
154 d_printf("cli_unlink failed: %s\n", nt_errstr(status));
155 return false;
158 return true;