28122da214e5ad60548c973e13b0ad8fb3470f4e
[sfrench/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 2 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, write to the Free Software
18    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 */
20
21 #include "includes.h"
22 #include "torture/torture.h"
23 #include "system/filesys.h"
24 #include "system/locale.h"
25 #include "libcli/libcli.h"
26 #include "torture/util.h"
27 #include "pstring.h"
28
29 bool torture_utable(struct torture_context *tctx, 
30                                         struct smbcli_state *cli)
31 {
32         fstring fname;
33         const char *alt_name;
34         int fnum;
35         uint8_t c2[4];
36         int c, len, fd;
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                 fstrcpy(fname, "\\utable\\x");
52                 p = fname+strlen(fname);
53                 len = convert_string(CH_UTF16, CH_UNIX, 
54                                      c2, 2, 
55                                      p, sizeof(fname)-strlen(fname));
56                 p[len] = 0;
57                 fstrcat(fname,"_a_long_extension");
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                         torture_comment(tctx, "%d (%d/%d)\r", c, chars_allowed, alt_allowed);
78                 }
79         }
80         torture_comment(tctx, "%d (%d/%d)\n", c, chars_allowed, alt_allowed);
81
82         smbcli_rmdir(cli->tree, "\\utable");
83
84         torture_comment(tctx, "%d chars allowed   %d alt chars allowed\n", chars_allowed, alt_allowed);
85
86         fd = open("valid.dat", O_WRONLY|O_CREAT|O_TRUNC, 0644);
87         torture_assert(tctx, fd != -1, 
88                 talloc_asprintf(tctx, 
89                 "Failed to create valid.dat - %s", strerror(errno)));
90         write(fd, valid, 0x10000);
91         close(fd);
92         torture_comment(tctx, "wrote valid.dat\n");
93
94         return true;
95 }
96
97
98 static char *form_name(int c)
99 {
100         static fstring fname;
101         uint8_t c2[4];
102         char *p;
103         int len;
104
105         fstrcpy(fname, "\\utable\\");
106         p = fname+strlen(fname);
107         SSVAL(c2, 0, c);
108
109         len = convert_string(CH_UTF16, CH_UNIX, 
110                              c2, 2, 
111                              p, sizeof(fname)-strlen(fname));
112         p[len] = 0;
113         return fname;
114 }
115
116 bool torture_casetable(struct torture_context *tctx, 
117                                            struct smbcli_state *cli)
118 {
119         char *fname;
120         int fnum;
121         int c, i;
122 #define MAX_EQUIVALENCE 8
123         codepoint_t equiv[0x10000][MAX_EQUIVALENCE];
124
125         torture_comment(tctx, "Determining upper/lower case table\n");
126
127         memset(equiv, 0, sizeof(equiv));
128
129         torture_assert(tctx, torture_setup_dir(cli, "\\utable"),
130                                    "Error setting up dir \\utable");
131
132         for (c=1; c < 0x10000; c++) {
133                 size_t size;
134
135                 if (c == '.' || c == '\\') continue;
136
137                 torture_comment(tctx, "%04x (%c)\n", c, isprint(c)?c:'.');
138
139                 fname = form_name(c);
140                 fnum = smbcli_nt_create_full(cli->tree, fname, 0,
141 #if 0
142                                              SEC_RIGHT_MAXIMUM_ALLOWED, 
143 #else
144                                              SEC_RIGHTS_FILE_ALL,
145 #endif
146                                              FILE_ATTRIBUTE_NORMAL,
147                                              NTCREATEX_SHARE_ACCESS_NONE,
148                                              NTCREATEX_DISP_OPEN_IF, 0, 0);
149
150                 torture_assert(tctx, fnum != -1, 
151                                            talloc_asprintf(tctx, 
152                         "Failed to create file with char %04x\n", c));
153
154                 size = 0;
155
156                 if (NT_STATUS_IS_ERR(smbcli_qfileinfo(cli->tree, fnum, NULL, &size, 
157                                                    NULL, NULL, NULL, NULL, NULL))) continue;
158
159                 if (size > 0) {
160                         /* found a character equivalence! */
161                         int c2[MAX_EQUIVALENCE];
162
163                         if (size/sizeof(int) >= MAX_EQUIVALENCE) {
164                                 torture_comment(tctx, "too many chars match?? size=%d c=0x%04x\n",
165                                        (int)size, c);
166                                 smbcli_close(cli->tree, fnum);
167                                 return False;
168                         }
169
170                         smbcli_read(cli->tree, fnum, c2, 0, size);
171                         torture_comment(tctx, "%04x: ", c);
172                         equiv[c][0] = c;
173                         for (i=0; i<size/sizeof(int); i++) {
174                                 torture_comment(tctx, "%04x ", c2[i]);
175                                 equiv[c][i+1] = c2[i];
176                         }
177                         torture_comment(tctx, "\n");
178                 }
179
180                 smbcli_write(cli->tree, fnum, 0, &c, size, sizeof(c));
181                 smbcli_close(cli->tree, fnum);
182         }
183
184         smbcli_unlink(cli->tree, "\\utable\\*");
185         smbcli_rmdir(cli->tree, "\\utable");
186
187         return true;
188 }