summaryrefslogtreecommitdiff
path: root/source4/torture/raw/search.c
blob: 081bbe8b3a88e4db050859192db04bebfabf28ac (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
/* 
   Unix SMB/CIFS implementation.
   RAW_SEARCH_* individual test suite
   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"


#define BASEDIR "\\testsearch"

/*
  callback function for single_search
*/
static BOOL single_search_callback(void *private, union smb_search_data *file)
{
	union smb_search_data *data = private;

	*data = *file;

	return True;
}

/*
  do a single file (non-wildcard) search 
*/
static NTSTATUS single_search(struct smbcli_state *cli, 
			      TALLOC_CTX *mem_ctx,
			      const char *pattern,
			      enum smb_search_level level,
			      union smb_search_data *data)
{
	union smb_search_first io;
	NTSTATUS status;

	io.generic.level = level;
	if (level == RAW_SEARCH_SEARCH) {
		io.search_first.in.max_count = 1;
		io.search_first.in.search_attrib = 0;
		io.search_first.in.pattern = pattern;
	} else {
		io.t2ffirst.in.search_attrib = 0;
		io.t2ffirst.in.max_count = 1;
		io.t2ffirst.in.flags = FLAG_TRANS2_FIND_CLOSE;
		io.t2ffirst.in.storage_type = 0;
		io.t2ffirst.in.pattern = pattern;
	}

	status = smb_raw_search_first(cli->tree, mem_ctx,
				      &io, (void *)data, single_search_callback);
	
	return status;
}


static struct {
	const char *name;
	enum smb_search_level level;
	uint32_t capability_mask;
	NTSTATUS status;
	union smb_search_data data;
} levels[] = {
	{"SEARCH",                 RAW_SEARCH_SEARCH, },
	{"STANDARD",               RAW_SEARCH_STANDARD, },
	{"EA_SIZE",                RAW_SEARCH_EA_SIZE, },
	{"DIRECTORY_INFO",         RAW_SEARCH_DIRECTORY_INFO, },
	{"FULL_DIRECTORY_INFO",    RAW_SEARCH_FULL_DIRECTORY_INFO, },
	{"NAME_INFO",              RAW_SEARCH_NAME_INFO, },
	{"BOTH_DIRECTORY_INFO",    RAW_SEARCH_BOTH_DIRECTORY_INFO, },
	{"ID_FULL_DIRECTORY_INFO", RAW_SEARCH_ID_FULL_DIRECTORY_INFO, },
	{"ID_BOTH_DIRECTORY_INFO", RAW_SEARCH_ID_BOTH_DIRECTORY_INFO, },
	{"UNIX_INFO",              RAW_SEARCH_UNIX_INFO, CAP_UNIX}
};

/* find a level in the table by name */
static union smb_search_data *find(const char *name)
{
	int i;
	for (i=0;i<ARRAY_SIZE(levels);i++) {
		if (NT_STATUS_IS_OK(levels[i].status) && 
		    strcmp(levels[i].name, name) == 0) {
			return &levels[i].data;
		}
	}
	return NULL;
}

/* 
   basic testing of all RAW_SEARCH_* calls using a single file
*/
static BOOL test_one_file(struct smbcli_state *cli, TALLOC_CTX *mem_ctx)
{
	BOOL ret = True;
	int fnum;
	const char *fname = "\\torture_search.txt";
	NTSTATUS status;
	int i;
	union smb_fileinfo all_info, alt_info, name_info, internal_info;
	union smb_search_data *s;

	printf("Testing one file searches\n");

	fnum = create_complex_file(cli, mem_ctx, fname);
	if (fnum == -1) {
		printf("ERROR: open of %s failed (%s)\n", fname, smbcli_errstr(cli->tree));
		ret = False;
		goto done;
	}

	/* call all the levels */
	for (i=0;i<ARRAY_SIZE(levels);i++) {
		uint32_t cap = cli->transport->negotiate.capabilities;

		printf("testing %s\n", levels[i].name);

		levels[i].status = single_search(cli, mem_ctx, fname, 
						 levels[i].level, &levels[i].data);

		/* see if this server claims to support this level */
		if ((cap & levels[i].capability_mask) != levels[i].capability_mask) {
			printf("search level %s(%d) not supported by server\n",
			       levels[i].name, (int)levels[i].level);
			continue;
		}

		if (!NT_STATUS_IS_OK(levels[i].status)) {
			printf("search level %s(%d) failed - %s\n",
			       levels[i].name, (int)levels[i].level, 
			       nt_errstr(levels[i].status));
			ret = False;
		}
	}

	/* get the all_info file into to check against */
	all_info.generic.level = RAW_FILEINFO_ALL_INFO;
	all_info.generic.in.fname = fname;
	status = smb_raw_pathinfo(cli->tree, mem_ctx, &all_info);
	if (!NT_STATUS_IS_OK(status)) {
		printf("RAW_FILEINFO_ALL_INFO failed - %s\n", nt_errstr(status));
		ret = False;
		goto done;
	}

	alt_info.generic.level = RAW_FILEINFO_ALT_NAME_INFO;
	alt_info.generic.in.fname = fname;
	status = smb_raw_pathinfo(cli->tree, mem_ctx, &alt_info);
	if (!NT_STATUS_IS_OK(status)) {
		printf("RAW_FILEINFO_ALT_NAME_INFO failed - %s\n", nt_errstr(status));
		ret = False;
		goto done;
	}

	internal_info.generic.level = RAW_FILEINFO_INTERNAL_INFORMATION;
	internal_info.generic.in.fname = fname;
	status = smb_raw_pathinfo(cli->tree, mem_ctx, &internal_info);
	if (!NT_STATUS_IS_OK(status)) {
		printf("RAW_FILEINFO_INTERNAL_INFORMATION failed - %s\n", nt_errstr(status));
		ret = False;
		goto done;
	}

	name_info.generic.level = RAW_FILEINFO_NAME_INFO;
	name_info.generic.in.fname = fname;
	status = smb_raw_pathinfo(cli->tree, mem_ctx, &name_info);
	if (!NT_STATUS_IS_OK(status)) {
		printf("RAW_FILEINFO_NAME_INFO failed - %s\n", nt_errstr(status));
		ret = False;
		goto done;
	}

#define CHECK_VAL(name, sname1, field1, v, sname2, field2) do { \
	s = find(name); \
	if (s) { \
		if ((s->sname1.field1) != (v.sname2.out.field2)) { \
			printf("(%d) %s/%s [0x%x] != %s/%s [0x%x]\n", \
			       __LINE__, \
				#sname1, #field1, (int)s->sname1.field1, \
				#sname2, #field2, (int)v.sname2.out.field2); \
			ret = False; \
		} \
	}} while (0)

#define CHECK_TIME(name, sname1, field1, v, sname2, field2) do { \
	s = find(name); \
	if (s) { \
		if (s->sname1.field1 != (~1 & nt_time_to_unix(v.sname2.out.field2))) { \
			printf("(%d) %s/%s [%s] != %s/%s [%s]\n", \
			       __LINE__, \
				#sname1, #field1, timestring(mem_ctx, s->sname1.field1), \
				#sname2, #field2, nt_time_string(mem_ctx, v.sname2.out.field2)); \
			ret = False; \
		} \
	}} while (0)

#define CHECK_NTTIME(name, sname1, field1, v, sname2, field2) do { \
	s = find(name); \
	if (s) { \
		if (s->sname1.field1 != v.sname2.out.field2) { \
			printf("(%d) %s/%s [%s] != %s/%s [%s]\n", \
			       __LINE__, \
				#sname1, #field1, nt_time_string(mem_ctx, s->sname1.field1), \
				#sname2, #field2, nt_time_string(mem_ctx, v.sname2.out.field2)); \
			ret = False; \
		} \
	}} while (0)

#define CHECK_STR(name, sname1, field1, v, sname2, field2) do { \
	s = find(name); \
	if (s) { \
		if (!s->sname1.field1 || strcmp(s->sname1.field1, v.sname2.out.field2.s)) { \
			printf("(%d) %s/%s [%s] != %s/%s [%s]\n", \
			       __LINE__, \
				#sname1, #field1, s->sname1.field1, \
				#sname2, #field2, v.sname2.out.field2.s); \
			ret = False; \
		} \
	}} while (0)

#define CHECK_WSTR(name, sname1, field1, v, sname2, field2, flags) do { \
	s = find(name); \
	if (s) { \
		if (!s->sname1.field1.s || \
		    strcmp(s->sname1.field1.s, v.sname2.out.field2.s) || \
		    wire_bad_flags(&s->sname1.field1, flags, cli)) { \
			printf("(%d) %s/%s [%s] != %s/%s [%s]\n", \
			       __LINE__, \
				#sname1, #field1, s->sname1.field1.s, \
				#sname2, #field2, v.sname2.out.field2.s); \
			ret = False; \
		} \
	}} while (0)

#define CHECK_NAME(name, sname1, field1, fname, flags) do { \
	s = find(name); \
	if (s) { \
		if (!s->sname1.field1.s || \
		    strcmp(s->sname1.field1.s, fname) || \
		    wire_bad_flags(&s->sname1.field1, flags, cli)) { \
			printf("(%d) %s/%s [%s] != %s\n", \
			       __LINE__, \
				#sname1, #field1, s->sname1.field1.s, \
				fname); \
			ret = False; \
		} \
	}} while (0)

#define CHECK_UNIX_NAME(name, sname1, field1, fname, flags) do { \
	s = find(name); \
	if (s) { \
		if (!s->sname1.field1 || \
		    strcmp(s->sname1.field1, fname)) { \
			printf("(%d) %s/%s [%s] != %s\n", \
			       __LINE__, \
				#sname1, #field1, s->sname1.field1, \
				fname); \
			ret = False; \
		} \
	}} while (0)
	
	/* check that all the results are as expected */
	CHECK_VAL("SEARCH",              search,              attrib, all_info, all_info, attrib&0xFFF);
	CHECK_VAL("STANDARD",            standard,            attrib, all_info, all_info, attrib&0xFFF);
	CHECK_VAL("EA_SIZE",             ea_size,             attrib, all_info, all_info, attrib&0xFFF);
	CHECK_VAL("DIRECTORY_INFO",      directory_info,      attrib, all_info, all_info, attrib);
	CHECK_VAL("FULL_DIRECTORY_INFO", full_directory_info, attrib, all_info, all_info, attrib);
	CHECK_VAL("BOTH_DIRECTORY_INFO", both_directory_info, attrib, all_info, all_info, attrib);
	CHECK_VAL("ID_FULL_DIRECTORY_INFO", id_full_directory_info,           attrib, all_info, all_info, attrib);
	CHECK_VAL("ID_BOTH_DIRECTORY_INFO", id_both_directory_info,           attrib, all_info, all_info, attrib);

	CHECK_TIME("SEARCH",             search,              write_time, all_info, all_info, write_time);
	CHECK_TIME("STANDARD",           standard,            write_time, all_info, all_info, write_time);
	CHECK_TIME("EA_SIZE",            ea_size,             write_time, all_info, all_info, write_time);
	CHECK_TIME("STANDARD",           standard,            create_time, all_info, all_info, create_time);
	CHECK_TIME("EA_SIZE",            ea_size,             create_time, all_info, all_info, create_time);
	CHECK_TIME("STANDARD",           standard,            access_time, all_info, all_info, access_time);
	CHECK_TIME("EA_SIZE",            ea_size,             access_time, all_info, all_info, access_time);

	CHECK_NTTIME("DIRECTORY_INFO",      directory_info,      write_time, all_info, all_info, write_time);
	CHECK_NTTIME("FULL_DIRECTORY_INFO", full_directory_info, write_time, all_info, all_info, write_time);
	CHECK_NTTIME("BOTH_DIRECTORY_INFO", both_directory_info, write_time, all_info, all_info, write_time);
	CHECK_NTTIME("ID_FULL_DIRECTORY_INFO", id_full_directory_info,           write_time, all_info, all_info, write_time);
	CHECK_NTTIME("ID_BOTH_DIRECTORY_INFO", id_both_directory_info,           write_time, all_info, all_info, write_time);

	CHECK_NTTIME("DIRECTORY_INFO",      directory_info,      create_time, all_info, all_info, create_time);
	CHECK_NTTIME("FULL_DIRECTORY_INFO", full_directory_info, create_time, all_info, all_info, create_time);
	CHECK_NTTIME("BOTH_DIRECTORY_INFO", both_directory_info, create_time, all_info, all_info, create_time);
	CHECK_NTTIME("ID_FULL_DIRECTORY_INFO", id_full_directory_info,           create_time, all_info, all_info, create_time);
	CHECK_NTTIME("ID_BOTH_DIRECTORY_INFO", id_both_directory_info,           create_time, all_info, all_info, create_time);

	CHECK_NTTIME("DIRECTORY_INFO",      directory_info,      access_time, all_info, all_info, access_time);
	CHECK_NTTIME("FULL_DIRECTORY_INFO", full_directory_info, access_time, all_info, all_info, access_time);
	CHECK_NTTIME("BOTH_DIRECTORY_INFO", both_directory_info, access_time, all_info, all_info, access_time);
	CHECK_NTTIME("ID_FULL_DIRECTORY_INFO", id_full_directory_info,           access_time, all_info, all_info, access_time);
	CHECK_NTTIME("ID_BOTH_DIRECTORY_INFO", id_both_directory_info,           access_time, all_info, all_info, access_time);

	CHECK_NTTIME("DIRECTORY_INFO",      directory_info,      create_time, all_info, all_info, create_time);
	CHECK_NTTIME("FULL_DIRECTORY_INFO", full_directory_info, create_time, all_info, all_info, create_time);
	CHECK_NTTIME("BOTH_DIRECTORY_INFO", both_directory_info, create_time, all_info, all_info, create_time);
	CHECK_NTTIME("ID_FULL_DIRECTORY_INFO", id_full_directory_info,           create_time, all_info, all_info, create_time);
	CHECK_NTTIME("ID_BOTH_DIRECTORY_INFO", id_both_directory_info,           create_time, all_info, all_info, create_time);

	CHECK_VAL("SEARCH",              search,              size, all_info, all_info, size);
	CHECK_VAL("STANDARD",            standard,            size, all_info, all_info, size);
	CHECK_VAL("EA_SIZE",             ea_size,             size, all_info, all_info, size);
	CHECK_VAL("DIRECTORY_INFO",      directory_info,      size, all_info, all_info, size);
	CHECK_VAL("FULL_DIRECTORY_INFO", full_directory_info, size, all_info, all_info, size);
	CHECK_VAL("BOTH_DIRECTORY_INFO", both_directory_info, size, all_info, all_info, size);
	CHECK_VAL("ID_FULL_DIRECTORY_INFO", id_full_directory_info,           size, all_info, all_info, size);
	CHECK_VAL("ID_BOTH_DIRECTORY_INFO", id_both_directory_info,           size, all_info, all_info, size);
	CHECK_VAL("UNIX_INFO",           unix_info,           size, all_info, all_info, size);

	CHECK_VAL("STANDARD",            standard,            alloc_size, all_info, all_info, alloc_size);
	CHECK_VAL("EA_SIZE",             ea_size,             alloc_size, all_info, all_info, alloc_size);
	CHECK_VAL("DIRECTORY_INFO",      directory_info,      alloc_size, all_info, all_info, alloc_size);
	CHECK_VAL("FULL_DIRECTORY_INFO", full_directory_info, alloc_size, all_info, all_info, alloc_size);
	CHECK_VAL("BOTH_DIRECTORY_INFO", both_directory_info, alloc_size, all_info, all_info, alloc_size);
	CHECK_VAL("ID_FULL_DIRECTORY_INFO", id_full_directory_info,           alloc_size, all_info, all_info, alloc_size);
	CHECK_VAL("ID_BOTH_DIRECTORY_INFO", id_both_directory_info,           alloc_size, all_info, all_info, alloc_size);
	CHECK_VAL("UNIX_INFO",           unix_info,           alloc_size, all_info, all_info, alloc_size);

	CHECK_VAL("EA_SIZE",             ea_size,             ea_size, all_info, all_info, ea_size);
	CHECK_VAL("FULL_DIRECTORY_INFO", full_directory_info, ea_size, all_info, all_info, ea_size);
	CHECK_VAL("BOTH_DIRECTORY_INFO", both_directory_info, ea_size, all_info, all_info, ea_size);
	CHECK_VAL("ID_FULL_DIRECTORY_INFO", id_full_directory_info,           ea_size, all_info, all_info, ea_size);
	CHECK_VAL("ID_BOTH_DIRECTORY_INFO", id_both_directory_info,           ea_size, all_info, all_info, ea_size);

	CHECK_STR("SEARCH", search, name, alt_info, alt_name_info, fname);
	CHECK_WSTR("BOTH_DIRECTORY_INFO", both_directory_info, short_name, alt_info, alt_name_info, fname, STR_UNICODE);

	CHECK_NAME("STANDARD",            standard,            name, fname+1, 0);
	CHECK_NAME("EA_SIZE",             ea_size,             name, fname+1, 0);
	CHECK_NAME("DIRECTORY_INFO",      directory_info,      name, fname+1, STR_TERMINATE_ASCII);
	CHECK_NAME("FULL_DIRECTORY_INFO", full_directory_info, name, fname+1, STR_TERMINATE_ASCII);
	CHECK_NAME("NAME_INFO",           name_info,           name, fname+1, STR_TERMINATE_ASCII);
	CHECK_NAME("BOTH_DIRECTORY_INFO", both_directory_info, name, fname+1, STR_TERMINATE_ASCII);
	CHECK_NAME("ID_FULL_DIRECTORY_INFO", id_full_directory_info,           name, fname+1, STR_TERMINATE_ASCII);
	CHECK_NAME("ID_BOTH_DIRECTORY_INFO", id_both_directory_info,           name, fname+1, STR_TERMINATE_ASCII);
	CHECK_UNIX_NAME("UNIX_INFO",           unix_info,           name, fname+1, STR_TERMINATE_ASCII);

	CHECK_VAL("ID_FULL_DIRECTORY_INFO", id_full_directory_info, file_id, internal_info, internal_information, file_id);
	CHECK_VAL("ID_BOTH_DIRECTORY_INFO", id_both_directory_info, file_id, internal_info, internal_information, file_id);

