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