tor-browser

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

secmodtest.c (2861B)


      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 /*
      6 * Regression test for bug 588269
      7 *
      8 * SECMOD_CloseUserDB should properly close the user DB, and it should
      9 * be possible to later re-add that same user DB as a new slot
     10 */
     11 
     12 #include "secutil.h"
     13 
     14 #include <stdio.h>
     15 #include <stdlib.h>
     16 #include <string.h>
     17 
     18 #include "nspr.h"
     19 #include "nss.h"
     20 #include "secerr.h"
     21 #include "pk11pub.h"
     22 #include "plgetopt.h"
     23 
     24 void
     25 Usage(char *progName)
     26 {
     27    fprintf(stderr, "Usage: %s -d dbDir\n", progName);
     28    exit(1);
     29 }
     30 
     31 SECStatus
     32 TestOpenCloseUserDB(char *progName, char *configDir, char *tokenName)
     33 {
     34    char *modspec = NULL;
     35    SECStatus rv = SECSuccess;
     36    PK11SlotInfo *userDbSlot = NULL;
     37 
     38    printf("Loading database <%s> - %s\n", configDir, tokenName);
     39    modspec = PR_smprintf("configDir='%s' tokenDescription='%s'",
     40                          configDir, tokenName);
     41    if (!modspec) {
     42        rv = SECFailure;
     43        goto loser;
     44    }
     45 
     46    userDbSlot = SECMOD_OpenUserDB(modspec);
     47    PR_smprintf_free(modspec);
     48    if (!userDbSlot) {
     49        SECU_PrintError(progName, "couldn't open database");
     50        rv = SECFailure;
     51        goto loser;
     52    }
     53 
     54    printf("Closing database\n");
     55    rv = SECMOD_CloseUserDB(userDbSlot);
     56 
     57    if (rv != SECSuccess) {
     58        SECU_PrintError(progName, "couldn't close database");
     59    }
     60 
     61    PK11_FreeSlot(userDbSlot);
     62 
     63 loser:
     64    return rv;
     65 }
     66 
     67 int
     68 main(int argc, char **argv)
     69 {
     70    PLOptState *optstate;
     71    PLOptStatus optstatus;
     72    SECStatus rv = SECFailure;
     73    char *progName;
     74    char *dbDir = NULL;
     75 
     76    progName = strrchr(argv[0], '/');
     77    if (!progName) {
     78        progName = strrchr(argv[0], '\\');
     79    }
     80    progName = progName ? progName + 1 : argv[0];
     81 
     82    optstate = PL_CreateOptState(argc, argv, "d:");
     83    while ((optstatus = PL_GetNextOpt(optstate)) == PL_OPT_OK) {
     84        switch (optstate->option) {
     85            case 'd':
     86                dbDir = strdup(optstate->value);
     87                break;
     88        }
     89    }
     90    if (optstatus == PL_OPT_BAD || dbDir == NULL) {
     91        Usage(progName);
     92    }
     93 
     94    rv = NSS_NoDB_Init(NULL);
     95    if (rv != SECSuccess) {
     96        SECU_PrintError(progName, "unable to initialize NSS");
     97        goto loser;
     98    }
     99 
    100    printf("Open and Close Test 1\n");
    101    rv = TestOpenCloseUserDB(progName, dbDir, "Test Slot 1");
    102    if (rv != SECSuccess) {
    103        goto loser;
    104    }
    105 
    106    printf("Open and Close Test 2\n");
    107    rv = TestOpenCloseUserDB(progName, dbDir, "Test Slot 2");
    108    if (rv != SECSuccess) {
    109        goto loser;
    110    }
    111 
    112 loser:
    113    if (dbDir)
    114        free(dbDir);
    115 
    116    if (NSS_Shutdown() != SECSuccess) {
    117        exit(1);
    118    }
    119    PR_Cleanup();
    120 
    121    if (rv != SECSuccess) {
    122        exit(1);
    123    }
    124 
    125    return 0;
    126 }