PageRenderTime 51ms CodeModel.GetById 15ms app.highlight 31ms RepoModel.GetById 1ms app.codeStats 0ms

/security/nss/cmd/libpkix/pkix_pl/system/test_string2.c

http://github.com/zpao/v8monkey
C | 375 lines | 260 code | 70 blank | 45 comment | 17 complexity | 866a7eef9c2308747b9d923e8fcfac66 MD5 | raw file
  1/* ***** BEGIN LICENSE BLOCK *****
  2 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
  3 *
  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/
  8 *
  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.
 13 *
 14 * The Original Code is the PKIX-C library.
 15 *
 16 * The Initial Developer of the Original Code is
 17 * Sun Microsystems, Inc.
 18 * Portions created by the Initial Developer are
 19 * Copyright 2004-2007 Sun Microsystems, Inc.  All Rights Reserved.
 20 *
 21 * Contributor(s):
 22 *   Sun Microsystems, Inc.
 23 *
 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.
 35 *
 36 * ***** END LICENSE BLOCK ***** */
 37/*
 38 * test_string2.c
 39 *
 40 * Tests International Strings
 41 *
 42 */
 43
 44#include "testutil.h"
 45#include "testutil_nss.h"
 46
 47static void *plContext = NULL;
 48
 49static void
 50createString(
 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)
 57{
 58        /* this is meant to fail - it highlights bug 0002 */
 59        unsigned char utf16String[4] = { 0xF8, 0x60,
 60                                        0xFC, 0x60};
 61
 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
 68        };
 69
 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";
 73
 74        char *vivaEspana =
 75                "¡Viva España!";
 76
 77        char *strauss =
 78                "Strauß was born in Österreich";
 79
 80        PKIX_TEST_STD_VARS();
 81
 82        /* ---------------------------- */
 83        subTest("String Creation");
 84
 85        PKIX_TEST_EXPECT_NO_ERROR(PKIX_PL_String_Create(
 86                                        PKIX_ESCASCII,
 87                                        vivaEspana,
 88                                        PL_strlen(vivaEspana),
 89                                        vivaEspanaString,
 90                                        plContext));
 91
 92        PKIX_TEST_EXPECT_NO_ERROR(PKIX_PL_String_Create(
 93                                        PKIX_ESCASCII,
 94                                        strauss,
 95                                        PL_strlen(strauss),
 96                                        straussString,
 97                                        plContext));
 98
 99        PKIX_TEST_EXPECT_NO_ERROR(PKIX_PL_String_Create(
100                                        PKIX_UTF8,
101                                        gorbachev,
102                                        PL_strlen(gorbachev),
103                                        gorbachevString,
104                                        plContext));
105
106        PKIX_TEST_EXPECT_NO_ERROR(PKIX_PL_String_Create(
107                                        PKIX_UTF16,
108                                        utf16String,
109                                        4,
110                                        testUTF16String,
111                                        plContext));
112
113
114        PKIX_TEST_EXPECT_NO_ERROR(PKIX_PL_String_Create(
115                                        PKIX_UTF8,
116                                        chinese,
117                                        16,
118                                        chineseString,
119                                        plContext));
120
121        PKIX_TEST_EXPECT_NO_ERROR(PKIX_PL_String_Create(
122                                        PKIX_UTF8,
123                                        jeanReno,
124                                        PL_strlen(jeanReno),
125                                        jeanRenoString,
126                                        plContext));
127
128cleanup:
129
130        PKIX_TEST_RETURN();
131}
132
133static void
134testGetEncoded(PKIX_PL_String *string, PKIX_UInt32 format)
135{
136        void *dest = NULL;
137        PKIX_UInt32 length;
138
139        PKIX_TEST_STD_VARS();
140
141        PKIX_TEST_EXPECT_NO_ERROR(PKIX_PL_String_GetEncoded
142                                (string,
143                                format,
144                                &dest,
145                                &length,
146                                plContext));
147
148        if (dest){
149                (void) printf("\tResult: %s\n", (char *)dest);
150                PKIX_TEST_EXPECT_NO_ERROR(PKIX_PL_Free(dest, plContext));
151        }
152
153cleanup:
154        PKIX_TEST_RETURN();
155}
156
157
158static void
159testHTMLOutput(
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)
166{
167        void *dest = NULL;
168        PKIX_UInt32 length;
169
170        FILE *htmlFile = NULL;
171
172        PKIX_TEST_STD_VARS();
173
174        /* Opening a file for output */
175        htmlFile = fopen("utf8.html", "w");
176
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");
195        }
196
197        PKIX_TEST_EXPECT_NO_ERROR(PKIX_PL_Free(dest, plContext));
198        dest = NULL;
199        length = 0;
200
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");
210        }
211
212        PKIX_TEST_EXPECT_NO_ERROR(PKIX_PL_Free(dest, plContext));
213        dest = NULL;
214        length = 0;
215
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");
225        }
226
227        PKIX_TEST_EXPECT_NO_ERROR(PKIX_PL_Free(dest, plContext));
228        dest = NULL;
229        length = 0;
230
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");
240        }
241
242        PKIX_TEST_EXPECT_NO_ERROR(PKIX_PL_Free(dest, plContext));
243        dest = NULL;
244        length = 0;
245
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");
255        }
256
257        PKIX_TEST_EXPECT_NO_ERROR(PKIX_PL_Free(dest, plContext));
258        dest = NULL;
259        length = 0;
260
261
262
263        PKIX_TEST_EXPECT_NO_ERROR(PKIX_PL_String_GetEncoded(straussString,
264                                            PKIX_UTF8,
265                                            &dest,
266                                            &length,
267                                            plContext));
268
269        PKIX_TEST_EXPECT_NO_ERROR(PKIX_PL_Free(dest, plContext));
270        dest = NULL;
271        length = 0;
272
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");
282        }
283
284        PKIX_TEST_EXPECT_NO_ERROR(PKIX_PL_Free(dest, plContext));
285        dest = NULL;
286        length = 0;
287
288        if (htmlFile != plContext) {
289                (void) fprintf(htmlFile, "</font>\n");
290                (void) fprintf(htmlFile, "</body></html>\n");
291                (void) fclose(htmlFile);
292        }
293
294cleanup:
295
296        PKIX_TEST_RETURN();
297}
298
299static void
300testDestroy(
301        PKIX_PL_String *string)
302{
303        PKIX_TEST_STD_VARS();
304
305        PKIX_TEST_DECREF_BC(string);
306
307cleanup:
308
309        PKIX_TEST_RETURN();
310}
311
312int test_string2(int argc, char *argv[]) {
313
314        PKIX_PL_String *vivaEspanaString, *straussString, *testUTF16String;
315        PKIX_PL_String *chineseString, *jeanRenoString, *gorbachevString;
316        PKIX_UInt32 actualMinorVersion;
317        PKIX_UInt32 j = 0;
318
319        PKIX_TEST_STD_VARS();
320
321        startTests("Unicode Strings");
322
323        PKIX_TEST_EXPECT_NO_ERROR(
324            PKIX_PL_NssContext_Create(0, PKIX_FALSE, NULL, &plContext));
325
326        subTest("PKIX_PL_String_Create");
327        createString(&vivaEspanaString,
328                    &straussString,
329                    &gorbachevString,
330                    &testUTF16String,
331                    &chineseString,
332                    &jeanRenoString);
333
334        subTest("Converting UTF-16 to EscASCII");
335        testGetEncoded(testUTF16String, PKIX_ESCASCII);
336
337        subTest("Converting UTF-8 to EscASCII");
338        testGetEncoded(chineseString, PKIX_ESCASCII);
339
340        subTest("Converting UTF-8 to EscASCII");
341        testGetEncoded(jeanRenoString, PKIX_ESCASCII);
342
343        subTest("Converting EscASCII to UTF-16");
344        testGetEncoded(vivaEspanaString, PKIX_UTF16);
345
346        subTest("Converting UTF-8 to UTF-16");
347        testGetEncoded(chineseString, PKIX_UTF16);
348
349        subTest("Creating HTML Output File \'utf8.html\'");
350        testHTMLOutput(vivaEspanaString,
351                    straussString,
352                    gorbachevString,
353                    testUTF16String,
354                    chineseString,
355                    jeanRenoString);
356
357        subTest("Unicode Destructors");
358        testDestroy(testUTF16String);
359        testDestroy(chineseString);
360        testDestroy(jeanRenoString);
361        testDestroy(vivaEspanaString);
362        testDestroy(straussString);
363        testDestroy(gorbachevString);
364
365cleanup:
366
367        PKIX_Shutdown(plContext);
368
369        PKIX_TEST_RETURN();
370
371        endTests("Unicode Strings");
372
373        return (0);
374
375}