summaryrefslogtreecommitdiffstats
path: root/source4/torture/smb2/timestamps.c
blob: 3d6d3d1a2fdd5658408ab587192f8e30511c1ecd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
/*
   Unix SMB/CIFS implementation.

   test timestamps

   Copyright (C) Ralph Boehme 2019

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 3 of the License, or
   (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

#include "includes.h"
#include "libcli/smb2/smb2.h"
#include "libcli/smb2/smb2_calls.h"
#include "torture/torture.h"
#include "torture/util.h"
#include "torture/smb2/proto.h"

#define BASEDIR "smb2-timestamps"
#define FNAME "testfile.dat"

static bool test_close_no_attrib(struct torture_context *tctx,
				 struct smb2_tree *tree)
{
	const char *filename = BASEDIR "/" FNAME;
	struct smb2_create cr;
	struct smb2_handle handle = {{0}};
	struct smb2_handle testdirh = {{0}};
	struct smb2_close c;
	NTSTATUS status;
	bool ret = true;

	smb2_deltree(tree, BASEDIR);

	status = torture_smb2_testdir(tree, BASEDIR, &testdirh);
	torture_assert_ntstatus_ok_goto(tctx, status, ret, done,
					"torture_smb2_testdir failed\n");
	smb2_util_close(tree, testdirh);

	cr = (struct smb2_create) {
		.in.desired_access = SEC_FLAG_MAXIMUM_ALLOWED,
		.in.file_attributes = FILE_ATTRIBUTE_NORMAL,
		.in.share_access = NTCREATEX_SHARE_ACCESS_MASK,
		.in.create_disposition = NTCREATEX_DISP_OPEN_IF,
		.in.impersonation_level = NTCREATEX_IMPERSONATION_ANONYMOUS,
		.in.fname = filename,
	};

	status = smb2_create(tree, tctx, &cr);
	torture_assert_ntstatus_ok_goto(tctx, status, ret, done,
					"smb2_create failed\n");
	handle = cr.out.file.handle;

	c = (struct smb2_close) {
		.in.file.handle = handle,
	};

	status = smb2_close(tree, &c);
	torture_assert_ntstatus_ok_goto(tctx, status, ret, done,
					"close failed\n");
	ZERO_STRUCT(handle);

	torture_assert_u64_equal_goto(tctx, c.out.create_time, NTTIME_OMIT,
				      ret, done, "Unexpected create time\n");
	torture_assert_u64_equal_goto(tctx, c.out.access_time, NTTIME_OMIT,
				      ret, done, "Unexpected access time\n");
	torture_assert_u64_equal_goto(tctx, c.out.write_time, NTTIME_OMIT,
				      ret, done, "Unexpected write time\n");
	torture_assert_u64_equal_goto(tctx, c.out.change_time, NTTIME_OMIT,
				      ret, done, "Unexpected change time\n");
	torture_assert_u64_equal_goto(tctx, c.out.size, 0,
				      ret, done, "Unexpected size\n");
	torture_assert_u64_equal_goto(tctx, c.out.file_attr, 0,
				      ret, done, "Unexpected attributes\n");

done:
	if (!smb2_util_handle_empty(handle)) {
		smb2_util_close(tree, handle);
	}
	smb2_deltree(tree, BASEDIR);
	return ret;
}

static bool test_time_t(struct torture_context *tctx,
			struct smb2_tree *tree,
			const char *fname,
			time_t t)
{
	char *filename = NULL;
	struct smb2_create cr;
	struct smb2_handle handle = {{0}};
	struct smb2_handle testdirh = {{0}};
	struct timespec ts = { .tv_sec = t };
	uint64_t nttime;
	union smb_fileinfo gi;
	union smb_setfileinfo si;
	struct smb2_find find;
	unsigned int count;
	union smb_search_data *d;
	NTSTATUS status;
	bool ret = true;

	smb2_deltree(tree, BASEDIR);

	status = torture_smb2_testdir(tree, BASEDIR, &testdirh);
	torture_assert_ntstatus_ok_goto(tctx, status, ret, done,
					"torture_smb2_testdir failed\n");

	filename = talloc_asprintf(tctx, "%s\\%s", BASEDIR, fname);
	torture_assert_not_null_goto(tctx, filename, ret, done,
				     "talloc_asprintf failed\n");

	cr = (struct smb2_create) {
		.in.desired_access = SEC_FLAG_MAXIMUM_ALLOWED,
		.in.file_attributes = FILE_ATTRIBUTE_NORMAL,
		.in.share_access = NTCREATEX_SHARE_ACCESS_MASK,
		.in.create_disposition = NTCREATEX_DISP_OPEN_IF,
		.in.impersonation_level = NTCREATEX_IMPERSONATION_ANONYMOUS,
		.in.fname = filename,
	};

	status = smb2_create(tree, tctx, &cr);
	torture_assert_ntstatus_ok_goto(tctx, status, ret, done,
					"smb2_create failed\n");
	handle = cr.out.file.handle;

	si = (union smb_setfileinfo) {
		.basic_info.level = RAW_SFILEINFO_BASIC_INFORMATION,
		.basic_info.in.file.handle = handle,
	};

	nttime = full_timespec_to_nt_time(&ts);
	si.basic_info.in.create_time = nttime;
	si.basic_info.in.write_time = nttime;
	si.basic_info.in.change_time = nttime;

	status = smb2_setinfo_file(tree, &si);
	torture_assert_ntstatus_ok_goto(tctx, status, ret, done,
					"smb2_setinfo_file failed\n");

	gi = (union smb_fileinfo) {
		.generic.level = SMB_QFILEINFO_BASIC_INFORMATION,
		.generic.in.file.handle = handle,
	};

	status = smb2_getinfo_file(tree, tctx, &gi);
	torture_assert_ntstatus_ok_goto(tctx, status, ret, done,
					"smb2_getinfo_file failed\n");

	torture_comment(tctx, "Got: create: %s, write: %s, change: %s\n",
			nt_time_string(tctx, gi.basic_info.out.create_time),
			nt_time_string(tctx, gi.basic_info.out.write_time),
			nt_time_string(tctx, gi.basic_info.out.change_time));

	torture_assert_u64_equal_goto(tctx,
				      nttime,
				      gi.basic_info.out.create_time,
				      ret, done,
				      "Wrong create time\n");
	torture_assert_u64_equal_goto(tctx,
				      nttime,
				      gi.basic_info.out.write_time,
				      ret, done,
				      "Wrong write time\n");
	torture_assert_u64_equal_goto(tctx,
				      nttime,
				      gi.basic_info.out.change_time,
				      ret, done,
				      "Wrong change time\n");

	find = (struct smb2_find) {
		.in.file.handle = testdirh,
		.in.pattern = fname,
		.in.max_response_size = 0x1000,
		.in.level = SMB2_FIND_ID_BOTH_DIRECTORY_INFO,
	};

	status = smb2_find_level(tree, tree, &find, &count, &d);
	torture_assert_ntstatus_ok_goto(tctx, status, ret, done,
					"smb2_find_level failed\n");

	torture_assert_u64_equal_goto(tctx,
				      nttime,
				      d[0].id_both_directory_info.create_time,
				      ret, done,
				      "Wrong create time\n");
	torture_assert_u64_equal_goto(tctx,
				      nttime,
				      d[0].id_both_directory_info.write_time,
				      ret, done,
				      "Wrong write time\n");
	torture_assert_u64_equal_goto(tctx,
				      nttime,
				      d[0].id_both_directory_info.change_time,
				      ret, done,
				      "Wrong change time\n");

	status = smb2_util_close(tree, handle);
	torture_assert_ntstatus_ok_goto(tctx, status, ret, done,
					"smb2_util_close failed\n");
	ZERO_STRUCT(handle);

	cr = (struct smb2_create) {
		.in.desired_access = SEC_FLAG_MAXIMUM_ALLOWED,
		.in.file_attributes = FILE_ATTRIBUTE_NORMAL,
		.in.share_access = NTCREATEX_SHARE_ACCESS_MASK,
		.in.create_disposition = NTCREATEX_DISP_OPEN,
		.in.impersonation_level = NTCREATEX_IMPERSONATION_ANONYMOUS,
		.in.fname = filename,
	};

	status = smb2_create(tree, tctx, &cr);
	torture_assert_ntstatus_ok_goto(tctx, status, ret, done,
					"smb2_create failed\n");
	handle = cr.out.file.handle;

	gi = (union smb_fileinfo) {
		.generic.level = SMB_QFILEINFO_BASIC_INFORMATION,
		.generic.in.file.handle = handle,
	};

	status = smb2_getinfo_file(tree, tctx, &gi);
	torture_assert_ntstatus_ok_goto(tctx, status, ret, done,
					"smb2_getinfo_file failed\n");

	torture_comment(tctx, "Got: create: %s, write: %s, change: %s\n",
			nt_time_string(tctx, gi.basic_info.out.create_time),
			nt_time_string(tctx, gi.basic_info.out.write_time),
			nt_time_string(tctx, gi.basic_info.out.change_time));

	torture_assert_u64_equal_goto(tctx,
				      nttime,
				      gi.basic_info.out.create_time,
				      ret, done,
				      "Wrong create time\n");
	torture_assert_u64_equal_goto(tctx,
				      nttime,
				      gi.basic_info.out.write_time,
				      ret, done,
				      "Wrong write time\n");
	torture_assert_u64_equal_goto(tctx,
				      nttime,
				      gi.basic_info.out.change_time,
				      ret, done,
				      "Wrong change time\n");

	find = (struct smb2_find) {
		.in.continue_flags = SMB2_CONTINUE_FLAG_RESTART,
		.in.file.handle = testdirh,
		.in.pattern = fname,
		.in.max_response_size = 0x1000,
		.in.level = SMB2_FIND_ID_BOTH_DIRECTORY_INFO,
	};

	status = smb2_find_level(tree, tree, &find, &count, &d);
	torture_assert_ntstatus_ok_goto(tctx, status, ret, done,
					"smb2_find_level failed\n");

	torture_assert_u64_equal_goto(tctx,
				      nttime,
				      d[0].id_both_directory_info.create_time,
				      ret, done,
				      "Wrong create time\n");
	torture_assert_u64_equal_goto(tctx,
				      nttime,
				      d[0].id_both_directory_info.write_time,
				      ret, done,
				      "Wrong write time\n");
	torture_assert_u64_equal_goto(tctx,
				      nttime,
				      d[0].id_both_directory_info.change_time,
				      ret, done,
				      "Wrong change time\n");

	status = smb2_util_close(tree, handle);
	torture_assert_ntstatus_ok_goto(tctx, status, ret, done,
					"smb2_util_close failed\n");
	ZERO_STRUCT(handle);

done:
	if (!smb2_util_handle_empty(handle)) {
		smb2_util_close(tree, handle);
	}
	if (!smb2_util_handle_empty(testdirh)) {
		smb2_util_close(tree, testdirh);
	}
	smb2_deltree(tree, BASEDIR);
	return ret;
}

static bool test_time_t_15032385535(struct torture_context *tctx,
				    struct smb2_tree *tree)
{
	return test_time_t(tctx, tree, "test_time_t_15032385535.txt",
			   15032385535 /* >> INT32_MAX, limit on ext */);
}

