Import from 1.9a8 tarball
[mozilla-nss.git] / security / nss / cmd / libpkix / pkix_pl / system / test_string2.c
blob343afbe4f74818f030aae470f6e694dbf0348a38
1 /* ***** BEGIN LICENSE BLOCK *****
2 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
4 * The contents of this file are subject to the Mozilla Public License Version
5 * 1.1 (the "License"); you may not use this file except in compliance with
6 * the License. You may obtain a copy of the License at
7 * http://www.mozilla.org/MPL/
9 * Software distributed under the License is distributed on an "AS IS" basis,
10 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
11 * for the specific language governing rights and limitations under the
12 * License.
14 * The Original Code is the Netscape security libraries.
16 * The Initial Developer of the Original Code is
17 * Netscape Communications Corporation.
18 * Portions created by the Initial Developer are Copyright (C) 1994-2000
19 * the Initial Developer. All Rights Reserved.
21 * Contributor(s):
22 * Sun Microsystems
24 * Alternatively, the contents of this file may be used under the terms of
25 * either the GNU General Public License Version 2 or later (the "GPL"), or
26 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
27 * in which case the provisions of the GPL or the LGPL are applicable instead
28 * of those above. If you wish to allow use of your version of this file only
29 * under the terms of either the GPL or the LGPL, and not to allow others to
30 * use your version of this file under the terms of the MPL, indicate your
31 * decision by deleting the provisions above and replace them with the notice
32 * and other provisions required by the GPL or the LGPL. If you do not delete
33 * the provisions above, a recipient may use your version of this file under
34 * the terms of any one of the MPL, the GPL or the LGPL.
36 * ***** END LICENSE BLOCK ***** */
38 * test_string2.c
40 * Tests International Strings
44 #include "testutil.h"
45 #include "testutil_nss.h"
47 void *plContext = NULL;
49 void
50 createString(
51 PKIX_PL_String **vivaEspanaString,
52 PKIX_PL_String **straussString,
53 PKIX_PL_String **gorbachevString,
54 PKIX_PL_String **testUTF16String,
55 PKIX_PL_String **chineseString,
56 PKIX_PL_String **jeanRenoString)
58 /* this is meant to fail - it highlights bug 0002 */
59 unsigned char utf16String[4] = { 0xF8, 0x60,
60 0xFC, 0x60};
62 unsigned char chinese[16] = { 0xe7, 0xab, 0xa0,
63 0xe5, 0xad, 0x90,
64 0xe6, 0x80, 0xa1,
65 0x20,
66 0xe4, 0xb8, 0xad,
67 0xe5, 0x9b, 0xbd
70 char* jeanReno = "Jean R\303\251no is an actor.";
71 char* gorbachev = /* This is the name "Gorbachev" in cyrllic */
72 "\xd0\x93\xd0\xbe\xd1\x80\xd0\xb1\xd0\xb0\xd1\x87\xd1\x91\xd0\xb2";
74 char *vivaEspana =
75 "¡Viva España!";
77 char *strauss =
78 "Strauß was born in Österreich";
80 PKIX_TEST_STD_VARS();
82 /* ---------------------------- */
83 subTest("String Creation");
85 PKIX_TEST_EXPECT_NO_ERROR(PKIX_PL_String_Create(
86 PKIX_ESCASCII,
87 vivaEspana,
88 PL_strlen(vivaEspana),
89 vivaEspanaString,
90 plContext));
92 PKIX_TEST_EXPECT_NO_ERROR(PKIX_PL_String_Create(
93 PKIX_ESCASCII,
94 strauss,
95 PL_strlen(strauss),
96 straussString,
97 plContext));
99 PKIX_TEST_EXPECT_NO_ERROR(PKIX_PL_String_Create(
100 PKIX_UTF8,
101 gorbachev,
102 PL_strlen(gorbachev),
103 gorbachevString,
104 plContext));
106 PKIX_TEST_EXPECT_NO_ERROR(PKIX_PL_String_Create(
107 PKIX_UTF16,
108 utf16String,
110 testUTF16String,
111 plContext));
114 PKIX_TEST_EXPECT_NO_ERROR(PKIX_PL_String_Create(
115 PKIX_UTF8,
116 chinese,
118 chineseString,
119 plContext));
121 PKIX_TEST_EXPECT_NO_ERROR(PKIX_PL_String_Create(
122 PKIX_UTF8,
123 jeanReno,
124 PL_strlen(jeanReno),
125 jeanRenoString,
126 plContext));
128 cleanup:
130 PKIX_TEST_RETURN();
133 void
134 testGetEncoded(PKIX_PL_String *string, PKIX_UInt32 format)
136 void *dest = NULL;
137 PKIX_UInt32 length;
139 PKIX_TEST_STD_VARS();
141 PKIX_TEST_EXPECT_NO_ERROR(PKIX_PL_String_GetEncoded
142 (string,
143 format,
144 &dest,
145 &length,
146 plContext));
148 if (dest){
149 (void) printf("\tResult: %s\n", (char *)dest);
150 PKIX_TEST_EXPECT_NO_ERROR(PKIX_PL_Free(dest, plContext));
153 cleanup:
154 PKIX_TEST_RETURN();
158 void
159 testHTMLOutput(
160 PKIX_PL_String *vivaEspanaString,
161 PKIX_PL_String *straussString,
162 PKIX_PL_String *gorbachevString,
163 PKIX_PL_String *testUTF16String,
164 PKIX_PL_String *chineseString,
165 PKIX_PL_String *jeanRenoString)
167 void *dest = NULL;
168 PKIX_UInt32 length;
170 FILE *htmlFile = NULL;
172 PKIX_TEST_STD_VARS();
174 /* Opening a file for output */
175 htmlFile = fopen("utf8.html", "w");
177 if (htmlFile != plContext) {
178 (void) fprintf(htmlFile, "<html><head>\n");
179 (void) fprintf(htmlFile, "<meta http-equiv=\"Content-Type\"");
180 (void) fprintf(htmlFile,
181 "content = \"text/html; charset = UTF-8\">\n");
182 (void) fprintf(htmlFile, "</head><body>\n");
183 (void) fprintf(htmlFile, "<font size =\"+2\">\n");
184 } else
185 (void) printf("Could not open HTML file\n");
186 PKIX_TEST_EXPECT_NO_ERROR(PKIX_PL_String_GetEncoded(testUTF16String,
187 PKIX_UTF8,
188 &dest,
189 &length,
190 plContext));
191 if (htmlFile != plContext) {
192 (void) printf("%d bytes written to HTML file\n",
193 fwrite(dest, length, 1, htmlFile));
194 (void) fprintf(htmlFile, "<BR>\n");
197 PKIX_TEST_EXPECT_NO_ERROR(PKIX_PL_Free(dest, plContext));
198 dest = NULL;
199 length = 0;
201 PKIX_TEST_EXPECT_NO_ERROR(PKIX_PL_String_GetEncoded(chineseString,
202 PKIX_UTF8,
203 &dest,
204 &length,
205 plContext));
206 if (htmlFile != plContext) {
207 (void) printf("%d bytes written to HTML file\n",
208 fwrite(dest, length, 1, htmlFile));
209 (void) fprintf(htmlFile, "<BR>\n");
212 PKIX_TEST_EXPECT_NO_ERROR(PKIX_PL_Free(dest, plContext));
213 dest = NULL;
214 length = 0;
216 PKIX_TEST_EXPECT_NO_ERROR(PKIX_PL_String_GetEncoded(jeanRenoString,
217 PKIX_UTF8,
218 &dest,
219 &length,
220 plContext));
221 if (htmlFile != plContext) {
222 (void) printf("%d bytes written to HTML file\n",
223 fwrite(dest, length, 1, htmlFile));
224 (void) fprintf(htmlFile, "<BR>\n");
227 PKIX_TEST_EXPECT_NO_ERROR(PKIX_PL_Free(dest, plContext));
228 dest = NULL;
229 length = 0;
231 PKIX_TEST_EXPECT_NO_ERROR(PKIX_PL_String_GetEncoded(vivaEspanaString,
232 PKIX_UTF8,
233 &dest,
234 &length,
235 plContext));
236 if (htmlFile != plContext) {
237 (void) printf("%d bytes written to HTML file\n",
238 fwrite(dest, length, 1, htmlFile));
239 (void) fprintf(htmlFile, "<BR>\n");
242 PKIX_TEST_EXPECT_NO_ERROR(PKIX_PL_Free(dest, plContext));
243 dest = NULL;
244 length = 0;
246 PKIX_TEST_EXPECT_NO_ERROR(PKIX_PL_String_GetEncoded(straussString,
247 PKIX_UTF8,
248 &dest,
249 &length,
250 plContext));
251 if (htmlFile != plContext) {
252 (void) printf("%d bytes written to HTML file\n",
253 fwrite(dest, length, 1, htmlFile));
254 (void) fprintf(htmlFile, "<BR>\n");
257 PKIX_TEST_EXPECT_NO_ERROR(PKIX_PL_Free(dest, plContext));
258 dest = NULL;
259 length = 0;
263 PKIX_TEST_EXPECT_NO_ERROR(PKIX_PL_String_GetEncoded(straussString,
264 PKIX_UTF8,
265 &dest,
266 &length,
267 plContext));
269 PKIX_TEST_EXPECT_NO_ERROR(PKIX_PL_Free(dest, plContext));
270 dest = NULL;
271 length = 0;
273 PKIX_TEST_EXPECT_NO_ERROR(PKIX_PL_String_GetEncoded(gorbachevString,
274 PKIX_UTF8,
275 &dest,
276 &length,
277 plContext));
278 if (htmlFile != plContext) {
279 (void) printf("%d bytes written to HTML file\n",
280 fwrite(dest, length, 1, htmlFile));
281 (void) fprintf(htmlFile, "<BR>\n");
284 PKIX_TEST_EXPECT_NO_ERROR(PKIX_PL_Free(dest, plContext));
285 dest = NULL;
286 length = 0;
288 if (htmlFile != plContext) {
289 (void) fprintf(htmlFile, "</font>\n");
290 (void) fprintf(htmlFile, "</body></html>\n");
291 (void) fclose(htmlFile);
294 cleanup:
296 PKIX_TEST_RETURN();
299 void
300 testDestroy(
301 PKIX_PL_String *string)
303 PKIX_TEST_STD_VARS();
305 PKIX_TEST_DECREF_BC(string);
307 cleanup:
309 PKIX_TEST_RETURN();
312 int main(int argc, char *argv[]) {
314 PKIX_PL_String *vivaEspanaString, *straussString, *testUTF16String;
315 PKIX_PL_String *chineseString, *jeanRenoString, *gorbachevString;
316 PKIX_UInt32 actualMinorVersion;
317 PKIX_UInt32 j = 0;
318 PKIX_Boolean useArenas = PKIX_FALSE;
320 PKIX_TEST_STD_VARS();
322 startTests("Unicode Strings");
324 useArenas = PKIX_TEST_ARENAS_ARG(argv[1]);
326 PKIX_TEST_EXPECT_NO_ERROR(PKIX_Initialize
327 (PKIX_TRUE, /* nssInitNeeded */
328 useArenas,
329 PKIX_MAJOR_VERSION,
330 PKIX_MINOR_VERSION,
331 PKIX_MINOR_VERSION,
332 &actualMinorVersion,
333 &plContext));
335 subTest("PKIX_PL_String_Create");
336 createString(&vivaEspanaString,
337 &straussString,
338 &gorbachevString,
339 &testUTF16String,
340 &chineseString,
341 &jeanRenoString);
343 subTest("Converting UTF-16 to EscASCII");
344 testGetEncoded(testUTF16String, PKIX_ESCASCII);
346 subTest("Converting UTF-8 to EscASCII");
347 testGetEncoded(chineseString, PKIX_ESCASCII);
349 subTest("Converting UTF-8 to EscASCII");
350 testGetEncoded(jeanRenoString, PKIX_ESCASCII);
352 subTest("Converting EscASCII to UTF-16");
353 testGetEncoded(vivaEspanaString, PKIX_UTF16);
355 subTest("Converting UTF-8 to UTF-16");
356 testGetEncoded(chineseString, PKIX_UTF16);
358 subTest("Creating HTML Output File \'utf8.html\'");
359 testHTMLOutput(vivaEspanaString,
360 straussString,
361 gorbachevString,
362 testUTF16String,
363 chineseString,
364 jeanRenoString);
366 subTest("Unicode Destructors");
367 testDestroy(testUTF16String);
368 testDestroy(chineseString);
369 testDestroy(jeanRenoString);
370 testDestroy(vivaEspanaString);
371 testDestroy(straussString);
372 testDestroy(gorbachevString);
374 cleanup:
376 PKIX_Shutdown(plContext);
378 PKIX_TEST_RETURN();
380 endTests("Unicode Strings");
382 return (0);