1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
|
/*
* This file and its contents are supplied under the terms of the
* Common Development and Distribution License ("CDDL"), version 1.0.
* You may only use this file in accordance with the terms of version
* 1.0 of the CDDL.
*
* A full copy of the text of the CDDL should have accompanied this
* source. A copy of the CDDL is also available via the Internet at
* http://www.illumos.org/license/CDDL.
*/
/*
* Copyright 2018 Nexenta Systems, Inc. All rights reserved.
*/
/*
* Test conversion of strings UTF-8 to/from UTF-16 etc.
*
* This tests both 16-bit unicode symbols (UCS-2) and so called
* "enhanced" unicode symbols such as the "poop emoji" that are
* above 65535 and encode to four bytes as UTF-8.
*/
#include <sys/types.h>
#include <sys/debug.h>
#include <sys/u8_textprep.h>
#include <smbsrv/string.h>
#include <stdio.h>
#include <string.h>
#include "test_defs.h"
#define U_FW_A 0xff21 // full-width A (A)
static const char fwA[4] = "\xef\xbc\xa1";
#define U_POOP 0x1f4a9 // poop emoji (💩)
static const char poop[5] = "\xf0\x9f\x92\xa9";
static char mbsa[] = "A\xef\xbc\xa1."; // A fwA . (5)
static char mbsp[] = "P\xf0\x9f\x92\xa9."; // P poop . (6)
static smb_wchar_t wcsa[] = { 'A', U_FW_A, '.', 0 }; // (3)
static smb_wchar_t wcsp[] = { 'P', 0xd83d, 0xdca9, '.', 0 }; // (4)
static void
conv_wctomb()
{
char mbs[8];
int len;
len = smb_wctomb(mbs, U_FW_A);
if (len != 3) {
printf("Fail: conv_wctomb fwA ret=%d\n", len);
return;
}
mbs[len] = '\0';
if (strcmp(mbs, fwA)) {
printf("Fail: conv_wctomb fwA cmp:\n");
hexdump((uchar_t *)mbs, len+1);
return;
}
len = smb_wctomb(mbs, U_POOP);
if (len != 4) {
printf("Fail: conv_wctomb poop ret=%d\n", len);
return;
}
mbs[len] = '\0';
if (strcmp(mbs, poop)) {
printf("Fail: conv_wctomb poop cmp:\n");
hexdump((uchar_t *)mbs, len+1);
return;
}
/* null wc to mbs should return 1 and put a null */
len = smb_wctomb(mbs, 0);
if (len != 1) {
printf("Fail: conv_wctomb null ret=%d\n", len);
return;
}
if (mbs[0] != '\0') {
printf("Fail: conv_wctomb null cmp:\n");
hexdump((uchar_t *)mbs, len+1);
return;
}
printf("Pass: conv_wctomb\n");
}
static void
conv_mbtowc()
{
uint32_t wch = 0;
int len;
/*
* The (void *) cast here is to let this build both
* before and after an interface change in smb_mbtowc
* (uint16_t vs uint32_t)
*/
len = smb_mbtowc((void *)&wch, fwA, 4);
if (len != 3) {
printf("Fail: conv_mbtowc fwA ret=%d\n", len);
return;
}
if (wch != U_FW_A) {
printf("Fail: conv_mbtowc fwA cmp: 0x%x\n", wch);
return;
}
len = smb_mbtowc((void *)&wch, poop, 4); // poop emoji
if (len != 4) {
printf("Fail: conv_mbtowc poop ret=%d\n", len);
return;
}
if (wch != U_POOP) {
printf("Fail: conv_mbtowc poop cmp: 0x%x\n", wch);
return;
}
/* null mbs to wc should return 0 (and set wch=0) */
len = smb_mbtowc((void *)&wch, "", 4);
if (len != 0) {
printf("Fail: conv_mbtowc null ret=%d\n", len);
return;
}
if (wch != 0) {
printf("Fail: conv_mbtowc null cmp: 0x%x\n", wch);
return;
}
printf("Pass: conv_mbtowc\n");
}
static void
conv_wcstombs()
{
char tmbs[16];
int len;
len = smb_wcstombs(tmbs, wcsa, sizeof (tmbs));
if (len != 5) {
printf("Fail: conv_wcstombs A ret=%d\n", len);
return;
}
if (strcmp(tmbs, mbsa)) {
printf("Fail: conv_wcstombs A cmp:\n");
hexdump((uchar_t *)tmbs, len+2);
return;
}
len = smb_wcstombs(tmbs, wcsp, sizeof (tmbs));
if (len != 6) {
printf("Fail: conv_wcstombs f ret=%d\n", len);
return;
}
if (strcmp(tmbs, mbsp)) {
printf("Fail: conv_wcstombs f cmp:\n");
hexdump((uchar_t *)tmbs, len+2);
return;
}
printf("Pass: conv_wcstombs\n");
}
static void
conv_mbstowcs()
{
smb_wchar_t twcs[8];
uint32_t wch = 0;
int len;
len = smb_mbstowcs(twcs, mbsa, sizeof (twcs));
if (len != 3) {
printf("Fail: conv_mbstowcs A ret=%d\n", len);
return;
}
if (memcmp(twcs, wcsa, len+2)) {
printf("Fail: conv_mbstowcs A cmp: 0x%x\n", wch);
hexdump((uchar_t *)twcs, len+2);
return;
}
len = smb_mbstowcs(twcs, mbsp, sizeof (twcs));
if (len != 4) {
printf("Fail: conv_mbstowcs P ret=%d\n", len);
return;
}
if (memcmp(twcs, wcsp, len+2)) {
printf("Fail: conv_mbstowcs P cmp: 0x%x\n", wch);
hexdump((uchar_t *)twcs, len+2);
return;
}
printf("Pass: conv_mbstowcs\n");
}
/*
* An OEM string that will require iconv.
*/
static uchar_t fubar_oem[] = "F\201bar"; // CP850 x81 (ü)
static char fubar_mbs[] = "F\303\274bar"; // UTF8 xC3 xBC
static void
conv_oemtombs()
{
char tmbs[16];
int len;
len = smb_oemtombs(tmbs, (uchar_t *)"foo", 4);
if (len != 3) {
printf("Fail: conv_wctomb foo ret=%d\n", len);
return;
}
if (strcmp(tmbs, "foo")) {
printf("Fail: conv_wctomb foo cmp:\n");
hexdump((uchar_t *)tmbs, len+1);
return;
}
len = smb_oemtombs(tmbs, fubar_oem, 7);
if (len != 6) {
printf("Fail: conv_oemtombs fubar ret=%d\n", len);
return;
}
if (strcmp(tmbs, fubar_mbs)) {
printf("Fail: conv_oemtombs fubar cmp:\n");
hexdump((uchar_t *)tmbs, len+1);
return;
}
printf("Pass: conv_oemtombs\n");
}
static void
conv_mbstooem()
{
uint8_t oemcs[8];
uint32_t wch = 0;
int len;
len = smb_mbstooem(oemcs, "foo", 8);
if (len != 3) {
printf("Fail: conv_mbstooem foo ret=%d\n", len);
return;
}
if (memcmp(oemcs, "foo", len+1)) {
printf("Fail: conv_mbstooem P cmp: 0x%x\n", wch);
hexdump((uchar_t *)oemcs, len+1);
return;
}
len = smb_mbstooem(oemcs, fubar_mbs, 8);
if (len != 5) {
printf("Fail: conv_mbstooem fubar ret=%d\n", len);
return;
}
if (memcmp(oemcs, (char *)fubar_oem, len+1)) {
printf("Fail: conv_mbstooem fubar cmp: 0x%x\n", wch);
hexdump((uchar_t *)oemcs, len+1);
return;
}
len = smb_mbstooem(oemcs, mbsp, 8);
if (len != 3) {
printf("Fail: conv_mbstooem poop ret=%d\n", len);
return;
}
if (memcmp(oemcs, "P?.", len+1)) {
printf("Fail: conv_mbstooem poop cmp: 0x%x\n", wch);
hexdump((uchar_t *)oemcs, len+1);
return;
}
printf("Pass: conv_mbstooem\n");
}
static void
conv_sbequiv_strlen()
{
int len;
len = (int)smb_sbequiv_strlen("a");
if (len != 1) {
printf("Fail: conv_sbequiv_strlen (a) len=%d\n", len);
return;
}
len = (int)smb_sbequiv_strlen(fubar_mbs);
if (len != strlen((char *)fubar_oem)) {
printf("Fail: conv_sbequiv_strlen (fubar) len=%d\n", len);
return;
}
len = (int)smb_sbequiv_strlen(mbsp);
if (len != 3) { // "P?."
printf("Fail: conv_sbequiv_strlen (poop) len=%d\n", len);
return;
}
printf("Pass: conv_sbequiv_strlen\n");
}
static void
conv_wcequiv_strlen()
{
int len;
len = (int)smb_wcequiv_strlen("a");
if (len != 2) {
printf("Fail: conv_wcequiv_strlen (a) len=%d\n", len);
return;
}
len = (int)smb_wcequiv_strlen(fwA);
if (len != 2) {
printf("Fail: conv_wcequiv_strlen (fwA) len=%d\n", len);
return;
}
len = (int)smb_wcequiv_strlen(poop);
if (len != 4) {
printf("Fail: conv_wcequiv_strlen (poop) len=%d\n", len);
return;
}
printf("Pass: conv_wcequiv_strlen\n");
}
void
test_conv()
{
conv_wctomb();
conv_mbtowc();
conv_wcstombs();
conv_mbstowcs();
conv_oemtombs();
conv_mbstooem();
conv_sbequiv_strlen();
conv_wcequiv_strlen();
}
|