r12542: Move some more prototypes out to seperate headers
[kamenim/samba.git] / source4 / torture / raw / ioctl.c
1 /* 
2    Unix SMB/CIFS implementation.
3    ioctl individual test suite
4    Copyright (C) Andrew Tridgell 2003
5    Copyright (C) James J Myers 2003 <myersjj@samba.org>
6    
7    This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 2 of the License, or
10    (at your option) any later version.
11    
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16    
17    You should have received a copy of the GNU General Public License
18    along with this program; if not, write to the Free Software
19    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 */
21
22 #include "includes.h"
23 #include "ioctl.h"
24 #include "libcli/raw/libcliraw.h"
25
26 #define BASEDIR "\\rawioctl"
27
28 #define CHECK_STATUS(status, correct) do { \
29         if (!NT_STATUS_EQUAL(status, correct)) { \
30                 printf("(%d) Incorrect status %s - should be %s\n", \
31                        __LINE__, nt_errstr(status), nt_errstr(correct)); \
32                 ret = False; \
33                 goto done; \
34         }} while (0)
35
36
37 /* test some ioctls */
38 static BOOL test_ioctl(struct smbcli_state *cli, TALLOC_CTX *mem_ctx)
39 {
40         union smb_ioctl ctl;
41         int fnum;
42         NTSTATUS status;
43         BOOL ret = True;
44         const char *fname = BASEDIR "\\test.dat";
45
46         printf("TESTING IOCTL FUNCTIONS\n");
47
48         fnum = create_complex_file(cli, mem_ctx, fname);
49         if (fnum == -1) {
50                 printf("Failed to create test.dat - %s\n", smbcli_errstr(cli->tree));
51                 ret = False;
52                 goto done;
53         }
54
55         printf("Trying 0xFFFF\n");
56         ctl.ioctl.level = RAW_IOCTL_IOCTL;
57         ctl.ioctl.in.fnum = fnum;
58         ctl.ioctl.in.request = 0xFFFF;
59
60         status = smb_raw_ioctl(cli->tree, mem_ctx, &ctl);
61         CHECK_STATUS(status, NT_STATUS_DOS(ERRSRV, ERRerror));
62
63         printf("Trying QUERY_JOB_INFO\n");
64         ctl.ioctl.level = RAW_IOCTL_IOCTL;
65         ctl.ioctl.in.fnum = fnum;
66         ctl.ioctl.in.request = IOCTL_QUERY_JOB_INFO;
67
68         status = smb_raw_ioctl(cli->tree, mem_ctx, &ctl);
69         CHECK_STATUS(status, NT_STATUS_DOS(ERRSRV, ERRerror));
70
71         printf("Trying bad handle\n");
72         ctl.ioctl.in.fnum = fnum+1;
73         status = smb_raw_ioctl(cli->tree, mem_ctx, &ctl);
74         CHECK_STATUS(status, NT_STATUS_DOS(ERRSRV, ERRerror));
75
76 done:
77         smbcli_close(cli->tree, fnum);
78         return ret;
79 }
80
81 /* test some filesystem control functions */
82 static BOOL test_fsctl(struct smbcli_state *cli, TALLOC_CTX *mem_ctx)
83 {
84         int fnum;
85         NTSTATUS status;
86         BOOL ret = True;
87         const char *fname = BASEDIR "\\test.dat";
88         union smb_ioctl nt;
89
90         printf("\nTESTING FSCTL FUNCTIONS\n");
91
92         fnum = create_complex_file(cli, mem_ctx, fname);
93         if (fnum == -1) {
94                 printf("Failed to create test.dat - %s\n", smbcli_errstr(cli->tree));
95                 ret = False;
96                 goto done;
97         }
98
99         printf("trying sparse file\n");
100         nt.ioctl.level = RAW_IOCTL_NTIOCTL;
101         nt.ntioctl.in.function = FSCTL_SET_SPARSE;
102         nt.ntioctl.in.fnum = fnum;
103         nt.ntioctl.in.fsctl = True;
104         nt.ntioctl.in.filter = 0;
105
106         status = smb_raw_ioctl(cli->tree, mem_ctx, &nt);
107         CHECK_STATUS(status, NT_STATUS_OK);
108
109         printf("trying batch oplock\n");
110         nt.ioctl.level = RAW_IOCTL_NTIOCTL;
111         nt.ntioctl.in.function = (FSCTL_FILESYSTEM | (2<<2));
112         nt.ntioctl.in.fnum = fnum;
113         nt.ntioctl.in.fsctl = True;
114         nt.ntioctl.in.filter = 0;
115
116         status = smb_raw_ioctl(cli->tree, mem_ctx, &nt);
117         if (NT_STATUS_IS_OK(status)) {
118                 printf("Server supports batch oplock upgrades on open files\n");
119         } else {
120                 printf("Server does not support batch oplock upgrades on open files\n");
121         }
122
123         printf("Trying bad handle\n");
124         nt.ntioctl.in.fnum = fnum+1;
125         status = smb_raw_ioctl(cli->tree, mem_ctx, &nt);
126         CHECK_STATUS(status, NT_STATUS_INVALID_HANDLE);
127
128 #if 0
129         nt.ntioctl.in.fnum = fnum;
130         for (i=0;i<100;i++) {
131                 nt.ntioctl.in.function = FSCTL_FILESYSTEM + (i<<2);
132                 status = smb_raw_ioctl(cli->tree, mem_ctx, &nt);
133                 if (!NT_STATUS_EQUAL(status, NT_STATUS_NOT_SUPPORTED)) {
134                         printf("filesystem fsctl 0x%x - %s\n",
135                                i, nt_errstr(status));
136                 }
137         }
138 #endif
139
140 done:
141         smbcli_close(cli->tree, fnum);
142         return ret;
143 }
144
145 /* 
146    basic testing of some ioctl calls 
147 */
148 BOOL torture_raw_ioctl(void)
149 {
150         struct smbcli_state *cli;
151         BOOL ret = True;
152         TALLOC_CTX *mem_ctx;
153
154         if (!torture_open_connection(&cli)) {
155                 return False;
156         }
157
158         mem_ctx = talloc_init("torture_raw_ioctl");
159
160         if (!torture_setup_dir(cli, BASEDIR)) {
161                 return False;
162         }
163
164         if (!test_ioctl(cli, mem_ctx)) {
165                 ret = False;
166         }
167
168         if (!test_fsctl(cli, mem_ctx)) {
169                 ret = False;
170         }
171
172         smb_raw_exit(cli->session);
173         smbcli_deltree(cli->tree, BASEDIR);
174
175         torture_close_connection(cli);
176         talloc_free(mem_ctx);
177         return ret;
178 }