r12608: Remove some unused #include lines.
[bbaumbach/samba-autobuild/.git] / source4 / torture / basic / disconnect.c
1 /* 
2    Unix SMB/CIFS implementation.
3
4    test server handling of unexpected client disconnects
5
6    Copyright (C) Andrew Tridgell 2004
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 2 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, write to the Free Software
20    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 */
22
23 #include "includes.h"
24 #include "system/filesys.h"
25 #include "libcli/raw/libcliraw.h"
26
27 #define BASEDIR "\\test_disconnect"
28
29 #define CHECK_STATUS(status, correct) do { \
30         if (!NT_STATUS_EQUAL(status, correct)) { \
31                 printf("(%s) Incorrect status %s - should be %s\n", \
32                        __location__, nt_errstr(status), nt_errstr(correct)); \
33                 talloc_free(cli); \
34                 return False; \
35         }} while (0)
36
37 /*
38   test disconnect after async open
39 */
40 static BOOL test_disconnect_open(struct smbcli_state *cli, TALLOC_CTX *mem_ctx)
41 {
42         union smb_open io;
43         NTSTATUS status;
44         struct smbcli_request *req1, *req2;
45
46         printf("trying open/disconnect\n");
47
48         io.generic.level = RAW_OPEN_NTCREATEX;
49         io.ntcreatex.in.root_fid = 0;
50         io.ntcreatex.in.flags = 0;
51         io.ntcreatex.in.access_mask = SEC_FILE_READ_DATA;
52         io.ntcreatex.in.create_options = 0;
53         io.ntcreatex.in.file_attr = FILE_ATTRIBUTE_NORMAL;
54         io.ntcreatex.in.share_access = NTCREATEX_SHARE_ACCESS_READ;
55         io.ntcreatex.in.alloc_size = 0;
56         io.ntcreatex.in.open_disposition = NTCREATEX_DISP_OPEN_IF;
57         io.ntcreatex.in.impersonation = NTCREATEX_IMPERSONATION_ANONYMOUS;
58         io.ntcreatex.in.security_flags = 0;
59         io.ntcreatex.in.fname = BASEDIR "\\open.dat";
60         status = smb_raw_open(cli->tree, mem_ctx, &io);
61         CHECK_STATUS(status, NT_STATUS_OK);
62
63         io.ntcreatex.in.share_access = 0;
64         req1 = smb_raw_open_send(cli->tree, &io);
65         req2 = smb_raw_open_send(cli->tree, &io);
66
67         status = smbcli_chkpath(cli->tree, "\\");
68         CHECK_STATUS(status, NT_STATUS_OK);
69         
70         talloc_free(cli);
71
72         return True;
73 }
74
75
76 /*
77   test disconnect with timed lock
78 */
79 static BOOL test_disconnect_lock(struct smbcli_state *cli, TALLOC_CTX *mem_ctx)
80 {
81         union smb_lock io;
82         NTSTATUS status;
83         int fnum;
84         struct smbcli_request *req;
85         struct smb_lock_entry lock[1];
86
87         printf("trying disconnect with async lock\n");
88
89         fnum = smbcli_open(cli->tree, BASEDIR "\\write.dat", 
90                            O_RDWR | O_CREAT, DENY_NONE);
91         if (fnum == -1) {
92                 printf("open failed in mux_write - %s\n", smbcli_errstr(cli->tree));
93                 return False;
94         }
95
96         io.lockx.level = RAW_LOCK_LOCKX;
97         io.lockx.in.fnum = fnum;
98         io.lockx.in.mode = 0;
99         io.lockx.in.timeout = 0;
100         io.lockx.in.lock_cnt = 1;
101         io.lockx.in.ulock_cnt = 0;
102         lock[0].pid = 1;
103         lock[0].offset = 0;
104         lock[0].count = 4;
105         io.lockx.in.locks = &lock[0];
106
107         status = smb_raw_lock(cli->tree, &io);
108         CHECK_STATUS(status, NT_STATUS_OK);
109
110         lock[0].pid = 2;
111         io.lockx.in.timeout = 3000;
112         req = smb_raw_lock_send(cli->tree, &io);
113
114         status = smbcli_chkpath(cli->tree, "\\");
115         CHECK_STATUS(status, NT_STATUS_OK);
116
117         talloc_free(cli);
118
119         return True;
120 }
121
122
123
124 /* 
125    basic testing of disconnects
126 */
127 BOOL torture_disconnect(void)
128 {
129         struct smbcli_state *cli;
130         BOOL ret = True;
131         TALLOC_CTX *mem_ctx;
132         int i;
133         extern int torture_numops;
134
135         mem_ctx = talloc_init("torture_raw_mux");
136
137         if (!torture_open_connection(&cli)) {
138                 return False;
139         }
140
141         if (!torture_setup_dir(cli, BASEDIR)) {
142                 return False;
143         }
144
145         for (i=0;i<torture_numops;i++) {
146                 ret &= test_disconnect_lock(cli, mem_ctx);
147                 if (!torture_open_connection(&cli)) {
148                         return False;
149                 }
150
151                 ret &= test_disconnect_open(cli, mem_ctx);
152                 if (!torture_open_connection(&cli)) {
153                         return False;
154                 }
155         }
156
157         smb_raw_exit(cli->session);
158         smbcli_deltree(cli->tree, BASEDIR);
159         torture_close_connection(cli);
160         talloc_free(mem_ctx);
161         return ret;
162 }