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