r23792: convert Samba4 to GPLv3
[ira/wip.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 3 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, see <http://www.gnu.org/licenses/>.
18 */
19
20 #include "includes.h"
21 #include "torture/torture.h"
22 #include "system/time.h"
23 #include "libcli/raw/libcliraw.h"
24 #include "libcli/libcli.h"
25 #include "torture/util.h"
26
27
28 /* basic testing of all RAW_CLOSE_* calls 
29 */
30 BOOL torture_raw_close(struct torture_context *torture)
31 {
32         struct smbcli_state *cli;
33         BOOL ret = True;
34         TALLOC_CTX *mem_ctx;
35         union smb_close io;
36         union smb_flush io_flush;
37         int fnum;
38         const char *fname = "\\torture_close.txt";
39         time_t basetime = (time(NULL) + 3*86400) & ~1;
40         union smb_fileinfo finfo, finfo2;
41         NTSTATUS status;
42
43         if (!torture_open_connection(&cli, 0)) {
44                 return False;
45         }
46
47         mem_ctx = talloc_init("torture_raw_close");
48
49 #define REOPEN do { \
50         fnum = create_complex_file(cli, mem_ctx, fname); \
51         if (fnum == -1) { \
52                 printf("(%d) Failed to create %s\n", __LINE__, fname); \
53                 ret = False; \
54                 goto done; \
55         }} while (0)
56
57 #define CHECK_STATUS(status, correct) do { \
58         if (!NT_STATUS_EQUAL(status, correct)) { \
59                 printf("(%d) Incorrect status %s - should be %s\n", \
60                        __LINE__, nt_errstr(status), nt_errstr(correct)); \
61                 ret = False; \
62                 goto done; \
63         }} while (0)
64
65         REOPEN;
66
67         io.close.level = RAW_CLOSE_CLOSE;
68         io.close.in.file.fnum = fnum;
69         io.close.in.write_time = basetime;
70         status = smb_raw_close(cli->tree, &io);
71         CHECK_STATUS(status, NT_STATUS_OK);
72
73         status = smb_raw_close(cli->tree, &io);
74         CHECK_STATUS(status, NT_STATUS_INVALID_HANDLE);
75         
76         printf("testing close.in.write_time\n");
77
78         /* the file should have the write time set */
79         finfo.generic.level = RAW_FILEINFO_ALL_INFO;
80         finfo.generic.in.file.path = fname;
81         status = smb_raw_pathinfo(cli->tree, mem_ctx, &finfo);
82         CHECK_STATUS(status, NT_STATUS_OK);
83
84         if (basetime != nt_time_to_unix(finfo.all_info.out.write_time)) {
85                 printf("Incorrect write time on file - %s - %s\n",
86                        timestring(mem_ctx, basetime), 
87                        nt_time_string(mem_ctx, finfo.all_info.out.write_time));
88                 dump_all_info(mem_ctx, &finfo);
89                 ret = False;
90         }
91
92         printf("testing other times\n");
93
94         /* none of the other times should be set to that time */
95         if (nt_time_equal(&finfo.all_info.out.write_time, 
96                           &finfo.all_info.out.access_time) ||
97             nt_time_equal(&finfo.all_info.out.write_time, 
98                           &finfo.all_info.out.create_time) ||
99             nt_time_equal(&finfo.all_info.out.write_time, 
100                           &finfo.all_info.out.change_time)) {
101                 printf("Incorrect times after close - only write time should be set\n");
102                 dump_all_info(mem_ctx, &finfo);
103
104                 if (!lp_parm_bool(-1, "torture", "samba3", False)) {
105                         /*
106                          * In Samba3 as of 3.0.23d we don't yet support all
107                          * file times, so don't mark this as a critical
108                          * failure
109                          */
110                         ret = False;
111                 }
112         }
113             
114
115         smbcli_unlink(cli->tree, fname);
116         REOPEN;
117
118         finfo2.generic.level = RAW_FILEINFO_ALL_INFO;
119         finfo2.generic.in.file.path = fname;
120         status = smb_raw_pathinfo(cli->tree, mem_ctx, &finfo2);
121         CHECK_STATUS(status, NT_STATUS_OK);
122
123         io.close.level = RAW_CLOSE_CLOSE;
124         io.close.in.file.fnum = fnum;
125         io.close.in.write_time = 0;
126         status = smb_raw_close(cli->tree, &io);
127         CHECK_STATUS(status, NT_STATUS_OK);
128
129         /* the file should have the write time set equal to access time */
130         finfo.generic.level = RAW_FILEINFO_ALL_INFO;
131         finfo.generic.in.file.path = fname;
132         status = smb_raw_pathinfo(cli->tree, mem_ctx, &finfo);
133         CHECK_STATUS(status, NT_STATUS_OK);
134
135         if (!nt_time_equal(&finfo.all_info.out.write_time, 
136                            &finfo2.all_info.out.write_time)) {
137                 printf("Incorrect write time on file - 0 time should be ignored\n");
138                 dump_all_info(mem_ctx, &finfo);
139                 ret = False;
140         }
141
142         printf("testing splclose\n");
143
144         /* check splclose on a file */
145         REOPEN;
146         io.splclose.level = RAW_CLOSE_SPLCLOSE;
147         io.splclose.in.file.fnum = fnum;
148         status = smb_raw_close(cli->tree, &io);
149         CHECK_STATUS(status, NT_STATUS_DOS(ERRSRV, ERRerror));
150
151         printf("testing flush\n");
152         smbcli_close(cli->tree, fnum);
153
154         io_flush.flush.level            = RAW_FLUSH_FLUSH;
155         io_flush.flush.in.file.fnum     = fnum;
156         status = smb_raw_flush(cli->tree, &io_flush);
157         CHECK_STATUS(status, NT_STATUS_INVALID_HANDLE);
158
159         io_flush.flush_all.level        = RAW_FLUSH_ALL;
160         status = smb_raw_flush(cli->tree, &io_flush);
161         CHECK_STATUS(status, NT_STATUS_OK);
162
163         REOPEN;
164
165         io_flush.flush.level            = RAW_FLUSH_FLUSH;
166         io_flush.flush.in.file.fnum     = fnum;
167         status = smb_raw_flush(cli->tree, &io_flush);
168         CHECK_STATUS(status, NT_STATUS_OK);
169
170         printf("Testing SMBexit\n");
171         smb_raw_exit(cli->session);
172
173         io_flush.flush.level            = RAW_FLUSH_FLUSH;
174         io_flush.flush.in.file.fnum     = fnum;
175         status = smb_raw_flush(cli->tree, &io_flush);
176         CHECK_STATUS(status, NT_STATUS_INVALID_HANDLE);
177         
178
179 done:
180         smbcli_close(cli->tree, fnum);
181         smbcli_unlink(cli->tree, fname);
182         torture_close_connection(cli);
183         talloc_free(mem_ctx);
184         return ret;
185 }