2 Unix SMB/CIFS implementation.
3 SMB torture tester - mangling test
4 Copyright (C) Andrew Tridgell 2002
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.
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.
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/>.
22 extern int torture_numops
;
24 static TDB_CONTEXT
*tdb
;
26 #define NAME_LENGTH 20
28 static unsigned total
, collisions
, failures
;
30 static bool test_one(struct cli_state
*cli
, const char *name
)
40 fnum
= cli_open(cli
, name
, O_RDWR
|O_CREAT
|O_EXCL
, DENY_NONE
);
42 printf("open of %s failed (%s)\n", name
, cli_errstr(cli
));
46 if (!cli_close(cli
, fnum
)) {
47 printf("close of %s failed (%s)\n", name
, cli_errstr(cli
));
51 /* get the short name */
52 status
= cli_qpathinfo_alt_name(cli
, name
, shortname
);
53 if (!NT_STATUS_IS_OK(status
)) {
54 printf("query altname of %s failed (%s)\n", name
, cli_errstr(cli
));
58 fstr_sprintf(name2
, "\\mangle_test\\%s", shortname
);
59 if (!cli_unlink(cli
, name2
)) {
60 printf("unlink of %s (%s) failed (%s)\n",
61 name2
, name
, cli_errstr(cli
));
65 /* recreate by short name */
66 fnum
= cli_open(cli
, name2
, O_RDWR
|O_CREAT
|O_EXCL
, DENY_NONE
);
68 printf("open2 of %s failed (%s)\n", name2
, cli_errstr(cli
));
71 if (!cli_close(cli
, fnum
)) {
72 printf("close of %s failed (%s)\n", name
, cli_errstr(cli
));
76 /* and unlink by long name */
77 if (!cli_unlink(cli
, name
)) {
78 printf("unlink2 of %s (%s) failed (%s)\n",
79 name
, name2
, cli_errstr(cli
));
81 cli_unlink(cli
, name2
);
85 /* see if the short name is already in the tdb */
86 data
= tdb_fetch_bystring(tdb
, shortname
);
88 /* maybe its a duplicate long name? */
89 if (!strequal(name
, (const char *)data
.dptr
)) {
90 /* we have a collision */
92 printf("Collision between %s and %s -> %s "
93 " (coll/tot: %u/%u)\n",
94 name
, data
.dptr
, shortname
, collisions
, total
);
99 /* store it for later */
100 namedata
.dptr
= CONST_DISCARD(uint8
*, name
);
101 namedata
.dsize
= strlen(name
)+1;
102 tdb_store_bystring(tdb
, shortname
, namedata
, TDB_REPLACE
);
109 static void gen_name(char *name
)
111 const char *chars
= "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz._-$~... ";
112 unsigned max_idx
= strlen(chars
);
117 fstrcpy(name
, "\\mangle_test\\");
118 p
= name
+ strlen(name
);
120 len
= 1 + random() % NAME_LENGTH
;
122 for (i
=0;i
<len
;i
++) {
123 p
[i
] = chars
[random() % max_idx
];
128 if (strcmp(p
, ".") == 0 || strcmp(p
, "..") == 0) {
132 /* have a high probability of a common lead char */
133 if (random() % 2 == 0) {
137 /* and a medium probability of a common lead string */
138 if (random() % 10 == 0) {
139 if (strlen(p
) <= 5) {
142 /* try not to kill off the null termination */
143 memcpy(p
, "ABCDE", 5);
147 /* and a high probability of a good extension length */
148 if (random() % 2 == 0) {
149 char *s
= strrchr(p
, '.');
155 /* ..... and a 100% proability of a file not ending in "." */
156 if (p
[strlen(p
)-1] == '.')
157 p
[strlen(p
)-1] = '_';
161 bool torture_mangle(int dummy
)
163 static struct cli_state
*cli
;
167 printf("starting mangle test\n");
169 if (!torture_open_connection(&cli
, 0)) {
173 /* we will use an internal tdb to store the names we have used */
174 tdb
= tdb_open(NULL
, 100000, TDB_INTERNAL
, 0, 0);
176 printf("ERROR: Failed to open tdb\n");
180 cli_unlink(cli
, "\\mangle_test\\*");
181 cli_rmdir(cli
, "\\mangle_test");
183 if (!cli_mkdir(cli
, "\\mangle_test")) {
184 printf("ERROR: Failed to make directory\n");
188 for (i
=0;i
<torture_numops
;i
++) {
194 if (!test_one(cli
, name
)) {
198 if (total
&& total
% 100 == 0) {
199 printf("collisions %u/%u - %.2f%% (%u failures)\r",
200 collisions
, total
, (100.0*collisions
) / total
, failures
);
204 cli_unlink(cli
, "\\mangle_test\\*");
205 if (!cli_rmdir(cli
, "\\mangle_test")) {
206 printf("ERROR: Failed to remove directory\n");
210 printf("\nTotal collisions %u/%u - %.2f%% (%u failures)\n",
211 collisions
, total
, (100.0*collisions
) / total
, failures
);
213 torture_close_connection(cli
);
215 printf("mangle test finished\n");
216 return (ret
&& (failures
== 0));