s3-libsmb: move protos to libsmb/proto.h
[obnox/samba-ctdb.git] / source3 / torture / 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 "system/filesys.h"
22 #include "torture/proto.h"
23 #include "../libcli/security/security.h"
24 #include "libsmb/libsmb.h"
25 #include "libsmb/clirap.h"
26
27 bool torture_utable(int dummy)
28 {
29         struct cli_state *cli;
30         fstring fname, alt_name;
31         uint16_t fnum;
32         smb_ucs2_t c2;
33         int c, len, fd;
34         int chars_allowed=0, alt_allowed=0;
35         uint8 valid[0x10000];
36
37         printf("starting utable\n");
38
39         if (!torture_open_connection(&cli, 0)) {
40                 return False;
41         }
42
43         memset(valid, 0, sizeof(valid));
44
45         cli_mkdir(cli, "\\utable");
46         cli_unlink(cli, "\\utable\\*", FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
47
48         for (c=1; c < 0x10000; c++) {
49                 char *p;
50
51                 SSVAL(&c2, 0, c);
52                 fstrcpy(fname, "\\utable\\x");
53                 p = fname+strlen(fname);
54                 len = convert_string(CH_UTF16LE, CH_UNIX, 
55                                      &c2, 2, 
56                                      p, sizeof(fname)-strlen(fname), True);
57                 p[len] = 0;
58                 fstrcat(fname,"_a_long_extension");
59
60                 if (!NT_STATUS_IS_OK(cli_open(cli, fname, O_RDWR | O_CREAT | O_TRUNC, 
61                                 DENY_NONE, &fnum))) {
62                         continue;
63                 }
64
65                 chars_allowed++;
66
67                 cli_qpathinfo_alt_name(cli, fname, alt_name);
68
69                 if (strncmp(alt_name, "X_A_L", 5) != 0) {
70                         alt_allowed++;
71                         valid[c] = 1;
72                         d_printf("fname=[%s] alt_name=[%s]\n", fname, alt_name);
73                 }
74
75                 cli_close(cli, fnum);
76                 cli_unlink(cli, fname, FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
77
78                 if (c % 100 == 0) {
79                         printf("%d (%d/%d)\r", c, chars_allowed, alt_allowed);
80                 }
81         }
82         printf("%d (%d/%d)\n", c, chars_allowed, alt_allowed);
83
84         cli_rmdir(cli, "\\utable");
85
86         d_printf("%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         if (fd == -1) {
90                 d_printf("Failed to create valid.dat - %s", strerror(errno));
91                 return False;
92         }
93         if (write(fd, valid, 0x10000) != 0x10000) {
94                 d_printf("Failed to create valid.dat - %s", strerror(errno));
95                 close(fd);
96                 return false;
97         }
98         close(fd);
99         d_printf("wrote valid.dat\n");
100
101         return True;
102 }
103
104
105 static char *form_name(int c)
106 {
107         static fstring fname;
108         smb_ucs2_t c2;
109         char *p;
110         int len;
111
112         fstrcpy(fname, "\\utable\\");
113         p = fname+strlen(fname);
114         SSVAL(&c2, 0, c);
115
116         len = convert_string(CH_UTF16LE, CH_UNIX, 
117                              &c2, 2, 
118                              p, sizeof(fname)-strlen(fname), True);
119         p[len] = 0;
120         return fname;
121 }
122
123 bool torture_casetable(int dummy)
124 {
125         static struct cli_state *cli;
126         char *fname;
127         uint16_t fnum;
128         int c, i;
129 #define MAX_EQUIVALENCE 8
130         smb_ucs2_t equiv[0x10000][MAX_EQUIVALENCE];
131         printf("starting casetable\n");
132
133         if (!torture_open_connection(&cli, 0)) {
134                 return False;
135         }
136
137         memset(equiv, 0, sizeof(equiv));
138
139         cli_unlink(cli, "\\utable\\*", FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
140         cli_rmdir(cli, "\\utable");
141         if (!NT_STATUS_IS_OK(cli_mkdir(cli, "\\utable"))) {
142                 printf("Failed to create utable directory!\n");
143                 return False;
144         }
145
146         for (c=1; c < 0x10000; c++) {
147                 SMB_OFF_T size;
148
149                 if (c == '.' || c == '\\') continue;
150
151                 printf("%04x (%c)\n", c, isprint(c)?c:'.');
152
153                 fname = form_name(c);
154                 if (!NT_STATUS_IS_OK(cli_ntcreate(cli, fname, 0,
155                                           GENERIC_ALL_ACCESS, 
156                                           FILE_ATTRIBUTE_NORMAL,
157                                           FILE_SHARE_NONE,
158                                           FILE_OPEN_IF, 0, 0, &fnum))) {
159                         printf("Failed to create file with char %04x\n", c);
160                         continue;
161                 }
162
163                 size = 0;
164
165                 if (!NT_STATUS_IS_OK(cli_qfileinfo_basic(
166                                              cli, fnum, NULL, &size,
167                                              NULL, NULL, NULL, NULL, NULL))) {
168                         continue;
169                 }
170
171                 if (size > 0) {
172                         /* found a character equivalence! */
173                         int c2[MAX_EQUIVALENCE];
174
175                         if (size/sizeof(int) >= MAX_EQUIVALENCE) {
176                                 printf("too many chars match?? size=%ld c=0x%04x\n",
177                                        (unsigned long)size, c);
178                                 cli_close(cli, fnum);
179                                 return False;
180                         }
181
182                         cli_read(cli, fnum, (char *)c2, 0, size);
183                         printf("%04x: ", c);
184                         equiv[c][0] = c;
185                         for (i=0; i<size/sizeof(int); i++) {
186                                 printf("%04x ", c2[i]);
187                                 equiv[c][i+1] = c2[i];
188                         }
189                         printf("\n");
190                         fflush(stdout);
191                 }
192
193                 cli_writeall(cli, fnum, 0, (uint8_t *)&c, size, sizeof(c),
194                              NULL);
195                 cli_close(cli, fnum);
196         }
197
198         cli_unlink(cli, "\\utable\\*", FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN);
199         cli_rmdir(cli, "\\utable");
200
201         return True;
202 }