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 "system/dir.h"
24 #include "../lib/util/util_tdb.h"
25 #include "libcli/libcli.h"
26 #include "torture/util.h"
27 #include "torture/basic/proto.h"
31 static TDB_CONTEXT
*tdb
;
33 #define NAME_LENGTH 20
35 static unsigned int total
, collisions
, failures
;
37 static bool test_one(struct torture_context
*tctx
,struct smbcli_state
*cli
,
41 const char *shortname
;
48 fnum
= smbcli_open(cli
->tree
, name
, O_RDWR
|O_CREAT
|O_EXCL
, DENY_NONE
);
50 printf("open of %s failed (%s)\n", name
, smbcli_errstr(cli
->tree
));
54 if (NT_STATUS_IS_ERR(smbcli_close(cli
->tree
, fnum
))) {
55 printf("close of %s failed (%s)\n", name
, smbcli_errstr(cli
->tree
));
59 /* get the short name */
60 status
= smbcli_qpathinfo_alt_name(cli
->tree
, name
, &shortname
);
61 if (!NT_STATUS_IS_OK(status
)) {
62 printf("query altname of %s failed (%s)\n", name
, smbcli_errstr(cli
->tree
));
66 name2
= talloc_asprintf(tctx
, "\\mangle_test\\%s", shortname
);
67 if (NT_STATUS_IS_ERR(smbcli_unlink(cli
->tree
, name2
))) {
68 printf("unlink of %s (%s) failed (%s)\n",
69 name2
, name
, smbcli_errstr(cli
->tree
));
73 /* recreate by short name */
74 fnum
= smbcli_open(cli
->tree
, name2
, O_RDWR
|O_CREAT
|O_EXCL
, DENY_NONE
);
76 printf("open2 of %s failed (%s)\n", name2
, smbcli_errstr(cli
->tree
));
79 if (NT_STATUS_IS_ERR(smbcli_close(cli
->tree
, fnum
))) {
80 printf("close of %s failed (%s)\n", name
, smbcli_errstr(cli
->tree
));
84 /* and unlink by long name */
85 if (NT_STATUS_IS_ERR(smbcli_unlink(cli
->tree
, name
))) {
86 printf("unlink2 of %s (%s) failed (%s)\n",
87 name
, name2
, smbcli_errstr(cli
->tree
));
89 smbcli_unlink(cli
->tree
, name2
);
93 /* see if the short name is already in the tdb */
94 data
= tdb_fetch_bystring(tdb
, shortname
);
96 /* maybe its a duplicate long name? */
97 if (strcasecmp(name
, (const char *)data
.dptr
) != 0) {
98 /* we have a collision */
100 printf("Collision between %s and %s -> %s "
101 " (coll/tot: %u/%u)\n",
102 name
, data
.dptr
, shortname
, collisions
, total
);
107 /* store it for later */
108 namedata
.dptr
= discard_const_p(uint8_t, name
);
109 namedata
.dsize
= strlen(name
)+1;
110 tdb_store_bystring(tdb
, shortname
, namedata
, TDB_REPLACE
);
117 static char *gen_name(TALLOC_CTX
*mem_ctx
)
119 const char *chars
= "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz._-$~...";
120 unsigned int max_idx
= strlen(chars
);
126 name
= talloc_strdup(mem_ctx
, "\\mangle_test\\");
128 len
= 1 + random() % NAME_LENGTH
;
130 name
= talloc_realloc(mem_ctx
, name
, char, strlen(name
) + len
+ 6);
131 p
= name
+ strlen(name
);
133 for (i
=0;i
<len
;i
++) {
134 p
[i
] = chars
[random() % max_idx
];
139 if (ISDOT(p
) || ISDOTDOT(p
)) {
143 /* have a high probability of a common lead char */
144 if (random() % 2 == 0) {
148 /* and a medium probability of a common lead string */
149 if ((len
> 5) && (random() % 10 == 0)) {
150 strlcpy(p
, "ABCDE", 6);
153 /* and a high probability of a good extension length */
154 if (random() % 2 == 0) {
155 char *s
= strrchr(p
, '.');
165 bool torture_mangle(struct torture_context
*torture
,
166 struct smbcli_state
*cli
)
168 extern int torture_numops
;
171 /* we will use an internal tdb to store the names we have used */
172 tdb
= tdb_open(NULL
, 100000, TDB_INTERNAL
, 0, 0);
174 printf("ERROR: Failed to open tdb\n");
178 if (!torture_setup_dir(cli
, "\\mangle_test")) {
182 for (i
=0;i
<torture_numops
;i
++) {
185 name
= gen_name(torture
);
187 if (!test_one(torture
, cli
, name
)) {
190 if (total
&& total
% 100 == 0) {
191 if (torture_setting_bool(torture
, "progress", true)) {
192 printf("collisions %u/%u - %.2f%% (%u failures)\r",
193 collisions
, total
, (100.0*collisions
) / total
, failures
);
198 smbcli_unlink(cli
->tree
, "\\mangle_test\\*");
199 if (NT_STATUS_IS_ERR(smbcli_rmdir(cli
->tree
, "\\mangle_test"))) {
200 printf("ERROR: Failed to remove directory\n");
204 printf("\nTotal collisions %u/%u - %.2f%% (%u failures)\n",
205 collisions
, total
, (100.0*collisions
) / total
, failures
);
207 return (failures
== 0);