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