e5f69479a502d624dc6966c733e5a7e5a2f5ec45
[gd/samba/.git] / source3 / torture / 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.h"
22
23 extern int torture_numops;
24
25 static TDB_CONTEXT *tdb;
26
27 #define NAME_LENGTH 20
28
29 static unsigned total, collisions, failures;
30
31 static bool test_one(struct cli_state *cli, const char *name)
32 {
33         uint16_t fnum;
34         fstring shortname;
35         fstring name2;
36         NTSTATUS status;
37         TDB_DATA data;
38
39         total++;
40
41         if (!NT_STATUS_IS_OK(cli_open(cli, name, O_RDWR|O_CREAT|O_EXCL, DENY_NONE, &fnum))) {
42                 printf("open of %s failed (%s)\n", name, cli_errstr(cli));
43                 return False;
44         }
45
46         if (!NT_STATUS_IS_OK(cli_close(cli, fnum))) {
47                 printf("close of %s failed (%s)\n", name, cli_errstr(cli));
48                 return False;
49         }
50
51         /* get the short name */
52         status = cli_qpathinfo_alt_name(cli, name, shortname);
53         if (!NT_STATUS_IS_OK(status)) {
54                 printf("query altname of %s failed (%s)\n", name, cli_errstr(cli));
55                 return False;
56         }
57
58         fstr_sprintf(name2, "\\mangle_test\\%s", shortname);
59         if (!NT_STATUS_IS_OK(cli_unlink(cli, name2, aSYSTEM | aHIDDEN))) {
60                 printf("unlink of %s  (%s) failed (%s)\n", 
61                        name2, name, cli_errstr(cli));
62                 return False;
63         }
64
65         /* recreate by short name */
66         if (!NT_STATUS_IS_OK(cli_open(cli, name2, O_RDWR|O_CREAT|O_EXCL, DENY_NONE, &fnum))) {
67                 printf("open2 of %s failed (%s)\n", name2, cli_errstr(cli));
68                 return False;
69         }
70         if (!NT_STATUS_IS_OK(cli_close(cli, fnum))) {
71                 printf("close of %s failed (%s)\n", name, cli_errstr(cli));
72                 return False;
73         }
74
75         /* and unlink by long name */
76         if (!NT_STATUS_IS_OK(cli_unlink(cli, name, aSYSTEM | aHIDDEN))) {
77                 printf("unlink2 of %s  (%s) failed (%s)\n", 
78                        name, name2, cli_errstr(cli));
79                 failures++;
80                 cli_unlink(cli, name2, aSYSTEM | aHIDDEN);
81                 return True;
82         }
83
84         /* see if the short name is already in the tdb */
85         data = tdb_fetch_bystring(tdb, shortname);
86         if (data.dptr) {
87                 /* maybe its a duplicate long name? */
88                 if (!strequal(name, (const char *)data.dptr)) {
89                         /* we have a collision */
90                         collisions++;
91                         printf("Collision between %s and %s   ->  %s "
92                                 " (coll/tot: %u/%u)\n", 
93                                 name, data.dptr, shortname, collisions, total);
94                 }
95                 free(data.dptr);
96         } else {
97                 TDB_DATA namedata;
98                 /* store it for later */
99                 namedata.dptr = CONST_DISCARD(uint8 *, name);
100                 namedata.dsize = strlen(name)+1;
101                 tdb_store_bystring(tdb, shortname, namedata, TDB_REPLACE);
102         }
103
104         return True;
105 }
106
107
108 static void gen_name(char *name)
109 {
110         const char *chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz._-$~... ";
111         unsigned max_idx = strlen(chars);
112         unsigned len;
113         int i;
114         char *p;
115
116         fstrcpy(name, "\\mangle_test\\");
117         p = name + strlen(name);
118
119         len = 1 + random() % NAME_LENGTH;
120         
121         for (i=0;i<len;i++) {
122                 p[i] = chars[random() % max_idx];
123         }
124
125         p[i] = 0;
126
127         if (strcmp(p, ".") == 0 || strcmp(p, "..") == 0) {
128                 p[0] = '_';
129         }
130
131         /* have a high probability of a common lead char */
132         if (random() % 2 == 0) {
133                 p[0] = 'A';
134         }
135
136         /* and a medium probability of a common lead string */
137         if (random() % 10 == 0) {
138                 if (strlen(p) <= 5) {
139                         fstrcpy(p, "ABCDE");
140                 } else {
141                         /* try not to kill off the null termination */
142                         memcpy(p, "ABCDE", 5);
143                 }
144         }
145
146         /* and a high probability of a good extension length */
147         if (random() % 2 == 0) {
148                 char *s = strrchr(p, '.');
149                 if (s) {
150                         s[4] = 0;
151                 }
152         }
153
154         /* ..... and a 100% proability of a file not ending in "." */
155         if (p[strlen(p)-1] == '.')
156                 p[strlen(p)-1] = '_';
157 }
158
159
160 bool torture_mangle(int dummy)
161 {
162         static struct cli_state *cli;
163         int i;
164         bool ret = True;
165
166         printf("starting mangle test\n");
167
168         if (!torture_open_connection(&cli, 0)) {
169                 return False;
170         }
171
172         /* we will use an internal tdb to store the names we have used */
173         tdb = tdb_open(NULL, 100000, TDB_INTERNAL, 0, 0);
174         if (!tdb) {
175                 printf("ERROR: Failed to open tdb\n");
176                 return False;
177         }
178
179         cli_unlink(cli, "\\mangle_test\\*", aSYSTEM | aHIDDEN);
180         cli_rmdir(cli, "\\mangle_test");
181
182         if (!NT_STATUS_IS_OK(cli_mkdir(cli, "\\mangle_test"))) {
183                 printf("ERROR: Failed to make directory\n");
184                 return False;
185         }
186
187         for (i=0;i<torture_numops;i++) {
188                 fstring name;
189                 ZERO_STRUCT(name);
190
191                 gen_name(name);
192                 
193                 if (!test_one(cli, name)) {
194                         ret = False;
195                         break;
196                 }
197                 if (total && total % 100 == 0) {
198                         printf("collisions %u/%u  - %.2f%%   (%u failures)\r",
199                                collisions, total, (100.0*collisions) / total, failures);
200                 }
201         }
202
203         cli_unlink(cli, "\\mangle_test\\*", aSYSTEM | aHIDDEN);
204         if (!NT_STATUS_IS_OK(cli_rmdir(cli, "\\mangle_test"))) {
205                 printf("ERROR: Failed to remove directory\n");
206                 return False;
207         }
208
209         printf("\nTotal collisions %u/%u  - %.2f%%   (%u failures)\n",
210                collisions, total, (100.0*collisions) / total, failures);
211
212         torture_close_connection(cli);
213
214         printf("mangle test finished\n");
215         return (ret && (failures == 0));
216 }