8176379: java.util.Base64 mime encoder behaves incorrectly if initialized with a line length of size 1-3
authorsherman
Wed, 31 Jan 2018 10:34:59 -0800
changeset 48689 e704f48d8277
parent 48688 08b5eb52ccfd
child 48690 374c63fbe652
8176379: java.util.Base64 mime encoder behaves incorrectly if initialized with a line length of size 1-3 Reviewed-by: rriggs, alanb, psandoz
src/java.base/share/classes/java/util/Base64.java
test/jdk/java/util/Base64/Base64GetEncoderTest.java
test/jdk/java/util/Base64/TestBase64.java
--- a/src/java.base/share/classes/java/util/Base64.java	Wed Jan 31 08:42:59 2018 -0800
+++ b/src/java.base/share/classes/java/util/Base64.java	Wed Jan 31 10:34:59 2018 -0800
@@ -116,8 +116,8 @@
      *
      * @param   lineLength
      *          the length of each output line (rounded down to nearest multiple
-     *          of 4). If {@code lineLength <= 0} the output will not be separated
-     *          in lines
+     *          of 4). If the rounded down line length is not a positive value,
+     *          the output will not be separated in lines
      * @param   lineSeparator
      *          the line separator for each output line
      *
@@ -135,10 +135,12 @@
                  throw new IllegalArgumentException(
                      "Illegal base64 line separator character 0x" + Integer.toString(b, 16));
          }
+         // round down to nearest multiple of 4
+         lineLength &= ~0b11;
          if (lineLength <= 0) {
              return Encoder.RFC4648;
          }
-         return new Encoder(false, lineSeparator, lineLength >> 2 << 2, true);
+         return new Encoder(false, lineSeparator, lineLength, true);
     }
 
     /**
--- a/test/jdk/java/util/Base64/Base64GetEncoderTest.java	Wed Jan 31 08:42:59 2018 -0800
+++ b/test/jdk/java/util/Base64/Base64GetEncoderTest.java	Wed Jan 31 10:34:59 2018 -0800
@@ -34,23 +34,23 @@
 
 /**
  * @test
- * @bug 8007799
+ * @bug 8007799 8176379
  * @summary test Encoder with linemax == 0, line separator should not appear in encoded data
  */
 
 public class Base64GetEncoderTest {
 
     public static void main(String args[]) throws Throwable {
-        final Base64.Encoder encoder = Base64.getMimeEncoder(0, "$$$".getBytes(US_ASCII));
 
-        testEncodeToString(encoder);
+        for (int maxlen = -4; maxlen < 4; maxlen++) {
+
+            final Base64.Encoder encoder = Base64.getMimeEncoder(maxlen, "$$$".getBytes(US_ASCII));
 
-        testWrapEncode1(encoder);
-
-        testEncodeToStringWithLongInputData(encoder);
-
-        testWrapEncode2(encoder);
-
+            testEncodeToString(encoder);
+            testWrapEncode1(encoder);
+            testEncodeToStringWithLongInputData(encoder);
+            testWrapEncode2(encoder);
+        }
     }
 
     private static void testWrapEncode2(final Base64.Encoder encoder)
--- a/test/jdk/java/util/Base64/TestBase64.java	Wed Jan 31 08:42:59 2018 -0800
+++ b/test/jdk/java/util/Base64/TestBase64.java	Wed Jan 31 10:34:59 2018 -0800
@@ -24,7 +24,7 @@
 /**
  * @test
  * @bug 4235519 8004212 8005394 8007298 8006295 8006315 8006530 8007379 8008925
- *      8014217 8025003 8026330 8028397 8129544 8165243
+ *      8014217 8025003 8026330 8028397 8129544 8165243 8176379
  * @summary tests java.util.Base64
  * @library /test/lib
  * @build jdk.test.lib.RandomFactory
@@ -78,6 +78,21 @@
                  numRuns, numBytes);
         }
 
+        // test mime case with < 4 length
+        for (int len = 0; len < 4; len++) {
+            test(Base64.getMimeEncoder(len, nl_1),
+                 Base64.getMimeDecoder(),
+                 numRuns, numBytes);
+
+            test(Base64.getMimeEncoder(len, nl_2),
+                 Base64.getMimeDecoder(),
+                 numRuns, numBytes);
+
+            test(Base64.getMimeEncoder(len, nl_3),
+                 Base64.getMimeDecoder(),
+                 numRuns, numBytes);
+        }
+
         testNull(Base64.getEncoder());
         testNull(Base64.getUrlEncoder());
         testNull(Base64.getMimeEncoder());