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