done:
	smb_raw_exit(cli->session);
	smbcli_unlink(cli->tree, fname);

	return ret;
}


struct multiple_result {
	TALLOC_CTX *mem_ctx;
	int count;
	union smb_search_data *list;
};

/*
  callback function for multiple_search
*/
static BOOL multiple_search_callback(void *private, union smb_search_data *file)
{
	struct multiple_result *data = private;


	data->count++;
	data->list = talloc_realloc(data->list, 
				    data->count * (sizeof(data->list[0])));

	data->list[data->count-1] = *file;

	return True;
}

enum continue_type {CONT_FLAGS, CONT_NAME, CONT_RESUME_KEY};

/*
  do a single file (non-wildcard) search 
*/
static NTSTATUS multiple_search(struct smbcli_state *cli, 
				TALLOC_CTX *mem_ctx,
				const char *pattern,
				enum smb_search_level level,
				enum continue_type cont_type,
				void *data)
{
	union smb_search_first io;
	union smb_search_next io2;
	NTSTATUS status;
	const int per_search = 300;
	struct multiple_result *result = data;

	io.generic.level = level;
	if (level == RAW_SEARCH_SEARCH) {
		io.search_first.in.max_count = per_search;
		io.search_first.in.search_attrib = 0;
		io.search_first.in.pattern = pattern;
	} else {
		io.t2ffirst.in.search_attrib = 0;
		io.t2ffirst.in.max_count = per_search;
		io.t2ffirst.in.flags = 0;
		io.t2ffirst.in.storage_type = 0;
		io.t2ffirst.in.pattern = pattern;
		if (cont_type == CONT_RESUME_KEY) {
			io.t2ffirst.in.flags = FLAG_TRANS2_FIND_REQUIRE_RESUME | 
				FLAG_TRANS2_FIND_BACKUP_INTENT;
		}
	}

	status = smb_raw_search_first(cli->tree, mem_ctx,
				      &io, data, multiple_search_callback);
	

	while (NT_STATUS_IS_OK(status)) {
		io2.generic.level = level;
		if (level == RAW_SEARCH_SEARCH) {
			io2.search_next.in.max_count = per_search;
			io2.search_next.in.search_attrib = 0;
			io2.search_next.in.search_id = result->list[result->count-1].search.search_id;
		} else {
			io2.t2fnext.in.handle = io.t2ffirst.out.handle;
			io2.t2fnext.in.max_count = per_search;
			io2.t2fnext.in.resume_key = 0;
			io2.t2fnext.in.flags = 0;
			io2.t2fnext.in.last_name = "";
			switch (cont_type) {
			case CONT_RESUME_KEY:
				if (level == RAW_SEARCH_STANDARD) {
					io2.t2fnext.in.resume_key = 
						result->list[result->count-1].standard.resume_key;
				} else if (level == RAW_SEARCH_EA_SIZE) {
					io2.t2fnext.in.resume_key = 
						result->list[result->count-1].ea_size.resume_key;
				} else if (level == RAW_SEARCH_DIRECTORY_INFO) {
					io2.t2fnext.in.resume_key = 
						result->list[result->count-1].directory_info.file_index;
				} else {
					io2.t2fnext.in.resume_key = 
						result->list[result->count-1].both_directory_info.file_index;
				}
				if (io2.t2fnext.in.resume_key == 0) {
					printf("Server does not support resume by key\n");
					return NT_STATUS_NOT_SUPPORTED;
				}
				io2.t2fnext.in.flags = FLAG_TRANS2_FIND_REQUIRE_RESUME |
					FLAG_TRANS2_FIND_BACKUP_INTENT;
				break;
			case CONT_NAME:
				if (level == RAW_SEARCH_STANDARD) {
					io2.t2fnext.in.last_name = 
						result->list[result->count-1].standard.name.s;
				} else if (level == RAW_SEARCH_EA_SIZE) {
					io2.t2fnext.in.last_name = 
						result->list[result->count-1].ea_size.name.s;
				} else if (level == RAW_SEARCH_DIRECTORY_INFO) {
					io2.t2fnext.in.last_name = 
						result->list[result->count-1].directory_info.name.s;
				} else {
					io2.t2fnext.in.last_name = 
						result->list[result->count-1].both_directory_info.name.s;
				}
				break;
			case CONT_FLAGS:
				io2.t2fnext.in.flags = FLAG_TRANS2_FIND_CONTINUE;
				break;
			}
		}

		status = smb_raw_search_next(cli->tree, mem_ctx,
					     &io2, data, multiple_search_callback);
		if (!NT_STATUS_IS_OK(status)) {
			break;
		}
		if (level == RAW_SEARCH_SEARCH) {
			if (io2.search_next.out.count == 0) {
				break;
			}
		} else if (io2.t2fnext.out.count == 0 ||
			   io2.t2fnext.out.end_of_search) {
			break;
		}
	}

	return status;
}

