Methods Summary |
---|
private void | checkBase64Structure(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 void | setUp()
super.setUp();
|
protected void | tearDown()
super.tearDown();
|
public void | testLineLength111()Repeat the above tests with 2x line lengths
byte[] out = Base64.encodeBase64Chunked(getByteArray(111));
checkBase64Structure(out, 2);
|
public void | testLineLength112()
byte[] out = Base64.encodeBase64Chunked(getByteArray(112));
checkBase64Structure(out, 2);
|
public void | testLineLength113()
byte[] out = Base64.encodeBase64Chunked(getByteArray(113));
checkBase64Structure(out, 2);
|
public void | testLineLength114()
byte[] out = Base64.encodeBase64Chunked(getByteArray(114));
checkBase64Structure(out, 2);
|
public void | testLineLength115()
byte[] out = Base64.encodeBase64Chunked(getByteArray(115));
checkBase64Structure(out, 3);
|
public void | testLineLength54()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 void | testLineLength55()
byte[] out = Base64.encodeBase64Chunked(getByteArray(55));
checkBase64Structure(out, 1);
|
public void | testLineLength56()
byte[] out = Base64.encodeBase64Chunked(getByteArray(56));
checkBase64Structure(out, 1);
|
public void | testLineLength57()
byte[] out = Base64.encodeBase64Chunked(getByteArray(57));
checkBase64Structure(out, 1);
|
public void | testLineLength58()
byte[] out = Base64.encodeBase64Chunked(getByteArray(58));
checkBase64Structure(out, 2);
|
public void | testLineLength59()
byte[] out = Base64.encodeBase64Chunked(getByteArray(59));
checkBase64Structure(out, 2);
|