ws-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From cohei...@apache.org
Subject svn commit: r1159670 - in /webservices/wss4j/trunk/src: main/java/org/apache/ws/security/message/token/ test/java/org/apache/ws/security/message/token/
Date Fri, 19 Aug 2011 15:52:47 GMT
Author: coheigea
Date: Fri Aug 19 15:52:47 2011
New Revision: 1159670

URL: http://svn.apache.org/viewvc?rev=1159670&view=rev
Log:
[WSS-307] - Added equals/hashcode for remaining types.
 - Patch applied thanks!
 - I removed the "id" checking, as we only care about a logical comparison.

Added:
    webservices/wss4j/trunk/src/test/java/org/apache/ws/security/message/token/DerivedKeyTokenTest.java
Modified:
    webservices/wss4j/trunk/src/main/java/org/apache/ws/security/message/token/DOMX509IssuerSerial.java
    webservices/wss4j/trunk/src/main/java/org/apache/ws/security/message/token/DerivedKeyToken.java
    webservices/wss4j/trunk/src/main/java/org/apache/ws/security/message/token/Reference.java
    webservices/wss4j/trunk/src/main/java/org/apache/ws/security/message/token/SecurityTokenReference.java
    webservices/wss4j/trunk/src/test/java/org/apache/ws/security/message/token/ReferenceTest.java

Modified: webservices/wss4j/trunk/src/main/java/org/apache/ws/security/message/token/DOMX509IssuerSerial.java
URL: http://svn.apache.org/viewvc/webservices/wss4j/trunk/src/main/java/org/apache/ws/security/message/token/DOMX509IssuerSerial.java?rev=1159670&r1=1159669&r2=1159670&view=diff
==============================================================================
--- webservices/wss4j/trunk/src/main/java/org/apache/ws/security/message/token/DOMX509IssuerSerial.java
(original)
+++ webservices/wss4j/trunk/src/main/java/org/apache/ws/security/message/token/DOMX509IssuerSerial.java
Fri Aug 19 15:52:47 2011
@@ -55,7 +55,12 @@ public final class DOMX509IssuerSerial {
                 element, WSConstants.X509_SERIAL_NUMBER_LN, WSConstants.SIG_NS
             );
         String serialNumberStr = getChildText(serialNumberElement);
