You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@santuario.apache.org by co...@apache.org on 2011/01/19 13:33:07 UTC
svn commit: r1060790 - in /santuario/xml-security-java/trunk: ./
src/test/java/org/apache/xml/security/test/signature/ src_unitTests/
Author: coheigea
Date: Wed Jan 19 12:33:07 2011
New Revision: 1060790
URL: http://svn.apache.org/viewvc?rev=1060790&view=rev
Log:
[SANTUARIO-257] - More work on the tests.
- Also removed some redundant files.
Removed:
santuario/xml-security-java/trunk/Readme.html
santuario/xml-security-java/trunk/ReleaseProcess.txt
santuario/xml-security-java/trunk/TODO
santuario/xml-security-java/trunk/provider.properties
santuario/xml-security-java/trunk/proxy.properties
santuario/xml-security-java/trunk/src_unitTests/
Modified:
santuario/xml-security-java/trunk/build.xml
santuario/xml-security-java/trunk/src/test/java/org/apache/xml/security/test/signature/ProcessingInstructionTest.java
santuario/xml-security-java/trunk/src/test/java/org/apache/xml/security/test/signature/SignatureTest.java
santuario/xml-security-java/trunk/src/test/java/org/apache/xml/security/test/signature/UnknownAlgoSignatureTest.java
santuario/xml-security-java/trunk/src/test/java/org/apache/xml/security/test/signature/X509DataTest.java
santuario/xml-security-java/trunk/src/test/java/org/apache/xml/security/test/signature/XMLSignatureInputTest.java
santuario/xml-security-java/trunk/src/test/java/org/apache/xml/security/test/signature/XmlSecTest.java
Modified: santuario/xml-security-java/trunk/build.xml
URL: http://svn.apache.org/viewvc/santuario/xml-security-java/trunk/build.xml?rev=1060790&r1=1060789&r2=1060790&view=diff
==============================================================================
--- santuario/xml-security-java/trunk/build.xml (original)
+++ santuario/xml-security-java/trunk/build.xml Wed Jan 19 12:33:07 2011
@@ -1,12 +1,5 @@
<?xml version="1.0"?>
<project name="Apache Santuario" default="help" basedir=".">
- <!-- P R O P E R T Y F I L E S -->
- <property file="build.properties" />
- <property file="provider.properties" />
- <property file="proxy.properties" />
-
-
-
<!-- G L O B A L P R O P E R T I E S -->
<!-- Product name and version properties -->
@@ -154,8 +147,6 @@
<include name="NOTICE"/>
<include name="Readme.html"/>
<include name="TODO"/>
- <include name="provider.properties"/>
- <include name="proxy.properties"/>
</patternset>
<patternset id="id.patternset.dataFiles">
@@ -829,8 +820,6 @@
<echo message="test - performs a series of tests" />
<echo message=" (see ant help.test for more information)" />
<echo message="help - displays this help message (default)" />
- <echo message="config - optional configuration information" />
- <echo message=" (see ant help.config for more information)" />
<echo message=""/>
<echo message="See also help.samples for help on building samples"/>
</target >
@@ -873,12 +862,6 @@
<echo message="encryptAndDecrypt - run encryption sample" />
</target>
- <target depends="init" name="help.config" >
- <echo message="1. to configure the provider setings," />
- <echo message=" see the contents of provider.properties" />
- <echo message="2. to configure the proxy setings," />
- <echo message=" see the contents of proxy.properties" />
- </target >
</project>
<!-- Keep this comment at the end of the file
Local variables:
Modified: santuario/xml-security-java/trunk/src/test/java/org/apache/xml/security/test/signature/ProcessingInstructionTest.java
URL: http://svn.apache.org/viewvc/santuario/xml-security-java/trunk/src/test/java/org/apache/xml/security/test/signature/ProcessingInstructionTest.java?rev=1060790&r1=1060789&r2=1060790&view=diff
==============================================================================
--- santuario/xml-security-java/trunk/src/test/java/org/apache/xml/security/test/signature/ProcessingInstructionTest.java (original)
+++ santuario/xml-security-java/trunk/src/test/java/org/apache/xml/security/test/signature/ProcessingInstructionTest.java Wed Jan 19 12:33:07 2011
@@ -30,10 +30,6 @@ import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathFactory;
-import junit.framework.Test;
-import junit.framework.TestCase;
-import junit.framework.TestSuite;
-
import org.apache.xml.security.signature.XMLSignature;
import org.apache.xml.security.signature.XMLSignatureInput;
import org.w3c.dom.Attr;
@@ -46,7 +42,7 @@ import org.apache.xml.utils.URI;
/**
* A test-case for Bugzilla bug 45744 - "XPath transform and xml-stylesheet".
*/
-public class ProcessingInstructionTest extends TestCase {
+public class ProcessingInstructionTest extends org.junit.Assert {
static {
org.apache.xml.security.Init.init();
@@ -54,12 +50,7 @@ public class ProcessingInstructionTest e
private static String dir;
- public static Test suite() {
- return new TestSuite(ProcessingInstructionTest.class);
- }
-
- public ProcessingInstructionTest(String name) {
- super(name);
+ public ProcessingInstructionTest() {
String base = System.getProperty("basedir") == null
? "./" : System.getProperty("basedir");
String fs = System.getProperty("file.separator");
@@ -67,6 +58,7 @@ public class ProcessingInstructionTest e
fs + "security" + fs + "testcases" + fs;
}
+ @org.junit.Test
public void testProcessingInstruction() throws Exception {
String signatureFileName = dir + "upp_sign.xml";
DocumentBuilderFactory dbf =
@@ -81,8 +73,9 @@ public class ProcessingInstructionTest e
xpath.setNamespaceContext(new NamespaceContext() {
public String getNamespaceURI(String arg0) {
- if (!arg0.equals("ds"))
+ if (!arg0.equals("ds")) {
throw new RuntimeException();
+ }
return "http://www.w3.org/2000/09/xmldsig#";
}
@@ -90,19 +83,18 @@ public class ProcessingInstructionTest e
return "ds";
}
- public Iterator getPrefixes(String arg0) {
- List al = new ArrayList();
+ public Iterator<String> getPrefixes(String arg0) {
+ List<String> al = new ArrayList<String>();
al.add("ds");
return al.iterator();
}
-
});
String expression = "//ds:Signature[1]";
Element sigElement =
(Element) xpath.evaluate(expression, doc, XPathConstants.NODE);
- String baseUri = new File(".").toURL().toString();
+ String baseUri = new File(".").toURI().toURL().toString();
XMLSignature signature = new XMLSignature(sigElement, baseUri);
signature.addResourceResolver(FileResolver.getInstance());
X509Certificate cert = signature.getKeyInfo().getX509Certificate();
@@ -111,7 +103,6 @@ public class ProcessingInstructionTest e
}
}
-
/**
* This class resolves "out.xml" on the local filesystem.
*/
@@ -131,7 +122,6 @@ public class ProcessingInstructionTest e
public XMLSignatureInput engineResolve(Attr uri, String baseURI)
throws ResourceResolverException {
-
try {
URI uriNew = new URI(uri.getNodeValue(), baseURI);
Modified: santuario/xml-security-java/trunk/src/test/java/org/apache/xml/security/test/signature/SignatureTest.java
URL: http://svn.apache.org/viewvc/santuario/xml-security-java/trunk/src/test/java/org/apache/xml/security/test/signature/SignatureTest.java?rev=1060790&r1=1060789&r2=1060790&view=diff
==============================================================================
--- santuario/xml-security-java/trunk/src/test/java/org/apache/xml/security/test/signature/SignatureTest.java (original)
+++ santuario/xml-security-java/trunk/src/test/java/org/apache/xml/security/test/signature/SignatureTest.java Wed Jan 19 12:33:07 2011
@@ -35,119 +35,30 @@ import org.apache.xml.security.utils.res
import org.w3c.dom.Document;
import org.w3c.dom.Element;
-import junit.framework.Test;
-import junit.framework.TestCase;
-import junit.framework.TestSuite;
-
-public class SignatureTest extends TestCase {
+public class SignatureTest extends org.junit.Assert {
public final static String DS_NS = "http://www.w3.org/2000/09/xmldsig#";
private static final String BASEDIR =
System.getProperty("basedir") == null ? "./": System.getProperty("basedir");
public final static String KEYSTORE_DIRECTORY = BASEDIR + "/data/";
public final static String KEYSTORE_PASSWORD_STRING = "changeit";
- public final static char[] KEYSTORE_PASSWORD = KEYSTORE_PASSWORD_STRING
- .toCharArray();
-
- public SignatureTest(String test) {
- super(test);
- }
-
- public static Test suite() {
- return new TestSuite(SignatureTest.class);
- }
-
- /**
- * Loads the 'localhost' keystore from the test keystore.
- *
- * @return test keystore.
- * @throws Exception
- */
- public KeyStore getKeyStore() throws Exception {
- KeyStore ks = KeyStore.getInstance("JKS");
- InputStream ksis = new FileInputStream(KEYSTORE_DIRECTORY + "test.jks");
- ks.load(ksis, KEYSTORE_PASSWORD);
- ksis.close();
- return ks;
- }
-
- public PublicKey getPublicKey() throws Exception {
- KeyStore keyStore = getKeyStore();
- Enumeration aliases = keyStore.aliases();
- while (aliases.hasMoreElements()) {
- String alias = (String) aliases.nextElement();
- if (keyStore.isKeyEntry(alias)) {
- return keyStore.getCertificate(alias).getPublicKey();
- }
- }
- return null;
- }
-
- public PrivateKey getPrivateKey() throws Exception {
- KeyStore keyStore = getKeyStore();
- Enumeration aliases = keyStore.aliases();
- while (aliases.hasMoreElements()) {
- String alias = (String) aliases.nextElement();
- if (keyStore.isKeyEntry(alias)) {
- return (PrivateKey) keyStore.getKey(alias, KEYSTORE_PASSWORD);
- }
- }
- return null;
- }
-
- public Document getOriginalDocument() throws Throwable {
- DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
- dbf.setNamespaceAware(true);
- DocumentBuilder db = dbf.newDocumentBuilder();
- Document doc = db.newDocument();
-
- Element rootElement = doc.createElementNS("http://ns.example.org/",
- "root");
- rootElement.appendChild(doc.createTextNode("Hello World!"));
- doc.appendChild(rootElement);
-
- return doc;
- }
+ public final static char[] KEYSTORE_PASSWORD = KEYSTORE_PASSWORD_STRING.toCharArray();
- public XMLSignature signDocument(Document doc) throws Throwable {
- XMLSignature sig = new XMLSignature(doc, "",
- XMLSignature.ALGO_ID_SIGNATURE_DSA);
- Element root = doc.getDocumentElement();
- root.appendChild(sig.getElement());
-
- sig.getSignedInfo().addResourceResolver(new ResolverXPointer());
-
- Transforms transforms = new Transforms(doc);
- transforms.addTransform(Transforms.TRANSFORM_ENVELOPED_SIGNATURE);
- transforms.addTransform(Transforms.TRANSFORM_C14N_WITH_COMMENTS);
- sig.addDocument("", transforms, Constants.ALGO_ID_DIGEST_SHA1);
-
- sig.addKeyInfo(getPublicKey());
- sig.sign(getPrivateKey());
-
- // displayDocument(doc);
-
- return sig;
- }
-
- public void setUp() throws Exception {
+ public SignatureTest() throws Exception {
Init.init();
Constants.setSignatureSpecNSprefix("ds");
}
- public void displayOriginalDocument() throws Throwable {
- // displayDocument(getOriginalDocument());
- }
-
+ @org.junit.Test
public void testSigning() throws Throwable {
signDocument(getOriginalDocument());
}
+ @org.junit.Test
public void testSigningVerifyingFromRebuildSignature() throws Throwable {
Document doc = getOriginalDocument();
signDocument(doc);
- Element signatureElem = (Element) doc.getElementsByTagNameNS(DS_NS,
- "Signature").item(0);
+ Element signatureElem = (Element) doc.getElementsByTagNameNS(DS_NS, "Signature").item(0);
XMLSignature signature = new XMLSignature(signatureElem, "");
PublicKey pubKey = getPublicKey();
@@ -155,6 +66,7 @@ public class SignatureTest extends TestC
assertTrue(signature.checkSignatureValue(pubKey));
}
+ @org.junit.Test
public void testSigningVerifyingFromExistingSignature() throws Throwable {
Document doc = getOriginalDocument();
XMLSignature signature = signDocument(doc);
@@ -163,6 +75,7 @@ public class SignatureTest extends TestC
assertTrue(signature.checkSignatureValue(pubKey));
}
+ @org.junit.Test
public void testSigningVerifyingFromExistingSignatureSameThread()
throws Throwable {
Document doc = getOriginalDocument();
@@ -178,6 +91,7 @@ public class SignatureTest extends TestC
assertTrue(r.result);
}
+ @org.junit.Test
public void testSigningVerifyingFromExistingSignatureSeparateThread()
throws Throwable {
Document doc = getOriginalDocument();
@@ -213,4 +127,73 @@ public class SignatureTest extends TestC
}
}
}
+
+ /**
+ * Loads the 'localhost' keystore from the test keystore.
+ *
+ * @return test keystore.
+ * @throws Exception
+ */
+ private KeyStore getKeyStore() throws Exception {
+ KeyStore ks = KeyStore.getInstance("JKS");
+ InputStream ksis = new FileInputStream(KEYSTORE_DIRECTORY + "test.jks");
+ ks.load(ksis, KEYSTORE_PASSWORD);
+ ksis.close();
+ return ks;
+ }
+
+ private PublicKey getPublicKey() throws Exception {
+ KeyStore keyStore = getKeyStore();
+ Enumeration<String> aliases = keyStore.aliases();
+ while (aliases.hasMoreElements()) {
+ String alias = aliases.nextElement();
+ if (keyStore.isKeyEntry(alias)) {
+ return keyStore.getCertificate(alias).getPublicKey();
+ }
+ }
+ return null;
+ }
+
+ private PrivateKey getPrivateKey() throws Exception {
+ KeyStore keyStore = getKeyStore();
+ Enumeration<String> aliases = keyStore.aliases();
+ while (aliases.hasMoreElements()) {
+ String alias = aliases.nextElement();
+ if (keyStore.isKeyEntry(alias)) {
+ return (PrivateKey) keyStore.getKey(alias, KEYSTORE_PASSWORD);
+ }
+ }
+ return null;
+ }
+
+ private Document getOriginalDocument() throws Throwable {
+ DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
+ dbf.setNamespaceAware(true);
+ DocumentBuilder db = dbf.newDocumentBuilder();
+ Document doc = db.newDocument();
+
+ Element rootElement = doc.createElementNS("http://ns.example.org/", "root");
+ rootElement.appendChild(doc.createTextNode("Hello World!"));
+ doc.appendChild(rootElement);
+
+ return doc;
+ }
+
+ private XMLSignature signDocument(Document doc) throws Throwable {
+ XMLSignature sig = new XMLSignature(doc, "", XMLSignature.ALGO_ID_SIGNATURE_DSA);
+ Element root = doc.getDocumentElement();
+ root.appendChild(sig.getElement());
+
+ sig.getSignedInfo().addResourceResolver(new ResolverXPointer());
+
+ Transforms transforms = new Transforms(doc);
+ transforms.addTransform(Transforms.TRANSFORM_ENVELOPED_SIGNATURE);
+ transforms.addTransform(Transforms.TRANSFORM_C14N_WITH_COMMENTS);
+ sig.addDocument("", transforms, Constants.ALGO_ID_DIGEST_SHA1);
+
+ sig.addKeyInfo(getPublicKey());
+ sig.sign(getPrivateKey());
+
+ return sig;
+ }
}
Modified: santuario/xml-security-java/trunk/src/test/java/org/apache/xml/security/test/signature/UnknownAlgoSignatureTest.java
URL: http://svn.apache.org/viewvc/santuario/xml-security-java/trunk/src/test/java/org/apache/xml/security/test/signature/UnknownAlgoSignatureTest.java?rev=1060790&r1=1060789&r2=1060790&view=diff
==============================================================================
--- santuario/xml-security-java/trunk/src/test/java/org/apache/xml/security/test/signature/UnknownAlgoSignatureTest.java (original)
+++ santuario/xml-security-java/trunk/src/test/java/org/apache/xml/security/test/signature/UnknownAlgoSignatureTest.java Wed Jan 19 12:33:07 2011
@@ -31,11 +31,6 @@ import javax.xml.parsers.DocumentBuilder
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.TransformerException;
-import junit.framework.Test;
-import junit.framework.TestCase;
-import junit.framework.TestSuite;
-import junit.textui.TestRunner;
-
import org.apache.xml.security.Init;
import org.apache.xml.security.c14n.InvalidCanonicalizerException;
import org.apache.xml.security.exceptions.XMLSecurityException;
@@ -55,18 +50,20 @@ import org.xml.sax.SAXException;
* org.apache.xml.security.samples.signature.CreateEnvelopingSignature</code>
* </p>
*/
-public class UnknownAlgoSignatureTest extends TestCase {
+public class UnknownAlgoSignatureTest extends org.junit.Assert {
private static final String BASEDIR = System.getProperty("basedir");
private static final String SEP = System.getProperty("file.separator");
protected static final String KEYSTORE_TYPE = "JKS";
- protected static final String KEYSTORE_FILE = "data/org/apache/xml/security/samples/input/keystore.jks";
+ protected static final String KEYSTORE_FILE =
+ "data/org/apache/xml/security/samples/input/keystore.jks";
protected static final String CERT_ALIAS = "test";
- protected static final String SIGNATURE_SOURCE_PATH = "data/org/apache/xml/security/temp/signature";
+ protected static final String SIGNATURE_SOURCE_PATH =
+ "data/org/apache/xml/security/temp/signature";
protected PublicKey publicKey;
@@ -74,22 +71,8 @@ public class UnknownAlgoSignatureTest ex
Init.init();
}
- public static Test suite() {
- return new TestSuite(UnknownAlgoSignatureTest.class);
- }
-
- public UnknownAlgoSignatureTest(String name) {
- super(name);
- }
-
- public static void main(String[] args) {
- String[] testCaseName = { "-noloading",
- UnknownAlgoSignatureTest.class.getName() };
- TestRunner.main(testCaseName);
- }
-
- public void setUp() throws KeyStoreException, NoSuchAlgorithmException,
- CertificateException, IOException {
+ public UnknownAlgoSignatureTest() throws KeyStoreException, NoSuchAlgorithmException,
+ CertificateException, IOException {
FileInputStream fis = null;
if (BASEDIR != null && !"".equals(BASEDIR)) {
fis = new FileInputStream(BASEDIR + SEP + KEYSTORE_FILE);
@@ -98,20 +81,19 @@ public class UnknownAlgoSignatureTest ex
}
KeyStore keyStore = KeyStore.getInstance(KEYSTORE_TYPE);
keyStore.load(fis, null);
- X509Certificate cert = (X509Certificate) keyStore
- .getCertificate(CERT_ALIAS);
+ X509Certificate cert = (X509Certificate) keyStore.getCertificate(CERT_ALIAS);
publicKey = cert.getPublicKey();
}
+ @org.junit.Test
public void testGood() throws ParserConfigurationException, SAXException,
- IOException, TransformerException, XMLSignatureException,
- XMLSecurityException {
+ IOException, TransformerException, XMLSignatureException, XMLSecurityException {
assertTrue(checkSignature("signature-good.xml"));
}
+ @org.junit.Test
public void testBadC14NAlgo() throws ParserConfigurationException,
- SAXException, IOException, TransformerException,
- XMLSecurityException {
+ SAXException, IOException, TransformerException, XMLSecurityException {
try {
assertTrue(checkSignature("signature-bad-c14n-algo.xml"));
fail("Exception not caught");
@@ -120,9 +102,9 @@ public class UnknownAlgoSignatureTest ex
}
}
+ @org.junit.Test
public void testBadSigAlgo() throws ParserConfigurationException,
- SAXException, IOException, TransformerException,
- XMLSecurityException {
+ SAXException, IOException, TransformerException, XMLSecurityException {
try {
assertTrue(checkSignature("signature-bad-sig-algo.xml"));
fail("Exception not caught");
@@ -131,9 +113,9 @@ public class UnknownAlgoSignatureTest ex
}
}
+ @org.junit.Test
public void testBadTransformAlgo() throws ParserConfigurationException,
- SAXException, IOException, TransformerException,
- XMLSecurityException {
+ SAXException, IOException, TransformerException, XMLSecurityException {
try {
assertTrue(checkReferences("signature-bad-transform-algo.xml"));
fail("Exception not caught");
@@ -143,22 +125,22 @@ public class UnknownAlgoSignatureTest ex
}
protected boolean checkSignature(String fileName)
- throws ParserConfigurationException, SAXException, IOException,
- TransformerException, XMLSecurityException {
+ throws ParserConfigurationException, SAXException, IOException,
+ TransformerException, XMLSecurityException {
XMLSignature signature = unmarshalXMLSignature(fileName);
return signature.checkSignatureValue(publicKey);
}
protected boolean checkReferences(String fileName)
- throws ParserConfigurationException, SAXException, IOException,
- TransformerException, XMLSecurityException {
+ throws ParserConfigurationException, SAXException, IOException,
+ TransformerException, XMLSecurityException {
XMLSignature signature = unmarshalXMLSignature(fileName);
return signature.getSignedInfo().verify(false);
}
private XMLSignature unmarshalXMLSignature(String fileName)
- throws ParserConfigurationException, SAXException, IOException,
- TransformerException, XMLSecurityException {
+ throws ParserConfigurationException, SAXException, IOException,
+ TransformerException, XMLSecurityException {
File file = null;
if (BASEDIR != null && !"".equals(BASEDIR)) {
file = new File(BASEDIR + SEP + SIGNATURE_SOURCE_PATH, fileName);
@@ -166,21 +148,19 @@ public class UnknownAlgoSignatureTest ex
file = new File(SIGNATURE_SOURCE_PATH, fileName);
}
Document doc = getDocument(file);
- Element nscontext = TestUtils.createDSctx(doc, "ds",
- Constants.SignatureSpecNS);
- Element signatureEl = (Element) XPathAPI.selectSingleNode(doc,
- "//ds:Signature[1]", nscontext);
- XMLSignature signature = new XMLSignature(signatureEl, file.toURL()
- .toString());
- return signature;
+ Element nscontext = TestUtils.createDSctx(doc, "ds", Constants.SignatureSpecNS);
+ Element signatureEl =
+ (Element) XPathAPI.selectSingleNode(doc, "//ds:Signature[1]", nscontext);
+ return new XMLSignature(signatureEl, file.toURI().toURL().toString());
}
public static Document getDocument(File file)
- throws ParserConfigurationException, SAXException, IOException {
+ throws ParserConfigurationException, SAXException, IOException {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
dbf.setNamespaceAware(true);
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.parse(new FileInputStream(file));
return doc;
}
+
}
Modified: santuario/xml-security-java/trunk/src/test/java/org/apache/xml/security/test/signature/X509DataTest.java
URL: http://svn.apache.org/viewvc/santuario/xml-security-java/trunk/src/test/java/org/apache/xml/security/test/signature/X509DataTest.java?rev=1060790&r1=1060789&r2=1060790&view=diff
==============================================================================
--- santuario/xml-security-java/trunk/src/test/java/org/apache/xml/security/test/signature/X509DataTest.java (original)
+++ santuario/xml-security-java/trunk/src/test/java/org/apache/xml/security/test/signature/X509DataTest.java Wed Jan 19 12:33:07 2011
@@ -23,8 +23,6 @@ import java.security.KeyStore;
import java.security.PrivateKey;
import java.security.cert.X509Certificate;
-import junit.framework.TestCase;
-
import org.apache.xml.security.Init;
import org.apache.xml.security.signature.XMLSignature;
import org.apache.xml.security.utils.RFC2253Parser;
@@ -32,86 +30,95 @@ import org.apache.xml.security.utils.XML
import org.w3c.dom.Document;
import org.w3c.dom.Element;
-public class X509DataTest extends TestCase {
+public class X509DataTest extends org.junit.Assert {
private static final String BASEDIR = System.getProperty("basedir");
- KeyStore ks=null;
- KeyStore getKeyStore() throws Exception {
- if (ks!=null)
- return ks;
- String keystoreType = "JKS";
- String keystoreFile = "data/org/apache/xml/security/samples/input/keystore.jks";
- String keystorePass = "xmlsecurity";
- ks = KeyStore.getInstance(keystoreType);
- FileInputStream fis = null;
- if (BASEDIR != null && !"".equals(BASEDIR)) {
- fis = new FileInputStream(BASEDIR + "/" + keystoreFile);
- } else {
- fis = new FileInputStream(keystoreFile);
- }
- //load the keystore
- ks.load(fis, keystorePass.toCharArray());
+ KeyStore ks = null;
+
+ @org.junit.Test
+ public void testAddX509SubjectName() throws Exception {
+ Init.init();
+ javax.xml.parsers.DocumentBuilderFactory dbf =
+ javax.xml.parsers.DocumentBuilderFactory.newInstance();
+
+ //XML Signature needs to be namespace aware
+ dbf.setNamespaceAware(true);
+
+ javax.xml.parsers.DocumentBuilder db = dbf.newDocumentBuilder();
+ org.w3c.dom.Document doc = db.newDocument();
+ XMLSignature sig = new XMLSignature(doc, "", XMLSignature.ALGO_ID_SIGNATURE_DSA);
+
+ doc.appendChild(sig.getElement());
+ sig.addDocument("");
+
+ //Add in the KeyInfo for the certificate that we used the private key of
+ X509Certificate cert =getCertificate();
+ sig.addKeyInfo(cert);
+ sig.addKeyInfo(cert.getPublicKey());
+
+ // Add these three lines
+ org.apache.xml.security.keys.KeyInfo ki = sig.getKeyInfo();
+ ki.itemX509Data(0).addSubjectName(cert.getSubjectDN().toString());
+ ki.itemX509Data(0).addIssuerSerial(cert.getIssuerDN().toString(), cert.getSerialNumber());
+
+ sig.sign(getPrivateKey());
+ ByteArrayOutputStream os = new ByteArrayOutputStream();
+ XMLUtils.outputDOM(doc, os);
+ XMLSignature newSig = getSignature(os.toByteArray());
+
+ assertNotNull(newSig.getKeyInfo().itemX509Data(0));
+ assertEquals(RFC2253Parser.normalize(cert.getSubjectDN().toString()),
+ newSig.getKeyInfo().itemX509Data(0).itemSubjectName(0).getSubjectName());
+ assertEquals(RFC2253Parser.normalize(cert.getIssuerDN().toString()),
+ newSig.getKeyInfo().itemX509Data(0).itemIssuerSerial(0).getIssuerName());
+ }
+
+ private XMLSignature getSignature(byte[] s) throws Exception {
+ javax.xml.parsers.DocumentBuilderFactory dbf =
+ javax.xml.parsers.DocumentBuilderFactory.newInstance();
+
+ //XML Signature needs to be namespace aware
+ dbf.setNamespaceAware(true);
+
+ javax.xml.parsers.DocumentBuilder db = dbf.newDocumentBuilder();
+ Document doc = db.parse(new ByteArrayInputStream(s));
+ Element el = (Element)doc.getFirstChild();
+ return new XMLSignature(el, "");
+ }
+
+ private KeyStore getKeyStore() throws Exception {
+ if (ks != null) {
return ks;
}
- X509Certificate getCertificate() throws Exception {
- String certificateAlias = "test";
- X509Certificate cert =
- (X509Certificate) getKeyStore().getCertificate(certificateAlias);
- return cert;
- }
- PrivateKey getPrivateKey() throws Exception {
- String privateKeyAlias = "test";
- String privateKeyPass = "xmlsecurity";
- PrivateKey privateKey = (PrivateKey) getKeyStore().getKey(privateKeyAlias,
- privateKeyPass.toCharArray());
- return privateKey;
- }
- public XMLSignature getSignature(byte[] s) throws Exception {
- javax.xml.parsers.DocumentBuilderFactory dbf =
- javax.xml.parsers.DocumentBuilderFactory.newInstance();
-
- //XML Signature needs to be namespace aware
- dbf.setNamespaceAware(true);
-
- javax.xml.parsers.DocumentBuilder db = dbf.newDocumentBuilder();
- Document doc=db.parse(new ByteArrayInputStream(s));
- Element el=(Element)doc.getFirstChild();
- return new XMLSignature(el,"");
- }
- public void testAddX509SubjectName() throws Exception {
- Init.init();
- javax.xml.parsers.DocumentBuilderFactory dbf =
- javax.xml.parsers.DocumentBuilderFactory.newInstance();
-
- //XML Signature needs to be namespace aware
- dbf.setNamespaceAware(true);
-
- javax.xml.parsers.DocumentBuilder db = dbf.newDocumentBuilder();
- org.w3c.dom.Document doc = db.newDocument();
- XMLSignature sig = new XMLSignature(doc, "",
- XMLSignature.ALGO_ID_SIGNATURE_DSA);
- doc.appendChild(sig.getElement());
- sig.addDocument("");
- //Add in the KeyInfo for the certificate that we used the private key of
- X509Certificate cert =getCertificate();
- sig.addKeyInfo(cert);
- sig.addKeyInfo(cert.getPublicKey());
- // Add these three lines
- org.apache.xml.security.keys.KeyInfo ki = sig.getKeyInfo();
- ki.itemX509Data(0).addSubjectName(cert.getSubjectDN().toString());
- ki.itemX509Data(0).addIssuerSerial(cert.getIssuerDN().toString(),
- cert.getSerialNumber());
- sig.sign(getPrivateKey());
- ByteArrayOutputStream os = new ByteArrayOutputStream();
- XMLUtils.outputDOM(doc, os);
- XMLSignature newSig=getSignature(os.toByteArray());
- assertNotNull(newSig.getKeyInfo().itemX509Data(0));
- assertEquals(RFC2253Parser.normalize(cert.getSubjectDN().toString()),
- newSig.getKeyInfo().itemX509Data(0).itemSubjectName(0).getSubjectName());
- assertEquals(RFC2253Parser.normalize(cert.getIssuerDN().toString()),
- newSig.getKeyInfo().itemX509Data(0).itemIssuerSerial(0).getIssuerName());
-
+ String keystoreType = "JKS";
+ String keystoreFile = "data/org/apache/xml/security/samples/input/keystore.jks";
+ String keystorePass = "xmlsecurity";
+ ks = KeyStore.getInstance(keystoreType);
+ FileInputStream fis = null;
+ if (BASEDIR != null && !"".equals(BASEDIR)) {
+ fis = new FileInputStream(BASEDIR + "/" + keystoreFile);
+ } else {
+ fis = new FileInputStream(keystoreFile);
}
-
+ //load the keystore
+ ks.load(fis, keystorePass.toCharArray());
+ return ks;
+ }
+
+ private X509Certificate getCertificate() throws Exception {
+ String certificateAlias = "test";
+ X509Certificate cert =
+ (X509Certificate) getKeyStore().getCertificate(certificateAlias);
+ return cert;
+ }
+
+ private PrivateKey getPrivateKey() throws Exception {
+ String privateKeyAlias = "test";
+ String privateKeyPass = "xmlsecurity";
+ PrivateKey privateKey =
+ (PrivateKey) getKeyStore().getKey(privateKeyAlias, privateKeyPass.toCharArray());
+ return privateKey;
+ }
+
}
Modified: santuario/xml-security-java/trunk/src/test/java/org/apache/xml/security/test/signature/XMLSignatureInputTest.java
URL: http://svn.apache.org/viewvc/santuario/xml-security-java/trunk/src/test/java/org/apache/xml/security/test/signature/XMLSignatureInputTest.java?rev=1060790&r1=1060789&r2=1060790&view=diff
==============================================================================
--- santuario/xml-security-java/trunk/src/test/java/org/apache/xml/security/test/signature/XMLSignatureInputTest.java (original)
+++ santuario/xml-security-java/trunk/src/test/java/org/apache/xml/security/test/signature/XMLSignatureInputTest.java Wed Jan 19 12:33:07 2011
@@ -17,198 +17,121 @@
*/
package org.apache.xml.security.test.signature;
-
-
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
-import junit.framework.Test;
-import junit.framework.TestCase;
-import junit.framework.TestSuite;
-
import org.apache.xml.security.c14n.CanonicalizationException;
import org.apache.xml.security.c14n.InvalidCanonicalizerException;
import org.apache.xml.security.signature.XMLSignatureInput;
-
/**
* Unit test for {@link org.apache.xml.security.signature.XMLSignatureInput}
*
* @author Christian Geuer-Pollmann
* @see <A HREF="http://nagoya.apache.org/bugzilla/show_bug.cgi?id=4336">Bug 4336</A>
*/
-public class XMLSignatureInputTest extends TestCase {
+public class XMLSignatureInputTest extends org.junit.Assert {
- /** {@link org.apache.commons.logging} logging facility */
+ /** {@link org.apache.commons.logging} logging facility */
static org.apache.commons.logging.Log log =
org.apache.commons.logging.LogFactory.getLog(XMLSignatureInputTest.class.getName());
- /**
- * Method suite
- *
- *
- */
- public static Test suite() {
- return new TestSuite(XMLSignatureInputTest.class);
- }
-
- //J-
- static final String _octetStreamTextInput = "Kleiner Test";
- //J+
-
- /**
- * Constructor XMLSignatureInputTest
- *
- *
- * @param Name_
- *
- */
- public XMLSignatureInputTest(String Name_) {
- super(Name_);
- }
-
- /**
- * Method main
- *
- *
- * @param args
- *
- */
- public static void main(String[] args) {
-
- String[] testCaseName = { "-noloading",
- XMLSignatureInputTest.class.getName() };
-
- junit.textui.TestRunner.main(testCaseName);
- }
-
-
-
- /**
- * Method testSetOctetStreamGetOctetStream
- *
- * @throws CanonicalizationException
- * @throws IOException
- * @throws InvalidCanonicalizerException
- */
- public static void testSetOctetStreamGetOctetStream()
- throws IOException, CanonicalizationException,
- InvalidCanonicalizerException {
-
- InputStream inputStream =
- new ByteArrayInputStream(_octetStreamTextInput.getBytes("UTF-8"));
- XMLSignatureInput input = new XMLSignatureInput(inputStream);
- ByteArrayOutputStream baos = new ByteArrayOutputStream();
- InputStream res = input.getOctetStream();
- int off = 0;
-
- while (res.available() > 0) {
- byte array[] = new byte[1024];
- int len = res.read(array);
-
- baos.write(array, off, len);
-
- off += len;
- }
-
- byte resBytes[] = baos.toByteArray();
- String resString = new String(resBytes, "UTF-8");
-
- assertTrue(resString.equals(_octetStreamTextInput));
- }
-
- //J-
- static final String _nodeSetInput1 =
+ static final String _octetStreamTextInput = "Kleiner Test";
+ static final String _nodeSetInput1 =
"<?xml version=\"1.0\"?>\n"
- + "<!DOCTYPE doc [\n"
- + "<!ELEMENT doc (n+)>\n"
- + "<!ELEMENT n (#PCDATA)>\n"
- + "]>\n"
- + "<!-- full document with decl -->"
- + "<doc>"
- + "<n>1</n>"
- + "<n>2</n>"
- + "<n>3</n>"
- + "<n>4</n>"
- + "</doc>";
- // added one for xmlns:xml since Xalan 2.2.D11
- static final int _nodeSetInput1Nodes = 11; // was 10
- static final int _nodeSetInput1NodesWithComments = _nodeSetInput1Nodes + 1;
- //J+
- //J-
- static final String _nodeSetInput2 =
+ + "<!DOCTYPE doc [\n"
+ + "<!ELEMENT doc (n+)>\n"
+ + "<!ELEMENT n (#PCDATA)>\n"
+ + "]>\n"
+ + "<!-- full document with decl -->"
+ + "<doc>"
+ + "<n>1</n>"
+ + "<n>2</n>"
+ + "<n>3</n>"
+ + "<n>4</n>"
+ + "</doc>";
+ // added one for xmlns:xml since Xalan 2.2.D11
+ static final int _nodeSetInput1Nodes = 11; // was 10
+ static final int _nodeSetInput1NodesWithComments = _nodeSetInput1Nodes + 1;
+ static final String _nodeSetInput2 =
"<?xml version=\"1.0\"?>\n"
- + "<!-- full document -->"
- + "<doc>"
- + "<n>1</n>"
- + "<n>2</n>"
- + "<n>3</n>"
- + "<n>4</n>"
- + "</doc>";
- // added one for xmlns:xml since Xalan 2.2.D11
- static final int _nodeSetInput2Nodes = 11; // was 10
- static final int _nodeSetInput2NodesWithComments = _nodeSetInput2Nodes + 1;
- //J+
- //J-
- static final String _nodeSetInput3 =
+ + "<!-- full document -->"
+ + "<doc>"
+ + "<n>1</n>"
+ + "<n>2</n>"
+ + "<n>3</n>"
+ + "<n>4</n>"
+ + "</doc>";
+ // added one for xmlns:xml since Xalan 2.2.D11
+ static final int _nodeSetInput2Nodes = 11; // was 10
+ static final int _nodeSetInput2NodesWithComments = _nodeSetInput2Nodes + 1;
+ static final String _nodeSetInput3 =
"<!-- document -->"
- + "<doc>"
- + "<n>1</n>"
- + "<n>2</n>"
- + "<n>3</n>"
- + "<n>4</n>"
- + "</doc>";
- // added one for xmlns:xml since Xalan 2.2.D11
- static final int _nodeSetInput3Nodes = 11; // was 10
- static final int _nodeSetInput3NodesWithComments = _nodeSetInput3Nodes + 1;
- //J+
-
-
- /**
- * Method testIsInitialized
- *
- * @throws IOException
- */
- public static void testIsInitializedWithOctetStream() throws IOException {
-
- InputStream inputStream =
- new ByteArrayInputStream(_octetStreamTextInput.getBytes());
- XMLSignatureInput input = new XMLSignatureInput(inputStream);
-
- assertTrue("Input is initialized", input.isInitialized());
- }
-
- /**
- * Method testOctetStreamIsOctetStream
- *
- * @throws IOException
- */
- public static void testOctetStreamIsOctetStream() throws IOException {
-
- InputStream inputStream =
- new ByteArrayInputStream(_octetStreamTextInput.getBytes());
- XMLSignatureInput input = new XMLSignatureInput(inputStream);
-
- assertTrue("Input is octet stream", input.isOctetStream());
- }
-
- /**
- * Method testOctetStreamIsNotNodeSet
- *
- * @throws IOException
- */
- public static void testOctetStreamIsNotNodeSet() throws IOException {
-
- InputStream inputStream =
- new ByteArrayInputStream(_octetStreamTextInput.getBytes());
- XMLSignatureInput input = new XMLSignatureInput(inputStream);
-
- assertTrue("Input is not node set", !input.isNodeSet());
- }
-
- static {
- org.apache.xml.security.Init.init();
- }
+ + "<doc>"
+ + "<n>1</n>"
+ + "<n>2</n>"
+ + "<n>3</n>"
+ + "<n>4</n>"
+ + "</doc>";
+ // added one for xmlns:xml since Xalan 2.2.D11
+ static final int _nodeSetInput3Nodes = 11; // was 10
+ static final int _nodeSetInput3NodesWithComments = _nodeSetInput3Nodes + 1;
+
+ static {
+ org.apache.xml.security.Init.init();
+ }
+
+ @org.junit.Test
+ public void testSetOctetStreamGetOctetStream()
+ throws IOException, CanonicalizationException, InvalidCanonicalizerException {
+ InputStream inputStream =
+ new ByteArrayInputStream(_octetStreamTextInput.getBytes("UTF-8"));
+ XMLSignatureInput input = new XMLSignatureInput(inputStream);
+ ByteArrayOutputStream baos = new ByteArrayOutputStream();
+ InputStream res = input.getOctetStream();
+ int off = 0;
+
+ while (res.available() > 0) {
+ byte array[] = new byte[1024];
+ int len = res.read(array);
+
+ baos.write(array, off, len);
+ off += len;
+ }
+
+ byte resBytes[] = baos.toByteArray();
+ String resString = new String(resBytes, "UTF-8");
+
+ assertTrue(resString.equals(_octetStreamTextInput));
+ }
+
+ @org.junit.Test
+ public void testIsInitializedWithOctetStream() throws IOException {
+ InputStream inputStream =
+ new ByteArrayInputStream(_octetStreamTextInput.getBytes());
+ XMLSignatureInput input = new XMLSignatureInput(inputStream);
+
+ assertTrue("Input is initialized", input.isInitialized());
+ }
+
+ @org.junit.Test
+ public void testOctetStreamIsOctetStream() throws IOException {
+ InputStream inputStream =
+ new ByteArrayInputStream(_octetStreamTextInput.getBytes());
+ XMLSignatureInput input = new XMLSignatureInput(inputStream);
+
+ assertTrue("Input is octet stream", input.isOctetStream());
+ }
+
+ @org.junit.Test
+ public void testOctetStreamIsNotNodeSet() throws IOException {
+ InputStream inputStream =
+ new ByteArrayInputStream(_octetStreamTextInput.getBytes());
+ XMLSignatureInput input = new XMLSignatureInput(inputStream);
+
+ assertTrue("Input is not node set", !input.isNodeSet());
+ }
+
}
Modified: santuario/xml-security-java/trunk/src/test/java/org/apache/xml/security/test/signature/XmlSecTest.java
URL: http://svn.apache.org/viewvc/santuario/xml-security-java/trunk/src/test/java/org/apache/xml/security/test/signature/XmlSecTest.java?rev=1060790&r1=1060789&r2=1060790&view=diff
==============================================================================
--- santuario/xml-security-java/trunk/src/test/java/org/apache/xml/security/test/signature/XmlSecTest.java (original)
+++ santuario/xml-security-java/trunk/src/test/java/org/apache/xml/security/test/signature/XmlSecTest.java Wed Jan 19 12:33:07 2011
@@ -39,17 +39,13 @@ import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
-import junit.framework.Test;
-import junit.framework.TestCase;
-import junit.framework.TestSuite;
-
/**
* Tests creating and validating an XML Signature with an XPath Transform.
* Tests bug #44617.
*
* @author Frank Cornelis
*/
-public class XmlSecTest extends TestCase {
+public class XmlSecTest extends org.junit.Assert {
private static final String BASEDIR =
System.getProperty("basedir") == null ? "./": System.getProperty("basedir");
@@ -58,16 +54,16 @@ public class XmlSecTest extends TestCase
org.apache.commons.logging.LogFactory.getLog
(XmlSecTest.class.getName());
- public static Test suite() {
- return new TestSuite(XmlSecTest.class);
- }
-
+ @org.junit.Test
public void testCheckXmlSignatureSoftwareStack() throws Exception {
checkXmlSignatureSoftwareStack(false);
}
+
+ @org.junit.Test
public void testCheckXmlSignatureSoftwareStackWithCert() throws Exception {
checkXmlSignatureSoftwareStack(true);
}
+
private void checkXmlSignatureSoftwareStack(boolean cert) throws Exception {
Init.init();
DocumentBuilderFactory documentBuilderFactory =
@@ -95,8 +91,8 @@ public class XmlSecTest extends TestCase
if (cert) {
// get key & self-signed certificate from keystore
String fs = System.getProperty("file.separator");
- FileInputStream fis = new FileInputStream
- (BASEDIR + fs + "data" + fs + "test.jks");
+ FileInputStream fis =
+ new FileInputStream(BASEDIR + fs + "data" + fs + "test.jks");
KeyStore ks = KeyStore.getInstance("JKS");
ks.load(fis, "changeit".toCharArray());
signingCert = (X509Certificate) ks.getCertificate("mullan");
@@ -108,9 +104,11 @@ public class XmlSecTest extends TestCase
privateKey = keyPair.getPrivate();
}
- XMLSignature signature = new XMLSignature(testDocument, "",
- XMLSignature.ALGO_ID_SIGNATURE_DSA,
- Canonicalizer.ALGO_ID_C14N_WITH_COMMENTS);
+ XMLSignature signature =
+ new XMLSignature(
+ testDocument, "", XMLSignature.ALGO_ID_SIGNATURE_DSA,
+ Canonicalizer.ALGO_ID_C14N_WITH_COMMENTS
+ );
Element signatureElement = signature.getElement();
rootElement.appendChild(signatureElement);
@@ -119,11 +117,9 @@ public class XmlSecTest extends TestCase
XPathContainer xpath = new XPathContainer(testDocument);
xpath.setXPathNamespaceContext("ds", Constants.SignatureSpecNS);
xpath.setXPath("not(ancestor-or-self::ds:Signature)");
- transforms.addTransform(Transforms.TRANSFORM_XPATH, xpath
- .getElementPlusReturns());
+ transforms.addTransform(Transforms.TRANSFORM_XPATH, xpath.getElementPlusReturns());
transforms.addTransform(Transforms.TRANSFORM_C14N_WITH_COMMENTS);
- signature.addDocument("", transforms,
- MessageDigestAlgorithm.ALGO_ID_DIGEST_SHA1);
+ signature.addDocument("", transforms, MessageDigestAlgorithm.ALGO_ID_DIGEST_SHA1);
if (cert) {
signature.addKeyInfo(signingCert);
@@ -132,17 +128,14 @@ public class XmlSecTest extends TestCase
}
Element nsElement = testDocument.createElementNS(null, "nsElement");
- nsElement.setAttributeNS(Constants.NamespaceSpecNS, "xmlns:ds",
- Constants.SignatureSpecNS);
+ nsElement.setAttributeNS(
+ Constants.NamespaceSpecNS, "xmlns:ds", Constants.SignatureSpecNS
+ );
signature.sign(privateKey);
- // TransformerFactory tf = TransformerFactory.newInstance();
- // Transformer t = tf.newTransformer();
- // t.transform(new DOMSource(testDocument), new StreamResult(System.out));
-
- NodeList signatureElems = XPathAPI.selectNodeList(testDocument,
- "//ds:Signature", nsElement);
+ NodeList signatureElems =
+ XPathAPI.selectNodeList(testDocument, "//ds:Signature", nsElement);
signatureElement = (Element) signatureElems.item(0);
XMLSignature signatureToVerify = new XMLSignature(signatureElement, "");
@@ -150,4 +143,5 @@ public class XmlSecTest extends TestCase
assertTrue(signResult);
}
+
}