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