PageRenderTime 33ms CodeModel.GetById 13ms app.highlight 17ms RepoModel.GetById 1ms app.codeStats 0ms

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

http://github.com/zpao/v8monkey
C | 479 lines | 330 code | 95 blank | 54 comment | 14 complexity | 635125617e87fd9e657622e016bfc379 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_string.c
 39 *
 40 * Tests 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 **testString,
 52        PKIX_UInt32 format,
 53        char *stringAscii,
 54        PKIX_UInt32 length)
 55{
 56        PKIX_TEST_STD_VARS();
 57
 58        PKIX_TEST_EXPECT_NO_ERROR
 59                (PKIX_PL_String_Create
 60                (format, stringAscii, length, testString, plContext));
 61
 62cleanup:
 63
 64        PKIX_TEST_RETURN();
 65}
 66
 67static void
 68createStringOther(
 69        PKIX_PL_String **testEscAscii,
 70        PKIX_PL_String **testUtf16,
 71        PKIX_PL_String **ampString,
 72        PKIX_PL_String **testDebugAscii,
 73        PKIX_PL_String **testNullString,
 74        PKIX_UInt32 *utf16data)
 75{
 76        char *nullText = "Hi� there!";
 77
 78        char *escAsciiString =
 79                "¡𐀀࿿􀀁";
 80
 81        char *debugAsciiString =
 82                "string with
newlines and	tabs";
 83
 84        char * utfAmp = "\x00&";
 85
 86        PKIX_TEST_STD_VARS();
 87
 88        createString(testEscAscii,
 89                    PKIX_ESCASCII,
 90                    escAsciiString,
 91                    PL_strlen(escAsciiString));
 92
 93        createString(testUtf16, PKIX_UTF16, (char *)utf16data, 12);
 94
 95        createString(ampString, PKIX_UTF16, utfAmp, 2);
 96
 97        createString(testDebugAscii,
 98                    PKIX_ESCASCII_DEBUG,
 99                    debugAsciiString,
100                    PL_strlen(debugAsciiString));
101
102        createString(testNullString,
103                    PKIX_ESCASCII_DEBUG,
104                    nullText,
105                    PL_strlen(nullText));
106
107        goto cleanup;
108
109cleanup:
110
111        PKIX_TEST_RETURN();
112}
113
114static void
115testGetEncoded(
116        PKIX_PL_String *testEscAscii,
117        PKIX_PL_String *testString0,
118        PKIX_PL_String *testDebugAscii,
119        PKIX_PL_String *testNullString,
120        PKIX_UInt32 *utf16data)
121{
122        char *temp = NULL;
123        void *dest = NULL;
124        void *dest2 = NULL;
125        char *plainText = "string with\nnewlines and\ttabs";
126        PKIX_UInt32 length, length2, i;
127
128        PKIX_TEST_STD_VARS();
129
130        PKIX_TEST_EXPECT_NO_ERROR(PKIX_PL_String_GetEncoded(testEscAscii,
131                                        PKIX_UTF16,
132                                        &dest,
133                                        &length,
134                                        plContext));
135        for (i = 0; i < length; i++) {
136                if (((char*)dest)[i] != ((char*)utf16data)[i]) {
137                        testError("UTF-16 Data Differs from Source");
138                        printf("%d-th char is different -%c-%c-\n", i,
139                               ((char*)dest)[i], ((char*)utf16data)[i]);
140                }
141        }
142
143        length = 0;
144        PKIX_TEST_EXPECT_NO_ERROR(PKIX_PL_Free(dest, plContext));
145
146        PKIX_TEST_EXPECT_NO_ERROR(PKIX_PL_String_GetEncoded(testNullString,
147                                        PKIX_UTF16,
148                                        &dest,
149                                        &length,
150                                        plContext));
151
152        length = 0;
153        PKIX_TEST_EXPECT_NO_ERROR(PKIX_PL_Free(dest, plContext));
154
155        PKIX_TEST_EXPECT_NO_ERROR(PKIX_PL_String_GetEncoded(testString0,
156                                        PKIX_ESCASCII_DEBUG,
157                                        &dest,
158                                        &length,
159                                        plContext));
160
161        PKIX_TEST_EXPECT_NO_ERROR(PKIX_PL_String_GetEncoded(testDebugAscii,
162                                        PKIX_ESCASCII_DEBUG,
163                                        &dest2,
164                                        &length2,
165                                        plContext));
166
167        for (i = 0; (i < length) && (i < length2); i++)
168                if (((char*)dest)[i] != ((char*)dest2)[i]) {
169                        testError("Equivalent strings are unequal");
170                        break;
171                }
172
173        length = 0;
174        PKIX_TEST_EXPECT_NO_ERROR(PKIX_PL_Free(dest, plContext));
175        length2 = 0;
176        PKIX_TEST_EXPECT_NO_ERROR(PKIX_PL_Free(dest2, plContext));
177
178        temp = PKIX_String2ASCII(testDebugAscii, plContext);
179        if (temp){
180                if (PL_strcmp(plainText, temp) != 0)
181                        testError("Debugged ASCII does not match "
182                                    "equivalent EscAscii");
183                PKIX_TEST_EXPECT_NO_ERROR(PKIX_PL_Free(temp, plContext));
184        }
185
186cleanup:
187
188        PKIX_TEST_RETURN();
189}
190
191static void
192testSprintf(void)
193{
194        PKIX_Int32 x = 0xCAFE;
195        PKIX_Int32 y = -12345;
196        PKIX_PL_String *testString = NULL;
197        PKIX_PL_String *formatString = NULL;
198        PKIX_PL_String *sprintfString = NULL;
199        char *plainText = "Testing Sprintf";
200        char *format = "%s %x %u %d";
201        char *convertedFormat = "%s %lx %lu %ld";
202        char *temp = NULL;
203        char *temp2 = NULL;
204
205        PKIX_TEST_STD_VARS();
206
207        PKIX_TEST_EXPECT_NO_ERROR(PKIX_PL_String_Create(
208                                        PKIX_ESCASCII,
209                                        plainText,
210                                        PL_strlen(plainText),
211                                        &testString,
212                                        plContext));
213
214        PKIX_TEST_EXPECT_NO_ERROR(PKIX_PL_String_Create(
215                                        PKIX_ESCASCII,
216                                        format,
217                                        11,
218                                        &formatString,
219                                        plContext));
220
221        PKIX_TEST_EXPECT_NO_ERROR(PKIX_PL_Sprintf(&sprintfString,
222                                plContext,
223                                formatString,
224                                testString, x, y, y));
225        PKIX_TEST_DECREF_BC(testString);
226
227        temp = PR_smprintf(convertedFormat, plainText, x, y, y);
228        temp2 = PKIX_String2ASCII(sprintfString, plContext);
229
230        if (PL_strcmp(temp, temp2) != 0)
231                testError("Sprintf produced incorrect output");
232
233        PKIX_TEST_EXPECT_NO_ERROR(PKIX_PL_Free(temp, plContext));
234        PKIX_TEST_EXPECT_NO_ERROR(PKIX_PL_Free(temp2, plContext));
235
236
237        PKIX_TEST_DECREF_BC(sprintfString);
238
239
240        PKIX_TEST_DECREF_BC(formatString);
241
242
243cleanup:
244
245        PKIX_TEST_RETURN();
246}
247
248static void
249testErrorHandling(void)
250{
251        char *debugAsciiString =
252                "string with&#x000A;newlines and&#x0009;tabs";
253
254        PKIX_PL_String *testString = NULL;
255
256        PKIX_TEST_STD_VARS();
257
258        PKIX_TEST_EXPECT_ERROR(PKIX_PL_String_Create(
259                                        PKIX_ESCASCII,
260                                        NULL,
261                                        50,
262                                        &testString,
263                                        plContext));
264
265        PKIX_TEST_EXPECT_ERROR(PKIX_PL_String_Create(PKIX_ESCASCII,
266                                        "blah", 4, NULL, plContext));
267
268        PKIX_TEST_EXPECT_ERROR(PKIX_PL_Sprintf(&testString, plContext, NULL));
269
270        PKIX_TEST_EXPECT_ERROR
271                (PKIX_PL_GetString(0, NULL, &testString, plContext));
272
273        PKIX_TEST_EXPECT_ERROR(PKIX_PL_GetString(0, "blah", 0, plContext));
274
275        /* ---------------------------- */
276        subTest("Unicode Error Handling");
277
278        /* &#x must be followed by 4 hexadecimal digits */
279        PKIX_TEST_EXPECT_ERROR(PKIX_PL_String_Create(
280                                            PKIX_ESCASCII,
281                                            "&#x003k;",
282                                            7,
283                                            &testString,
284                                            plContext));
285
286        /* &#x must be followed by 4 hexadecimal digits */
287        PKIX_TEST_EXPECT_ERROR(PKIX_PL_String_Create(
288                                            PKIX_ESCASCII,
289                                            "abc&#x00",
290                                            8,
291                                            &testString,
292                                            plContext));
293
294        /* &#x must be between 00010000-0010FFFF */
295        PKIX_TEST_EXPECT_ERROR(PKIX_PL_String_Create(
296                                            PKIX_ESCASCII,
297                                            "&#x00200101;",
298                                            11,
299                                            &testString,
300                                            plContext));
301
302        /* &#x must be followed by 8 hexadecimal digits */
303        PKIX_TEST_EXPECT_ERROR(PKIX_PL_String_Create(
304                                            PKIX_ESCASCII,
305                                            "&#x001000",
306                                            10,
307                                            &testString,
308                                            plContext));
309
310        /* &#x must be followed by 8 hexadecimal digits */
311        PKIX_TEST_EXPECT_ERROR(PKIX_PL_String_Create(
312                                            PKIX_ESCASCII,
313                                            "&#x0010m00;",
314                                            10,
315                                            &testString,
316                                            plContext));
317
318        /* Byte values D800-DFFF are reserved */
319        PKIX_TEST_EXPECT_ERROR(PKIX_PL_String_Create(
320                                            PKIX_ESCASCII,
321                                            "&#xD800;",
322                                            7,
323                                            &testString,
324                                            plContext));
325
326        /* Can't use &#x for regular characters */
327        PKIX_TEST_EXPECT_ERROR(PKIX_PL_String_Create(
328                                            PKIX_ESCASCII,
329                                            "&#x0032;",
330                                            7,
331                                            &testString,
332                                            plContext));
333
334        /* Can't use non-printable characters */
335        PKIX_TEST_EXPECT_ERROR(PKIX_PL_String_Create(
336                                            PKIX_ESCASCII,
337                                            "\xA1",
338                                            1,
339                                            &testString,
340                                            plContext));
341
342        /* Only legal \\ characters are \\, u and U */
343        PKIX_TEST_EXPECT_ERROR(PKIX_PL_String_Create(
344                                            PKIX_ESCASCII,
345                                            "&blah",
346                                            5,
347                                            &testString,
348                                            plContext));
349
350
351
352        /* Surrogate pairs must be legal */
353        PKIX_TEST_EXPECT_ERROR(PKIX_PL_String_Create(
354                                            PKIX_UTF16,
355                                            "\xd8\x00\x0\x66",
356                                            4,
357                                            &testString,
358                                            plContext));
359
360        /* Debugged EscASCII should not be accepted as EscASCII */
361        PKIX_TEST_EXPECT_ERROR(PKIX_PL_String_Create(
362                                        PKIX_ESCASCII,
363                                        debugAsciiString,
364                                        PL_strlen(debugAsciiString),
365                                        &testString,
366                                        plContext));
367cleanup:
368
369        PKIX_TEST_RETURN();
370}
371
372static void
373testDestroy(
374        PKIX_PL_String *string)
375{
376        PKIX_TEST_STD_VARS();
377
378        PKIX_TEST_DECREF_BC(string);
379
380cleanup:
381
382        PKIX_TEST_RETURN();
383}
384
385
386int test_string(int argc, char *argv[]) {
387
388        PKIX_PL_String *testString[6] = {NULL};
389        PKIX_PL_String *testNullString = NULL;
390        PKIX_PL_String *testDebugAscii = NULL;
391        PKIX_PL_String *testEscAscii = NULL;
392        PKIX_PL_String *testUtf16 = NULL;
393        PKIX_PL_String *ampString = NULL;
394        unsigned char utf16Data[] = {0x00, 0xA1, 0xD8, 0x00,
395                            0xDC, 0x00, 0x0F, 0xFF,
396                            0xDB, 0xC0, 0xDC, 0x01};
397        PKIX_UInt32 i, size = 6;
398
399        char *plainText[6] = {
400                "string with\nnewlines and\ttabs",
401                "Not an escaped char: &amp;#x0012;",
402                "Encode &amp; with &amp;amp; in ASCII",
403                "&#x00A1;",
404                "&amp;",
405                "string with\nnewlines and\ttabs"
406        };
407
408        PKIX_UInt32 actualMinorVersion;
409        PKIX_UInt32 j = 0;
410
411        PKIX_TEST_STD_VARS();
412
413        startTests("Strings");
414
415        PKIX_TEST_EXPECT_NO_ERROR(
416            PKIX_PL_NssContext_Create(0, PKIX_FALSE, NULL, &plContext));
417
418        subTest("PKIX_PL_String_Create <ascii format>");
419        for (i = 0; i < size; i++) {
420                testString[i] = NULL;
421                createString
422                        (&testString[i],
423                        PKIX_ESCASCII,
424                        plainText[i],
425                        PL_strlen(plainText[i]));
426        }
427
428        subTest("PKIX_PL_String_Create <other formats>");
429        createStringOther
430                (&testEscAscii,
431                &testUtf16,
432                &ampString,
433                &testDebugAscii,
434                &testNullString,
435                (PKIX_UInt32 *)utf16Data);
436
437        PKIX_TEST_EQ_HASH_TOSTR_DUP
438                (testString[0],
439                testString[5],
440                testString[1],
441                plainText[0],
442                String,
443                PKIX_TRUE);
444
445        subTest("PKIX_PL_String_GetEncoded");
446        testGetEncoded
447                (testEscAscii,
448                testString[0],
449                testDebugAscii,
450                testNullString,
451                (PKIX_UInt32 *)utf16Data);
452
453        subTest("PKIX_PL_Sprintf");
454        testSprintf();
455
456        subTest("PKIX_PL_String_Create <error_handling>");
457        testErrorHandling();
458
459        subTest("PKIX_PL_String_Destroy");
460        for (i = 0; i < size; i++) {
461                testDestroy(testString[i]);
462        }
463        testDestroy(testEscAscii);
464        testDestroy(testUtf16);
465        testDestroy(ampString);
466        testDestroy(testDebugAscii);
467        testDestroy(testNullString);
468
469cleanup:
470
471        PKIX_Shutdown(plContext);
472
473        PKIX_TEST_RETURN();
474
475        endTests("String");
476
477        return (0);
478
479}