665360fc64344cfc2e3101540095907e5393b048
[jelmer/samba4-debian.git] / source / 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 3 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, see <http://www.gnu.org/licenses/>.
18 */
19
20 #include "includes.h"
21 #include "torture/torture.h"
22 #include "system/filesys.h"
23 #include "system/dir.h"
24 #include "lib/tdb/include/tdb.h"
25 #include "lib/util/util_tdb.h"
26 #include "libcli/libcli.h"
27 #include "torture/util.h"
28 #include "pstring.h"
29
30 static TDB_CONTEXT *tdb;
31
32 #define NAME_LENGTH 20
33
34 static uint_t total, collisions, failures;
35
36 static BOOL test_one(struct smbcli_state *cli, const char *name)
37 {
38         int fnum;
39         const char *shortname;
40         fstring name2;
41         NTSTATUS status;
42         TDB_DATA data;
43
44         total++;
45
46         fnum = smbcli_open(cli->tree, name, O_RDWR|O_CREAT|O_EXCL, DENY_NONE);
47         if (fnum == -1) {
48                 printf("open of %s failed (%s)\n", name, smbcli_errstr(cli->tree));
49                 return False;
50         }
51
52         if (NT_STATUS_IS_ERR(smbcli_close(cli->tree, fnum))) {
53                 printf("close of %s failed (%s)\n", name, smbcli_errstr(cli->tree));
54                 return False;
55         }
56
57         /* get the short name */
58         status = smbcli_qpathinfo_alt_name(cli->tree, name, &shortname);
59         if (!NT_STATUS_IS_OK(status)) {
60                 printf("query altname of %s failed (%s)\n", name, smbcli_errstr(cli->tree));
61                 return False;
62         }
63
64         snprintf(name2, sizeof(name2), "\\mangle_test\\%s", shortname);
65         if (NT_STATUS_IS_ERR(smbcli_unlink(cli->tree, name2))) {
66                 printf("unlink of %s  (%s) failed (%s)\n", 
67                        name2, name, smbcli_errstr(cli->tree));
68                 return False;
69         }
70
71         /* recreate by short name */
72         fnum = smbcli_open(cli->tree, name2, O_RDWR|O_CREAT|O_EXCL, DENY_NONE);
73         if (fnum == -1) {
74                 printf("open2 of %s failed (%s)\n", name2, smbcli_errstr(cli->tree));
75                 return False;
76         }
77         if (NT_STATUS_IS_ERR(smbcli_close(cli->tree, fnum))) {
78                 printf("close of %s failed (%s)\n", name, smbcli_errstr(cli->tree));
79                 return False;
80         }
81
82         /* and unlink by long name */
83         if (NT_STATUS_IS_ERR(smbcli_unlink(cli->tree, name))) {
84                 printf("unlink2 of %s  (%s) failed (%s)\n", 
85                        name, name2, smbcli_errstr(cli->tree));
86                 failures++;
87                 smbcli_unlink(cli->tree, name2);
88                 return True;
89         }
90
91         /* see if the short name is already in the tdb */
92         data = tdb_fetch_bystring(tdb, shortname);
93         if (data.dptr) {
94                 /* maybe its a duplicate long name? */
95                 if (strcasecmp(name, (const char *)data.dptr) != 0) {
96                         /* we have a collision */
97                         collisions++;
98                         printf("Collision between %s and %s   ->  %s "
99                                 " (coll/tot: %u/%u)\n", 
100                                 name, data.dptr, shortname, collisions, total);
101                 }
102                 free(data.dptr);
103         } else {
104                 TDB_DATA namedata;
105                 /* store it for later */
106                 namedata.dptr = discard_const_p(uint8_t, name);
107                 namedata.dsize = strlen(name)+1;
108                 tdb_store_bystring(tdb, shortname, namedata, TDB_REPLACE);
109         }
110
111         return True;
112 }
113
114
115 static void gen_name(char *name)
116 {
117         const char *chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz._-$~...";
118         uint_t max_idx = strlen(chars);
119         uint_t len;
120         int i;
121         char *p;
122
123         fstrcpy(name, "\\mangle_test\\");
124         p = name + strlen(name);
125
126         len = 1 + random() % NAME_LENGTH;
127         
128         for (i=0;i<len;i++) {
129                 p[i] = chars[random() % max_idx];
130         }
131
132         p[i] = 0;
133
134         if (ISDOT(p) || ISDOTDOT(p)) {
135                 p[0] = '_';
136         }
137
138         /* have a high probability of a common lead char */
139         if (random() % 2 == 0) {
140                 p[0] = 'A';
141         }
142
143         /* and a medium probability of a common lead string */
144         if (random() % 10 == 0) {
145                 strncpy(p, "ABCDE", 5);
146         }
147
148         /* and a high probability of a good extension length */
149         if (random() % 2 == 0) {
150                 char *s = strrchr(p, '.');
151                 if (s) {
152                         s[4] = 0;
153                 }
154         }
155 }
156
157
158 BOOL torture_mangle(struct torture_context *torture, 
159                                         struct smbcli_state *cli)
160 {
161         extern int torture_numops;
162         int i;
163
164         /* we will use an internal tdb to store the names we have used */
165         tdb = tdb_open(NULL, 100000, TDB_INTERNAL, 0, 0);
166         if (!tdb) {
167                 printf("ERROR: Failed to open tdb\n");
168                 return False;
169         }
170
171         if (!torture_setup_dir(cli, "\\mangle_test")) {
172                 return False;
173         }
174
175         for (i=0;i<torture_numops;i++) {
176                 fstring name;
177
178                 ZERO_STRUCT(name);
179
180                 gen_name(name);
181
182                 if (!test_one(cli, name)) {
183                         break;
184                 }
185                 if (total && total % 100 == 0) {
186                         if (torture_setting_bool(torture, "progress", true)) {
187                                 printf("collisions %u/%u  - %.2f%%   (%u failures)\r",
188                                        collisions, total, (100.0*collisions) / total, failures);
189                         }
190                 }
191         }
192
193         smbcli_unlink(cli->tree, "\\mangle_test\\*");
194         if (NT_STATUS_IS_ERR(smbcli_rmdir(cli->tree, "\\mangle_test"))) {
195                 printf("ERROR: Failed to remove directory\n");
196                 return False;
197         }
198
199         printf("\nTotal collisions %u/%u  - %.2f%%   (%u failures)\n",
200                collisions, total, (100.0*collisions) / total, failures);
201
202         return (failures == 0);
203 }