Imported Upstream version 3.13.6
[platform/upstream/nss.git] / mozilla / security / nss / cmd / libpkix / pkix_pl / pki / test_generalname.c
1 /* ***** BEGIN LICENSE BLOCK *****
2  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
3  *
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/
8  *
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.
13  *
14  * The Original Code is the PKIX-C library.
15  *
16  * The Initial Developer of the Original Code is
17  * Sun Microsystems, Inc.
18  * Portions created by the Initial Developer are
19  * Copyright 2004-2007 Sun Microsystems, Inc.  All Rights Reserved.
20  *
21  * Contributor(s):
22  *   Sun Microsystems, Inc.
23  *
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.
35  *
36  * ***** END LICENSE BLOCK ***** */
37 /*
38  * test_generalname.c
39  *
40  * Test GeneralName Type
41  *
42  */
43
44 #include "testutil.h"
45 #include "testutil_nss.h"
46
47 static void *plContext = NULL;
48
49 static void
50 createGeneralNames(PKIX_UInt32 nameType, char *goodInput, char *diffInput,
51                     PKIX_PL_GeneralName **goodName,
52                     PKIX_PL_GeneralName **equalName,
53                     PKIX_PL_GeneralName **diffName){
54
55         subTest("PKIX_PL_GeneralName_Create <goodName>");
56         *goodName = createGeneralName(nameType, goodInput, plContext);
57
58         subTest("PKIX_PL_GeneralName_Create <equalName>");
59         *equalName = createGeneralName(nameType, goodInput, plContext);
60
61         subTest("PKIX_PL_GeneralName_Create <diffName>");
62         *diffName = createGeneralName(nameType, diffInput, plContext);
63
64 }
65
66 static void
67 testDestroy(void *goodObject, void *equalObject, void *diffObject)
68 {
69         PKIX_TEST_STD_VARS();
70
71         subTest("PKIX_PL_GeneralName_Destroy");
72
73         PKIX_TEST_DECREF_BC(goodObject);
74         PKIX_TEST_DECREF_BC(equalObject);
75         PKIX_TEST_DECREF_BC(diffObject);
76
77 cleanup:
78
79         PKIX_TEST_RETURN();
80
81 }
82
83 static void testNameType
84 (PKIX_UInt32 nameType, char *goodInput, char *diffInput, char *expectedAscii){
85
86         PKIX_PL_GeneralName *goodName = NULL;
87         PKIX_PL_GeneralName *equalName = NULL;
88         PKIX_PL_GeneralName *diffName = NULL;
89
90         createGeneralNames(nameType, goodInput, diffInput,
91                             &goodName, &equalName, &diffName);
92
93         PKIX_TEST_EQ_HASH_TOSTR_DUP
94                 (goodName,
95                 equalName,
96                 diffName,
97                 expectedAscii,
98                 GeneralName,
99                 PKIX_TRUE);
100
101         testDestroy(goodName, equalName, diffName);
102 }
103
104 int test_generalname(int argc, char *argv[]) {
105
106         char *goodInput = NULL;
107         char *diffInput = NULL;
108         PKIX_UInt32 actualMinorVersion;
109         PKIX_UInt32 j = 0;
110
111         PKIX_TEST_STD_VARS();
112
113         startTests("GeneralName");
114
115         PKIX_TEST_EXPECT_NO_ERROR(
116             PKIX_PL_NssContext_Create(0, PKIX_FALSE, NULL, &plContext));
117
118         goodInput = "john@sun.com";
119         diffInput = "john@labs.com";
120         testNameType(PKIX_RFC822_NAME, goodInput, diffInput, goodInput);
121
122         goodInput = "example1.com";
123         diffInput = "ex2.net";
124         testNameType(PKIX_DNS_NAME, goodInput, diffInput, goodInput);
125
126         goodInput = "cn=yassir, ou=labs, o=sun, c=us";
127         diffInput = "cn=alice, ou=labs, o=sun, c=us";
128         testNameType(PKIX_DIRECTORY_NAME,
129                     goodInput,
130                     diffInput,
131                     "CN=yassir,OU=labs,O=sun,C=us");
132
133         goodInput = "http://example1.com";
134         diffInput = "http://ex2.net";
135         testNameType(PKIX_URI_NAME, goodInput, diffInput, goodInput);
136
137         goodInput = "1.2.840.11";
138         diffInput = "1.2.840.115349";
139         testNameType(PKIX_OID_NAME, goodInput, diffInput, goodInput);
140
141         /*
142          * We don't support creating PKIX_EDIPARTY_NAME,
143          * PKIX_IP_NAME, OTHER_NAME, X400_ADDRESS from strings
144          */
145
146 cleanup:
147
148         PKIX_Shutdown(plContext);
149
150         PKIX_TEST_RETURN();
151
152         endTests("GeneralName");
153
154         return (0);
155 }