-        serialNumber = new BigInteger(serialNumberStr);
+        if (serialNumberStr != null) {
+            serialNumber = new BigInteger(serialNumberStr);
+        } else {
+            serialNumber = null;
+        }
+        
     }
 
     /**
@@ -126,6 +131,9 @@ public final class DOMX509IssuerSerial {
     
     
     private String getChildText(Node parentNode) {
+        if (parentNode == null) {
+            return null;
+        }
         Node node = parentNode.getFirstChild();
         StringBuilder buffer = new StringBuilder();
         while (node != null) {

Modified: webservices/wss4j/trunk/src/main/java/org/apache/ws/security/message/token/DerivedKeyToken.java
URL: http://svn.apache.org/viewvc/webservices/wss4j/trunk/src/main/java/org/apache/ws/security/message/token/DerivedKeyToken.java?rev=1159670&r1=1159669&r2=1159670&view=diff
==============================================================================
--- webservices/wss4j/trunk/src/main/java/org/apache/ws/security/message/token/DerivedKeyToken.java
(original)
+++ webservices/wss4j/trunk/src/main/java/org/apache/ws/security/message/token/DerivedKeyToken.java
Fri Aug 19 15:52:47 2011
@@ -557,5 +557,104 @@ public class DerivedKeyToken {
             );
         }
     }
-
+    
+    @Override
+    public int hashCode() {
+        int result = 17;
+        String algorithm = getAlgorithm();
+        if (algorithm != null) {
+            result = 31 * result + algorithm.hashCode();
+        }
+        try {
+            SecurityTokenReference tokenReference = getSecurityTokenReference();
+            if (tokenReference != null) {
+                result = 31 * result + tokenReference.hashCode();
+            }
+        } catch (WSSecurityException e) {
+            log.error(e);
+        }
+        
+        Map<String, String> properties = getProperties();
+        if (properties != null) {
+            result = 31 * result + properties.hashCode();
+        }
+        int generation = getGeneration();
+        if (generation != -1) {
+            result = 31 * result + generation;
+        }
+        int offset = getOffset();
+        if (offset != -1) {
+            result = 31 * result + offset;
+        }
+        int length = getLength();
+        if (length != -1) {
+            result = 31 * result + length;
+        }
+        String label = getLabel();
+        if (label != null) {
+            result = 31 * result + label.hashCode();
+        }
+        String nonce = getNonce();
+        if (nonce != null) {
+            result = 31 * result + nonce.hashCode();
+        }
+        
+        return result;
+    }
+    
+    @Override
+    public boolean equals(Object object) {
+        if (!(object instanceof DerivedKeyToken)) {
+            return false;
+        }
+        DerivedKeyToken token = (DerivedKeyToken)object;
+        if (!compare(getAlgorithm(), token.getAlgorithm())) {
+            return false;
+        }
+        try {
+            if (!getSecurityTokenReference().equals(token.getSecurityTokenReference())) {
+                return false;
+            }
+        } catch (WSSecurityException e) {
+            log.error(e);
+            return false;
+        }
+        if (!compare(getProperties(), token.getProperties())) {
+            return false;
+        }
+        if (getGeneration() != token.getGeneration()) {
+            return false;
+        }
+        if (getOffset() != token.getOffset()) {
+            return false;
+        }
+        if (getLength() != token.getLength()) {
+            return false;
+        }
+        if (!compare(getLabel(), token.getLabel())) {
+            return false;
+        }
+        if (!compare(getNonce(), token.getNonce())) {
+            return false;
+        }
+        return true;
+    }
+    
+    private boolean compare(String item1, String item2) {
+        if (item1 == null && item2 != null) { 
+            return false;
+        } else if (item1 != null && !item1.equals(item2)) {
+            return false;
+        }
+        return true;
+    }
+    
+    private boolean compare(Map<String, String> item1, Map<String, String> item2)
{
+        if (item1 == null && item2 != null) { 
+            return false;
+        } else if (item1 != null && !item1.equals(item2)) {
+            return false;
+        }
+        return true;
+    }
 }

Modified: webservices/wss4j/trunk/src/main/java/org/apache/ws/security/message/token/Reference.java
URL: http://svn.apache.org/viewvc/webservices/wss4j/trunk/src/main/java/org/apache/ws/security/message/token/Reference.java?rev=1159670&r1=1159669&r2=1159670&view=diff
==============================================================================
--- webservices/wss4j/trunk/src/main/java/org/apache/ws/security/message/token/Reference.java
(original)
+++ webservices/wss4j/trunk/src/main/java/org/apache/ws/security/message/token/Reference.java
Fri Aug 19 15:52:47 2011
@@ -19,6 +19,8 @@
 
 package org.apache.ws.security.message.token;
 
+import javax.xml.namespace.QName;
+
 import org.apache.ws.security.WSConstants;
 import org.apache.ws.security.WSSecurityException;
 import org.apache.ws.security.util.DOM2Writer;
@@ -27,8 +29,6 @@ import org.w3c.dom.Document;
 import org.w3c.dom.Element;
 import org.w3c.dom.Node;
 
-import javax.xml.namespace.QName;
-
 /**
  * Reference.
  * 
@@ -135,4 +135,42 @@ public class Reference {
     public String toString() {
         return DOM2Writer.nodeToString((Node)element);
     }
+    
+    @Override
+    public int hashCode() {
+        int result = 17;
+        String uri = getURI();
+        if (uri != null) {
+            result = 31 * result + uri.hashCode();
+        }
+        String valueType = getValueType();
+        if (valueType != null) {
+            result = 31 * result + valueType.hashCode();
+        }
+        return result;
+    }
+    
+    @Override
+    public boolean equals(Object object) {
+        if (!(object instanceof Reference)) {
+            return false;
+        }
+        Reference reference = (Reference)object;
+        if (!compare(getURI(), reference.getURI())) {
+            return false;
+        }
+        if (!compare(getValueType(), reference.getValueType())) {
+            return false;
+        }
+        return true;
+    }
+    
+    private boolean compare(String item1, String item2) {
+        if (item1 == null && item2 != null) { 
+            return false;
+        } else if (item1 != null && !item1.equals(item2)) {
+            return false;
+        }
+        return true;
+    }
 }

Modified: webservices/wss4j/trunk/src/main/java/org/apache/ws/security/message/token/SecurityTokenReference.java
URL: http://svn.apache.org/viewvc/webservices/wss4j/trunk/src/main/java/org/apache/ws/security/message/token/SecurityTokenReference.java?rev=1159670&r1=1159669&r2=1159670&view=diff
==============================================================================
--- webservices/wss4j/trunk/src/main/java/org/apache/ws/security/message/token/SecurityTokenReference.java
(original)
+++ webservices/wss4j/trunk/src/main/java/org/apache/ws/security/message/token/SecurityTokenReference.java
Fri Aug 19 15:52:47 2011
@@ -40,8 +40,10 @@ import javax.xml.namespace.QName;
 import javax.security.auth.callback.Callback;
 import javax.security.auth.callback.CallbackHandler;
 
+import java.math.BigInteger;
 import java.security.cert.CertificateEncodingException;
 import java.security.cert.X509Certificate;
+import java.util.Arrays;
 
 /**
  * Security Token Reference.
@@ -848,4 +850,117 @@ public class SecurityTokenReference {
             }
         }
     }
+    
+    @Override
+    public int hashCode() {
+        int result = 17;
+        try {
+            Reference reference = getReference();
+            if (reference != null) {
+                result = 31 * result + reference.hashCode();
+            }
+        } catch (WSSecurityException e) {
+            log.error(e);
+        }
+        String keyIdentifierEncodingType = getKeyIdentifierEncodingType();
+        if (keyIdentifierEncodingType != null) {
+            result = 31 * result + keyIdentifierEncodingType.hashCode();
+        }
+        String keyIdentifierValueType = getKeyIdentifierValueType();
+        if (keyIdentifierValueType != null) {
+            result = 31 * result + keyIdentifierValueType.hashCode();
+        }
+        String keyIdentifierValue = getKeyIdentifierValue();
+        if (keyIdentifierValue != null) {
+            result = 31 * result + keyIdentifierValue.hashCode();
+        }
+        String tokenType = getTokenType();
+        if (tokenType != null) {
+            result = 31 * result + tokenType.hashCode();
+        }
+        byte[] skiBytes = getSKIBytes();
+        if (skiBytes != null) {
+            result = 31 * result + Arrays.hashCode(skiBytes);
+        }
+        String issuer = null;
+        BigInteger serialNumber = null;
+        
+        try {
+            issuer = getIssuerSerial().getIssuer();
+            serialNumber = getIssuerSerial().getSerialNumber();
+        } catch (WSSecurityException e) {
+           log.error(e);
+        }
+        if (issuer != null) {
+            result = 31 * result + issuer.hashCode();
+        }
+        if (serialNumber != null) {
+            result = 31 * result + serialNumber.hashCode();
+        }
+        return result;
+    }
+    
+    @Override
+    public boolean equals(Object object) {
+        if (!(object instanceof SecurityTokenReference)) {
+            return false;
+        }
+        SecurityTokenReference tokenReference = (SecurityTokenReference)object;
+        try {
+            if (!getReference().equals(tokenReference.getReference())) {
+                return false;
+            }
+        } catch (WSSecurityException e) {
+           log.error(e);
+           return false;
+        }
+        if (!compare(getKeyIdentifierEncodingType(), tokenReference.getKeyIdentifierEncodingType()))
{
+            return false;
+        }
+        if (!compare(getKeyIdentifierValueType(), tokenReference.getKeyIdentifierValueType()))
{
+            return false;
+        }
+        if (!compare(getKeyIdentifierValue(), tokenReference.getKeyIdentifierValue())) {
+            return false;
+        }
+        if (!compare(getTokenType(), tokenReference.getTokenType())) {
+            return false;
+        }
+        if (!Arrays.equals(getSKIBytes(), tokenReference.getSKIBytes())) {
+            return false;
+        }
+        try {
+            if (getIssuerSerial() != null && tokenReference.getIssuerSerial() !=
null) {
+                if (!compare(getIssuerSerial().getIssuer(), tokenReference.getIssuerSerial().getIssuer()))
{
+                    return false;
+                }
+                if (!compare(getIssuerSerial().getSerialNumber(), tokenReference.getIssuerSerial().getSerialNumber()))
{
+                    return false;
+                } 
+            }
+        } catch (WSSecurityException e) {
+           log.error(e);
+           return false;
+        }
+            
+        return true;
+    }
+    
+    private boolean compare(String item1, String item2) {
+        if (item1 == null && item2 != null) { 
+            return false;
+        } else if (item1 != null && !item1.equals(item2)) {
+            return false;
+        }
+        return true;
+    }
+    
+    private boolean compare(BigInteger item1, BigInteger item2) {
+        if (item1 == null && item2 != null) { 
+            return false;
+        } else if (item1 != null && !item1.equals(item2)) {
+            return false;
+        }
+        return true;
+    }
 }

Added: webservices/wss4j/trunk/src/test/java/org/apache/ws/security/message/token/DerivedKeyTokenTest.java
URL: http://svn.apache.org/viewvc/webservices/wss4j/trunk/src/test/java/org/apache/ws/security/message/token/DerivedKeyTokenTest.java?rev=1159670&view=auto
==============================================================================
--- webservices/wss4j/trunk/src/test/java/org/apache/ws/security/message/token/DerivedKeyTokenTest.java
(added)
+++ webservices/wss4j/trunk/src/test/java/org/apache/ws/security/message/token/DerivedKeyTokenTest.java
Fri Aug 19 15:52:47 2011
@@ -0,0 +1,95 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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 org.apache.ws.security.message.token;
+
+/**
+ * Tests for DerivedKeyToken type.
+ */
+
+public class DerivedKeyTokenTest extends org.junit.Assert {
+
+    private static final String TEST_TOKEN_TEMPLATE = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
+            + "<wsc:DerivedKeyToken "
+            + "xmlns:wsc=\"http://schemas.xmlsoap.org/ws/2005/02/sc\" xmlns:wsu=\"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd\"
wsu:Id=\"_3\" >"
+            + "<wsse:SecurityTokenReference "
+            + "xmlns:wsse=\"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd\"
wsu:Id=\"_5002\">"
+            + "<wsse:Reference ValueType=\"http://schemas.xmlsoap.org/ws/2005/02/sc/sct\"
URI=\"PLACEHOLDER1\" />"
+            + "</wsse:SecurityTokenReference>"
+            + "<wsc:Offset>0</wsc:Offset>"
+            + "<wsc:Length>PLACEHOLDER2</wsc:Length>"
+            + "<wsc:Nonce>Kq/1ptgjZpX2g1q6MiJcSfTX</wsc:Nonce>"
+            + "<wsc:Label>WS-SecureConversationWS-SecureConversation</wsc:Label>"
+            + "<wsc:Generation>3</wsc:Generation>"
+            + "<wsc:Properties>"
+            + "<wsc:Name>.../derivedKeySource</wsc:Name>"
+            + "</wsc:Properties>"
+            + "</wsc:DerivedKeyToken>";
+
+    private DerivedKeyToken token;
+    private DerivedKeyToken tokenEqual;
+    private DerivedKeyToken tokenNotEqual;
+    
+    
+    public DerivedKeyTokenTest() throws Exception {
+        token = new DerivedKeyToken(createReferenceDocument(
+                TEST_TOKEN_TEMPLATE,
+                "#uuid-4063ae9b-fe66-4e09-a5fb-8fda903f34d8", "16")
+                .getDocumentElement());
+        tokenEqual = new DerivedKeyToken(createReferenceDocument(
+                TEST_TOKEN_TEMPLATE,
+                "#uuid-4063ae9b-fe66-4e09-a5fb-8fda903f34d8", "16")
+                .getDocumentElement());
+        tokenNotEqual = new DerivedKeyToken(createReferenceDocument(
+                TEST_TOKEN_TEMPLATE,
+                "#uuid-5603ae9b-fe66-4e09-a5fb-8fda903f34d8", "88")
+                .getDocumentElement());
+    }
+
+    @org.junit.Test
+    public void testEquals() throws Exception{
+        assertTrue(token.equals(tokenEqual));
+        assertTrue(tokenEqual.equals(token));
+        assertFalse(token.equals(tokenNotEqual));
+        assertFalse(token.equals(null));
+        assertFalse(token.equals("string"));        
+    }
+    
+    @org.junit.Test
+    public void testHashcode() throws Exception{
+        assertEquals(token.hashCode(), tokenEqual.hashCode());
+        assertTrue(!(token.hashCode() == tokenNotEqual.hashCode()));
+    }
+
+    private static org.w3c.dom.Document createReferenceDocument(
+            final String template, final String placeholder1,
+            final String placeholder2)
+            throws javax.xml.parsers.ParserConfigurationException,
+            org.xml.sax.SAXException, java.io.IOException {
+        final java.io.InputStream in = new java.io.ByteArrayInputStream(
+                template.replaceFirst("PLACEHOLDER1", placeholder1)
+                        .replaceFirst("PLACEHOLDER2", placeholder2).getBytes());
+        final javax.xml.parsers.DocumentBuilderFactory factory = javax.xml.parsers.DocumentBuilderFactory
+                .newInstance();
+        factory.setNamespaceAware(true);
+        final javax.xml.parsers.DocumentBuilder builder = factory
+                .newDocumentBuilder();
+        return builder.parse(in);
+    }
+}

