r22969: fix some more places where we could end up with more than one event
[sfrench/samba-autobuild/.git] / source4 / torture / basic / mangle_test.c
1 /* 
2    Unix SMB/CIFS implementation.
3    SMB torture tester - mangling test
4    Copyright (C) Andrew Tridgell 2002
5    
6    This program is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 2 of the License, or
9    (at your option) any later version.
10    
11    This program is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15    
16    You should have received a copy of the GNU General Public License
17    along with this program; if not, write to the Free Software
18    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 */
20
21 #include "includes.h"
22 #include "torture/torture.h"
23 #include "system/filesys.h"
24 #include "system/dir.h"
25 #include "lib/tdb/include/tdb.h"
26 #include "lib/util/util_tdb.h"
27 #include "libcli/libcli.h"
28 #include "torture/util.h"
29 #include "pstring.h"
30
31 static TDB_CONTEXT *tdb;
32
33 #define NAME_LENGTH 20
34
35 static uint_t total, collisions, failures;
36
37 static BOOL test_one(struct smbcli_state *cli, const char *name)
38 {
39         int fnum;
40         const char *shortname;
41         fstring name2;
42         NTSTATUS status;
43         TDB_DATA data;
44
45         total++;
46
47         fnum = smbcli_open(cli->tree, name, O_RDWR|O_CREAT|O_EXCL, DENY_NONE);
48         if (fnum == -1) {
49                 printf("open of %s failed (%s)\n", name, smbcli_errstr(cli->tree));
50                 return False;
51         }
52
53         if (NT_STATUS_IS_ERR(smbcli_close(cli->tree, fnum))) {
54                 printf("close of %s failed (%s)\n", name, smbcli_errstr(cli->tree));
55                 return False;
56         }
57
58         /* get the short name */
59         status = smbcli_qpathinfo_alt_name(cli->tree, name, &shortname);
60         if (!NT_STATUS_IS_OK(status)) {
61                 printf("query altname of %s failed (%s)\n", name, smbcli_errstr(cli->tree));
62                 return False;
63         }
64
65         snprintf(name2, sizeof(name2), "\\mangle_test\\%s", shortname);
66         if (NT_STATUS_IS_ERR(smbcli_unlink(cli->tree, name2))) {
67                 printf("unlink of %s  (%s) failed (%s)\n", 
68                        name2, name, smbcli_errstr(cli->tree));
69                 return False;
70         }
71
72         /* recreate by short name */
73         fnum = smbcli_open(cli->tree, name2, O_RDWR|O_CREAT|O_EXCL, DENY_NONE);
74         if (fnum == -1) {
75                 printf("open2 of %s failed (%s)\n", name2, smbcli_errstr(cli->tree));
76                 return False;
77         }
78         if (NT_STATUS_IS_ERR(smbcli_close(cli->tree, fnum))) {
79                 printf("close of %s failed (%s)\n", name, smbcli_errstr(cli->tree));
80                 return False;
81         }
82
83         /* and unlink by long name */
84         if (NT_STATUS_IS_ERR(smbcli_unlink(cli->tree, name))) {
85                 printf("unlink2 of %s  (%s) failed (%s)\n", 
86                        name, name2, smbcli_errstr(cli->tree));
87                 failures++;
88                 smbcli_unlink(cli->tree, name2);
89                 return True;
90         }
91
92         /* see if the short name is already in the tdb */
93         data = tdb_fetch_bystring(tdb, shortname);
94         if (data.dptr) {
95                 /* maybe its a duplicate long name? */
96                 if (strcasecmp(name, (const char *)data.dptr) != 0) {
97                         /* we have a collision */
98                         collisions++;
99                         printf("Collision between %s and %s   ->  %s "
100                                 " (coll/tot: %u/%u)\n", 
101                                 name, data.dptr, shortname, collisions, total);
102                 }
103                 free(data.dptr);
104         } else {
105                 TDB_DATA namedata;
106                 /* store it for later */
107                 namedata.dptr = discard_const_p(uint8_t, name);
108                 namedata.dsize = strlen(name)+1;
109                 tdb_store_bystring(tdb, shortname, namedata, TDB_REPLACE);
110         }
111
112         return True;
113 }
114
115
116 static void gen_name(char *name)
117 {
118         const char *chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz._-$~...";
119         uint_t max_idx = strlen(chars);
120         uint_t len;
121         int i;
122         char *p;
123
124         fstrcpy(name, "\\mangle_test\\");
125         p = name + strlen(name);
126
127         len = 1 + random() % NAME_LENGTH;
128         
129         for (i=0;i<len;i++) {
130                 p[i] = chars[random() % max_idx];
131         }
132
133         p[i] = 0;
134
135         if (ISDOT(p) || ISDOTDOT(p)) {
136                 p[0] = '_';
137         }
138
139         /* have a high probability of a common lead char */
140         if (random() % 2 == 0) {
141                 p[0] = 'A';
142         }
143
144         /* and a medium probability of a common lead string */
145         if (random() % 10 == 0) {
146                 strncpy(p, "ABCDE", 5);
147         }
148
149         /* and a high probability of a good extension length */
150         if (random() % 2 == 0) {
151                 char *s = strrchr(p, '.');
152                 if (s) {
153                         s[4] = 0;
154                 }
155         }
156 }
157
158
159 BOOL torture_mangle(struct torture_context *torture, 
160                                         struct smbcli_state *cli)
161 {
162         extern int torture_numops;
163         int i;
164
165         /* we will use an internal tdb to store the names we have used */
166         tdb = tdb_open(NULL, 100000, TDB_INTERNAL, 0, 0);
167         if (!tdb) {
168                 printf("ERROR: Failed to open tdb\n");
169                 return False;
170         }
171
172         if (!torture_setup_dir(cli, "\\mangle_test")) {
173                 return False;
174         }
175
176         for (i=0;i<torture_numops;i++) {
177                 fstring name;
178
179                 ZERO_STRUCT(name);
180
181                 gen_name(name);
182
183                 if (!test_one(cli, name)) {
184                         break;
185                 }
186                 if (total && total % 100 == 0) {
187                         if (torture_setting_bool(torture, "progress", true)) {
188                                 printf("collisions %u/%u  - %.2f%%   (%u failures)\r",
189                                        collisions, total, (100.0*collisions) / total, failures);
190                         }
191                 }
192         }
193
194         smbcli_unlink(cli->tree, "\\mangle_test\\*");
195         if (NT_STATUS_IS_ERR(smbcli_rmdir(cli->tree, "\\mangle_test"))) {
196                 printf("ERROR: Failed to remove directory\n");
197                 return False;
198         }
199
200         printf("\nTotal collisions %u/%u  - %.2f%%   (%u failures)\n",
201                collisions, total, (100.0*collisions) / total, failures);
202
203         return (failures == 0);
204 }