nss: import at 3.0.1 beta 1
[mozilla-nss.git] / security / nss / cmd / libpkix / pkix_pl / module / test_httpcertstore.c
blob6a45c477632c8e64ea56bbea743467cb12fbe800
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 static void *plContext = NULL;
44 static
45 void printUsage(char *testname)
47 char *fmt =
48 "USAGE: %s [-arenas] certDir certName\n";
49 printf(fmt, "test_httpcertstore");
52 /* Functional tests for Socket public functions */
53 static
54 void do_other_work(void) { /* while waiting for nonblocking I/O to complete */
55 (void) PR_Sleep(2*60);
58 PKIX_Error *
59 PKIX_PL_HttpCertStore_Create(
60 PKIX_PL_HttpClient *client, /* if NULL, use default Client */
61 PKIX_PL_GeneralName *location,
62 PKIX_CertStore **pCertStore,
63 void *plContext);
65 PKIX_Error *
66 pkix_pl_HttpCertStore_CreateWithAsciiName(
67 PKIX_PL_HttpClient *client, /* if NULL, use default Client */
68 char *location,
69 PKIX_CertStore **pCertStore,
70 void *plContext);
72 static PKIX_Error *
73 getLocation(
74 PKIX_PL_Cert *certWithAia,
75 PKIX_PL_GeneralName **pLocation,
76 void *plContext)
78 PKIX_List *aiaList = NULL;
79 PKIX_UInt32 size = 0;
80 PKIX_PL_InfoAccess *aia = NULL;
81 PKIX_UInt32 iaType = PKIX_INFOACCESS_LOCATION_UNKNOWN;
82 PKIX_PL_GeneralName *location = NULL;
84 PKIX_TEST_STD_VARS();
86 subTest("Getting Authority Info Access");
88 PKIX_TEST_EXPECT_NO_ERROR(PKIX_PL_Cert_GetAuthorityInfoAccess
89 (certWithAia, &aiaList, plContext));
91 PKIX_TEST_EXPECT_NO_ERROR(PKIX_List_GetLength
92 (aiaList, &size, plContext));
94 if (size != 1) {
95 pkixTestErrorMsg = "unexpected number of AIA";
96 goto cleanup;
99 PKIX_TEST_EXPECT_NO_ERROR(PKIX_List_GetItem
100 (aiaList, 0, (PKIX_PL_Object **) &aia, plContext));
102 PKIX_TEST_EXPECT_NO_ERROR(PKIX_PL_InfoAccess_GetLocationType
103 (aia, &iaType, plContext));
105 if (iaType != PKIX_INFOACCESS_LOCATION_HTTP) {
106 pkixTestErrorMsg = "unexpected location type in AIA";
107 goto cleanup;
111 PKIX_TEST_EXPECT_NO_ERROR(PKIX_PL_InfoAccess_GetLocation
112 (aia, &location, plContext));
114 *pLocation = location;
116 cleanup:
117 PKIX_TEST_DECREF_AC(aiaList);
118 PKIX_TEST_DECREF_AC(aia);
120 PKIX_TEST_RETURN();
122 return (NULL);
125 int test_httpcertstore(int argc, char *argv[])
128 PKIX_UInt32 i = 0;
129 PKIX_UInt32 numCerts = 0;
130 PKIX_UInt32 numCrls = 0;
131 int j = 0;
132 PKIX_UInt32 actualMinorVersion;
133 PKIX_UInt32 length = 0;
135 char *certName = NULL;
136 char *certDir = NULL;
137 PKIX_PL_Cert *cmdLineCert = NULL;
138 PKIX_PL_Cert *cert = NULL;
139 PKIX_CertSelector *certSelector = NULL;
140 PKIX_CertStore *certStore = NULL;
141 PKIX_CertStore *crlStore = NULL;
142 PKIX_PL_GeneralName *location = NULL;
143 PKIX_CertStore_CertCallback getCerts = NULL;
144 PKIX_List *certs = NULL;
145 char *asciiResult = NULL;
146 void *nbio = NULL;
148 PKIX_PL_CRL *crl = NULL;
149 PKIX_CRLSelector *crlSelector = NULL;
150 char *crlLocation = "http://betty.nist.gov/pathdiscoverytestsuite/CRL"
151 "files/BasicHTTPURIPeer2CACRL.crl";
152 PKIX_CertStore_CRLCallback getCrls = NULL;
153 PKIX_List *crls = NULL;
154 PKIX_PL_String *crlString = NULL;
156 PKIX_TEST_STD_VARS();
158 startTests("HttpCertStore");
160 PKIX_TEST_EXPECT_NO_ERROR(
161 PKIX_PL_NssContext_Create(0, PKIX_FALSE, NULL, &plContext));
163 if (argc != (j + 3)) {
164 printUsage(argv[0]);
165 pkixTestErrorMsg = "Missing command line argument.";
166 goto cleanup;
169 certDir = argv[++j];
170 certName = argv[++j];
172 cmdLineCert = createCert(certDir, certName, plContext);
173 if (cmdLineCert == NULL) {
174 pkixTestErrorMsg = "Unable to create Cert";
175 goto cleanup;
178 /* muster arguments to create HttpCertStore */
179 PKIX_TEST_EXPECT_NO_ERROR(getLocation
180 (cmdLineCert, &location, plContext));
182 if (location == NULL) {
183 pkixTestErrorMsg = "Give me a cert with an HTTP URI!";
184 goto cleanup;
187 /* create HttpCertStore */
188 PKIX_TEST_EXPECT_NO_ERROR(PKIX_PL_HttpCertStore_Create
189 (NULL, location, &certStore, plContext));
191 /* get the GetCerts callback */
192 PKIX_TEST_EXPECT_NO_ERROR(PKIX_CertStore_GetCertCallback
193 (certStore, &getCerts, plContext));
195 /* create a CertSelector */
196 PKIX_TEST_EXPECT_NO_ERROR(PKIX_CertSelector_Create
197 (NULL, NULL, &certSelector, plContext));
199 /* Get the certs */
200 PKIX_TEST_EXPECT_NO_ERROR(getCerts
201 (certStore, certSelector, &nbio, &certs, plContext));
203 while (nbio != NULL) {
204 /* poll for a completion */
206 PKIX_TEST_EXPECT_NO_ERROR(PKIX_CertStore_CertContinue
207 (certStore, certSelector, &nbio, &certs, plContext));
210 if (certs) {
212 PKIX_TEST_EXPECT_NO_ERROR
213 (PKIX_List_GetLength(certs, &numCerts, plContext));
215 if (numCerts == 0) {
216 printf("HttpCertStore returned an empty Cert list\n");
217 goto cleanup;
220 for (i = 0; i < numCerts; i++) {
221 PKIX_TEST_EXPECT_NO_ERROR
222 (PKIX_List_GetItem
223 (certs,
225 (PKIX_PL_Object**)&cert,
226 plContext));
228 asciiResult = PKIX_Cert2ASCII(cert);
230 printf("CERT[%d]:\n%s\n", i, asciiResult);
232 /* PKIX_Cert2ASCII used PKIX_PL_Malloc(...,,NULL) */
233 PKIX_TEST_EXPECT_NO_ERROR
234 (PKIX_PL_Free(asciiResult, NULL));
235 asciiResult = NULL;
237 PKIX_TEST_DECREF_BC(cert);
239 } else {
240 printf("HttpCertStore returned a NULL Cert list\n");
243 /* create HttpCertStore */
244 PKIX_TEST_EXPECT_NO_ERROR(pkix_pl_HttpCertStore_CreateWithAsciiName
245 (NULL, crlLocation, &crlStore, plContext));
247 /* get the GetCrls callback */
248 PKIX_TEST_EXPECT_NO_ERROR(PKIX_CertStore_GetCRLCallback
249 (crlStore, &getCrls, plContext));
251 /* create a CrlSelector */
252 PKIX_TEST_EXPECT_NO_ERROR(PKIX_CRLSelector_Create
253 (NULL, NULL, &crlSelector, plContext));
255 /* Get the crls */
256 PKIX_TEST_EXPECT_NO_ERROR(getCrls
257 (crlStore, crlSelector, &nbio, &crls, plContext));
259 while (nbio != NULL) {
260 /* poll for a completion */
262 PKIX_TEST_EXPECT_NO_ERROR(PKIX_CertStore_CrlContinue
263 (crlStore, crlSelector, &nbio, &crls, plContext));
266 if (crls) {
268 PKIX_TEST_EXPECT_NO_ERROR
269 (PKIX_List_GetLength(crls, &numCrls, plContext));
271 if (numCrls == 0) {
272 printf("HttpCertStore returned an empty CRL list\n");
273 goto cleanup;
276 for (i = 0; i < numCrls; i++) {
277 PKIX_TEST_EXPECT_NO_ERROR
278 (PKIX_List_GetItem
279 (crls,
281 (PKIX_PL_Object**)&crl,
282 plContext));
284 PKIX_TEST_EXPECT_NO_ERROR(PKIX_PL_Object_ToString(
285 (PKIX_PL_Object *)crl,
286 &crlString,
287 plContext));
289 PKIX_TEST_EXPECT_NO_ERROR(PKIX_PL_String_GetEncoded
290 (crlString,
291 PKIX_ESCASCII,
292 (void **)&asciiResult,
293 &length,
294 plContext));
296 printf("CRL[%d]:\n%s\n", i, asciiResult);
298 PKIX_TEST_EXPECT_NO_ERROR
299 (PKIX_PL_Free(asciiResult, plContext));
300 PKIX_TEST_DECREF_BC(crlString);
301 PKIX_TEST_DECREF_BC(crl);
303 } else {
304 printf("HttpCertStore returned a NULL CRL list\n");
307 cleanup:
309 PKIX_TEST_DECREF_AC(cert);
310 PKIX_TEST_DECREF_AC(cmdLineCert);
311 PKIX_TEST_DECREF_AC(certStore);
312 PKIX_TEST_DECREF_AC(crlStore);
313 PKIX_TEST_DECREF_AC(location);
314 PKIX_TEST_DECREF_AC(certs);
315 PKIX_TEST_DECREF_AC(crl);
316 PKIX_TEST_DECREF_AC(crlString);
317 PKIX_TEST_DECREF_AC(crls);
319 PKIX_TEST_RETURN();
321 endTests("HttpDefaultClient");
323 return (0);