Import from 1.9a8 tarball
[mozilla-nss.git] / security / nss / cmd / libpkix / pkix_pl / module / test_httpcertstore.c
blob4d9948a8a12f23b3ba1b0ffa61f9bc9d1a889fc0
1 /*
2 * test_httpcertstore.c
4 * Test Httpcertstore Type
6 * Copyright 2004-2005 Sun Microsystems, Inc. All rights reserved.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions are met:
11 * 1. Redistribution of source code must retain the above copyright notice,
12 * this list of conditions and the following disclaimer.
14 * 2. Redistribution in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
18 * Neither the name of Sun Microsystems, Inc. or the names of contributors may
19 * be used to endorse or promote products derived from this software without
20 * specific prior written permission.
22 * This software is provided "AS IS," without a warranty of any kind. ALL
23 * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING
24 * ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE
25 * OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN MICROSYSTEMS, INC. ("SUN")
26 * AND ITS LICENSORS SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE
27 * AS A RESULT OF USING, MODIFYING OR DISTRIBUTING THIS SOFTWARE OR ITS
28 * DERIVATIVES. IN NO EVENT WILL SUN OR ITS LICENSORS BE LIABLE FOR ANY LOST
29 * REVENUE, PROFIT OR DATA, OR FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL,
30 * INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY
31 * OF LIABILITY, ARISING OUT OF THE USE OF OR INABILITY TO USE THIS SOFTWARE,
32 * EVEN IF SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
34 * You acknowledge that this software is not designed or intended for use in
35 * the design, construction, operation or maintenance of any nuclear facility.
38 #include "testutil.h"
39 #include "testutil_nss.h"
40 #include "pkix_pl_common.h"
42 void *plContext = NULL;
44 void printUsage(char *testname) {
45 char *fmt =
46 "USAGE: %s [-arenas] certDir certName\n";
47 printf(fmt, "test_httpcertstore");
50 /* Functional tests for Socket public functions */
51 void do_other_work(void) { /* while waiting for nonblocking I/O to complete */
52 (void) PR_Sleep(2*60);
55 PKIX_Error *
56 PKIX_PL_HttpCertStore_Create(
57 PKIX_PL_HttpClient *client, /* if NULL, use default Client */
58 PKIX_PL_GeneralName *location,
59 PKIX_CertStore **pCertStore,
60 void *plContext);
62 PKIX_Error *
63 pkix_pl_HttpCertStore_CreateWithAsciiName(
64 PKIX_PL_HttpClient *client, /* if NULL, use default Client */
65 char *location,
66 PKIX_CertStore **pCertStore,
67 void *plContext);
69 PKIX_Error *
70 getLocation(
71 PKIX_PL_Cert *certWithAia,
72 PKIX_PL_GeneralName **pLocation,
73 void *plContext)
75 PKIX_List *aiaList = NULL;
76 PKIX_UInt32 size = 0;
77 PKIX_PL_InfoAccess *aia = NULL;
78 PKIX_UInt32 iaType = PKIX_INFOACCESS_LOCATION_UNKNOWN;
79 PKIX_PL_GeneralName *location = NULL;
81 PKIX_TEST_STD_VARS();
83 subTest("Getting Authority Info Access");
85 PKIX_TEST_EXPECT_NO_ERROR(PKIX_PL_Cert_GetAuthorityInfoAccess
86 (certWithAia, &aiaList, plContext));
88 PKIX_TEST_EXPECT_NO_ERROR(PKIX_List_GetLength
89 (aiaList, &size, plContext));
91 if (size != 1) {
92 pkixTestErrorMsg = "unexpected number of AIA";
93 goto cleanup;
96 PKIX_TEST_EXPECT_NO_ERROR(PKIX_List_GetItem
97 (aiaList, 0, (PKIX_PL_Object **) &aia, plContext));
99 PKIX_TEST_EXPECT_NO_ERROR(PKIX_PL_InfoAccess_GetLocationType
100 (aia, &iaType, plContext));
102 if (iaType != PKIX_INFOACCESS_LOCATION_HTTP) {
103 pkixTestErrorMsg = "unexpected location type in AIA";
104 goto cleanup;
108 PKIX_TEST_EXPECT_NO_ERROR(PKIX_PL_InfoAccess_GetLocation
109 (aia, &location, plContext));
111 *pLocation = location;
113 cleanup:
114 PKIX_TEST_DECREF_AC(aiaList);
115 PKIX_TEST_DECREF_AC(aia);
117 PKIX_TEST_RETURN();
119 return (NULL);
122 int main(int argc, char *argv[]) {
124 PKIX_UInt32 i = 0;
125 PKIX_UInt32 numCerts = 0;
126 PKIX_UInt32 numCrls = 0;
127 int j = 0;
128 PKIX_Boolean useArenas = PKIX_FALSE;
129 PKIX_UInt32 actualMinorVersion;
130 PKIX_UInt32 length = 0;
132 char *certName = NULL;
133 char *certDir = NULL;
134 PKIX_PL_Cert *cmdLineCert = NULL;
135 PKIX_PL_Cert *cert = NULL;
136 PKIX_CertSelector *certSelector = NULL;
137 PKIX_CertStore *certStore = NULL;
138 PKIX_CertStore *crlStore = NULL;
139 PKIX_PL_GeneralName *location = NULL;
140 PKIX_CertStore_CertCallback getCerts = NULL;
141 PKIX_List *certs = NULL;
142 char *asciiResult = NULL;
143 void *nbio = NULL;
145 PKIX_PL_CRL *crl = NULL;
146 PKIX_CRLSelector *crlSelector = NULL;
147 char *crlLocation = "http://betty.nist.gov/pathdiscoverytestsuite/CRL"
148 "files/BasicHTTPURIPeer2CACRL.crl";
149 PKIX_CertStore_CRLCallback getCrls = NULL;
150 PKIX_List *crls = NULL;
151 PKIX_PL_String *crlString = NULL;
153 PKIX_TEST_STD_VARS();
155 startTests("HttpCertStore");
157 useArenas = PKIX_TEST_ARENAS_ARG(argv[1]);
159 PKIX_TEST_EXPECT_NO_ERROR(PKIX_Initialize
160 (PKIX_TRUE, /* nssInitNeeded */
161 useArenas,
162 PKIX_MAJOR_VERSION,
163 PKIX_MINOR_VERSION,
164 PKIX_MINOR_VERSION,
165 &actualMinorVersion,
166 &plContext));
168 if (argc != (j + 3)) {
169 printUsage(argv[0]);
170 pkixTestErrorMsg = "Missing command line argument.";
171 goto cleanup;
174 certDir = argv[++j];
175 certName = argv[++j];
177 cmdLineCert = createCert(certDir, certName, plContext);
178 if (cmdLineCert == NULL) {
179 pkixTestErrorMsg = "Unable to create Cert";
180 goto cleanup;
183 /* muster arguments to create HttpCertStore */
184 PKIX_TEST_EXPECT_NO_ERROR(getLocation
185 (cmdLineCert, &location, plContext));
187 if (location == NULL) {
188 pkixTestErrorMsg = "Give me a cert with an HTTP URI!";
189 goto cleanup;
192 /* create HttpCertStore */
193 PKIX_TEST_EXPECT_NO_ERROR(PKIX_PL_HttpCertStore_Create
194 (NULL, location, &certStore, plContext));
196 /* get the GetCerts callback */
197 PKIX_TEST_EXPECT_NO_ERROR(PKIX_CertStore_GetCertCallback
198 (certStore, &getCerts, plContext));
200 /* create a CertSelector */
201 PKIX_TEST_EXPECT_NO_ERROR(PKIX_CertSelector_Create
202 (NULL, NULL, &certSelector, plContext));
204 /* Get the certs */
205 PKIX_TEST_EXPECT_NO_ERROR(getCerts
206 (certStore, certSelector, &nbio, &certs, plContext));
208 while (nbio != NULL) {
209 /* poll for a completion */
211 PKIX_TEST_EXPECT_NO_ERROR(PKIX_CertStore_CertContinue
212 (certStore, certSelector, &nbio, &certs, plContext));
215 if (certs) {
217 PKIX_TEST_EXPECT_NO_ERROR
218 (PKIX_List_GetLength(certs, &numCerts, plContext));
220 if (numCerts == 0) {
221 printf("HttpCertStore returned an empty Cert list\n");
222 goto cleanup;
225 for (i = 0; i < numCerts; i++) {
226 PKIX_TEST_EXPECT_NO_ERROR
227 (PKIX_List_GetItem
228 (certs,
230 (PKIX_PL_Object**)&cert,
231 plContext));
233 asciiResult = PKIX_Cert2ASCII(cert);
235 printf("CERT[%d]:\n%s\n", i, asciiResult);
237 /* PKIX_Cert2ASCII used PKIX_PL_Malloc(...,,NULL) */
238 PKIX_TEST_EXPECT_NO_ERROR
239 (PKIX_PL_Free(asciiResult, NULL));
240 asciiResult = NULL;
242 PKIX_TEST_DECREF_BC(cert);
244 } else {
245 printf("HttpCertStore returned a NULL Cert list\n");
248 /* create HttpCertStore */
249 PKIX_TEST_EXPECT_NO_ERROR(pkix_pl_HttpCertStore_CreateWithAsciiName
250 (NULL, crlLocation, &crlStore, plContext));
252 /* get the GetCrls callback */
253 PKIX_TEST_EXPECT_NO_ERROR(PKIX_CertStore_GetCRLCallback
254 (crlStore, &getCrls, plContext));
256 /* create a CrlSelector */
257 PKIX_TEST_EXPECT_NO_ERROR(PKIX_CRLSelector_Create
258 (NULL, NULL, &crlSelector, plContext));
260 /* Get the crls */
261 PKIX_TEST_EXPECT_NO_ERROR(getCrls
262 (crlStore, crlSelector, &nbio, &crls, plContext));
264 while (nbio != NULL) {
265 /* poll for a completion */
267 PKIX_TEST_EXPECT_NO_ERROR(PKIX_CertStore_CrlContinue
268 (crlStore, crlSelector, &nbio, &crls, plContext));
271 if (crls) {
273 PKIX_TEST_EXPECT_NO_ERROR
274 (PKIX_List_GetLength(crls, &numCrls, plContext));
276 if (numCrls == 0) {
277 printf("HttpCertStore returned an empty CRL list\n");
278 goto cleanup;
281 for (i = 0; i < numCrls; i++) {
282 PKIX_TEST_EXPECT_NO_ERROR
283 (PKIX_List_GetItem
284 (crls,
286 (PKIX_PL_Object**)&crl,
287 plContext));
289 PKIX_TEST_EXPECT_NO_ERROR(PKIX_PL_Object_ToString(
290 (PKIX_PL_Object *)crl,
291 &crlString,
292 plContext));
294 PKIX_TEST_EXPECT_NO_ERROR(PKIX_PL_String_GetEncoded
295 (crlString,
296 PKIX_ESCASCII,
297 (void **)&asciiResult,
298 &length,
299 plContext));
301 printf("CRL[%d]:\n%s\n", i, asciiResult);
303 PKIX_TEST_EXPECT_NO_ERROR
304 (PKIX_PL_Free(asciiResult, plContext));
305 PKIX_TEST_DECREF_BC(crlString);
306 PKIX_TEST_DECREF_BC(crl);
308 } else {
309 printf("HttpCertStore returned a NULL CRL list\n");
312 cleanup:
314 PKIX_TEST_DECREF_AC(cert);
315 PKIX_TEST_DECREF_AC(cmdLineCert);
316 PKIX_TEST_DECREF_AC(certStore);
317 PKIX_TEST_DECREF_AC(crlStore);
318 PKIX_TEST_DECREF_AC(location);
319 PKIX_TEST_DECREF_AC(certs);
320 PKIX_TEST_DECREF_AC(crl);
321 PKIX_TEST_DECREF_AC(crlString);
322 PKIX_TEST_DECREF_AC(crls);
324 PKIX_TEST_RETURN();
326 endTests("HttpDefaultClient");
328 return (0);