check_parent_exists() can change errno. Ensure we preserve it across calls.
[Samba.git] / source3 / torture / mangle_test.c
bloba96f47cc34f1ba99b696520457cb822b71a0df46
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 if (!NT_STATUS_IS_OK(cli_open(cli, name, O_RDWR|O_CREAT|O_EXCL, DENY_NONE, &fnum))) {
46 printf("open of %s failed (%s)\n", name, cli_errstr(cli));
47 return False;
50 if (!NT_STATUS_IS_OK(cli_close(cli, fnum))) {
51 printf("close of %s failed (%s)\n", name, cli_errstr(cli));
52 return False;
55 /* get the short name */
56 status = cli_qpathinfo_alt_name(cli, name, shortname);
57 if (!NT_STATUS_IS_OK(status)) {
58 printf("query altname of %s failed (%s)\n", name, cli_errstr(cli));
59 return False;
62 fstr_sprintf(name2, "\\mangle_test\\%s", shortname);
63 if (!NT_STATUS_IS_OK(cli_unlink(cli, name2, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN))) {
64 printf("unlink of %s (%s) failed (%s)\n",
65 name2, name, cli_errstr(cli));
66 return False;
69 /* recreate by short name */
70 if (!NT_STATUS_IS_OK(cli_open(cli, name2, O_RDWR|O_CREAT|O_EXCL, DENY_NONE, &fnum))) {
71 printf("open2 of %s failed (%s)\n", name2, cli_errstr(cli));
72 return False;
74 if (!NT_STATUS_IS_OK(cli_close(cli, fnum))) {
75 printf("close of %s failed (%s)\n", name, cli_errstr(cli));
76 return False;
79 /* and unlink by long name */
80 if (!NT_STATUS_IS_OK(cli_unlink(cli, name, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN))) {
81 printf("unlink2 of %s (%s) failed (%s)\n",
82 name, name2, cli_errstr(cli));
83 failures++;
84 cli_unlink(cli, name2, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
85 return True;
88 /* see if the short name is already in the tdb */
89 data = tdb_fetch_bystring(tdb, shortname);
90 if (data.dptr) {
91 /* maybe its a duplicate long name? */
92 if (!strequal(name, (const char *)data.dptr)) {
93 /* we have a collision */
94 collisions++;
95 printf("Collision between %s and %s -> %s "
96 " (coll/tot: %u/%u)\n",
97 name, data.dptr, shortname, collisions, total);
99 free(data.dptr);
100 } else {
101 TDB_DATA namedata;
102 /* store it for later */
103 namedata.dptr = CONST_DISCARD(uint8 *, name);
104 namedata.dsize = strlen(name)+1;
105 tdb_store_bystring(tdb, shortname, namedata, TDB_REPLACE);
108 return True;
112 static void gen_name(char *name)
114 const char *chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz._-$~... ";
115 unsigned max_idx = strlen(chars);
116 unsigned len;
117 int i;
118 char *p;
120 fstrcpy(name, "\\mangle_test\\");
121 p = name + strlen(name);
123 len = 1 + random() % NAME_LENGTH;
125 for (i=0;i<len;i++) {
126 p[i] = chars[random() % max_idx];
129 p[i] = 0;
131 if (strcmp(p, ".") == 0 || strcmp(p, "..") == 0) {
132 p[0] = '_';
135 /* have a high probability of a common lead char */
136 if (random() % 2 == 0) {
137 p[0] = 'A';
140 /* and a medium probability of a common lead string */
141 if (random() % 10 == 0) {
142 if (strlen(p) <= 5) {
143 fstrcpy(p, "ABCDE");
144 } else {
145 /* try not to kill off the null termination */
146 memcpy(p, "ABCDE", 5);
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;
158 /* ..... and a 100% proability of a file not ending in "." */
159 if (p[strlen(p)-1] == '.')
160 p[strlen(p)-1] = '_';
164 bool torture_mangle(int dummy)
166 static struct cli_state *cli;
167 int i;
168 bool ret = True;
170 printf("starting mangle test\n");
172 if (!torture_open_connection(&cli, 0)) {
173 return False;
176 /* we will use an internal tdb to store the names we have used */
177 tdb = tdb_open(NULL, 100000, TDB_INTERNAL, 0, 0);
178 if (!tdb) {
179 printf("ERROR: Failed to open tdb\n");
180 return False;
183 cli_unlink(cli, "\\mangle_test\\*", FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
184 cli_rmdir(cli, "\\mangle_test");
186 if (!NT_STATUS_IS_OK(cli_mkdir(cli, "\\mangle_test"))) {
187 printf("ERROR: Failed to make directory\n");
188 return False;
191 for (i=0;i<torture_numops;i++) {
192 fstring name;
193 ZERO_STRUCT(name);
195 gen_name(name);
197 if (!test_one(cli, name)) {
198 ret = False;
199 break;
201 if (total && total % 100 == 0) {
202 printf("collisions %u/%u - %.2f%% (%u failures)\r",
203 collisions, total, (100.0*collisions) / total, failures);
207 cli_unlink(cli, "\\mangle_test\\*", FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
208 if (!NT_STATUS_IS_OK(cli_rmdir(cli, "\\mangle_test"))) {
209 printf("ERROR: Failed to remove directory\n");
210 return False;
213 printf("\nTotal collisions %u/%u - %.2f%% (%u failures)\n",
214 collisions, total, (100.0*collisions) / total, failures);
216 torture_close_connection(cli);
218 printf("mangle test finished\n");
219 return (ret && (failures == 0));