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
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.
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 ***** */
40 * Tests International Strings
45 #include "testutil_nss.h"
47 void *plContext
= NULL
;
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,
62 unsigned char chinese
[16] = { 0xe7, 0xab, 0xa0,
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";
75 "¡Viva España!";
78 "Strauß was born in Österreich";
82 /* ---------------------------- */
83 subTest("String Creation");
85 PKIX_TEST_EXPECT_NO_ERROR(PKIX_PL_String_Create(
88 PL_strlen(vivaEspana
),
92 PKIX_TEST_EXPECT_NO_ERROR(PKIX_PL_String_Create(
99 PKIX_TEST_EXPECT_NO_ERROR(PKIX_PL_String_Create(
102 PL_strlen(gorbachev
),
106 PKIX_TEST_EXPECT_NO_ERROR(PKIX_PL_String_Create(
114 PKIX_TEST_EXPECT_NO_ERROR(PKIX_PL_String_Create(
121 PKIX_TEST_EXPECT_NO_ERROR(PKIX_PL_String_Create(
134 testGetEncoded(PKIX_PL_String
*string
, PKIX_UInt32 format
)
139 PKIX_TEST_STD_VARS();
141 PKIX_TEST_EXPECT_NO_ERROR(PKIX_PL_String_GetEncoded
149 (void) printf("\tResult: %s\n", (char *)dest
);
150 PKIX_TEST_EXPECT_NO_ERROR(PKIX_PL_Free(dest
, plContext
));
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
)
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");
185 (void) printf("Could not open HTML file\n");
186 PKIX_TEST_EXPECT_NO_ERROR(PKIX_PL_String_GetEncoded(testUTF16String
,
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
));
201 PKIX_TEST_EXPECT_NO_ERROR(PKIX_PL_String_GetEncoded(chineseString
,
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
));
216 PKIX_TEST_EXPECT_NO_ERROR(PKIX_PL_String_GetEncoded(jeanRenoString
,
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
));
231 PKIX_TEST_EXPECT_NO_ERROR(PKIX_PL_String_GetEncoded(vivaEspanaString
,
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
));
246 PKIX_TEST_EXPECT_NO_ERROR(PKIX_PL_String_GetEncoded(straussString
,
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
));
263 PKIX_TEST_EXPECT_NO_ERROR(PKIX_PL_String_GetEncoded(straussString
,
269 PKIX_TEST_EXPECT_NO_ERROR(PKIX_PL_Free(dest
, plContext
));
273 PKIX_TEST_EXPECT_NO_ERROR(PKIX_PL_String_GetEncoded(gorbachevString
,
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
));
288 if (htmlFile
!= plContext
) {
289 (void) fprintf(htmlFile
, "</font>\n");
290 (void) fprintf(htmlFile
, "</body></html>\n");
291 (void) fclose(htmlFile
);
301 PKIX_PL_String
*string
)
303 PKIX_TEST_STD_VARS();
305 PKIX_TEST_DECREF_BC(string
);
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
;
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 */
335 subTest("PKIX_PL_String_Create");
336 createString(&vivaEspanaString
,
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
,
366 subTest("Unicode Destructors");
367 testDestroy(testUTF16String
);
368 testDestroy(chineseString
);
369 testDestroy(jeanRenoString
);
370 testDestroy(vivaEspanaString
);
371 testDestroy(straussString
);
372 testDestroy(gorbachevString
);
376 PKIX_Shutdown(plContext
);
380 endTests("Unicode Strings");