Imported Upstream version 3.13.6
[platform/upstream/nss.git] / mozilla / security / nss / cmd / libpkix / pkix_pl / pki / test_nameconstraints.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_nameconstraints.c
39  *
40  * Test CERT Name Constraints Type
41  *
42  */
43
44 #include "testutil.h"
45 #include "testutil_nss.h"
46
47 static void *plContext = NULL;
48
49 static char *catDirName(char *platform, char *dir, void *plContext)
50 {
51         char *pathName = NULL;
52         PKIX_UInt32 dirLen;
53         PKIX_UInt32 platformLen;
54
55         PKIX_TEST_STD_VARS();
56
57         dirLen = PL_strlen(dir);
58         platformLen = PL_strlen(platform);
59
60         PKIX_TEST_EXPECT_NO_ERROR(PKIX_PL_Malloc
61                 (platformLen + dirLen + 2, (void **)&pathName, plContext));
62
63         PL_strcpy(pathName, platform);
64         PL_strcat(pathName, "/");
65         PL_strcat(pathName, dir);
66
67 cleanup:
68
69         PKIX_TEST_RETURN();
70
71         return (pathName);
72 }
73
74 static void
75 testNameConstraints(char *dataDir)
76 {
77         char *goodPname = "nameConstraintsDN5CACert.crt";
78         PKIX_PL_Cert *goodCert = NULL;
79         PKIX_PL_CertNameConstraints *goodNC = NULL;
80         char *expectedAscii =
81                 "[\n"
82                 "\t\tPermitted Name:  (OU=permittedSubtree1,"
83                 "O=Test Certificates,C=US)\n"
84                 "\t\tExcluded Name:   (OU=excludedSubtree1,"
85                 "OU=permittedSubtree1,O=Test Certificates,C=US)\n"
86                 "\t]\n";
87
88         PKIX_TEST_STD_VARS();
89
90         subTest("PKIX_PL_CertNameConstraints");
91
92         goodCert = createCert(dataDir, goodPname, plContext);
93
94         subTest("PKIX_PL_Cert_GetNameConstraints");
95
96         PKIX_TEST_EXPECT_NO_ERROR(PKIX_PL_Cert_GetNameConstraints
97                 (goodCert, &goodNC, plContext));
98
99         testToStringHelper
100                 ((PKIX_PL_Object *)goodNC, expectedAscii, plContext);
101
102 cleanup:
103
104         PKIX_TEST_DECREF_AC(goodNC);
105         PKIX_TEST_DECREF_AC(goodCert);
106
107         PKIX_TEST_RETURN();
108 }
109
110 static
111 void printUsage(void) {
112         (void) printf
113                 ("\nUSAGE:\ttest_nameconstraints <test-purpose>"
114                  " <data-dir> <platform-prefix>\n\n");
115 }
116
117 /* Functional tests for CRL public functions */
118
119 int test_nameconstraints(int argc, char *argv[]) {
120         PKIX_UInt32 actualMinorVersion;
121         PKIX_UInt32 j = 0;
122         char *platformDir = NULL;
123         char *dataDir = NULL;
124         char *combinedDir = NULL;
125
126         /* Note XXX serialnumber and reasoncode need debug */
127
128         PKIX_TEST_STD_VARS();
129
130         startTests("NameConstraints");
131
132         PKIX_TEST_EXPECT_NO_ERROR(
133             PKIX_PL_NssContext_Create(0, PKIX_FALSE, NULL, &plContext));
134
135         if (argc < 3 + j) {
136                 printUsage();
137                 return (0);
138         }
139
140         dataDir = argv[2 + j];
141         platformDir = argv[3 + j];
142         combinedDir = catDirName(platformDir, dataDir, plContext);
143
144         testNameConstraints(combinedDir);
145
146 cleanup:
147
148         pkixTestErrorResult = PKIX_PL_Free(combinedDir, plContext);
149
150         PKIX_Shutdown(plContext);
151
152         PKIX_TEST_RETURN();
153
154         endTests("NameConstraints");
155
156         return (0);
157 }