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 2 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, write to the Free Software
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 #include "lib/tdb/include/tdbutil.h"
24 static TDB_CONTEXT *tdb;
26 #define NAME_LENGTH 20
28 static uint_t total, collisions, failures;
30 static BOOL test_one(struct smbcli_state *cli, const char *name)
33 const char *shortname;
40 fnum = smbcli_open(cli->tree, name, O_RDWR|O_CREAT|O_EXCL, DENY_NONE);
42 printf("open of %s failed (%s)\n", name, smbcli_errstr(cli->tree));
46 if (NT_STATUS_IS_ERR(smbcli_close(cli->tree, fnum))) {
47 printf("close of %s failed (%s)\n", name, smbcli_errstr(cli->tree));
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));
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));
65 /* recreate by short name */
66 fnum = smbcli_open(cli->tree, name2, O_RDWR|O_CREAT|O_EXCL, DENY_NONE);
68 printf("open2 of %s failed (%s)\n", name2, smbcli_errstr(cli->tree));
71 if (NT_STATUS_IS_ERR(smbcli_close(cli->tree, fnum))) {
72 printf("close of %s failed (%s)\n", name, smbcli_errstr(cli->tree));
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));
81 smbcli_unlink(cli->tree, 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 (strcasecmp(name, data.dptr) != 0) {
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 = discard_const_p(char, 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 uint_t 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 strncpy(p, "ABCDE", 5);
142 /* and a high probability of a good extension length */
143 if (random() % 2 == 0) {
144 char *s = strrchr(p, '.');
152 BOOL torture_mangle(void)
154 extern int torture_numops;
155 static struct smbcli_state *cli;
158 printf("starting mangle test\n");
160 if (!torture_open_connection(&cli)) {
164 /* we will use an internal tdb to store the names we have used */
165 tdb = tdb_open(NULL, 100000, TDB_INTERNAL, 0, 0);
167 printf("ERROR: Failed to open tdb\n");
171 if (!torture_setup_dir(cli, "\\mangle_test")) {
175 for (i=0;i<torture_numops;i++) {
180 if (!test_one(cli, name)) {
183 if (total && total % 100 == 0) {
184 printf("collisions %u/%u - %.2f%% (%u failures)\r",
185 collisions, total, (100.0*collisions) / total, failures);
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");
195 printf("\nTotal collisions %u/%u - %.2f%% (%u failures)\n",
196 collisions, total, (100.0*collisions) / total, failures);
198 torture_close_connection(cli);
200 printf("mangle test finished\n");
201 return (failures == 0);