Modified: webservices/wss4j/trunk/src/test/java/org/apache/ws/security/message/token/ReferenceTest.java
URL: http://svn.apache.org/viewvc/webservices/wss4j/trunk/src/test/java/org/apache/ws/security/message/token/ReferenceTest.java?rev=1159670&r1=1159669&r2=1159670&view=diff
==============================================================================
--- webservices/wss4j/trunk/src/test/java/org/apache/ws/security/message/token/ReferenceTest.java
(original)
+++ webservices/wss4j/trunk/src/test/java/org/apache/ws/security/message/token/ReferenceTest.java
Fri Aug 19 15:52:47 2011
@@ -46,7 +46,17 @@ public class ReferenceTest extends org.j
         +       "/>"
         ;
 
-
+    private Reference ref;
+    private Reference refEqual;
+    private Reference refNotEqual;
+    
+    public ReferenceTest() throws Exception{
+        ref = new Reference (createReferenceDocument(TEST_REFERENCE_TEMPLATE, "test", "equalscheck").getDocumentElement());
+        refEqual = new Reference (createReferenceDocument(TEST_REFERENCE_TEMPLATE, "test",
"equalscheck").getDocumentElement());
+        refNotEqual = new Reference (createReferenceDocument(TEST_REFERENCE_TEMPLATE, "test",
"unequalscheck").getDocumentElement());
+    }
+    
+ 
     @org.junit.Test
     public void
     testConstructor() throws Exception {
@@ -103,6 +113,21 @@ public class ReferenceTest extends org.j
         assertEquals(ref.getURI(), "bar");
     }
     
+    @org.junit.Test
+    public void testEquals() throws Exception{
+        assertTrue(ref.equals(refEqual));
+        assertTrue(refEqual.equals(ref));
+        assertFalse(ref.equals(refNotEqual));
+        assertFalse(ref.equals(null));
+        assertFalse(ref.equals("string"));        
+    }
+    
+    @org.junit.Test
+    public void testHashcode() throws Exception{
+        assertEquals(ref.hashCode(), refEqual.hashCode());
+        assertTrue(!(ref.hashCode() == refNotEqual.hashCode()));
+    }
+    
     private static org.w3c.dom.Document
     createReferenceDocument(
         final String template,



Mime
View raw message