FileDocCategorySizeDatePackage
Base64Test.javaAPI DocAndroid 1.5 API5562Wed May 06 22:42:46 BST 2009com.android.email.codec.binary

Base64Test

public class Base64Test extends TestCase
A series of tests of the Base64 encoder.

Fields Summary
Constructors Summary
Methods Summary
private voidcheckBase64Structure(byte[] buffer, int expectedChunks)
Validate that base64 output is structurally sound. Does not independently confirm that the actual encoding is valid.

        
        // outer loop - divide into chunks
        int chunkCount = 0;
        int chunkStart;
        int nextChunkStart = 0;
        int limit = buffer.length;
        while (nextChunkStart < limit) {
            chunkStart = -1;
            int chunkEnd;
            for (chunkEnd = nextChunkStart; chunkEnd < limit; ++chunkEnd) {
                assertFalse("nulls in chunk", buffer[chunkEnd] == 0);
                if (buffer[chunkEnd] == '\r") {
                    assertTrue(buffer[chunkEnd+1] == '\n");
                    chunkStart = nextChunkStart;
                    break;
                }
                if (chunkEnd == limit) {
                    chunkStart = nextChunkStart;
                    break;
                }
            }
            chunkCount++;
            nextChunkStart = chunkEnd + 2;
            assertTrue("chunk not found", chunkStart >= 0);
            
            // At this point we have a single chunk from chunkStart to chunkEnd
            // And we can analyze it for structural correctness
            int chunkLen = chunkEnd - chunkStart;
            
            // Max chunk length
            assertTrue("chunk length <= 76", chunkLen <= 76);
            
            // Multiple of 4 (every 3 bytes of source -> 4 bytes of output)
            assertEquals("chunk length mod 4", 0, chunkLen % 4);
            
            // 0, 1 or 2 '=' at the end
            boolean lastEquals1 = buffer[chunkEnd-1] == '=";
            boolean lastEquals2 = buffer[chunkEnd-2] == '=";
            boolean lastEquals3 = buffer[chunkEnd-3] == '=";
            
            assertTrue("trailing equals",
                    (!lastEquals1 && !lastEquals2) ||        // 0
                    (lastEquals1 && !lastEquals2) ||         // or 1
                    (lastEquals1 && lastEquals2));           // or 2
        }
        
        assertEquals("total chunk count", expectedChunks, chunkCount);
    
private byte[]getByteArray(int size)
Generate a test sequence of a given length.

        byte[] result = new byte[size];
        byte fillChar = '1";
        for (int i = 0; i < size; ++i) {
            result[i] = fillChar++;
            if (fillChar > '9") {
                fillChar = '0";
            }
        }
        return result;
    
protected voidsetUp()

        super.setUp();
    
protected voidtearDown()

        super.tearDown();
    
public voidtestLineLength111()
Repeat the above tests with 2x line lengths

        byte[] out = Base64.encodeBase64Chunked(getByteArray(111));
        checkBase64Structure(out, 2);
    
public voidtestLineLength112()

        byte[] out = Base64.encodeBase64Chunked(getByteArray(112));
        checkBase64Structure(out, 2);
    
public voidtestLineLength113()

        byte[] out = Base64.encodeBase64Chunked(getByteArray(113));
        checkBase64Structure(out, 2);
    
public voidtestLineLength114()

        byte[] out = Base64.encodeBase64Chunked(getByteArray(114));
        checkBase64Structure(out, 2);
    
public voidtestLineLength115()

        byte[] out = Base64.encodeBase64Chunked(getByteArray(115));
        checkBase64Structure(out, 3);
    
public voidtestLineLength54()
Looking for issues with line length and trailing zeros. The code we're modeling is in mail.internet.TextBody: byte[] bytes = mBody.getBytes("UTF-8"); out.write(Base64.encodeBase64Chunked(bytes));

        byte[] out = Base64.encodeBase64Chunked(getByteArray(54));
        checkBase64Structure(out, 1);
    
public voidtestLineLength55()

        byte[] out = Base64.encodeBase64Chunked(getByteArray(55));
        checkBase64Structure(out, 1);
    
public voidtestLineLength56()

        byte[] out = Base64.encodeBase64Chunked(getByteArray(56));
        checkBase64Structure(out, 1);
    
public voidtestLineLength57()

        byte[] out = Base64.encodeBase64Chunked(getByteArray(57));
        checkBase64Structure(out, 1);
    
public voidtestLineLength58()

        byte[] out = Base64.encodeBase64Chunked(getByteArray(58));
        checkBase64Structure(out, 2);
    
public voidtestLineLength59()

        byte[] out = Base64.encodeBase64Chunked(getByteArray(59));
        checkBase64Structure(out, 2);