| 1 |
/* |
| 2 |
* |
| 3 |
* Licensed to the Apache Software Foundation (ASF) under one |
| 4 |
* or more contributor license agreements. See the NOTICE file |
| 5 |
* distributed with this work for additional information |
| 6 |
* regarding copyright ownership. The ASF licenses this file |
| 7 |
* to you under the Apache License, Version 2.0 (the |
| 8 |
* "License"); you may not use this file except in compliance |
| 9 |
* with the License. You may obtain a copy of the License at |
| 10 |
* |
| 11 |
* http://www.apache.org/licenses/LICENSE-2.0 |
| 12 |
* |
| 13 |
* Unless required by applicable law or agreed to in writing, |
| 14 |
* software distributed under the License is distributed on an |
| 15 |
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 16 |
* KIND, either express or implied. See the License for the |
| 17 |
* specific language governing permissions and limitations |
| 18 |
* under the License. |
| 19 |
* |
| 20 |
*/ |
| 21 |
|
| 22 |
package org.globus.gsi.stores; |
| 23 |
|
| 24 |
import junit.framework.Assert; |
| 25 |
import junit.framework.TestCase; |
| 26 |
import org.apache.commons.logging.Log; |
| 27 |
import org.apache.commons.logging.LogFactory; |
| 28 |
import org.globus.gsi.SigningPolicy; |
| 29 |
|
| 30 |
import java.io.File; |
| 31 |
import java.io.FileInputStream; |
| 32 |
import java.security.cert.CertificateFactory; |
| 33 |
import java.security.cert.X509Certificate; |
| 34 |
|
| 35 |
/** |
| 36 |
* User: AmilaJ ([email protected]) |
| 37 |
* Date: 6/11/13 |
| 38 |
* Time: 10:26 AM |
| 39 |
*/ |
| 40 |
|
| 41 |
public class ResourceSigningPolicyStoreTest extends TestCase { |
| 42 |
|
| 43 |
private String caCertsLocation; |
| 44 |
|
| 45 |
private Log logger = LogFactory.getLog(getClass()); |
| 46 |
|
| 47 |
public void setUp() throws Exception { |
| 48 |
String projectDirectory = System.getProperty("projectDirectory"); |
| 49 |
|
| 50 |
if (projectDirectory == null) { |
| 51 |
projectDirectory = "src/test/resources/org/globus/gsi/stores/"; |
| 52 |
|
| 53 |
File f = new File(projectDirectory); |
| 54 |
|
| 55 |
if (!f.isDirectory()) { |
| 56 |
projectDirectory = "ssl-proxies/src/test/resources/org/globus/gsi/stores/"; |
| 57 |
} |
| 58 |
} else { |
| 59 |
projectDirectory = projectDirectory + "/src/test/resources/org/globus/gsi/stores/"; |
| 60 |
} |
| 61 |
|
| 62 |
File projectDir = new File(projectDirectory); |
| 63 |
|
| 64 |
caCertsLocation = projectDir.getAbsolutePath(); |
| 65 |
|
| 66 |
logger.info("CA cert location is set to " + caCertsLocation); |
| 67 |
|
| 68 |
} |
| 69 |
|
| 70 |
public void testGetSigningPolicyWithOutDNPrincipal() throws Exception { |
| 71 |
|
| 72 |
String sigPolPattern = caCertsLocation + "/*.signing_policy"; |
| 73 |
ResourceSigningPolicyStore sigPolStore |
| 74 |
= new ResourceSigningPolicyStore(new ResourceSigningPolicyStoreParameters(sigPolPattern)); |
| 75 |
|
| 76 |
String certPath1 = caCertsLocation + "/ffc3d59b.0"; |
| 77 |
|
| 78 |
X509Certificate crt1 = readCertificate(certPath1); |
| 79 |
Assert.assertNotNull("Unable to read certificate in " + certPath1 ,crt1); |
| 80 |
|
| 81 |
// According to https://github.com/jglobus/JGlobus/issues/102 the second attempt is failing. |
| 82 |
// Therefore we query twice. |
| 83 |
SigningPolicy signingPolicy = sigPolStore.getSigningPolicy(crt1.getSubjectX500Principal()); |
| 84 |
|
| 85 |
Assert.assertNotNull(signingPolicy); |
| 86 |
|
| 87 |
signingPolicy = sigPolStore.getSigningPolicy(crt1.getSubjectX500Principal()); |
| 88 |
|
| 89 |
Assert.assertNotNull(signingPolicy); |
| 90 |
|
| 91 |
} |
| 92 |
|
| 93 |
public void testGetSigningPolicyWithDNPrincipal() throws Exception { |
| 94 |
|
| 95 |
String sigPolPattern = caCertsLocation + "/*.signing_policy"; |
| 96 |
ResourceSigningPolicyStore sigPolStore |
| 97 |
= new ResourceSigningPolicyStore(new ResourceSigningPolicyStoreParameters(sigPolPattern)); |
| 98 |
|
| 99 |
String certPath1 = caCertsLocation + "/e5cc84c2.0"; |
| 100 |
|
| 101 |
X509Certificate crt1 = readCertificate(certPath1); |
| 102 |
Assert.assertNotNull("Unable to read certificate in " + certPath1 ,crt1); |
| 103 |
|
| 104 |
SigningPolicy signingPolicy = sigPolStore.getSigningPolicy(crt1.getSubjectX500Principal()); |
| 105 |
|
| 106 |
Assert.assertNotNull(signingPolicy); |
| 107 |
|
| 108 |
// According to https://github.com/jglobus/JGlobus/issues/102 the second attempt is failing. |
| 109 |
// Therefore we query twice. |
| 110 |
signingPolicy = sigPolStore.getSigningPolicy(crt1.getSubjectX500Principal()); |
| 111 |
|
| 112 |
Assert.assertNotNull(signingPolicy); |
| 113 |
|
| 114 |
} |
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
private X509Certificate readCertificate(String certPath) { |
| 119 |
try { |
| 120 |
FileInputStream fr = new FileInputStream(certPath); |
| 121 |
CertificateFactory cf = |
| 122 |
CertificateFactory.getInstance("X509"); |
| 123 |
X509Certificate crt = (X509Certificate) |
| 124 |
cf.generateCertificate(fr); |
| 125 |
logger.info("Read certificate:"); |
| 126 |
logger.info("\tCertificate for: " + |
| 127 |
crt.getSubjectDN()); |
| 128 |
logger.info("\tCertificate issued by: " + |
| 129 |
crt.getIssuerDN()); |
| 130 |
logger.info("\tCertificate is valid from " + |
| 131 |
crt.getNotBefore() + " to " + crt.getNotAfter()); |
| 132 |
logger.info("\tCertificate SN# " + |
| 133 |
crt.getSerialNumber()); |
| 134 |
logger.info("\tGenerated with " + |
| 135 |
crt.getSigAlgName()); |
| 136 |
|
| 137 |
return crt; |
| 138 |
} catch (Exception e) { |
| 139 |
e.printStackTrace(); |
| 140 |
} |
| 141 |
|
| 142 |
return null; |
| 143 |
} |
| 144 |
} |