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