2 Unix SMB/CIFS implementation.
4 test suite for delayed write update
6 Copyright (C) Volker Lendecke 2004
7 Copyright (C) Andrew Tridgell 2004
8 Copyright (C) Jeremy Allison 2004
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 3 of the License, or
13 (at your option) any later version.
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program. If not, see <http://www.gnu.org/licenses/>.
25 #include "torture/torture.h"
26 #include "libcli/raw/libcliraw.h"
27 #include "system/time.h"
28 #include "system/filesys.h"
29 #include "libcli/libcli.h"
30 #include "torture/util.h"
32 #define BASEDIR "\\delaywrite"
34 static BOOL test_delayed_write_update(struct torture_context *tctx, struct smbcli_state *cli)
36 union smb_fileinfo finfo1, finfo2;
37 const char *fname = BASEDIR "\\torture_file.txt";
44 if (!torture_setup_dir(cli, BASEDIR)) {
48 fnum1 = smbcli_open(cli->tree, fname, O_RDWR|O_CREAT, DENY_NONE);
50 torture_comment(tctx, "Failed to open %s\n", fname);
54 finfo1.basic_info.level = RAW_FILEINFO_BASIC_INFO;
55 finfo1.basic_info.in.file.fnum = fnum1;
58 status = smb_raw_fileinfo(cli->tree, tctx, &finfo1);
60 if (!NT_STATUS_IS_OK(status)) {
61 DEBUG(0, ("fileinfo failed: %s\n", nt_errstr(status)));
65 torture_comment(tctx, "Initial write time %s\n",
66 nt_time_string(tctx, finfo1.basic_info.out.write_time));
68 /* 3 second delay to ensure we get past any 2 second time
69 granularity (older systems may have that) */
72 written = smbcli_write(cli->tree, fnum1, 0, "x", 0, 1);
75 torture_comment(tctx, "write failed - wrote %d bytes (%s)\n",
76 (int)written, __location__);
82 while (time(NULL) < t+120) {
83 status = smb_raw_fileinfo(cli->tree, tctx, &finfo2);
85 if (!NT_STATUS_IS_OK(status)) {
86 DEBUG(0, ("fileinfo failed: %s\n", nt_errstr(status)));
90 torture_comment(tctx, "write time %s\n",
91 nt_time_string(tctx, finfo2.basic_info.out.write_time));
92 if (finfo1.basic_info.out.write_time != finfo2.basic_info.out.write_time) {
93 torture_comment(tctx, "Server updated write_time after %d seconds\n",
94 (int)(time(NULL) - t));
101 if (finfo1.basic_info.out.write_time == finfo2.basic_info.out.write_time) {
102 torture_comment(tctx, "Server did not update write time?!\n");
108 smbcli_close(cli->tree, fnum1);
109 smbcli_unlink(cli->tree, fname);
110 smbcli_deltree(cli->tree, BASEDIR);
116 * Do as above, but using 2 connections.
119 static BOOL test_delayed_write_update2(struct torture_context *tctx, struct smbcli_state *cli,
120 struct smbcli_state *cli2)
122 union smb_fileinfo finfo1, finfo2;
123 const char *fname = BASEDIR "\\torture_file.txt";
130 union smb_flush flsh;
132 if (!torture_setup_dir(cli, BASEDIR)) {
136 fnum1 = smbcli_open(cli->tree, fname, O_RDWR|O_CREAT, DENY_NONE);
138 torture_comment(tctx, "Failed to open %s\n", fname);
142 finfo1.basic_info.level = RAW_FILEINFO_BASIC_INFO;
143 finfo1.basic_info.in.file.fnum = fnum1;
146 status = smb_raw_fileinfo(cli->tree, tctx, &finfo1);
148 if (!NT_STATUS_IS_OK(status)) {
149 DEBUG(0, ("fileinfo failed: %s\n", nt_errstr(status)));
153 torture_comment(tctx, "Initial write time %s\n",
154 nt_time_string(tctx, finfo1.basic_info.out.write_time));
156 /* 3 second delay to ensure we get past any 2 second time
157 granularity (older systems may have that) */
161 /* Try using setfileinfo instead of write to update write time. */
162 union smb_setfileinfo sfinfo;
163 time_t t_set = time(NULL);
164 sfinfo.basic_info.level = RAW_SFILEINFO_BASIC_INFO;
165 sfinfo.basic_info.in.file.fnum = fnum1;
166 sfinfo.basic_info.in.create_time = finfo1.basic_info.out.create_time;
167 sfinfo.basic_info.in.access_time = finfo1.basic_info.out.access_time;
169 /* I tried this with both + and - ve to see if it makes a different.
170 It doesn't - once the filetime is set via setfileinfo it stays that way. */
172 unix_to_nt_time(&sfinfo.basic_info.in.write_time, t_set - 30000);
174 unix_to_nt_time(&sfinfo.basic_info.in.write_time, t_set + 30000);
176 sfinfo.basic_info.in.change_time = finfo1.basic_info.out.change_time;
177 sfinfo.basic_info.in.attrib = finfo1.basic_info.out.attrib;
179 status = smb_raw_setfileinfo(cli->tree, &sfinfo);
181 if (!NT_STATUS_IS_OK(status)) {
182 DEBUG(0, ("sfileinfo failed: %s\n", nt_errstr(status)));
189 while (time(NULL) < t+120) {
190 finfo2.basic_info.in.file.path = fname;
192 status = smb_raw_pathinfo(cli2->tree, tctx, &finfo2);
194 if (!NT_STATUS_IS_OK(status)) {
195 DEBUG(0, ("fileinfo failed: %s\n", nt_errstr(status)));
199 torture_comment(tctx, "write time %s\n",
200 nt_time_string(tctx, finfo2.basic_info.out.write_time));
201 if (finfo1.basic_info.out.write_time != finfo2.basic_info.out.write_time) {
202 torture_comment(tctx, "Server updated write_time after %d seconds\n",
203 (int)(time(NULL) - t));
210 if (finfo1.basic_info.out.write_time == finfo2.basic_info.out.write_time) {
211 torture_comment(tctx, "Server did not update write time?!\n");
215 /* Now try a write to see if the write time gets reset. */
217 finfo1.basic_info.level = RAW_FILEINFO_BASIC_INFO;
218 finfo1.basic_info.in.file.fnum = fnum1;
221 status = smb_raw_fileinfo(cli->tree, tctx, &finfo1);
223 if (!NT_STATUS_IS_OK(status)) {
224 DEBUG(0, ("fileinfo failed: %s\n", nt_errstr(status)));
228 torture_comment(tctx, "Modified write time %s\n",
229 nt_time_string(tctx, finfo1.basic_info.out.write_time));
232 torture_comment(tctx, "Doing a 10 byte write to extend the file and see if this changes the last write time.\n");
234 written = smbcli_write(cli->tree, fnum1, 0, "0123456789", 1, 10);
237 torture_comment(tctx, "write failed - wrote %d bytes (%s)\n",
238 (int)written, __location__);
242 /* Just to prove to tridge that the an smbflush has no effect on
243 the write time :-). The setfileinfo IS STICKY. JRA. */
245 torture_comment(tctx, "Doing flush after write\n");
247 flsh.flush.level = RAW_FLUSH_FLUSH;
248 flsh.flush.in.file.fnum = fnum1;
249 status = smb_raw_flush(cli->tree, &flsh);
250 if (!NT_STATUS_IS_OK(status)) {
251 DEBUG(0, ("smbflush failed: %s\n", nt_errstr(status)));
257 /* Once the time was set using setfileinfo then it stays set - writes
258 don't have any effect. But make sure. */
260 while (time(NULL) < t+15) {
261 status = smb_raw_fileinfo(cli->tree, tctx, &finfo2);
263 if (!NT_STATUS_IS_OK(status)) {
264 DEBUG(0, ("fileinfo failed: %s\n", nt_errstr(status)));
268 torture_comment(tctx, "write time %s\n",
269 nt_time_string(tctx, finfo2.basic_info.out.write_time));
270 if (finfo1.basic_info.out.write_time != finfo2.basic_info.out.write_time) {
271 torture_comment(tctx, "Server updated write_time after %d seconds\n",
272 (int)(time(NULL) - t));
279 if (finfo1.basic_info.out.write_time == finfo2.basic_info.out.write_time) {
280 torture_comment(tctx, "Server did not update write time\n");
283 fnum2 = smbcli_open(cli->tree, fname, O_RDWR, DENY_NONE);
285 torture_comment(tctx, "Failed to open %s\n", fname);
289 torture_comment(tctx, "Doing a 10 byte write to extend the file via second fd and see if this changes the last write time.\n");
291 written = smbcli_write(cli->tree, fnum2, 0, "0123456789", 11, 10);
294 torture_comment(tctx, "write failed - wrote %d bytes (%s)\n",
295 (int)written, __location__);
299 status = smb_raw_fileinfo(cli->tree, tctx, &finfo2);
301 if (!NT_STATUS_IS_OK(status)) {
302 DEBUG(0, ("fileinfo failed: %s\n", nt_errstr(status)));
305 torture_comment(tctx, "write time %s\n",
306 nt_time_string(tctx, finfo2.basic_info.out.write_time));
307 if (finfo1.basic_info.out.write_time != finfo2.basic_info.out.write_time) {
308 torture_comment(tctx, "Server updated write_time\n");
311 torture_comment(tctx, "Closing the first fd to see if write time updated.\n");
312 smbcli_close(cli->tree, fnum1);
315 torture_comment(tctx, "Doing a 10 byte write to extend the file via second fd and see if this changes the last write time.\n");
317 written = smbcli_write(cli->tree, fnum2, 0, "0123456789", 21, 10);
320 torture_comment(tctx, "write failed - wrote %d bytes (%s)\n",
321 (int)written, __location__);
325 finfo1.basic_info.level = RAW_FILEINFO_BASIC_INFO;
326 finfo1.basic_info.in.file.fnum = fnum2;
328 status = smb_raw_fileinfo(cli->tree, tctx, &finfo2);
330 if (!NT_STATUS_IS_OK(status)) {
331 DEBUG(0, ("fileinfo failed: %s\n", nt_errstr(status)));
334 torture_comment(tctx, "write time %s\n",
335 nt_time_string(tctx, finfo2.basic_info.out.write_time));
336 if (finfo1.basic_info.out.write_time != finfo2.basic_info.out.write_time) {
337 torture_comment(tctx, "Server updated write_time\n");
342 /* Once the time was set using setfileinfo then it stays set - writes
343 don't have any effect. But make sure. */
345 while (time(NULL) < t+15) {
346 status = smb_raw_fileinfo(cli->tree, tctx, &finfo2);
348 if (!NT_STATUS_IS_OK(status)) {
349 DEBUG(0, ("fileinfo failed: %s\n", nt_errstr(status)));
353 torture_comment(tctx, "write time %s\n",
354 nt_time_string(tctx, finfo2.basic_info.out.write_time));
355 if (finfo1.basic_info.out.write_time != finfo2.basic_info.out.write_time) {
356 torture_comment(tctx, "Server updated write_time after %d seconds\n",
357 (int)(time(NULL) - t));
364 if (finfo1.basic_info.out.write_time == finfo2.basic_info.out.write_time) {
365 torture_comment(tctx, "Server did not update write time\n");
368 torture_comment(tctx, "Closing both fd's to see if write time updated.\n");
370 smbcli_close(cli->tree, fnum2);
373 fnum1 = smbcli_open(cli->tree, fname, O_RDWR, DENY_NONE);
375 torture_comment(tctx, "Failed to open %s\n", fname);
379 finfo1.basic_info.level = RAW_FILEINFO_BASIC_INFO;
380 finfo1.basic_info.in.file.fnum = fnum1;
383 status = smb_raw_fileinfo(cli->tree, tctx, &finfo1);
385 if (!NT_STATUS_IS_OK(status)) {
386 DEBUG(0, ("fileinfo failed: %s\n", nt_errstr(status)));
390 torture_comment(tctx, "Second open initial write time %s\n",
391 nt_time_string(tctx, finfo1.basic_info.out.write_time));
394 torture_comment(tctx, "Doing a 10 byte write to extend the file to see if this changes the last write time.\n");
396 written = smbcli_write(cli->tree, fnum1, 0, "0123456789", 31, 10);
399 torture_comment(tctx, "write failed - wrote %d bytes (%s)\n",
400 (int)written, __location__);
404 finfo1.basic_info.level = RAW_FILEINFO_BASIC_INFO;
405 finfo1.basic_info.in.file.fnum = fnum1;
407 status = smb_raw_fileinfo(cli->tree, tctx, &finfo2);
409 if (!NT_STATUS_IS_OK(status)) {
410 DEBUG(0, ("fileinfo failed: %s\n", nt_errstr(status)));
413 torture_comment(tctx, "write time %s\n",
414 nt_time_string(tctx, finfo2.basic_info.out.write_time));
415 if (finfo1.basic_info.out.write_time != finfo2.basic_info.out.write_time) {
416 torture_comment(tctx, "Server updated write_time\n");
421 /* Once the time was set using setfileinfo then it stays set - writes
422 don't have any effect. But make sure. */
424 while (time(NULL) < t+15) {
425 status = smb_raw_fileinfo(cli->tree, tctx, &finfo2);
427 if (!NT_STATUS_IS_OK(status)) {
428 DEBUG(0, ("fileinfo failed: %s\n", nt_errstr(status)));
432 torture_comment(tctx, "write time %s\n",
433 nt_time_string(tctx, finfo2.basic_info.out.write_time));
434 if (finfo1.basic_info.out.write_time != finfo2.basic_info.out.write_time) {
435 torture_comment(tctx, "Server updated write_time after %d seconds\n",
436 (int)(time(NULL) - t));
443 if (finfo1.basic_info.out.write_time == finfo2.basic_info.out.write_time) {
444 torture_comment(tctx, "Server did not update write time\n");
448 /* One more test to do. We should read the filetime via findfirst on the
449 second connection to ensure it's the same. This is very easy for a Windows
450 server but a bastard to get right on a POSIX server. JRA. */
453 smbcli_close(cli->tree, fnum1);
454 smbcli_unlink(cli->tree, fname);
455 smbcli_deltree(cli->tree, BASEDIR);
461 /* Windows does obviously not update the stat info during a write call. I
462 * *think* this is the problem causing a spurious Excel 2003 on XP error
463 * message when saving a file. Excel does a setfileinfo, writes, and then does
464 * a getpath(!)info. Or so... For Samba sometimes it displays an error message
465 * that the file might have been changed in between. What i've been able to
466 * trace down is that this happens if the getpathinfo after the write shows a
467 * different last write time than the setfileinfo showed. This is really
471 static BOOL test_finfo_after_write(struct torture_context *tctx, struct smbcli_state *cli,
472 struct smbcli_state *cli2)
474 union smb_fileinfo finfo1, finfo2;
475 const char *fname = BASEDIR "\\torture_file.txt";
482 if (!torture_setup_dir(cli, BASEDIR)) {
486 fnum1 = smbcli_open(cli->tree, fname, O_RDWR|O_CREAT, DENY_NONE);
489 torture_result(tctx, TORTURE_FAIL, __location__": unable to open %s", fname);
493 finfo1.basic_info.level = RAW_FILEINFO_BASIC_INFO;
494 finfo1.basic_info.in.file.fnum = fnum1;
496 status = smb_raw_fileinfo(cli->tree, tctx, &finfo1);
498 if (!NT_STATUS_IS_OK(status)) {
500 torture_result(tctx, TORTURE_FAIL, __location__": fileinfo failed: %s", nt_errstr(status));
506 written = smbcli_write(cli->tree, fnum1, 0, "x", 0, 1);
509 torture_result(tctx, TORTURE_FAIL, __location__": written gave %d - should have been 1", (int)written);
514 fnum2 = smbcli_open(cli2->tree, fname, O_RDWR, DENY_NONE);
516 torture_result(tctx, TORTURE_FAIL, __location__": failed to open 2nd time - %s",
517 smbcli_errstr(cli2->tree));
522 written = smbcli_write(cli2->tree, fnum2, 0, "x", 0, 1);
525 torture_result(tctx, TORTURE_FAIL, __location__": written gave %d - should have been 1",
531 finfo2.basic_info.level = RAW_FILEINFO_BASIC_INFO;
532 finfo2.basic_info.in.file.path = fname;
534 status = smb_raw_pathinfo(cli2->tree, tctx, &finfo2);
536 if (!NT_STATUS_IS_OK(status)) {
537 torture_result(tctx, TORTURE_FAIL, __location__": fileinfo failed: %s",
543 if (finfo1.basic_info.out.create_time !=
544 finfo2.basic_info.out.create_time) {
545 torture_result(tctx, TORTURE_FAIL, __location__": create_time changed");
550 if (finfo1.basic_info.out.access_time !=
551 finfo2.basic_info.out.access_time) {
552 torture_result(tctx, TORTURE_FAIL, __location__": access_time changed");
557 if (finfo1.basic_info.out.write_time !=
558 finfo2.basic_info.out.write_time) {
559 torture_result(tctx, TORTURE_FAIL, __location__": write_time changed:\n"
560 "write time conn 1 = %s, conn 2 = %s",
561 nt_time_string(tctx, finfo1.basic_info.out.write_time),
562 nt_time_string(tctx, finfo2.basic_info.out.write_time));
567 if (finfo1.basic_info.out.change_time !=
568 finfo2.basic_info.out.change_time) {
569 torture_result(tctx, TORTURE_FAIL, __location__": change_time changed");
574 /* One of the two following calls updates the qpathinfo. */
576 /* If you had skipped the smbcli_write on fnum2, it would
577 * *not* have updated the stat on disk */
579 smbcli_close(cli2->tree, fnum2);
582 /* This call is only for the people looking at ethereal :-) */
583 finfo2.basic_info.level = RAW_FILEINFO_BASIC_INFO;
584 finfo2.basic_info.in.file.path = fname;
586 status = smb_raw_pathinfo(cli->tree, tctx, &finfo2);
588 if (!NT_STATUS_IS_OK(status)) {
589 torture_result(tctx, TORTURE_FAIL, __location__": fileinfo failed: %s", nt_errstr(status));
596 smbcli_close(cli->tree, fnum1);
597 smbcli_unlink(cli->tree, fname);
598 smbcli_deltree(cli->tree, BASEDIR);
605 testing of delayed update of write_time
607 struct torture_suite *torture_delay_write(void)
609 struct torture_suite *suite = torture_suite_create(talloc_autofree_context(), "DELAYWRITE");
611 torture_suite_add_2smb_test(suite, "finfo update on close", test_finfo_after_write);
612 torture_suite_add_1smb_test(suite, "delayed update of write time", test_delayed_write_update);
613 torture_suite_add_2smb_test(suite, "delayed update of write time using 2 connections", test_delayed_write_update2);