s3:smbd: s/struct timed_event/struct tevent_timer
[Samba/gebeck_regimport.git] / source4 / torture / basic / utable.c
blob0b4569235d90f452a5bf54a12a0865797eeeb253
1 /*
2 Unix SMB/CIFS implementation.
3 SMB torture tester - unicode table dumper
4 Copyright (C) Andrew Tridgell 2001
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/locale.h"
23 #include "libcli/libcli.h"
24 #include "torture/util.h"
25 #include "param/param.h"
26 #include "torture/basic/proto.h"
28 bool torture_utable(struct torture_context *tctx,
29 struct smbcli_state *cli)
31 char fname[256];
32 const char *alt_name;
33 int fnum;
34 uint8_t c2[4];
35 int c, fd;
36 size_t len;
37 int chars_allowed=0, alt_allowed=0;
38 uint8_t valid[0x10000];
40 torture_comment(tctx, "Generating valid character table\n");
42 memset(valid, 0, sizeof(valid));
44 torture_assert(tctx, torture_setup_dir(cli, "\\utable"),
45 "Setting up dir \\utable failed");
47 for (c=1; c < 0x10000; c++) {
48 char *p;
50 SSVAL(c2, 0, c);
51 strncpy(fname, "\\utable\\x", sizeof(fname)-1);
52 p = fname+strlen(fname);
53 len = 0;
54 if (!convert_string(CH_UTF16, CH_UNIX,
55 c2, 2,
56 p, sizeof(fname)-strlen(fname), &len)) {
57 torture_comment(tctx, "convert_string failed [%s]\n",
58 fname);
59 continue;
62 p[len] = 0;
63 strncat(fname,"_a_long_extension",sizeof(fname)-1);
65 fnum = smbcli_open(cli->tree, fname, O_RDWR | O_CREAT | O_TRUNC,
66 DENY_NONE);
67 if (fnum == -1) continue;
69 chars_allowed++;
71 smbcli_qpathinfo_alt_name(cli->tree, fname, &alt_name);
73 if (strncmp(alt_name, "X_A_L", 5) != 0) {
74 alt_allowed++;
75 valid[c] = 1;
76 torture_comment(tctx, "fname=[%s] alt_name=[%s]\n", fname, alt_name);
79 smbcli_close(cli->tree, fnum);
80 smbcli_unlink(cli->tree, fname);
82 if (c % 100 == 0) {
83 if (torture_setting_bool(tctx, "progress", true)) {
84 torture_comment(tctx, "%d (%d/%d)\r", c, chars_allowed, alt_allowed);
85 fflush(stdout);
89 torture_comment(tctx, "%d (%d/%d)\n", c, chars_allowed, alt_allowed);
91 smbcli_rmdir(cli->tree, "\\utable");
93 torture_comment(tctx, "%d chars allowed %d alt chars allowed\n", chars_allowed, alt_allowed);
95 fd = open("valid.dat", O_WRONLY|O_CREAT|O_TRUNC, 0644);
96 torture_assert(tctx, fd != -1,
97 talloc_asprintf(tctx,
98 "Failed to create valid.dat - %s", strerror(errno)));
99 write(fd, valid, 0x10000);
100 close(fd);
101 torture_comment(tctx, "wrote valid.dat\n");
103 return true;
107 static char *form_name(int c)
109 static char fname[256];
110 uint8_t c2[4];
111 char *p;
112 size_t len = 0;
114 strncpy(fname, "\\utable\\", sizeof(fname)-1);
115 p = fname+strlen(fname);
116 SSVAL(c2, 0, c);
118 if (!convert_string(CH_UTF16, CH_UNIX,
119 c2, 2,
120 p, sizeof(fname)-strlen(fname), &len)) {
121 return NULL;
123 p[len] = 0;
124 return fname;
127 bool torture_casetable(struct torture_context *tctx,
128 struct smbcli_state *cli)
130 char *fname;
131 int fnum;
132 int c, i;
133 #define MAX_EQUIVALENCE 8
134 codepoint_t equiv[0x10000][MAX_EQUIVALENCE];
136 torture_comment(tctx, "Determining upper/lower case table\n");
138 memset(equiv, 0, sizeof(equiv));
140 torture_assert(tctx, torture_setup_dir(cli, "\\utable"),
141 "Error setting up dir \\utable");
143 for (c=1; c < 0x10000; c++) {
144 size_t size;
146 if (c == '.' || c == '\\') continue;
148 torture_comment(tctx, "%04x (%c)\n", c, isprint(c)?c:'.');
150 fname = form_name(c);
151 if (fname == NULL) continue;
152 fnum = smbcli_nt_create_full(cli->tree, fname, 0,
153 #if 0
154 SEC_RIGHT_MAXIMUM_ALLOWED,
155 #else
156 SEC_RIGHTS_FILE_ALL,
157 #endif
158 FILE_ATTRIBUTE_NORMAL,
159 NTCREATEX_SHARE_ACCESS_NONE,
160 NTCREATEX_DISP_OPEN_IF, 0, 0);
162 if (fnum == -1) {
163 torture_comment(tctx, "Failed to create file with char %04x\n", c);
164 continue;
167 size = 0;
169 if (NT_STATUS_IS_ERR(smbcli_qfileinfo(cli->tree, fnum, NULL, &size,
170 NULL, NULL, NULL, NULL, NULL))) continue;
172 if (size > 0) {
173 /* found a character equivalence! */
174 int c2[MAX_EQUIVALENCE];
176 if (size/sizeof(int) >= MAX_EQUIVALENCE) {
177 torture_comment(tctx, "too many chars match?? size=%d c=0x%04x\n",
178 (int)size, c);
179 smbcli_close(cli->tree, fnum);
180 return false;
183 smbcli_read(cli->tree, fnum, c2, 0, size);
184 torture_comment(tctx, "%04x: ", c);
185 equiv[c][0] = c;
186 for (i=0; i<size/sizeof(int); i++) {
187 torture_comment(tctx, "%04x ", c2[i]);
188 equiv[c][i+1] = c2[i];
190 torture_comment(tctx, "\n");
193 smbcli_write(cli->tree, fnum, 0, &c, size, sizeof(c));
194 smbcli_close(cli->tree, fnum);
197 smbcli_unlink(cli->tree, "\\utable\\*");
198 smbcli_rmdir(cli->tree, "\\utable");
200 return true;