summaryrefslogtreecommitdiff
path: root/source4/torture/raw/notify.c
blob: 8d024f983c3c8b6dbd5c646a1141d76efeff17a2 (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
/* 
   Unix SMB/CIFS implementation.
   basic raw test suite for change notify
   Copyright (C) Andrew Tridgell 2003
   
   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 2 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, write to the Free Software
   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/

#include "includes.h"
#include "torture/torture.h"
#include "libcli/raw/libcliraw.h"
#include "libcli/libcli.h"
#include "system/filesys.h"
#include "torture/util.h"

#define BASEDIR "\\test_notify"

#define CHECK_STATUS(status, correct) do { \
	if (!NT_STATUS_EQUAL(status, correct)) { \
		printf("(%d) Incorrect status %s - should be %s\n", \
		       __LINE__, nt_errstr(status), nt_errstr(correct)); \
		ret = False; \
		goto done; \
	}} while (0)


#define CHECK_VAL(v, correct) do { \
	if ((v) != (correct)) { \
		printf("(%d) wrong value for %s  0x%x should be 0x%x\n", \
		       __LINE__, #v, (int)v, (int)correct); \
		ret = False; \
		goto done; \
	}} while (0)

#define CHECK_WSTR(field, value, flags) do { \
	if (!field.s || strcmp(field.s, value) || wire_bad_flags(&field, flags, cli)) { \
		printf("(%d) %s [%s] != %s\n",  __LINE__, #field, field.s, value); \
			ret = False; \
		goto done; \
	}} while (0)


/* 
   basic testing of change notify on directories
*/
static BOOL test_notify_dir(struct smbcli_state *cli, TALLOC_CTX *mem_ctx)
{
	BOOL ret = True;
	NTSTATUS status;
	struct smb_notify notify;
	union smb_open io;
	union smb_close cl;
	int i, count, fnum, fnum2;
	struct smbcli_request *req, *req2;
	extern int torture_numops;

	printf("TESTING CHANGE NOTIFY ON DIRECTRIES\n");
		
	/*
	  get a handle on the directory
	*/
	io.generic.level = RAW_OPEN_NTCREATEX;
	io.ntcreatex.in.root_fid = 0;
	io.ntcreatex.in.flags = 0;
	io.ntcreatex.in.access_mask = SEC_FILE_ALL;
	io.ntcreatex.in.create_options = NTCREATEX_OPTIONS_DIRECTORY;
	io.ntcreatex.in.file_attr = FILE_ATTRIBUTE_NORMAL;
	io.ntcreatex.in.share_access = NTCREATEX_SHARE_ACCESS_READ | NTCREATEX_SHARE_ACCESS_WRITE;
	io.ntcreatex.in.alloc_size = 0;
	io.ntcreatex.in.open_disposition = NTCREATEX_DISP_OPEN;
	io.ntcreatex.in.impersonation = NTCREATEX_IMPERSONATION_ANONYMOUS;
	io.ntcreatex.in.security_flags = 0;
	io.ntcreatex.in.fname = BASEDIR;

	status = smb_raw_open(cli->tree, mem_ctx, &io);
	CHECK_STATUS(status, NT_STATUS_OK);
	fnum = io.ntcreatex.out.file.fnum;

	status = smb_raw_open(cli->tree, mem_ctx, &io);
	CHECK_STATUS(status, NT_STATUS_OK);
	fnum2 = io.ntcreatex.out.file.fnum;

	/* ask for a change notify,
	   on file or directory name changes */
	notify.in.buffer_size = 1000;
	notify.in.completion_filter = FILE_NOTIFY_CHANGE_NAME;
	notify.in.file.fnum = fnum;
	notify.in.recursive = True;

	printf("testing notify cancel\n");

	req = smb_raw_changenotify_send(cli->tree, &notify);
	smb_raw_ntcancel(req);
	status = smb_raw_changenotify_recv(req, mem_ctx, &notify);
	CHECK_STATUS(status, NT_STATUS_CANCELLED);

	printf("testing notify mkdir\n");

	req = smb_raw_changenotify_send(cli->tree, &notify);
	smbcli_mkdir(cli->tree, BASEDIR "\\subdir-name");

	status = smb_raw_changenotify_recv(req, mem_ctx, &notify);
	CHECK_STATUS(status, NT_STATUS_OK);

	CHECK_VAL(notify.out.num_changes, 1);
	CHECK_VAL(notify.out.changes[0].action, NOTIFY_ACTION_ADDED);
	CHECK_WSTR(notify.out.changes[0].name, "subdir-name", STR_UNICODE);

	printf("testing notify rmdir\n");

	req = smb_raw_changenotify_send(cli->tree, &notify);
	smbcli_rmdir(cli->tree, BASEDIR "\\subdir-name");

	status = smb_raw_changenotify_recv(req, mem_ctx, &notify);
	CHECK_STATUS(status, NT_STATUS_OK);
	CHECK_VAL(notify.out.num_changes, 1);
	CHECK_VAL(notify.out.changes[0].action, NOTIFY_ACTION_REMOVED);
	CHECK_WSTR(notify.out.changes[0].name, "subdir-name", STR_UNICODE);

	printf("testing notify mkdir - rmdir - mkdir - rmdir\n");

	smbcli_mkdir(cli->tree, BASEDIR "\\subdir-name");
	smbcli_rmdir(cli->tree, BASEDIR "\\subdir-name");
	smbcli_mkdir(cli->tree, BASEDIR "\\subdir-name");
	smbcli_rmdir(cli->tree, BASEDIR "\\subdir-name");
	req = smb_raw_changenotify_send(cli->tree, &notify);
	status = smb_raw_changenotify_recv(req, mem_ctx, &notify);
	CHECK_STATUS(status, NT_STATUS_OK);
	CHECK_VAL(notify.out.num_changes, 4);
	CHECK_VAL(notify.out.changes[0].action, NOTIFY_ACTION_ADDED);
	CHECK_WSTR(notify.out.changes[0].name, "subdir-name", STR_UNICODE);
	CHECK_VAL(notify.out.changes[1].action, NOTIFY_ACTION_REMOVED);
	CHECK_WSTR(notify.out.changes[1].name, "subdir-name", STR_UNICODE);
	CHECK_VAL(notify.out.changes[2].action, NOTIFY_ACTION_ADDED);
	CHECK_WSTR(notify.out.changes[2].name, "subdir-name", STR_UNICODE);
	CHECK_VAL(notify.out.changes[3].action, NOTIFY_ACTION_REMOVED);
	CHECK_WSTR(notify.out.changes[3].name, "subdir-name", STR_UNICODE);

	count = torture_numops;
	printf("testing buffered notify on create of %d files\n", count);
	for (i=0;i<count;i++) {
		char *fname = talloc_asprintf(cli, BASEDIR "\\test%d.txt", i);
		int fnum3 = smbcli_open(cli->tree, fname, O_CREAT|O_RDWR, DENY_NONE);
		if (fnum3 == -1) {
			printf("Failed to create %s - %s\n", 
			       fname, smbcli_errstr(cli->tree));
			ret = False;
			goto done;
		}
		talloc_free(fname);
		smbcli_close(cli->tree, fnum3);
	}

	/* (1st notify) setup a new notify on a different directory handle.
	   This new notify won't see the events above. */
	notify.in.file.fnum = fnum2;
	req2 = smb_raw_changenotify_send(cli->tree, &notify);

	/* (2nd notify) whereas this notify will see the above buffered events,
	   and it directly returns the buffered events */
	notify.in.file.fnum = fnum;
	req = smb_raw_changenotify_send(cli->tree, &notify);

	/* (1st unlink) as the 2nd notify directly returns,
	   this unlink is only seen by the 1st notify and 
	   the 3rd notify (later) */
	printf("testing notify on unlink for the first file\n");
	status = smbcli_unlink(cli->tree, BASEDIR "\\test0.txt");
	CHECK_STATUS(status, NT_STATUS_OK);

	/* receive the reply from the 2nd notify */
	status = smb_raw_changenotify_recv(req, mem_ctx, &notify);
	CHECK_STATUS(status, NT_STATUS_OK);

	CHECK_VAL(notify.out.num_changes, count);
	for (i=1;i<notify.out.num_changes;i++) {
		CHECK_VAL(notify.out.changes[i].action, NOTIFY_ACTION_ADDED);
	}
	CHECK_WSTR(notify.out.changes[0].name, "test0.txt", STR_UNICODE);

	/* and now from the 1st notify */
	status = smb_raw_changenotify_recv(req2, mem_ctx, &notify);
	CHECK_STATUS(status, NT_STATUS_OK);
	CHECK_VAL(notify.out.num_changes, 1);
	CHECK_VAL(notify.out.changes[0].action, NOTIFY_ACTION_REMOVED);
	CHECK_WSTR(notify.out.changes[0].name, "test0.txt", STR_UNICODE);

	/* (3rd notify) this notify will only see the 1st unlink */
	req = smb_raw_changenotify_send(cli->tree, &notify);

	printf("testing notify on wildcard unlink for %d files\n", count-1);
	/* (2nd unlink) do a wildcard unlink */
	status = smbcli_unlink(cli->tree, BASEDIR "\\test*.txt");
	CHECK_STATUS(status, NT_STATUS_OK);

	/* receive the 3rd notify */
	status = smb_raw_changenotify_recv(req, mem_ctx, &notify);
	CHECK_STATUS(status, NT_STATUS_OK);
	CHECK_VAL(notify.out.num_changes, 1);
	CHECK_VAL(notify.out.changes[0].action, NOTIFY_ACTION_REMOVED);
	CHECK_WSTR(notify.out.changes[0].name, "test0.txt", STR_UNICODE);

	/* and we now see the rest of the unlink calls on both directory handles */
	notify.in.file.fnum = fnum;
	msleep(10);
	req = smb_raw_changenotify_send(cli->tree, &notify);
	status = smb_raw_changenotify_recv(req, mem_ctx, &notify);
	CHECK_STATUS(status, NT_STATUS_OK);
	CHECK_VAL(notify.out.num_changes, count-1);
	for (i=0;i<notify.out.num_changes;i++) {
		CHECK_VAL(notify.out.changes[i].action, NOTIFY_ACTION_REMOVED);
	}
	notify.in.file.fnum = fnum2;
	req = smb_raw_changenotify_send(cli->tree, &notify);
	status = smb_raw_changenotify_recv(req, mem_ctx, &notify);
	CHECK_STATUS(status, NT_STATUS_OK);
	CHECK_VAL(notify.out.num_changes, count-1);
	for (i=0;i<notify.out.num_changes;i++) {
		CHECK_VAL(notify.out.changes[i].action, NOTIFY_ACTION_REMOVED);
	}

	printf("testing if a close() on the dir handle triggers the notify reply\n");

	notify.in.file.fnum = fnum;
	req = smb_raw_changenotify_send(cli->tree, &notify);

	cl.close.level = RAW_CLOSE_CLOSE;
	cl.close.in.file.fnum = fnum;
	cl.close.in.write_time = 0;
	status = smb_raw_close(cli->tree, &cl);
	CHECK_STATUS(status, NT_STATUS_OK);

	status = smb_raw_changenotify_recv(req, mem_ctx, &notify);
	CHECK_STATUS(status, NT_STATUS_OK);
	CHECK_VAL(notify.out.num_changes, 0);

done:
	smb_raw_exit(cli->session);
	return ret;
}


/* 
   testing of recursive change notify
*/
static BOOL test_notify_recursive(struct smbcli_state *cli, TALLOC_CTX *mem_ctx)
{
	BOOL ret = True;
	NTSTATUS status;
	struct smb_notify notify;
	union smb_open io;
	int fnum;
	struct smbcli_request *req1, *req2;

	printf("TESTING CHANGE NOTIFY WITH RECURSION\n");
		
	/*
	  get a handle on the directory
	*/
	io.generic.level = RAW_OPEN_NTCREATEX;
	io.ntcreatex.in.root_fid = 0;
	io.ntcreatex.in.flags = 0;
	io.ntcreatex.in.access_mask = SEC_FILE_ALL;
	io.ntcreatex.in.create_options = NTCREATEX_OPTIONS_DIRECTORY;
	io.ntcreatex.in.file_attr = FILE_ATTRIBUTE_NORMAL;
	io.ntcreatex.in.share_access = NTCREATEX_SHARE_ACCESS_READ | NTCREATEX_SHARE_ACCESS_WRITE;
	io.ntcreatex.in.alloc_size = 0;
	io.ntcreatex.in.open_disposition = NTCREATEX_DISP_OPEN;
	io.ntcreatex.in.impersonation = NTCREATEX_IMPERSONATION_ANONYMOUS;
	io.ntcreatex.in.security_flags = 0;
	io.ntcreatex.in.fname = BASEDIR;

	status = smb_raw_open(cli->tree, mem_ctx, &io);
	CHECK_STATUS(status, NT_STATUS_OK);
	fnum = io.ntcreatex.out.file.fnum;

	/* ask for a change notify, on file or directory name
	   changes. Setup both with and without recursion */
	notify.in.buffer_size = 1000;
	notify.in.completion_filter = FILE_NOTIFY_CHANGE_NAME | FILE_NOTIFY_CHANGE_ATTRIBUTES | FILE_NOTIFY_CHANGE_CREATION;
	notify.in.file.fnum = fnum;

	notify.in.recursive = True;
	req1 = smb_raw_changenotify_send(cli->tree, &notify);

	notify.in.recursive = False;
	req2 = smb_raw_changenotify_send(cli->tree, &notify);

	/* cancel initial requests so the buffer is setup */
	smb_raw_ntcancel(req1);
	status = smb_raw_changenotify_recv(req1, mem_ctx, &notify);
	CHECK_STATUS(status, NT_STATUS_CANCELLED);

	smb_raw_ntcancel(req2);
	status = smb_raw_changenotify_recv(req2, mem_ctx, &notify);
	CHECK_STATUS(status, NT_STATUS_CANCELLED);

	smbcli_mkdir(cli->tree, BASEDIR "\\subdir-name");
	smbcli_mkdir(cli->tree, BASEDIR "\\subdir-name\\subname1");
	smbcli_close(cli->tree, 
		     smbcli_open(cli->tree, BASEDIR "\\subdir-name\\subname2", O_CREAT, 0));
	smbcli_rename(cli->tree, BASEDIR "\\subdir-name\\subname1", BASEDIR "\\subdir-name\\subname1-r");
	smbcli_rename(cli->tree, BASEDIR "\\subdir-name\\subname2", BASEDIR "\\subname2-r");
	smbcli_rename(cli->tree, BASEDIR "\\subname2-r", BASEDIR "\\subname3-r");

	notify.in.completion_filter = 0;
	notify.in.recursive = True;
	msleep(10);
	req1 = smb_raw_changenotify_send(cli->tree, &notify);

	smbcli_rmdir(cli->tree, BASEDIR "\\subdir-name\\subname1-r");
	smbcli_rmdir(cli->tree, BASEDIR "\\subdir-name");
	smbcli_unlink(cli->tree, BASEDIR "\\subname3-r");

	notify.in.recursive = False;
	req2 = smb_raw_changenotify_send(cli->tree, &notify);

	status = smb_raw_changenotify_recv(req1, mem_ctx, &notify);
	CHECK_STATUS(status, NT_STATUS_OK);

	CHECK_VAL(notify.out.num_changes, 11);
	CHECK_VAL(notify.out.changes[0].action, NOTIFY_ACTION_ADDED);
	CHECK_WSTR(notify.out.changes[0].name, "subdir-name", STR_UNICODE);
	CHECK_VAL(notify.out.changes[1].action, NOTIFY_ACTION_ADDED);
	CHECK_WSTR(notify.out.changes[1].name, "subdir-name\\subname1", STR_UNICODE);
	CHECK_VAL(notify.out.changes[2].action, NOTIFY_ACTION_ADDED);
	CHECK_WSTR(notify.out.changes[2].name, "subdir-name\\subname2", STR_UNICODE);
	CHECK_VAL(notify.out.changes[3].action, NOTIFY_ACTION_OLD_NAME);
	CHECK_WSTR(notify.out.changes[3].name, "subdir-name\\subname1", STR_UNICODE);
	CHECK_VAL(notify.out.changes[4].action, NOTIFY_ACTION_NEW_NAME);
	CHECK_WSTR(notify.out.changes[4].name, "subdir-name\\subname1-r", STR_UNICODE);

	/* the remove/add between directories is acceptable in either order */
	if (notify.out.changes[5].action == NOTIFY_ACTION_ADDED) {
		CHECK_VAL(notify.out.changes[6].action, NOTIFY_ACTION_REMOVED);
		CHECK_WSTR(notify.out.changes[6].name, "subdir-name\\subname2", STR_UNICODE);
		CHECK_VAL(notify.out.changes[5].action, NOTIFY_ACTION_ADDED);
		CHECK_WSTR(notify.out.changes[5].name, "subname2-r", STR_UNICODE);
	} else {
		CHECK_VAL(notify.out.changes[5].action, NOTIFY_ACTION_REMOVED);
		CHECK_WSTR(notify.out.changes[5].name, "subdir-name\\subname2", STR_UNICODE);
		CHECK_VAL(notify.out.changes[6].action, NOTIFY_ACTION_ADDED);
		CHECK_WSTR(notify.out.changes[6].name, "subname2-r", STR_UNICODE);
	}

	CHECK_VAL(notify.out.changes[7].action, NOTIFY_ACTION_MODIFIED);
	CHECK_WSTR(notify.out.changes[7].name, "subname2-r", STR_UNICODE);

	CHECK_VAL(notify.out.changes[8].action, NOTIFY_ACTION_OLD_NAME);
	CHECK_WSTR(notify.out.changes[8].name, "subname2-r", STR_UNICODE);
	CHECK_VAL(notify.out.changes[9].action, NOTIFY_ACTION_NEW_NAME);
	CHECK_WSTR(notify.out.changes[9].name, "subname3-r", STR_UNICODE);
	CHECK_VAL(notify.out.changes[10].action, NOTIFY_ACTION_MODIFIED);
	CHECK_WSTR(notify.out.changes[10].name, "subname3-r", STR_UNICODE);

	status = smb_raw_changenotify_recv(req2, mem_ctx, &notify);
	CHECK_STATUS(status, NT_STATUS_OK);

	CHECK_VAL(notify.out.num_changes, 3);
	CHECK_VAL(notify.out.changes[0].action, NOTIFY_ACTION_REMOVED);
	CHECK_WSTR(notify.out.changes[0].name, "subdir-name\\subname1-r", STR_UNICODE);
	CHECK_VAL(notify.out.changes[1].action, NOTIFY_ACTION_REMOVED);
	CHECK_WSTR(notify.out.changes[1].name, "subdir-name", STR_UNICODE);
	CHECK_VAL(notify.out.changes[2].action, NOTIFY_ACTION_REMOVED);
	CHECK_WSTR(notify.out.changes[2].name, "subname3-r", STR_UNICODE);

done:
	smb_raw_exit(cli->session);
	return ret;
}


/* 
   testing of mask bits for change notify
*/
static BOOL test_notify_mask(struct smbcli_state *cli, TALLOC_CTX *mem_ctx)
{
	BOOL ret = True;
	NTSTATUS status;
	struct smb_notify notify;
	union smb_open io;
	int fnum, fnum2;
	uint32_t mask;
	int i;
	char c = 1;
	struct timeval tv;
	NTTIME t;

	printf("TESTING CHANGE NOTIFY COMPLETION FILTERS\n");

	tv = timeval_current_ofs(1000, 0);
	t = timeval_to_nttime(&tv);
		
	/*
	  get a handle on the directory
	*/
	io.generic.level = RAW_OPEN_NTCREATEX;
	io.ntcreatex.in.root_fid = 0;
	io.ntcreatex.in.flags = 0;
	io.ntcreatex.in.access_mask = SEC_FILE_ALL;
	io.ntcreatex.in.create_options = NTCREATEX_OPTIONS_DIRECTORY;
	io.ntcreatex.in.file_attr = FILE_ATTRIBUTE_NORMAL;
	io.ntcreatex.in.share_access = NTCREATEX_SHARE_ACCESS_READ | NTCREATEX_SHARE_ACCESS_WRITE;
	io.ntcreatex.in.alloc_size = 0;
	io.ntcreatex.in.open_disposition = NTCREATEX_DISP_OPEN;
	io.ntcreatex.in.impersonation = NTCREATEX_IMPERSONATION_ANONYMOUS;
	io.ntcreatex.in.security_flags = 0;
	io.ntcreatex.in.fname = BASEDIR;

	notify.in.buffer_size = 1000;
	notify.in.recursive = True;

#define NOTIFY_MASK_TEST(setup, op, cleanup, Action, expected, nchanges) \
	do { for (mask=i=0;i<32;i++) { \
		struct smbcli_request *req; \
		status = smb_raw_open(cli->tree, mem_ctx, &io); \
		CHECK_STATUS(status, NT_STATUS_OK); \
		fnum = io.ntcreatex.out.file.fnum; \
		setup \
		notify.in.file.fnum = fnum;	\
		notify.in.completion_filter = (1<<i); \
		req = smb_raw_changenotify_send(cli->tree, &notify); \
		op \
		msleep(10); smb_raw_ntcancel(req); \
		status = smb_raw_changenotify_recv(req, mem_ctx, &notify); \
		cleanup \
		smbcli_close(cli->tree, fnum); \
		if (NT_STATUS_EQUAL(status, NT_STATUS_CANCELLED)) continue; \
		CHECK_STATUS(status, NT_STATUS_OK); \
		/* special case to cope with file rename behaviour */ \
		if (nchanges == 2 && notify.out.num_changes == 1 && \
		    notify.out.changes[0].action == NOTIFY_ACTION_MODIFIED && \
		    ((expected) & FILE_NOTIFY_CHANGE_ATTRIBUTES) && \
		    Action == NOTIFY_ACTION_OLD_NAME) { \
			printf("(rename file special handling OK)\n"); \
		} else if (nchanges != notify.out.num_changes || \
		    notify.out.changes[0].action != Action || \
		    strcmp(notify.out.changes[0].name.s, "tname1") != 0) { \
			printf("ERROR: nchanges=%d action=%d filter=0x%08x\n", \
			       notify.out.num_changes, \
			       notify.out.changes[0].action, \
			       notify.in.completion_filter); \
			ret = False; \
		} \
		mask |= (1<<i); \
	} \
	if ((expected) != mask) { \
		if (((expected) & ~mask) != 0) { \
			printf("ERROR: trigger on too few bits. mask=0x%08x expected=0x%08x\n", \
			       mask, expected); \
			ret = False; \
		} else { \
			printf("WARNING: trigger on too many bits. mask=0x%08x expected=0x%08x\n", \
			       mask, expected); \
		} \
	} \
	} while (0)

	printf("testing mkdir\n");
	NOTIFY_MASK_TEST(;,
			 smbcli_mkdir(cli->tree, BASEDIR "\\tname1");,
			 smbcli_rmdir(cli->tree, BASEDIR "\\tname1");,
			 NOTIFY_ACTION_ADDED,
			 FILE_NOTIFY_CHANGE_DIR_NAME, 1);

	printf("testing create file\n");
	NOTIFY_MASK_TEST(;,
			 smbcli_close(cli->tree, smbcli_open(cli->tree, BASEDIR "\\tname1", O_CREAT, 0));,
			 smbcli_unlink(cli->tree, BASEDIR "\\tname1");,
			 NOTIFY_ACTION_ADDED,
			 FILE_NOTIFY_CHANGE_FILE_NAME, 1);

	printf("testing unlink\n");
	NOTIFY_MASK_TEST(
			 smbcli_close(cli->tree, smbcli_open(cli->tree, BASEDIR "\\tname1", O_CREAT, 0));,
			 smbcli_unlink(cli->tree, BASEDIR "\\tname1");,
			 ;,
			 NOTIFY_ACTION_REMOVED,
			 FILE_NOTIFY_CHANGE_FILE_NAME, 1);

	printf("testing rmdir\n");
	NOTIFY_MASK_TEST(
			 smbcli_mkdir(cli->tree, BASEDIR "\\tname1");,
			 smbcli_rmdir(cli->tree, BASEDIR "\\tname1");,
			 ;,
			 NOTIFY_ACTION_REMOVED,
			 FILE_NOTIFY_CHANGE_DIR_NAME, 1);

	printf("testing rename file\n");
	NOTIFY_MASK_TEST(
			 smbcli_close(cli->tree, smbcli_open(cli->tree, BASEDIR "\\tname1", O_CREAT, 0));,
			 smbcli_rename(cli->tree, BASEDIR "\\tname1", BASEDIR "\\tname2");,
			 smbcli_unlink(cli->tree, BASEDIR "\\tname2");,
			 NOTIFY_ACTION_OLD_NAME,
			 FILE_NOTIFY_CHANGE_FILE_NAME|FILE_NOTIFY_CHANGE_ATTRIBUTES|FILE_NOTIFY_CHANGE_CREATION, 2);

	printf("testing rename dir\n");
	NOTIFY_MASK_TEST(
		smbcli_mkdir(cli->tree, BASEDIR "\\tname1");,
		smbcli_rename(cli->tree, BASEDIR "\\tname1", BASEDIR "\\tname2");,
		smbcli_rmdir(cli->tree, BASEDIR "\\tname2");,
		NOTIFY_ACTION_OLD_NAME,
		FILE_NOTIFY_CHANGE_DIR_NAME, 2);

	printf("testing set path attribute\n");
	NOTIFY_MASK_TEST(
		smbcli_close(cli->tree, smbcli_open(cli->tree, BASEDIR "\\tname1", O_CREAT, 0));,
		smbcli_setatr(cli->tree, BASEDIR "\\tname1", FILE_ATTRIBUTE_HIDDEN, 0);,
		smbcli_unlink(cli->tree, BASEDIR "\\tname1");,
		NOTIFY_ACTION_MODIFIED,
		FILE_NOTIFY_CHANGE_ATTRIBUTES, 1);

	printf("testing set path write time\n");
	NOTIFY_MASK_TEST(
		smbcli_close(cli->tree, smbcli_open(cli->tree, BASEDIR "\\tname1", O_CREAT, 0));,
		smbcli_setatr(cli->tree, BASEDIR "\\tname1", FILE_ATTRIBUTE_NORMAL, 1000);,
		smbcli_unlink(cli->tree, BASEDIR "\\tname1");,
		NOTIFY_ACTION_MODIFIED,
		FILE_NOTIFY_CHANGE_LAST_WRITE, 1);

	printf("testing set file attribute\n");
	NOTIFY_MASK_TEST(
		fnum2 = create_complex_file(cli, mem_ctx, BASEDIR "\\tname1");,
		smbcli_fsetatr(cli->tree, fnum2, FILE_ATTRIBUTE_HIDDEN, 0, 0, 0, 0);,
		(smbcli_close(cli->tree, fnum2), smbcli_unlink(cli->tree, BASEDIR "\\tname1"));,
		NOTIFY_ACTION_MODIFIED,
		FILE_NOTIFY_CHANGE_ATTRIBUTES, 1);

	printf("testing set file create time\n");
	NOTIFY_MASK_TEST(
		fnum2 = create_complex_file(cli, mem_ctx, BASEDIR "\\tname1");,
		smbcli_fsetatr(cli->tree, fnum2, 0, t, 0, 0, 0);,
		(smbcli_close(cli->tree, fnum2), smbcli_unlink(cli->tree, BASEDIR "\\tname1"));,
		NOTIFY_ACTION_MODIFIED,
		FILE_NOTIFY_CHANGE_CREATION, 1);

	printf("testing set file access time\n");
	NOTIFY_MASK_TEST(
		fnum2 = create_complex_file(cli, mem_ctx, BASEDIR "\\tname1");,
		smbcli_fsetatr(cli->tree, fnum2, 0, 0, t, 0, 0);,
		(smbcli_close(cli->tree, fnum2), smbcli_unlink(cli->tree, BASEDIR "\\tname1"));,
		NOTIFY_ACTION_MODIFIED,
		FILE_NOTIFY_CHANGE_LAST_ACCESS, 1);

	printf("testing set file write time\n");
	NOTIFY_MASK_TEST(
		fnum2 = create_complex_file(cli, mem_ctx, BASEDIR "\\tname1");,
		smbcli_fsetatr(cli->tree, fnum2, 0, 0, 0, t, 0);,
		(smbcli_close(cli->tree, fnum2), smbcli_unlink(cli->tree, BASEDIR "\\tname1"));,
		NOTIFY_ACTION_MODIFIED,
		FILE_NOTIFY_CHANGE_LAST_WRITE, 1);

	printf("testing set file change time\n");
	NOTIFY_MASK_TEST(
		fnum2 = create_complex_file(cli, mem_ctx, BASEDIR "\\tname1");,
		smbcli_fsetatr(cli->tree, fnum2, 0, 0, 0, 0, t);,
		(smbcli_close(cli->tree, fnum2), smbcli_unlink(cli->tree, BASEDIR "\\tname1"));,
		NOTIFY_ACTION_MODIFIED,
		0, 1);


	printf("testing write\n");
	NOTIFY_MASK_TEST(
		fnum2 = create_complex_file(cli, mem_ctx, BASEDIR "\\tname1");,
		smbcli_write(cli->tree, fnum2, 1, &c, 10000, 1);,
		(smbcli_close(cli->tree, fnum2), smbcli_unlink(cli->tree, BASEDIR "\\tname1"));,
		NOTIFY_ACTION_MODIFIED,
		0, 1);

	printf("testing truncate\n");
	NOTIFY_MASK_TEST(
		fnum2 = create_complex_file(cli, mem_ctx, BASEDIR "\\tname1");,
		smbcli_ftruncate(cli->tree, fnum2, 10000);,
		(smbcli_close(cli->tree, fnum2), smbcli_unlink(cli->tree, BASEDIR "\\tname1"));,
		NOTIFY_ACTION_MODIFIED,
		FILE_NOTIFY_CHANGE_SIZE | FILE_NOTIFY_CHANGE_ATTRIBUTES, 1);

done:
	smb_raw_exit(cli->session);
	return ret;
}

/*
  basic testing of change notify on files
*/
static BOOL test_notify_file(struct smbcli_state *cli, TALLOC_CTX *mem_ctx)
{
	NTSTATUS status;
	BOOL ret = True;
	union smb_open io;
	union smb_close cl;
	struct smb_notify notify;
	struct smbcli_request *req;
	int fnum;
	const char *fname = BASEDIR "\\file.txt";

	printf("TESTING CHANGE NOTIFY ON FILES\n");

	io.generic.level = RAW_OPEN_NTCREATEX;
	io.ntcreatex.in.root_fid = 0;
	io.ntcreatex.in.flags = 0;
	io.ntcreatex.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
	io.ntcreatex.in.create_options = 0;
	io.ntcreatex.in.file_attr = FILE_ATTRIBUTE_NORMAL;
	io.ntcreatex.in.share_access = NTCREATEX_SHARE_ACCESS_READ | NTCREATEX_SHARE_ACCESS_WRITE;
	io.ntcreatex.in.alloc_size = 0;
	io.ntcreatex.in.open_disposition = NTCREATEX_DISP_CREATE;
	io.ntcreatex.in.impersonation = NTCREATEX_IMPERSONATION_ANONYMOUS;
	io.ntcreatex.in.security_flags = 0;
	io.ntcreatex.in.fname = fname;
	status = smb_raw_open(cli->tree, mem_ctx, &io);
	CHECK_STATUS(status, NT_STATUS_OK);
	fnum = io.ntcreatex.out.file.fnum;

	/* ask for a change notify,
	   on file or directory name changes */
	notify.in.file.fnum = fnum;
	notify.in.buffer_size = 1000;
	notify.in.completion_filter = FILE_NOTIFY_CHANGE_STREAM_NAME;
	notify.in.recursive = False;

	printf("testing if notifies on file handles are invalid (should be)\n");

	req = smb_raw_changenotify_send(cli->tree, &notify);
	status = smb_raw_changenotify_recv(req, mem_ctx, &notify);
	CHECK_STATUS(status, NT_STATUS_INVALID_PARAMETER);

	cl.close.level = RAW_CLOSE_CLOSE;
	cl.close.in.file.fnum = fnum;
	cl.close.in.write_time = 0;
	status = smb_raw_close(cli->tree, &cl);
	CHECK_STATUS(status, NT_STATUS_OK);

	status = smbcli_unlink(cli->tree, fname);
	CHECK_STATUS(status, NT_STATUS_OK);

done:
	smb_raw_exit(cli->session);
	return ret;
}

/*
  basic testing of change notifies followed by a tdis
*/
static BOOL test_notify_tdis(TALLOC_CTX *mem_ctx)
{
	BOOL ret = True;
	NTSTATUS status;
	struct smb_notify notify;
	union smb_open io;
	int fnum;
	struct smbcli_request *req;
	struct smbcli_state *cli = NULL;

	printf("TESTING CHANGE NOTIFY FOLLOWED BY TDIS\n");

	if (!torture_open_connection(&cli)) {
		return False;
	}

	/*
	  get a handle on the directory
	*/
	io.generic.level = RAW_OPEN_NTCREATEX;
	io.ntcreatex.in.root_fid = 0;
	io.ntcreatex.in.flags = 0;
	io.ntcreatex.in.access_mask = SEC_FILE_ALL;
	io.ntcreatex.in.create_options = NTCREATEX_OPTIONS_DIRECTORY;
	io.ntcreatex.in.file_attr = FILE_ATTRIBUTE_NORMAL;
	io.ntcreatex.in.share_access = NTCREATEX_SHARE_ACCESS_READ | NTCREATEX_SHARE_ACCESS_WRITE;
	io.ntcreatex.in.alloc_size = 0;
	io.ntcreatex.in.open_disposition = NTCREATEX_DISP_OPEN;
	io.ntcreatex.in.impersonation = NTCREATEX_IMPERSONATION_ANONYMOUS;
	io.ntcreatex.in.security_flags = 0;
	io.ntcreatex.in.fname = BASEDIR;

	status = smb_raw_open(cli->tree, mem_ctx, &io);
	CHECK_STATUS(status, NT_STATUS_OK);
	fnum = io.ntcreatex.out.file.fnum;

	/* ask for a change notify,
	   on file or directory name changes */
	notify.in.buffer_size = 1000;
	notify.in.completion_filter = FILE_NOTIFY_CHANGE_NAME;
	notify.in.file.fnum = fnum;
	notify.in.recursive = True;

	req = smb_raw_changenotify_send(cli->tree, &notify);

	status = smbcli_tdis(cli);
	CHECK_STATUS(status, NT_STATUS_OK);

	status = smb_raw_changenotify_recv(req, mem_ctx, &notify);
	CHECK_STATUS(status, NT_STATUS_OK);
	CHECK_VAL(notify.out.num_changes, 0);

done:
	torture_close_connection(cli);
	return ret;
}

/*
  basic testing of change notifies followed by a exit
*/
static BOOL test_notify_exit(TALLOC_CTX *mem_ctx)
{
	BOOL ret = True;
	NTSTATUS status;
	struct smb_notify notify;
	union smb_open io;
	int fnum;
	struct smbcli_request *req;
	struct smbcli_state *cli = NULL;

	printf("TESTING CHANGE NOTIFY FOLLOWED BY EXIT\n");

	if (!torture_open_connection(&cli)) {
		return False;
	}

	/*
	  get a handle on the directory
	*/
	io.generic.level = RAW_OPEN_NTCREATEX;
	io.ntcreatex.in.root_fid = 0;
	io.ntcreatex.in.flags = 0;
	io.ntcreatex.in.access_mask = SEC_FILE_ALL;
	io.ntcreatex.in.create_options = NTCREATEX_OPTIONS_DIRECTORY;
	io.ntcreatex.in.file_attr = FILE_ATTRIBUTE_NORMAL;
	io.ntcreatex.in.share_access = NTCREATEX_SHARE_ACCESS_READ | NTCREATEX_SHARE_ACCESS_WRITE;
	io.ntcreatex.in.alloc_size = 0;
	io.ntcreatex.in.open_disposition = NTCREATEX_DISP_OPEN;
	io.ntcreatex.in.impersonation = NTCREATEX_IMPERSONATION_ANONYMOUS;
	io.ntcreatex.in.security_flags = 0;
	io.ntcreatex.in.fname = BASEDIR;

	status = smb_raw_open(cli->tree, mem_ctx, &io);
	CHECK_STATUS(status, NT_STATUS_OK);
	fnum = io.ntcreatex.out.file.fnum;

	/* ask for a change notify,
	   on file or directory name changes */
	notify.in.buffer_size = 1000;
	notify.in.completion_filter = FILE_NOTIFY_CHANGE_NAME;
	notify.in.file.fnum = fnum;
	notify.in.recursive = True;

	req = smb_raw_changenotify_send(cli->tree, &notify);

	status = smb_raw_exit(cli->session);
	CHECK_STATUS(status, NT_STATUS_OK);

	status = smb_raw_changenotify_recv(req, mem_ctx, &notify);
	CHECK_STATUS(status, NT_STATUS_OK);
	CHECK_VAL(notify.out.num_changes, 0);

done:
	torture_close_connection(cli);
	return ret;
}

/*
  basic testing of change notifies followed by a ulogoff
*/
static BOOL test_notify_ulogoff(TALLOC_CTX *mem_ctx)
{
	BOOL ret = True;
	NTSTATUS status;
	struct smb_notify notify;
	union smb_open io;
	int fnum;
	struct smbcli_request *req;
	struct smbcli_state *cli = NULL;

	printf("TESTING CHANGE NOTIFY FOLLOWED BY ULOGOFF\n");

	if (!torture_open_connection(&cli)) {
		return False;
	}

	/*
	  get a handle on the directory
	*/
	io.generic.level = RAW_OPEN_NTCREATEX;
	io.ntcreatex.in.root_fid = 0;
	io.ntcreatex.in.flags = 0;
	io.ntcreatex.in.access_mask = SEC_FILE_ALL;
	io.ntcreatex.in.create_options = NTCREATEX_OPTIONS_DIRECTORY;
	io.ntcreatex.in.file_attr = FILE_ATTRIBUTE_NORMAL;
	io.ntcreatex.in.share_access = NTCREATEX_SHARE_ACCESS_READ | NTCREATEX_SHARE_ACCESS_WRITE;
	io.ntcreatex.in.alloc_size = 0;
	io.ntcreatex.in.open_disposition = NTCREATEX_DISP_OPEN;
	io.ntcreatex.in.impersonation = NTCREATEX_IMPERSONATION_ANONYMOUS;
	io.ntcreatex.in.security_flags = 0;
	io.ntcreatex.in.fname = BASEDIR;

	status = smb_raw_open(cli->tree, mem_ctx, &io);
	CHECK_STATUS(status, NT_STATUS_OK);
	fnum = io.ntcreatex.out.file.fnum;

	/* ask for a change notify,
	   on file or directory name changes */
	notify.in.buffer_size = 1000;
	notify.in.completion_filter = FILE_NOTIFY_CHANGE_NAME;
	notify.in.file.fnum = fnum;
	notify.in.recursive = True;

	req = smb_raw_changenotify_send(cli->tree, &notify);

	status = smb_raw_ulogoff(cli->session);
	CHECK_STATUS(status, NT_STATUS_OK);

	status = smb_raw_changenotify_recv(req, mem_ctx, &notify);
	CHECK_STATUS(status, NT_STATUS_OK);
	CHECK_VAL(notify.out.num_changes, 0);

done:
	torture_close_connection(cli);
	return ret;
}


/* 
   test setting up two change notify requests on one handle
*/
static BOOL test_notify_double(struct smbcli_state *cli, TALLOC_CTX *mem_ctx)
{
	BOOL ret = True;
	NTSTATUS status;
	struct smb_notify notify;
	union smb_open io;
	int fnum;
	struct smbcli_request *req1, *req2;

	printf("TESTING CHANGE NOTIFY TWICE ON ONE DIRECTORY\n");
		
	/*
	  get a handle on the directory
	*/
	io.generic.level = RAW_OPEN_NTCREATEX;
	io.ntcreatex.in.root_fid = 0;
	io.ntcreatex.in.flags = 0;
	io.ntcreatex.in.access_mask = SEC_FILE_ALL;
	io.ntcreatex.in.create_options = NTCREATEX_OPTIONS_DIRECTORY;
	io.ntcreatex.in.file_attr = FILE_ATTRIBUTE_NORMAL;
	io.ntcreatex.in.share_access = NTCREATEX_SHARE_ACCESS_READ | NTCREATEX_SHARE_ACCESS_WRITE;
	io.ntcreatex.in.alloc_size = 0;
	io.ntcreatex.in.open_disposition = NTCREATEX_DISP_OPEN;
	io.ntcreatex.in.impersonation = NTCREATEX_IMPERSONATION_ANONYMOUS;
	io.ntcreatex.in.security_flags = 0;
	io.ntcreatex.in.fname = BASEDIR;

	status = smb_raw_open(cli->tree, mem_ctx, &io);
	CHECK_STATUS(status, NT_STATUS_OK);
	fnum = io.ntcreatex.out.file.fnum;

	/* ask for a change notify,
	   on file or directory name changes */
	notify.in.buffer_size = 1000;
	notify.in.completion_filter = FILE_NOTIFY_CHANGE_NAME;
	notify.in.file.fnum = fnum;
	notify.in.recursive = True;

	req1 = smb_raw_changenotify_send(cli->tree, &notify);
	req2 = smb_raw_changenotify_send(cli->tree, &notify);

	smbcli_mkdir(cli->tree, BASEDIR "\\subdir-name");

	status = smb_raw_changenotify_recv(req1, mem_ctx, &notify);
	CHECK_STATUS(status, NT_STATUS_OK);
	CHECK_VAL(notify.out.num_changes, 1);
	CHECK_WSTR(notify.out.changes[0].name, "subdir-name", STR_UNICODE);

	smbcli_mkdir(cli->tree, BASEDIR "\\subdir-name2");

	status = smb_raw_changenotify_recv(req2, mem_ctx, &notify);
	CHECK_STATUS(status, NT_STATUS_OK);
	CHECK_VAL(notify.out.num_changes, 1);
	CHECK_WSTR(notify.out.changes[0].name, "subdir-name2", STR_UNICODE);

done:
	smb_raw_exit(cli->session);
	return ret;
}


/* 
   test multiple change notifies at different depths and with/without recursion
*/
static BOOL test_notify_tree(struct smbcli_state *cli, TALLOC_CTX *mem_ctx)
{
	BOOL ret = True;
	struct smb_notify notify;
	union smb_open io;
	struct smbcli_request *req;
	struct {
		const char *path;
		BOOL recursive;
		uint32_t filter;
		int expected;
		int fnum;
	} dirs[] = {
		{BASEDIR "\\abc",               True, FILE_NOTIFY_CHANGE_NAME, 30 },
		{BASEDIR "\\zqy",               True, FILE_NOTIFY_CHANGE_NAME, 8 },
		{BASEDIR "\\atsy",              True, FILE_NOTIFY_CHANGE_NAME, 4 },
		{BASEDIR "\\abc\\foo",          True,  FILE_NOTIFY_CHANGE_NAME, 2 },
		{BASEDIR "\\abc\\blah",         True,  FILE_NOTIFY_CHANGE_NAME, 13 },
		{BASEDIR "\\abc\\blah",         False, FILE_NOTIFY_CHANGE_NAME, 7 },
		{BASEDIR "\\abc\\blah\\a",      True, FILE_NOTIFY_CHANGE_NAME, 2 },
		{BASEDIR "\\abc\\blah\\b",      True, FILE_NOTIFY_CHANGE_NAME, 2 },
		{BASEDIR "\\abc\\blah\\c",      True, FILE_NOTIFY_CHANGE_NAME, 2 },
		{BASEDIR "\\abc\\fooblah",      True, FILE_NOTIFY_CHANGE_NAME, 2 },
		{BASEDIR "\\zqy\\xx",           True, FILE_NOTIFY_CHANGE_NAME, 2 },
		{BASEDIR "\\zqy\\yyy",          True, FILE_NOTIFY_CHANGE_NAME, 2 },
		{BASEDIR "\\zqy\\..",           True, FILE_NOTIFY_CHANGE_NAME, 40 },
		{BASEDIR,                       True, FILE_NOTIFY_CHANGE_NAME, 40 },
		{BASEDIR,                       False,FILE_NOTIFY_CHANGE_NAME, 6 },
		{BASEDIR "\\atsy",              False,FILE_NOTIFY_CHANGE_NAME, 4 },
		{BASEDIR "\\abc",               True, FILE_NOTIFY_CHANGE_NAME, 24 },
		{BASEDIR "\\abc",               False,FILE_NOTIFY_CHANGE_FILE_NAME, 0 },
		{BASEDIR "\\abc",               True, FILE_NOTIFY_CHANGE_FILE_NAME, 0 },
		{BASEDIR "\\abc",               True, FILE_NOTIFY_CHANGE_NAME, 24 },
	};
	int i;
	NTSTATUS status;

	printf("TESTING CHANGE NOTIFY FOR DIFFERENT DEPTHS\n");

	io.generic.level = RAW_OPEN_NTCREATEX;
	io.ntcreatex.in.root_fid = 0;
	io.ntcreatex.in.flags = 0;
	io.ntcreatex.in.access_mask = SEC_FILE_ALL;
	io.ntcreatex.in.create_options = NTCREATEX_OPTIONS_DIRECTORY;
	io.ntcreatex.in.file_attr = FILE_ATTRIBUTE_NORMAL;
	io.ntcreatex.in.share_access = NTCREATEX_SHARE_ACCESS_READ | NTCREATEX_SHARE_ACCESS_WRITE;
	io.ntcreatex.in.alloc_size = 0;
	io.ntcreatex.in.open_disposition = NTCREATEX_DISP_OPEN_IF;
	io.ntcreatex.in.impersonation = NTCREATEX_IMPERSONATION_ANONYMOUS;
	io.ntcreatex.in.security_flags = 0;

	notify.in.buffer_size = 20000;

	/*
	  setup the directory tree, and the notify buffer on each directory
	*/
	for (i=0;i<ARRAY_SIZE(dirs);i++) {
		io.ntcreatex.in.fname = dirs[i].path;
		status = smb_raw_open(cli->tree, mem_ctx, &io);
		CHECK_STATUS(status, NT_STATUS_OK);
		dirs[i].fnum = io.ntcreatex.out.file.fnum;

		notify.in.completion_filter = dirs[i].filter;
		notify.in.file.fnum = dirs[i].fnum;
		notify.in.recursive = dirs[i].recursive;
		req = smb_raw_changenotify_send(cli->tree, &notify);
		smb_raw_ntcancel(req);
		status = smb_raw_changenotify_recv(req, mem_ctx, &notify);
		CHECK_STATUS(status, NT_STATUS_CANCELLED);
	}

	/* trigger 2 events in each dir */
	for (i=0;i<ARRAY_SIZE(dirs);i++) {
		char *path = talloc_asprintf(mem_ctx, "%s\\test.dir", dirs[i].path);
		smbcli_mkdir(cli->tree, path);
		smbcli_rmdir(cli->tree, path);
		talloc_free(path);
	}

	/* give a bit of time for all the events to propogate */
	sleep(2);

	/* count events that have happened in each dir */
	for (i=0;i<ARRAY_SIZE(dirs);i++) {
		notify.in.file.fnum = dirs[i].fnum;
		req = smb_raw_changenotify_send(cli->tree, &notify);
		smb_raw_ntcancel(req);
		notify.out.num_changes = 0;
		status = smb_raw_changenotify_recv(req, mem_ctx, &notify);
		if (notify.out.num_changes != dirs[i].expected) {
			printf("ERROR: i=%d expected %d got %d for '%s'\n",
			       i, dirs[i].expected, notify.out.num_changes,
			       dirs[i].path);
			ret = False;
		}
	}

	/*
	  run from the back, closing and deleting
	*/
	for (i=ARRAY_SIZE(dirs)-1;i>=0;i--) {
		smbcli_close(cli->tree, dirs[i].fnum);
		smbcli_rmdir(cli->tree, dirs[i].path);
	}

done:
	smb_raw_exit(cli->session);
	return ret;
}

/* 
   basic testing of change notify
*/
BOOL torture_raw_notify(struct torture_context *torture)
{
	struct smbcli_state *cli;
	BOOL ret = True;
	TALLOC_CTX *mem_ctx;
		
	if (!torture_open_connection(&cli)) {
		return False;
	}

	mem_ctx = talloc_init("torture_raw_notify");

	if (!torture_setup_dir(cli, BASEDIR)) {
		return False;
	}

	ret &= test_notify_dir(cli, mem_ctx);
	ret &= test_notify_mask(cli, mem_ctx);
	ret &= test_notify_recursive(cli, mem_ctx);
	ret &= test_notify_file(cli, mem_ctx);
	ret &= test_notify_tdis(mem_ctx);
	ret &= test_notify_exit(mem_ctx);
	ret &= test_notify_ulogoff(mem_ctx);
	ret &= test_notify_double(cli, mem_ctx);
	ret &= test_notify_tree(cli, mem_ctx);

	smb_raw_exit(cli->session);
	smbcli_deltree(cli->tree, BASEDIR);
	torture_close_connection(cli);
	talloc_free(mem_ctx);
	return ret;
}