michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: /* michael@0: * Regression test for bug 588269 michael@0: * michael@0: * SECMOD_CloseUserDB should properly close the user DB, and it should michael@0: * be possible to later re-add that same user DB as a new slot michael@0: */ michael@0: michael@0: #include "secutil.h" michael@0: michael@0: #include michael@0: #include michael@0: #include michael@0: michael@0: #include "nspr.h" michael@0: #include "nss.h" michael@0: #include "secerr.h" michael@0: #include "pk11pub.h" michael@0: #include "plgetopt.h" michael@0: michael@0: void Usage(char *progName) michael@0: { michael@0: fprintf(stderr, "Usage: %s -d dbDir\n", progName); michael@0: exit(1); michael@0: } michael@0: michael@0: SECStatus TestOpenCloseUserDB(char *progName, char *configDir, char *tokenName) michael@0: { michael@0: char *modspec = NULL; michael@0: SECStatus rv = SECSuccess; michael@0: PK11SlotInfo *userDbSlot = NULL; michael@0: michael@0: printf("Loading database <%s> - %s\n", configDir, tokenName); michael@0: modspec = PR_smprintf("configDir='%s' tokenDescription='%s'", michael@0: configDir, tokenName); michael@0: if (!modspec) { michael@0: rv = SECFailure; michael@0: goto loser; michael@0: } michael@0: michael@0: userDbSlot = SECMOD_OpenUserDB(modspec); michael@0: PR_smprintf_free(modspec); michael@0: if (!userDbSlot) { michael@0: SECU_PrintError(progName, "couldn't open database"); michael@0: rv = SECFailure; michael@0: goto loser; michael@0: } michael@0: michael@0: printf("Closing database\n"); michael@0: rv = SECMOD_CloseUserDB(userDbSlot); michael@0: michael@0: if (rv != SECSuccess) { michael@0: SECU_PrintError(progName, "couldn't close database"); michael@0: } michael@0: michael@0: PK11_FreeSlot(userDbSlot); michael@0: michael@0: loser: michael@0: return rv; michael@0: } michael@0: michael@0: int main(int argc, char **argv) michael@0: { michael@0: PLOptState *optstate; michael@0: PLOptStatus optstatus; michael@0: SECStatus rv = SECFailure; michael@0: char *progName; michael@0: char *dbDir = NULL; michael@0: michael@0: progName = strrchr(argv[0], '/'); michael@0: if (!progName) { michael@0: progName = strrchr(argv[0], '\\'); michael@0: } michael@0: progName = progName ? progName+1 : argv[0]; michael@0: michael@0: optstate = PL_CreateOptState(argc, argv, "d:"); michael@0: while ((optstatus = PL_GetNextOpt(optstate)) == PL_OPT_OK) { michael@0: switch (optstate->option) { michael@0: case 'd': michael@0: dbDir = strdup(optstate->value); michael@0: break; michael@0: } michael@0: } michael@0: if (optstatus == PL_OPT_BAD || dbDir == NULL) { michael@0: Usage(progName); michael@0: } michael@0: michael@0: rv = NSS_NoDB_Init(NULL); michael@0: if (rv != SECSuccess) { michael@0: SECU_PrintError(progName, "unable to initialize NSS"); michael@0: goto loser; michael@0: } michael@0: michael@0: printf("Open and Close Test 1\n"); michael@0: rv = TestOpenCloseUserDB(progName, dbDir, "Test Slot 1"); michael@0: if (rv != SECSuccess) { michael@0: goto loser; michael@0: } michael@0: michael@0: printf("Open and Close Test 2\n"); michael@0: rv = TestOpenCloseUserDB(progName, dbDir, "Test Slot 2"); michael@0: if (rv != SECSuccess) { michael@0: goto loser; michael@0: } michael@0: michael@0: loser: michael@0: if (dbDir) free(dbDir); michael@0: michael@0: if (NSS_Shutdown() != SECSuccess) { michael@0: exit(1); michael@0: } michael@0: PR_Cleanup(); michael@0: michael@0: if (rv != SECSuccess) { michael@0: exit(1); michael@0: } michael@0: michael@0: return 0; michael@0: }