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