View Javadoc

    1   /*
    2    * Licensed to the Apache Software Foundation (ASF) under one or more
    3    * contributor license agreements.  See the NOTICE file distributed with
    4    * this work for additional information regarding copyright ownership.
    5    * The ASF licenses this file to You under the Apache License, Version 2.0
    6    * (the "License"); you may not use this file except in compliance with
    7    * the License.  You may obtain a copy of the License at
    8    *
    9    *      http://www.apache.org/licenses/LICENSE-2.0
    10   *
    11   * Unless required by applicable law or agreed to in writing, software
    12   * distributed under the License is distributed on an "AS IS" BASIS,
    13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    14   * See the License for the specific language governing permissions and
    15   * limitations under the License.
    16   */
    17  package org.apache.commons.fileupload.util.mime;
    18
    19  import static org.junit.Assert.assertArrayEquals;
    20  import static org.junit.Assert.assertTrue;
    21  import static org.junit.Assert.fail;
    22
    23  import java.io.ByteArrayOutputStream;
    24  import java.io.IOException;
    25  import java.io.UnsupportedEncodingException;
    26
    27  import org.junit.Test;
    28
    29  /**
    30   * @since 1.3
    31   */
    32  public final class Base64DecoderTestCase {
    33
    34      private static final String US_ASCII_CHARSET = "US-ASCII";
    35
    36      /**
    37       * Tests RFC 4648 section 10 test vectors.
    38       * <ul>
    39       * <li>BASE64("") = ""</li>
    40       * <li>BASE64("f") = "Zg=="</li>
    41       * <li>BASE64("fo") = "Zm8="</li>
    42       * <li>BASE64("foo") = "Zm9v"</li>
    43       * <li>BASE64("foob") = "Zm9vYg=="</li>
    44       * <li>BASE64("fooba") = "Zm9vYmE="</li>
    45       * <li>BASE64("foobar") = "Zm9vYmFy"</li>
    46       * </ul>
    47       *
    48       * @see <a href="http://tools.ietf.org/html/rfc4648">http://tools.ietf.org/html/rfc4648</a>
    49       */
    50      @Test
    51      public void rfc4648Section10Decode() throws Exception {
    52          assertEncoded("", "");
    53          assertEncoded("f", "Zg==");
    54          assertEncoded("fo", "Zm8=");
    55          assertEncoded("foo", "Zm9v");
    56          assertEncoded("foob", "Zm9vYg==");
    57          assertEncoded("fooba", "Zm9vYmE=");
    58          assertEncoded("foobar", "Zm9vYmFy");
    59      }
    60
    61      /**
    62       * Test our decode with pad character in the middle.
    63       * Continues provided that the padding is in the correct place,
    64       * i.e. concatenated valid strings decode OK.
    65       */
    66      @Test
    67      public void decodeWithInnerPad() throws Exception {
    68          assertEncoded("Hello WorldHello World", "SGVsbG8gV29ybGQ=SGVsbG8gV29ybGQ=");
    69      }
    70
    71      /**
    72       * Ignores non-BASE64 bytes.
    73       */
    74      @Test
    75      public void nonBase64Bytes() throws Exception {
    76          assertEncoded("Hello World", "S?G!V%sbG 8g\rV\t\n29ybGQ*=");
    77      }
    78
    79      @Test(expected = IOException.class)
    80      public void truncatedString() throws Exception {
    81          final byte[] x = new byte[]{'n'};
    82          Base64Decoder.decode(x, new ByteArrayOutputStream());
    83      }
    84
    85      @Test
    86      public void decodeTrailingJunk() throws Exception {
    87          assertEncoded("foobar", "Zm9vYmFy!!!");
    88      }
    89
    90      // If there are valid trailing Base64 chars, complain
    91      @Test
    92      public void decodeTrailing1() throws Exception {
    93          assertIOException("truncated", "Zm9vYmFy1");
    94      }
    95
    96      // If there are valid trailing Base64 chars, complain
    97      @Test
    98      public void decodeTrailing2() throws Exception {
    99          assertIOException("truncated", "Zm9vYmFy12");
    100     }
    101
    102     // If there are valid trailing Base64 chars, complain
    103     @Test
    104     public void decodeTrailing3() throws Exception {
    105         assertIOException("truncated", "Zm9vYmFy123");
    106     }
    107
    108     @Test
    109     public void badPadding() throws Exception {
    110         assertIOException("incorrect padding, 4th byte", "Zg=a");
    111     }
    112
    113     @Test
    114     public void badPaddingLeading1() throws Exception {
    115         assertIOException("incorrect padding, first two bytes cannot be padding", "=A==");
    116     }
    117
    118     @Test
    119     public void badPaddingLeading2() throws Exception {
    120         assertIOException("incorrect padding, first two bytes cannot be padding", "====");
    121     }
    122
    123     // This input causes java.lang.ArrayIndexOutOfBoundsException: 1
    124     // in the Java 6 method DatatypeConverter.parseBase64Binary(String)
    125     // currently reported as truncated (the last chunk consists just of '=')
    126     @Test
    127     public void badLength() throws Exception {
    128         assertIOException("truncated", "Zm8==");
    129     }
    130
    131     // These inputs cause java.lang.ArrayIndexOutOfBoundsException
    132     // in the Java 6 method DatatypeConverter.parseBase64Binary(String)
    133     // The non-ASCII characters should just be ignored
    134     @Test
    135     public void nonASCIIcharacter() throws Exception {
    136         assertEncoded("f","Zg=À="); // A-grave
    137         assertEncoded("f","Zg=\u0100=");
    138     }
    139
    140     private static void assertEncoded(String clearText, String encoded) throws Exception {
    141         byte[] expected = clearText.getBytes(US_ASCII_CHARSET);
    142
    143         ByteArrayOutputStream out = new ByteArrayOutputStream(encoded.length());
    144         byte[] encodedData = encoded.getBytes(US_ASCII_CHARSET);
    145         Base64Decoder.decode(encodedData, out);
    146         byte[] actual = out.toByteArray();
    147
    148         assertArrayEquals(expected, actual);
    149     }
    150
    151     private static void assertIOException(String messageText, String encoded) throws UnsupportedEncodingException {
    152         ByteArrayOutputStream out = new ByteArrayOutputStream(encoded.length());
    153         byte[] encodedData = encoded.getBytes(US_ASCII_CHARSET);
    154         try {
    155             Base64Decoder.decode(encodedData, out);
    156             fail("Expected IOException");
    157         } catch (IOException e) {
    158             String em = e.getMessage();
    159             assertTrue("Expected to find " + messageText + " in '" + em + "'",em.contains(messageText));
    160         }
    161     }
    162
    163 }