r23792: convert Samba4 to GPLv3
[gd/samba-autobuild/.git] / source4 / torture / basic / utable.c
1 /* 
2    Unix SMB/CIFS implementation.
3    SMB torture tester - unicode table dumper
4    Copyright (C) Andrew Tridgell 2001
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 "torture/torture.h"
22 #include "system/filesys.h"
23 #include "system/locale.h"
24 #include "libcli/libcli.h"
25 #include "torture/util.h"
26 #include "pstring.h"
27
28 bool torture_utable(struct torture_context *tctx, 
29                                         struct smbcli_state *cli)
30 {
31         fstring fname;
32         const char *alt_name;
33         int fnum;
34         uint8_t c2[4];
35         int c, len, fd;
36         int chars_allowed=0, alt_allowed=0;
37         uint8_t valid[0x10000];
38
39         torture_comment(tctx, "Generating valid character table\n");
40
41         memset(valid, 0, sizeof(valid));
42
43         torture_assert(tctx, torture_setup_dir(cli, "\\utable"),
44                                    "Setting up dir \\utable failed");
45
46         for (c=1; c < 0x10000; c++) {
47                 char *p;
48
49                 SSVAL(c2, 0, c);
50                 fstrcpy(fname, "\\utable\\x");
51                 p = fname+strlen(fname);
52                 len = convert_string(CH_UTF16, CH_UNIX, 
53                                      c2, 2, 
54                                      p, sizeof(fname)-strlen(fname));
55                 p[len] = 0;
56                 fstrcat(fname,"_a_long_extension");
57
58                 fnum = smbcli_open(cli->tree, fname, O_RDWR | O_CREAT | O_TRUNC, 
59                                 DENY_NONE);
60                 if (fnum == -1) continue;
61
62                 chars_allowed++;
63
64                 smbcli_qpathinfo_alt_name(cli->tree, fname, &alt_name);
65
66                 if (strncmp(alt_name, "X_A_L", 5) != 0) {
67                         alt_allowed++;
68                         valid[c] = 1;
69                         torture_comment(tctx, "fname=[%s] alt_name=[%s]\n", fname, alt_name);
70                 }
71
72                 smbcli_close(cli->tree, fnum);
73                 smbcli_unlink(cli->tree, fname);
74
75                 if (c % 100 == 0) {
76                         if (torture_setting_bool(tctx, "progress", true)) {
77                                 torture_comment(tctx, "%d (%d/%d)\r", c, chars_allowed, alt_allowed);
78                                 fflush(stdout);
79                         }
80                 }
81         }
82         torture_comment(tctx, "%d (%d/%d)\n", c, chars_allowed, alt_allowed);
83
84         smbcli_rmdir(cli->tree, "\\utable");
85
86         torture_comment(tctx, "%d chars allowed   %d alt chars allowed\n", chars_allowed, alt_allowed);
87
88         fd = open("valid.dat", O_WRONLY|O_CREAT|O_TRUNC, 0644);
89         torture_assert(tctx, fd != -1, 
90                 talloc_asprintf(tctx, 
91                 "Failed to create valid.dat - %s", strerror(errno)));
92         write(fd, valid, 0x10000);
93         close(fd);
94         torture_comment(tctx, "wrote valid.dat\n");
95
96         return true;
97 }
98
99
100 static char *form_name(int c)
101 {
102         static fstring fname;
103         uint8_t c2[4];
104         char *p;
105         int len;
106
107         fstrcpy(fname, "\\utable\\");
108         p = fname+strlen(fname);
109         SSVAL(c2, 0, c);
110
111         len = convert_string(CH_UTF16, CH_UNIX, 
112                              c2, 2, 
113                              p, sizeof(fname)-strlen(fname));
114         p[len] = 0;
115         return fname;
116 }
117
118 bool torture_casetable(struct torture_context *tctx, 
119                                            struct smbcli_state *cli)
120 {
121         char *fname;
122         int fnum;
123         int c, i;
124 #define MAX_EQUIVALENCE 8
125         codepoint_t equiv[0x10000][MAX_EQUIVALENCE];
126
127         torture_comment(tctx, "Determining upper/lower case table\n");
128
129         memset(equiv, 0, sizeof(equiv));
130
131         torture_assert(tctx, torture_setup_dir(cli, "\\utable"),
132                                    "Error setting up dir \\utable");
133
134         for (c=1; c < 0x10000; c++) {
135                 size_t size;
136
137                 if (c == '.' || c == '\\') continue;
138
139                 torture_comment(tctx, "%04x (%c)\n", c, isprint(c)?c:'.');
140
141                 fname = form_name(c);
142                 fnum = smbcli_nt_create_full(cli->tree, fname, 0,
143 #if 0
144                                              SEC_RIGHT_MAXIMUM_ALLOWED, 
145 #else
146                                              SEC_RIGHTS_FILE_ALL,
147 #endif
148                                              FILE_ATTRIBUTE_NORMAL,
149                                              NTCREATEX_SHARE_ACCESS_NONE,
150                                              NTCREATEX_DISP_OPEN_IF, 0, 0);
151
152                 torture_assert(tctx, fnum != -1, 
153                                            talloc_asprintf(tctx, 
154                         "Failed to create file with char %04x\n", c));
155
156                 size = 0;
157
158                 if (NT_STATUS_IS_ERR(smbcli_qfileinfo(cli->tree, fnum, NULL, &size, 
159                                                    NULL, NULL, NULL, NULL, NULL))) continue;
160
161                 if (size > 0) {
162                         /* found a character equivalence! */
163                         int c2[MAX_EQUIVALENCE];
164
165                         if (size/sizeof(int) >= MAX_EQUIVALENCE) {
166                                 torture_comment(tctx, "too many chars match?? size=%d c=0x%04x\n",
167                                        (int)size, c);
168                                 smbcli_close(cli->tree, fnum);
169                                 return False;
170                         }
171
172                         smbcli_read(cli->tree, fnum, c2, 0, size);
173                         torture_comment(tctx, "%04x: ", c);
174                         equiv[c][0] = c;
175                         for (i=0; i<size/sizeof(int); i++) {
176                                 torture_comment(tctx, "%04x ", c2[i]);
177                                 equiv[c][i+1] = c2[i];
178                         }
179                         torture_comment(tctx, "\n");
180                 }
181
182                 smbcli_write(cli->tree, fnum, 0, &c, size, sizeof(c));
183                 smbcli_close(cli->tree, fnum);
184         }
185
186         smbcli_unlink(cli->tree, "\\utable\\*");
187         smbcli_rmdir(cli->tree, "\\utable");
188
189         return true;
190 }