#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_VALUE(v, correct) do { \
	if ((v) != (correct)) { \
		printf("(%d) Incorrect value %s=%d - should be %d\n", \
		       __LINE__, #v, v, correct); \
		ret = False; \
	}} while (0)


static int search_both_compare(union smb_search_data *d1, union smb_search_data *d2)
{
	return strcmp_safe(d1->both_directory_info.name.s, d2->both_directory_info.name.s);
}

static int search_standard_compare(union smb_search_data *d1, union smb_search_data *d2)
{
	return strcmp_safe(d1->standard.name.s, d2->standard.name.s);
}

static int search_ea_size_compare(union smb_search_data *d1, union smb_search_data *d2)
{
	return strcmp_safe(d1->ea_size.name.s, d2->ea_size.name.s);
}

static int search_directory_info_compare(union smb_search_data *d1, union smb_search_data *d2)
{
	return strcmp_safe(d1->directory_info.name.s, d2->directory_info.name.s);
}

static int search_old_compare(union smb_search_data *d1, union smb_search_data *d2)
{
	return strcmp_safe(d1->search.name, d2->search.name);
}


/* 
   basic testing of search calls using many files
*/
static BOOL test_many_files(struct smbcli_state *cli, TALLOC_CTX *mem_ctx)
{
	const int num_files = 700;
	int i, fnum, t;
	char *fname;
	BOOL ret = True;
	NTSTATUS status;
	struct multiple_result result;
	struct {
		const char *name;
		const char *cont_name;
		enum smb_search_level level;
		enum continue_type cont_type;
	} search_types[] = {
		{"BOTH_DIRECTORY_INFO", "NAME",  RAW_SEARCH_BOTH_DIRECTORY_INFO, CONT_NAME},
		{"BOTH_DIRECTORY_INFO", "FLAGS", RAW_SEARCH_BOTH_DIRECTORY_INFO, CONT_FLAGS},
		{"BOTH_DIRECTORY_INFO", "KEY",   RAW_SEARCH_BOTH_DIRECTORY_INFO, CONT_RESUME_KEY},
		{"STANDARD",            "FLAGS", RAW_SEARCH_STANDARD,            CONT_FLAGS},
		{"STANDARD",            "KEY",   RAW_SEARCH_STANDARD,            CONT_RESUME_KEY},
		{"STANDARD",            "NAME",  RAW_SEARCH_STANDARD,            CONT_NAME},
		{"EA_SIZE",             "FLAGS", RAW_SEARCH_EA_SIZE,             CONT_FLAGS},
		{"EA_SIZE",             "KEY",   RAW_SEARCH_EA_SIZE,             CONT_RESUME_KEY},
		{"EA_SIZE",             "NAME",  RAW_SEARCH_EA_SIZE,             CONT_NAME},
		{"DIRECTORY_INFO",      "FLAGS", RAW_SEARCH_DIRECTORY_INFO,      CONT_FLAGS},
		{"DIRECTORY_INFO",      "KEY",   RAW_SEARCH_DIRECTORY_INFO,      CONT_RESUME_KEY},
		{"DIRECTORY_INFO",      "NAME",  RAW_SEARCH_DIRECTORY_INFO,      CONT_NAME},
		{"SEARCH",              "ID",    RAW_SEARCH_SEARCH,              CONT_RESUME_KEY}
	};

	if (smbcli_deltree(cli->tree, BASEDIR) == -1 || 
	    NT_STATUS_IS_ERR(smbcli_mkdir(cli->tree, BASEDIR))) {
		printf("Failed to create " BASEDIR " - %s\n", smbcli_errstr(cli->tree));
		return False;
	}

	printf("Creating %d files\n", num_files);

	for (i=0;i<num_files;i++) {
		asprintf(&fname, BASEDIR "\\t%03d-%d.txt", i, i);
		fnum = smbcli_open(cli->tree, fname, O_CREAT|O_RDWR, DENY_NONE);
		if (fnum == -1) {
			printf("Failed to create %s - %s\n", fname, smbcli_errstr(cli->tree));
			ret = False;
			goto done;
		}
		free(fname);
		smbcli_close(cli->tree, fnum);
	}


	for (t=0;t<ARRAY_SIZE(search_types);t++) {
		ZERO_STRUCT(result);
		result.mem_ctx = mem_ctx;
	
		printf("Continue %s via %s\n", search_types[t].name, search_types[t].cont_name);

		status = multiple_search(cli, mem_ctx, BASEDIR "\\*.*", 
					 search_types[t].level,
					 search_types[t].cont_type,
					 &result);
	
		if (!NT_STATUS_IS_OK(status)) {
			printf("search failed - %s\n", nt_errstr(status));
			ret = False;
			continue;
		}
		CHECK_VALUE(result.count, num_files);

		if (search_types[t].level == RAW_SEARCH_BOTH_DIRECTORY_INFO) {
			qsort(result.list, result.count, sizeof(result.list[0]), 
			      QSORT_CAST  search_both_compare);
		} else if (search_types[t].level == RAW_SEARCH_STANDARD) {
			qsort(result.list, result.count, sizeof(result.list[0]), 
			      QSORT_CAST search_standard_compare);
		} else if (search_types[t].level == RAW_SEARCH_EA_SIZE) {
			qsort(result.list, result.count, sizeof(result.list[0]), 
			      QSORT_CAST search_ea_size_compare);
		} else if (search_types[t].level == RAW_SEARCH_DIRECTORY_INFO) {
			qsort(result.list, result.count, sizeof(result.list[0]), 
			      QSORT_CAST search_directory_info_compare);
		} else {
			qsort(result.list, result.count, sizeof(result.list[0]), 
			      QSORT_CAST search_old_compare);
		}

		for (i=0;i<num_files;i++) {
			const char *s;
			if (search_types[t].level == RAW_SEARCH_BOTH_DIRECTORY_INFO) {
				s = result.list[i].both_directory_info.name.s;
			} else if (search_types[t].level == RAW_SEARCH_STANDARD) {
				s = result.list[i].standard.name.s;
			} else if (search_types[t].level == RAW_SEARCH_EA_SIZE) {
				s = result.list[i].ea_size.name.s;
			} else if (search_types[t].level == RAW_SEARCH_DIRECTORY_INFO) {
				s = result.list[i].directory_info.name.s;
			} else {
				s = result.list[i].search.name;
			}
			asprintf(&fname, "t%03d-%d.txt", i, i);
			if (strcmp(fname, s)) {
				printf("Incorrect name %s at entry %d\n", s, i);
				ret = False;
				break;
			}
			free(fname);
		}
	}

done:
	smb_raw_exit(cli->session);
	smbcli_deltree(cli->tree, BASEDIR);

	return ret;
}

