[java-support] branch master updated: JSPT-93 - Clean up API for BaseXXSupport decoders (ENCODE)

Phil Smart philip.smart at jisc.ac.uk
Fri Feb 7 12:08:40 EST 2020


This is an automated email from the git hooks/post-receive script.

philsmart pushed a commit to branch master
in repository java-support.

View the commit online:
http://git.shibboleth.net/view/?p=java-support.git;a=commit;h=66ef1e9134a9a6b4cce1db4b1be2433757656a39

The following commit(s) were added to refs/heads/master by this push:
       new  66ef1e9   JSPT-93 - Clean up API for BaseXXSupport decoders (ENCODE)
66ef1e9 is described below

commit 66ef1e9134a9a6b4cce1db4b1be2433757656a39
Author: Phil Smart <philip.smart at jisc.ac.uk>
AuthorDate: Fri Feb 7 16:51:09 2020 +0000

    JSPT-93 - Clean up API for BaseXXSupport decoders (ENCODE)
    
     - Throw a checked EncodingException from both Base64Support and Base32Support when
       any exception (checked or unchecked) is throw from the commons-codec Base64 and
       Base32 encode methods.
     - Add appropriate error handling for the new EncodingException
    
    https://issues.shibboleth.net/jira/browse/JSPT-93
---
 .../java/support/codec/Base32Support.java          | 26 +++++++++++++-----
 .../java/support/codec/Base64Support.java          | 31 +++++++++++++++-------
 .../java/support/codec/StringDigester.java         |  8 +++++-
 .../java/support/codec/Base32SupportTest.java      | 17 ++++++++++++
 .../java/support/codec/Base64SupportTest.java      | 12 ++++++++-
 5 files changed, 75 insertions(+), 19 deletions(-)

