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  * testwrapper.c
6  *
7  * Wrpper programm for libpkix tests.
8  *
9  */
10 
11 #include <stdio.h>
12 
13 #include "nspr.h"
14 #include "plgetopt.h"
15 
16 #include "nss.h"
17 #include "secport.h"
18 
19 typedef int (*mainTestFn)(int argc, char *argv[]);
20 
21 extern int libpkix_buildthreads(int argc, char *argv[]);
22 extern int nss_threads(int argc, char *argv[]);
23 extern int test_certselector(int argc, char *argv[]);
24 extern int test_comcertselparams(int argc, char *argv[]);
25 extern int test_certchainchecker(int argc, char *argv[]);
26 extern int test_comcrlselparams(int argc, char *argv[]);
27 extern int test_crlselector(int argc, char *argv[]);
28 
29 extern int test_procparams(int argc, char *argv[]);
30 extern int test_resourcelimits(int argc, char *argv[]);
31 extern int test_trustanchor(int argc, char *argv[]);
32 extern int test_valparams(int argc, char *argv[]);
33 extern int test_buildresult(int argc, char *argv[]);
34 extern int test_policynode(int argc, char *argv[]);
35 extern int test_valresult(int argc, char *argv[]);
36 extern int test_verifynode(int argc, char *argv[]);
37 extern int test_store(int argc, char *argv[]);
38 extern int test_basicchecker(int argc, char *argv[]);
39 extern int test_basicconstraintschecker(int argc, char *argv[]);
40 extern int test_buildchain(int argc, char *argv[]);
41 extern int test_buildchain_partialchain(int argc, char *argv[]);
42 extern int test_buildchain_resourcelimits(int argc, char *argv[]);
43 extern int test_buildchain_uchecker(int argc, char *argv[]);
44 extern int test_customcrlchecker(int argc, char *argv[]);
45 extern int test_defaultcrlchecker2stores(int argc, char *argv[]);
46 extern int test_ocsp(int argc, char *argv[]);
47 extern int test_policychecker(int argc, char *argv[]);
48 extern int test_subjaltnamechecker(int argc, char *argv[]);
49 extern int test_validatechain(int argc, char *argv[]);
50 extern int test_validatechain_NB(int argc, char *argv[]);
51 extern int test_validatechain_bc(int argc, char *argv[]);
52 extern int test_error(int argc, char *argv[]);
53 extern int test_list(int argc, char *argv[]);
54 extern int test_list2(int argc, char *argv[]);
55 extern int test_logger(int argc, char *argv[]);
56 extern int test_colcertstore(int argc, char *argv[]);
57 extern int test_ekuchecker(int argc, char *argv[]);
58 extern int test_httpcertstore(int argc, char *argv[]);
59 extern int test_pk11certstore(int argc, char *argv[]);
60 extern int test_socket(int argc, char *argv[]);
61 extern int test_authorityinfoaccess(int argc, char *argv[]);
62 extern int test_cert(int argc, char *argv[]);
63 extern int test_crl(int argc, char *argv[]);
64 extern int test_crlentry(int argc, char *argv[]);
65 extern int test_date(int argc, char *argv[]);
66 extern int test_generalname(int argc, char *argv[]);
67 extern int test_nameconstraints(int argc, char *argv[]);
68 extern int test_subjectinfoaccess(int argc, char *argv[]);
69 extern int test_x500name(int argc, char *argv[]);
70 extern int stress_test(int argc, char *argv[]);
71 extern int test_bigint(int argc, char *argv[]);
72 extern int test_bytearray(int argc, char *argv[]);
73 extern int test_hashtable(int argc, char *argv[]);
74 extern int test_mem(int argc, char *argv[]);
75 extern int test_monitorlock(int argc, char *argv[]);
76 extern int test_mutex(int argc, char *argv[]);
77 extern int test_mutex2(int argc, char *argv[]);
78 extern int test_mutex3(int argc, char *argv[]);
79 extern int test_object(int argc, char *argv[]);
80 extern int test_oid(int argc, char *argv[]);
81 
82 /* Taken out. Problem with build                   */
83 /* extern int test_rwlock(int argc, char *argv[]); */
84 extern int test_string(int argc, char *argv[]);
85 extern int test_string2(int argc, char *argv[]);
86 extern int build_chain(int argc, char *argv[]);
87 extern int dumpcert(int argc, char *argv[]);
88 extern int dumpcrl(int argc, char *argv[]);
89 extern int validate_chain(int argc, char *argv[]);
90 
91 typedef struct {
92     char *fnName;
93     mainTestFn fnPointer;
94 } testFunctionRef;
95 
96 testFunctionRef testFnRefTable[] = {
97     { "libpkix_buildthreads", libpkix_buildthreads },
98     { "nss_threads", nss_threads },
99     { "test_certselector", test_certselector },
100     { "test_comcertselparams", test_comcertselparams },
101     { "test_certchainchecker", test_certchainchecker },
102     { "test_comcrlselparams", test_comcrlselparams },
103     { "test_crlselector", test_crlselector },
104     { "test_procparams", test_procparams },
105     { "test_resourcelimits", test_resourcelimits },
106     { "test_trustanchor", test_trustanchor },
107     { "test_valparams", test_valparams },
108     { "test_buildresult", test_buildresult },
109     { "test_policynode", test_policynode },
110     { "test_valresult", test_valresult },
111     { "test_verifynode", test_verifynode },
112     { "test_store", test_store },
113     { "test_basicchecker", test_basicchecker },
114     { "test_basicconstraintschecker", test_basicconstraintschecker },
115     { "test_buildchain", test_buildchain },
116     { "test_buildchain_partialchain", test_buildchain_partialchain },
117     { "test_buildchain_resourcelimits", test_buildchain_resourcelimits },
118     { "test_buildchain_uchecker", test_buildchain_uchecker },
119     { "test_customcrlchecker", test_customcrlchecker },
120     { "test_defaultcrlchecker2stores", test_defaultcrlchecker2stores },
121     { "test_ocsp", test_ocsp },
122     { "test_policychecker", test_policychecker },
123     { "test_subjaltnamechecker", test_subjaltnamechecker },
124     { "test_validatechain", test_validatechain },
125     { "test_validatechain_NB", test_validatechain_NB },
126     { "test_validatechain_bc", test_validatechain_bc },
127     { "test_error", test_error },
128     { "test_list", test_list },
129     { "test_list2", test_list2 },
130     { "test_logger", test_logger },
131     { "test_colcertstore", test_colcertstore },
132     { "test_ekuchecker", test_ekuchecker },
133     { "test_httpcertstore", test_httpcertstore },
134     { "test_pk11certstore", test_pk11certstore },
135     { "test_socket", test_socket },
136     { "test_authorityinfoaccess", test_authorityinfoaccess },
137     { "test_cert", test_cert },
138     { "test_crl", test_crl },
139     { "test_crlentry", test_crlentry },
140     { "test_date", test_date },
141     { "test_generalname", test_generalname },
142     { "test_nameconstraints", test_nameconstraints },
143     { "test_subjectinfoaccess", test_subjectinfoaccess },
144     { "test_x500name", test_x500name },
145     { "stress_test", stress_test },
146     { "test_bigint", test_bigint },
147     { "test_bytearray", test_bytearray },
148     { "test_hashtable", test_hashtable },
149     { "test_mem", test_mem },
150     { "test_monitorlock", test_monitorlock },
151     { "test_mutex", test_mutex },
152     { "test_mutex2", test_mutex2 },
153     { "test_mutex3", test_mutex3 },
154     { "test_object", test_object },
155     { "test_oid", test_oid },
156     /*  {"test_rwlock",                    test_rwlock }*/
157     { "test_string", test_string },
158     { "test_string2", test_string2 },
159     { "build_chain", build_chain },
160     { "dumpcert", dumpcert },
161     { "dumpcrl", dumpcrl },
162     { "validate_chain", validate_chain },
163     { NULL, NULL },
164 };
165 
166 static void
printUsage(char * cmdName)167 printUsage(char *cmdName)
168 {
169     int fnCounter = 0;
170 
171     fprintf(stderr, "Usage: %s [test name] [arg1]...[argN]\n\n", cmdName);
172     fprintf(stderr, "List of possible names for the tests:");
173     while (testFnRefTable[fnCounter].fnName != NULL) {
174         if (fnCounter % 2 == 0) {
175             fprintf(stderr, "\n");
176         }
177         fprintf(stderr, "  %-35s ", testFnRefTable[fnCounter].fnName);
178         fnCounter += 1;
179     }
180     fprintf(stderr, "\n");
181 }
182 
183 static SECStatus
getTestArguments(int argc,char ** argv,mainTestFn * ptestFn,char ** pdbPath,int * pargc,char *** pargv)184 getTestArguments(int argc,
185                  char **argv,
186                  mainTestFn *ptestFn,
187                  char **pdbPath,
188                  int *pargc,
189                  char ***pargv)
190 {
191     PLOptState *optstate = NULL;
192     PLOptStatus status;
193     mainTestFn testFunction = NULL;
194     char **wArgv = NULL;
195     char *dbPath = NULL;
196     char *fnName = NULL;
197     int wArgc = 0;
198     int fnCounter = 0;
199 
200     if (argc < 2) {
201         printf("ERROR: insufficient number of arguments: %s.\n", fnName);
202         return SECFailure;
203     }
204 
205     fnName = argv[1];
206     while (testFnRefTable[fnCounter].fnName != NULL) {
207         if (!PORT_Strcmp(fnName, testFnRefTable[fnCounter].fnName)) {
208             testFunction = testFnRefTable[fnCounter].fnPointer;
209             break;
210         }
211         fnCounter += 1;
212     }
213     if (!testFunction) {
214         printf("ERROR: unknown name of the test: %s.\n", fnName);
215         return SECFailure;
216     }
217 
218     wArgv = PORT_ZNewArray(char *, argc);
219     if (!wArgv) {
220         return SECFailure;
221     }
222 
223     /* set name of the function as a first arg and increment arg count. */
224     wArgv[0] = fnName;
225     wArgc += 1;
226 
227     optstate = PL_CreateOptState(argc - 1, argv + 1, "d:");
228     while ((status = PL_GetNextOpt(optstate)) == PL_OPT_OK) {
229         switch (optstate->option) {
230             case 'd':
231                 dbPath = (char *)optstate->value;
232                 break;
233 
234             default:
235                 wArgv[wArgc] = (char *)optstate->value;
236                 wArgc += 1;
237                 break;
238         }
239     }
240     PL_DestroyOptState(optstate);
241 
242     *ptestFn = testFunction;
243     *pdbPath = dbPath;
244     *pargc = wArgc;
245     *pargv = wArgv;
246 
247     return SECSuccess;
248 }
249 
250 static int
runCmd(mainTestFn fnPointer,int argc,char ** argv,char * dbPath)251 runCmd(mainTestFn fnPointer,
252        int argc,
253        char **argv,
254        char *dbPath)
255 {
256     int retStat = 0;
257 
258     /*  Initialize NSPR and NSS.  */
259     PR_Init(PR_SYSTEM_THREAD, PR_PRIORITY_NORMAL, 1);
260 
261     /* if using databases, use NSS_Init and not NSS_NoDB_Init */
262     if (dbPath && PORT_Strlen(dbPath) != 0) {
263         if (NSS_Init(dbPath) != SECSuccess)
264             return SECFailure;
265     } else {
266         if (NSS_NoDB_Init(NULL) != 0)
267             return SECFailure;
268     }
269     retStat = fnPointer(argc, argv);
270 
271     if (NSS_Shutdown() != SECSuccess) {
272         exit(1);
273     }
274     PR_Cleanup();
275     return retStat;
276 }
277 
278 int
main(int argc,char ** argv)279 main(int argc, char **argv)
280 {
281     mainTestFn testFunction = NULL;
282     char *dbPath = NULL;
283     char **testArgv = NULL;
284     int testArgc = 0;
285     int rv = 0;
286 
287     rv = getTestArguments(argc, argv, &testFunction, &dbPath,
288                           &testArgc, &testArgv);
289     if (rv != SECSuccess) {
290         printUsage(argv[0]);
291         return 1;
292     }
293 
294     rv = runCmd(testFunction, testArgc, testArgv, dbPath);
295 
296     PORT_Free(testArgv);
297 
298     return rv;
299 }
300