static bool test_time_t_10000000000(struct torture_context *tctx,
				    struct smb2_tree *tree)
{
	return test_time_t(tctx, tree, "test_time_t_10000000000.txt",
			   10000000000 /* >> INT32_MAX */);
}

static bool test_time_t_4294967295(struct torture_context *tctx,
				   struct smb2_tree *tree)
{
	return test_time_t(tctx, tree, "test_time_t_4294967295.txt",
			   4294967295 /* INT32_MAX */);
}

static bool test_time_t_1(struct torture_context *tctx,
			  struct smb2_tree *tree)
{
	return test_time_t(tctx, tree, "test_time_t_1.txt", 1);
}

static bool test_time_t_0(struct torture_context *tctx,
			  struct smb2_tree *tree)
{
	return test_time_t(tctx, tree, "test_time_t_0.txt", 0);
}

static bool test_time_t_minus_1(struct torture_context *tctx,
				struct smb2_tree *tree)
{
	return test_time_t(tctx, tree, "test_time_t_-1.txt", -1);
}

static bool test_time_t_minus_2(struct torture_context *tctx,
				struct smb2_tree *tree)
{
	return test_time_t(tctx, tree, "test_time_t_-2.txt", -2);
}

static bool test_time_t_1968(struct torture_context *tctx,
			     struct smb2_tree *tree)
{
	return test_time_t(tctx, tree, "test_time_t_1968.txt",
			   -63158400 /* 1968 */);
}