diff --git a/src/main/java/net/shibboleth/utilities/java/support/codec/Base32Support.java b/src/main/java/net/shibboleth/utilities/java/support/codec/Base32Support.java
index 435e5a3..b957771 100644
--- a/src/main/java/net/shibboleth/utilities/java/support/codec/Base32Support.java
+++ b/src/main/java/net/shibboleth/utilities/java/support/codec/Base32Support.java
@@ -59,16 +59,28 @@ public final class Base32Support {
      * @param chunked whether the encoded data should be chunked or not
      * 
      * @return the base32 encoded data
+     * @throws EncodingException when any {@link Exception} is thrown from the underlying encoder, 
+     *                                  or the output is null.
      */
-    @Nonnull public static String encode(@Nonnull final byte[] data, final boolean chunked) {
+    @Nonnull public static String encode(@Nonnull final byte[] data, final boolean chunked) throws EncodingException {
         Constraint.isNotNull(data, "Binary data to be encoded can not be null");
-        if (chunked) {
-            return Constraint.isNotNull(StringSupport.trim(CHUNKED_ENCODER.encodeToString(data)),
-                    "Encoded data was null");
-        }
         
-        return Constraint.isNotNull(StringSupport.trim(UNCHUNKED_ENCODER.encodeToString(data)),
-                "Encoded data was null");
+        try {
+            String encoded = null;
+            if (chunked) {
+                encoded = StringSupport.trim(CHUNKED_ENCODER.encodeToString(data));
+            } else {
+                encoded = StringSupport.trim(UNCHUNKED_ENCODER.encodeToString(data));
+            }
+            //TODO: can this ever be null, do we need to check for null?
+            if (null == encoded) {
+                throw new EncodingException("Base32 encoded string was null");
+            }        
+            return encoded;
+        } catch (final Exception e) {
+            //wrap any exception on invalid input with our own.
+            throw new EncodingException("Unable to base32 encode data: "+e.getMessage(),e);
+        }
     }
 
     /**
diff --git a/src/main/java/net/shibboleth/utilities/java/support/codec/Base64Support.java b/src/main/java/net/shibboleth/utilities/java/support/codec/Base64Support.java
index e2c2a79..9ef01fa 100644
--- a/src/main/java/net/shibboleth/utilities/java/support/codec/Base64Support.java
+++ b/src/main/java/net/shibboleth/utilities/java/support/codec/Base64Support.java
@@ -17,8 +17,6 @@
 
 package net.shibboleth.utilities.java.support.codec;
 
-import java.util.Optional;
-
 import javax.annotation.Nonnull;
 
 import net.shibboleth.utilities.java.support.logic.Constraint;
@@ -61,16 +59,28 @@ public final class Base64Support {
      * @param chunked whether the encoded data should be chunked or not
      * 
      * @return the base64 encoded data
+     * @throws EncodingException when any {@link Exception} is thrown from the underlying encoder, 
+     *                                  or the output is null.
      */
-    @Nonnull public static String encode(@Nonnull final byte[] data, final boolean chunked) {
+    @Nonnull public static String encode(@Nonnull final byte[] data, final boolean chunked) throws EncodingException{
         Constraint.isNotNull(data, "Binary data to be encoded can not be null");
-        if (chunked) {
-            return Constraint.isNotNull(StringSupport.trim(CHUNKED_ENCODER.encodeToString(data)),
-                    "Encoded data was null");
-        }
         
-        return Constraint.isNotNull(StringSupport.trim(UNCHUNKED_ENCODER.encodeToString(data)),
-                "Encoded data was null");
+        try {
+            String encoded = null;
+            if (chunked) {
+                encoded = StringSupport.trim(CHUNKED_ENCODER.encodeToString(data));
+            } else {
+                encoded = StringSupport.trim(UNCHUNKED_ENCODER.encodeToString(data));
+            }
+            //TODO: can this ever be null, do we need to check for null?
+            if (null == encoded) {
+                throw new EncodingException("Base64 encoded string was null");
+            }        
+            return encoded;
+        } catch (final Exception e) {
+            //wrap any exception on invalid input with our own.
+            throw new EncodingException("Unable to base64 encode data: "+e.getMessage(),e);
+        }
     }
 
     /**
@@ -108,8 +118,9 @@ public final class Base64Support {
      * @param data data to encode
      * 
      * @return the base64url encoded data
+     * @throws EncodingException if the input data can not be encoded as a base64 string.
      */
-    @Nonnull public static String encodeURLSafe(@Nonnull final byte[] data) {
+    @Nonnull public static String encodeURLSafe(@Nonnull final byte[] data) throws EncodingException {
         String s = encode(data, false);
         s = s.split("=")[0];
         s = s.replace('+', '-');
diff --git a/src/main/java/net/shibboleth/utilities/java/support/codec/StringDigester.java b/src/main/java/net/shibboleth/utilities/java/support/codec/StringDigester.java
index 87b3f5e..9d6394d 100644
--- a/src/main/java/net/shibboleth/utilities/java/support/codec/StringDigester.java
+++ b/src/main/java/net/shibboleth/utilities/java/support/codec/StringDigester.java
@@ -178,7 +178,13 @@ public class StringDigester implements Function<String,String> {
         
         switch(outputFormat) {
             case BASE64:
-                output = Base64Support.encode(digestedBytes, false);
+                try {
+                    output = Base64Support.encode(digestedBytes, false);
+                } catch (final EncodingException e) {
+                    //unlikely to happen.
+                    log.warn("Could not base64 encode digest bytes, no data returned",e);
+                    return null;
+                }
                 break;
             case HEX_LOWER:
                 output = new String(Hex.encodeHex(digestedBytes, true));
diff --git a/src/test/java/net/shibboleth/utilities/java/support/codec/Base32SupportTest.java b/src/test/java/net/shibboleth/utilities/java/support/codec/Base32SupportTest.java
index a49f91b..85973f6 100644
--- a/src/test/java/net/shibboleth/utilities/java/support/codec/Base32SupportTest.java
+++ b/src/test/java/net/shibboleth/utilities/java/support/codec/Base32SupportTest.java
@@ -19,11 +19,19 @@ package net.shibboleth.utilities.java.support.codec;
 
 import org.testng.annotations.Test;
 
+import junit.framework.Assert;
 import net.shibboleth.utilities.java.support.logic.ConstraintViolationException;
 
 /** {@link Base32Support} unit test. */
 public class Base32SupportTest {
 
+    
+    /** A plain text string to be encoded. */
+    private final static String PLAIN_TEXT = "test data";
+    
+    /** Base32 encoded version of <code>PLAIN_TEXT</code>. */
+    private final static String ENCODED_TEXT = "ORSXG5BAMRQXIYI=";
+                       
     /** 
      * Invalid base32 string as it has invalid trailing digits.
      * Correctly fails with commons-codec 1.14 and greater.
@@ -65,5 +73,14 @@ public class Base32SupportTest {
             throws EncodingException {
         Base32Support.encode(null, false);
     }
+    
+    /**
+     * Test encoding a byte array works.
+     * 
+     * @throws EncodingException if there is an issue encoding the byte array, should not happen.
+     */
+    @Test public void testEncode() throws EncodingException {        
+        Assert.assertEquals(ENCODED_TEXT,  Base32Support.encode(PLAIN_TEXT.getBytes(), false));
+    }
   
 }
\ No newline at end of file
diff --git a/src/test/java/net/shibboleth/utilities/java/support/codec/Base64SupportTest.java b/src/test/java/net/shibboleth/utilities/java/support/codec/Base64SupportTest.java
index fadbbdd..918eff6 100644
--- a/src/test/java/net/shibboleth/utilities/java/support/codec/Base64SupportTest.java
+++ b/src/test/java/net/shibboleth/utilities/java/support/codec/Base64SupportTest.java
@@ -99,9 +99,19 @@ public class Base64SupportTest {
     /**
      * Test a null byte array argument violates the method contract and throws a {@link ConstraintViolationException}.
      * 
-     * @throws DecodingException on decoding failure.
+     * @throws EncodingException on encoding failure.
      */
     @Test(expectedExceptions = ConstraintViolationException.class) public void testEncodeNullInput() 
+            throws EncodingException {
+        Base64Support.encode(null,false);
+    }
+    
+    /**
+     * Test a null string argument violates the method contract and throws a {@link ConstraintViolationException}.
+     * 
+     * @throws DecodingException on decoding failure.
+     */
+    @Test(expectedExceptions = ConstraintViolationException.class) public void testDecodeNullInput() 
             throws DecodingException {
         Base64Support.decode(null);
     }

-- 
To stop receiving notification emails like this one, please contact
the administrator of this repository.


More information about the commits mailing list