|
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
|
2 /* This Source Code Form is subject to the terms of the Mozilla Public |
|
3 * License, v. 2.0. If a copy of the MPL was not distributed with this |
|
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
|
5 |
|
6 #include <stdio.h> |
|
7 #include "nsXPCOM.h" |
|
8 #include "nsIEntityConverter.h" |
|
9 #include "nsISaveAsCharset.h" |
|
10 #include "nsCOMPtr.h" |
|
11 #include "nsIUnicodeNormalizer.h" |
|
12 #include "nsStringAPI.h" |
|
13 #include "nsUnicharUtils.h" |
|
14 #include "nsMemory.h" |
|
15 #include "nsComponentManagerUtils.h" |
|
16 #include "nsServiceManagerUtils.h" |
|
17 |
|
18 NS_DEFINE_CID(kEntityConverterCID, NS_ENTITYCONVERTER_CID); |
|
19 NS_DEFINE_CID(kSaveAsCharsetCID, NS_SAVEASCHARSET_CID); |
|
20 NS_DEFINE_CID(kUnicodeNormalizerCID, NS_UNICODE_NORMALIZER_CID); |
|
21 |
|
22 #define TESTLEN 32 |
|
23 #define T2LEN TESTLEN |
|
24 #define T3LEN TESTLEN |
|
25 #define T4LEN TESTLEN |
|
26 |
|
27 // test data for ToUpper |
|
28 static char16_t t2data [T2LEN+1] = { |
|
29 0x0031 , // 0 |
|
30 0x0019 , // 1 |
|
31 0x0043 , // 2 |
|
32 0x0067 , // 3 |
|
33 0x00C8 , // 4 |
|
34 0x00E9 , // 5 |
|
35 0x0147 , // 6 |
|
36 0x01C4 , // 7 |
|
37 0x01C6 , // 8 |
|
38 0x01C5 , // 9 |
|
39 0x03C0 , // 10 |
|
40 0x03B2 , // 11 |
|
41 0x0438 , // 12 |
|
42 0x04A5 , // 13 |
|
43 0x05D0 , // 14 |
|
44 0x0A20 , // 15 |
|
45 0x30B0 , // 16 |
|
46 0x5185 , // 17 |
|
47 0xC021 , // 18 |
|
48 0xFF48 , // 19 |
|
49 0x01C7 , // 20 |
|
50 0x01C8 , // 21 |
|
51 0x01C9 , // 22 |
|
52 0x01CA , // 23 |
|
53 0x01CB , // 24 |
|
54 0x01CC , // 25 |
|
55 0x01F1 , // 26 |
|
56 0x01F2 , // 27 |
|
57 0x01F3 , // 28 |
|
58 0x0250 , // 29 |
|
59 0x0271 , // 30 |
|
60 0xA641 , // 31 |
|
61 0x00 |
|
62 }; |
|
63 // expected result for ToUpper |
|
64 static char16_t t2result[T2LEN+1] = { |
|
65 0x0031 , // 0 |
|
66 0x0019 , // 1 |
|
67 0x0043 , // 2 |
|
68 0x0047 , // 3 |
|
69 0x00C8 , // 4 |
|
70 0x00C9 , // 5 |
|
71 0x0147 , // 6 |
|
72 0x01C4 , // 7 |
|
73 0x01C4 , // 8 |
|
74 0x01C4 , // 9 |
|
75 0x03A0 , // 10 |
|
76 0x0392 , // 11 |
|
77 0x0418 , // 12 |
|
78 0x04A4 , // 13 |
|
79 0x05D0 , // 14 |
|
80 0x0A20 , // 15 |
|
81 0x30B0 , // 16 |
|
82 0x5185 , // 17 |
|
83 0xC021 , // 18 |
|
84 0xFF28 , // 19 |
|
85 0x01C7 , // 20 |
|
86 0x01C7 , // 21 |
|
87 0x01C7 , // 22 |
|
88 0x01CA , // 23 |
|
89 0x01CA , // 24 |
|
90 0x01CA , // 25 |
|
91 0x01F1 , // 26 |
|
92 0x01F1 , // 27 |
|
93 0x01F1 , // 28 |
|
94 0x2C6F , // 29 |
|
95 0x2C6E , // 30 |
|
96 0xA640 , // 31 |
|
97 0x00 |
|
98 }; |
|
99 // test data for ToLower |
|
100 static char16_t t3data [T3LEN+1] = { |
|
101 0x0031 , // 0 |
|
102 0x0019 , // 1 |
|
103 0x0043 , // 2 |
|
104 0x0067 , // 3 |
|
105 0x00C8 , // 4 |
|
106 0x00E9 , // 5 |
|
107 0x0147 , // 6 |
|
108 0x01C4 , // 7 |
|
109 0x01C6 , // 8 |
|
110 0x01C5 , // 9 |
|
111 0x03A0 , // 10 |
|
112 0x0392 , // 11 |
|
113 0x0418 , // 12 |
|
114 0x04A4 , // 13 |
|
115 0x05D0 , // 14 |
|
116 0x0A20 , // 15 |
|
117 0x30B0 , // 16 |
|
118 0x5187 , // 17 |
|
119 0xC023 , // 18 |
|
120 0xFF28 , // 19 |
|
121 0x01C7 , // 20 |
|
122 0x01C8 , // 21 |
|
123 0x01C9 , // 22 |
|
124 0x01CA , // 23 |
|
125 0x01CB , // 24 |
|
126 0x01CC , // 25 |
|
127 0x01F1 , // 26 |
|
128 0x01F2 , // 27 |
|
129 0x01F3 , // 28 |
|
130 0x2C6F , // 29 |
|
131 0x2C6E , // 30 |
|
132 0xA640 , // 31 |
|
133 0x00 |
|
134 }; |
|
135 // expected result for ToLower |
|
136 static char16_t t3result[T3LEN+1] = { |
|
137 0x0031 , // 0 |
|
138 0x0019 , // 1 |
|
139 0x0063 , // 2 |
|
140 0x0067 , // 3 |
|
141 0x00E8 , // 4 |
|
142 0x00E9 , // 5 |
|
143 0x0148 , // 6 |
|
144 0x01C6 , // 7 |
|
145 0x01C6 , // 8 |
|
146 0x01C6 , // 9 |
|
147 0x03C0 , // 10 |
|
148 0x03B2 , // 11 |
|
149 0x0438 , // 12 |
|
150 0x04A5 , // 13 |
|
151 0x05D0 , // 14 |
|
152 0x0A20 , // 15 |
|
153 0x30B0 , // 16 |
|
154 0x5187 , // 17 |
|
155 0xC023 , // 18 |
|
156 0xFF48 , // 19 |
|
157 0x01C9 , // 20 |
|
158 0x01C9 , // 21 |
|
159 0x01C9 , // 22 |
|
160 0x01CC , // 23 |
|
161 0x01CC , // 24 |
|
162 0x01CC , // 25 |
|
163 0x01F3 , // 26 |
|
164 0x01F3 , // 27 |
|
165 0x01F3 , // 28 |
|
166 0x0250 , // 29 |
|
167 0x0271 , // 30 |
|
168 0xA641 , // 31 |
|
169 0x00 |
|
170 }; |
|
171 // test data for ToTitle |
|
172 static char16_t t4data [T4LEN+2] = { |
|
173 0x0031 , // 0 |
|
174 0x0019 , // 1 |
|
175 0x0043 , // 2 |
|
176 0x0067 , // 3 |
|
177 0x00C8 , // 4 |
|
178 0x00E9 , // 5 |
|
179 0x0147 , // 6 |
|
180 0x01C4 , // 7 |
|
181 0x01C6 , // 8 |
|
182 0x01C5 , // 9 |
|
183 0x03C0 , // 10 |
|
184 0x03B2 , // 11 |
|
185 0x0438 , // 12 |
|
186 0x04A5 , // 13 |
|
187 0x05D0 , // 14 |
|
188 0x0A20 , // 15 |
|
189 0x30B0 , // 16 |
|
190 0x5189 , // 17 |
|
191 0xC013 , // 18 |
|
192 0xFF52 , // 19 |
|
193 0x01C7 , // 20 |
|
194 0x01C8 , // 21 |
|
195 0x01C9 , // 22 |
|
196 0x01CA , // 23 |
|
197 0x01CB , // 24 |
|
198 0x01CC , // 25 |
|
199 0x01F1 , // 26 |
|
200 0x01F2 , // 27 |
|
201 0x01F3 , // 28 |
|
202 0x0250 , // 29 |
|
203 0x0271 , // 30 |
|
204 0xA641 , // 31 |
|
205 0x0041 , // Dummy entry to prevent overflow |
|
206 0x00 |
|
207 }; |
|
208 // expected result for ToTitle |
|
209 static char16_t t4result[T4LEN+2] = { |
|
210 0x0031 , // 0 |
|
211 0x0019 , // 1 |
|
212 0x0043 , // 2 |
|
213 0x0047 , // 3 |
|
214 0x00C8 , // 4 |
|
215 0x00C9 , // 5 |
|
216 0x0147 , // 6 |
|
217 0x01C4 , // 7 |
|
218 0x01C5 , // 8 |
|
219 0x01C5 , // 9 |
|
220 0x03A0 , // 10 |
|
221 0x0392 , // 11 |
|
222 0x0418 , // 12 |
|
223 0x04A4 , // 13 |
|
224 0x05D0 , // 14 |
|
225 0x0A20 , // 15 |
|
226 0x30B0 , // 16 |
|
227 0x5189 , // 17 |
|
228 0xC013 , // 18 |
|
229 0xFF32 , // 19 |
|
230 0x01C7 , // 20 |
|
231 0x01C8 , // 21 |
|
232 0x01C8 , // 22 |
|
233 0x01CA , // 23 |
|
234 0x01CB , // 24 |
|
235 0x01CB , // 25 |
|
236 0x01F1 , // 26 |
|
237 0x01F2 , // 27 |
|
238 0x01F2 , // 28 |
|
239 0x2C6F , // 29 |
|
240 0x2C6E , // 30 |
|
241 0xA640 , // 31 |
|
242 0x0041 , // Dummy entry to prevent overflow |
|
243 0x00 |
|
244 }; |
|
245 |
|
246 static unsigned char t6lhs[] = { |
|
247 0x31 , // 0 |
|
248 0x19 , // 1 |
|
249 0x43 , // 2 |
|
250 0x67 , // 3 |
|
251 0xC3, 0x88 , // 4 |
|
252 0xC3, 0xA9 , // 5 |
|
253 0xC5, 0x87 , // 6 |
|
254 0xC7, 0x84 , // 7 |
|
255 0xC7, 0x86 , // 8 |
|
256 0xC7, 0x85 , // 9 |
|
257 0xCF, 0x80 , // 10 |
|
258 0xCE, 0xB2 , // 11 |
|
259 0xD0, 0xB8 , // 12 |
|
260 0xD2, 0xA5 , // 13 |
|
261 0xD7, 0x90 , // 14 |
|
262 0xE0, 0xA8, 0xA0 , // 15 |
|
263 0xE3, 0x82, 0xB0 , // 16 |
|
264 0xE5, 0x86, 0x85 , // 17 |
|
265 0xEC, 0x80, 0xA1 , // 18 |
|
266 0xEF, 0xBD, 0x88 , // 19 |
|
267 0xC7, 0x87 , // 20 |
|
268 0xC7, 0x88 , // 21 |
|
269 0xC7, 0x89 , // 22 |
|
270 0xC7, 0x8A , // 23 |
|
271 0xC7, 0x8B , // 24 |
|
272 0xC7, 0x8C , // 25 |
|
273 0xC7, 0xB1 , // 26 |
|
274 0xC7, 0xB2 , // 27 |
|
275 0xC7, 0xB3 , // 28 |
|
276 0xC9, 0x90 , // 29 |
|
277 0xC9, 0xB1 , // 30 |
|
278 0xEA, 0x99, 0x81 , // 31 |
|
279 0x00 |
|
280 }; |
|
281 |
|
282 static unsigned char t6rhs[] = { |
|
283 0x31 , // 0 |
|
284 0x19 , // 1 |
|
285 0x43 , // 2 |
|
286 0x47 , // 3 |
|
287 0xC3, 0x88 , // 4 |
|
288 0xC3, 0x89 , // 5 |
|
289 0xC5, 0x87 , // 6 |
|
290 0xC7, 0x84 , // 7 |
|
291 0xC7, 0x84 , // 8 |
|
292 0xC7, 0x84 , // 9 |
|
293 0xCE, 0xA0 , // 10 |
|
294 0xCE, 0x92 , // 11 |
|
295 0xD0, 0x98 , // 12 |
|
296 0xD2, 0xA4 , // 13 |
|
297 0xD7, 0x90 , // 14 |
|
298 0xE0, 0xA8, 0xA0 , // 15 |
|
299 0xE3, 0x82, 0xB0 , // 16 |
|
300 0xE5, 0x86, 0x85 , // 17 |
|
301 0xEC, 0x80, 0xA1 , // 18 |
|
302 0xEF, 0xBC, 0xA8 , // 19 |
|
303 0xC7, 0x87 , // 20 |
|
304 0xC7, 0x87 , // 21 |
|
305 0xC7, 0x87 , // 22 |
|
306 0xC7, 0x8a , // 23 |
|
307 0xC7, 0x8a , // 24 |
|
308 0xC7, 0x8a , // 25 |
|
309 0xC7, 0xB1 , // 26 |
|
310 0xC7, 0xB1 , // 27 |
|
311 0xC7, 0xB1 , // 28 |
|
312 0xE2, 0xB1, 0xAF , // 29 |
|
313 0xE2, 0xB1, 0xAE , // 30 |
|
314 0xEA, 0x99, 0x80 , // 31 |
|
315 0x00 |
|
316 }; |
|
317 |
|
318 static const char *t7lhs = "aBcDeFGHIJKL1!!2!!a!uuuu"; |
|
319 static const char *t7rhs = "AbCdEFghijkL1!!2!!A!UUuU"; |
|
320 |
|
321 static const char *t8lhs = "aazzz"; |
|
322 static const char *t8rhs = "aBa"; |
|
323 |
|
324 static const char *t9lhs = "@a"; |
|
325 static const char *t9rhs = "`a"; |
|
326 |
|
327 bool CharByCharCompareEqual(const char *a, const char *b, |
|
328 uint32_t aLen, uint32_t bLen) |
|
329 { |
|
330 // Do basically a CaseInsensitiveCompare(), but using |
|
331 // CaseInsensitiveUTF8CharsEqual(). |
|
332 |
|
333 const char *aEnd = a + aLen; |
|
334 const char *bEnd = b + bLen; |
|
335 while (a < aEnd && b < bEnd) { |
|
336 bool err; |
|
337 if (!CaseInsensitiveUTF8CharsEqual(a, b, aEnd, bEnd, &a, &b, &err) || err) |
|
338 return false; |
|
339 } |
|
340 return true; |
|
341 } |
|
342 |
|
343 void TestCaseConversion() |
|
344 { |
|
345 printf("==========================\n"); |
|
346 printf("Start case conversion test\n"); |
|
347 printf("==========================\n"); |
|
348 |
|
349 int i; |
|
350 char16_t buf[256]; |
|
351 |
|
352 printf("Test 1 - ToUpper(char16_t, char16_t*):\n"); |
|
353 for(i=0;i < T2LEN ; i++) |
|
354 { |
|
355 char16_t ch = ToUpperCase(t2data[i]); |
|
356 if(ch != t2result[i]) |
|
357 printf("\tFailed!! result unexpected %d\n", i); |
|
358 } |
|
359 |
|
360 |
|
361 printf("Test 2 - ToLower(char16_t, char16_t*):\n"); |
|
362 for(i=0;i < T3LEN; i++) |
|
363 { |
|
364 char16_t ch = ToLowerCase(t3data[i]); |
|
365 if(ch != t3result[i]) |
|
366 printf("\tFailed!! result unexpected %d\n", i); |
|
367 } |
|
368 |
|
369 printf("Test 3 - ToTitle(char16_t, char16_t*):\n"); |
|
370 for(i=0;i < T4LEN; i++) |
|
371 { |
|
372 char16_t ch = ToTitleCase(t4data[i]); |
|
373 if(ch != t4result[i]) |
|
374 printf("\tFailed!! result unexpected %d\n", i); |
|
375 } |
|
376 |
|
377 printf("Test 4 - ToUpper(char16_t*, char16_t*, uint32_t):\n"); |
|
378 ToUpperCase(t2data, buf, T2LEN); |
|
379 for(i = 0; i < T2LEN; i++) |
|
380 { |
|
381 if(buf[i] != t2result[i]) |
|
382 { |
|
383 printf("\tFailed!! result unexpected %d\n", i); |
|
384 break; |
|
385 } |
|
386 } |
|
387 |
|
388 printf("Test 5 - ToLower(char16_t*, char16_t*, uint32_t):\n"); |
|
389 ToLowerCase(t3data, buf, T3LEN); |
|
390 for(i = 0; i < T3LEN; i++) |
|
391 { |
|
392 if(buf[i] != t3result[i]) |
|
393 { |
|
394 printf("\tFailed!! result unexpected %d\n", i); |
|
395 break; |
|
396 } |
|
397 } |
|
398 |
|
399 printf("Test 6 - CaseInsensitiveCompare UTF-8 (1):\n"); |
|
400 if (CaseInsensitiveCompare((char*)t6lhs, (char*)t6rhs, sizeof(t6lhs), sizeof(t6rhs))) |
|
401 printf("\tFailed!\n"); |
|
402 if (!CharByCharCompareEqual((char*)t6lhs, (char*)t6rhs, sizeof(t6lhs), sizeof(t6rhs))) |
|
403 printf("\tFailed character-by-character comparison!\n"); |
|
404 |
|
405 printf("Test 7 - CaseInsensitiveCompare UTF-8 (2):\n"); |
|
406 if (CaseInsensitiveCompare(t7lhs, t7rhs, strlen(t7lhs), strlen(t7rhs))) |
|
407 printf("\tFailed!\n"); |
|
408 if (!CharByCharCompareEqual(t7lhs, t7rhs, sizeof(t7lhs), sizeof(t7rhs))) |
|
409 printf("\tFailed character-by-character comparison!\n"); |
|
410 |
|
411 printf("Test 8a - CaseInsensitiveCompare UTF-8 (3):\n"); |
|
412 if (CaseInsensitiveCompare(t8lhs, t8rhs, strlen(t8lhs), strlen(t8rhs)) != -1) |
|
413 printf("\tFailed!\n"); |
|
414 if (CharByCharCompareEqual(t8lhs, t8rhs, strlen(t8lhs), strlen(t8rhs))) |
|
415 printf("\tFailed character-by-character comparison!\n"); |
|
416 |
|
417 printf("Test 8b - CaseInsensitiveCompare UTF-8 (4):\n"); |
|
418 if (CaseInsensitiveCompare(t8rhs, t8lhs, strlen(t8rhs), strlen(t8lhs)) != 1) |
|
419 printf("\tFailed!\n"); |
|
420 |
|
421 // This test may seem a bit strange. But it's actually an easy bug to make |
|
422 // if we tried to be clever and say that two ASCII characters x and y are |
|
423 // case-insensitively equal if (x & ~0x20) == (y & ~0x20). |
|
424 printf("Test 9 - CaseInsensitiveCompare UTF-8 (5):\n"); |
|
425 if (CaseInsensitiveCompare(t9rhs, t9lhs, strlen(t9lhs), strlen(t9rhs)) != 1) |
|
426 printf("\tFailed!\n"); |
|
427 if (CharByCharCompareEqual(t9lhs, t9rhs, strlen(t9lhs), strlen(t9rhs))) |
|
428 printf("\tFailed character-by-character comparison!\n"); |
|
429 |
|
430 printf("===========================\n"); |
|
431 printf("Finish case conversion test\n"); |
|
432 printf("===========================\n"); |
|
433 } |
|
434 |
|
435 static void FuzzOneInvalidCaseConversion() |
|
436 { |
|
437 uint32_t aLen = rand() % 32; |
|
438 uint32_t bLen = rand() % 32; |
|
439 |
|
440 // We could use a static length-32 buffer for these, but then Valgrind |
|
441 // wouldn't be able to detect errors. |
|
442 unsigned char *aBuf = (unsigned char*)malloc(aLen * sizeof(unsigned char)); |
|
443 unsigned char *bBuf = (unsigned char*)malloc(bLen * sizeof(unsigned char)); |
|
444 |
|
445 for (uint32_t i = 0; i < aLen; i++) { |
|
446 aBuf[i] = rand() & 0xff; |
|
447 } |
|
448 |
|
449 for (uint32_t i = 0; i < bLen; i++) { |
|
450 bBuf[i] = rand() & 0xff; |
|
451 } |
|
452 |
|
453 if (!CaseInsensitiveCompare((char*)aBuf, (char*)bBuf, aLen, bLen)) |
|
454 printf("\tSurprise, two random strings compared insensitively as equal!\n"); |
|
455 if (CharByCharCompareEqual((char*)aBuf, (char*)bBuf, aLen, bLen)) |
|
456 printf("\tSurprise, two random strings compared as exactly equal!\n"); |
|
457 |
|
458 free(aBuf); |
|
459 free(bBuf); |
|
460 } |
|
461 |
|
462 static void FuzzCaseConversion() |
|
463 { |
|
464 printf("==========================\n"); |
|
465 printf("Start fuzz case conversion\n"); |
|
466 printf("==========================\n"); |
|
467 |
|
468 srand(0); |
|
469 |
|
470 printf("Fuzzing invalid UTF8 data...\n"); |
|
471 for (uint32_t i = 0; i < 100000; i++) { |
|
472 FuzzOneInvalidCaseConversion(); |
|
473 } |
|
474 |
|
475 printf("===========================\n"); |
|
476 printf("Finish fuzz case conversion\n"); |
|
477 printf("===========================\n"); |
|
478 } |
|
479 |
|
480 static void TestEntityConversion(uint32_t version) |
|
481 { |
|
482 printf("==============================\n"); |
|
483 printf("Start nsIEntityConverter Test \n"); |
|
484 printf("==============================\n"); |
|
485 |
|
486 uint32_t i; |
|
487 nsString inString; |
|
488 char16_t uChar; |
|
489 nsresult res; |
|
490 |
|
491 |
|
492 inString.Assign(NS_ConvertASCIItoUTF16("\xA0\xA1\xA2\xA3")); |
|
493 uChar = (char16_t) 8364; //euro |
|
494 inString.Append(&uChar, 1); |
|
495 uChar = (char16_t) 9830; // |
|
496 inString.Append(&uChar, 1); |
|
497 |
|
498 nsCOMPtr <nsIEntityConverter> entityConv = do_CreateInstance(kEntityConverterCID, &res);; |
|
499 if (NS_FAILED(res)) {printf("\tFailed!! return value != NS_OK\n"); return;} |
|
500 |
|
501 const char16_t *data; |
|
502 uint32_t length = NS_StringGetData(inString, &data); |
|
503 |
|
504 // convert char by char |
|
505 for (i = 0; i < length; i++) { |
|
506 char *entity = nullptr; |
|
507 res = entityConv->ConvertToEntity(data[i], version, &entity); |
|
508 if (NS_SUCCEEDED(res) && entity) { |
|
509 printf("%c %s\n", data[i], entity); |
|
510 nsMemory::Free(entity); |
|
511 } |
|
512 } |
|
513 |
|
514 // convert at once as a string |
|
515 char16_t *entities; |
|
516 res = entityConv->ConvertToEntities(inString.get(), version, &entities); |
|
517 if (NS_SUCCEEDED(res) && entities) { |
|
518 for (char16_t *centity = entities; *centity; ++centity) { |
|
519 printf("%c", (char) *centity); |
|
520 if (';' == (char) *centity) |
|
521 printf("\n"); |
|
522 } |
|
523 nsMemory::Free(entities); |
|
524 } |
|
525 |
|
526 printf("==============================\n"); |
|
527 printf("Finish nsIEntityConverter Test \n"); |
|
528 printf("==============================\n\n"); |
|
529 } |
|
530 |
|
531 static void TestSaveAsCharset() |
|
532 { |
|
533 printf("==============================\n"); |
|
534 printf("Start nsISaveAsCharset Test \n"); |
|
535 printf("==============================\n"); |
|
536 |
|
537 nsresult res; |
|
538 |
|
539 nsString inString; |
|
540 inString.Assign(NS_ConvertASCIItoUTF16("\x61\x62\x80\xA0\x63")); |
|
541 char *outString; |
|
542 |
|
543 const char16_t *data; |
|
544 uint32_t length = NS_StringGetData(inString, &data); |
|
545 |
|
546 // first, dump input string |
|
547 for (uint32_t i = 0; i < length; i++) { |
|
548 printf("%c ", data[i]); |
|
549 } |
|
550 printf("\n"); |
|
551 |
|
552 nsCOMPtr <nsISaveAsCharset> saveAsCharset = do_CreateInstance(kSaveAsCharsetCID, &res); |
|
553 if (NS_FAILED(res)) {printf("\tFailed!! return value != NS_OK\n");} |
|
554 |
|
555 printf("ISO-8859-1 attr_plainTextDefault entityNone\n"); |
|
556 res = saveAsCharset->Init("ISO-8859-1", |
|
557 nsISaveAsCharset::attr_plainTextDefault, |
|
558 nsIEntityConverter::entityNone); |
|
559 if (NS_FAILED(res)) {printf("\tFailed!! return value != NS_OK\n");} |
|
560 res = saveAsCharset->Convert(inString.get(), &outString); |
|
561 if (NS_FAILED(res)) {printf("\tFailed!! return value != NS_OK\n");} |
|
562 if (!outString) {printf("\tFailed!! output null\n");} |
|
563 else {printf("%s\n", outString); nsMemory::Free(outString);} |
|
564 |
|
565 printf("ISO-2022-JP attr_plainTextDefault entityNone\n"); |
|
566 res = saveAsCharset->Init("ISO-2022-JP", |
|
567 nsISaveAsCharset::attr_plainTextDefault, |
|
568 nsIEntityConverter::entityNone); |
|
569 if (NS_FAILED(res)) {printf("\tFailed!! return value != NS_OK\n");} |
|
570 res = saveAsCharset->Convert(inString.get(), &outString); |
|
571 if (NS_FAILED(res)) {printf("\tFailed!! return value != NS_OK\n");} |
|
572 if (!outString) {printf("\tFailed!! output null\n");} |
|
573 else {printf("%s\n", outString); nsMemory::Free(outString);} |
|
574 if (NS_ERROR_UENC_NOMAPPING == res) { |
|
575 outString = ToNewUTF8String(inString); |
|
576 if (!outString) {printf("\tFailed!! output null\n");} |
|
577 else {printf("Fall back to UTF-8: %s\n", outString); nsMemory::Free(outString);} |
|
578 } |
|
579 |
|
580 printf("ISO-2022-JP attr_FallbackQuestionMark entityNone\n"); |
|
581 res = saveAsCharset->Init("ISO-2022-JP", |
|
582 nsISaveAsCharset::attr_FallbackQuestionMark, |
|
583 nsIEntityConverter::entityNone); |
|
584 if (NS_FAILED(res)) {printf("\tFailed!! return value != NS_OK\n");} |
|
585 res = saveAsCharset->Convert(inString.get(), &outString); |
|
586 if (NS_FAILED(res)) {printf("\tFailed!! return value != NS_OK\n");} |
|
587 if (!outString) {printf("\tFailed!! output null\n");} |
|
588 else {printf("%s\n", outString); nsMemory::Free(outString);} |
|
589 |
|
590 printf("ISO-2022-JP attr_FallbackEscapeU entityNone\n"); |
|
591 res = saveAsCharset->Init("ISO-2022-JP", |
|
592 nsISaveAsCharset::attr_FallbackEscapeU, |
|
593 nsIEntityConverter::entityNone); |
|
594 if (NS_FAILED(res)) {printf("\tFailed!! return value != NS_OK\n");} |
|
595 res = saveAsCharset->Convert(inString.get(), &outString); |
|
596 if (NS_FAILED(res)) {printf("\tFailed!! return value != NS_OK\n");} |
|
597 if (!outString) {printf("\tFailed!! output null\n");} |
|
598 else {printf("%s\n", outString); nsMemory::Free(outString);} |
|
599 |
|
600 printf("ISO-8859-1 attr_htmlTextDefault html40Latin1\n"); |
|
601 res = saveAsCharset->Init("ISO-8859-1", |
|
602 nsISaveAsCharset::attr_htmlTextDefault, |
|
603 nsIEntityConverter::html40Latin1); |
|
604 if (NS_FAILED(res)) {printf("\tFailed!! return value != NS_OK\n");} |
|
605 res = saveAsCharset->Convert(inString.get(), &outString); |
|
606 if (NS_ERROR_UENC_NOMAPPING != res && NS_FAILED(res)) {printf("\tFailed!! return value != NS_OK\n");} |
|
607 if (!outString) {printf("\tFailed!! output null\n");} |
|
608 else {printf("%s\n", outString); nsMemory::Free(outString);} |
|
609 |
|
610 printf("ISO-8859-1 attr_FallbackHexNCR+attr_EntityAfterCharsetConv html40Latin1 \n"); |
|
611 res = saveAsCharset->Init("ISO-8859-1", |
|
612 nsISaveAsCharset::attr_FallbackHexNCR + |
|
613 nsISaveAsCharset::attr_EntityAfterCharsetConv, |
|
614 nsIEntityConverter::html40Latin1); |
|
615 if (NS_FAILED(res)) {printf("\tFailed!! return value != NS_OK\n");} |
|
616 res = saveAsCharset->Convert(inString.get(), &outString); |
|
617 if (NS_ERROR_UENC_NOMAPPING != res && NS_FAILED(res)) {printf("\tFailed!! return value != NS_OK\n");} |
|
618 if (!outString) {printf("\tFailed!! output null\n");} |
|
619 else {printf("%s\n", outString); nsMemory::Free(outString);} |
|
620 |
|
621 |
|
622 printf("==============================\n"); |
|
623 printf("Finish nsISaveAsCharset Test \n"); |
|
624 printf("==============================\n\n"); |
|
625 } |
|
626 |
|
627 static char16_t normStr[] = |
|
628 { |
|
629 0x00E1, |
|
630 0x0061, |
|
631 0x0301, |
|
632 0x0107, |
|
633 0x0063, |
|
634 0x0301, |
|
635 0x0000 |
|
636 }; |
|
637 |
|
638 static char16_t nfdForm[] = |
|
639 { |
|
640 0x0061, |
|
641 0x0301, |
|
642 0x0061, |
|
643 0x0301, |
|
644 0x0063, |
|
645 0x0301, |
|
646 0x0063, |
|
647 0x0301, |
|
648 0x0000 |
|
649 }; |
|
650 |
|
651 void TestNormalization() |
|
652 { |
|
653 printf("==============================\n"); |
|
654 printf("Start nsIUnicodeNormalizer Test \n"); |
|
655 printf("==============================\n"); |
|
656 nsIUnicodeNormalizer *t = nullptr; |
|
657 nsresult res; |
|
658 res = CallGetService(kUnicodeNormalizerCID, &t); |
|
659 |
|
660 printf("Test 1 - GetService():\n"); |
|
661 if(NS_FAILED(res) || !t) { |
|
662 printf("\t1st Norm GetService failed\n"); |
|
663 } else { |
|
664 NS_RELEASE(t); |
|
665 } |
|
666 |
|
667 res = CallGetService(kUnicodeNormalizerCID, &t); |
|
668 |
|
669 if(NS_FAILED(res) || !t) { |
|
670 printf("\t2nd GetService failed\n"); |
|
671 } else { |
|
672 printf("Test 2 - NormalizeUnicode(uint32_t, const nsAString&, nsAString&):\n"); |
|
673 nsAutoString resultStr; |
|
674 res = t->NormalizeUnicodeNFD(nsDependentString(normStr), resultStr); |
|
675 if (resultStr.Equals(nsDependentString(nfdForm))) { |
|
676 printf(" Succeeded in NFD UnicodeNormalizer test. \n"); |
|
677 } else { |
|
678 printf(" Failed in NFD UnicodeNormalizer test. \n"); |
|
679 } |
|
680 |
|
681 NS_RELEASE(t); |
|
682 } |
|
683 printf("==============================\n"); |
|
684 printf("Finish nsIUnicodeNormalizer Test \n"); |
|
685 printf("==============================\n"); |
|
686 |
|
687 } |
|
688 |
|
689 |
|
690 int main(int argc, char** argv) { |
|
691 |
|
692 nsresult rv = NS_InitXPCOM2(nullptr, nullptr, nullptr); |
|
693 if (NS_FAILED(rv)) { |
|
694 printf("NS_InitXPCOM2 failed\n"); |
|
695 return 1; |
|
696 } |
|
697 |
|
698 // -------------------------------------------- |
|
699 |
|
700 TestCaseConversion(); |
|
701 |
|
702 // -------------------------------------------- |
|
703 |
|
704 FuzzCaseConversion(); |
|
705 |
|
706 // -------------------------------------------- |
|
707 |
|
708 TestEntityConversion(nsIEntityConverter::html40); |
|
709 |
|
710 // -------------------------------------------- |
|
711 |
|
712 TestSaveAsCharset(); |
|
713 |
|
714 // -------------------------------------------- |
|
715 |
|
716 TestNormalization(); |
|
717 |
|
718 // -------------------------------------------- |
|
719 printf("Finish All The Test Cases\n"); |
|
720 |
|
721 return 0; |
|
722 } |