static bool test_freeze_thaw(struct torture_context *tctx,
			     struct smb2_tree *tree)
{
	const char *filename = BASEDIR "\\test_freeze_thaw";
	struct smb2_create cr;
	struct smb2_handle handle = {{0}};
	struct smb2_handle testdirh = {{0}};
	struct timespec ts = { .tv_sec = time(NULL) };
	uint64_t nttime;
	union smb_fileinfo gi;
	union smb_setfileinfo si;
	NTSTATUS status;
	bool ret = true;

	smb2_deltree(tree, BASEDIR);

	status = torture_smb2_testdir(tree, BASEDIR, &testdirh);
	torture_assert_ntstatus_ok_goto(tctx, status, ret, done,
					"torture_smb2_testdir failed\n");

	cr = (struct smb2_create) {
		.in.desired_access = SEC_FLAG_MAXIMUM_ALLOWED,
		.in.file_attributes = FILE_ATTRIBUTE_NORMAL,
		.in.share_access = NTCREATEX_SHARE_ACCESS_MASK,
		.in.create_disposition = NTCREATEX_DISP_OPEN_IF,
		.in.impersonation_level = NTCREATEX_IMPERSONATION_ANONYMOUS,
		.in.fname = filename,
	};

	status = smb2_create(tree, tctx, &cr);
	torture_assert_ntstatus_ok_goto(tctx, status, ret, done,
					"smb2_create failed\n");
	handle = cr.out.file.handle;

	si = (union smb_setfileinfo) {
		.basic_info.level = RAW_SFILEINFO_BASIC_INFORMATION,
		.basic_info.in.file.handle = handle,
	};

	/*
	 * Step 1:
	 * First set timestamps of testfile to current time
	 */

	nttime = full_timespec_to_nt_time(&ts);
	si.basic_info.in.create_time = nttime;
	si.basic_info.in.write_time = nttime;
	si.basic_info.in.change_time = nttime;

	status = smb2_setinfo_file(tree, &si);
	torture_assert_ntstatus_ok_goto(tctx, status, ret, done,
					"smb2_setinfo_file failed\n");

	gi = (union smb_fileinfo) {
		.generic.level = SMB_QFILEINFO_BASIC_INFORMATION,
		.generic.in.file.handle = handle,
	};

	/*
	 * Step 2:
	 * Verify timestamps are indeed set to the value in "nttime".
	 */

	status = smb2_getinfo_file(tree, tctx, &gi);
	torture_assert_ntstatus_ok_goto(tctx, status, ret, done,
					"smb2_getinfo_file failed\n");

	torture_comment(tctx, "Got: create: %s, write: %s, change: %s\n",
			nt_time_string(tctx, gi.basic_info.out.create_time),
			nt_time_string(tctx, gi.basic_info.out.write_time),
			nt_time_string(tctx, gi.basic_info.out.change_time));

	torture_assert_u64_equal_goto(tctx,
				      nttime,
				      gi.basic_info.out.create_time,
				      ret, done,
				      "Wrong create time\n");
	torture_assert_u64_equal_goto(tctx,
				      nttime,
				      gi.basic_info.out.write_time,
				      ret, done,
				      "Wrong write time\n");
	torture_assert_u64_equal_goto(tctx,
				      nttime,
				      gi.basic_info.out.change_time,
				      ret, done,
				      "Wrong change time\n");

	/*
	 * Step 3:
	 * First set timestamps with NTTIME_FREEZE, must not change any
	 * timestamp value.
	 */

	si.basic_info.in.create_time = NTTIME_FREEZE;
	si.basic_info.in.write_time = NTTIME_FREEZE;
	si.basic_info.in.change_time = NTTIME_FREEZE;

	status = smb2_setinfo_file(tree, &si);
	torture_assert_ntstatus_ok_goto(tctx, status, ret, done,
					"smb2_setinfo_file failed\n");

	gi = (union smb_fileinfo) {
		.generic.level = SMB_QFILEINFO_BASIC_INFORMATION,
		.generic.in.file.handle = handle,
	};

	/*
	 * Step 4:
	 * Verify timestamps are unmodified from step 2.
	 */

	gi = (union smb_fileinfo) {
		.generic.level = SMB_QFILEINFO_BASIC_INFORMATION,
		.generic.in.file.handle = handle,
	};

	status = smb2_getinfo_file(tree, tctx, &gi);
	torture_assert_ntstatus_ok_goto(tctx, status, ret, done,
					"smb2_getinfo_file failed\n");

	torture_comment(tctx, "Got: create: %s, write: %s, change: %s\n",
			nt_time_string(tctx, gi.basic_info.out.create_time),
			nt_time_string(tctx, gi.basic_info.out.write_time),
			nt_time_string(tctx, gi.basic_info.out.change_time));

	torture_assert_u64_equal_goto(tctx,
				      nttime,
				      gi.basic_info.out.create_time,
				      ret, done,
				      "Wrong create time\n");
	torture_assert_u64_equal_goto(tctx,
				      nttime,
				      gi.basic_info.out.write_time,
				      ret, done,
				      "Wrong write time\n");
	torture_assert_u64_equal_goto(tctx,
				      nttime,
				      gi.basic_info.out.change_time,
				      ret, done,
				      "Wrong change time\n");

	/*
	 * Step 5:
	 * First set timestamps with NTTIME_THAW, must not change any timestamp
	 * value.
	 */

	si.basic_info.in.create_time = NTTIME_THAW;
	si.basic_info.in.write_time = NTTIME_THAW;
	si.basic_info.in.change_time = NTTIME_THAW;

	status = smb2_setinfo_file(tree, &si);
	torture_assert_ntstatus_ok_goto(tctx, status, ret, done,
					"smb2_setinfo_file failed\n");

	gi = (union smb_fileinfo) {
		.generic.level = SMB_QFILEINFO_BASIC_INFORMATION,
		.generic.in.file.handle = handle,
	};

