r14464: Don't include ndr_BASENAME.h files unless strictly required, instead
[kai/samba-autobuild/.git] / source4 / torture / basic / dir.c
1 /* 
2    Unix SMB/CIFS implementation.
3
4    directory scanning tests
5
6    Copyright (C) Andrew Tridgell 2003
7    
8    This program is free software; you can redistribute it and/or modify
9    it under the terms of the GNU General Public License as published by
10    the Free Software Foundation; either version 2 of the License, or
11    (at your option) any later version.
12    
13    This program is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16    GNU General Public License for more details.
17    
18    You should have received a copy of the GNU General Public License
19    along with this program; if not, write to the Free Software
20    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 */
22
23 #include "includes.h"
24 #include "libcli/libcli.h"
25 #include "torture/torture.h"
26 #include "system/filesys.h"
27
28 static void list_fn(struct clilist_file_info *finfo, const char *name, void *state)
29 {
30         
31 }
32
33 /*
34   test directory listing speed
35  */
36 BOOL torture_dirtest1(void)
37 {
38         int i;
39         struct smbcli_state *cli;
40         int fnum;
41         BOOL correct = True;
42         extern int torture_numops;
43         struct timeval tv;
44
45         printf("starting dirtest1\n");
46
47         if (!torture_open_connection(&cli)) {
48                 return False;
49         }
50
51         printf("Creating %d random filenames\n", torture_numops);
52
53         srandom(0);
54         tv = timeval_current();
55         for (i=0;i<torture_numops;i++) {
56                 char *fname;
57                 asprintf(&fname, "\\%x", (int)random());
58                 fnum = smbcli_open(cli->tree, fname, O_RDWR|O_CREAT, DENY_NONE);
59                 if (fnum == -1) {
60                         fprintf(stderr,"(%s) Failed to open %s\n", 
61                                 __location__, fname);
62                         return False;
63                 }
64                 smbcli_close(cli->tree, fnum);
65                 free(fname);
66         }
67
68         printf("Matched %d\n", smbcli_list(cli->tree, "a*.*", 0, list_fn, NULL));
69         printf("Matched %d\n", smbcli_list(cli->tree, "b*.*", 0, list_fn, NULL));
70         printf("Matched %d\n", smbcli_list(cli->tree, "xyzabc", 0, list_fn, NULL));
71
72         printf("dirtest core %g seconds\n", timeval_elapsed(&tv));
73
74         srandom(0);
75         for (i=0;i<torture_numops;i++) {
76                 char *fname;
77                 asprintf(&fname, "\\%x", (int)random());
78                 smbcli_unlink(cli->tree, fname);
79                 free(fname);
80         }
81
82         if (!torture_close_connection(cli)) {
83                 correct = False;
84         }
85
86         printf("finished dirtest1\n");
87
88         return correct;
89 }
90
91 BOOL torture_dirtest2(void)
92 {
93         int i;
94         struct smbcli_state *cli;
95         int fnum, num_seen;
96         BOOL correct = True;
97         extern int torture_entries;
98
99         printf("starting dirtest2\n");
100
101         if (!torture_open_connection(&cli)) {
102                 return False;
103         }
104
105         if (!torture_setup_dir(cli, "\\LISTDIR")) {
106                 return False;
107         }
108
109         printf("Creating %d files\n", torture_entries);
110
111         /* Create torture_entries files and torture_entries directories. */
112         for (i=0;i<torture_entries;i++) {
113                 char *fname;
114                 asprintf(&fname, "\\LISTDIR\\f%d", i);
115                 fnum = smbcli_nt_create_full(cli->tree, fname, 0, 
116                                              SEC_RIGHTS_FILE_ALL,
117                                              FILE_ATTRIBUTE_ARCHIVE,
118                                              NTCREATEX_SHARE_ACCESS_READ|NTCREATEX_SHARE_ACCESS_WRITE, 
119                                              NTCREATEX_DISP_OVERWRITE_IF, 0, 0);
120                 if (fnum == -1) {
121                         fprintf(stderr,"(%s) Failed to open %s, error=%s\n", 
122                                 __location__, fname, smbcli_errstr(cli->tree));
123                         return False;
124                 }
125                 free(fname);
126                 smbcli_close(cli->tree, fnum);
127         }
128         for (i=0;i<torture_entries;i++) {
129                 char *fname;
130                 asprintf(&fname, "\\LISTDIR\\d%d", i);
131                 if (NT_STATUS_IS_ERR(smbcli_mkdir(cli->tree, fname))) {
132                         fprintf(stderr,"(%s) Failed to open %s, error=%s\n", 
133                                 __location__, fname, smbcli_errstr(cli->tree));
134                         return False;
135                 }
136                 free(fname);
137         }
138
139         /* Now ensure that doing an old list sees both files and directories. */
140         num_seen = smbcli_list_old(cli->tree, "\\LISTDIR\\*", FILE_ATTRIBUTE_DIRECTORY, list_fn, NULL);
141         printf("num_seen = %d\n", num_seen );
142         /* We should see (torture_entries) each of files & directories + . and .. */
143         if (num_seen != (2*torture_entries)+2) {
144                 correct = False;
145                 fprintf(stderr,"(%s) entry count mismatch, should be %d, was %d\n",
146                         __location__, (2*torture_entries)+2, num_seen);
147         }
148                 
149
150         /* Ensure if we have the "must have" bits we only see the
151          * relevant entries.
152          */
153         num_seen = smbcli_list_old(cli->tree, "\\LISTDIR\\*", (FILE_ATTRIBUTE_DIRECTORY<<8)|FILE_ATTRIBUTE_DIRECTORY, list_fn, NULL);
154         printf("num_seen = %d\n", num_seen );
155         if (num_seen != torture_entries+2) {
156                 correct = False;
157                 fprintf(stderr,"(%s) entry count mismatch, should be %d, was %d\n",
158                         __location__, torture_entries+2, num_seen);
159         }
160
161         num_seen = smbcli_list_old(cli->tree, "\\LISTDIR\\*", (FILE_ATTRIBUTE_ARCHIVE<<8)|FILE_ATTRIBUTE_DIRECTORY, list_fn, NULL);
162         printf("num_seen = %d\n", num_seen );
163         if (num_seen != torture_entries) {
164                 correct = False;
165                 fprintf(stderr,"(%s) entry count mismatch, should be %d, was %d\n",
166                         __location__, torture_entries, num_seen);
167         }
168
169         /* Delete everything. */
170         if (smbcli_deltree(cli->tree, "\\LISTDIR") == -1) {
171                 fprintf(stderr,"(%s) Failed to deltree %s, error=%s\n", "\\LISTDIR", 
172                         __location__, smbcli_errstr(cli->tree));
173                 return False;
174         }
175
176 #if 0
177         printf("Matched %d\n", smbcli_list(cli->tree, "a*.*", 0, list_fn, NULL));
178         printf("Matched %d\n", smbcli_list(cli->tree, "b*.*", 0, list_fn, NULL));
179         printf("Matched %d\n", smbcli_list(cli->tree, "xyzabc", 0, list_fn, NULL));
180 #endif
181
182         if (!torture_close_connection(cli)) {
183                 correct = False;
184         }
185
186         printf("finished dirtest1\n");
187
188         return correct;
189 }