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