	/*
	 * Step 6:
	 * Verify timestamps are unmodified from step 2.
	 */

	gi = (union smb_fileinfo) {
		.generic.level = SMB_QFILEINFO_BASIC_INFORMATION,
		.generic.in.file.handle = handle,
	};

	status = smb2_getinfo_file(tree, tctx, &gi);
	torture_assert_ntstatus_ok_goto(tctx, status, ret, done,
					"smb2_getinfo_file failed\n");

	torture_comment(tctx, "Got: create: %s, write: %s, change: %s\n",
			nt_time_string(tctx, gi.basic_info.out.create_time),
			nt_time_string(tctx, gi.basic_info.out.write_time),
			nt_time_string(tctx, gi.basic_info.out.change_time));

	torture_assert_u64_equal_goto(tctx,
				      nttime,
				      gi.basic_info.out.create_time,
				      ret, done,
				      "Wrong create time\n");
	torture_assert_u64_equal_goto(tctx,
				      nttime,
				      gi.basic_info.out.write_time,
				      ret, done,
				      "Wrong write time\n");
	torture_assert_u64_equal_goto(tctx,
				      nttime,
				      gi.basic_info.out.change_time,
				      ret, done,
				      "Wrong change time\n");

done:
	if (!smb2_util_handle_empty(handle)) {
		smb2_util_close(tree, handle);
	}
	if (!smb2_util_handle_empty(testdirh)) {
		smb2_util_close(tree, testdirh);
	}
	smb2_deltree(tree, BASEDIR);
	return ret;
}

static bool test_delayed_write_vs_seteof(struct torture_context *tctx,
					 struct smb2_tree *tree)
{
	struct smb2_create cr;
	struct smb2_handle h1 = {{0}};
	struct smb2_handle h2 = {{0}};
	NTTIME create_time;
	NTTIME set_time;
	union smb_fileinfo finfo;
	union smb_setfileinfo setinfo;
	struct smb2_close c;
	NTSTATUS status;
	bool ret = true;

	smb2_deltree(tree, BASEDIR);
	status = torture_smb2_testdir(tree, BASEDIR, &h1);
	torture_assert_ntstatus_ok_goto(tctx, status, ret, done,
					"create failed\n");
	status = smb2_util_close(tree, h1);
	torture_assert_ntstatus_ok_goto(tctx, status, ret, done,
					"close failed\n");

	torture_comment(tctx, "Open file-handle 1\n");

	cr = (struct smb2_create) {
		.in.desired_access     = SEC_FLAG_MAXIMUM_ALLOWED,
		.in.create_disposition = NTCREATEX_DISP_OPEN_IF,
		.in.share_access       = NTCREATEX_SHARE_ACCESS_MASK,
		.in.fname              = BASEDIR "\\" FNAME,
	};
	status = smb2_create(tree, tctx, &cr);
	torture_assert_ntstatus_ok_goto(tctx, status, ret, done,
					"create failed\n");
	h1 = cr.out.file.handle;
	create_time = cr.out.create_time;
	sleep(1);

	torture_comment(tctx, "Write to file-handle 1\n");

	status = smb2_util_write(tree, h1, "s", 0, 1);
	torture_assert_ntstatus_ok_goto(tctx, status, ret, done,
					"write failed\n");

	torture_comment(tctx, "Check writetime hasn't been updated\n");

	finfo = (union smb_fileinfo) {
		.generic.level = RAW_FILEINFO_SMB2_ALL_INFORMATION,
		.generic.in.file.handle = h1,
	};
	status = smb2_getinfo_file(tree, tree, &finfo);
	torture_assert_ntstatus_ok_goto(tctx, status, ret, done,
					"getinfo failed\n");

	torture_assert_nttime_equal(tctx,
				    finfo.all_info.out.write_time,
				    create_time,
				    "Writetime != set_time (wrong!)\n");

	torture_comment(tctx, "Setinfo EOF on file-handle 1,"
			" should flush pending writetime update\n");

	setinfo = (union smb_setfileinfo) {
		.generic.level = RAW_SFILEINFO_END_OF_FILE_INFORMATION,
	};
	setinfo.end_of_file_info.in.file.handle = h1;
	setinfo.end_of_file_info.in.size = 1; /* same size! */

	status = smb2_setinfo_file(tree, &setinfo);
	torture_assert_ntstatus_ok_goto(tctx, status, ret, done,
					"close failed\n");

	torture_comment(tctx, "Check writetime has been updated "
			"by the setinfo EOF\n");

	finfo = (union smb_fileinfo) {
		.generic.level = RAW_FILEINFO_SMB2_ALL_INFORMATION,
		.generic.in.file.handle = h1,
	};
	status = smb2_getinfo_file(tree, tree, &finfo);
	torture_assert_ntstatus_ok_goto(tctx, status, ret, done,
					"getinfo failed\n");
	if (!(finfo.all_info.out.write_time > create_time)) {
		ret = false;
		torture_fail_goto(tctx, done, "setinfo EOF hasn't updated writetime\n");
	}

	torture_comment(tctx, "Open file-handle 2\n");

	cr = (struct smb2_create) {
		.in.desired_access     = SEC_FILE_WRITE_ATTRIBUTE,
		.in.create_disposition = NTCREATEX_DISP_OPEN,
		.in.share_access       = NTCREATEX_SHARE_ACCESS_MASK,
		.in.fname              = BASEDIR "\\" FNAME,
	};
	status = smb2_create(tree, tctx, &cr);
	torture_assert_ntstatus_ok_goto(tctx, status, ret, done,
					"create failed\n");
	h2 = cr.out.file.handle;

	torture_comment(tctx, "Set write time on file-handle 2\n");

	setinfo = (union smb_setfileinfo) {
		.generic.level = SMB_QFILEINFO_BASIC_INFORMATION,
	};
	setinfo.generic.in.file.handle = h2;
	unix_to_nt_time(&set_time, time(NULL) + 86400);
	setinfo.basic_info.in.write_time = set_time;

	status = smb2_setinfo_file(tree, &setinfo);
	torture_assert_ntstatus_ok_goto(tctx, status, ret, done,
					"close failed\n");

	status = smb2_util_close(tree, h2);
	torture_assert_ntstatus_ok_goto(tctx, status, ret, done,
					"close failed\n");
	ZERO_STRUCT(h2);

