Import from 1.9a8 tarball
[mozilla-nss.git] / security / nss / cmd / libpkix / pkix / params / test_trustanchor.c
blobf0b22188f468a3b3daae47dfd58f1ab9f6bb2757
1 /* ***** BEGIN LICENSE BLOCK *****
2 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
4 * The contents of this file are subject to the Mozilla Public License Version
5 * 1.1 (the "License"); you may not use this file except in compliance with
6 * the License. You may obtain a copy of the License at
7 * http://www.mozilla.org/MPL/
9 * Software distributed under the License is distributed on an "AS IS" basis,
10 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
11 * for the specific language governing rights and limitations under the
12 * License.
14 * The Original Code is the Netscape security libraries.
16 * The Initial Developer of the Original Code is
17 * Netscape Communications Corporation.
18 * Portions created by the Initial Developer are Copyright (C) 1994-2000
19 * the Initial Developer. All Rights Reserved.
21 * Contributor(s):
22 * Sun Microsystems
24 * Alternatively, the contents of this file may be used under the terms of
25 * either the GNU General Public License Version 2 or later (the "GPL"), or
26 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
27 * in which case the provisions of the GPL or the LGPL are applicable instead
28 * of those above. If you wish to allow use of your version of this file only
29 * under the terms of either the GPL or the LGPL, and not to allow others to
30 * use your version of this file under the terms of the MPL, indicate your
31 * decision by deleting the provisions above and replace them with the notice
32 * and other provisions required by the GPL or the LGPL. If you do not delete
33 * the provisions above, a recipient may use your version of this file under
34 * the terms of any one of the MPL, the GPL or the LGPL.
36 * ***** END LICENSE BLOCK ***** */
38 * test_trustanchor.c
40 * Test TrustAnchor Type
44 #include "testutil.h"
45 #include "testutil_nss.h"
47 void *plContext = NULL;
49 void createTrustAnchors(
50 char *dirName,
51 char *goodInput,
52 PKIX_TrustAnchor **goodObject,
53 PKIX_TrustAnchor **equalObject,
54 PKIX_TrustAnchor **diffObject)
56 subTest("PKIX_TrustAnchor_CreateWithNameKeyPair <goodObject>");
57 *goodObject = createTrustAnchor
58 (dirName, goodInput, PKIX_FALSE, plContext);
60 subTest("PKIX_TrustAnchor_CreateWithNameKeyPair <equalObject>");
61 *equalObject = createTrustAnchor
62 (dirName, goodInput, PKIX_FALSE, plContext);
64 subTest("PKIX_TrustAnchor_CreateWithCert <diffObject>");
65 *diffObject = createTrustAnchor
66 (dirName, goodInput, PKIX_TRUE, plContext);
69 void testGetCAName(
70 PKIX_PL_Cert *diffCert,
71 PKIX_TrustAnchor *equalObject){
73 PKIX_PL_X500Name *diffCAName = NULL;
74 PKIX_PL_X500Name *equalCAName = NULL;
76 PKIX_TEST_STD_VARS();
77 subTest("PKIX_TrustAnchor_GetCAName");
79 PKIX_TEST_EXPECT_NO_ERROR(PKIX_PL_Cert_GetSubject
80 (diffCert, &diffCAName, plContext));
82 PKIX_TEST_EXPECT_NO_ERROR(PKIX_TrustAnchor_GetCAName
83 (equalObject, &equalCAName, plContext));
85 testEqualsHelper((PKIX_PL_Object *)diffCAName,
86 (PKIX_PL_Object *)equalCAName,
87 PKIX_TRUE,
88 plContext);
90 cleanup:
92 PKIX_TEST_DECREF_AC(diffCAName);
93 PKIX_TEST_DECREF_AC(equalCAName);
95 PKIX_TEST_RETURN();
98 void testGetCAPublicKey(
99 PKIX_PL_Cert *diffCert,
100 PKIX_TrustAnchor *equalObject){
102 PKIX_PL_PublicKey *diffPubKey = NULL;
103 PKIX_PL_PublicKey *equalPubKey = NULL;
105 PKIX_TEST_STD_VARS();
106 subTest("PKIX_TrustAnchor_GetCAPublicKey");
108 PKIX_TEST_EXPECT_NO_ERROR(PKIX_PL_Cert_GetSubjectPublicKey
109 (diffCert, &diffPubKey, plContext));
111 PKIX_TEST_EXPECT_NO_ERROR(PKIX_TrustAnchor_GetCAPublicKey
112 (equalObject, &equalPubKey, plContext));
114 testEqualsHelper((PKIX_PL_Object *)diffPubKey,
115 (PKIX_PL_Object *)equalPubKey,
116 PKIX_TRUE,
117 plContext);
119 cleanup:
121 PKIX_TEST_DECREF_AC(diffPubKey);
122 PKIX_TEST_DECREF_AC(equalPubKey);
124 PKIX_TEST_RETURN();
127 void testGetNameConstraints(char *dirName)
129 PKIX_TrustAnchor *goodObject = NULL;
130 PKIX_TrustAnchor *equalObject = NULL;
131 PKIX_TrustAnchor *diffObject = NULL;
132 PKIX_PL_Cert *diffCert;
133 PKIX_PL_CertNameConstraints *diffNC = NULL;
134 PKIX_PL_CertNameConstraints *equalNC = NULL;
135 char *goodInput = "nameConstraintsDN5CACert.crt";
136 char *expectedAscii =
137 "[\n"
138 "\tTrusted CA Name: CN=nameConstraints DN5 CA,"
139 "O=Test Certificates,C=US\n"
140 "\tTrusted CA PublicKey: PKCS #1 RSA Encryption\n"
141 "\tInitial Name Constraints:[\n"
142 "\t\tPermitted Name: (OU=permittedSubtree1,"
143 "O=Test Certificates,C=US)\n"
144 "\t\tExcluded Name: (OU=excludedSubtree1,"
145 "OU=permittedSubtree1,O=Test Certificates,C=US)\n"
146 "\t]\n"
147 "\n"
148 "]\n";
150 PKIX_TEST_STD_VARS();
152 subTest("Create TrustAnchors and compare");
154 createTrustAnchors
155 (dirName, goodInput, &goodObject, &equalObject, &diffObject);
157 PKIX_TEST_EQ_HASH_TOSTR_DUP
158 (goodObject,
159 equalObject,
160 diffObject,
161 expectedAscii,
162 TrustAnchor,
163 PKIX_TRUE);
165 subTest("PKIX_TrustAnchor_GetTrustedCert");
167 PKIX_TEST_EXPECT_NO_ERROR(PKIX_TrustAnchor_GetTrustedCert
168 (diffObject, &diffCert, plContext));
170 subTest("PKIX_PL_Cert_GetNameConstraints");
172 PKIX_TEST_EXPECT_NO_ERROR(PKIX_PL_Cert_GetNameConstraints
173 (diffCert, &diffNC, plContext));
175 subTest("PKIX_TrustAnchor_GetNameConstraints");
177 PKIX_TEST_EXPECT_NO_ERROR(PKIX_TrustAnchor_GetNameConstraints
178 (equalObject, &equalNC, plContext));
180 testEqualsHelper((PKIX_PL_Object *)diffNC,
181 (PKIX_PL_Object *)equalNC,
182 PKIX_TRUE,
183 plContext);
185 cleanup:
187 PKIX_TEST_DECREF_AC(diffNC);
188 PKIX_TEST_DECREF_AC(equalNC);
189 PKIX_TEST_DECREF_BC(diffCert);
190 PKIX_TEST_DECREF_BC(goodObject);
191 PKIX_TEST_DECREF_BC(equalObject);
192 PKIX_TEST_DECREF_BC(diffObject);
194 PKIX_TEST_RETURN();
197 static void
198 testDestroy(void *goodObject, void *equalObject, void *diffObject)
200 PKIX_TEST_STD_VARS();
202 subTest("PKIX_TrustAnchor_Destroy");
204 PKIX_TEST_DECREF_BC(goodObject);
205 PKIX_TEST_DECREF_BC(equalObject);
206 PKIX_TEST_DECREF_BC(diffObject);
208 cleanup:
210 PKIX_TEST_RETURN();
214 void printUsage(void) {
215 (void) printf("\nUSAGE:\ttest_trustanchor <NIST_FILES_DIR> <central-data-dir>\n\n");
218 int main(int argc, char *argv[]) {
220 PKIX_TrustAnchor *goodObject = NULL;
221 PKIX_TrustAnchor *equalObject = NULL;
222 PKIX_TrustAnchor *diffObject = NULL;
223 PKIX_PL_Cert *diffCert = NULL;
224 PKIX_UInt32 actualMinorVersion;
225 PKIX_Boolean useArenas = PKIX_FALSE;
226 PKIX_UInt32 j = 0;
228 char *goodInput = "yassir2yassir";
229 char *expectedAscii =
230 "[\n"
231 "\tTrusted CA Name: "
232 "CN=yassir,OU=bcn,OU=east,O=sun,C=us\n"
233 "\tTrusted CA PublicKey: ANSI X9.57 DSA Signature\n"
234 "\tInitial Name Constraints:(null)\n"
235 "]\n";
236 char *dirName = NULL;
237 char *dataCentralDir = NULL;
239 PKIX_TEST_STD_VARS();
241 startTests("TrustAnchor");
243 useArenas = PKIX_TEST_ARENAS_ARG(argv[1]);
245 PKIX_TEST_EXPECT_NO_ERROR(PKIX_Initialize
246 (PKIX_TRUE, /* nssInitNeeded */
247 useArenas,
248 PKIX_MAJOR_VERSION,
249 PKIX_MINOR_VERSION,
250 PKIX_MINOR_VERSION,
251 &actualMinorVersion,
252 &plContext));
254 if (argc < 3) {
255 printUsage();
256 return (0);
259 dirName = argv[j+1];
260 dataCentralDir = argv[j+2];
262 createTrustAnchors
263 (dataCentralDir,
264 goodInput,
265 &goodObject,
266 &equalObject,
267 &diffObject);
269 PKIX_TEST_EQ_HASH_TOSTR_DUP
270 (goodObject,
271 equalObject,
272 diffObject,
273 expectedAscii,
274 TrustAnchor,
275 PKIX_TRUE);
277 subTest("PKIX_TrustAnchor_GetTrustedCert");
278 PKIX_TEST_EXPECT_NO_ERROR(PKIX_TrustAnchor_GetTrustedCert
279 (diffObject, &diffCert, plContext));
281 testGetCAName(diffCert, equalObject);
282 testGetCAPublicKey(diffCert, equalObject);
284 testGetNameConstraints(dirName);
286 testDestroy(goodObject, equalObject, diffObject);
288 cleanup:
290 PKIX_TEST_DECREF_AC(diffCert);
292 PKIX_Shutdown(plContext);
294 PKIX_TEST_RETURN();
296 endTests("TrustAnchor");
298 return (0);