first public release of samba4 code
[gd/samba-autobuild/.git] / source4 / torture / raw / ioctl.c
1 /* 
2    Unix SMB/CIFS implementation.
3    ioctl individual test suite
4    Copyright (C) Andrew Tridgell 2003
5    
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 2 of the License, or
9    (at your option) any later version.
10    
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.
15    
16    You should have received a copy of the GNU General Public License
17    along with this program; if not, write to the Free Software
18    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 */
20
21 #include "includes.h"
22
23 #define BASEDIR "\\rawioctl"
24
25 #define CHECK_STATUS(status, correct) do { \
26         if (!NT_STATUS_EQUAL(status, correct)) { \
27                 printf("(%d) Incorrect status %s - should be %s\n", \
28                        __LINE__, nt_errstr(status), nt_errstr(correct)); \
29                 ret = False; \
30                 goto done; \
31         }} while (0)
32
33
34 /* test some ioctls */
35 static BOOL test_ioctl(struct cli_state *cli, TALLOC_CTX *mem_ctx)
36 {
37         struct smb_ioctl ctl;
38         int fnum;
39         NTSTATUS status;
40         BOOL ret = True;
41         const char *fname = BASEDIR "\\test.dat";
42
43         printf("TESTING IOCTL FUNCTIONS\n");
44
45         fnum = create_complex_file(cli, mem_ctx, fname);
46         if (fnum == -1) {
47                 printf("Failed to create test.dat - %s\n", cli_errstr(cli));
48                 ret = False;
49                 goto done;
50         }
51
52         printf("Trying QUERY_JOB_INFO\n");
53         ctl.in.fnum = fnum;
54         ctl.in.request = IOCTL_QUERY_JOB_INFO;
55
56         status = smb_raw_ioctl(cli->tree, mem_ctx, &ctl);
57         CHECK_STATUS(status, NT_STATUS_UNSUCCESSFUL);
58
59         printf("Trying bad handle\n");
60         ctl.in.fnum = fnum+1;
61         status = smb_raw_ioctl(cli->tree, mem_ctx, &ctl);
62         CHECK_STATUS(status, NT_STATUS_UNSUCCESSFUL);
63
64 done:
65         cli_close(cli, fnum);
66         return ret;
67 }
68
69 /* test some filesystem control functions */
70 static BOOL test_fsctl(struct cli_state *cli, TALLOC_CTX *mem_ctx)
71 {
72         int fnum;
73         NTSTATUS status;
74         BOOL ret = True;
75         const char *fname = BASEDIR "\\test.dat";
76         struct smb_ntioctl nt;
77
78         printf("\nTESTING FSCTL FUNCTIONS\n");
79
80         fnum = create_complex_file(cli, mem_ctx, fname);
81         if (fnum == -1) {
82                 printf("Failed to create test.dat - %s\n", cli_errstr(cli));
83                 ret = False;
84                 goto done;
85         }
86
87         printf("trying sparse file\n");
88         nt.in.function = FSCTL_SET_SPARSE;
89         nt.in.fnum = fnum;
90         nt.in.fsctl = True;
91         nt.in.filter = 0;
92
93         status = smb_raw_ntioctl(cli->tree, &nt);
94         CHECK_STATUS(status, NT_STATUS_OK);
95
96         printf("Trying bad handle\n");
97         nt.in.fnum = fnum+1;
98         status = smb_raw_ntioctl(cli->tree, &nt);
99         CHECK_STATUS(status, NT_STATUS_INVALID_HANDLE);
100
101 #if 0
102         nt.in.fnum = fnum;
103         for (i=0;i<100;i++) {
104                 nt.in.function = FSCTL_FILESYSTEM + (i<<2);
105                 status = smb_raw_ntioctl(cli->tree, &nt);
106                 if (!NT_STATUS_EQUAL(status, NT_STATUS_NOT_SUPPORTED)) {
107                         printf("filesystem fsctl 0x%x - %s\n",
108                                i, nt_errstr(status));
109                 }
110         }
111 #endif
112
113 done:
114         cli_close(cli, fnum);
115         return ret;
116 }
117
118 /* 
119    basic testing of some ioctl calls 
120 */
121 BOOL torture_raw_ioctl(int dummy)
122 {
123         struct cli_state *cli;
124         BOOL ret = True;
125         TALLOC_CTX *mem_ctx;
126
127         if (!torture_open_connection(&cli)) {
128                 return False;
129         }
130
131         mem_ctx = talloc_init("torture_raw_ioctl");
132
133         if (cli_deltree(cli, BASEDIR) == -1) {
134                 printf("Failed to clean " BASEDIR "\n");
135                 return False;
136         }
137         if (!cli_mkdir(cli, BASEDIR)) {
138                 printf("Failed to create " BASEDIR " - %s\n", cli_errstr(cli));
139                 return False;
140         }
141
142         if (!test_ioctl(cli, mem_ctx)) {
143                 ret = False;
144         }
145
146         if (!test_fsctl(cli, mem_ctx)) {
147                 ret = False;
148         }
149
150         smb_raw_exit(cli->session);
151         cli_deltree(cli, BASEDIR);
152
153         torture_close_connection(cli);
154         talloc_destroy(mem_ctx);
155         return ret;
156 }