	torture_comment(tctx, "Close file-handle 1, write-time should not be updated\n");

	c = (struct smb2_close) {
		.in.file.handle = h1,
		.in.flags = SMB2_CLOSE_FLAGS_FULL_INFORMATION,
	};

	status = smb2_close(tree, &c);
	torture_assert_ntstatus_ok_goto(tctx, status, ret, done,
					"close failed\n");
	ZERO_STRUCT(h1);

	torture_assert_nttime_equal(tctx,
				    c.out.write_time,
				    set_time,
				    "Writetime != set_time (wrong!)\n");

done:
	if (!smb2_util_handle_empty(h1)) {
		smb2_util_close(tree, h1);
	}
	if (!smb2_util_handle_empty(h2)) {
		smb2_util_close(tree, h2);
	}
	smb2_deltree(tree, BASEDIR);
	return ret;
}

static bool test_delayed_write_vs_flush(struct torture_context *tctx,
					struct smb2_tree *tree)
{
	struct smb2_create cr;
	struct smb2_handle h1 = {{0}};
	union smb_fileinfo finfo;
	struct smb2_flush f;
	struct smb2_close c;
	NTTIME create_time;
	NTTIME flush_time;
	NTSTATUS status;
	bool ret = true;

	smb2_deltree(tree, BASEDIR);
	status = torture_smb2_testdir(tree, BASEDIR, &h1);
	torture_assert_ntstatus_ok_goto(tctx, status, ret, done,
					"create failed\n");
	status = smb2_util_close(tree, h1);
	torture_assert_ntstatus_ok_goto(tctx, status, ret, done,
					"close failed\n");

	torture_comment(tctx, "Open file-handle 1\n");

	cr = (struct smb2_create) {
		.in.desired_access     = SEC_FLAG_MAXIMUM_ALLOWED,
		.in.create_disposition = NTCREATEX_DISP_OPEN_IF,
		.in.share_access       = NTCREATEX_SHARE_ACCESS_MASK,
		.in.fname              = BASEDIR "\\" FNAME,
	};
	status = smb2_create(tree, tctx, &cr);
	torture_assert_ntstatus_ok_goto(tctx, status, ret, done,
					"create failed\n");
	h1 = cr.out.file.handle;
	create_time = cr.out.create_time;
	sleep(1);

	torture_comment(tctx, "Write to file-handle 1\n");

	status = smb2_util_write(tree, h1, "s", 0, 1);
	torture_assert_ntstatus_ok_goto(tctx, status, ret, done,
					"write failed\n");

	torture_comment(tctx, "Check writetime hasn't been updated\n");

	finfo = (union smb_fileinfo) {
		.generic.level = RAW_FILEINFO_SMB2_ALL_INFORMATION,
		.generic.in.file.handle = h1,
	};
	status = smb2_getinfo_file(tree, tree, &finfo);
	torture_assert_ntstatus_ok_goto(tctx, status, ret, done,
					"getinfo failed\n");

	torture_assert_nttime_equal(tctx,
				    finfo.all_info.out.write_time,
				    create_time,
				    "Writetime != create_time (wrong!)\n");

	torture_comment(tctx, "Flush file, "
			"should flush pending writetime update\n");

	f = (struct smb2_flush) {
		.in.file.handle = h1,
	};

	status = smb2_flush(tree, &f);
	torture_assert_ntstatus_ok_goto(tctx, status, ret, done,
					"flush failed\n");

	torture_comment(tctx, "Check writetime has been updated "
			"by the setinfo EOF\n");

	finfo = (union smb_fileinfo) {
		.generic.level = RAW_FILEINFO_SMB2_ALL_INFORMATION,
		.generic.in.file.handle = h1,
	};
	status = smb2_getinfo_file(tree, tree, &finfo);
	torture_assert_ntstatus_ok_goto(tctx, status, ret, done,
					"getinfo failed\n");

	flush_time = finfo.all_info.out.write_time;
	if (!(flush_time > create_time)) {
		ret = false;
		torture_fail_goto(tctx, done, "flush hasn't updated writetime\n");
	}

	torture_comment(tctx, "Close file-handle 1, write-time should not be updated\n");

	c = (struct smb2_close) {
		.in.file.handle = h1,
		.in.flags = SMB2_CLOSE_FLAGS_FULL_INFORMATION,
	};

	status = smb2_close(tree, &c);
	torture_assert_ntstatus_ok_goto(tctx, status, ret, done,
					"close failed\n");
	ZERO_STRUCT(h1);

	torture_assert_nttime_equal(tctx,
				    c.out.write_time,
				    flush_time,
				    "writetime != flushtime (wrong!)\n");

done:
	if (!smb2_util_handle_empty(h1)) {
		smb2_util_close(tree, h1);
	}
	smb2_deltree(tree, BASEDIR);
	return ret;
}

