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