Import from 1.9a8 tarball
[mozilla-nss.git] / security / nss / cmd / libpkix / pkix_pl / pki / test_x500name.c
blobeb7bc640441dbd04791bc9c0fa5ac9ad8745e666
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_x500name.c
40 * Test X500Name Type
44 #include "testutil.h"
45 #include "testutil_nss.h"
47 void *plContext = NULL;
49 static PKIX_PL_X500Name *
50 createX500Name(char *asciiName, PKIX_Boolean expectedToPass){
52 PKIX_PL_X500Name *x500Name = NULL;
53 PKIX_PL_String *plString = NULL;
55 PKIX_TEST_STD_VARS();
57 PKIX_TEST_EXPECT_NO_ERROR
58 (PKIX_PL_String_Create
59 (PKIX_ESCASCII, asciiName, 0, &plString, plContext));
61 if (expectedToPass){
62 PKIX_TEST_EXPECT_NO_ERROR
63 (PKIX_PL_X500Name_Create
64 (plString, &x500Name, plContext));
65 } else {
66 PKIX_TEST_EXPECT_ERROR
67 (PKIX_PL_X500Name_Create
68 (plString, &x500Name, plContext));
71 cleanup:
73 PKIX_TEST_DECREF_AC(plString);
75 PKIX_TEST_RETURN();
77 return (x500Name);
80 static void
81 createX500Names(char *goodInput, char *diffInput, char *diffInputMatch,
82 PKIX_PL_X500Name **goodObject,
83 PKIX_PL_X500Name **equalObject,
84 PKIX_PL_X500Name **diffObject,
85 PKIX_PL_X500Name **diffObjectMatch)
87 char *badAscii = "cn=yas#sir,ou=labs,o=sun,c=us";
88 PKIX_PL_X500Name *badObject = NULL;
90 subTest("PKIX_PL_X500Name_Create <goodObject>");
91 *goodObject = createX500Name(goodInput, PKIX_TRUE);
93 subTest("PKIX_PL_X500Name_Create <equalObject>");
94 *equalObject = createX500Name(goodInput, PKIX_TRUE);
96 subTest("PKIX_PL_X500Name_Create <diffObject>");
97 *diffObject = createX500Name(diffInput, PKIX_TRUE);
99 subTest("PKIX_PL_X500Name_Create <diffObjectMatch>");
100 *diffObjectMatch = createX500Name(diffInputMatch, PKIX_TRUE);
102 subTest("PKIX_PL_X500Name_Create <negative>");
103 badObject = createX500Name(badAscii, PKIX_FALSE);
106 static void testMatchHelper
107 (PKIX_PL_X500Name *goodName, PKIX_PL_X500Name *otherName, PKIX_Boolean match)
109 PKIX_Boolean cmpResult;
111 PKIX_TEST_STD_VARS();
113 PKIX_TEST_EXPECT_NO_ERROR(PKIX_PL_X500Name_Match
114 (goodName,
115 otherName,
116 &cmpResult,
117 plContext));
119 if ((match && !cmpResult) || (!match && cmpResult)){
120 testError("unexpected mismatch");
121 (void) printf("Actual value:\t%d\n", cmpResult);
122 (void) printf("Expected value:\t%d\n", match);
125 cleanup:
127 PKIX_TEST_RETURN();
131 static void
132 testMatch(void *goodObject, void *diffObject, void *diffObjectMatch)
134 subTest("PKIX_PL_X500Name_Match <match>");
135 testMatchHelper((PKIX_PL_X500Name *)diffObject,
136 (PKIX_PL_X500Name *)diffObjectMatch,
137 PKIX_TRUE);
139 subTest("PKIX_PL_X500Name_Match <non-match>");
140 testMatchHelper((PKIX_PL_X500Name *)goodObject,
141 (PKIX_PL_X500Name *)diffObject,
142 PKIX_FALSE);
145 static void testDestroy
146 (void *goodObject, void *equalObject, void *diffObject, void *diffObjectMatch)
148 PKIX_TEST_STD_VARS();
150 subTest("PKIX_PL_X500Name_Destroy");
152 PKIX_TEST_DECREF_BC(goodObject);
153 PKIX_TEST_DECREF_BC(equalObject);
154 PKIX_TEST_DECREF_BC(diffObject);
155 PKIX_TEST_DECREF_BC(diffObjectMatch);
157 cleanup:
159 PKIX_TEST_RETURN();
163 int main(int argc, char *argv[]) {
165 PKIX_PL_X500Name *goodObject = NULL;
166 PKIX_PL_X500Name *equalObject = NULL;
167 PKIX_PL_X500Name *diffObject = NULL;
168 PKIX_PL_X500Name *diffObjectMatch = NULL;
169 PKIX_Boolean useArenas = PKIX_FALSE;
170 PKIX_UInt32 actualMinorVersion;
171 PKIX_UInt32 j = 0;
173 /* goodInput is encoded in PKIX_ESCASCII */
174 char *goodInput = "cn=Strau&#x00Df;,ou=labs,o=sun,c=us";
175 char *diffInput = "cn=steve,ou=labs,o=sun,c=us";
176 char *diffInputMatch = "Cn=SteVe,Ou=lABs,o=SUn,c=uS";
177 char *expectedAscii = "CN=Strau&#x00DF;,OU=labs,O=sun,C=us";
179 PKIX_TEST_STD_VARS();
181 startTests("X500Name");
183 useArenas = PKIX_TEST_ARENAS_ARG(argv[1]);
185 PKIX_TEST_EXPECT_NO_ERROR(PKIX_Initialize
186 (PKIX_TRUE, /* nssInitNeeded */
187 useArenas,
188 PKIX_MAJOR_VERSION,
189 PKIX_MINOR_VERSION,
190 PKIX_MINOR_VERSION,
191 &actualMinorVersion,
192 &plContext));
194 createX500Names
195 (goodInput, diffInput, diffInputMatch,
196 &goodObject, &equalObject, &diffObject, &diffObjectMatch);
198 PKIX_TEST_EQ_HASH_TOSTR_DUP
199 (goodObject,
200 equalObject,
201 diffObject,
202 expectedAscii,
203 X500Name,
204 PKIX_TRUE);
206 testMatch(goodObject, diffObject, diffObjectMatch);
208 testDestroy(goodObject, equalObject, diffObject, diffObjectMatch);
210 cleanup:
212 PKIX_Shutdown(plContext);
214 PKIX_TEST_RETURN();
216 endTests("X500Name");
218 return (0);