blob: cf765e28810e77e47784b3e5a5f6a366e0c68cef [file] [log] [blame]
package org.bouncycastle.mime.test;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.security.SecureRandom;
import junit.framework.TestCase;
import org.bouncycastle.util.encoders.Base64;
public class Base64TransferEncodingTest
extends TestCase
{
private SecureRandom random = new SecureRandom();
/**
* Test the decoding of some base64 arranged in lines of
* 64 byte base 64 encoded rows terminated CRLF.
*
* @throws Exception
*/
public void testDecodeWellFormed()
throws Exception
{
byte[][] original = new byte[4][48];
ByteArrayOutputStream bos = new ByteArrayOutputStream();
//
// Create 4 lines of 64bytes of base64 encoded data.
//
for (int i = 0; i != original.length; i++)
{
byte[] row = original[i];
random.nextBytes(row);
bos.write(Base64.encode(row));
bos.write('\r');
bos.write('\n');
}
verifyDecode(original, bos);
}
/**
* Test decode without CR only LF.
*
* @throws Exception
*/
public void testDecodeWithoutCR()
throws Exception
{
byte[][] original = new byte[4][48];
ByteArrayOutputStream bos = new ByteArrayOutputStream();
//
// Create 4 lines of 64bytes of base64 encoded data.
//
for (int i = 0; i != original.length; i++)
{
byte[] row = original[i];
random.nextBytes(row);
bos.write(Base64.encode(row));
bos.write('\n');
}
verifyDecode(original, bos);
}
/**
* Test decode with long lines past the length in the spec.
*
* @throws Exception
*/
public void testDecodeLongLines()
throws Exception
{
byte[][] original = new byte[4][765];
ByteArrayOutputStream bos = new ByteArrayOutputStream();
//
// Create 4 lines of 1023 bytes of base64 encoded data.
//
for (int i = 0; i != original.length; i++)
{
byte[] row = original[i];
random.nextBytes(row);
bos.write(Base64.encode(row));
bos.write('\n');
}
verifyDecode(original, bos);
}
/**
* Test decode with long lines past the length in the spec.
*
* @throws Exception
*/
public void testExcessiveLongLine()
throws Exception
{
byte[][] original = new byte[4][766];
ByteArrayOutputStream bos = new ByteArrayOutputStream();
//
// Create 4 lines of 1023 bytes of base64 encoded data.
//
for (int i = 0; i != original.length; i++)
{
byte[] row = original[i];
random.nextBytes(row);
bos.write(Base64.encode(row));
bos.write('\n');
}
try
{
verifyDecode(original, bos);
}
catch (Exception ex)
{
TestCase.assertEquals("End of line of base64 not reached before line buffer overflow.", ex.getMessage());
}
}
/**
* Test decode of empty data.
*
* @throws Exception
*/
public void testEmpty()
throws Exception
{
// Assertions in verifyDecode()
verifyDecode(new byte[0][0], new ByteArrayOutputStream());
}
private void verifyDecode(byte[][] original, ByteArrayOutputStream bos)
throws IOException
{
// MimeParserInputStream source = new MimeParserInputStream(new ByteArrayInputStream(bos.toByteArray()), 1024);
// Base64TransferDecoder bte = new Base64TransferDecoder(source, 1024);
//
// for (byte[] row : original)
// {
// for (byte expected : row)
// {
// TestCase.assertEquals(expected & 0xFF, bte.read());
// }
// }
//
// TestCase.assertEquals(-1, bte.read());
}
/**
* This test causes the final line of base64 to not be a multiple of 64.
*
* @throws Exception
*/
public void testDecodeLengths()
throws Exception
{
byte[][] original = new byte[4][48];
original[original.length - 1] = new byte[22];
ByteArrayOutputStream bos = new ByteArrayOutputStream();
//
// Create 4 lines of 64bytes of base64 encoded data.
//
for (int i = 0; i != original.length; i++)
{
byte[] row = original[i];
random.nextBytes(row);
bos.write(Base64.encode(row));
bos.write('\r');
bos.write('\n');
}
verifyDecode(original, bos);
}
/**
* This test causes the final line of base64 to not be a multiple of 64.
*
* @throws Exception
*/
public void testPartialLineEnding()
throws Exception
{
byte[][] original = new byte[4][48];
original[original.length - 1] = new byte[22];
ByteArrayOutputStream bos = new ByteArrayOutputStream();
//
// Create 4 lines of 64bytes of base64 encoded data.
//
for (int i = 0; i != original.length; i++)
{
byte[] row = original[i];
random.nextBytes(row);
bos.write(Base64.encode(row));
bos.write('\r');
bos.write('\n');
}
verifyDecode(original, bos);
}
public void testMultilined()
throws Exception
{
String b64 = "MIAGCSqGSIb3DQEHA6CAMIACAQAxggFOMIIBSgIBADCBsjCBrDELMAkGA1UEBhMCQVQxEDAOBgNV\n" +
"BAgTB0F1c3RyaWExDzANBgNVBAcTBlZpZW5uYTEaMBgGA1UEChMRVGl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rycp5O\n" +
"zgAAAAAAAAAAAAA=";
byte[] data = Base64.decode(b64);
//
// MimeParserInputStream mpin = new MimeParserInputStream(new ByteArrayInputStream(b64.getBytes()), 1024);
// Base64TransferDecoder btd = new Base64TransferDecoder(mpin, 1024);
//
//
// for (int t = 0; t < data.length; t++)
// {
// TestCase.assertEquals("Position: " + t, data[t] & 0xFF, btd.read());
// }
//
// TestCase.assertEquals(-1, btd.read());
}
}