s3-printing: rename queue->job sysjob
[Samba/id10ts.git] / source3 / torture / mangle_test.c
blob36e4aafff050f41d14030e03d5078d3ee8ccdf6c
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 "torture/proto.h"
23 #include "libsmb/libsmb.h"
24 #include "libsmb/clirap.h"
25 #include "util_tdb.h"
27 extern int torture_numops;
29 static TDB_CONTEXT *tdb;
31 #define NAME_LENGTH 20
33 static unsigned total, collisions, failures;
35 static bool test_one(struct cli_state *cli, const char *name)
37 uint16_t fnum;
38 fstring shortname;
39 fstring name2;
40 NTSTATUS status;
41 TDB_DATA data;
43 total++;
45 status = cli_openx(cli, name, O_RDWR|O_CREAT|O_EXCL, DENY_NONE, &fnum);
46 if (!NT_STATUS_IS_OK(status)) {
47 printf("open of %s failed (%s)\n", name, nt_errstr(status));
48 return False;
51 status = cli_close(cli, fnum);
52 if (!NT_STATUS_IS_OK(status)) {
53 printf("close of %s failed (%s)\n", name, nt_errstr(status));
54 return False;
57 /* get the short name */
58 status = cli_qpathinfo_alt_name(cli, name, shortname);
59 if (!NT_STATUS_IS_OK(status)) {
60 printf("query altname of %s failed (%s)\n", name, nt_errstr(status));
61 return False;
64 fstr_sprintf(name2, "\\mangle_test\\%s", shortname);
65 status = cli_unlink(cli, name2, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
66 if (!NT_STATUS_IS_OK(status)) {
67 printf("unlink of %s (%s) failed (%s)\n",
68 name2, name, nt_errstr(status));
69 return False;
72 /* recreate by short name */
73 status = cli_openx(cli, name2, O_RDWR|O_CREAT|O_EXCL, DENY_NONE, &fnum);
74 if (!NT_STATUS_IS_OK(status)) {
75 printf("open2 of %s failed (%s)\n", name2, nt_errstr(status));
76 return False;
79 status = cli_close(cli, fnum);
80 if (!NT_STATUS_IS_OK(status)) {
81 printf("close of %s failed (%s)\n", name, nt_errstr(status));
82 return False;
85 /* and unlink by long name */
86 status = cli_unlink(cli, name, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
87 if (!NT_STATUS_IS_OK(status)) {
88 printf("unlink2 of %s (%s) failed (%s)\n",
89 name, name2, nt_errstr(status));
90 failures++;
91 cli_unlink(cli, name2, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
92 return True;
95 /* see if the short name is already in the tdb */
96 data = tdb_fetch_bystring(tdb, shortname);
97 if (data.dptr) {
98 /* maybe its a duplicate long name? */
99 if (!strequal(name, (const char *)data.dptr)) {
100 /* we have a collision */
101 collisions++;
102 printf("Collision between %s and %s -> %s "
103 " (coll/tot: %u/%u)\n",
104 name, data.dptr, shortname, collisions, total);
106 free(data.dptr);
107 } else {
108 TDB_DATA namedata;
109 /* store it for later */
110 namedata.dptr = discard_const_p(uint8, name);
111 namedata.dsize = strlen(name)+1;
112 tdb_store_bystring(tdb, shortname, namedata, TDB_REPLACE);
115 return True;
119 static void gen_name(char *name)
121 const char *chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz._-$~... ";
122 unsigned max_idx = strlen(chars);
123 unsigned len;
124 int i;
125 char *p;
127 fstrcpy(name, "\\mangle_test\\");
128 p = name + strlen(name);
130 len = 1 + random() % NAME_LENGTH;
132 for (i=0;i<len;i++) {
133 p[i] = chars[random() % max_idx];
136 p[i] = 0;
138 if (strcmp(p, ".") == 0 || strcmp(p, "..") == 0) {
139 p[0] = '_';
142 /* have a high probability of a common lead char */
143 if (random() % 2 == 0) {
144 p[0] = 'A';
147 /* and a medium probability of a common lead string */
148 if (random() % 10 == 0) {
149 if (strlen(p) <= 5) {
150 fstrcpy(p, "ABCDE");
151 } else {
152 /* try not to kill off the null termination */
153 memcpy(p, "ABCDE", 5);
157 /* and a high probability of a good extension length */
158 if (random() % 2 == 0) {
159 char *s = strrchr(p, '.');
160 if (s) {
161 s[4] = 0;
165 /* ..... and a 100% proability of a file not ending in "." */
166 if (p[strlen(p)-1] == '.')
167 p[strlen(p)-1] = '_';
171 bool torture_mangle(int dummy)
173 static struct cli_state *cli;
174 int i;
175 bool ret = True;
177 printf("starting mangle test\n");
179 if (!torture_open_connection(&cli, 0)) {
180 return False;
183 /* we will use an internal tdb to store the names we have used */
184 tdb = tdb_open_compat(NULL, 100000, TDB_INTERNAL, 0, 0, NULL, NULL);
185 if (!tdb) {
186 printf("ERROR: Failed to open tdb\n");
187 return False;
190 cli_unlink(cli, "\\mangle_test\\*", FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
191 cli_rmdir(cli, "\\mangle_test");
193 if (!NT_STATUS_IS_OK(cli_mkdir(cli, "\\mangle_test"))) {
194 printf("ERROR: Failed to make directory\n");
195 return False;
198 for (i=0;i<torture_numops;i++) {
199 fstring name;
200 ZERO_STRUCT(name);
202 gen_name(name);
204 if (!test_one(cli, name)) {
205 ret = False;
206 break;
208 if (total && total % 100 == 0) {
209 printf("collisions %u/%u - %.2f%% (%u failures)\r",
210 collisions, total, (100.0*collisions) / total, failures);
214 cli_unlink(cli, "\\mangle_test\\*", FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
215 if (!NT_STATUS_IS_OK(cli_rmdir(cli, "\\mangle_test"))) {
216 printf("ERROR: Failed to remove directory\n");
217 return False;
220 printf("\nTotal collisions %u/%u - %.2f%% (%u failures)\n",
221 collisions, total, (100.0*collisions) / total, failures);
223 torture_close_connection(cli);
225 printf("mangle test finished\n");
226 return (ret && (failures == 0));