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