Revert "named_pipe_echo raw mode..."
[metze/samba/wip.git] / source4 / torture / raw / named_pipe_echo.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 "torture/torture.h"
24 #include "libcli/raw/libcliraw.h"
25 #include "libcli/raw/raw_proto.h"
26 #include "libcli/libcli.h"
27 #include "torture/util.h"
28
29 #define CHECK_VAL(v, correct) do { \
30         if ((v) != (correct)) { \
31                 torture_result(tctx, TORTURE_FAIL, "(%s): wrong value for %s got 0x%x - should be 0x%x\n", \
32                                 __location__, #v, (int)v, (int)correct); \
33                 ret = false; \
34         }} while (0)
35
36 #define CHECK_STATUS(status, correct) do { \
37         if (!NT_STATUS_EQUAL(status, correct)) { \
38                 torture_result(tctx, TORTURE_FAIL, __location__": Incorrect status %s - should be %s", \
39                        nt_errstr(status), nt_errstr(correct)); \
40                 ret = false; \
41                 goto done; \
42         }} while (0)
43
44 #define NP_ECHO_MESSAGE_FNAME "\\NPECHO_MESSAGE"
45
46 bool torture_raw_np_echo01(struct torture_context *tctx,
47                            struct smbcli_state *cli)
48 {
49         bool ret = true;
50         NTSTATUS status;
51         union smb_open op;
52         struct smb_trans2 tr;
53         union smb_write wr;
54         union smb_read rd;
55         union smb_close cl;
56         int fnum;
57         uint8_t cmd_buf[256*8];
58         uint16_t setup[2];
59         struct smbcli_request *reqs[4];
60         uint8_t zero20[20];
61         ZERO_STRUCT(zero20);
62
63         torture_comment(tctx, "open message mode named pipe\n");
64         ZERO_STRUCT(op);
65         op.ntcreatex.level = RAW_OPEN_NTCREATEX;
66         op.ntcreatex.in.fname = NP_ECHO_MESSAGE_FNAME;
67         op.ntcreatex.in.flags = NTCREATEX_FLAGS_EXTENDED |
68                                 NTCREATEX_FLAGS_REQUEST_BATCH_OPLOCK |
69                                 NTCREATEX_FLAGS_REQUEST_OPLOCK;
70         op.ntcreatex.in.root_fid = 0;
71         op.ntcreatex.in.access_mask = SEC_RIGHTS_FILE_ALL;
72         op.ntcreatex.in.alloc_size = 0;
73         op.ntcreatex.in.file_attr = 0;
74         op.ntcreatex.in.share_access = 0;
75         op.ntcreatex.in.open_disposition = NTCREATEX_DISP_OPEN_IF;
76         op.ntcreatex.in.create_options = NTCREATEX_OPTIONS_NON_DIRECTORY_FILE;
77         op.ntcreatex.in.security_flags = NTCREATEX_SECURITY_ALL |
78                                          NTCREATEX_SECURITY_DYNAMIC;
79         op.ntcreatex.in.impersonation = NTCREATEX_IMPERSONATION_IMPERSONATION;
80
81         status = smb_raw_open(cli->tree, tctx, &op);
82         CHECK_STATUS(status, NT_STATUS_OK);
83         fnum = op.ntcreatex.out.file.fnum;
84         CHECK_VAL(op.ntcreatex.out.attrib, FILE_ATTRIBUTE_NORMAL);
85         CHECK_VAL(op.ntcreatex.out.create_action, 1);
86         CHECK_VAL(op.ntcreatex.out.is_directory, 0);
87         CHECK_VAL(op.ntcreatex.out.size, 0);
88         //CHECK_VAL(op.ntcreatex.out.alloc_size, 0);
89         CHECK_VAL(op.ntcreatex.out.maximal_access, 0);
90         CHECK_VAL(op.ntcreatex.out.file_type, FILE_TYPE_MESSAGE_MODE_PIPE);
91         CHECK_VAL(op.ntcreatex.out.ipc_state, 0x5ff);
92
93         torture_comment(tctx, "select cmds\n");
94 #define NPECHO_CMD_SLEEP 0
95 #define NPECHO_CMD_READ 1
96 #define NPECHO_CMD_WRITE 2
97
98 #define NPECHO_BUF_CMD_SIZE 8
99
100 #define NPECHO_BUF_SETUP_CMD(buf, idx, cmd, val) do { \
101         SIVAL(buf, (idx*NPECHO_BUF_CMD_SIZE)+0, cmd); \
102         SIVAL(buf, (idx*NPECHO_BUF_CMD_SIZE)+4, val); \
103         } while (0)
104
105         NPECHO_BUF_SETUP_CMD(cmd_buf, 0, NPECHO_CMD_SLEEP, 300);
106         NPECHO_BUF_SETUP_CMD(cmd_buf, 1, NPECHO_CMD_READ, 20);
107         NPECHO_BUF_SETUP_CMD(cmd_buf, 2, NPECHO_CMD_READ, 20);
108         NPECHO_BUF_SETUP_CMD(cmd_buf, 3, NPECHO_CMD_SLEEP, 300);
109         NPECHO_BUF_SETUP_CMD(cmd_buf, 4, NPECHO_CMD_WRITE, 20);
110         NPECHO_BUF_SETUP_CMD(cmd_buf, 5, NPECHO_CMD_WRITE, 20);
111         NPECHO_BUF_SETUP_CMD(cmd_buf, 6, NPECHO_CMD_SLEEP, 300);
112         NPECHO_BUF_SETUP_CMD(cmd_buf, 7, NPECHO_CMD_READ, 20);
113         NPECHO_BUF_SETUP_CMD(cmd_buf, 8, NPECHO_CMD_READ, 20);
114         NPECHO_BUF_SETUP_CMD(cmd_buf, 9, NPECHO_CMD_SLEEP, 300);
115         NPECHO_BUF_SETUP_CMD(cmd_buf,10, NPECHO_CMD_WRITE, 20);
116         NPECHO_BUF_SETUP_CMD(cmd_buf,11, NPECHO_CMD_WRITE, 20);
117         NPECHO_BUF_SETUP_CMD(cmd_buf,12, NPECHO_CMD_SLEEP, 300);
118         NPECHO_BUF_SETUP_CMD(cmd_buf,13, NPECHO_CMD_READ, 20);
119         NPECHO_BUF_SETUP_CMD(cmd_buf,14, NPECHO_CMD_READ, 20);
120         NPECHO_BUF_SETUP_CMD(cmd_buf,15, NPECHO_CMD_SLEEP, 300);
121         NPECHO_BUF_SETUP_CMD(cmd_buf,16, NPECHO_CMD_WRITE, 20);
122         NPECHO_BUF_SETUP_CMD(cmd_buf,17, NPECHO_CMD_WRITE, 20);
123         NPECHO_BUF_SETUP_CMD(cmd_buf,18, NPECHO_CMD_SLEEP, 300);
124         NPECHO_BUF_SETUP_CMD(cmd_buf,19, NPECHO_CMD_READ, 20);
125         NPECHO_BUF_SETUP_CMD(cmd_buf,20, NPECHO_CMD_SLEEP, 300);
126         NPECHO_BUF_SETUP_CMD(cmd_buf,21, NPECHO_CMD_WRITE, 20);
127         NPECHO_BUF_SETUP_CMD(cmd_buf,22, NPECHO_CMD_WRITE, 20);
128         NPECHO_BUF_SETUP_CMD(cmd_buf,23, NPECHO_CMD_SLEEP, 300);
129
130         setup[0] = TRANSACT_DCERPCCMD;
131         setup[1] = fnum;
132
133         ZERO_STRUCT(tr);
134         tr.in.flags = 0;
135         tr.in.timeout = 0;
136         tr.in.setup_count = 2;
137         tr.in.setup = setup;
138         tr.in.params = data_blob(NULL, 0);
139         tr.in.data = data_blob_const(cmd_buf, 24*NPECHO_BUF_CMD_SIZE);
140         tr.in.max_setup = 0;
141         tr.in.max_param = 0;
142         tr.in.max_data = 4;
143         status = smb_raw_trans(cli->tree, tctx, &tr);
144         CHECK_STATUS(status, NT_STATUS_OK);
145
146         /* now do operations and send them all at the same time */
147         ZERO_STRUCT(wr);
148         wr.writex.level = RAW_WRITE_WRITEX;
149         wr.writex.in.file.fnum = fnum;
150         wr.writex.in.wmode = 0;
151         wr.writex.in.remaining = 0;
152         wr.writex.in.offset = 0;
153         wr.writex.in.count = sizeof(zero20);
154         wr.writex.in.data = zero20;
155
156         ZERO_STRUCT(rd);
157         rd.readx.level = RAW_READ_READX;
158         rd.readx.in.file.fnum = fnum;
159         rd.readx.in.maxcnt = sizeof(zero20);
160         rd.readx.in.mincnt = sizeof(zero20);
161         rd.readx.in.offset = 0;
162         rd.readx.in.read_for_execute = false;
163         rd.readx.in.remaining = 0;
164         rd.readx.out.data = zero20;
165
166         tr.in.data = data_blob_const(zero20, sizeof(zero20));
167         tr.in.max_data = sizeof(zero20);
168
169         torture_comment(tctx, "do write, write, read, read\n");
170         reqs[0] = smb_raw_write_send(cli->tree, &wr);
171         reqs[1] = smb_raw_write_send(cli->tree, &wr);
172         reqs[2] = smb_raw_read_send(cli->tree, &rd);
173         reqs[3] = smb_raw_read_send(cli->tree, &rd);
174
175         status = smb_raw_write_recv(reqs[0], &wr);
176         CHECK_STATUS(status, NT_STATUS_OK);
177         status = smb_raw_write_recv(reqs[1], &wr);
178         CHECK_STATUS(status, NT_STATUS_OK);
179         status = smb_raw_read_recv(reqs[2], &rd);
180         CHECK_STATUS(status, NT_STATUS_OK);
181         status = smb_raw_read_recv(reqs[3], &rd);
182         CHECK_STATUS(status, NT_STATUS_OK);
183
184         torture_comment(tctx, "do write, trans, read\n");
185         reqs[0] = smb_raw_write_send(cli->tree, &wr);
186         reqs[1] = smb_raw_trans_send(cli->tree, &tr);
187         reqs[2] = smb_raw_read_send(cli->tree, &rd);
188
189         status = smb_raw_write_recv(reqs[0], &wr);
190         CHECK_STATUS(status, NT_STATUS_OK);
191         status = smb_raw_trans_recv(reqs[1], tctx, &tr);
192         CHECK_STATUS(status, NT_STATUS_OK);
193         status = smb_raw_read_recv(reqs[2], &rd);
194         CHECK_STATUS(status, NT_STATUS_OK);
195
196         torture_comment(tctx, "do write, trans, trans(pipe_busy), read\n");
197         reqs[0] = smb_raw_write_send(cli->tree, &wr);
198         reqs[1] = smb_raw_trans_send(cli->tree, &tr);
199         reqs[2] = smb_raw_trans_send(cli->tree, &tr);
200         reqs[3] = smb_raw_read_send(cli->tree, &rd);
201
202         status = smb_raw_write_recv(reqs[0], &wr);
203         CHECK_STATUS(status, NT_STATUS_OK);
204         status = smb_raw_trans_recv(reqs[1], tctx, &tr);
205         CHECK_STATUS(status, NT_STATUS_OK);
206         status = smb_raw_trans_recv(reqs[2], tctx, &tr);
207         CHECK_STATUS(status, NT_STATUS_PIPE_BUSY);
208         status = smb_raw_read_recv(reqs[3], &rd);
209         CHECK_STATUS(status, NT_STATUS_OK);
210
211         torture_comment(tctx, "do write, read, trans(pipe_busy), read\n");
212         reqs[0] = smb_raw_write_send(cli->tree, &wr);
213         reqs[1] = smb_raw_read_send(cli->tree, &rd);
214         reqs[2] = smb_raw_trans_send(cli->tree, &tr);
215         reqs[3] = smb_raw_read_send(cli->tree, &rd);
216
217         status = smb_raw_write_recv(reqs[0], &wr);
218         CHECK_STATUS(status, NT_STATUS_OK);
219         status = smb_raw_read_recv(reqs[1], &rd);
220         CHECK_STATUS(status, NT_STATUS_OK);
221         status = smb_raw_trans_recv(reqs[2], tctx, &tr);
222         CHECK_STATUS(status, NT_STATUS_PIPE_BUSY);
223         status = smb_raw_read_recv(reqs[3], &rd);
224         CHECK_STATUS(status, NT_STATUS_OK);
225
226         torture_comment(tctx, "close message mode named pipe\n");
227         ZERO_STRUCT(cl);
228         cl.close.level = RAW_CLOSE_CLOSE;
229         cl.close.in.file.fnum = fnum;
230         cl.close.in.write_time = 0;
231         status = smb_raw_close(cli->tree, &cl);
232         CHECK_STATUS(status, NT_STATUS_OK);
233
234 done:
235         return ret;
236 }
237