test/java/util/Base64/TestBase64.java

Print this page

        

@@ -20,11 +20,11 @@
  * or visit www.oracle.com if you need additional information or have any
  * questions.
  */
 
 /**
- * @test 4235519 8004212 8005394
+ * @test 4235519 8004212 8005394 8007298
  * @summary tests java.util.Base64
  */
 
 import java.io.ByteArrayInputStream;
 import java.io.ByteArrayOutputStream;

@@ -107,10 +107,13 @@
         checkIAE(new Runnable() { public void run() {
             Base64.getDecoder().decode(ByteBuffer.wrap(decoded), ByteBuffer.allocateDirect(1024)); }});
 
         // test return value from decode(ByteBuffer, ByteBuffer)
         testDecBufRet();
+
+        // test single-non-base64 character for mime decoding
+        testSingleNonBase64MimeDec();
     }
 
     private static sun.misc.BASE64Encoder sunmisc = new sun.misc.BASE64Encoder();
 
     private static void test(Base64.Encoder enc, Base64.Decoder dec,

@@ -354,10 +357,23 @@
             r.run();
             throw new RuntimeException("IAE is not thrown as expected");
         } catch (IllegalArgumentException iae) {}
     }
 
+    // single-non-base64-char should be ignored for mime decoding, but
+    // iae for basic decoding
+    private static void testSingleNonBase64MimeDec() throws Throwable {
+        for (String nonBase64 : new String[] {"#", "(", "!", "\\", "-", "_"}) {
+            if (Base64.getMimeDecoder().decode(nonBase64).length != 0) {
+                throw new RuntimeException("non-base64 char is not ignored");
+            }
+            try {
+                Base64.getDecoder().decode(nonBase64);
+                throw new RuntimeException("No IAE for single non-base64 char");
+            } catch (IllegalArgumentException iae) {}
+        } 
+    }
 
     private static void testDecBufRet() throws Throwable {
         Random rnd = new java.util.Random();
         Base64.Encoder encoder = Base64.getEncoder();
         Base64.Decoder decoder = Base64.getDecoder();