static bool test_delayed_write_vs_setbasic_do(struct torture_context *tctx,
					      struct smb2_tree *tree,
					      union smb_setfileinfo *setinfo,
					      bool expect_update)
{
	char *path = NULL;
	struct smb2_create cr;
	struct smb2_handle h1 = {{0}};
	NTTIME create_time;
	union smb_fileinfo finfo;
	NTSTATUS status;
	bool ret = true;

	torture_comment(tctx, "Create testfile\n");

	path = talloc_asprintf(tree, BASEDIR "\\" FNAME ".%" PRIu32,
			       generate_random());
	torture_assert_not_null_goto(tctx, path, ret, done, "OOM\n");

	cr = (struct smb2_create) {
		.in.desired_access     = SEC_FLAG_MAXIMUM_ALLOWED,
		.in.create_disposition = NTCREATEX_DISP_CREATE,
		.in.share_access       = NTCREATEX_SHARE_ACCESS_MASK,
		.in.fname              = path,
	};
	status = smb2_create(tree, tctx, &cr);
	torture_assert_ntstatus_ok_goto(tctx, status, ret, done,
					"create failed\n");
	h1 = cr.out.file.handle;
	create_time = cr.out.create_time;

	torture_comment(tctx, "Write to file\n");

	status = smb2_util_write(tree, h1, "s", 0, 1);
	torture_assert_ntstatus_ok_goto(tctx, status, ret, done,
					"write failed\n");

	torture_comment(tctx, "Get timestamps\n");

	finfo = (union smb_fileinfo) {
		.generic.level = RAW_FILEINFO_SMB2_ALL_INFORMATION,
		.generic.in.file.handle = h1,
	};
	status = smb2_getinfo_file(tree, tree, &finfo);
	torture_assert_ntstatus_ok_goto(tctx, status, ret, done,
					"getinfo failed\n");

	torture_assert_nttime_equal(tctx,
				    finfo.all_info.out.write_time,
				    create_time,
				    "Writetime != create_time (wrong!)\n");

	torture_comment(tctx, "Set timestamps\n");

	setinfo->end_of_file_info.in.file.handle = h1;
	status = smb2_setinfo_file(tree, setinfo);
	torture_assert_ntstatus_ok_goto(tctx, status, ret, done,
					"close failed\n");

	torture_comment(tctx, "Check timestamps\n");

	finfo = (union smb_fileinfo) {
		.generic.level = RAW_FILEINFO_SMB2_ALL_INFORMATION,
		.generic.in.file.handle = h1,
	};
	status = smb2_getinfo_file(tree, tree, &finfo);
	torture_assert_ntstatus_ok_goto(tctx, status, ret, done,
					"getinfo failed\n");

	if (expect_update) {
		if (!(finfo.all_info.out.write_time > create_time)) {
			ret = false;
			torture_fail_goto(tctx, done, "setinfo basicinfo "
					  "hasn't updated writetime\n");
		}
	} else {
		if (finfo.all_info.out.write_time != create_time) {
			ret = false;
			torture_fail_goto(tctx, done, "setinfo basicinfo "
					  "hasn't updated writetime\n");
		}
	}

	status = smb2_util_close(tree, h1);
	torture_assert_ntstatus_ok_goto(tctx, status, ret, done,
					"close failed\n");
	ZERO_STRUCT(h1);

	status = smb2_util_unlink(tree, path);
	torture_assert_ntstatus_ok_goto(tctx, status, ret, done,
					"close failed\n");

done:
	TALLOC_FREE(path);
	if (!smb2_util_handle_empty(h1)) {
		smb2_util_close(tree, h1);
	}
	return ret;
}

static bool test_delayed_write_vs_setbasic(struct torture_context *tctx,
					   struct smb2_tree *tree)
{
	struct smb2_handle h1 = {{0}};
	union smb_setfileinfo setinfo;
	time_t t = time(NULL) - 86400;
	NTSTATUS status;
	bool ret = true;

	smb2_deltree(tree, BASEDIR);
	status = torture_smb2_testdir(tree, BASEDIR, &h1);
	torture_assert_ntstatus_ok_goto(tctx, status, ret, done,
					"create failed\n");
	status = smb2_util_close(tree, h1);
	torture_assert_ntstatus_ok_goto(tctx, status, ret, done,
					"close failed\n");

	/*
	 * Yes, this is correct, tested against Windows 2016: even if all
	 * timestamp fields are 0, a pending write time is flushed.
	 */
	torture_comment(tctx, "Test: setting all-0 timestamps flushes?\n");

	setinfo = (union smb_setfileinfo) {
		.generic.level = RAW_SFILEINFO_BASIC_INFORMATION,
	};
	ret = test_delayed_write_vs_setbasic_do(tctx, tree, &setinfo, true);
	if (ret != true) {
		goto done;
	}

	torture_comment(tctx, "Test: setting create_time flushes?\n");
	unix_to_nt_time(&setinfo.basic_info.in.create_time, t);
	ret = test_delayed_write_vs_setbasic_do(tctx, tree, &setinfo, true);
	if (ret != true) {
		goto done;
	}

	torture_comment(tctx, "Test: setting access_time flushes?\n");
	unix_to_nt_time(&setinfo.basic_info.in.access_time, t);
	ret = test_delayed_write_vs_setbasic_do(tctx, tree, &setinfo, true);
	if (ret != true) {
		goto done;
	}

	torture_comment(tctx, "Test: setting change_time flushes?\n");
	unix_to_nt_time(&setinfo.basic_info.in.change_time, t);
	ret = test_delayed_write_vs_setbasic_do(tctx, tree, &setinfo, true);
	if (ret != true) {
		goto done;
	}

done:
	smb2_deltree(tree, BASEDIR);
	return ret;
}

static bool test_delayed_1write(struct torture_context *tctx,
				struct smb2_tree *tree)
{
	struct smb2_create cr;
	struct smb2_handle h1 = {{0}};
	union smb_fileinfo finfo;
	struct smb2_close c;
	NTTIME create_time;
	NTTIME write_time;
	NTTIME close_time;
	NTSTATUS status;
	bool ret = true;

	smb2_deltree(tree, BASEDIR);
	status = torture_smb2_testdir(tree, BASEDIR, &h1);
	torture_assert_ntstatus_ok_goto(tctx, status, ret, done,
					"create failed\n");
	status = smb2_util_close(tree, h1);
	torture_assert_ntstatus_ok_goto(tctx, status, ret, done,
					"close failed\n");

	torture_comment(tctx, "Open file-handle 1\n");

	cr = (struct smb2_create) {
		.in.desired_access     = SEC_FLAG_MAXIMUM_ALLOWED,
		.in.create_disposition = NTCREATEX_DISP_OPEN_IF,
		.in.share_access       = NTCREATEX_SHARE_ACCESS_MASK,
		.in.fname              = BASEDIR "\\" FNAME,
	};
	status = smb2_create(tree, tctx, &cr);
	torture_assert_ntstatus_ok_goto(tctx, status, ret, done,
					"create failed\n");
	h1 = cr.out.file.handle;
	create_time = cr.out.create_time;
	sleep(1);

	torture_comment(tctx, "Write to file-handle 1\n");

	status = smb2_util_write(tree, h1, "s", 0, 1);
	torture_assert_ntstatus_ok_goto(tctx, status, ret, done,
					"write failed\n");
	sleep(3);

	torture_comment(tctx, "Check writetime has been updated\n");