/*
  check a individual file result
*/
static BOOL check_result(struct multiple_result *result, const char *name, BOOL exist, uint32_t attrib)
{
	int i;
	for (i=0;i<result->count;i++) {
		if (strcmp(name, result->list[i].both_directory_info.name.s) == 0) break;
	}
	if (i == result->count) {
		if (exist) {
			printf("failed: '%s' should exist with attribute %s\n", 
			       name, attrib_string(NULL, attrib));
			return False;
		}
		return True;
	}

	if (!exist) {
		printf("failed: '%s' should NOT exist (has attribute %s)\n", 
		       name, attrib_string(NULL, result->list[i].both_directory_info.attrib));
		return False;
	}

	if ((result->list[i].both_directory_info.attrib&0xFFF) != attrib) {
		printf("failed: '%s' should have attribute 0x%x (has 0x%x)\n",
		       name, 
		       attrib, result->list[i].both_directory_info.attrib);
		return False;
	}
	return True;
}

/* 
   test what happens when the directory is modified during a search
*/
static BOOL test_modify_search(struct smbcli_state *cli, TALLOC_CTX *mem_ctx)
{
	const int num_files = 20;
	int i, fnum;
	char *fname;
	BOOL ret = True;
	NTSTATUS status;
	struct multiple_result result;
	union smb_search_first io;
	union smb_search_next io2;
	union smb_setfileinfo sfinfo;

	if (smbcli_deltree(cli->tree, BASEDIR) == -1 || 
	    NT_STATUS_IS_ERR(smbcli_mkdir(cli->tree, BASEDIR))) {
		printf("Failed to create " BASEDIR " - %s\n", smbcli_errstr(cli->tree));
		return False;
	}

	printf("Creating %d files\n", num_files);

	for (i=0;i<num_files;i++) {
		asprintf(&fname, BASEDIR "\\t%03d-%d.txt", i, i);
		fnum = smbcli_open(cli->tree, fname, O_CREAT|O_RDWR, DENY_NONE);
		if (fnum == -1) {
			printf("Failed to create %s - %s\n", fname, smbcli_errstr(cli->tree));
			ret = False;
			goto done;
		}
		free(fname);
		smbcli_close(cli->tree, fnum);
	}

	printf("pulling the first 10 files\n");
	ZERO_STRUCT(result);

	io.generic.level = RAW_SEARCH_BOTH_DIRECTORY_INFO;
	io.t2ffirst.in.search_attrib = 0;
	io.t2ffirst.in.max_count = 0;
	io.t2ffirst.in.flags = 0;
	io.t2ffirst.in.storage_type = 0;
	io.t2ffirst.in.pattern = BASEDIR "\\*.*";

	status = smb_raw_search_first(cli->tree, mem_ctx,
				      &io, &result, multiple_search_callback);
	CHECK_STATUS(status, NT_STATUS_OK);
	CHECK_VALUE(result.count, 1);

	io2.generic.level = RAW_SEARCH_BOTH_DIRECTORY_INFO;
	io2.t2fnext.in.handle = io.t2ffirst.out.handle;
	io2.t2fnext.in.max_count = num_files/2 - 1;
	io2.t2fnext.in.resume_key = 0;
	io2.t2fnext.in.flags = 0;
	io2.t2fnext.in.last_name = result.list[result.count-1].both_directory_info.name.s;

	status = smb_raw_search_next(cli->tree, mem_ctx,
				     &io2, &result, multiple_search_callback);
	CHECK_STATUS(status, NT_STATUS_OK);
	CHECK_VALUE(result.count, num_files/2);

	printf("Changing attributes and deleting\n");
	smbcli_open(cli->tree, BASEDIR "\\T003-03.txt.2", O_CREAT|O_RDWR, DENY_NONE);
	smbcli_open(cli->tree, BASEDIR "\\T013-13.txt.2", O_CREAT|O_RDWR, DENY_NONE);
	fnum = create_complex_file(cli, mem_ctx, BASEDIR "\\T013-13.txt.3");
	smbcli_unlink(cli->tree, BASEDIR "\\T014-14.txt");
	torture_set_file_attribute(cli->tree, BASEDIR "\\T015-15.txt", FILE_ATTRIBUTE_HIDDEN);
	torture_set_file_attribute(cli->tree, BASEDIR "\\T016-16.txt", FILE_ATTRIBUTE_NORMAL);
	torture_set_file_attribute(cli->tree, BASEDIR "\\T017-17.txt", FILE_ATTRIBUTE_SYSTEM);	
	sfinfo.generic.level = RAW_SFILEINFO_DISPOSITION_INFORMATION;
	sfinfo.generic.file.fnum = fnum;
	sfinfo.disposition_info.in.delete_on_close = 1;
	status = smb_raw_setfileinfo(cli->tree, &sfinfo);
	CHECK_STATUS(status, NT_STATUS_OK);

	io2.generic.level = RAW_SEARCH_BOTH_DIRECTORY_INFO;
	io2.t2fnext.in.handle = io.t2ffirst.out.handle;
	io2.t2fnext.in.max_count = num_files/2;
	io2.t2fnext.in.resume_key = 0;
	io2.t2fnext.in.flags = 0;
	io2.t2fnext.in.last_name = result.list[result.count-1].both_directory_info.name.s;

	status = smb_raw_search_next(cli->tree, mem_ctx,
				     &io2, &result, multiple_search_callback);
	CHECK_STATUS(status, NT_STATUS_OK);
	CHECK_VALUE(result.count, 19);

	ret &= check_result(&result, "t009-9.txt", True, FILE_ATTRIBUTE_ARCHIVE);
	ret &= check_result(&result, "t014-14.txt", False, 0);
	ret &= check_result(&result, "t015-15.txt", False, 0);
	ret &= check_result(&result, "t016-16.txt", True, FILE_ATTRIBUTE_NORMAL);
	ret &= check_result(&result, "t017-17.txt", False, 0);
	ret &= check_result(&result, "t018-18.txt", True, FILE_ATTRIBUTE_ARCHIVE);
	ret &= check_result(&result, "t019-19.txt", True, FILE_ATTRIBUTE_ARCHIVE);
	ret &= check_result(&result, "T013-13.txt.2", True, FILE_ATTRIBUTE_ARCHIVE);
	ret &= check_result(&result, "T003-3.txt.2", False, 0);
	ret &= check_result(&result, "T013-13.txt.3", True, FILE_ATTRIBUTE_ARCHIVE);

	if (!ret) {
		for (i=0;i<result.count;i++) {
			printf("%s %s (0x%x)\n", 
			       result.list[i].both_directory_info.name.s, 
			       attrib_string(mem_ctx, result.list[i].both_directory_info.attrib),
			       result.list[i].both_directory_info.attrib);
		}
	}

done:
	smb_raw_exit(cli->session);
	smbcli_deltree(cli->tree, BASEDIR);

	return ret;
}


