* patch based on work by Jim Myers to unify the ioctl handling to be
[rusty/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
24 #define BASEDIR "\\rawioctl"
25
26 #define CHECK_STATUS(status, correct) do { \
27         if (!NT_STATUS_EQUAL(status, correct)) { \
28                 printf("(%d) Incorrect status %s - should be %s\n", \
29                        __LINE__, nt_errstr(status), nt_errstr(correct)); \
30                 ret = False; \
31                 goto done; \
32         }} while (0)
33
34
35 /* test some ioctls */
36 static BOOL test_ioctl(struct cli_state *cli, TALLOC_CTX *mem_ctx)
37 {
38         union smb_ioctl ctl;
39         int fnum;
40         NTSTATUS status;
41         BOOL ret = True;
42         const char *fname = BASEDIR "\\test.dat";
43
44         printf("TESTING IOCTL FUNCTIONS\n");
45
46         fnum = create_complex_file(cli, mem_ctx, fname);
47         if (fnum == -1) {
48                 printf("Failed to create test.dat - %s\n", cli_errstr(cli));
49                 ret = False;
50                 goto done;
51         }
52
53         printf("Trying QUERY_JOB_INFO\n");
54         ctl.ioctl.level = RAW_IOCTL_IOCTL;
55         ctl.ioctl.in.fnum = fnum;
56         ctl.ioctl.in.request = IOCTL_QUERY_JOB_INFO;
57
58         status = smb_raw_ioctl(cli->tree, mem_ctx, &ctl);
59         CHECK_STATUS(status, NT_STATUS_UNSUCCESSFUL);
60
61         printf("Trying bad handle\n");
62         ctl.ioctl.in.fnum = fnum+1;
63         status = smb_raw_ioctl(cli->tree, mem_ctx, &ctl);
64         CHECK_STATUS(status, NT_STATUS_UNSUCCESSFUL);
65
66 done:
67         cli_close(cli, fnum);
68         return ret;
69 }
70
71 /* test some filesystem control functions */
72 static BOOL test_fsctl(struct cli_state *cli, TALLOC_CTX *mem_ctx)
73 {
74         int fnum;
75         NTSTATUS status;
76         BOOL ret = True;
77         const char *fname = BASEDIR "\\test.dat";
78         union smb_ioctl nt;
79
80         printf("\nTESTING FSCTL FUNCTIONS\n");
81
82         fnum = create_complex_file(cli, mem_ctx, fname);
83         if (fnum == -1) {
84                 printf("Failed to create test.dat - %s\n", cli_errstr(cli));
85                 ret = False;
86                 goto done;
87         }
88
89         printf("trying sparse file\n");
90         nt.ioctl.level = RAW_IOCTL_NTIOCTL;
91         nt.ntioctl.in.function = FSCTL_SET_SPARSE;
92         nt.ntioctl.in.fnum = fnum;
93         nt.ntioctl.in.fsctl = True;
94         nt.ntioctl.in.filter = 0;
95
96         status = smb_raw_ioctl(cli->tree, mem_ctx, &nt);
97         CHECK_STATUS(status, NT_STATUS_OK);
98
99         printf("Trying bad handle\n");
100         nt.ntioctl.in.fnum = fnum+1;
101         status = smb_raw_ioctl(cli->tree, mem_ctx, &nt);
102         CHECK_STATUS(status, NT_STATUS_INVALID_HANDLE);
103
104 #if 0
105         nt.ntioctl.in.fnum = fnum;
106         for (i=0;i<100;i++) {
107                 nt.ntioctl.in.function = FSCTL_FILESYSTEM + (i<<2);
108                 status = smb_raw_ioctl(cli->tree, mem_ctx, &nt);
109                 if (!NT_STATUS_EQUAL(status, NT_STATUS_NOT_SUPPORTED)) {
110                         printf("filesystem fsctl 0x%x - %s\n",
111                                i, nt_errstr(status));
112                 }
113         }
114 #endif
115
116 done:
117         cli_close(cli, fnum);
118         return ret;
119 }
120
121 /* 
122    basic testing of some ioctl calls 
123 */
124 BOOL torture_raw_ioctl(int dummy)
125 {
126         struct cli_state *cli;
127         BOOL ret = True;
128         TALLOC_CTX *mem_ctx;
129
130         if (!torture_open_connection(&cli)) {
131                 return False;
132         }
133
134         mem_ctx = talloc_init("torture_raw_ioctl");
135
136         if (cli_deltree(cli, BASEDIR) == -1) {
137                 printf("Failed to clean " BASEDIR "\n");
138                 return False;
139         }
140         if (!cli_mkdir(cli, BASEDIR)) {
141                 printf("Failed to create " BASEDIR " - %s\n", cli_errstr(cli));
142                 return False;
143         }
144
145         if (!test_ioctl(cli, mem_ctx)) {
146                 ret = False;
147         }
148
149         if (!test_fsctl(cli, mem_ctx)) {
150                 ret = False;
151         }
152
153         smb_raw_exit(cli->session);
154         cli_deltree(cli, BASEDIR);
155
156         torture_close_connection(cli);
157         talloc_destroy(mem_ctx);
158         return ret;
159 }