Debian Codegraph @

ssl-proxies/src/test/java/org/globus/gsi/filestore/TestFileBasedTrustStore.java

raw
1 /*
2 * Copyright 1999-2010 University of Chicago
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in
5 * compliance with the License. You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software distributed under the License is
10 * distributed on an "AS IS" BASIS,WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
11 * express or implied.
12 *
13 * See the License for the specific language governing permissions and limitations under the License.
14 */
15 package org.globus.gsi.filestore;
16
17 import static org.hamcrest.CoreMatchers.instanceOf;
18 import static org.hamcrest.CoreMatchers.is;
19 import static org.hamcrest.CoreMatchers.not;
20 import static org.hamcrest.CoreMatchers.nullValue;
21 import static org.junit.Assert.assertThat;
22 import static org.junit.Assert.assertTrue;
23 import org.globus.gsi.stores.ResourceCertStoreParameters;
24 import org.globus.gsi.stores.ResourceSigningPolicyStore;
25 import org.globus.gsi.stores.ResourceSigningPolicyStoreParameters;
26 import org.globus.gsi.testutils.DirSetupUtil;
27 import org.globus.gsi.provider.GlobusProvider;
28 import org.globus.gsi.provider.SigningPolicyStore;
29 import org.globus.gsi.provider.SigningPolicyStoreParameters;
30 import java.io.File;
31 import java.io.FilenameFilter;
32 import java.security.Security;
33 import java.security.cert.CRL;
34 import java.security.cert.CertStore;
35 import java.security.cert.CertStoreParameters;
36 import java.security.cert.Certificate;
37 import java.security.cert.X509CRL;
38 import java.security.cert.X509CertSelector;
39 import java.security.cert.X509Certificate;
40 import java.util.Collection;
41
42 import javax.security.auth.x500.X500Principal;
43
44 import org.globus.gsi.SigningPolicy;
45 import org.junit.AfterClass;
46 import org.junit.BeforeClass;
47 import org.junit.Test;
48
49 /**
50 * FILL ME
51 *
52 * @author [email protected]
53 */
54 public class TestFileBasedTrustStore {
55
56 static DirSetupUtil dir;
57 static CertStoreParameters parameters;
58 static CertStoreParameters directoryParameters;
59 static CertStore certStore;
60 static CertStoreParameters crlParameters;
61 static SigningPolicyStoreParameters policyParameters;
62 static Collection<? extends Certificate> trustAnchors;
63
64 @BeforeClass
65 public static void setUp() throws Exception {
66
67 // JGLOBUS-103
68 dir = new DirSetupUtil(new String[] { "testTrustStore/1c3f2ca8.0", "testTrustStore/b38b4d8c.0",
69 "testTrustStore/d1b603c3.0", "testTrustStore/1c3f2ca8.r0", "testTrustStore/d1b603c3.r0",
70 "testTrustStore/1c3f2ca8.signing_policy", "testTrustStore/b38b4d8c.signing_policy",
71 "testTrustStore/d1b603c3.signing_policy" });
72 dir.createTempDirectory();
73 dir.copy();
74 parameters = new ResourceCertStoreParameters("classpath:/testTrustStore/*.0,classpath:/testTrustStore/*.9",
75 null);
76 crlParameters = new ResourceCertStoreParameters(null, "classpath:/testTrustStore/*.r*");
77 policyParameters = new ResourceSigningPolicyStoreParameters("classpath:/testTrustStore/*.signing_policy");
78 directoryParameters = new ResourceCertStoreParameters("file:" + dir.getTempDirectory().getAbsolutePath()
79 + "/*.0", null);
80 Security.addProvider(new GlobusProvider());
81 }
82
83 @Test
84 public void testEngineGetCertificates() throws Exception {
85
86
87 certStore = CertStore.getInstance("PEMFilebasedCertStore", parameters);
88
89 assert certStore != null;
90
91 trustAnchors = certStore.getCertificates(new X509CertSelector());
92
93 assert trustAnchors != null;
94
95 assertTrue(trustAnchors.size() > 0);
96
97 // assert caFiles != null;
98
99 assertThat(trustAnchors.size(), is(3));
100
101 for (Certificate trustAnchor : trustAnchors) {
102
103 assert (trustAnchor instanceof X509Certificate);
104
105 }
106
107 // JGLOBUS-103
108
109 }
110
111 @Test
112 public void testEngineGetCertificatesDirectory() throws Exception {
113 File tempDir = this.dir.getTempDirectory();
114 // number of CA files
115 // String[] caFiles = tempDir.list(new TrustAnchorFilter());
116 this.certStore = CertStore.getInstance("PEMFilebasedCertStore", directoryParameters);
117
118 assert certStore != null;
119
120 this.trustAnchors = certStore.getCertificates(new X509CertSelector());
121
122 assert trustAnchors != null;
123
124 assertTrue(trustAnchors.size() > 0);
125
126 // assert caFiles != null;
127
128 assertTrue(trustAnchors.size() == 3);
129
130 for (Certificate trustAnchor : trustAnchors) {
131
132 assertThat(trustAnchor, instanceOf(X509Certificate.class));
133
134 }
135
136 }
137
138 public static class CrlFilter implements FilenameFilter {
139
140 public boolean accept(File dir, String file) {
141
142 if (file == null) {
143 throw new IllegalArgumentException();
144 }
145
146 int length = file.length();
147 return length > 3 && file.charAt(length - 3) == '.' && file.charAt(length - 2) == 'r'
148 && file.charAt(length - 1) >= '0' && file.charAt(length - 1) <= '9';
149
150 }
151 }
152
153 @Test
154 public void testEngineGetCRLs() throws Exception {
155
156 File tempDir = dir.getTempDirectory();
157 // number of CRL files
158 String[] crlFiles = tempDir.list(new CrlFilter());
159
160 // Get comparison parameters
161 certStore = CertStore.getInstance("PEMFilebasedCertStore", crlParameters);
162
163 assert certStore != null;
164
165 Collection<? extends CRL> crls = certStore.getCRLs(null);
166
167 assertThat(crls, not(nullValue()));
168
169 assertTrue(crls.size() > 0);
170
171 assert crlFiles != null;
172
173 assertThat(crls.size(), is(crlFiles.length));
174
175 for (CRL crl : crls) {
176
177 assertThat(crl, instanceOf(X509CRL.class));
178
179 }
180
181 // JGLOBUS-103
182 }
183
184 @Test
185 public void testGetSigningPolicies() throws Exception {
186
187 SigningPolicyStore store = new ResourceSigningPolicyStore(policyParameters);
188
189 SigningPolicy policy = store.getSigningPolicy(null);
190
191 assert (policy == null);
192
193 policy = store.getSigningPolicy(new X500Principal("C=US, CN=Foo"));
194
195 assert (policy == null);
196
197 for (Certificate trustAnchor : trustAnchors) {
198
199 X509Certificate certificate = (X509Certificate) trustAnchor;
200
201 X500Principal principal = certificate.getIssuerX500Principal();
202
203 policy = store.getSigningPolicy(principal);
204
205 assert (policy != null);
206
207 assert (policy.getAllowedDNs() != null);
208 }
209
210 // JGLOBUS-103
211 }
212 public static boolean deleteDir(File dir) {
213 if (dir.isDirectory()) {
214 String[] dirContent = dir.list();
215 for (int i=0; i<dirContent.length; i++){
216 boolean success = deleteDir(new File(dir, dirContent[i]));
217 if (!success) {
218 return false;
219 }
220 }
221 } // The directory is now empty so delete it
222 return dir.delete();
223 }
224 @AfterClass
225 public static void tearDown() throws Exception {
226 //dir.delete();
227 deleteDir(dir.getTempDirectory());
228 }
229 }