1 /* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
4 /*
5 * test_string2.c
6 *
7 * Tests International Strings
8 *
9 */
10
11 #include "testutil.h"
12 #include "testutil_nss.h"
13
14 static void *plContext = NULL;
15
16 static void
createString(PKIX_PL_String ** vivaEspanaString,PKIX_PL_String ** straussString,PKIX_PL_String ** gorbachevString,PKIX_PL_String ** testUTF16String,PKIX_PL_String ** chineseString,PKIX_PL_String ** jeanRenoString)17 createString(
18 PKIX_PL_String **vivaEspanaString,
19 PKIX_PL_String **straussString,
20 PKIX_PL_String **gorbachevString,
21 PKIX_PL_String **testUTF16String,
22 PKIX_PL_String **chineseString,
23 PKIX_PL_String **jeanRenoString)
24 {
25 /* this is meant to fail - it highlights bug 0002 */
26 unsigned char utf16String[4] = { 0xF8, 0x60,
27 0xFC, 0x60 };
28
29 unsigned char chinese[16] = { 0xe7, 0xab, 0xa0,
30 0xe5, 0xad, 0x90,
31 0xe6, 0x80, 0xa1,
32 0x20,
33 0xe4, 0xb8, 0xad,
34 0xe5, 0x9b, 0xbd };
35
36 char *jeanReno = "Jean R\303\251no is an actor.";
37 char *gorbachev = /* This is the name "Gorbachev" in cyrllic */
38 "\xd0\x93\xd0\xbe\xd1\x80\xd0\xb1\xd0\xb0\xd1\x87\xd1\x91\xd0\xb2";
39
40 char *vivaEspana =
41 "¡Viva España!";
42
43 char *strauss =
44 "Strauß was born in Österreich";
45
46 PKIX_TEST_STD_VARS();
47
48 /* ---------------------------- */
49 subTest("String Creation");
50
51 PKIX_TEST_EXPECT_NO_ERROR(PKIX_PL_String_Create(
52 PKIX_ESCASCII,
53 vivaEspana,
54 PL_strlen(vivaEspana),
55 vivaEspanaString,
56 plContext));
57
58 PKIX_TEST_EXPECT_NO_ERROR(PKIX_PL_String_Create(
59 PKIX_ESCASCII,
60 strauss,
61 PL_strlen(strauss),
62 straussString,
63 plContext));
64
65 PKIX_TEST_EXPECT_NO_ERROR(PKIX_PL_String_Create(
66 PKIX_UTF8,
67 gorbachev,
68 PL_strlen(gorbachev),
69 gorbachevString,
70 plContext));
71
72 PKIX_TEST_EXPECT_NO_ERROR(PKIX_PL_String_Create(
73 PKIX_UTF16,
74 utf16String,
75 4,
76 testUTF16String,
77 plContext));
78
79 PKIX_TEST_EXPECT_NO_ERROR(PKIX_PL_String_Create(
80 PKIX_UTF8,
81 chinese,
82 16,
83 chineseString,
84 plContext));
85
86 PKIX_TEST_EXPECT_NO_ERROR(PKIX_PL_String_Create(
87 PKIX_UTF8,
88 jeanReno,
89 PL_strlen(jeanReno),
90 jeanRenoString,
91 plContext));
92
93 cleanup:
94
95 PKIX_TEST_RETURN();
96 }
97
98 static void
testGetEncoded(PKIX_PL_String * string,PKIX_UInt32 format)99 testGetEncoded(PKIX_PL_String *string, PKIX_UInt32 format)
100 {
101 void *dest = NULL;
102 PKIX_UInt32 length;
103
104 PKIX_TEST_STD_VARS();
105
106 PKIX_TEST_EXPECT_NO_ERROR(PKIX_PL_String_GetEncoded(string,
107 format,
108 &dest,
109 &length,
110 plContext));
111
112 if (dest) {
113 (void)printf("\tResult: %s\n", (char *)dest);
114 PKIX_TEST_EXPECT_NO_ERROR(PKIX_PL_Free(dest, plContext));
115 }
116
117 cleanup:
118 PKIX_TEST_RETURN();
119 }
120
121 static void
testHTMLOutput(PKIX_PL_String * vivaEspanaString,PKIX_PL_String * straussString,PKIX_PL_String * gorbachevString,PKIX_PL_String * testUTF16String,PKIX_PL_String * chineseString,PKIX_PL_String * jeanRenoString)122 testHTMLOutput(
123 PKIX_PL_String *vivaEspanaString,
124 PKIX_PL_String *straussString,
125 PKIX_PL_String *gorbachevString,
126 PKIX_PL_String *testUTF16String,
127 PKIX_PL_String *chineseString,
128 PKIX_PL_String *jeanRenoString)
129 {
130 void *dest = NULL;
131 PKIX_UInt32 length;
132
133 FILE *htmlFile = NULL;
134
135 PKIX_TEST_STD_VARS();
136
137 /* Opening a file for output */
138 htmlFile = fopen("utf8.html", "w");
139
140 if (htmlFile != plContext) {
141 (void)fprintf(htmlFile, "<html><head>\n");
142 (void)fprintf(htmlFile, "<meta http-equiv=\"Content-Type\"");
143 (void)fprintf(htmlFile,
144 "content = \"text/html; charset = UTF-8\">\n");
145 (void)fprintf(htmlFile, "</head><body>\n");
146 (void)fprintf(htmlFile, "<font size =\"+2\">\n");
147 } else
148 (void)printf("Could not open HTML file\n");
149 PKIX_TEST_EXPECT_NO_ERROR(PKIX_PL_String_GetEncoded(testUTF16String,
150 PKIX_UTF8,
151 &dest,
152 &length,
153 plContext));
154 if (htmlFile != plContext) {
155 (void)printf("%d bytes written to HTML file\n",
156 fwrite(dest, length, 1, htmlFile));
157 (void)fprintf(htmlFile, "<BR>\n");
158 }
159
160 PKIX_TEST_EXPECT_NO_ERROR(PKIX_PL_Free(dest, plContext));
161 dest = NULL;
162 length = 0;
163
164 PKIX_TEST_EXPECT_NO_ERROR(PKIX_PL_String_GetEncoded(chineseString,
165 PKIX_UTF8,
166 &dest,
167 &length,
168 plContext));
169 if (htmlFile != plContext) {
170 (void)printf("%d bytes written to HTML file\n",
171 fwrite(dest, length, 1, htmlFile));
172 (void)fprintf(htmlFile, "<BR>\n");
173 }
174
175 PKIX_TEST_EXPECT_NO_ERROR(PKIX_PL_Free(dest, plContext));
176 dest = NULL;
177 length = 0;
178
179 PKIX_TEST_EXPECT_NO_ERROR(PKIX_PL_String_GetEncoded(jeanRenoString,
180 PKIX_UTF8,
181 &dest,
182 &length,
183 plContext));
184 if (htmlFile != plContext) {
185 (void)printf("%d bytes written to HTML file\n",
186 fwrite(dest, length, 1, htmlFile));
187 (void)fprintf(htmlFile, "<BR>\n");
188 }
189
190 PKIX_TEST_EXPECT_NO_ERROR(PKIX_PL_Free(dest, plContext));
191 dest = NULL;
192 length = 0;
193
194 PKIX_TEST_EXPECT_NO_ERROR(PKIX_PL_String_GetEncoded(vivaEspanaString,
195 PKIX_UTF8,
196 &dest,
197 &length,
198 plContext));
199 if (htmlFile != plContext) {
200 (void)printf("%d bytes written to HTML file\n",
201 fwrite(dest, length, 1, htmlFile));
202 (void)fprintf(htmlFile, "<BR>\n");
203 }
204
205 PKIX_TEST_EXPECT_NO_ERROR(PKIX_PL_Free(dest, plContext));
206 dest = NULL;
207 length = 0;
208
209 PKIX_TEST_EXPECT_NO_ERROR(PKIX_PL_String_GetEncoded(straussString,
210 PKIX_UTF8,
211 &dest,
212 &length,
213 plContext));
214 if (htmlFile != plContext) {
215 (void)printf("%d bytes written to HTML file\n",
216 fwrite(dest, length, 1, htmlFile));
217 (void)fprintf(htmlFile, "<BR>\n");
218 }
219
220 PKIX_TEST_EXPECT_NO_ERROR(PKIX_PL_Free(dest, plContext));
221 dest = NULL;
222 length = 0;
223
224 PKIX_TEST_EXPECT_NO_ERROR(PKIX_PL_String_GetEncoded(straussString,
225 PKIX_UTF8,
226 &dest,
227 &length,
228 plContext));
229
230 PKIX_TEST_EXPECT_NO_ERROR(PKIX_PL_Free(dest, plContext));
231 dest = NULL;
232 length = 0;
233
234 PKIX_TEST_EXPECT_NO_ERROR(PKIX_PL_String_GetEncoded(gorbachevString,
235 PKIX_UTF8,
236 &dest,
237 &length,
238 plContext));
239 if (htmlFile != plContext) {
240 (void)printf("%d bytes written to HTML file\n",
241 fwrite(dest, length, 1, htmlFile));
242 (void)fprintf(htmlFile, "<BR>\n");
243 }
244
245 PKIX_TEST_EXPECT_NO_ERROR(PKIX_PL_Free(dest, plContext));
246 dest = NULL;
247 length = 0;
248
249 if (htmlFile != plContext) {
250 (void)fprintf(htmlFile, "</font>\n");
251 (void)fprintf(htmlFile, "</body></html>\n");
252 (void)fclose(htmlFile);
253 }
254
255 cleanup:
256
257 PKIX_TEST_RETURN();
258 }
259
260 static void
testDestroy(PKIX_PL_String * string)261 testDestroy(
262 PKIX_PL_String *string)
263 {
264 PKIX_TEST_STD_VARS();
265
266 PKIX_TEST_DECREF_BC(string);
267
268 cleanup:
269
270 PKIX_TEST_RETURN();
271 }
272
273 int
test_string2(int argc,char * argv[])274 test_string2(int argc, char *argv[])
275 {
276
277 PKIX_PL_String *vivaEspanaString, *straussString, *testUTF16String;
278 PKIX_PL_String *chineseString, *jeanRenoString, *gorbachevString;
279 PKIX_UInt32 actualMinorVersion;
280 PKIX_UInt32 j = 0;
281
282 PKIX_TEST_STD_VARS();
283
284 startTests("Unicode Strings");
285
286 PKIX_TEST_EXPECT_NO_ERROR(
287 PKIX_PL_NssContext_Create(0, PKIX_FALSE, NULL, &plContext));
288
289 subTest("PKIX_PL_String_Create");
290 createString(&vivaEspanaString,
291 &straussString,
292 &gorbachevString,
293 &testUTF16String,
294 &chineseString,
295 &jeanRenoString);
296
297 subTest("Converting UTF-16 to EscASCII");
298 testGetEncoded(testUTF16String, PKIX_ESCASCII);
299
300 subTest("Converting UTF-8 to EscASCII");
301 testGetEncoded(chineseString, PKIX_ESCASCII);
302
303 subTest("Converting UTF-8 to EscASCII");
304 testGetEncoded(jeanRenoString, PKIX_ESCASCII);
305
306 subTest("Converting EscASCII to UTF-16");
307 testGetEncoded(vivaEspanaString, PKIX_UTF16);
308
309 subTest("Converting UTF-8 to UTF-16");
310 testGetEncoded(chineseString, PKIX_UTF16);
311
312 subTest("Creating HTML Output File \'utf8.html\'");
313 testHTMLOutput(vivaEspanaString,
314 straussString,
315 gorbachevString,
316 testUTF16String,
317 chineseString,
318 jeanRenoString);
319
320 subTest("Unicode Destructors");
321 testDestroy(testUTF16String);
322 testDestroy(chineseString);
323 testDestroy(jeanRenoString);
324 testDestroy(vivaEspanaString);
325 testDestroy(straussString);
326 testDestroy(gorbachevString);
327
328 cleanup:
329
330 PKIX_Shutdown(plContext);
331
332 PKIX_TEST_RETURN();
333
334 endTests("Unicode Strings");
335
336 return (0);
337 }
338