Merge branch 'v4-0-test' of ssh://git.samba.org/data/git/samba into v4-0-gmake3
[nivanova/samba-autobuild/.git] / source4 / torture / basic / delaywrite.c
1 /* 
2    Unix SMB/CIFS implementation.
3
4    test suite for delayed write update 
5
6    Copyright (C) Volker Lendecke 2004
7    Copyright (C) Andrew Tridgell 2004
8    Copyright (C) Jeremy Allison 2004
9    
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.
14    
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.
19    
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/>.
22 */
23
24 #include "includes.h"
25 #include "torture/torture.h"
26 #include "libcli/raw/libcliraw.h"
27 #include "libcli/raw/raw_proto.h"
28 #include "system/time.h"
29 #include "system/filesys.h"
30 #include "libcli/libcli.h"
31 #include "torture/util.h"
32
33 #define BASEDIR "\\delaywrite"
34
35 static bool test_delayed_write_update(struct torture_context *tctx, struct smbcli_state *cli)
36 {
37         union smb_fileinfo finfo1, finfo2;
38         const char *fname = BASEDIR "\\torture_file.txt";
39         NTSTATUS status;
40         int fnum1 = -1;
41         bool ret = true;
42         ssize_t written;
43         time_t t;
44
45         if (!torture_setup_dir(cli, BASEDIR)) {
46                 return false;
47         }
48
49         fnum1 = smbcli_open(cli->tree, fname, O_RDWR|O_CREAT, DENY_NONE);
50         if (fnum1 == -1) {
51                 torture_comment(tctx, "Failed to open %s\n", fname);
52                 return false;
53         }
54
55         finfo1.basic_info.level = RAW_FILEINFO_BASIC_INFO;
56         finfo1.basic_info.in.file.fnum = fnum1;
57         finfo2 = finfo1;
58
59         status = smb_raw_fileinfo(cli->tree, tctx, &finfo1);
60
61         if (!NT_STATUS_IS_OK(status)) {
62                 DEBUG(0, ("fileinfo failed: %s\n", nt_errstr(status)));
63                 return false;
64         }
65         
66         torture_comment(tctx, "Initial write time %s\n", 
67                nt_time_string(tctx, finfo1.basic_info.out.write_time));
68
69         /* 3 second delay to ensure we get past any 2 second time
70            granularity (older systems may have that) */
71         sleep(3);
72
73         written =  smbcli_write(cli->tree, fnum1, 0, "x", 0, 1);
74
75         if (written != 1) {
76                 torture_comment(tctx, "write failed - wrote %d bytes (%s)\n", 
77                        (int)written, __location__);
78                 return false;
79         }
80
81         t = time(NULL);
82
83         while (time(NULL) < t+120) {
84                 status = smb_raw_fileinfo(cli->tree, tctx, &finfo2);
85
86                 if (!NT_STATUS_IS_OK(status)) {
87                         DEBUG(0, ("fileinfo failed: %s\n", nt_errstr(status)));
88                         ret = false;
89                         break;
90                 }
91                 torture_comment(tctx, "write time %s\n", 
92                        nt_time_string(tctx, finfo2.basic_info.out.write_time));
93                 if (finfo1.basic_info.out.write_time != finfo2.basic_info.out.write_time) {
94                         int diff = time(NULL) - t;
95                         if (diff < 2) {
96                                 torture_comment(tctx, "Server updated write_time after %d seconds (wrong!)\n",
97                                                 diff);
98                                 ret = false;
99                                 break;
100                         }
101
102                         torture_comment(tctx, "Server updated write_time after %d seconds (correct)\n",
103                                         diff);
104                         break;
105                 }
106                 sleep(1);
107                 fflush(stdout);
108         }
109         
110         if (finfo1.basic_info.out.write_time == finfo2.basic_info.out.write_time) {
111                 torture_comment(tctx, "Server did not update write time (wrong!)\n");
112                 ret = false;
113         }
114
115
116         if (fnum1 != -1)
117                 smbcli_close(cli->tree, fnum1);
118         smbcli_unlink(cli->tree, fname);
119         smbcli_deltree(cli->tree, BASEDIR);
120
121         return ret;
122 }
123
124 /* 
125  * Do as above, but using 2 connections.
126  */
127
128 static bool test_delayed_write_update2(struct torture_context *tctx, struct smbcli_state *cli, 
129                                                                            struct smbcli_state *cli2)
130 {
131         union smb_fileinfo finfo1, finfo2;
132         const char *fname = BASEDIR "\\torture_file.txt";
133         NTSTATUS status;
134         int fnum1 = -1;
135         int fnum2 = -1;
136         bool ret = true;
137         ssize_t written;
138         time_t t;
139         union smb_flush flsh;
140
141         if (!torture_setup_dir(cli, BASEDIR)) {
142                 return false;
143         }
144
145         fnum1 = smbcli_open(cli->tree, fname, O_RDWR|O_CREAT, DENY_NONE);
146         if (fnum1 == -1) {
147                 torture_comment(tctx, "Failed to open %s\n", fname);
148                 return false;
149         }
150
151         finfo1.basic_info.level = RAW_FILEINFO_BASIC_INFO;
152         finfo1.basic_info.in.file.fnum = fnum1;
153         finfo2 = finfo1;
154
155         status = smb_raw_fileinfo(cli->tree, tctx, &finfo1);
156
157         if (!NT_STATUS_IS_OK(status)) {
158                 DEBUG(0, ("fileinfo failed: %s\n", nt_errstr(status)));
159                 return false;
160         }
161         
162         torture_comment(tctx, "Initial write time %s\n", 
163                nt_time_string(tctx, finfo1.basic_info.out.write_time));
164
165         /* 3 second delay to ensure we get past any 2 second time
166            granularity (older systems may have that) */
167         sleep(3);
168
169         {
170                 /* Try using setfileinfo instead of write to update write time. */
171                 union smb_setfileinfo sfinfo;
172                 time_t t_set = time(NULL);
173                 sfinfo.basic_info.level = RAW_SFILEINFO_BASIC_INFO;
174                 sfinfo.basic_info.in.file.fnum = fnum1;
175                 sfinfo.basic_info.in.create_time = finfo1.basic_info.out.create_time;
176                 sfinfo.basic_info.in.access_time = finfo1.basic_info.out.access_time;
177
178                 /* I tried this with both + and - ve to see if it makes a different.
179                    It doesn't - once the filetime is set via setfileinfo it stays that way. */
180 #if 1
181                 unix_to_nt_time(&sfinfo.basic_info.in.write_time, t_set - 30000);
182 #else
183                 unix_to_nt_time(&sfinfo.basic_info.in.write_time, t_set + 30000);
184 #endif
185                 sfinfo.basic_info.in.change_time = finfo1.basic_info.out.change_time;
186                 sfinfo.basic_info.in.attrib = finfo1.basic_info.out.attrib;
187
188                 status = smb_raw_setfileinfo(cli->tree, &sfinfo);
189
190                 if (!NT_STATUS_IS_OK(status)) {
191                         DEBUG(0, ("sfileinfo failed: %s\n", nt_errstr(status)));
192                         return false;
193                 }
194         }
195
196         finfo2.basic_info.in.file.path = fname;
197         
198         status = smb_raw_pathinfo(cli2->tree, tctx, &finfo2);
199
200         if (!NT_STATUS_IS_OK(status)) {
201                 DEBUG(0, ("fileinfo failed: %s\n", nt_errstr(status)));
202                 return false;
203         }
204         torture_comment(tctx, "write time %s\n",
205                nt_time_string(tctx, finfo2.basic_info.out.write_time));
206
207         if (finfo1.basic_info.out.write_time != finfo2.basic_info.out.write_time) {
208                 torture_comment(tctx, "Server updated write_time (correct)\n");
209         } else {
210                 torture_comment(tctx, "Server did not update write time (wrong!)\n");
211                 ret = false;
212         }
213
214         /* Now try a write to see if the write time gets reset. */
215
216         finfo1.basic_info.level = RAW_FILEINFO_BASIC_INFO;
217         finfo1.basic_info.in.file.fnum = fnum1;
218         finfo2 = finfo1;
219
220         status = smb_raw_fileinfo(cli->tree, tctx, &finfo1);
221
222         if (!NT_STATUS_IS_OK(status)) {
223                 DEBUG(0, ("fileinfo failed: %s\n", nt_errstr(status)));
224                 return false;
225         }
226         
227         torture_comment(tctx, "Modified write time %s\n", 
228                nt_time_string(tctx, finfo1.basic_info.out.write_time));
229
230
231         torture_comment(tctx, "Doing a 10 byte write to extend the file and see if this changes the last write time.\n");
232
233         written =  smbcli_write(cli->tree, fnum1, 0, "0123456789", 1, 10);
234
235         if (written != 10) {
236                 torture_comment(tctx, "write failed - wrote %d bytes (%s)\n", 
237                        (int)written, __location__);
238                 return false;
239         }
240
241         /* Just to prove to tridge that the an smbflush has no effect on
242            the write time :-). The setfileinfo IS STICKY. JRA. */
243
244         torture_comment(tctx, "Doing flush after write\n");
245
246         flsh.flush.level        = RAW_FLUSH_FLUSH;
247         flsh.flush.in.file.fnum = fnum1;
248         status = smb_raw_flush(cli->tree, &flsh);
249         if (!NT_STATUS_IS_OK(status)) {
250                 DEBUG(0, ("smbflush failed: %s\n", nt_errstr(status)));
251                 return false;
252         }
253
254         t = time(NULL);
255
256         /* Once the time was set using setfileinfo then it stays set - writes
257            don't have any effect. But make sure. */
258
259         while (time(NULL) < t+15) {
260                 status = smb_raw_fileinfo(cli->tree, tctx, &finfo2);
261
262                 if (!NT_STATUS_IS_OK(status)) {
263                         DEBUG(0, ("fileinfo failed: %s\n", nt_errstr(status)));
264                         ret = false;
265                         break;
266                 }
267                 torture_comment(tctx, "write time %s\n", 
268                        nt_time_string(tctx, finfo2.basic_info.out.write_time));
269                 if (finfo1.basic_info.out.write_time != finfo2.basic_info.out.write_time) {
270                         torture_comment(tctx, "Server updated write_time after %d seconds (wrong!)\n",
271                                (int)(time(NULL) - t));
272                         ret = false;
273                         break;
274                 }
275                 sleep(1);
276                 fflush(stdout);
277         }
278         
279         if (finfo1.basic_info.out.write_time == finfo2.basic_info.out.write_time) {
280                 torture_comment(tctx, "Server did not update write time (correct)\n");
281         }
282
283         fnum2 = smbcli_open(cli->tree, fname, O_RDWR, DENY_NONE);
284         if (fnum2 == -1) {
285                 torture_comment(tctx, "Failed to open %s\n", fname);
286                 return false;
287         }
288         
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");
290
291         written =  smbcli_write(cli->tree, fnum2, 0, "0123456789", 11, 10);
292
293         if (written != 10) {
294                 torture_comment(tctx, "write failed - wrote %d bytes (%s)\n", 
295                        (int)written, __location__);
296                 return false;
297         }
298
299         status = smb_raw_fileinfo(cli->tree, tctx, &finfo2);
300
301         if (!NT_STATUS_IS_OK(status)) {
302                 DEBUG(0, ("fileinfo failed: %s\n", nt_errstr(status)));
303                 return false;
304         }
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 (wrong!)\n");
309                 ret = false;
310         }
311
312         torture_comment(tctx, "Closing the first fd to see if write time updated.\n");
313         smbcli_close(cli->tree, fnum1);
314         fnum1 = -1;
315
316         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
318         written =  smbcli_write(cli->tree, fnum2, 0, "0123456789", 21, 10);
319
320         if (written != 10) {
321                 torture_comment(tctx, "write failed - wrote %d bytes (%s)\n", 
322                        (int)written, __location__);
323                 return false;
324         }
325
326         finfo1.basic_info.level = RAW_FILEINFO_BASIC_INFO;
327         finfo1.basic_info.in.file.fnum = fnum2;
328         finfo2 = finfo1;
329         status = smb_raw_fileinfo(cli->tree, tctx, &finfo2);
330
331         if (!NT_STATUS_IS_OK(status)) {
332                 DEBUG(0, ("fileinfo failed: %s\n", nt_errstr(status)));
333                 return false;
334         }
335         torture_comment(tctx, "write time %s\n", 
336                nt_time_string(tctx, finfo2.basic_info.out.write_time));
337         if (finfo1.basic_info.out.write_time != finfo2.basic_info.out.write_time) {
338                 torture_comment(tctx, "Server updated write_time (wrong!)\n");
339                 ret = false;
340         }
341
342         t = time(NULL);
343
344         /* Once the time was set using setfileinfo then it stays set - writes
345            don't have any effect. But make sure. */
346
347         while (time(NULL) < t+15) {
348                 status = smb_raw_fileinfo(cli->tree, tctx, &finfo2);
349
350                 if (!NT_STATUS_IS_OK(status)) {
351                         DEBUG(0, ("fileinfo failed: %s\n", nt_errstr(status)));
352                         ret = false;
353                         break;
354                 }
355                 torture_comment(tctx, "write time %s\n", 
356                        nt_time_string(tctx, finfo2.basic_info.out.write_time));
357                 if (finfo1.basic_info.out.write_time != finfo2.basic_info.out.write_time) {
358                         torture_comment(tctx, "Server updated write_time after %d seconds (wrong!)\n",
359                                (int)(time(NULL) - t));
360                         ret = false;
361                         break;
362                 }
363                 sleep(1);
364                 fflush(stdout);
365         }
366         
367         if (finfo1.basic_info.out.write_time == finfo2.basic_info.out.write_time) {
368                 torture_comment(tctx, "Server did not update write time (correct)\n");
369         }
370
371         torture_comment(tctx, "Closing second fd to see if write time updated.\n");
372
373         smbcli_close(cli->tree, fnum2);
374         fnum2 = -1;
375
376         fnum1 = smbcli_open(cli->tree, fname, O_RDWR, DENY_NONE);
377         if (fnum1 == -1) {
378                 torture_comment(tctx, "Failed to open %s\n", fname);
379                 return false;
380         }
381
382         finfo1.basic_info.level = RAW_FILEINFO_BASIC_INFO;
383         finfo1.basic_info.in.file.fnum = fnum1;
384         finfo2 = finfo1;
385
386         status = smb_raw_fileinfo(cli->tree, tctx, &finfo1);
387
388         if (!NT_STATUS_IS_OK(status)) {
389                 DEBUG(0, ("fileinfo failed: %s\n", nt_errstr(status)));
390                 return false;
391         }
392         
393         torture_comment(tctx, "Second open initial write time %s\n", 
394                nt_time_string(tctx, finfo1.basic_info.out.write_time));
395
396         sleep(10);
397         torture_comment(tctx, "Doing a 10 byte write to extend the file to see if this changes the last write time.\n");
398
399         written =  smbcli_write(cli->tree, fnum1, 0, "0123456789", 31, 10);
400
401         if (written != 10) {
402                 torture_comment(tctx, "write failed - wrote %d bytes (%s)\n", 
403                        (int)written, __location__);
404                 return false;
405         }
406
407         finfo1.basic_info.level = RAW_FILEINFO_BASIC_INFO;
408         finfo1.basic_info.in.file.fnum = fnum1;
409         finfo2 = finfo1;
410         status = smb_raw_fileinfo(cli->tree, tctx, &finfo2);
411
412         if (!NT_STATUS_IS_OK(status)) {
413                 DEBUG(0, ("fileinfo failed: %s\n", nt_errstr(status)));
414                 return false;
415         }
416         torture_comment(tctx, "write time %s\n", 
417                nt_time_string(tctx, finfo2.basic_info.out.write_time));
418         if (finfo1.basic_info.out.write_time != finfo2.basic_info.out.write_time) {
419                 torture_comment(tctx, "Server updated write_time (wrong!)\n");
420                 ret = false;
421         }
422
423         t = time(NULL);
424
425         /* Now the write time should be updated again */
426
427         while (time(NULL) < t+15) {
428                 status = smb_raw_fileinfo(cli->tree, tctx, &finfo2);
429
430                 if (!NT_STATUS_IS_OK(status)) {
431                         DEBUG(0, ("fileinfo failed: %s\n", nt_errstr(status)));
432                         ret = false;
433                         break;
434                 }
435                 torture_comment(tctx, "write time %s\n", 
436                        nt_time_string(tctx, finfo2.basic_info.out.write_time));
437                 if (finfo1.basic_info.out.write_time != finfo2.basic_info.out.write_time) {
438                         int diff = time(NULL) - t;
439                         if (diff < 2) {
440                                 torture_comment(tctx, "Server updated write_time after %d seconds (wrong!)\n",
441                                                 diff);
442                                 ret = false;
443                                 break;
444                         }
445
446                         torture_comment(tctx, "Server updated write_time after %d seconds (correct)\n",
447                                         diff);
448                         break;
449                 }
450                 sleep(1);
451                 fflush(stdout);
452         }
453         
454         if (finfo1.basic_info.out.write_time == finfo2.basic_info.out.write_time) {
455                 torture_comment(tctx, "Server did not update write time (wrong!)\n");
456                 ret = false;
457         }
458
459
460         /* One more test to do. We should read the filetime via findfirst on the
461            second connection to ensure it's the same. This is very easy for a Windows
462            server but a bastard to get right on a POSIX server. JRA. */
463
464         if (fnum1 != -1)
465                 smbcli_close(cli->tree, fnum1);
466         smbcli_unlink(cli->tree, fname);
467         smbcli_deltree(cli->tree, BASEDIR);
468
469         return ret;
470 }
471
472
473 /* Windows does obviously not update the stat info during a write call. I
474  * *think* this is the problem causing a spurious Excel 2003 on XP error
475  * message when saving a file. Excel does a setfileinfo, writes, and then does
476  * a getpath(!)info. Or so... For Samba sometimes it displays an error message
477  * that the file might have been changed in between. What i've been able to
478  * trace down is that this happens if the getpathinfo after the write shows a
479  * different last write time than the setfileinfo showed. This is really
480  * nasty....
481  */
482
483 static bool test_finfo_after_write(struct torture_context *tctx, struct smbcli_state *cli, 
484                                                                    struct smbcli_state *cli2)
485 {
486         union smb_fileinfo finfo1, finfo2;
487         const char *fname = BASEDIR "\\torture_file.txt";
488         NTSTATUS status;
489         int fnum1 = -1;
490         int fnum2;
491         bool ret = true;
492         ssize_t written;
493
494         if (!torture_setup_dir(cli, BASEDIR)) {
495                 return false;
496         }
497
498         fnum1 = smbcli_open(cli->tree, fname, O_RDWR|O_CREAT, DENY_NONE);
499         if (fnum1 == -1) {
500                 ret = false;
501                 torture_result(tctx, TORTURE_FAIL, __location__": unable to open %s", fname);
502                 goto done;
503         }
504
505         finfo1.basic_info.level = RAW_FILEINFO_BASIC_INFO;
506         finfo1.basic_info.in.file.fnum = fnum1;
507
508         status = smb_raw_fileinfo(cli->tree, tctx, &finfo1);
509
510         if (!NT_STATUS_IS_OK(status)) {
511                 ret = false;
512                 torture_result(tctx, TORTURE_FAIL, __location__": fileinfo failed: %s", nt_errstr(status));
513                 goto done;
514         }
515
516         msleep(1000);
517
518         written =  smbcli_write(cli->tree, fnum1, 0, "x", 0, 1);
519
520         if (written != 1) {
521                 torture_result(tctx, TORTURE_FAIL, __location__": written gave %d - should have been 1", (int)written);
522                 ret = false;
523                 goto done;
524         }
525
526         fnum2 = smbcli_open(cli2->tree, fname, O_RDWR, DENY_NONE);
527         if (fnum2 == -1) {
528                 torture_result(tctx, TORTURE_FAIL, __location__": failed to open 2nd time - %s", 
529                        smbcli_errstr(cli2->tree));
530                 ret = false;
531                 goto done;
532         }
533         
534         written =  smbcli_write(cli2->tree, fnum2, 0, "x", 0, 1);
535         
536         if (written != 1) {
537                 torture_result(tctx, TORTURE_FAIL, __location__": written gave %d - should have been 1", 
538                        (int)written);
539                 ret = false;
540                 goto done;
541         }
542         
543         finfo2.basic_info.level = RAW_FILEINFO_BASIC_INFO;
544         finfo2.basic_info.in.file.path = fname;
545         
546         status = smb_raw_pathinfo(cli2->tree, tctx, &finfo2);
547         
548         if (!NT_STATUS_IS_OK(status)) {
549                 torture_result(tctx, TORTURE_FAIL, __location__": fileinfo failed: %s", 
550                           nt_errstr(status));
551                 ret = false;
552                 goto done;
553         }
554         
555         if (finfo1.basic_info.out.create_time !=
556             finfo2.basic_info.out.create_time) {
557                 torture_result(tctx, TORTURE_FAIL, __location__": create_time changed");
558                 ret = false;
559                 goto done;
560         }
561         
562         if (finfo1.basic_info.out.access_time !=
563             finfo2.basic_info.out.access_time) {
564                 torture_result(tctx, TORTURE_FAIL, __location__": access_time changed");
565                 ret = false;
566                 goto done;
567         }
568         
569         if (finfo1.basic_info.out.write_time !=
570             finfo2.basic_info.out.write_time) {
571                 torture_result(tctx, TORTURE_FAIL, __location__": write_time changed:\n"
572                                            "write time conn 1 = %s, conn 2 = %s", 
573                        nt_time_string(tctx, finfo1.basic_info.out.write_time),
574                        nt_time_string(tctx, finfo2.basic_info.out.write_time));
575                 ret = false;
576                 goto done;
577         }
578         
579         if (finfo1.basic_info.out.change_time !=
580             finfo2.basic_info.out.change_time) {
581                 torture_result(tctx, TORTURE_FAIL, __location__": change_time changed");
582                 ret = false;
583                 goto done;
584         }
585         
586         /* One of the two following calls updates the qpathinfo. */
587         
588         /* If you had skipped the smbcli_write on fnum2, it would
589          * *not* have updated the stat on disk */
590         
591         smbcli_close(cli2->tree, fnum2);
592         cli2 = NULL;
593
594         /* This call is only for the people looking at ethereal :-) */
595         finfo2.basic_info.level = RAW_FILEINFO_BASIC_INFO;
596         finfo2.basic_info.in.file.path = fname;
597
598         status = smb_raw_pathinfo(cli->tree, tctx, &finfo2);
599
600         if (!NT_STATUS_IS_OK(status)) {
601                 torture_result(tctx, TORTURE_FAIL, __location__": fileinfo failed: %s", nt_errstr(status));
602                 ret = false;
603                 goto done;
604         }
605
606  done:
607         if (fnum1 != -1)
608                 smbcli_close(cli->tree, fnum1);
609         smbcli_unlink(cli->tree, fname);
610         smbcli_deltree(cli->tree, BASEDIR);
611
612         return ret;
613 }
614
615 #define COMPARE_WRITE_TIME_CMP(given, correct, cmp) do { \
616         NTTIME g = (given).basic_info.out.write_time; \
617         NTTIME c = (correct).basic_info.out.write_time; \
618         if (g cmp c) { \
619                 torture_result(tctx, TORTURE_FAIL, __location__": wrong write_time (%s)%s %s (%s)%s", \
620                                 #given, nt_time_string(tctx, g), \
621                                 #cmp, #correct, nt_time_string(tctx, c)); \
622                 ret = false; \
623                 goto done; \
624         } \
625 } while (0)
626 #define COMPARE_WRITE_TIME_EQUAL(given,correct) \
627         COMPARE_WRITE_TIME_CMP(given,correct,!=)
628 #define COMPARE_WRITE_TIME_GREATER(given,correct) \
629         COMPARE_WRITE_TIME_CMP(given,correct,<=)
630 #define COMPARE_WRITE_TIME_LESS(given,correct) \
631         COMPARE_WRITE_TIME_CMP(given,correct,>=)
632
633 #define COMPARE_ACCESS_TIME_CMP(given, correct, cmp) do { \
634         NTTIME g = (given).basic_info.out.access_time; \
635         NTTIME c = (correct).basic_info.out.access_time; \
636         if (g cmp c) { \
637                 torture_result(tctx, TORTURE_FAIL, __location__": wrong access_time (%s)%s %s (%s)%s", \
638                                 #given, nt_time_string(tctx, g), \
639                                 #cmp, #correct, nt_time_string(tctx, c)); \
640                 ret = false; \
641                 goto done; \
642         } \
643 } while (0)
644 #define COMPARE_ACCESS_TIME_EQUAL(given,correct) \
645         COMPARE_ACCESS_TIME_CMP(given,correct,!=)
646 #define COMPARE_ACCESS_TIME_GREATER(given,correct) \
647         COMPARE_ACCESS_TIME_CMP(given,correct,<=)
648 #define COMPARE_ACCESS_TIME_LESS(given,correct) \
649         COMPARE_ACCESS_TIME_CMP(given,correct,>=)
650
651 #define COMPARE_BOTH_TIMES_EQUAL(given,correct) do { \
652         COMPARE_ACCESS_TIME_EQUAL(given,correct); \
653         COMPARE_WRITE_TIME_EQUAL(given,correct); \
654 } while (0)
655 #define COMPARE_BOTH_TIMES_GEATER(given,correct) do { \
656         COMPARE_ACCESS_TIME_GREATER(given,correct); \
657         COMPARE_WRITE_TIME_GREATER(given,correct); \
658 } while (0)
659 #define COMPARE_BOTH_TIMES_LESS(given,correct) do { \
660         COMPARE_ACCESS_TIME_LESS(given,correct); \
661         COMPARE_WRITE_TIME_LESS(given,correct); \
662 } while (0)
663
664 #define GET_INFO_FILE(finfo) do { \
665         NTSTATUS _status; \
666         _status = smb_raw_fileinfo(cli->tree, tctx, &finfo); \
667         if (!NT_STATUS_IS_OK(_status)) { \
668                 ret = false; \
669                 torture_result(tctx, TORTURE_FAIL, __location__": fileinfo failed: %s", \
670                                nt_errstr(_status)); \
671                 goto done; \
672         } \
673         torture_comment(tctx, "fileinfo: Access(%s) Write(%s)\n", \
674                         nt_time_string(tctx, finfo.basic_info.out.access_time), \
675                         nt_time_string(tctx, finfo.basic_info.out.write_time)); \
676 } while (0)
677 #define GET_INFO_PATH(pinfo) do { \
678         NTSTATUS _status; \
679         _status = smb_raw_pathinfo(cli2->tree, tctx, &pinfo); \
680         if (!NT_STATUS_IS_OK(_status)) { \
681                 torture_result(tctx, TORTURE_FAIL, __location__": pathinfo failed: %s", \
682                                nt_errstr(_status)); \
683                 ret = false; \
684                 goto done; \
685         } \
686         torture_comment(tctx, "pathinfo: Access(%s) Write(%s)\n", \
687                         nt_time_string(tctx, pinfo.basic_info.out.access_time), \
688                         nt_time_string(tctx, pinfo.basic_info.out.write_time)); \
689 } while (0)
690 #define GET_INFO_BOTH(finfo,pinfo) do { \
691         GET_INFO_FILE(finfo); \
692         GET_INFO_PATH(pinfo); \
693         COMPARE_BOTH_TIMES_EQUAL(finfo,pinfo); \
694 } while (0)
695
696 #define SET_INFO_FILE_EX(finfo, wrtime, tree, tfnum) do { \
697         NTSTATUS _status; \
698         union smb_setfileinfo sfinfo; \
699         sfinfo.basic_info.level = RAW_SFILEINFO_BASIC_INFO; \
700         sfinfo.basic_info.in.file.fnum = tfnum; \
701         sfinfo.basic_info.in.create_time = 0; \
702         sfinfo.basic_info.in.access_time = 0; \
703         unix_to_nt_time(&sfinfo.basic_info.in.write_time, (wrtime)); \
704         sfinfo.basic_info.in.change_time = 0; \
705         sfinfo.basic_info.in.attrib = finfo1.basic_info.out.attrib; \
706         _status = smb_raw_setfileinfo(tree, &sfinfo); \
707         if (!NT_STATUS_IS_OK(_status)) { \
708                 torture_result(tctx, TORTURE_FAIL, __location__": setfileinfo failed: %s", \
709                                nt_errstr(_status)); \
710                 ret = false; \
711                 goto done; \
712         } \
713 } while (0)
714 #define SET_INFO_FILE(finfo, wrtime) \
715         SET_INFO_FILE_EX(finfo, wrtime, cli->tree, fnum1)
716
717 static bool test_delayed_write_update3(struct torture_context *tctx,
718                                        struct smbcli_state *cli,
719                                        struct smbcli_state *cli2)
720 {
721         union smb_fileinfo finfo0, finfo1, finfo2, finfo3, finfo4;
722         union smb_fileinfo pinfo0, pinfo1, pinfo2, pinfo3, pinfo4, pinfo5;
723         const char *fname = BASEDIR "\\torture_file.txt";
724         int fnum1 = -1;
725         bool ret = true;
726         ssize_t written;
727         time_t t;
728
729         if (!torture_setup_dir(cli, BASEDIR)) {
730                 return false;
731         }
732
733         torture_comment(tctx, "Open the file handle\n");
734         fnum1 = smbcli_open(cli->tree, fname, O_RDWR|O_CREAT, DENY_NONE);
735         if (fnum1 == -1) {
736                 ret = false;
737                 torture_result(tctx, TORTURE_FAIL, __location__": unable to open %s", fname);
738                 goto done;
739         }
740
741         finfo0.basic_info.level = RAW_FILEINFO_BASIC_INFO;
742         finfo0.basic_info.in.file.fnum = fnum1;
743         finfo1 = finfo0;
744         finfo2 = finfo0;
745         finfo3 = finfo0;
746         finfo4 = finfo0;
747         pinfo0.basic_info.level = RAW_FILEINFO_BASIC_INFO;
748         pinfo0.basic_info.in.file.path = fname;
749         pinfo1 = pinfo0;
750         pinfo2 = pinfo0;
751         pinfo3 = pinfo0;
752         pinfo4 = pinfo0;
753         pinfo5 = pinfo0;
754
755         /* get the initial times */
756         GET_INFO_BOTH(finfo0,pinfo0);
757
758         /*
759          * make sure the write time is updated 2 seconds later
760          * calcuated from the first write
761          * (but expect upto 5 seconds extra time for a busy server)
762          */
763         t = time(NULL);
764         while (time(NULL) < t+7) {
765                 /* do a write */
766                 torture_comment(tctx, "Do a write on the file handle\n");
767                 written = smbcli_write(cli->tree, fnum1, 0, "x", 0, 1);
768                 if (written != 1) {
769                         torture_result(tctx, TORTURE_FAIL, __location__": written gave %d - should have been 1", (int)written);
770                         ret = false;
771                         goto done;
772                 }
773                 /* get the times after the write */
774                 GET_INFO_FILE(finfo1);
775
776                 if (finfo1.basic_info.out.write_time > finfo0.basic_info.out.write_time) {
777                         int diff = time(NULL) - t;
778                         if (diff < 2) {
779                                 torture_comment(tctx, "Server updated write_time after %d seconds (wrong!)\n",
780                                                 diff);
781                                 ret = false;
782                                 break;
783                         }
784
785                         torture_comment(tctx, "Server updated write_time after %d seconds (correct)\n",
786                                         diff);
787                         break;
788                 }
789                 msleep(500);
790         }
791
792         GET_INFO_BOTH(finfo1,pinfo1);
793
794         /* sure any further write doesn't update the write time */
795         t = time(NULL);
796         while (time(NULL) < t+15) {
797                 /* do a write */
798                 torture_comment(tctx, "Do a write on the file handle\n");
799                 written = smbcli_write(cli->tree, fnum1, 0, "x", 0, 1);
800                 if (written != 1) {
801                         torture_result(tctx, TORTURE_FAIL, __location__": written gave %d - should have been 1", (int)written);
802                         ret = false;
803                         goto done;
804                 }
805                 /* get the times after the write */
806                 GET_INFO_BOTH(finfo2,pinfo2);
807
808                 if (finfo2.basic_info.out.write_time > finfo1.basic_info.out.write_time) {
809                         torture_comment(tctx, "Server updated write_time after %d seconds (wrong!)\n",
810                                (int)(time(NULL) - t));
811                         ret = false;
812                         break;
813                 }
814                 msleep(2000);
815         }
816
817         GET_INFO_BOTH(finfo2,pinfo2);
818         COMPARE_WRITE_TIME_EQUAL(finfo2, finfo1);
819         if (finfo2.basic_info.out.write_time == finfo1.basic_info.out.write_time) {
820                 torture_comment(tctx, "Server did not update write_time (correct)\n");
821         }
822
823         /* sleep */
824         msleep(5000);
825
826         GET_INFO_BOTH(finfo3,pinfo3);
827         COMPARE_WRITE_TIME_EQUAL(finfo3, finfo2);
828
829         /*
830          * the close updates the write time to the time of the close
831          * and not to the time of the last write!
832          */
833         torture_comment(tctx, "Close the file handle\n");
834         smbcli_close(cli->tree, fnum1);
835         fnum1 = -1;
836
837         GET_INFO_PATH(pinfo4);
838         COMPARE_WRITE_TIME_GREATER(pinfo4, pinfo3);
839
840         if (pinfo4.basic_info.out.write_time > pinfo3.basic_info.out.write_time) {
841                 torture_comment(tctx, "Server updated the write_time on close (correct)\n");
842         }
843
844  done:
845         if (fnum1 != -1)
846                 smbcli_close(cli->tree, fnum1);
847         smbcli_unlink(cli->tree, fname);
848         smbcli_deltree(cli->tree, BASEDIR);
849
850         return ret;
851 }
852
853 static bool test_delayed_write_update4(struct torture_context *tctx,
854                                        struct smbcli_state *cli,
855                                        struct smbcli_state *cli2)
856 {
857         union smb_fileinfo finfo0, finfo1, finfo2, finfo3, finfo4;
858         union smb_fileinfo pinfo0, pinfo1, pinfo2, pinfo3, pinfo4, pinfo5;
859         const char *fname = BASEDIR "\\torture_file.txt";
860         int fnum1 = -1;
861         bool ret = true;
862         ssize_t written;
863         time_t t;
864
865         if (!torture_setup_dir(cli, BASEDIR)) {
866                 return false;
867         }
868
869         torture_comment(tctx, "Open the file handle\n");
870         fnum1 = smbcli_open(cli->tree, fname, O_RDWR|O_CREAT, DENY_NONE);
871         if (fnum1 == -1) {
872                 ret = false;
873                 torture_result(tctx, TORTURE_FAIL, __location__": unable to open %s", fname);
874                 goto done;
875         }
876
877         finfo0.basic_info.level = RAW_FILEINFO_BASIC_INFO;
878         finfo0.basic_info.in.file.fnum = fnum1;
879         finfo1 = finfo0;
880         finfo2 = finfo0;
881         finfo3 = finfo0;
882         finfo4 = finfo0;
883         pinfo0.basic_info.level = RAW_FILEINFO_BASIC_INFO;
884         pinfo0.basic_info.in.file.path = fname;
885         pinfo1 = pinfo0;
886         pinfo2 = pinfo0;
887         pinfo3 = pinfo0;
888         pinfo4 = pinfo0;
889         pinfo5 = pinfo0;
890
891         /* get the initial times */
892         GET_INFO_BOTH(finfo0,pinfo0);
893
894         /* sleep a bit */
895         msleep(5000);
896
897         /* do a write */
898         torture_comment(tctx, "Do a write on the file handle\n");
899         written = smbcli_write(cli->tree, fnum1, 0, "x", 0, 1);
900         if (written != 1) {
901                 torture_result(tctx, TORTURE_FAIL, __location__": written gave %d - should have been 1", (int)written);
902                 ret = false;
903                 goto done;
904         }
905
906         GET_INFO_BOTH(finfo1,pinfo1);
907         COMPARE_WRITE_TIME_EQUAL(finfo1,finfo0);
908
909         /*
910          * make sure the write time is updated 2 seconds later
911          * calcuated from the first write
912          * (but expect upto 3 seconds extra time for a busy server)
913          */
914         t = time(NULL);
915         while (time(NULL) < t+5) {
916                 /* get the times after the first write */
917                 GET_INFO_FILE(finfo1);
918
919                 if (finfo1.basic_info.out.write_time > finfo0.basic_info.out.write_time) {
920                         int diff = time(NULL) - t;
921                         if (diff < 2) {
922                                 torture_comment(tctx, "Server updated write_time after %d seconds (wrong!)\n",
923                                                 diff);
924                                 ret = false;
925                                 break;
926                         }
927
928                         torture_comment(tctx, "Server updated write_time after %d seconds (correct)\n",
929                                         diff);
930                         break;
931                 }
932                 msleep(500);
933         }
934
935         GET_INFO_BOTH(finfo1,pinfo1);
936
937         /* sure any further write doesn't update the write time */
938         t = time(NULL);
939         while (time(NULL) < t+15) {
940                 /* do a write */
941                 torture_comment(tctx, "Do a write on the file handle\n");
942                 written = smbcli_write(cli->tree, fnum1, 0, "x", 0, 1);
943                 if (written != 1) {
944                         torture_result(tctx, TORTURE_FAIL, __location__": written gave %d - should have been 1", (int)written);
945                         ret = false;
946                         goto done;
947                 }
948                 /* get the times after the write */
949                 GET_INFO_BOTH(finfo2,pinfo2);
950
951                 if (finfo2.basic_info.out.write_time > finfo1.basic_info.out.write_time) {
952                         torture_comment(tctx, "Server updated write_time after %d seconds (wrong!)\n",
953                                (int)(time(NULL) - t));
954                         ret = false;
955                         break;
956                 }
957                 msleep(2000);
958         }
959
960         GET_INFO_BOTH(finfo2,pinfo2);
961         COMPARE_WRITE_TIME_EQUAL(finfo2, finfo1);
962         if (finfo2.basic_info.out.write_time == finfo1.basic_info.out.write_time) {
963                 torture_comment(tctx, "Server did not updatewrite_time (correct)\n");
964         }
965
966         /* sleep */
967         msleep(5000);
968
969         GET_INFO_BOTH(finfo3,pinfo3);
970         COMPARE_WRITE_TIME_EQUAL(finfo3, finfo2);
971
972         /*
973          * the close updates the write time to the time of the close
974          * and not to the time of the last write!
975          */
976         torture_comment(tctx, "Close the file handle\n");
977         smbcli_close(cli->tree, fnum1);
978         fnum1 = -1;
979
980         GET_INFO_PATH(pinfo4);
981         COMPARE_WRITE_TIME_GREATER(pinfo4, pinfo3);
982
983         if (pinfo4.basic_info.out.write_time > pinfo3.basic_info.out.write_time) {
984                 torture_comment(tctx, "Server updated the write_time on close (correct)\n");
985         }
986
987  done:
988         if (fnum1 != -1)
989                 smbcli_close(cli->tree, fnum1);
990         smbcli_unlink(cli->tree, fname);
991         smbcli_deltree(cli->tree, BASEDIR);
992
993         return ret;
994 }
995
996 static bool test_delayed_write_update5(struct torture_context *tctx,
997                                        struct smbcli_state *cli,
998                                        struct smbcli_state *cli2)
999 {
1000         union smb_fileinfo finfo0, finfo1, finfo2, finfo3, finfo4, finfo5;
1001         union smb_fileinfo pinfo0, pinfo1, pinfo2, pinfo3, pinfo4, pinfo5, pinfo6;
1002         const char *fname = BASEDIR "\\torture_file.txt";
1003         int fnum1 = -1;
1004         bool ret = true;
1005         ssize_t written;
1006         time_t t;
1007
1008         if (!torture_setup_dir(cli, BASEDIR)) {
1009                 return false;
1010         }
1011
1012         torture_comment(tctx, "Open the file handle\n");
1013         fnum1 = smbcli_open(cli->tree, fname, O_RDWR|O_CREAT, DENY_NONE);
1014         if (fnum1 == -1) {
1015                 ret = false;
1016                 torture_result(tctx, TORTURE_FAIL, __location__": unable to open %s", fname);
1017                 goto done;
1018         }
1019
1020         finfo0.basic_info.level = RAW_FILEINFO_BASIC_INFO;
1021         finfo0.basic_info.in.file.fnum = fnum1;
1022         finfo1 = finfo0;
1023         finfo2 = finfo0;
1024         finfo3 = finfo0;
1025         finfo4 = finfo0;
1026         finfo5 = finfo0;
1027         pinfo0.basic_info.level = RAW_FILEINFO_BASIC_INFO;
1028         pinfo0.basic_info.in.file.path = fname;
1029         pinfo1 = pinfo0;
1030         pinfo2 = pinfo0;
1031         pinfo3 = pinfo0;
1032         pinfo4 = pinfo0;
1033         pinfo5 = pinfo0;
1034         pinfo6 = pinfo0;
1035
1036         /* get the initial times */
1037         GET_INFO_BOTH(finfo0,pinfo0);
1038
1039         /* do a write */
1040         torture_comment(tctx, "Do a write on the file handle\n");
1041         written = smbcli_write(cli->tree, fnum1, 0, "x", 0, 1);
1042         if (written != 1) {
1043                 torture_result(tctx, TORTURE_FAIL, __location__": written gave %d - should have been 1", (int)written);
1044                 ret = false;
1045                 goto done;
1046         }
1047
1048         GET_INFO_BOTH(finfo1,pinfo1);
1049         COMPARE_WRITE_TIME_EQUAL(finfo1, finfo0);
1050
1051         torture_comment(tctx, "Set write time in the future on the file handle\n");
1052         SET_INFO_FILE(finfo0, time(NULL) + 86400);
1053         GET_INFO_BOTH(finfo2,pinfo2);
1054         COMPARE_WRITE_TIME_GREATER(finfo2, finfo1);
1055
1056         torture_comment(tctx, "Set write time in the past on the file handle\n");
1057         SET_INFO_FILE(finfo0, time(NULL) - 86400);
1058         GET_INFO_BOTH(finfo2,pinfo2);
1059         COMPARE_WRITE_TIME_LESS(finfo2, finfo1);
1060
1061         /* make sure the 2 second delay from the first write are canceled */
1062         t = time(NULL);
1063         while (time(NULL) < t+15) {
1064
1065                 /* get the times after the first write */
1066                 GET_INFO_BOTH(finfo3,pinfo3);
1067
1068                 if (finfo3.basic_info.out.write_time > finfo2.basic_info.out.write_time) {
1069                         torture_comment(tctx, "Server updated write_time after %d seconds (wrong!)\n",
1070                                (int)(time(NULL) - t));
1071                         ret = false;
1072                         break;
1073                 }
1074                 msleep(2000);
1075         }
1076
1077         GET_INFO_BOTH(finfo3,pinfo3);
1078         COMPARE_WRITE_TIME_EQUAL(finfo3, finfo2);
1079         if (finfo3.basic_info.out.write_time == finfo3.basic_info.out.write_time) {
1080                 torture_comment(tctx, "Server did not update write_time (correct)\n");
1081         }
1082
1083         /* sure any further write doesn't update the write time */
1084         t = time(NULL);
1085         while (time(NULL) < t+15) {
1086                 /* do a write */
1087                 torture_comment(tctx, "Do a write on the file handle\n");
1088                 written = smbcli_write(cli->tree, fnum1, 0, "x", 0, 1);
1089                 if (written != 1) {
1090                         torture_result(tctx, TORTURE_FAIL, __location__": written gave %d - should have been 1", (int)written);
1091                         ret = false;
1092                         goto done;
1093                 }
1094                 /* get the times after the write */
1095                 GET_INFO_BOTH(finfo4,pinfo4);
1096
1097                 if (finfo4.basic_info.out.write_time > finfo3.basic_info.out.write_time) {
1098                         torture_comment(tctx, "Server updated write_time after %d seconds (wrong!)\n",
1099                                (int)(time(NULL) - t));
1100                         ret = false;
1101                         break;
1102                 }
1103                 msleep(2000);
1104         }
1105
1106         GET_INFO_BOTH(finfo4,pinfo4);
1107         COMPARE_WRITE_TIME_EQUAL(finfo4, finfo3);
1108         if (finfo4.basic_info.out.write_time == finfo3.basic_info.out.write_time) {
1109                 torture_comment(tctx, "Server did not update write_time (correct)\n");
1110         }
1111
1112         /* sleep */
1113         msleep(5000);
1114
1115         GET_INFO_BOTH(finfo5,pinfo5);
1116         COMPARE_WRITE_TIME_EQUAL(finfo5, finfo4);
1117
1118         /*
1119          * the close doesn't update the write time
1120          */
1121         torture_comment(tctx, "Close the file handle\n");
1122         smbcli_close(cli->tree, fnum1);
1123         fnum1 = -1;
1124
1125         GET_INFO_PATH(pinfo6);
1126         COMPARE_WRITE_TIME_EQUAL(pinfo6, pinfo5);
1127
1128         if (pinfo6.basic_info.out.write_time == pinfo5.basic_info.out.write_time) {
1129                 torture_comment(tctx, "Server did not update the write_time on close (correct)\n");
1130         }
1131
1132  done:
1133         if (fnum1 != -1)
1134                 smbcli_close(cli->tree, fnum1);
1135         smbcli_unlink(cli->tree, fname);
1136         smbcli_deltree(cli->tree, BASEDIR);
1137
1138         return ret;
1139 }
1140
1141 static bool test_delayed_write_update6(struct torture_context *tctx,
1142                                        struct smbcli_state *cli,
1143                                        struct smbcli_state *cli2)
1144 {
1145         union smb_fileinfo finfo0, finfo1, finfo2, finfo3, finfo4, finfo5;
1146         union smb_fileinfo pinfo0, pinfo1, pinfo2, pinfo3, pinfo4, pinfo5, pinfo6, pinfo7;
1147         const char *fname = BASEDIR "\\torture_file.txt";
1148         int fnum1 = -1;
1149         int fnum2 = -1;
1150         bool ret = true;
1151         ssize_t written;
1152         time_t t;
1153         bool first = true;
1154
1155         if (!torture_setup_dir(cli, BASEDIR)) {
1156                 return false;
1157         }
1158 again:
1159         torture_comment(tctx, "Open the file handle\n");
1160         fnum1 = smbcli_open(cli->tree, fname, O_RDWR|O_CREAT, DENY_NONE);
1161         if (fnum1 == -1) {
1162                 ret = false;
1163                 torture_result(tctx, TORTURE_FAIL, __location__": unable to open %s", fname);
1164                 goto done;
1165         }
1166
1167         if (fnum2 == -1) {
1168                 torture_comment(tctx, "Open the 2nd file handle on 2nd connection\n");
1169                 fnum2 = smbcli_open(cli2->tree, fname, O_RDWR|O_CREAT, DENY_NONE);
1170                 if (fnum2 == -1) {
1171                         ret = false;
1172                         torture_result(tctx, TORTURE_FAIL, __location__": unable to open %s", fname);
1173                         goto done;
1174                 }
1175         }
1176
1177         finfo0.basic_info.level = RAW_FILEINFO_BASIC_INFO;
1178         finfo0.basic_info.in.file.fnum = fnum1;
1179         finfo1 = finfo0;
1180         finfo2 = finfo0;
1181         finfo3 = finfo0;
1182         finfo4 = finfo0;
1183         finfo5 = finfo0;
1184         pinfo0.basic_info.level = RAW_FILEINFO_BASIC_INFO;
1185         pinfo0.basic_info.in.file.path = fname;
1186         pinfo1 = pinfo0;
1187         pinfo2 = pinfo0;
1188         pinfo3 = pinfo0;
1189         pinfo4 = pinfo0;
1190         pinfo5 = pinfo0;
1191         pinfo6 = pinfo0;
1192         pinfo7 = pinfo0;
1193
1194         /* get the initial times */
1195         GET_INFO_BOTH(finfo0,pinfo0);
1196
1197         /* do a write */
1198         torture_comment(tctx, "Do a write on the file handle\n");
1199         written = smbcli_write(cli->tree, fnum1, 0, "x", 0, 1);
1200         if (written != 1) {
1201                 torture_result(tctx, TORTURE_FAIL, __location__": written gave %d - should have been 1", (int)written);
1202                 ret = false;
1203                 goto done;
1204         }
1205
1206         GET_INFO_BOTH(finfo1,pinfo1);
1207         COMPARE_WRITE_TIME_EQUAL(finfo1, finfo0);
1208
1209         torture_comment(tctx, "Set write time in the future on the 2nd file handle\n");
1210         SET_INFO_FILE_EX(finfo0, time(NULL) + 86400, cli2->tree, fnum2);
1211         GET_INFO_BOTH(finfo2,pinfo2);
1212         COMPARE_WRITE_TIME_GREATER(finfo2, finfo1);
1213
1214         torture_comment(tctx, "Set write time in the past on the 2nd file handle\n");
1215         SET_INFO_FILE_EX(finfo0, time(NULL) - 86400, cli2->tree, fnum2);
1216         GET_INFO_BOTH(finfo2,pinfo2);
1217         COMPARE_WRITE_TIME_LESS(finfo2, finfo1);
1218
1219         /* make sure the 2 second delay from the first write are canceled */
1220         t = time(NULL);
1221         while (time(NULL) < t+15) {
1222
1223                 /* get the times after the first write */
1224                 GET_INFO_BOTH(finfo3,pinfo3);
1225
1226                 if (finfo3.basic_info.out.write_time > finfo2.basic_info.out.write_time) {
1227                         torture_comment(tctx, "Server updated write_time after %d seconds (wrong!)\n",
1228                                (int)(time(NULL) - t));
1229                         ret = false;
1230                         break;
1231                 }
1232                 msleep(2000);
1233         }
1234
1235         GET_INFO_BOTH(finfo3,pinfo3);
1236         COMPARE_WRITE_TIME_EQUAL(finfo3, finfo2);
1237         if (finfo3.basic_info.out.write_time == finfo3.basic_info.out.write_time) {
1238                 torture_comment(tctx, "Server did not update write_time (correct)\n");
1239         }
1240
1241         /* sure any further write doesn't update the write time */
1242         t = time(NULL);
1243         while (time(NULL) < t+15) {
1244                 /* do a write */
1245                 torture_comment(tctx, "Do a write on the file handle\n");
1246                 written = smbcli_write(cli->tree, fnum1, 0, "x", 0, 1);
1247                 if (written != 1) {
1248                         torture_result(tctx, TORTURE_FAIL, __location__": written gave %d - should have been 1", (int)written);
1249                         ret = false;
1250                         goto done;
1251                 }
1252                 /* get the times after the write */
1253                 GET_INFO_BOTH(finfo4,pinfo4);
1254
1255                 if (finfo4.basic_info.out.write_time > finfo3.basic_info.out.write_time) {
1256                         torture_comment(tctx, "Server updated write_time after %d seconds (wrong!)\n",
1257                                (int)(time(NULL) - t));
1258                         ret = false;
1259                         break;
1260                 }
1261                 msleep(2000);
1262         }
1263
1264         GET_INFO_BOTH(finfo4,pinfo4);
1265         COMPARE_WRITE_TIME_EQUAL(finfo4, finfo3);
1266         if (finfo4.basic_info.out.write_time == finfo3.basic_info.out.write_time) {
1267                 torture_comment(tctx, "Server did not update write_time (correct)\n");
1268         }
1269
1270         /* sleep */
1271         msleep(5000);
1272
1273         GET_INFO_BOTH(finfo5,pinfo5);
1274         COMPARE_WRITE_TIME_EQUAL(finfo5, finfo4);
1275
1276         /*
1277          * the close updates the write time to the time of the close
1278          * as the write time was set on the 2nd handle
1279          */
1280         torture_comment(tctx, "Close the file handle\n");
1281         smbcli_close(cli->tree, fnum1);
1282         fnum1 = -1;
1283
1284         GET_INFO_PATH(pinfo6);
1285         COMPARE_WRITE_TIME_GREATER(pinfo6, pinfo5);
1286
1287         if (pinfo6.basic_info.out.write_time > pinfo5.basic_info.out.write_time) {
1288                 torture_comment(tctx, "Server updated the write_time on close (correct)\n");
1289         }
1290
1291         /* keep the 2nd handle open and rerun tests */
1292         if (first) {
1293                 first = false;
1294                 goto again;
1295         }
1296
1297         /*
1298          * closing the 2nd handle will cause no write time update
1299          * as the write time was explicit set on this handle
1300          */
1301         torture_comment(tctx, "Close the 2nd file handle\n");
1302         smbcli_close(cli2->tree, fnum2);
1303         fnum2 = -1;
1304
1305         GET_INFO_PATH(pinfo7);
1306         COMPARE_WRITE_TIME_EQUAL(pinfo7, pinfo6);
1307
1308         if (pinfo7.basic_info.out.write_time == pinfo6.basic_info.out.write_time) {
1309                 torture_comment(tctx, "Server did not update the write_time on close (correct)\n");
1310         }
1311
1312  done:
1313         if (fnum1 != -1)
1314                 smbcli_close(cli->tree, fnum1);
1315         if (fnum2 != -1)
1316                 smbcli_close(cli2->tree, fnum2);
1317         smbcli_unlink(cli->tree, fname);
1318         smbcli_deltree(cli->tree, BASEDIR);
1319
1320         return ret;
1321 }
1322
1323
1324 /* 
1325    testing of delayed update of write_time
1326 */
1327 struct torture_suite *torture_delay_write(void)
1328 {
1329         struct torture_suite *suite = torture_suite_create(talloc_autofree_context(), "DELAYWRITE");
1330
1331         torture_suite_add_2smb_test(suite, "finfo update on close", test_finfo_after_write);
1332         torture_suite_add_1smb_test(suite, "delayed update of write time", test_delayed_write_update);
1333         torture_suite_add_2smb_test(suite, "delayed update of write time using 2 connections", test_delayed_write_update2);
1334         torture_suite_add_2smb_test(suite, "delayed update of write time 3", test_delayed_write_update3);
1335         torture_suite_add_2smb_test(suite, "delayed update of write time 4", test_delayed_write_update4);
1336         torture_suite_add_2smb_test(suite, "delayed update of write time 5", test_delayed_write_update5);
1337         torture_suite_add_2smb_test(suite, "delayed update of write time 6", test_delayed_write_update6);
1338
1339         return suite;
1340 }