8176379: java.util.Base64 mime encoder behaves incorrectly if initialized with a line length of size 1-3

Reviewed-by: rriggs, alanb, psandoz
This commit is contained in:
Xueming Shen 2018-01-31 10:34:59 -08:00
parent 46d5554295
commit 2f7bcc872f
3 changed files with 29 additions and 12 deletions

View File

@ -116,8 +116,8 @@ public class Base64 {
*
* @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 @@ public class Base64 {
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);
}
/**

View File

@ -34,23 +34,23 @@ import static java.nio.charset.StandardCharsets.US_ASCII;
/**
* @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++) {
testWrapEncode1(encoder);
testEncodeToStringWithLongInputData(encoder);
testWrapEncode2(encoder);
final Base64.Encoder encoder = Base64.getMimeEncoder(maxlen, "$$$".getBytes(US_ASCII));
testEncodeToString(encoder);
testWrapEncode1(encoder);
testEncodeToStringWithLongInputData(encoder);
testWrapEncode2(encoder);
}
}
private static void testWrapEncode2(final Base64.Encoder encoder)

View File

@ -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 @@ public class TestBase64 {
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());