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