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