security/nss/cmd/tests/secmodtest.c

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/security/nss/cmd/tests/secmodtest.c	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,122 @@
     1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.6 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.7 +
     1.8 +/*
     1.9 + * Regression test for bug 588269
    1.10 + *
    1.11 + * SECMOD_CloseUserDB should properly close the user DB, and it should
    1.12 + * be possible to later re-add that same user DB as a new slot
    1.13 + */
    1.14 +
    1.15 +#include "secutil.h"
    1.16 +
    1.17 +#include <stdio.h>
    1.18 +#include <stdlib.h>
    1.19 +#include <string.h>
    1.20 +
    1.21 +#include "nspr.h"
    1.22 +#include "nss.h"
    1.23 +#include "secerr.h"
    1.24 +#include "pk11pub.h"
    1.25 +#include "plgetopt.h"
    1.26 +
    1.27 +void Usage(char *progName)
    1.28 +{
    1.29 +    fprintf(stderr, "Usage: %s -d dbDir\n", progName);
    1.30 +    exit(1);
    1.31 +}
    1.32 +
    1.33 +SECStatus TestOpenCloseUserDB(char *progName, char *configDir, char *tokenName)
    1.34 +{
    1.35 +    char *modspec = NULL;
    1.36 +    SECStatus rv = SECSuccess;
    1.37 +    PK11SlotInfo *userDbSlot = NULL;
    1.38 +
    1.39 +    printf("Loading database <%s> - %s\n", configDir, tokenName);
    1.40 +    modspec = PR_smprintf("configDir='%s' tokenDescription='%s'",
    1.41 +                          configDir, tokenName);
    1.42 +    if (!modspec) {
    1.43 +        rv = SECFailure;
    1.44 +        goto loser;
    1.45 +    }
    1.46 +
    1.47 +    userDbSlot = SECMOD_OpenUserDB(modspec);
    1.48 +    PR_smprintf_free(modspec);
    1.49 +    if (!userDbSlot) {
    1.50 +        SECU_PrintError(progName, "couldn't open database");
    1.51 +        rv = SECFailure;
    1.52 +        goto loser;
    1.53 +    }
    1.54 +
    1.55 +    printf("Closing database\n");
    1.56 +    rv = SECMOD_CloseUserDB(userDbSlot);
    1.57 +
    1.58 +    if (rv != SECSuccess) {
    1.59 +        SECU_PrintError(progName, "couldn't close database");
    1.60 +    }
    1.61 +
    1.62 +    PK11_FreeSlot(userDbSlot);
    1.63 +
    1.64 +loser:
    1.65 +    return rv;
    1.66 +}
    1.67 +
    1.68 +int main(int argc, char **argv)
    1.69 +{
    1.70 +    PLOptState *optstate;
    1.71 +    PLOptStatus optstatus;
    1.72 +    SECStatus rv = SECFailure;
    1.73 +    char *progName;
    1.74 +    char *dbDir = NULL;
    1.75 +
    1.76 +    progName = strrchr(argv[0], '/');
    1.77 +    if (!progName) {
    1.78 +        progName = strrchr(argv[0], '\\');
    1.79 +    }
    1.80 +    progName = progName ? progName+1 : argv[0];
    1.81 +
    1.82 +    optstate = PL_CreateOptState(argc, argv, "d:");
    1.83 +    while ((optstatus = PL_GetNextOpt(optstate)) == PL_OPT_OK) {
    1.84 +        switch (optstate->option) {
    1.85 +          case 'd':
    1.86 +            dbDir = strdup(optstate->value);
    1.87 +            break;
    1.88 +        }
    1.89 +    }
    1.90 +    if (optstatus == PL_OPT_BAD || dbDir == NULL) {
    1.91 +        Usage(progName);
    1.92 +    }
    1.93 +
    1.94 +    rv = NSS_NoDB_Init(NULL);
    1.95 +    if (rv != SECSuccess) {
    1.96 +        SECU_PrintError(progName, "unable to initialize NSS");
    1.97 +        goto loser;
    1.98 +    }
    1.99 +
   1.100 +    printf("Open and Close Test 1\n");
   1.101 +    rv = TestOpenCloseUserDB(progName, dbDir, "Test Slot 1");
   1.102 +    if (rv != SECSuccess) {
   1.103 +        goto loser;
   1.104 +    }
   1.105 +
   1.106 +    printf("Open and Close Test 2\n");
   1.107 +    rv = TestOpenCloseUserDB(progName, dbDir, "Test Slot 2");
   1.108 +    if (rv != SECSuccess) {
   1.109 +        goto loser;
   1.110 +    }
   1.111 +
   1.112 +loser:
   1.113 +    if (dbDir) free(dbDir);
   1.114 +
   1.115 +    if (NSS_Shutdown() != SECSuccess) {
   1.116 +        exit(1);
   1.117 +    }
   1.118 +    PR_Cleanup();
   1.119 +
   1.120 +    if (rv != SECSuccess) {
   1.121 +        exit(1);
   1.122 +    }
   1.123 +
   1.124 +    return 0;
   1.125 +}

mercurial