	finfo = (union smb_fileinfo) {
		.generic.level = RAW_FILEINFO_SMB2_ALL_INFORMATION,
		.generic.in.file.handle = h1,
	};
	status = smb2_getinfo_file(tree, tree, &finfo);
	torture_assert_ntstatus_ok_goto(tctx, status, ret, done,
					"getinfo failed\n");
	write_time = finfo.all_info.out.write_time;

	if (!(write_time > create_time)) {
		ret = false;
		torture_fail_goto(tctx, done,
				  "Write-time not updated (wrong!)\n");
	}

	torture_comment(tctx, "Close file-handle 1\n");
	sleep(1);

	c = (struct smb2_close) {
		.in.file.handle = h1,
		.in.flags = SMB2_CLOSE_FLAGS_FULL_INFORMATION,
	};

	status = smb2_close(tree, &c);
	torture_assert_ntstatus_ok_goto(tctx, status, ret, done,
					"close failed\n");
	ZERO_STRUCT(h1);
	close_time = c.out.write_time;

	torture_assert_nttime_equal(tctx, close_time, write_time,
				    "Writetime != close_time (wrong!)\n");

done:
	if (!smb2_util_handle_empty(h1)) {
		smb2_util_close(tree, h1);
	}
	smb2_deltree(tree, BASEDIR);
	return ret;
}

static bool test_delayed_2write(struct torture_context *tctx,
				struct smb2_tree *tree)
{
	struct smb2_create cr;
	struct smb2_handle h1 = {{0}};
	union smb_fileinfo finfo;
	struct smb2_close c;
	NTTIME create_time;
	NTTIME write_time;
	NTTIME write_time2;
	NTTIME close_time;
	NTSTATUS status;
	bool ret = true;

	smb2_deltree(tree, BASEDIR);
	status = torture_smb2_testdir(tree, BASEDIR, &h1);
	torture_assert_ntstatus_ok_goto(tctx, status, ret, done,
					"create failed\n");
	status = smb2_util_close(tree, h1);
	torture_assert_ntstatus_ok_goto(tctx, status, ret, done,
					"close failed\n");

	torture_comment(tctx, "Open file\n");

	cr = (struct smb2_create) {
		.in.desired_access     = SEC_FLAG_MAXIMUM_ALLOWED,
		.in.create_disposition = NTCREATEX_DISP_OPEN_IF,
		.in.share_access       = NTCREATEX_SHARE_ACCESS_MASK,
		.in.fname              = BASEDIR "\\" FNAME,
	};
	status = smb2_create(tree, tctx, &cr);
	torture_assert_ntstatus_ok_goto(tctx, status, ret, done,
					"create failed\n");
	h1 = cr.out.file.handle;
	create_time = cr.out.create_time;
	sleep(1);

	torture_comment(tctx, "Write to file\n");

	status = smb2_util_write(tree, h1, "s", 0, 1);
	torture_assert_ntstatus_ok_goto(tctx, status, ret, done,
					"write failed\n");
	sleep(3);

	torture_comment(tctx, "Check writetime has been updated\n");

	finfo = (union smb_fileinfo) {
		.generic.level = RAW_FILEINFO_SMB2_ALL_INFORMATION,
		.generic.in.file.handle = h1,
	};
	status = smb2_getinfo_file(tree, tree, &finfo);
	torture_assert_ntstatus_ok_goto(tctx, status, ret, done,
					"getinfo failed\n");
	write_time = finfo.all_info.out.write_time;

	if (!(write_time > create_time)) {
		ret = false;
		torture_fail_goto(tctx, done,
				  "Write-time not updated (wrong!)\n");
	}

	torture_comment(tctx, "Write a second time\n");

	status = smb2_util_write(tree, h1, "s", 0, 1);
	torture_assert_ntstatus_ok_goto(tctx, status, ret, done,
					"write failed\n");
	sleep(3);

	torture_comment(tctx, "Check writetime has NOT been updated\n");

	finfo = (union smb_fileinfo) {
		.generic.level = RAW_FILEINFO_SMB2_ALL_INFORMATION,
		.generic.in.file.handle = h1,
	};
	status = smb2_getinfo_file(tree, tree, &finfo);
	torture_assert_ntstatus_ok_goto(tctx, status, ret, done,
					"getinfo failed\n");
	write_time2 = finfo.all_info.out.write_time;

	torture_assert_nttime_equal(tctx, write_time2, write_time,
				    "second write updated write-time (wrong!)\n");

	torture_comment(tctx, "Close file-handle 1\n");
	sleep(2);

	torture_comment(tctx, "Check writetime has been updated\n");

	c = (struct smb2_close) {
		.in.file.handle = h1,
		.in.flags = SMB2_CLOSE_FLAGS_FULL_INFORMATION,
	};

	status = smb2_close(tree, &c);
	torture_assert_ntstatus_ok_goto(tctx, status, ret, done,
					"close failed\n");
	ZERO_STRUCT(h1);
	close_time = c.out.write_time;

	if (!(close_time > write_time)) {
		ret = false;
		torture_fail_goto(tctx, done,
				  "Write-time not updated (wrong!)\n");
	}

done:
	if (!smb2_util_handle_empty(h1)) {
		smb2_util_close(tree, h1);
	}
	smb2_deltree(tree, BASEDIR);
	return ret;
}

/*
   basic testing of SMB2 timestamps
*/
struct torture_suite *torture_smb2_timestamps_init(TALLOC_CTX *ctx)
{
	struct torture_suite *suite = torture_suite_create(ctx, "timestamps");

	torture_suite_add_1smb2_test(suite, "test_close_not_attrib", test_close_no_attrib);
	torture_suite_add_1smb2_test(suite, "time_t_15032385535", test_time_t_15032385535);
	torture_suite_add_1smb2_test(suite, "time_t_10000000000", test_time_t_10000000000);
	torture_suite_add_1smb2_test(suite, "time_t_4294967295", test_time_t_4294967295);
	torture_suite_add_1smb2_test(suite, "time_t_1", test_time_t_1);
	torture_suite_add_1smb2_test(suite, "time_t_0", test_time_t_0);
	torture_suite_add_1smb2_test(suite, "time_t_-1", test_time_t_minus_1);
	torture_suite_add_1smb2_test(suite, "time_t_-2", test_time_t_minus_2);
	torture_suite_add_1smb2_test(suite, "time_t_1968", test_time_t_1968);
	torture_suite_add_1smb2_test(suite, "freeze-thaw", test_freeze_thaw);

