Thu, 22 Jan 2015 13:21:57 +0100
Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6
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/. */
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 */
12 #include "secutil.h"
14 #include <stdio.h>
15 #include <stdlib.h>
16 #include <string.h>
18 #include "nspr.h"
19 #include "nss.h"
20 #include "secerr.h"
21 #include "pk11pub.h"
22 #include "plgetopt.h"
24 void Usage(char *progName)
25 {
26 fprintf(stderr, "Usage: %s -d dbDir\n", progName);
27 exit(1);
28 }
30 SECStatus TestOpenCloseUserDB(char *progName, char *configDir, char *tokenName)
31 {
32 char *modspec = NULL;
33 SECStatus rv = SECSuccess;
34 PK11SlotInfo *userDbSlot = NULL;
36 printf("Loading database <%s> - %s\n", configDir, tokenName);
37 modspec = PR_smprintf("configDir='%s' tokenDescription='%s'",
38 configDir, tokenName);
39 if (!modspec) {
40 rv = SECFailure;
41 goto loser;
42 }
44 userDbSlot = SECMOD_OpenUserDB(modspec);
45 PR_smprintf_free(modspec);
46 if (!userDbSlot) {
47 SECU_PrintError(progName, "couldn't open database");
48 rv = SECFailure;
49 goto loser;
50 }
52 printf("Closing database\n");
53 rv = SECMOD_CloseUserDB(userDbSlot);
55 if (rv != SECSuccess) {
56 SECU_PrintError(progName, "couldn't close database");
57 }
59 PK11_FreeSlot(userDbSlot);
61 loser:
62 return rv;
63 }
65 int main(int argc, char **argv)
66 {
67 PLOptState *optstate;
68 PLOptStatus optstatus;
69 SECStatus rv = SECFailure;
70 char *progName;
71 char *dbDir = NULL;
73 progName = strrchr(argv[0], '/');
74 if (!progName) {
75 progName = strrchr(argv[0], '\\');
76 }
77 progName = progName ? progName+1 : argv[0];
79 optstate = PL_CreateOptState(argc, argv, "d:");
80 while ((optstatus = PL_GetNextOpt(optstate)) == PL_OPT_OK) {
81 switch (optstate->option) {
82 case 'd':
83 dbDir = strdup(optstate->value);
84 break;
85 }
86 }
87 if (optstatus == PL_OPT_BAD || dbDir == NULL) {
88 Usage(progName);
89 }
91 rv = NSS_NoDB_Init(NULL);
92 if (rv != SECSuccess) {
93 SECU_PrintError(progName, "unable to initialize NSS");
94 goto loser;
95 }
97 printf("Open and Close Test 1\n");
98 rv = TestOpenCloseUserDB(progName, dbDir, "Test Slot 1");
99 if (rv != SECSuccess) {
100 goto loser;
101 }
103 printf("Open and Close Test 2\n");
104 rv = TestOpenCloseUserDB(progName, dbDir, "Test Slot 2");
105 if (rv != SECSuccess) {
106 goto loser;
107 }
109 loser:
110 if (dbDir) free(dbDir);
112 if (NSS_Shutdown() != SECSuccess) {
113 exit(1);
114 }
115 PR_Cleanup();
117 if (rv != SECSuccess) {
118 exit(1);
119 }
121 return 0;
122 }