You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@santuario.apache.org by mu...@apache.org on 2006/04/11 20:04:34 UTC

svn commit: r393277 - in /xml/security/trunk/src_unitTests/javax/xml/crypto/test/dsig: SignatureValidator.java ValidateSignatureTest.java

Author: mullan
Date: Tue Apr 11 11:04:26 2006
New Revision: 393277

URL: http://svn.apache.org/viewcvs?rev=393277&view=rev
Log:
New regression test for bug 39273: JSR 105 DOMCryptoContext.setIdAttributeNS 
not working when validating signatures

Added:
    xml/security/trunk/src_unitTests/javax/xml/crypto/test/dsig/ValidateSignatureTest.java
Modified:
    xml/security/trunk/src_unitTests/javax/xml/crypto/test/dsig/SignatureValidator.java

Modified: xml/security/trunk/src_unitTests/javax/xml/crypto/test/dsig/SignatureValidator.java
URL: http://svn.apache.org/viewcvs/xml/security/trunk/src_unitTests/javax/xml/crypto/test/dsig/SignatureValidator.java?rev=393277&r1=393276&r2=393277&view=diff
==============================================================================
--- xml/security/trunk/src_unitTests/javax/xml/crypto/test/dsig/SignatureValidator.java (original)
+++ xml/security/trunk/src_unitTests/javax/xml/crypto/test/dsig/SignatureValidator.java Tue Apr 11 11:04:26 2006
@@ -54,9 +54,8 @@
 	return validate(fn, ks, null);
     }
 
-    public boolean validate(String fn, KeySelector ks, URIDereferencer ud)
+    public DOMValidateContext getValidateContext(String fn, KeySelector ks)
 	throws Exception {
-
         DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
         dbf.setNamespaceAware(true);
         dbf.setValidating(false);
@@ -67,12 +66,25 @@
 	}
 	DOMValidateContext vc = new DOMValidateContext(ks, sigElement);
 	vc.setBaseURI(dir.toURI().toString());
-        XMLSignatureFactory factory = XMLSignatureFactory.getInstance
-            ("DOM", new org.jcp.xml.dsig.internal.dom.XMLDSigRI());
-    	XMLSignature signature = factory.unmarshalXMLSignature(vc);
+	return vc;
+    }
+
+    public boolean validate(String fn, KeySelector ks, URIDereferencer ud)
+	throws Exception {
+
+	DOMValidateContext vc = getValidateContext(fn, ks);
 	if (ud != null) {
 	    vc.setURIDereferencer(ud);
 	}
+
+	return validate(vc);
+    }
+
+    public boolean validate(DOMValidateContext vc) throws Exception {
+
+        XMLSignatureFactory factory = XMLSignatureFactory.getInstance
+            ("DOM", new org.jcp.xml.dsig.internal.dom.XMLDSigRI());
+    	XMLSignature signature = factory.unmarshalXMLSignature(vc);
     	boolean coreValidity = signature.validate(vc);
     
     	// Check core validation status
@@ -86,7 +98,7 @@
     	}
         return coreValidity;
     }
-    
+
     public static Element getSignatureElement(Document doc) {
         NodeIterator ni = ((DocumentTraversal)doc).createNodeIterator(
             doc.getDocumentElement(), NodeFilter.SHOW_ELEMENT, null, false);

Added: xml/security/trunk/src_unitTests/javax/xml/crypto/test/dsig/ValidateSignatureTest.java
URL: http://svn.apache.org/viewcvs/xml/security/trunk/src_unitTests/javax/xml/crypto/test/dsig/ValidateSignatureTest.java?rev=393277&view=auto
==============================================================================
--- xml/security/trunk/src_unitTests/javax/xml/crypto/test/dsig/ValidateSignatureTest.java (added)
+++ xml/security/trunk/src_unitTests/javax/xml/crypto/test/dsig/ValidateSignatureTest.java Tue Apr 11 11:04:26 2006
@@ -0,0 +1,73 @@
+/*
+ * Copyright 2006 The Apache Software Foundation.
+ *
+ *  Licensed under the Apache License, Version 2.0 (the "License");
+ *  you may not use this file except in compliance with the License.
+ *  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing, software
+ *  distributed under the License is distributed on an "AS IS" BASIS,
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  See the License for the specific language governing permissions and
+ *  limitations under the License.
+ *
+ */
+package javax.xml.crypto.test.dsig;
+
+import java.io.File;
+import java.security.Security;
+import org.w3c.dom.Document;
+import org.w3c.dom.Element;
+import org.w3c.dom.NodeList;
+
+import junit.framework.*;
+
+import javax.xml.crypto.test.KeySelectors;
+import javax.xml.crypto.dsig.dom.DOMValidateContext;
+
+/**
+ * This is a testcase that validates various signatures
+ *
+ * @author Sean Mullan
+ */
+public class ValidateSignatureTest extends TestCase {
+
+    private SignatureValidator validator;
+    private File dir;
+
+    static {
+        Security.insertProviderAt
+            (new org.jcp.xml.dsig.internal.dom.XMLDSigRI(), 1);
+    }
+
+    public ValidateSignatureTest(String name) {
+        super(name);
+	String fs = System.getProperty("file.separator");
+	dir = new File(System.getProperty("basedir") + fs + "data" + fs 
+	    + "javax" + fs + "xml" + fs + "crypto", "dsig");
+	validator = new SignatureValidator(dir);
+    }
+
+    /** 
+     * Validates a signature that references an element with an ID attribute. 
+     * The element's ID needs to be registered so that it can be found.
+     */
+    public void test_signature_with_ID() throws Exception {
+        String file = "envelopingSignature.xml";
+
+	DOMValidateContext vc = validator.getValidateContext
+	    (file, new KeySelectors.KeyValueKeySelector());
+	Document doc = vc.getNode().getOwnerDocument();
+	NodeList nl = doc.getElementsByTagName("Assertion");
+	vc.setIdAttributeNS((Element) nl.item(0), null, "AssertionID");
+	boolean coreValidity = validator.validate(vc);
+	assertTrue("Signature failed core validation", coreValidity);
+    }
+    
+    public static void main(String[] args) throws Exception {
+        ValidateSignatureTest vst = new ValidateSignatureTest("");
+        vst.test_signature_with_ID();
+    }
+}