commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From tobr...@apache.org
Subject cvs commit: jakarta-commons/codec/src/test/org/apache/commons/codec/binary Base64Test.java
Date Wed, 14 May 2003 02:40:18 GMT
tobrien     2003/05/13 19:40:18

  Modified:    codec/src/java/org/apache/commons/codec/binary Base64.java
               codec/src/test/org/apache/commons/codec/binary
                        Base64Test.java
  Log:
  Fix for Bug 19860: Modified Base64 to remedy non-compliance with RFC 2045.  Non-Base64 characters
were not being discarded during the decode.  RFC 2045 explicitly states that all characters
outside of the base64 alphabet are to be ignored.  A new function discardNonBase64 is called
prior to a decode.  Note that isArrayBase64 only discards whitespace before testing the contents
of an encoded byte array.  Unit tests for Base64 were updated to reflect the changes related
to discarding non-Base64 characters.
  
  Revision  Changes    Path
  1.3       +33 -18    jakarta-commons/codec/src/java/org/apache/commons/codec/binary/Base64.java
  
  Index: Base64.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/codec/src/java/org/apache/commons/codec/binary/Base64.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- Base64.java	6 May 2003 20:52:18 -0000	1.2
  +++ Base64.java	14 May 2003 02:40:18 -0000	1.3
  @@ -196,19 +196,8 @@
       }
   
       public byte[] decode(byte[] pArray) throws DecoderException {
  -
           byte[] result;
  -
  -        if (!isArrayByteBase64(pArray)) {
  -            throw new DecoderException(
  -                "Parameter supplied to "
  -                    + "Base64 "
  -                    + "decode is not a valid base64 data.");
  -        } 
  -        else {
  -            result = decodeBase64((byte[]) pArray);
  -        }
  -
  +        result = decodeBase64((byte[]) pArray);
           return (result);
       }
   
  @@ -375,9 +364,8 @@
        * @return Array containing decoded data.
        */
       public static byte[] decodeBase64(byte[] base64Data) {
  -        // RFC 2045 suggests line wrapping at (no more than) 76
  -        // characters -- we may have embedded whitespace.
  -        base64Data = discardWhitespace(base64Data);
  +        // RFC 2045 requires that we discard ALL non-Base64 characters
  +        base64Data = discardNonBase64(base64Data);
   
           // handle the edge case, so we don't have to worry about it later
           if (base64Data.length == 0) {
  @@ -468,6 +456,33 @@
   
           return packedData;
       }
  +    
  +	/**
  +	 * Discards any characters outside of the base64 alphabet, per
  +	 * the requirements on page 25 of RFC 2045 - "Any characters
  +	 * outside of the base64 alphabet are to be ignored in base64
  +	 * encoded data."
  +	 *
  +	 * @param data The base-64 encoded data to groom
  +	 * @return The data, less non-base64 characters (see RFC 2045).
  +	 */
  +	static byte[] discardNonBase64(byte[] data) {
  +		byte groomedData[] = new byte[data.length];
  +		int bytesCopied = 0;
  +
  +		for (int i = 0; i < data.length; i++) {
  +			if( isBase64(data[i]) ) {
  +			  groomedData[bytesCopied++] = data[i];
  +			}
  +		}
  +
  +		byte packedData[] = new byte[bytesCopied];
  +
  +		System.arraycopy(groomedData, 0, packedData, 0, bytesCopied);
  +
  +		return packedData;
  +	}
  +
   
       // Implementation of the Encoder Interface
   
  
  
  
  1.3       +32 -22    jakarta-commons/codec/src/test/org/apache/commons/codec/binary/Base64Test.java
  
  Index: Base64Test.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/codec/src/test/org/apache/commons/codec/binary/Base64Test.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- Base64Test.java	6 May 2003 20:45:16 -0000	1.2
  +++ Base64Test.java	14 May 2003 02:40:18 -0000	1.3
  @@ -313,28 +313,38 @@
           assertEquals("xyzzy!",new String(Base64.decodeBase64("eHl6enkh".getBytes())));
           } 
   
  -    public void testNonBase64() throws Exception {
  +	public void testNonBase64Test() throws Exception {
   
  -        byte[] bArray = { '%' };
  +		byte[] bArray = { '%' };
   
  -        assertFalse( "Invalid Base64 array was incorrectly validated as " +
  -                     "an array of Base64 encoded data", 
  -                     Base64.isArrayByteBase64( bArray ) );
  +		assertFalse( "Invalid Base64 array was incorrectly validated as " +
  +					 "an array of Base64 encoded data", 
  +					 Base64.isArrayByteBase64( bArray ) );
           
  -        boolean exceptionThrown = false;
  +		boolean exceptionThrown = false;
   
  -        try {
  -            Base64 b64 = new Base64();
  -            b64.decode( bArray );
  -        } 
  -        catch( Exception e ) {
  -            exceptionThrown = true;
  -        }
  +		try {
  +			Base64 b64 = new Base64();
  +			byte[] result = b64.decode( bArray );
  +            
  +			assertTrue( "The result should be empty as the test encoded content did " +
  +				"not contain any valid base 64 characters", result.length == 0);
  +		} 
  +		catch( Exception e ) {
  +			exceptionThrown = true;
  +		}
   
  -        assertTrue( "Exception wasn't thrown when trying to decode " +
  -                    "invalid base64 encoded data", exceptionThrown );
  +		assertFalse( "Exception was thrown when trying to decode " +
  +					"invalid base64 encoded data - RFC 2045 requires that all " +
  +					"non base64 character be discarded, an exception should not" +
  +					" have been thrown", exceptionThrown );
  +          
   
  -    }
  +	}
  +    
  +	public void testIgnoringNonBase64InDecode() throws Exception {
  +		assertEquals("The quick brown fox jumped over the lazy dogs.",new String(Base64.decodeBase64("VGhlIH@$#$@%F1aWN@#@#@@rIGJyb3duIGZve\n\r\t%#%#%#%CBqd##$#$W1wZWQgb3ZlciB0aGUgbGF6eSBkb2dzLg==".getBytes())));
  +	}
   
       public void testObjectDecodeWithInvalidParameter() throws Exception {
           boolean exceptionThrown = false;
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org


Mime
View raw message