	/*
	 * Testing of delayed write-time updates
	 */
	torture_suite_add_1smb2_test(suite, "delayed-write-vs-seteof", test_delayed_write_vs_seteof);
	torture_suite_add_1smb2_test(suite, "delayed-write-vs-flush", test_delayed_write_vs_flush);
	torture_suite_add_1smb2_test(suite, "delayed-write-vs-setbasic", test_delayed_write_vs_setbasic);
	torture_suite_add_1smb2_test(suite, "delayed-1write", test_delayed_1write);
	torture_suite_add_1smb2_test(suite, "delayed-2write", test_delayed_2write);

	suite->description = talloc_strdup(suite, "SMB2 timestamp tests");

	return suite;
}

/*
 * This test shows that Windows has a timestamp resolution of ~15ms. When so
 * when a smaller amount of time than that has passed it's not necessarily
 * detectable on a Windows 2019 and newer who implement immediate timestamp
 * updates.
 *
 * Note that this test relies on a low latency SMB connection. Even with a low
 * latency connection of eg 1m there's a chance of 1/15 that the first part of
 * the test expecting no timestamp change fails as the writetime is updated.
 *
 * Due to this timing dependency this test is skipped in Samba CI, but it is
 * preserved here for future SMB2 timestamps behaviour archealogists.
 *
 * See also: https://lists.samba.org/archive/cifs-protocol/2019-December/003358.html
 */
static bool test_timestamp_resolution1(struct torture_context *tctx,
				       struct smb2_tree *tree)
{
	union smb_fileinfo finfo1;
	const char *fname = BASEDIR "\\" FNAME;
	struct smb2_create cr;
	struct smb2_handle h = {{0}};
	struct smb2_close cl;
	NTSTATUS status;
	bool ret = true;

	smb2_deltree(tree, BASEDIR);
	status = torture_smb2_testdir(tree, BASEDIR, &h);
	torture_assert_ntstatus_ok_goto(tctx, status, ret, done,
					"create failed\n");
	status = smb2_util_close(tree, h );
	torture_assert_ntstatus_ok_goto(tctx, status, ret, done,
					"close failed\n");

	torture_comment(tctx, "Write without delay, expect no "
			"write-time change\n");

	smb2_generic_create(&cr, NULL, false, fname,
			    NTCREATEX_DISP_CREATE,
			    smb2_util_oplock_level(""), 0, 0);
	status = smb2_create(tree, tree, &cr);
	torture_assert_ntstatus_ok_goto(tctx, status, ret, done,
					"create failed\n");
	h = cr.out.file.handle;

	finfo1 = (union smb_fileinfo) {
		.generic.level = RAW_FILEINFO_SMB2_ALL_INFORMATION,
		.generic.in.file.handle = h,
	};
	status = smb2_getinfo_file(tree, tree, &finfo1);
	torture_assert_ntstatus_ok_goto(tctx, status, ret, done,
					"getinfo failed\n");

	status = smb2_util_write(tree, h, "123456789", 0, 9);
	torture_assert_ntstatus_ok_goto(tctx, status, ret, done,
					"write failed\n");

	cl = (struct smb2_close) {
		.in.file.handle = h,
		.in.flags = SMB2_CLOSE_FLAGS_FULL_INFORMATION,
	};

	status = smb2_close(tree, &cl);
	torture_assert_ntstatus_ok_goto(tctx, status, ret, done,
					"close failed\n");
	ZERO_STRUCT(h);

	torture_comment(tctx, "Initial: %s\nClose: %s\n",
			nt_time_string(tctx, finfo1.basic_info.out.write_time),
			nt_time_string(tctx, cl.out.write_time));

	torture_assert_u64_equal_goto(tctx,
				      finfo1.basic_info.out.write_time,
				      cl.out.write_time,
				      ret, done,
				      "Write time changed (wrong!)\n");

	torture_comment(tctx, "Write with 20 ms delay, expect "
			"write-time change\n");

	smb2_generic_create(&cr, NULL, false, fname,
			    NTCREATEX_DISP_OPEN,
			    smb2_util_oplock_level(""), 0, 0);
	status = smb2_create(tree, tree, &cr);
	torture_assert_ntstatus_ok_goto(tctx, status, ret, done,
					"create failed\n");
	h = cr.out.file.handle;

	finfo1 = (union smb_fileinfo) {
		.generic.level = RAW_FILEINFO_SMB2_ALL_INFORMATION,
		.generic.in.file.handle = h,
	};
	status = smb2_getinfo_file(tree, tree, &finfo1);
	torture_assert_ntstatus_ok_goto(tctx, status, ret, done,
					"getinfo failed\n");

	smb_msleep(20);

	status = smb2_util_write(tree, h, "123456789", 0, 9);
	torture_assert_ntstatus_ok_goto(tctx, status, ret, done,
					"write failed\n");

	cl = (struct smb2_close) {
		.in.file.handle = h,
		.in.flags = SMB2_CLOSE_FLAGS_FULL_INFORMATION,
	};

	status = smb2_close(tree, &cl);
	torture_assert_ntstatus_ok_goto(tctx, status, ret, done,
					"close failed\n");
	ZERO_STRUCT(h);

	torture_comment(tctx, "Initial: %s\nClose: %s\n",
			nt_time_string(tctx, finfo1.basic_info.out.write_time),
			nt_time_string(tctx, cl.out.write_time));

	torture_assert_u64_not_equal_goto(
		tctx,
		finfo1.basic_info.out.write_time,
		cl.out.write_time,
		ret, done,
		"Write time did not change (wrong!)\n");

done:
	if (!smb2_util_handle_empty(h)) {
		smb2_util_close(tree, h);
	}
	smb2_deltree(tree, BASEDIR);
	return ret;
}

/*
   basic testing of SMB2 timestamps
*/
struct torture_suite *torture_smb2_timestamp_resolution_init(TALLOC_CTX *ctx)
{
	struct torture_suite *suite = torture_suite_create(ctx, "timestamp_resolution");

	torture_suite_add_1smb2_test(suite, "resolution1", test_timestamp_resolution1);

	suite->description = talloc_strdup(suite, "SMB2 timestamp tests");

	return suite;
}