tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

test_object.c (7908B)


      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_object.c
      6 *
      7 * Test Object Allocation, toString, Equals, Destruction
      8 *
      9 */
     10 
     11 #include "testutil.h"
     12 #include "testutil_nss.h"
     13 
     14 static void *plContext = NULL;
     15 
     16 static PKIX_Error *
     17 destructor(
     18    /* ARGSUSED */ PKIX_PL_Object *object,
     19    /* ARGSUSED */ void *plContext)
     20 {
     21    (void)printf("\tUser defined destructor called\n");
     22    return (NULL);
     23 }
     24 
     25 static PKIX_Error *
     26 toStringCallback(
     27    PKIX_PL_Object *obj,
     28    PKIX_PL_String **pString,
     29    /* ARGSUSED */ void *plContext)
     30 {
     31 
     32    PKIX_Error *errorResult;
     33    PKIX_UInt32 type;
     34    char *format = "(addr: %x, type: %d)";
     35    PKIX_PL_String *formatString = NULL;
     36 
     37    errorResult = PKIX_PL_String_Create(
     38        PKIX_ESCASCII,
     39        format,
     40        PL_strlen(format),
     41        &formatString,
     42        plContext);
     43    if (errorResult)
     44        testError("PKIX_PL_String_Create failed");
     45 
     46    if (pString == plContext)
     47        testError("Null String");
     48 
     49    type = (unsigned int)0;
     50 
     51    (void)PKIX_PL_Object_GetType(obj, &type, plContext);
     52 
     53    errorResult = PKIX_PL_Sprintf(pString, plContext,
     54                                  formatString,
     55                                  (int)obj, type);
     56    if (errorResult)
     57        testError("PKIX_PL_Sprintf failed");
     58 
     59    errorResult = PKIX_PL_Object_DecRef((PKIX_PL_Object *)formatString,
     60                                        plContext);
     61    if (errorResult)
     62        testError("PKIX_PL_Object_DecRef failed");
     63 
     64    return (NULL);
     65 }
     66 
     67 static PKIX_Error *
     68 comparator(
     69    PKIX_PL_Object *first,
     70    PKIX_PL_Object *second,
     71    PKIX_Int32 *pValue,
     72    /* ARGSUSED */ void *plContext)
     73 {
     74    if (*(char *)first > *(char *)second)
     75        *pValue = 1;
     76    else if (*(char *)first < *(char *)second)
     77        *pValue = -1;
     78    else
     79        *pValue = 0;
     80    return (NULL);
     81 }
     82 
     83 static PKIX_Error *
     84 hashcodeCallback(
     85    PKIX_PL_Object *object,
     86    PKIX_UInt32 *pValue,
     87    /* ARGSUSED */ void *plContext)
     88 {
     89    *pValue = 123456789;
     90    return (NULL);
     91 }
     92 
     93 static PKIX_Error *
     94 equalsCallback(
     95    PKIX_PL_Object *first,
     96    PKIX_PL_Object *second,
     97    PKIX_Boolean *result,
     98    void *plContext)
     99 {
    100 
    101    PKIX_UInt32 firstType = 0, secondType = 0;
    102 
    103    if ((first == plContext) || (second == plContext))
    104        testError("Null Object");
    105 
    106    (void)PKIX_PL_Object_GetType(first, &firstType, plContext);
    107 
    108    (void)PKIX_PL_Object_GetType(second, &secondType, plContext);
    109 
    110    *result = (firstType == secondType) ? PKIX_TRUE : PKIX_FALSE;
    111 
    112    return (NULL);
    113 }
    114 
    115 static void
    116 createObjects(
    117    PKIX_PL_Object **obj,
    118    PKIX_PL_Object **obj2,
    119    PKIX_PL_Object **obj3,
    120    PKIX_PL_Object **obj4)
    121 {
    122    PKIX_TEST_STD_VARS();
    123 
    124 #ifdef PKIX_USER_OBJECT_TYPE
    125    PKIX_TEST_EXPECT_NO_ERROR(PKIX_PL_Object_RegisterType(1000,       /* type */
    126                                                          "thousand", /* description */
    127                                                          NULL,       /* destructor */
    128                                                          NULL,       /* equals */
    129                                                          (PKIX_PL_HashcodeCallback)hashcodeCallback,
    130                                                          NULL, /* toString */
    131                                                          NULL, /* Comparator */
    132                                                          NULL,
    133                                                          plContext));
    134 
    135    PKIX_TEST_EXPECT_NO_ERROR(PKIX_PL_Object_Alloc(1000, /* type */
    136                                                   12,   /* size */
    137                                                   obj,
    138                                                   plContext));
    139 
    140    PKIX_TEST_EXPECT_NO_ERROR(PKIX_PL_Object_RegisterType(2000, /* type */
    141                                                          "two thousand" /* description */,
    142                                                          (PKIX_PL_DestructorCallback)destructor,
    143                                                          (PKIX_PL_EqualsCallback)equalsCallback,
    144                                                          NULL, /* hashcode */
    145                                                          (PKIX_PL_ToStringCallback)toStringCallback,
    146                                                          (PKIX_PL_ComparatorCallback)comparator,
    147                                                          NULL,
    148                                                          plContext));
    149 
    150    PKIX_TEST_EXPECT_NO_ERROR(PKIX_PL_Object_Alloc(2000, /* type */
    151                                                   1,    /* size */
    152                                                   obj2,
    153                                                   plContext));
    154 
    155    PKIX_TEST_EXPECT_NO_ERROR(PKIX_PL_Object_Alloc(2000, /* type */
    156                                                   1,    /* size */
    157                                                   obj4,
    158                                                   plContext));
    159 
    160    *obj3 = *obj;
    161    PKIX_TEST_EXPECT_NO_ERROR(PKIX_PL_Object_IncRef(*obj3, plContext));
    162 
    163 cleanup:
    164 #endif /* PKIX_USER_OBJECT_TYPE */
    165    PKIX_TEST_RETURN();
    166 }
    167 
    168 static void
    169 testGetType(
    170    PKIX_PL_Object *obj,
    171    PKIX_PL_Object *obj2,
    172    PKIX_PL_Object *obj3)
    173 {
    174    PKIX_UInt32 testType;
    175 
    176    PKIX_TEST_STD_VARS();
    177 
    178    PKIX_TEST_EXPECT_NO_ERROR(PKIX_PL_Object_GetType(obj, &testType, plContext));
    179 
    180    if (testType != 1000)
    181        testError("Object 1 returned the wrong type");
    182 
    183    PKIX_TEST_EXPECT_NO_ERROR(PKIX_PL_Object_GetType(obj2, &testType, plContext));
    184    if (testType != 2000)
    185        testError("Object 2 returned the wrong type");
    186 
    187    PKIX_TEST_EXPECT_NO_ERROR(PKIX_PL_Object_GetType(obj3, &testType, plContext));
    188    if (testType != 1000)
    189        testError("Object 3 returned the wrong type");
    190 
    191 cleanup:
    192 
    193    PKIX_TEST_RETURN();
    194 }
    195 
    196 static void
    197 testCompare(
    198    PKIX_PL_Object *obj2,
    199    PKIX_PL_Object *obj4)
    200 {
    201    PKIX_Int32 cmpResult;
    202    PKIX_TEST_STD_VARS();
    203 
    204    *(char *)obj2 = 0x20;
    205    *(char *)obj4 = 0x10;
    206    PKIX_TEST_EXPECT_NO_ERROR(PKIX_PL_Object_Compare(obj2, obj4, &cmpResult, plContext));
    207    if (cmpResult <= 0)
    208        testError("Invalid Result from Object Compare");
    209 
    210    PKIX_TEST_EXPECT_NO_ERROR(PKIX_PL_Object_Compare(obj4, obj2, &cmpResult, plContext));
    211    if (cmpResult >= 0)
    212        testError("Invalid Result from Object Compare");
    213 
    214    PKIX_TEST_EXPECT_NO_ERROR(PKIX_PL_Object_Compare(obj4, obj4, &cmpResult, plContext));
    215 
    216    *(char *)obj2 = 0x10;
    217    if (cmpResult != 0)
    218        testError("Invalid Result from Object Compare");
    219 
    220 cleanup:
    221 
    222    PKIX_TEST_RETURN();
    223 }
    224 
    225 static void
    226 testDestroy(
    227    PKIX_PL_Object *obj,
    228    PKIX_PL_Object *obj2,
    229    PKIX_PL_Object *obj3,
    230    PKIX_PL_Object *obj4)
    231 {
    232    PKIX_TEST_STD_VARS();
    233 
    234    PKIX_TEST_DECREF_BC(obj);
    235    PKIX_TEST_DECREF_BC(obj2);
    236    PKIX_TEST_DECREF_BC(obj3);
    237    PKIX_TEST_DECREF_BC(obj4);
    238 
    239 cleanup:
    240 
    241    PKIX_TEST_RETURN();
    242 }
    243 
    244 int
    245 test_object(int argc, char *argv[])
    246 {
    247 
    248 #ifdef PKIX_USER_OBJECT_TYPE
    249    PKIX_PL_Object *obj, *obj2, *obj3, *obj4;
    250    PKIX_UInt32 actualMinorVersion;
    251    PKIX_UInt32 j = 0;
    252 
    253    PKIX_TEST_STD_VARS();
    254 
    255    startTests("Objects");
    256 
    257    PKIX_TEST_EXPECT_NO_ERROR(
    258        PKIX_PL_NssContext_Create(0, PKIX_FALSE, NULL, &plContext));
    259 
    260    subTest("PKIX_PL_Object_Create");
    261    createObjects(&obj, &obj2, &obj3, &obj4);
    262 
    263    PKIX_TEST_EQ_HASH_TOSTR_DUP(obj, obj3, obj2, NULL, Object, PKIX_FALSE);
    264 
    265    subTest("PKIX_PL_Object_GetType");
    266    testGetType(obj, obj2, obj3);
    267 
    268    subTest("PKIX_PL_Object_Compare");
    269    testCompare(obj2, obj4);
    270 
    271    subTest("PKIX_PL_Object_Destroy");
    272    testDestroy(obj, obj2, obj3, obj4);
    273 
    274 cleanup:
    275 
    276    PKIX_Shutdown(plContext);
    277 
    278    endTests("Objects");
    279 #endif /* PKIX_USER_OBJECT_TYPE */
    280    return (0);
    281 }