tor-browser

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

test_colcertstore.c (7133B)


      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_colcertstore.c
      6 *
      7 * Test CollectionCertStore Type
      8 *
      9 */
     10 
     11 #include "testutil.h"
     12 
     13 #include "testutil_nss.h"
     14 
     15 /* When CRL IDP is supported, change NUM_CRLS to 9 */
     16 #define PKIX_TEST_COLLECTIONCERTSTORE_NUM_CRLS 4
     17 #define PKIX_TEST_COLLECTIONCERTSTORE_NUM_CERTS 15
     18 
     19 static void *plContext = NULL;
     20 
     21 static PKIX_Error *
     22 testCRLSelectorMatchCallback(
     23    PKIX_CRLSelector *selector,
     24    PKIX_PL_CRL *crl,
     25    PKIX_Boolean *pMatch,
     26    void *plContext)
     27 {
     28    *pMatch = PKIX_TRUE;
     29 
     30    return (0);
     31 }
     32 
     33 static PKIX_Error *
     34 testCertSelectorMatchCallback(
     35    PKIX_CertSelector *selector,
     36    PKIX_PL_Cert *cert,
     37    PKIX_Boolean *pResult,
     38    void *plContext)
     39 {
     40    *pResult = PKIX_TRUE;
     41 
     42    return (0);
     43 }
     44 
     45 static PKIX_Error *
     46 getCertCallback(
     47    PKIX_CertStore *store,
     48    PKIX_CertSelector *certSelector,
     49    PKIX_List **pCerts,
     50    void *plContext)
     51 {
     52    return (0);
     53 }
     54 
     55 static char *
     56 catDirName(char *platform, char *dir, void *plContext)
     57 {
     58    char *pathName = NULL;
     59    PKIX_UInt32 dirLen;
     60    PKIX_UInt32 platformLen;
     61 
     62    PKIX_TEST_STD_VARS();
     63 
     64    dirLen = PL_strlen(dir);
     65    platformLen = PL_strlen(platform);
     66 
     67    PKIX_TEST_EXPECT_NO_ERROR(PKIX_PL_Malloc(platformLen +
     68                                                 dirLen +
     69                                                 2,
     70                                             (void **)&pathName, plContext));
     71 
     72    PL_strcpy(pathName, platform);
     73    PL_strcat(pathName, "/");
     74    PL_strcat(pathName, dir);
     75 
     76 cleanup:
     77 
     78    PKIX_TEST_RETURN();
     79 
     80    return (pathName);
     81 }
     82 
     83 static void
     84 testGetCRL(char *crlDir)
     85 {
     86    PKIX_PL_String *dirString = NULL;
     87    PKIX_CertStore_CRLCallback crlCallback;
     88    PKIX_CertStore *certStore = NULL;
     89    PKIX_CRLSelector *crlSelector = NULL;
     90    PKIX_List *crlList = NULL;
     91    PKIX_UInt32 numCrl = 0;
     92    void *nbioContext = NULL;
     93 
     94    PKIX_TEST_STD_VARS();
     95 
     96    PKIX_TEST_EXPECT_NO_ERROR(PKIX_PL_String_Create(PKIX_ESCASCII,
     97                                                    crlDir,
     98                                                    0,
     99                                                    &dirString,
    100                                                    plContext));
    101 
    102    subTest("PKIX_PL_CollectionCertStore_Create");
    103    PKIX_TEST_EXPECT_NO_ERROR(PKIX_PL_CollectionCertStore_Create(dirString,
    104                                                                 &certStore,
    105                                                                 plContext));
    106 
    107    subTest("PKIX_CRLSelector_Create");
    108    PKIX_TEST_EXPECT_NO_ERROR(PKIX_CRLSelector_Create(testCRLSelectorMatchCallback,
    109                                                      NULL,
    110                                                      &crlSelector,
    111                                                      plContext));
    112 
    113    subTest("PKIX_CertStore_GetCRLCallback");
    114    PKIX_TEST_EXPECT_NO_ERROR(PKIX_CertStore_GetCRLCallback(certStore, &crlCallback, NULL));
    115 
    116    subTest("Getting data from CRL Callback");
    117    PKIX_TEST_EXPECT_NO_ERROR(crlCallback(certStore,
    118                                          crlSelector,
    119                                          &nbioContext,
    120                                          &crlList,
    121                                          plContext));
    122 
    123    PKIX_TEST_EXPECT_NO_ERROR(PKIX_List_GetLength(crlList,
    124                                                  &numCrl,
    125                                                  plContext));
    126 
    127    if (numCrl != PKIX_TEST_COLLECTIONCERTSTORE_NUM_CRLS) {
    128        pkixTestErrorMsg = "unexpected CRL number mismatch";
    129    }
    130 
    131 cleanup:
    132 
    133    PKIX_TEST_DECREF_AC(dirString);
    134    PKIX_TEST_DECREF_AC(crlList);
    135    PKIX_TEST_DECREF_AC(crlSelector);
    136    PKIX_TEST_DECREF_AC(certStore);
    137 
    138    PKIX_TEST_RETURN();
    139 }
    140 
    141 static void
    142 testGetCert(char *certDir)
    143 {
    144    PKIX_PL_String *dirString = NULL;
    145    PKIX_CertStore_CertCallback certCallback;
    146    PKIX_CertStore *certStore = NULL;
    147    PKIX_CertSelector *certSelector = NULL;
    148    PKIX_List *certList = NULL;
    149    PKIX_UInt32 numCert = 0;
    150    void *nbioContext = NULL;
    151 
    152    PKIX_TEST_STD_VARS();
    153 
    154    PKIX_TEST_EXPECT_NO_ERROR(PKIX_PL_String_Create(PKIX_ESCASCII,
    155                                                    certDir,
    156                                                    0,
    157                                                    &dirString,
    158                                                    plContext));
    159 
    160    subTest("PKIX_PL_CollectionCertStore_Create");
    161    PKIX_TEST_EXPECT_NO_ERROR(PKIX_PL_CollectionCertStore_Create(dirString,
    162                                                                 &certStore,
    163                                                                 plContext));
    164 
    165    subTest("PKIX_CertSelector_Create");
    166    PKIX_TEST_EXPECT_NO_ERROR(PKIX_CertSelector_Create(testCertSelectorMatchCallback,
    167                                                       NULL,
    168                                                       &certSelector,
    169                                                       plContext));
    170 
    171    subTest("PKIX_CertStore_GetCertCallback");
    172    PKIX_TEST_EXPECT_NO_ERROR(PKIX_CertStore_GetCertCallback(certStore, &certCallback, NULL));
    173 
    174    subTest("Getting data from Cert Callback");
    175    PKIX_TEST_EXPECT_NO_ERROR(certCallback(certStore,
    176                                           certSelector,
    177                                           &nbioContext,
    178                                           &certList,
    179                                           plContext));
    180 
    181    PKIX_TEST_EXPECT_NO_ERROR(PKIX_List_GetLength(certList,
    182                                                  &numCert,
    183                                                  plContext));
    184 
    185    if (numCert != PKIX_TEST_COLLECTIONCERTSTORE_NUM_CERTS) {
    186        pkixTestErrorMsg = "unexpected Cert number mismatch";
    187    }
    188 
    189 cleanup:
    190 
    191    PKIX_TEST_DECREF_AC(dirString);
    192    PKIX_TEST_DECREF_AC(certList);
    193    PKIX_TEST_DECREF_AC(certSelector);
    194    PKIX_TEST_DECREF_AC(certStore);
    195 
    196    PKIX_TEST_RETURN();
    197 }
    198 
    199 static void
    200 printUsage(char *pName)
    201 {
    202    printf("\nUSAGE: %s test-purpose <data-dir> <platform-dir>\n\n", pName);
    203 }
    204 
    205 /* Functional tests for CollectionCertStore public functions */
    206 
    207 int
    208 test_colcertstore(int argc, char *argv[])
    209 {
    210 
    211    PKIX_UInt32 actualMinorVersion;
    212    PKIX_UInt32 j = 0;
    213    char *platformDir = NULL;
    214    char *dataDir = NULL;
    215    char *combinedDir = NULL;
    216 
    217    PKIX_TEST_STD_VARS();
    218 
    219    startTests("CollectionCertStore");
    220 
    221    PKIX_TEST_EXPECT_NO_ERROR(
    222        PKIX_PL_NssContext_Create(0, PKIX_FALSE, NULL, &plContext));
    223 
    224    if (argc < (3 + j)) {
    225        printUsage(argv[0]);
    226        return (0);
    227    }
    228 
    229    dataDir = argv[2 + j];
    230    platformDir = argv[3 + j];
    231    combinedDir = catDirName(platformDir, dataDir, plContext);
    232 
    233    testGetCRL(combinedDir);
    234    testGetCert(combinedDir);
    235 
    236 cleanup:
    237 
    238    pkixTestErrorResult = PKIX_PL_Free(combinedDir, plContext);
    239 
    240    PKIX_Shutdown(plContext);
    241 
    242    PKIX_TEST_RETURN();
    243 
    244    endTests("CollectionCertStore");
    245 
    246    return (0);
    247 }