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