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