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