r3324: made the smbtorture code completely warning free
[jra/samba/.git] / source4 / torture / raw / close.c
1 /* 
2    Unix SMB/CIFS implementation.
3    RAW_CLOSE_* 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
24 /* basic testing of all RAW_CLOSE_* calls 
25 */
26 BOOL torture_raw_close(void)
27 {
28         struct smbcli_state *cli;
29         BOOL ret = True;
30         TALLOC_CTX *mem_ctx;
31         union smb_close io;
32         struct smb_flush io_flush;
33         int fnum;
34         const char *fname = "\\torture_close.txt";
35         time_t basetime = (time(NULL) + 3*86400) & ~1;
36         union smb_fileinfo finfo, finfo2;
37         NTSTATUS status;
38
39         if (!torture_open_connection(&cli)) {
40                 return False;
41         }
42
43         mem_ctx = talloc_init("torture_raw_close");
44
45 #define REOPEN do { \
46         fnum = create_complex_file(cli, mem_ctx, fname); \
47         if (fnum == -1) { \
48                 printf("(%d) Failed to create %s\n", __LINE__, fname); \
49                 ret = False; \
50                 goto done; \
51         }} while (0)
52
53 #define CHECK_STATUS(status, correct) do { \
54         if (!NT_STATUS_EQUAL(status, correct)) { \
55                 printf("(%d) Incorrect status %s - should be %s\n", \
56                        __LINE__, nt_errstr(status), nt_errstr(correct)); \
57                 ret = False; \
58                 goto done; \
59         }} while (0)
60
61         REOPEN;
62
63         io.close.level = RAW_CLOSE_CLOSE;
64         io.close.in.fnum = fnum;
65         io.close.in.write_time = basetime;
66         status = smb_raw_close(cli->tree, &io);
67         CHECK_STATUS(status, NT_STATUS_OK);
68
69         status = smb_raw_close(cli->tree, &io);
70         CHECK_STATUS(status, NT_STATUS_INVALID_HANDLE);
71         
72         printf("testing close.in.write_time\n");
73
74         /* the file should have the write time set */
75         finfo.generic.in.fname = fname;
76         finfo.generic.level = RAW_FILEINFO_ALL_INFO;
77         status = smb_raw_pathinfo(cli->tree, mem_ctx, &finfo);
78         CHECK_STATUS(status, NT_STATUS_OK);
79
80         if (basetime != nt_time_to_unix(finfo.all_info.out.write_time)) {
81                 printf("Incorrect write time on file - %s - %s\n",
82                        timestring(mem_ctx, basetime), 
83                        nt_time_string(mem_ctx, finfo.all_info.out.write_time));
84                 dump_all_info(mem_ctx, &finfo);
85                 ret = False;
86         }
87
88         printf("testing other times\n");
89
90         /* none of the other times should be set to that time */
91         if (nt_time_equal(&finfo.all_info.out.write_time, 
92                           &finfo.all_info.out.access_time) ||
93             nt_time_equal(&finfo.all_info.out.write_time, 
94                           &finfo.all_info.out.create_time) ||
95             nt_time_equal(&finfo.all_info.out.write_time, 
96                           &finfo.all_info.out.change_time)) {
97                 printf("Incorrect times after close - only write time should be set\n");
98                 dump_all_info(mem_ctx, &finfo);
99                 ret = False;
100         }
101             
102
103         smbcli_unlink(cli->tree, fname);
104         REOPEN;
105
106         finfo2.generic.in.fname = fname;
107         finfo2.generic.level = RAW_FILEINFO_ALL_INFO;
108         status = smb_raw_pathinfo(cli->tree, mem_ctx, &finfo2);
109         CHECK_STATUS(status, NT_STATUS_OK);
110
111         io.close.level = RAW_CLOSE_CLOSE;
112         io.close.in.fnum = fnum;
113         io.close.in.write_time = 0;
114         status = smb_raw_close(cli->tree, &io);
115         CHECK_STATUS(status, NT_STATUS_OK);
116
117         /* the file should have the write time set equal to access time */
118         finfo.generic.in.fname = fname;
119         finfo.generic.level = RAW_FILEINFO_ALL_INFO;
120         status = smb_raw_pathinfo(cli->tree, mem_ctx, &finfo);
121         CHECK_STATUS(status, NT_STATUS_OK);
122
123         if (!nt_time_equal(&finfo.all_info.out.write_time, 
124                            &finfo2.all_info.out.write_time)) {
125                 printf("Incorrect write time on file - 0 time should be ignored\n");
126                 dump_all_info(mem_ctx, &finfo);
127                 ret = False;
128         }
129
130         printf("testing splclose\n");
131
132         /* check splclose on a file */
133         REOPEN;
134         io.splclose.level = RAW_CLOSE_SPLCLOSE;
135         io.splclose.in.fnum = fnum;
136         status = smb_raw_close(cli->tree, &io);
137         CHECK_STATUS(status, NT_STATUS_UNSUCCESSFUL);
138
139         printf("testing flush\n");
140         smbcli_close(cli->tree, fnum);
141
142         io_flush.in.fnum = fnum;
143         status = smb_raw_flush(cli->tree, &io_flush);
144         CHECK_STATUS(status, NT_STATUS_INVALID_HANDLE);
145
146         io_flush.in.fnum = 0xffff;
147         status = smb_raw_flush(cli->tree, &io_flush);
148         CHECK_STATUS(status, NT_STATUS_OK);
149
150         REOPEN;
151
152         io_flush.in.fnum = fnum;
153         status = smb_raw_flush(cli->tree, &io_flush);
154         CHECK_STATUS(status, NT_STATUS_OK);
155
156         printf("Testing SMBexit\n");
157         smb_raw_exit(cli->session);
158
159         io_flush.in.fnum = fnum;
160         status = smb_raw_flush(cli->tree, &io_flush);
161         CHECK_STATUS(status, NT_STATUS_INVALID_HANDLE);
162         
163
164 done:
165         smbcli_close(cli->tree, fnum);
166         smbcli_unlink(cli->tree, fname);
167         torture_close_connection(cli);
168         talloc_destroy(mem_ctx);
169         return ret;
170 }