source: vendor/current/source4/torture/basic/mangle_test.c@ 740

Last change on this file since 740 was 740, checked in by Silvan Scherrer, 12 years ago

Samba Server: update vendor to 3.6.0

File size: 5.4 KB
Line 
1/*
2 Unix SMB/CIFS implementation.
3 SMB torture tester - mangling test
4 Copyright (C) Andrew Tridgell 2002
5
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.
10
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.
15
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/>.
18*/
19
20#include "includes.h"
21#include "system/filesys.h"
22#include "system/dir.h"
23#include <tdb.h>
24#include "../lib/util/util_tdb.h"
25#include "libcli/libcli.h"
26#include "torture/util.h"
27
28static TDB_CONTEXT *tdb;
29
30#define NAME_LENGTH 20
31
32static unsigned int total, collisions, failures;
33
34static bool test_one(struct torture_context *tctx ,struct smbcli_state *cli,
35 const char *name)
36{
37 int fnum;
38 const char *shortname;
39 const char *name2;
40 NTSTATUS status;
41 TDB_DATA data;
42
43 total++;
44
45 fnum = smbcli_open(cli->tree, name, O_RDWR|O_CREAT|O_EXCL, DENY_NONE);
46 if (fnum == -1) {
47 printf("open of %s failed (%s)\n", name, smbcli_errstr(cli->tree));
48 return false;
49 }
50
51 if (NT_STATUS_IS_ERR(smbcli_close(cli->tree, fnum))) {
52 printf("close of %s failed (%s)\n", name, smbcli_errstr(cli->tree));
53 return false;
54 }
55
56 /* get the short name */
57 status = smbcli_qpathinfo_alt_name(cli->tree, name, &shortname);
58 if (!NT_STATUS_IS_OK(status)) {
59 printf("query altname of %s failed (%s)\n", name, smbcli_errstr(cli->tree));
60 return false;
61 }
62
63 name2 = talloc_asprintf(tctx, "\\mangle_test\\%s", shortname);
64 if (NT_STATUS_IS_ERR(smbcli_unlink(cli->tree, name2))) {
65 printf("unlink of %s (%s) failed (%s)\n",
66 name2, name, smbcli_errstr(cli->tree));
67 return false;
68 }
69
70 /* recreate by short name */
71 fnum = smbcli_open(cli->tree, name2, O_RDWR|O_CREAT|O_EXCL, DENY_NONE);
72 if (fnum == -1) {
73 printf("open2 of %s failed (%s)\n", name2, smbcli_errstr(cli->tree));
74 return false;
75 }
76 if (NT_STATUS_IS_ERR(smbcli_close(cli->tree, fnum))) {
77 printf("close of %s failed (%s)\n", name, smbcli_errstr(cli->tree));
78 return false;
79 }
80
81 /* and unlink by long name */
82 if (NT_STATUS_IS_ERR(smbcli_unlink(cli->tree, name))) {
83 printf("unlink2 of %s (%s) failed (%s)\n",
84 name, name2, smbcli_errstr(cli->tree));
85 failures++;
86 smbcli_unlink(cli->tree, name2);
87 return true;
88 }
89
90 /* see if the short name is already in the tdb */
91 data = tdb_fetch_bystring(tdb, shortname);
92 if (data.dptr) {
93 /* maybe its a duplicate long name? */
94 if (strcasecmp(name, (const char *)data.dptr) != 0) {
95 /* we have a collision */
96 collisions++;
97 printf("Collision between %s and %s -> %s "
98 " (coll/tot: %u/%u)\n",
99 name, data.dptr, shortname, collisions, total);
100 }
101 free(data.dptr);
102 } else {
103 TDB_DATA namedata;
104 /* store it for later */
105 namedata.dptr = discard_const_p(uint8_t, name);
106 namedata.dsize = strlen(name)+1;
107 tdb_store_bystring(tdb, shortname, namedata, TDB_REPLACE);
108 }
109
110 return true;
111}
112
113
114static char *gen_name(TALLOC_CTX *mem_ctx)
115{
116 const char *chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz._-$~...";
117 unsigned int max_idx = strlen(chars);
118 unsigned int len;
119 int i;
120 char *p;
121 char *name;
122
123 name = talloc_strdup(mem_ctx, "\\mangle_test\\");
124
125 len = 1 + random() % NAME_LENGTH;
126
127 name = talloc_realloc(mem_ctx, name, char, strlen(name) + len + 6);
128 p = name + strlen(name);
129
130 for (i=0;i<len;i++) {
131 p[i] = chars[random() % max_idx];
132 }
133
134 p[i] = 0;
135
136 if (ISDOT(p) || ISDOTDOT(p)) {
137 p[0] = '_';
138 }
139
140 /* have a high probability of a common lead char */
141 if (random() % 2 == 0) {
142 p[0] = 'A';
143 }
144
145 /* and a medium probability of a common lead string */
146 if ((len > 5) && (random() % 10 == 0)) {
147 strncpy(p, "ABCDE", 5);
148 }
149
150 /* and a high probability of a good extension length */
151 if (random() % 2 == 0) {
152 char *s = strrchr(p, '.');
153 if (s) {
154 s[4] = 0;
155 }
156 }
157
158 return name;
159}
160
161
162bool torture_mangle(struct torture_context *torture,
163 struct smbcli_state *cli)
164{
165 extern int torture_numops;
166 int i;
167
168 /* we will use an internal tdb to store the names we have used */
169 tdb = tdb_open(NULL, 100000, TDB_INTERNAL, 0, 0);
170 if (!tdb) {
171 printf("ERROR: Failed to open tdb\n");
172 return false;
173 }
174
175 if (!torture_setup_dir(cli, "\\mangle_test")) {
176 return false;
177 }
178
179 for (i=0;i<torture_numops;i++) {
180 char *name;
181
182 name = gen_name(torture);
183
184 if (!test_one(torture, cli, name)) {
185 break;
186 }
187 if (total && total % 100 == 0) {
188 if (torture_setting_bool(torture, "progress", true)) {
189 printf("collisions %u/%u - %.2f%% (%u failures)\r",
190 collisions, total, (100.0*collisions) / total, failures);
191 }
192 }
193 }
194
195 smbcli_unlink(cli->tree, "\\mangle_test\\*");
196 if (NT_STATUS_IS_ERR(smbcli_rmdir(cli->tree, "\\mangle_test"))) {
197 printf("ERROR: Failed to remove directory\n");
198 return false;
199 }
200
201 printf("\nTotal collisions %u/%u - %.2f%% (%u failures)\n",
202 collisions, total, (100.0*collisions) / total, failures);
203
204 return (failures == 0);
205}
Note: See TracBrowser for help on using the repository browser.