/* 
   testing if directories always come back sorted
*/
static BOOL test_sorted(struct smbcli_state *cli, TALLOC_CTX *mem_ctx)
{
	const int num_files = 700;
	int i, fnum;
	char *fname;
	BOOL ret = True;
	NTSTATUS status;
	struct multiple_result result;

	if (smbcli_deltree(cli->tree, BASEDIR) == -1 || 
	    NT_STATUS_IS_ERR(smbcli_mkdir(cli->tree, BASEDIR))) {
		printf("Failed to create " BASEDIR " - %s\n", smbcli_errstr(cli->tree));
		return False;
	}

	printf("Creating %d files\n", num_files);

	for (i=0;i<num_files;i++) {
		asprintf(&fname, BASEDIR "\\%s.txt", generate_random_str(mem_ctx, 10));
		fnum = smbcli_open(cli->tree, fname, O_CREAT|O_RDWR, DENY_NONE);
		if (fnum == -1) {
			printf("Failed to create %s - %s\n", fname, smbcli_errstr(cli->tree));
			ret = False;
			goto done;
		}
		free(fname);
		smbcli_close(cli->tree, fnum);
	}


	ZERO_STRUCT(result);
	result.mem_ctx = mem_ctx;
	
	status = multiple_search(cli, mem_ctx, BASEDIR "\\*.*", 
				 RAW_SEARCH_BOTH_DIRECTORY_INFO,
				 CONT_NAME, &result);	
	CHECK_STATUS(status, NT_STATUS_OK);
	CHECK_VALUE(result.count, num_files);

	for (i=0;i<num_files-1;i++) {
		const char *name1, *name2;
		name1 = result.list[i].both_directory_info.name.s;
		name2 = result.list[i+1].both_directory_info.name.s;
		if (StrCaseCmp(name1, name2) > 0) {
			printf("non-alphabetical order at entry %d  '%s' '%s'\n", 
			       i, name1, name2);
			printf("Server does not produce sorted directory listings\n");
			ret = False;
			goto done;
		}
	}

done:
	smb_raw_exit(cli->session);
	smbcli_deltree(cli->tree, BASEDIR);

	return ret;
}


/* 
   basic testing of all RAW_SEARCH_* calls using a single file
*/
BOOL torture_raw_search(int dummy)
{
	struct smbcli_state *cli;
	BOOL ret = True;
	TALLOC_CTX *mem_ctx;

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

	mem_ctx = talloc_init("torture_search");

	if (!test_one_file(cli, mem_ctx)) {
		ret = False;
	}

	if (!test_many_files(cli, mem_ctx)) {
		ret = False;
	}

	if (!test_sorted(cli, mem_ctx)) {
		ret = False;
	}

	if (!test_modify_search(cli, mem_ctx)) {
		ret = False;
	}

	torture_close_connection(cli);
	talloc_destroy(mem_ctx);
	
	return ret;
}