knownfail: remove python[23] lines
[Samba.git] / source4 / torture / basic / mangle_test.c
blob0c3ccd54bf94e02e4db3212e553e52c3c256e236
1 /*
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/>.
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 #include "torture/basic/proto.h"
29 #undef strcasecmp
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,
38 const char *name)
40 int fnum;
41 const char *shortname;
42 const char *name2;
43 NTSTATUS status;
44 TDB_DATA data;
46 total++;
48 fnum = smbcli_open(cli->tree, name, O_RDWR|O_CREAT|O_EXCL, DENY_NONE);
49 if (fnum == -1) {
50 printf("open of %s failed (%s)\n", name, smbcli_errstr(cli->tree));
51 return false;
54 if (NT_STATUS_IS_ERR(smbcli_close(cli->tree, fnum))) {
55 printf("close of %s failed (%s)\n", name, smbcli_errstr(cli->tree));
56 return false;
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));
63 return false;
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));
70 return false;
73 /* recreate by short name */
74 fnum = smbcli_open(cli->tree, name2, O_RDWR|O_CREAT|O_EXCL, DENY_NONE);
75 if (fnum == -1) {
76 printf("open2 of %s failed (%s)\n", name2, smbcli_errstr(cli->tree));
77 return false;
79 if (NT_STATUS_IS_ERR(smbcli_close(cli->tree, fnum))) {
80 printf("close of %s failed (%s)\n", name, smbcli_errstr(cli->tree));
81 return false;
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));
88 failures++;
89 smbcli_unlink(cli->tree, name2);
90 return true;
93 /* see if the short name is already in the tdb */
94 data = tdb_fetch_bystring(tdb, shortname);
95 if (data.dptr) {
96 /* maybe its a duplicate long name? */
97 if (strcasecmp(name, (const char *)data.dptr) != 0) {
98 /* we have a collision */
99 collisions++;
100 printf("Collision between %s and %s -> %s "
101 " (coll/tot: %u/%u)\n",
102 name, data.dptr, shortname, collisions, total);
104 free(data.dptr);
105 } else {
106 TDB_DATA namedata;
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);
113 return true;
117 static char *gen_name(TALLOC_CTX *mem_ctx)
119 const char *chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz._-$~...";
120 unsigned int max_idx = strlen(chars);
121 unsigned int len;
122 int i;
123 char *p;
124 char *name;
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];
137 p[i] = 0;
139 if (ISDOT(p) || ISDOTDOT(p)) {
140 p[0] = '_';
143 /* have a high probability of a common lead char */
144 if (random() % 2 == 0) {
145 p[0] = 'A';
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, '.');
156 if (s) {
157 s[4] = 0;
161 return name;
165 bool torture_mangle(struct torture_context *torture,
166 struct smbcli_state *cli)
168 extern int torture_numops;
169 int i;
171 /* we will use an internal tdb to store the names we have used */
172 tdb = tdb_open(NULL, 100000, TDB_INTERNAL, 0, 0);
173 if (!tdb) {
174 printf("ERROR: Failed to open tdb\n");
175 return false;
178 if (!torture_setup_dir(cli, "\\mangle_test")) {
179 return false;
182 for (i=0;i<torture_numops;i++) {
183 char *name;
185 name = gen_name(torture);
187 if (!test_one(torture, cli, name)) {
188 break;
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");
201 return false;
204 printf("\nTotal collisions %u/%u - %.2f%% (%u failures)\n",
205 collisions, total, (100.0*collisions) / total, failures);
207 return (failures == 0);