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/>.
21 #include "system/filesys.h"
22 #include "torture/proto.h"
23 #include "libsmb/libsmb.h"
24 #include "libsmb/clirap.h"
27 extern int torture_numops
;
29 static TDB_CONTEXT
*tdb
;
31 #define NAME_LENGTH 20
33 static unsigned total
, collisions
, failures
;
35 static bool test_one(struct cli_state
*cli
, const char *name
)
45 status
= cli_open(cli
, name
, O_RDWR
|O_CREAT
|O_EXCL
, DENY_NONE
, &fnum
);
46 if (!NT_STATUS_IS_OK(status
)) {
47 printf("open of %s failed (%s)\n", name
, nt_errstr(status
));
51 status
= cli_close(cli
, fnum
);
52 if (!NT_STATUS_IS_OK(status
)) {
53 printf("close of %s failed (%s)\n", name
, nt_errstr(status
));
57 /* get the short name */
58 status
= cli_qpathinfo_alt_name(cli
, name
, shortname
);
59 if (!NT_STATUS_IS_OK(status
)) {
60 printf("query altname of %s failed (%s)\n", name
, nt_errstr(status
));
64 fstr_sprintf(name2
, "\\mangle_test\\%s", shortname
);
65 status
= cli_unlink(cli
, name2
, FILE_ATTRIBUTE_SYSTEM
| FILE_ATTRIBUTE_HIDDEN
);
66 if (!NT_STATUS_IS_OK(status
)) {
67 printf("unlink of %s (%s) failed (%s)\n",
68 name2
, name
, nt_errstr(status
));
72 /* recreate by short name */
73 status
= cli_open(cli
, name2
, O_RDWR
|O_CREAT
|O_EXCL
, DENY_NONE
, &fnum
);
74 if (!NT_STATUS_IS_OK(status
)) {
75 printf("open2 of %s failed (%s)\n", name2
, nt_errstr(status
));
79 status
= cli_close(cli
, fnum
);
80 if (!NT_STATUS_IS_OK(status
)) {
81 printf("close of %s failed (%s)\n", name
, nt_errstr(status
));
85 /* and unlink by long name */
86 status
= cli_unlink(cli
, name
, FILE_ATTRIBUTE_SYSTEM
| FILE_ATTRIBUTE_HIDDEN
);
87 if (!NT_STATUS_IS_OK(status
)) {
88 printf("unlink2 of %s (%s) failed (%s)\n",
89 name
, name2
, nt_errstr(status
));
91 cli_unlink(cli
, name2
, FILE_ATTRIBUTE_SYSTEM
| FILE_ATTRIBUTE_HIDDEN
);
95 /* see if the short name is already in the tdb */
96 data
= tdb_fetch_bystring(tdb
, shortname
);
98 /* maybe its a duplicate long name? */
99 if (!strequal(name
, (const char *)data
.dptr
)) {
100 /* we have a collision */
102 printf("Collision between %s and %s -> %s "
103 " (coll/tot: %u/%u)\n",
104 name
, data
.dptr
, shortname
, collisions
, total
);
109 /* store it for later */
110 namedata
.dptr
= discard_const_p(uint8
, name
);
111 namedata
.dsize
= strlen(name
)+1;
112 tdb_store_bystring(tdb
, shortname
, namedata
, TDB_REPLACE
);
119 static void gen_name(char *name
)
121 const char *chars
= "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz._-$~... ";
122 unsigned max_idx
= strlen(chars
);
127 fstrcpy(name
, "\\mangle_test\\");
128 p
= name
+ strlen(name
);
130 len
= 1 + random() % NAME_LENGTH
;
132 for (i
=0;i
<len
;i
++) {
133 p
[i
] = chars
[random() % max_idx
];
138 if (strcmp(p
, ".") == 0 || strcmp(p
, "..") == 0) {
142 /* have a high probability of a common lead char */
143 if (random() % 2 == 0) {
147 /* and a medium probability of a common lead string */
148 if (random() % 10 == 0) {
149 if (strlen(p
) <= 5) {
152 /* try not to kill off the null termination */
153 memcpy(p
, "ABCDE", 5);
157 /* and a high probability of a good extension length */
158 if (random() % 2 == 0) {
159 char *s
= strrchr(p
, '.');
165 /* ..... and a 100% proability of a file not ending in "." */
166 if (p
[strlen(p
)-1] == '.')
167 p
[strlen(p
)-1] = '_';
171 bool torture_mangle(int dummy
)
173 static struct cli_state
*cli
;
177 printf("starting mangle test\n");
179 if (!torture_open_connection(&cli
, 0)) {
183 /* we will use an internal tdb to store the names we have used */
184 tdb
= tdb_open(NULL
, 100000, TDB_INTERNAL
, 0, 0);
186 printf("ERROR: Failed to open tdb\n");
190 cli_unlink(cli
, "\\mangle_test\\*", FILE_ATTRIBUTE_SYSTEM
| FILE_ATTRIBUTE_HIDDEN
);
191 cli_rmdir(cli
, "\\mangle_test");
193 if (!NT_STATUS_IS_OK(cli_mkdir(cli
, "\\mangle_test"))) {
194 printf("ERROR: Failed to make directory\n");
198 for (i
=0;i
<torture_numops
;i
++) {
204 if (!test_one(cli
, name
)) {
208 if (total
&& total
% 100 == 0) {
209 printf("collisions %u/%u - %.2f%% (%u failures)\r",
210 collisions
, total
, (100.0*collisions
) / total
, failures
);
214 cli_unlink(cli
, "\\mangle_test\\*", FILE_ATTRIBUTE_SYSTEM
| FILE_ATTRIBUTE_HIDDEN
);
215 if (!NT_STATUS_IS_OK(cli_rmdir(cli
, "\\mangle_test"))) {
216 printf("ERROR: Failed to remove directory\n");
220 printf("\nTotal collisions %u/%u - %.2f%% (%u failures)\n",
221 collisions
, total
, (100.0*collisions
) / total
, failures
);
223 torture_close_connection(cli
);
225 printf("mangle test finished\n");
226 return (ret
&& (failures
== 0));