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.
23 static TDB_CONTEXT *tdb;
25 #define NAME_LENGTH 20
27 static unsigned total, collisions;
29 static BOOL test_one(struct cli_state *cli, const char *name)
39 fnum = cli_open(cli, name, O_RDWR|O_CREAT|O_EXCL, DENY_NONE);
41 printf("open of %s failed (%s)\n", name, cli_errstr(cli));
45 if (!cli_close(cli, fnum)) {
46 printf("close of %s failed (%s)\n", name, cli_errstr(cli));
50 /* get the short name */
51 status = cli_qpathinfo_alt_name(cli, name, shortname);
52 if (!NT_STATUS_IS_OK(status)) {
53 printf("query altname of %s failed (%s)\n", name, cli_errstr(cli));
57 snprintf(name2, sizeof(name2), "\\mangle_test\\%s", shortname);
58 if (!cli_unlink(cli, name2)) {
59 printf("unlink of %s (%s) failed (%s)\n",
60 name2, name, cli_errstr(cli));
64 /* recreate by short name */
65 fnum = cli_open(cli, name2, O_RDWR|O_CREAT|O_EXCL, DENY_NONE);
67 printf("open2 of %s failed (%s)\n", name2, cli_errstr(cli));
70 if (!cli_close(cli, fnum)) {
71 printf("close of %s failed (%s)\n", name, cli_errstr(cli));
75 /* and unlink by long name */
76 if (!cli_unlink(cli, name)) {
77 printf("unlink2 of %s (%s) failed (%s)\n",
78 name, name2, cli_errstr(cli));
82 /* see if the short name is already in the tdb */
83 data = tdb_fetch_by_string(tdb, shortname);
85 /* maybe its a duplicate long name? */
86 if (strcasecmp(name, data.dptr) != 0) {
87 /* we have a collision */
89 printf("Collision between %s and %s -> %s\n",
90 name, data.dptr, shortname);
94 /* store it for later */
95 tdb_store_by_string(tdb, shortname, name, strlen(name)+1);
102 static void gen_name(char *name)
104 const char *chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz._-$~...";
105 unsigned max_idx = strlen(chars);
110 fstrcpy(name, "\\mangle_test\\");
111 p = name + strlen(name);
113 len = 1 + random() % NAME_LENGTH;
115 for (i=0;i<len;i++) {
116 p[i] = chars[random() % max_idx];
121 if (strcmp(p, ".") == 0 || strcmp(p, "..") == 0) {
125 /* have a high probability of a common lead char */
126 if (random() % 2 == 0) {
130 /* and a high probability of a good extension length */
131 if (random() % 2 == 0) {
132 char *s = strrchr(p, '.');
140 BOOL torture_mangle(int dummy)
142 extern int torture_numops;
143 static struct cli_state cli;
146 printf("starting mangle test\n");
148 if (!torture_open_connection(&cli)) {
152 /* we will use an internal tdb to store the names we have used */
153 tdb = tdb_open(NULL, 100000, TDB_INTERNAL, 0, 0);
155 printf("ERROR: Failed to open tdb\n");
159 cli_unlink(&cli, "\\mangle_test\\*");
160 cli_rmdir(&cli, "\\mangle_test");
162 if (!cli_mkdir(&cli, "\\mangle_test")) {
163 printf("ERROR: Failed to make directory\n");
167 for (i=0;i<torture_numops;i++) {
172 if (!test_one(&cli, name)) {
175 if (total && total % 100 == 0) {
176 printf("collisions %u/%u - %.2f%%\r",
177 collisions, total, (100.0*collisions) / total);
181 if (!cli_rmdir(&cli, "\\mangle_test")) {
182 printf("ERROR: Failed to remove directory\n");
186 printf("\nTotal collisions %u/%u - %.2f%%\n",
187 collisions, total, (100.0*collisions) / total);
189 torture_close_connection(&cli);
191 printf("mangle test finished\n");