d7721ce5ae5f93b5648ab8193d07f7ee7420e2eb
[samba.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 "librpc/gen_ndr/ndr_security.h"
25
26 static void list_fn(struct clilist_file_info *finfo, const char *name, void *state)
27 {
28         
29 }
30
31 /*
32   test directory listing speed
33  */
34 BOOL torture_dirtest1(void)
35 {
36         int i;
37         struct smbcli_state *cli;
38         int fnum;
39         BOOL correct = True;
40         extern int torture_numops;
41         struct timeval tv;
42
43         printf("starting dirtest1\n");
44
45         if (!torture_open_connection(&cli)) {
46                 return False;
47         }
48
49         printf("Creating %d random filenames\n", torture_numops);
50
51         srandom(0);
52         tv = timeval_current();
53         for (i=0;i<torture_numops;i++) {
54                 char *fname;
55                 asprintf(&fname, "\\%x", (int)random());
56                 fnum = smbcli_open(cli->tree, fname, O_RDWR|O_CREAT, DENY_NONE);
57                 if (fnum == -1) {
58                         fprintf(stderr,"(%s) Failed to open %s\n", 
59                                 __location__, fname);
60                         return False;
61                 }
62                 smbcli_close(cli->tree, fnum);
63                 free(fname);
64         }
65
66         printf("Matched %d\n", smbcli_list(cli->tree, "a*.*", 0, list_fn, NULL));
67         printf("Matched %d\n", smbcli_list(cli->tree, "b*.*", 0, list_fn, NULL));
68         printf("Matched %d\n", smbcli_list(cli->tree, "xyzabc", 0, list_fn, NULL));
69
70         printf("dirtest core %g seconds\n", timeval_elapsed(&tv));
71
72         srandom(0);
73         for (i=0;i<torture_numops;i++) {
74                 char *fname;
75                 asprintf(&fname, "\\%x", (int)random());
76                 smbcli_unlink(cli->tree, fname);
77                 free(fname);
78         }
79
80         if (!torture_close_connection(cli)) {
81                 correct = False;
82         }
83
84         printf("finished dirtest1\n");
85
86         return correct;
87 }
88
89 BOOL torture_dirtest2(void)
90 {
91         int i;
92         struct smbcli_state *cli;
93         int fnum, num_seen;
94         BOOL correct = True;
95         extern int torture_entries;
96
97         printf("starting dirtest2\n");
98
99         if (!torture_open_connection(&cli)) {
100                 return False;
101         }
102
103         if (!torture_setup_dir(cli, "\\LISTDIR")) {
104                 return False;
105         }
106
107         printf("Creating %d files\n", torture_entries);
108
109         /* Create torture_entries files and torture_entries directories. */
110         for (i=0;i<torture_entries;i++) {
111                 char *fname;
112                 asprintf(&fname, "\\LISTDIR\\f%d", i);
113                 fnum = smbcli_nt_create_full(cli->tree, fname, 0, 
114                                              SEC_RIGHTS_FILE_ALL,
115                                              FILE_ATTRIBUTE_ARCHIVE,
116                                              NTCREATEX_SHARE_ACCESS_READ|NTCREATEX_SHARE_ACCESS_WRITE, 
117                                              NTCREATEX_DISP_OVERWRITE_IF, 0, 0);
118                 if (fnum == -1) {
119                         fprintf(stderr,"(%s) Failed to open %s, error=%s\n", 
120                                 __location__, fname, smbcli_errstr(cli->tree));
121                         return False;
122                 }
123                 free(fname);
124                 smbcli_close(cli->tree, fnum);
125         }
126         for (i=0;i<torture_entries;i++) {
127                 char *fname;
128                 asprintf(&fname, "\\LISTDIR\\d%d", i);
129                 if (NT_STATUS_IS_ERR(smbcli_mkdir(cli->tree, fname))) {
130                         fprintf(stderr,"(%s) Failed to open %s, error=%s\n", 
131                                 __location__, fname, smbcli_errstr(cli->tree));
132                         return False;
133                 }
134                 free(fname);
135         }
136
137         /* Now ensure that doing an old list sees both files and directories. */
138         num_seen = smbcli_list_old(cli->tree, "\\LISTDIR\\*", FILE_ATTRIBUTE_DIRECTORY, list_fn, NULL);
139         printf("num_seen = %d\n", num_seen );
140         /* We should see (torture_entries) each of files & directories + . and .. */
141         if (num_seen != (2*torture_entries)+2) {
142                 correct = False;
143                 fprintf(stderr,"(%s) entry count mismatch, should be %d, was %d\n",
144                         __location__, (2*torture_entries)+2, num_seen);
145         }
146                 
147
148         /* Ensure if we have the "must have" bits we only see the
149          * relevant entries.
150          */
151         num_seen = smbcli_list_old(cli->tree, "\\LISTDIR\\*", (FILE_ATTRIBUTE_DIRECTORY<<8)|FILE_ATTRIBUTE_DIRECTORY, list_fn, NULL);
152         printf("num_seen = %d\n", num_seen );
153         if (num_seen != torture_entries+2) {
154                 correct = False;
155                 fprintf(stderr,"(%s) entry count mismatch, should be %d, was %d\n",
156                         __location__, torture_entries+2, num_seen);
157         }
158
159         num_seen = smbcli_list_old(cli->tree, "\\LISTDIR\\*", (FILE_ATTRIBUTE_ARCHIVE<<8)|FILE_ATTRIBUTE_DIRECTORY, list_fn, NULL);
160         printf("num_seen = %d\n", num_seen );
161         if (num_seen != torture_entries) {
162                 correct = False;
163                 fprintf(stderr,"(%s) entry count mismatch, should be %d, was %d\n",
164                         __location__, torture_entries, num_seen);
165         }
166
167         /* Delete everything. */
168         if (smbcli_deltree(cli->tree, "\\LISTDIR") == -1) {
169                 fprintf(stderr,"(%s) Failed to deltree %s, error=%s\n", "\\LISTDIR", 
170                         __location__, smbcli_errstr(cli->tree));
171                 return False;
172         }
173
174 #if 0
175         printf("Matched %d\n", smbcli_list(cli->tree, "a*.*", 0, list_fn, NULL));
176         printf("Matched %d\n", smbcli_list(cli->tree, "b*.*", 0, list_fn, NULL));
177         printf("Matched %d\n", smbcli_list(cli->tree, "xyzabc", 0, list_fn, NULL));
178 #endif
179
180         if (!torture_close_connection(cli)) {
181                 correct = False;
182         }
183
184         printf("finished dirtest1\n");
185
186         return correct;
187 }