Add tests for DexWriter (and fixes to DexWriter, to make said tests pass)

This commit is contained in:
Ben Gruver 2012-12-28 16:37:41 -08:00
parent 9a90c5560c
commit 7dff6ada5e
5 changed files with 1101 additions and 16 deletions

View File

@ -176,7 +176,7 @@ public class DexWriter extends BufferedOutputStream {
} }
} }
tempBuf[index++] = (byte)value; tempBuf[index++] = (byte)value;
writeEncodedValueHeader(valueType, index); writeEncodedValueHeader(valueType, index-1);
write(tempBuf, 0, index); write(tempBuf, 0, index);
} }
@ -194,7 +194,7 @@ public class DexWriter extends BufferedOutputStream {
} }
} }
tempBuf[index++] = (byte)value; tempBuf[index++] = (byte)value;
writeEncodedValueHeader(valueType, index); writeEncodedValueHeader(valueType, index-1);
write(tempBuf, 0, index); write(tempBuf, 0, index);
} }
@ -202,34 +202,44 @@ public class DexWriter extends BufferedOutputStream {
int index = 0; int index = 0;
do { do {
tempBuf[index++] = (byte)value; tempBuf[index++] = (byte)value;
value >>= 8; value >>>= 8;
} while (value != 0); } while (value != 0);
writeEncodedValueHeader(valueType, index); writeEncodedValueHeader(valueType, index-1);
write(tempBuf, 0, index); write(tempBuf, 0, index);
} }
public void writeEncodedFloat(int valueType, float value) throws IOException { public void writeEncodedFloat(int valueType, float value) throws IOException {
int intValue = Float.floatToRawIntBits(value); writeRightZeroExtendedInt(valueType, Float.floatToRawIntBits(value));
}
protected void writeRightZeroExtendedInt(int valueType, int value) throws IOException {
int index = 3; int index = 3;
do { do {
tempBuf[index--] = (byte)((intValue & 0xFF000000) >>> 24); tempBuf[index--] = (byte)((value & 0xFF000000) >>> 24);
intValue <<= 8; value <<= 8;
} while (intValue != 0); } while (value != 0);
writeEncodedValueHeader(valueType, 4-index);
write(tempBuf, index+1, 4-index); int firstElement = index+1;
int encodedLength = 4-firstElement;
writeEncodedValueHeader(valueType, encodedLength - 1);
write(tempBuf, firstElement, encodedLength);
} }
public void writeEncodedDouble(int valueType, double value) throws IOException { public void writeEncodedDouble(int valueType, double value) throws IOException {
long longValue = Double.doubleToRawLongBits(value); writeRightZeroExtendedLong(valueType, Double.doubleToRawLongBits(value));
}
protected void writeRightZeroExtendedLong(int valueType, long value) throws IOException {
int index = 7; int index = 7;
do { do {
tempBuf[index--] = (byte)((longValue & 0xFF00000000000000L) >>> 56); tempBuf[index--] = (byte)((value & 0xFF00000000000000L) >>> 56);
longValue <<= 8; value <<= 8;
} while (longValue != 0); } while (value != 0);
writeEncodedValueHeader(valueType, 7-index);
write(tempBuf, index+1, 7-index); int firstElement = index+1;
int encodedLength = 8-firstElement;
writeEncodedValueHeader(valueType, encodedLength - 1);
write(tempBuf, firstElement, encodedLength);
} }
public void writeString(String string) throws IOException { public void writeString(String string) throws IOException {

View File

@ -0,0 +1,251 @@
/*
* Copyright 2012, Google Inc.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following disclaimer
* in the documentation and/or other materials provided with the
* distribution.
* * Neither the name of Google Inc. nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package org.jf.dexlib2.writer;
import junit.framework.Assert;
import org.jf.util.NakedByteArrayOutputStream;
import org.junit.Test;
import java.io.IOException;
public class DexWriterSleb128Test {
private NakedByteArrayOutputStream output = new NakedByteArrayOutputStream();
private int startPosition;
private DexWriter writer;
public void setup() throws IOException {
output.reset();
startPosition = 123;
int bufferSize = 256;
writer = new DexWriter(output, startPosition, bufferSize);
}
@Test
public void testSleb128() throws IOException {
performTest(0x0, new byte[]{0x0, 0x11}, 1);
performTest(0x1, new byte[]{0x1, 0x11}, 1);
performTest(0x3f, new byte[]{0x3f, 0x11}, 1);
performTest(0xffffffc0, new byte[]{0x40, 0x11}, 1);
performTest(0xfffffff0, new byte[]{0x70, 0x11}, 1);
performTest(0xffffffff, new byte[]{0x7f, 0x11}, 1);
performTest(0x80, new byte[]{(byte)0x80, 0x1, 0x11}, 2);
performTest(0x100, new byte[]{(byte)0x80, 0x2, 0x11}, 2);
performTest(0x800, new byte[]{(byte)0x80, 0x10, 0x11}, 2);
performTest(0x1f80, new byte[]{(byte)0x80, 0x3f, 0x11}, 2);
performTest(0xffffe000, new byte[]{(byte)0x80, 0x40, 0x11}, 2);
performTest(0xffffe080, new byte[]{(byte)0x80, 0x41, 0x11}, 2);
performTest(0xfffff800, new byte[]{(byte)0x80, 0x70, 0x11}, 2);
performTest(0xffffff80, new byte[]{(byte)0x80, 0x7f, 0x11}, 2);
performTest(0xff, new byte[]{(byte)0xff, 0x1, 0x11}, 2);
performTest(0x17f, new byte[]{(byte)0xff, 0x2, 0x11}, 2);
performTest(0x87f, new byte[]{(byte)0xff, 0x10, 0x11}, 2);
performTest(0x1fff, new byte[]{(byte)0xff, 0x3f, 0x11}, 2);
performTest(0xffffe07f, new byte[]{(byte)0xff, 0x40, 0x11}, 2);
performTest(0xffffe0ff, new byte[]{(byte)0xff, 0x41, 0x11}, 2);
performTest(0xfffff87f, new byte[]{(byte)0xff, 0x70, 0x11}, 2);
performTest(0x4000, new byte[]{(byte)0x80, (byte)0x80, 0x1, 0x11}, 3);
performTest(0x8000, new byte[]{(byte)0x80, (byte)0x80, 0x2, 0x11}, 3);
performTest(0x40000, new byte[]{(byte)0x80, (byte)0x80, 0x10, 0x11}, 3);
performTest(0xfc000, new byte[]{(byte)0x80, (byte)0x80, 0x3f, 0x11}, 3);
performTest(0xfff00000, new byte[]{(byte)0x80, (byte)0x80, 0x40, 0x11}, 3);
performTest(0xfff04000, new byte[]{(byte)0x80, (byte)0x80, 0x41, 0x11}, 3);
performTest(0xfffc0000, new byte[]{(byte)0x80, (byte)0x80, 0x70, 0x11}, 3);
performTest(0xffffc000, new byte[]{(byte)0x80, (byte)0x80, 0x7f, 0x11}, 3);
performTest(0x7fff, new byte[]{(byte)0xff, (byte)0xff, 0x1, 0x11}, 3);
performTest(0xbfff, new byte[]{(byte)0xff, (byte)0xff, 0x2, 0x11}, 3);
performTest(0x43fff, new byte[]{(byte)0xff, (byte)0xff, 0x10, 0x11}, 3);
performTest(0xfffff, new byte[]{(byte)0xff, (byte)0xff, 0x3f, 0x11}, 3);
performTest(0xfff03fff, new byte[]{(byte)0xff, (byte)0xff, 0x40, 0x11}, 3);
performTest(0xfff07fff, new byte[]{(byte)0xff, (byte)0xff, 0x41, 0x11}, 3);
performTest(0xfffc3fff, new byte[]{(byte)0xff, (byte)0xff, 0x70, 0x11}, 3);
performTest(0x200000, new byte[]{(byte)0x80, (byte)0x80, (byte)0x80, 0x1, 0x11}, 4);
performTest(0x400000, new byte[]{(byte)0x80, (byte)0x80, (byte)0x80, 0x2, 0x11}, 4);
performTest(0x2000000, new byte[]{(byte)0x80, (byte)0x80, (byte)0x80, 0x10, 0x11}, 4);
performTest(0x7e00000, new byte[]{(byte)0x80, (byte)0x80, (byte)0x80, 0x3f, 0x11}, 4);
performTest(0xf8000000, new byte[]{(byte)0x80, (byte)0x80, (byte)0x80, 0x40, 0x11}, 4);
performTest(0xf8200000, new byte[]{(byte)0x80, (byte)0x80, (byte)0x80, 0x41, 0x11}, 4);
performTest(0xfe000000, new byte[]{(byte)0x80, (byte)0x80, (byte)0x80, 0x70, 0x11}, 4);
performTest(0xffe00000, new byte[]{(byte)0x80, (byte)0x80, (byte)0x80, 0x7f, 0x11}, 4);
performTest(0x3fffff, new byte[]{(byte)0xff, (byte)0xff, (byte)0xff, 0x1, 0x11}, 4);
performTest(0x5fffff, new byte[]{(byte)0xff, (byte)0xff, (byte)0xff, 0x2, 0x11}, 4);
performTest(0x21fffff, new byte[]{(byte)0xff, (byte)0xff, (byte)0xff, 0x10, 0x11}, 4);
performTest(0x7ffffff, new byte[]{(byte)0xff, (byte)0xff, (byte)0xff, 0x3f, 0x11}, 4);
performTest(0xf81fffff, new byte[]{(byte)0xff, (byte)0xff, (byte)0xff, 0x40, 0x11}, 4);
performTest(0xf83fffff, new byte[]{(byte)0xff, (byte)0xff, (byte)0xff, 0x41, 0x11}, 4);
performTest(0xfe1fffff, new byte[]{(byte)0xff, (byte)0xff, (byte)0xff, 0x70, 0x11}, 4);
performTest(0x10000000, new byte[]{(byte)0x80, (byte)0x80, (byte)0x80, (byte)0x80, 0x1, 0x11}, 5);
performTest(0x20000000, new byte[]{(byte)0x80, (byte)0x80, (byte)0x80, (byte)0x80, 0x2, 0x11}, 5);
performTest(0x70000000, new byte[]{(byte)0x80, (byte)0x80, (byte)0x80, (byte)0x80, 0x7, 0x11}, 5);
performTest(0x80000000, new byte[]{(byte)0x80, (byte)0x80, (byte)0x80, (byte)0x80, 0x8, 0x11}, 5);
performTest(0xe0000000, new byte[]{(byte)0x80, (byte)0x80, (byte)0x80, (byte)0x80, 0xe, 0x11}, 5);
performTest(0xf0000000, new byte[]{(byte)0x80, (byte)0x80, (byte)0x80, (byte)0x80, 0xf, 0x11}, 5);
performTest(0x1fffffff, new byte[]{(byte)0xff, (byte)0xff, (byte)0xff, (byte)0xff, 0x1, 0x11}, 5);
performTest(0x2fffffff, new byte[]{(byte)0xff, (byte)0xff, (byte)0xff, (byte)0xff, 0x2, 0x11}, 5);
performTest(0x7fffffff, new byte[]{(byte)0xff, (byte)0xff, (byte)0xff, (byte)0xff, 0x7, 0x11}, 5);
performTest(0x8fffffff, new byte[]{(byte)0xff, (byte)0xff, (byte)0xff, (byte)0xff, 0x8, 0x11}, 5);
performTest(0xefffffff, new byte[]{(byte)0xff, (byte)0xff, (byte)0xff, (byte)0xff, 0xe, 0x11}, 5);
performTest(0x8197d2, new byte[]{(byte)0xd2, (byte)0xaf, (byte)0x86, 0x4});
performTest(0x3cc8eb78, new byte[]{(byte)0xf8, (byte)0xd6, (byte)0xa3, (byte)0xe6, 0x3});
performTest(0x51307f32, new byte[]{(byte)0xb2, (byte)0xfe, (byte)0xc1, (byte)0x89, 0x5});
performTest(0x8893, new byte[]{(byte)0x93, (byte)0x91, 0x2});
performTest(0x80fb, new byte[]{(byte)0xfb, (byte)0x81, 0x2});
performTest(0x3d, new byte[]{0x3d});
performTest(0x987c, new byte[]{(byte)0xfc, (byte)0xb0, 0x2});
performTest(0x5b2478, new byte[]{(byte)0xf8, (byte)0xc8, (byte)0xec, 0x2});
performTest(0x65350ed9, new byte[]{(byte)0xd9, (byte)0x9d, (byte)0xd4, (byte)0xa9, 0x6});
performTest(0x3e, new byte[]{0x3e});
performTest(0x7b1e, new byte[]{(byte)0x9e, (byte)0xf6, 0x1});
performTest(0xb5, new byte[]{(byte)0xb5, 0x1});
performTest(0x96, new byte[]{(byte)0x96, 0x1});
performTest(0xa1, new byte[]{(byte)0xa1, 0x1});
performTest(0x4d50a85d, new byte[]{(byte)0xdd, (byte)0xd0, (byte)0xc2, (byte)0xea, 0x4});
performTest(0xc419, new byte[]{(byte)0x99, (byte)0x88, 0x3});
performTest(0xcf34, new byte[]{(byte)0xb4, (byte)0x9e, 0x3});
performTest(0x527d, new byte[]{(byte)0xfd, (byte)0xa4, 0x1});
performTest(0x5a2894, new byte[]{(byte)0x94, (byte)0xd1, (byte)0xe8, 0x2});
performTest(0xa6, new byte[]{(byte)0xa6, 0x1});
performTest(0x3e05, new byte[]{(byte)0x85, (byte)0xfc, 0x0});
performTest(0x5f, new byte[]{(byte)0xdf, 0x0});
performTest(0xe2d9af, new byte[]{(byte)0xaf, (byte)0xb3, (byte)0x8b, 0x7});
performTest(0xa853fe14, new byte[]{(byte)0x94, (byte)0xfc, (byte)0xcf, (byte)0xc2, 0xa});
performTest(0xa853fe14, new byte[]{(byte)0x94, (byte)0xfc, (byte)0xcf, (byte)0xc2, 0x7a});
performTest(0x117de731, new byte[]{(byte)0xb1, (byte)0xce, (byte)0xf7, (byte)0x8b, 0x1});
performTest(0xb7c9, new byte[]{(byte)0xc9, (byte)0xef, 0x2});
performTest(0xb1, new byte[]{(byte)0xb1, 0x1});
performTest(0x4f194d, new byte[]{(byte)0xcd, (byte)0xb2, (byte)0xbc, 0x2});
performTest(0x8d5733, new byte[]{(byte)0xb3, (byte)0xae, (byte)0xb5, 0x4});
performTest(0x2824e9ae, new byte[]{(byte)0xae, (byte)0xd3, (byte)0x93, (byte)0xc1, 0x2});
performTest(0x792e, new byte[]{(byte)0xae, (byte)0xf2, 0x1});
performTest(0xadef, new byte[]{(byte)0xef, (byte)0xdb, 0x2});
performTest(0x5c, new byte[]{(byte)0xdc, 0x0});
performTest(0x14f9ccf8, new byte[]{(byte)0xf8, (byte)0x99, (byte)0xe7, (byte)0xa7, 0x1});
performTest(0xd1, new byte[]{(byte)0xd1, 0x1});
performTest(0xba787ecd, new byte[]{(byte)0xcd, (byte)0xfd, (byte)0xe1, (byte)0xd3, 0x7b});
performTest(0x4f, new byte[]{(byte)0xcf, 0x0});
performTest(0xfb03, new byte[]{(byte)0x83, (byte)0xf6, 0x3});
performTest(0xee3f7cd8, new byte[]{(byte)0xd8, (byte)0xf9, (byte)0xfd, (byte)0xf1, 0x7e});
performTest(0x9a6e, new byte[]{(byte)0xee, (byte)0xb4, 0x2});
performTest(0x8f0983, new byte[]{(byte)0x83, (byte)0x93, (byte)0xbc, 0x4});
performTest(0x3a00e01f, new byte[]{(byte)0x9f, (byte)0xc0, (byte)0x83, (byte)0xd0, 0x3});
performTest(0x7f532d93, new byte[]{(byte)0x93, (byte)0xdb, (byte)0xcc, (byte)0xfa, 0x7});
performTest(0x179d8d, new byte[]{(byte)0x8d, (byte)0xbb, (byte)0xde, 0x0});
performTest(0xfc5, new byte[]{(byte)0xc5, 0x1f});
performTest(0x11, new byte[]{0x11});
performTest(0xc9b53e8, new byte[]{(byte)0xe8, (byte)0xa7, (byte)0xed, (byte)0xe4, 0x0});
performTest(0x97, new byte[]{(byte)0x97, 0x1});
performTest(0x52b3, new byte[]{(byte)0xb3, (byte)0xa5, 0x1});
performTest(0x92, new byte[]{(byte)0x92, 0x1});
performTest(0xd2, new byte[]{(byte)0xd2, 0x1});
performTest(0x13d330, new byte[]{(byte)0xb0, (byte)0xa6, (byte)0xcf, 0x0});
performTest(0x672f41, new byte[]{(byte)0xc1, (byte)0xde, (byte)0x9c, 0x3});
performTest(0xcf, new byte[]{(byte)0xcf, 0x1});
performTest(0x54ddb6dd, new byte[]{(byte)0xdd, (byte)0xed, (byte)0xf6, (byte)0xa6, 0x5});
performTest(0x7ebcae, new byte[]{(byte)0xae, (byte)0xf9, (byte)0xfa, 0x3});
performTest(0x38, new byte[]{0x38});
performTest(0x8118f4e7, new byte[]{(byte)0xe7, (byte)0xe9, (byte)0xe3, (byte)0x88, 0x78});
performTest(0xac, new byte[]{(byte)0xac, 0x1});
performTest(0xab309c, new byte[]{(byte)0x9c, (byte)0xe1, (byte)0xac, 0x5});
performTest(0x1bf9b2, new byte[]{(byte)0xb2, (byte)0xf3, (byte)0xef, 0x0});
performTest(0x8b3c70, new byte[]{(byte)0xf0, (byte)0xf8, (byte)0xac, 0x4});
performTest(0x7774, new byte[]{(byte)0xf4, (byte)0xee, 0x1});
performTest(0x33e839, new byte[]{(byte)0xb9, (byte)0xd0, (byte)0xcf, 0x1});
performTest(0x84d655a0, new byte[]{(byte)0xa0, (byte)0xab, (byte)0xd9, (byte)0xa6, 0x78});
performTest(0xf3543ef3, new byte[]{(byte)0xf3, (byte)0xfd, (byte)0xd0, (byte)0x9a, 0x7f});
performTest(0x1d777e, new byte[]{(byte)0xfe, (byte)0xee, (byte)0xf5, 0x0});
performTest(0xf7, new byte[]{(byte)0xf7, 0x1});
performTest(0x2444, new byte[]{(byte)0xc4, (byte)0xc8, 0x0});
performTest(0x536b, new byte[]{(byte)0xeb, (byte)0xa6, 0x1});
performTest(0xa8, new byte[]{(byte)0xa8, 0x1});
performTest(0xdbfc, new byte[]{(byte)0xfc, (byte)0xb7, 0x3});
performTest(0xe66db7, new byte[]{(byte)0xb7, (byte)0xdb, (byte)0x99, 0x7});
performTest(0xb7ca, new byte[]{(byte)0xca, (byte)0xef, 0x2});
performTest(0xe807d0e5, new byte[]{(byte)0xe5, (byte)0xa1, (byte)0x9f, (byte)0xc0, 0x7e});
performTest(0x6a4, new byte[]{(byte)0xa4, 0xd});
performTest(0x64, new byte[]{(byte)0xe4, 0x0});
performTest(0xf3fb75, new byte[]{(byte)0xf5, (byte)0xf6, (byte)0xcf, 0x7});
performTest(0xb72cb6b9, new byte[]{(byte)0xb9, (byte)0xed, (byte)0xb2, (byte)0xb9, 0x7b});
performTest(0xfd, new byte[]{(byte)0xfd, 0x1});
performTest(0xb48b, new byte[]{(byte)0x8b, (byte)0xe9, 0x2});
performTest(0x39c3, new byte[]{(byte)0xc3, (byte)0xf3, 0x0});
performTest(0x12b8afbd, new byte[]{(byte)0xbd, (byte)0xdf, (byte)0xe2, (byte)0x95, 0x1});
performTest(0x56f149, new byte[]{(byte)0xc9, (byte)0xe2, (byte)0xdb, 0x2});
performTest(0xbf, new byte[]{(byte)0xbf, 0x1});
performTest(0x3ac72481, new byte[]{(byte)0x81, (byte)0xc9, (byte)0x9c, (byte)0xd6, 0x3});
performTest(0xb69ca721, new byte[]{(byte)0xa1, (byte)0xce, (byte)0xf2, (byte)0xb4, 0x7b});
performTest(0x2380, new byte[]{(byte)0x80, (byte)0xc7, 0x0});
performTest(0x656268, new byte[]{(byte)0xe8, (byte)0xc4, (byte)0x95, 0x3});
performTest(0x71, new byte[]{(byte)0xf1, 0x0});
performTest(0xf06425, new byte[]{(byte)0xa5, (byte)0xc8, (byte)0xc1, 0x7});
performTest(0xb587cb, new byte[]{(byte)0xcb, (byte)0x8f, (byte)0xd6, 0x5});
performTest(0x8742, new byte[]{(byte)0xc2, (byte)0x8e, 0x2});
performTest(0xc6, new byte[]{(byte)0xc6, 0x1});
performTest(0xee62789f, new byte[]{(byte)0x9f, (byte)0xf1, (byte)0x89, (byte)0xf3, 0x7e});
performTest(0x470a, new byte[]{(byte)0x8a, (byte)0x8e, 0x1});
performTest(0x11ef5cdc, new byte[]{(byte)0xdc, (byte)0xb9, (byte)0xbd, (byte)0x8f, 0x1});
performTest(0xc44ea9, new byte[]{(byte)0xa9, (byte)0x9d, (byte)0x91, 0x6});
performTest(0x94477f78, new byte[]{(byte)0xf8, (byte)0xfe, (byte)0x9d, (byte)0xa2, 0x79});
performTest(0xe47a0b4f, new byte[]{(byte)0xcf, (byte)0x96, (byte)0xe8, (byte)0xa3, 0x7e});
}
private void performTest(int integerValue, byte[] encodedValue) throws IOException {
performTest(integerValue, encodedValue, encodedValue.length);
}
private void performTest(int integerValue, byte[] encodedValue, int encodedLength) throws IOException {
setup();
writer.writeSleb128(integerValue);
writer.flush();
byte[] writtenData = output.getBuffer();
Assert.assertEquals(startPosition + encodedLength, writer.getPosition());
for (int i=0; i<encodedLength; i++) {
byte encoded = encodedValue[i];
byte written = writtenData[i];
if (i == 4) {
encoded = (byte)(encoded & 0x0F);
written = (byte)(written & 0x0F);
}
Assert.assertEquals(String.format("Values not equal at index %d", i), encoded, written);
}
}
}

View File

@ -0,0 +1,538 @@
/*
* Copyright 2012, Google Inc.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following disclaimer
* in the documentation and/or other materials provided with the
* distribution.
* * Neither the name of Google Inc. nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package org.jf.dexlib2.writer;
import junit.framework.Assert;
import org.jf.dexlib2.ValueType;
import org.jf.util.ExceptionWithContext;
import org.jf.util.NakedByteArrayOutputStream;
import org.junit.Before;
import org.junit.Test;
import java.io.IOException;
import java.util.Arrays;
import java.util.Random;
public class DexWriterTest {
private Random random;
private NakedByteArrayOutputStream output = new NakedByteArrayOutputStream();
private int startPosition;
private DexWriter writer;
@Before
public void setup() throws IOException {
// use a predefined seed, so we get a deterministic result
random = new Random();
output.reset();
startPosition = 123;
int bufferSize = 256;
writer = new DexWriter(output, startPosition, bufferSize);
}
// Note: we use int[] rather than byte[] so that we don't have to cast every value when manually constructing an
// array.
private void expectData(int... bytes) throws IOException {
Assert.assertEquals(startPosition+bytes.length, writer.getPosition());
writer.flush();
byte[] writtenData = output.getBuffer();
for (int i=0; i<bytes.length; i++) {
Assert.assertEquals(String.format("Values not equal at index %d", i), (byte)bytes[i], writtenData[i]);
}
}
private void expectData(byte[] bytes) throws IOException {
Assert.assertEquals(startPosition+bytes.length, writer.getPosition());
writer.flush();
byte[] writtenData = output.getBuffer();
for (int i=0; i<bytes.length; i++) {
Assert.assertEquals(String.format("Values not equal at index %d", i), bytes[i], writtenData[i]);
}
}
@Test
public void testWriteByte() throws IOException {
byte[] arr = new byte[257];
for (int i=0; i<256; i++) {
arr[i] = (byte)i;
writer.write(i);
}
arr[256] = (byte)0x80;
writer.write(0x180);
expectData(arr);
}
@Test
public void testWriteByteArray() throws IOException {
byte[] arr = new byte[345];
random.nextBytes(arr);
writer.write(arr);
expectData(arr);
}
@Test
public void testWriteByteArrayWithLengthAndOffset() throws IOException {
byte[] arr = new byte[345];
random.nextBytes(arr);
writer.write(arr, 10, 300);
expectData(Arrays.copyOfRange(arr, 10, 310));
}
@Test
public void testWriteLong() throws IOException {
writer.writeLong(0x1122334455667788L);
writer.writeLong(-0x1122334455667788L);
expectData(0x88, 0x77, 0x66, 0x55, 0x44, 0x33, 0x22, 0x11,
0x78, 0x88, 0x99, 0xAA, 0xBB, 0xCC, 0xDD, 0xEE);
}
@Test
public void testWriteInt() throws IOException {
writer.writeInt(0x11223344);
writer.writeInt(-0x11223344);
expectData(0x44, 0x33, 0x22, 0x11,
0xBC, 0xCC, 0xDD, 0xEE);
}
@Test
public void testWriteShort() throws IOException {
writer.writeShort(0);
writer.writeShort(0x1122);
writer.writeShort(-0x1122);
writer.writeShort(0x7FFF);
writer.writeShort(-0x8000);
expectData(0x00, 0x00,
0x22, 0x11,
0xDE, 0xEE,
0xFF, 0x7F,
0x00, 0x80);
}
@Test(expected=ExceptionWithContext.class)
public void testWriteShortOutOfBounds() throws IOException {
writer.writeShort(0x8000);
}
@Test(expected=ExceptionWithContext.class)
public void testWriteShortOutOfBounds2() throws IOException {
writer.writeShort(-0x8001);
}
@Test
public void testWriteUshort() throws IOException {
writer.writeUshort(0);
writer.writeUshort(0x1122);
writer.writeUshort(0x8899);
writer.writeUshort(0xFFFF);
expectData(0x00, 0x00,
0x22, 0x11,
0x99, 0x88,
0xFF, 0xFF);
}
@Test(expected=ExceptionWithContext.class)
public void testWriteUshortOutOfBounds() throws IOException {
writer.writeUshort(-1);
}
@Test(expected=ExceptionWithContext.class)
public void testWriteUshortOutOfBounds2() throws IOException {
writer.writeUshort(0x10000);
}
@Test
public void testWriteUbyte() throws IOException {
writer.writeUbyte(0);
writer.writeUbyte(1);
writer.writeUbyte(0x12);
writer.writeUbyte(0xFF);
expectData(0x00, 0x01, 0x12, 0xFF);
}
@Test(expected=ExceptionWithContext.class)
public void testWriteUbyteOutOfBounds() throws IOException {
writer.writeUbyte(-1);
}
@Test(expected=ExceptionWithContext.class)
public void testWriteUbyteOutOfBounds2() throws IOException {
writer.writeUbyte(256);
}
@Test
public void testWriteEncodedValueHeader() throws IOException {
writer.writeEncodedValueHeader(0x2, 0x1);
expectData(0x22);
}
private void testWriteEncodedIntHelper(int integerValue, int... encodedValue) throws IOException {
setup();
writer.writeEncodedInt(ValueType.INT, integerValue);
int[] arr = new int[encodedValue.length+1];
arr[0] = ValueType.INT | ((encodedValue.length-1) << 5);
System.arraycopy(encodedValue, 0, arr, 1, encodedValue.length);
expectData(arr);
}
@Test
public void testWriteEncodedInt() throws IOException {
testWriteEncodedIntHelper(0x00, 0x00);
testWriteEncodedIntHelper(0x40, 0x40);
testWriteEncodedIntHelper(0x7f, 0x7f);
testWriteEncodedIntHelper(0xff, 0xff, 0x00);
testWriteEncodedIntHelper(0xffff80, 0x80, 0xff, 0xff, 0x00);
testWriteEncodedIntHelper(0xffffff80, 0x80);
testWriteEncodedIntHelper(0xffffffff, 0xff);
testWriteEncodedIntHelper(0x100, 0x00, 0x01);
testWriteEncodedIntHelper(0x7fff, 0xff, 0x7f);
testWriteEncodedIntHelper(0x8000, 0x00, 0x80, 0x00);
testWriteEncodedIntHelper(0xffff8000, 0x00, 0x80);
testWriteEncodedIntHelper(0x10000, 0x00, 0x00, 0x01);
testWriteEncodedIntHelper(0x10203, 0x03, 0x02, 0x01);
testWriteEncodedIntHelper(0x810203, 0x03, 0x02, 0x81, 0x00);
testWriteEncodedIntHelper(0xff810203, 0x03, 0x02, 0x81);
testWriteEncodedIntHelper(0x1000000, 0x00, 0x00, 0x00, 0x01);
testWriteEncodedIntHelper(0x1020304, 0x04, 0x03, 0x02, 0x01);
testWriteEncodedIntHelper(0x7fffffff, 0xff, 0xff, 0xff, 0x7f);
testWriteEncodedIntHelper(0x80000000, 0x00, 0x00, 0x00, 0x80);
testWriteEncodedIntHelper(0x80000001, 0x01, 0x00, 0x00, 0x80);
}
private void testWriteEncodedUintHelper(int integerValue, int... encodedValue) throws IOException {
setup();
writer.writeEncodedUint(ValueType.METHOD, integerValue);
int[] arr = new int[encodedValue.length+1];
arr[0] = ValueType.METHOD | ((encodedValue.length-1) << 5);
System.arraycopy(encodedValue, 0, arr, 1, encodedValue.length);
expectData(arr);
}
@Test
public void testWriteEncodedUint() throws IOException {
testWriteEncodedUintHelper(0x00, 0x00);
testWriteEncodedUintHelper(0x01, 0x01);
testWriteEncodedUintHelper(0x40, 0x40);
testWriteEncodedUintHelper(0x7f, 0x7f);
testWriteEncodedUintHelper(0x80, 0x80);
testWriteEncodedUintHelper(0x81, 0x81);
testWriteEncodedUintHelper(0xff, 0xff);
testWriteEncodedUintHelper(0x100, 0x00, 0x01);
testWriteEncodedUintHelper(0x180, 0x80, 0x01);
testWriteEncodedUintHelper(0x8080, 0x80, 0x80);
testWriteEncodedUintHelper(0x1234, 0x34, 0x12);
testWriteEncodedUintHelper(0x1000, 0x00, 0x10);
testWriteEncodedUintHelper(0x8000, 0x00, 0x80);
testWriteEncodedUintHelper(0xff00, 0x00, 0xff);
testWriteEncodedUintHelper(0xffff, 0xff, 0xff);
testWriteEncodedUintHelper(0x10000, 0x00, 0x00, 0x01);
testWriteEncodedUintHelper(0x1ffff, 0xff, 0xff, 0x01);
testWriteEncodedUintHelper(0x80ffff, 0xff, 0xff, 0x80);
testWriteEncodedUintHelper(0xffffff, 0xff, 0xff, 0xff);
testWriteEncodedUintHelper(0x1000000, 0x00, 0x00, 0x00, 0x01);
testWriteEncodedUintHelper(0x1020304, 0x04, 0x03, 0x02, 0x01);
testWriteEncodedUintHelper(0x80000000, 0x00, 0x00, 0x00, 0x80);
testWriteEncodedUintHelper(0x80ffffff, 0xff, 0xff, 0xff, 0x80);
testWriteEncodedUintHelper(0xffffffff, 0xff, 0xff, 0xff, 0xff);
}
private void testWriteEncodedLongHelper(long longValue, int... encodedValue) throws IOException {
setup();
writer.writeEncodedLong(ValueType.LONG, longValue);
int[] arr = new int[encodedValue.length+1];
arr[0] = ValueType.LONG | ((encodedValue.length-1) << 5);
System.arraycopy(encodedValue, 0, arr, 1, encodedValue.length);
expectData(arr);
}
@Test
public void testWriteEncodedLong() throws IOException {
testWriteEncodedLongHelper(0x00L, 0x00);
testWriteEncodedLongHelper(0x40L, 0x40);
testWriteEncodedLongHelper(0x7fL, 0x7f);
testWriteEncodedLongHelper(0xffL, 0xff, 0x00);
testWriteEncodedLongHelper(0xffffffffffffff80L, 0x80);
testWriteEncodedLongHelper(0xffffffffffffffffL, 0xff);
testWriteEncodedLongHelper(0x100L, 0x00, 0x01);
testWriteEncodedLongHelper(0x7fffL, 0xff, 0x7f);
testWriteEncodedLongHelper(0x8000L, 0x00, 0x80, 0x00);
testWriteEncodedLongHelper(0xffffffffffff8000L, 0x00, 0x80);
testWriteEncodedLongHelper(0x10000L, 0x00, 0x00, 0x01);
testWriteEncodedLongHelper(0x10203L, 0x03, 0x02, 0x01);
testWriteEncodedLongHelper(0x810203L, 0x03, 0x02, 0x81, 0x00);
testWriteEncodedLongHelper(0xffffffffff810203L, 0x03, 0x02, 0x81);
testWriteEncodedLongHelper(0x1000000L, 0x00, 0x00, 0x00, 0x01);
testWriteEncodedLongHelper(0x1020304L, 0x04, 0x03, 0x02, 0x01);
testWriteEncodedLongHelper(0x7fffffffL, 0xff, 0xff, 0xff, 0x7f);
testWriteEncodedLongHelper(0x80000000L, 0x00, 0x00, 0x00, 0x80, 0x00);
testWriteEncodedLongHelper(0xffffffff80000000L, 0x00, 0x00, 0x00, 0x80);
testWriteEncodedLongHelper(0xffffffff80000001L, 0x01, 0x00, 0x00, 0x80);
testWriteEncodedLongHelper(0x100000000L, 0x00, 0x00, 0x00, 0x00, 0x01);
testWriteEncodedLongHelper(0x102030405L, 0x05, 0x04, 0x03, 0x02, 0x01);
testWriteEncodedLongHelper(0x7fffffffffL, 0xff, 0xff, 0xff, 0xff, 0x7f);
testWriteEncodedLongHelper(0x8000000000L, 0x00, 0x00, 0x00, 0x00, 0x80, 0x00);
testWriteEncodedLongHelper(0xffffff8000000000L, 0x00, 0x00, 0x00, 0x00, 0x80);
testWriteEncodedLongHelper(0xffffff8000000001L, 0x01, 0x00, 0x00, 0x00, 0x80);
testWriteEncodedLongHelper(0x10000000000L, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01);
testWriteEncodedLongHelper(0x10203040506L, 0x06, 0x05, 0x04, 0x03, 0x02, 0x01);
testWriteEncodedLongHelper(0x7fffffffffffL, 0xff, 0xff, 0xff, 0xff, 0xff, 0x7f);
testWriteEncodedLongHelper(0x800000000000L, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 0x00);
testWriteEncodedLongHelper(0xffff800000000000L, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80);
testWriteEncodedLongHelper(0xffff800000000001L, 0x01, 0x00, 0x00, 0x00, 0x00, 0x80);
testWriteEncodedLongHelper(0x1000000000000L, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01);
testWriteEncodedLongHelper(0x1020304050607L, 0x07, 0x06, 0x05, 0x04, 0x03, 0x02, 0x01);
testWriteEncodedLongHelper(0x7fffffffffffffL, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x7f);
testWriteEncodedLongHelper(0x80000000000000L, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 0x00);
testWriteEncodedLongHelper(0xff80000000000000L, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80);
testWriteEncodedLongHelper(0xff80000000000001L, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80);
testWriteEncodedLongHelper(0x100000000000000L, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01);
testWriteEncodedLongHelper(0x102030405060708L, 0x08, 0x07, 0x06, 0x05, 0x04, 0x03, 0x02, 0x01);
testWriteEncodedLongHelper(0x7fffffffffffffffL, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x7f);
testWriteEncodedLongHelper(0x8000000000000000L, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80);
testWriteEncodedLongHelper(0x8000000000000001L, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80);
testWriteEncodedLongHelper(0xfeffffffffffffffL, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe);
testWriteEncodedLongHelper(0x123456789ABCDEF0L, 0xF0, 0xDE, 0xBC, 0x9A, 0x78, 0x56, 0x34, 0x12);
}
private void testWriteRightZeroExtendedIntHelper(int intValue, int... encodedValue) throws IOException {
setup();
writer.writeRightZeroExtendedInt(ValueType.FLOAT, intValue);
int[] arr = new int[encodedValue.length+1];
arr[0] = ValueType.FLOAT | ((encodedValue.length-1) << 5);
System.arraycopy(encodedValue, 0, arr, 1, encodedValue.length);
expectData(arr);
}
@Test
public void testWriteRightZeroExtendedInt() throws IOException {
testWriteRightZeroExtendedIntHelper(0, 0x00);
testWriteRightZeroExtendedIntHelper(0x01000000, 0x01);
testWriteRightZeroExtendedIntHelper(0x7f000000, 0x7f);
testWriteRightZeroExtendedIntHelper(0x80000000, 0x80);
testWriteRightZeroExtendedIntHelper(0xf0000000, 0xf0);
testWriteRightZeroExtendedIntHelper(0xff000000, 0xff);
testWriteRightZeroExtendedIntHelper(0x010000, 0x01, 0x00);
testWriteRightZeroExtendedIntHelper(0x01100000, 0x10, 0x01);
testWriteRightZeroExtendedIntHelper(0x7f100000, 0x10, 0x7f);
testWriteRightZeroExtendedIntHelper(0x80100000, 0x10, 0x80);
testWriteRightZeroExtendedIntHelper(0xf0100000, 0x10, 0xf0);
testWriteRightZeroExtendedIntHelper(0xff100000, 0x10, 0xff);
testWriteRightZeroExtendedIntHelper(0xff000000, 0xff);
testWriteRightZeroExtendedIntHelper(0x0100, 0x01, 0x00, 0x00);
testWriteRightZeroExtendedIntHelper(0x01101000, 0x10, 0x10, 0x01);
testWriteRightZeroExtendedIntHelper(0x7f101000, 0x10, 0x10, 0x7f);
testWriteRightZeroExtendedIntHelper(0x80101000, 0x10, 0x10, 0x80);
testWriteRightZeroExtendedIntHelper(0xf0101000, 0x10, 0x10, 0xf0);
testWriteRightZeroExtendedIntHelper(0xff101000, 0x10, 0x10, 0xff);
testWriteRightZeroExtendedIntHelper(0x01, 0x01, 0x00, 0x00, 0x00);
testWriteRightZeroExtendedIntHelper(0x80, 0x80, 0x00, 0x00, 0x00);
testWriteRightZeroExtendedIntHelper(0xff, 0xff, 0x00, 0x00, 0x00);
testWriteRightZeroExtendedIntHelper(0x01101010, 0x10, 0x10, 0x10, 0x01);
testWriteRightZeroExtendedIntHelper(0x7f101010, 0x10, 0x10, 0x10, 0x7f);
testWriteRightZeroExtendedIntHelper(0x80101010, 0x10, 0x10, 0x10, 0x80);
testWriteRightZeroExtendedIntHelper(0xf0101010, 0x10, 0x10, 0x10, 0xf0);
testWriteRightZeroExtendedIntHelper(0xff101010, 0x10, 0x10, 0x10, 0xff);
}
private void testWriteRightZeroExtendedLongHelper(long longValue, int... encodedValue) throws IOException {
setup();
writer.writeRightZeroExtendedLong(ValueType.DOUBLE, longValue);
int[] arr = new int[encodedValue.length+1];
arr[0] = ValueType.DOUBLE | ((encodedValue.length-1) << 5);
System.arraycopy(encodedValue, 0, arr, 1, encodedValue.length);
expectData(arr);
}
@Test
public void testWriteRightZeroExtendedLong() throws IOException {
testWriteRightZeroExtendedLongHelper(0, 0x00);
testWriteRightZeroExtendedLongHelper(0x0100000000000000L, 0x01);
testWriteRightZeroExtendedLongHelper(0x7f00000000000000L, 0x7f);
testWriteRightZeroExtendedLongHelper(0x8000000000000000L, 0x80);
testWriteRightZeroExtendedLongHelper(0xf000000000000000L, 0xf0);
testWriteRightZeroExtendedLongHelper(0xff00000000000000L, 0xff);
testWriteRightZeroExtendedLongHelper(0x01000000000000L, 0x01, 0x00);
testWriteRightZeroExtendedLongHelper(0x0110000000000000L, 0x10, 0x01);
testWriteRightZeroExtendedLongHelper(0x7f10000000000000L, 0x10, 0x7f);
testWriteRightZeroExtendedLongHelper(0x8010000000000000L, 0x10, 0x80);
testWriteRightZeroExtendedLongHelper(0xf010000000000000L, 0x10, 0xf0);
testWriteRightZeroExtendedLongHelper(0xff10000000000000L, 0x10, 0xff);
testWriteRightZeroExtendedLongHelper(0x7fff000000000000L, 0xff, 0x7f);
testWriteRightZeroExtendedLongHelper(0x010000000000L, 0x01, 0x00, 0x00);
testWriteRightZeroExtendedLongHelper(0x0110100000000000L, 0x10, 0x10, 0x01);
testWriteRightZeroExtendedLongHelper(0x7f10100000000000L, 0x10, 0x10, 0x7f);
testWriteRightZeroExtendedLongHelper(0x8010100000000000L, 0x10, 0x10, 0x80);
testWriteRightZeroExtendedLongHelper(0xf010100000000000L, 0x10, 0x10, 0xf0);
testWriteRightZeroExtendedLongHelper(0xff10100000000000L, 0x10, 0x10, 0xff);
testWriteRightZeroExtendedLongHelper(0x7fffff0000000000L, 0xff, 0xff, 0x7f);
testWriteRightZeroExtendedLongHelper(0x0100000000L, 0x01, 0x00, 0x00, 0x00);
testWriteRightZeroExtendedLongHelper(0x0110101000000000L, 0x10, 0x10, 0x10, 0x01);
testWriteRightZeroExtendedLongHelper(0x7f10101000000000L, 0x10, 0x10, 0x10, 0x7f);
testWriteRightZeroExtendedLongHelper(0x8010101000000000L, 0x10, 0x10, 0x10, 0x80);
testWriteRightZeroExtendedLongHelper(0xf010101000000000L, 0x10, 0x10, 0x10, 0xf0);
testWriteRightZeroExtendedLongHelper(0xff10101000000000L, 0x10, 0x10, 0x10, 0xff);
testWriteRightZeroExtendedLongHelper(0x7fffffff00000000L, 0xff, 0xff, 0xff, 0x7f);
testWriteRightZeroExtendedLongHelper(0x01000000L, 0x01, 0x00, 0x00, 0x00, 0x00);
testWriteRightZeroExtendedLongHelper(0x0110101010000000L, 0x10, 0x10, 0x10, 0x10, 0x01);
testWriteRightZeroExtendedLongHelper(0x7f10101010000000L, 0x10, 0x10, 0x10, 0x10, 0x7f);
testWriteRightZeroExtendedLongHelper(0x8010101010000000L, 0x10, 0x10, 0x10, 0x10, 0x80);
testWriteRightZeroExtendedLongHelper(0xf010101010000000L, 0x10, 0x10, 0x10, 0x10, 0xf0);
testWriteRightZeroExtendedLongHelper(0xff10101010000000L, 0x10, 0x10, 0x10, 0x10, 0xff);
testWriteRightZeroExtendedLongHelper(0x7fffffffff000000L, 0xff, 0xff, 0xff, 0xff, 0x7f);
testWriteRightZeroExtendedLongHelper(0x010000L, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00);
testWriteRightZeroExtendedLongHelper(0x0110101010100000L, 0x10, 0x10, 0x10, 0x10, 0x10, 0x01);
testWriteRightZeroExtendedLongHelper(0x7f10101010100000L, 0x10, 0x10, 0x10, 0x10, 0x10, 0x7f);
testWriteRightZeroExtendedLongHelper(0x8010101010100000L, 0x10, 0x10, 0x10, 0x10, 0x10, 0x80);
testWriteRightZeroExtendedLongHelper(0xf010101010100000L, 0x10, 0x10, 0x10, 0x10, 0x10, 0xf0);
testWriteRightZeroExtendedLongHelper(0xff10101010100000L, 0x10, 0x10, 0x10, 0x10, 0x10, 0xff);
testWriteRightZeroExtendedLongHelper(0x7fffffffffff0000L, 0xff, 0xff, 0xff, 0xff, 0xff, 0x7f);
testWriteRightZeroExtendedLongHelper(0x0100L, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00);
testWriteRightZeroExtendedLongHelper(0x0110101010101000L, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x01);
testWriteRightZeroExtendedLongHelper(0x7f10101010101000L, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x7f);
testWriteRightZeroExtendedLongHelper(0x8010101010101000L, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x80);
testWriteRightZeroExtendedLongHelper(0xf010101010101000L, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0xf0);
testWriteRightZeroExtendedLongHelper(0xff10101010101000L, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0xff);
testWriteRightZeroExtendedLongHelper(0x7fffffffffffff00L, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x7f);
testWriteRightZeroExtendedLongHelper(0x01L, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00);
testWriteRightZeroExtendedLongHelper(0x0110101010101010L, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x01);
testWriteRightZeroExtendedLongHelper(0x7f10101010101010L, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x7f);
testWriteRightZeroExtendedLongHelper(0x8010101010101010L, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x80);
testWriteRightZeroExtendedLongHelper(0xf010101010101010L, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0xf0);
testWriteRightZeroExtendedLongHelper(0xff10101010101010L, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0xff);
testWriteRightZeroExtendedLongHelper(Long.MAX_VALUE, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x7f);
testWriteRightZeroExtendedLongHelper(Long.MIN_VALUE, 0x80);
testWriteRightZeroExtendedLongHelper(-1, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff);
}
private void testWriteStringHelper(String stringValue, int... encodedValue) throws IOException {
setup();
writer.writeString(stringValue);
expectData(encodedValue);
}
@Test
public void testWriteString() throws IOException {
testWriteStringHelper(new String(new char[]{0x00}), 0xc0, 0x80);
testWriteStringHelper(new String(new char[]{0x01}), 0x01);
testWriteStringHelper(new String(new char[]{0x40}), 0x40);
testWriteStringHelper(new String(new char[]{0x7f}), 0x7f);
testWriteStringHelper(new String(new char[]{0x80}), 0xc2, 0x80);
testWriteStringHelper(new String(new char[]{0x81}), 0xc2, 0x81);
testWriteStringHelper(new String(new char[]{0x100}), 0xc4, 0x80);
testWriteStringHelper(new String(new char[]{0x7ff}), 0xdf, 0xbf);
testWriteStringHelper(new String(new char[]{0x800}), 0xe0, 0xa0, 0x80);
testWriteStringHelper(new String(new char[]{0x801}), 0xe0, 0xa0, 0x81);
testWriteStringHelper(new String(new char[]{0x1000}), 0xe1, 0x80, 0x80);
testWriteStringHelper(new String(new char[]{0x7fff}), 0xe7, 0xbf, 0xbf);
testWriteStringHelper(new String(new char[]{0x8000}), 0xe8, 0x80, 0x80);
testWriteStringHelper(new String(new char[]{0x8001}), 0xe8, 0x80, 0x81);
testWriteStringHelper(new String(new char[]{0xffff}), 0xef, 0xbf, 0xbf);
}
@Test
public void testAlign() throws IOException {
// create a new writer so we can start at file position 0
startPosition = 0;
writer = new DexWriter(output, startPosition, 256);
writer.align();
writer.write(1);
writer.align();
writer.align();
writer.write(1);
writer.write(2);
writer.align();
writer.write(1);
writer.write(2);
writer.write(3);
writer.align();
writer.align();
writer.write(1);
writer.write(2);
writer.write(3);
writer.write(4);
writer.align();
writer.align();
writer.align();
writer.align();
writer.write(1);
writer.align();
expectData(0x01, 0x00, 0x00, 0x00,
0x01, 0x02, 0x00, 0x00,
0x01, 0x02, 0x03, 0x00,
0x01, 0x02, 0x03, 0x04,
0x01, 0x00, 0x00, 0x00);
}
}

View File

@ -0,0 +1,242 @@
/*
* Copyright 2012, Google Inc.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following disclaimer
* in the documentation and/or other materials provided with the
* distribution.
* * Neither the name of Google Inc. nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package org.jf.dexlib2.writer;
import junit.framework.Assert;
import org.jf.util.NakedByteArrayOutputStream;
import org.junit.Test;
import java.io.IOException;
public class DexWriterUleb128Test {
private NakedByteArrayOutputStream output = new NakedByteArrayOutputStream();
private int startPosition;
private DexWriter writer;
public void setup() throws IOException {
output.reset();
startPosition = 123;
int bufferSize = 256;
writer = new DexWriter(output, startPosition, bufferSize);
}
@Test
public void testUleb128() throws IOException {
performTest(0x0, new byte[]{0x0, 0x11}, 1);
performTest(0x1, new byte[]{0x1, 0x11}, 1);
performTest(0x3f, new byte[]{0x3f, 0x11}, 1);
performTest(0x40, new byte[]{0x40, 0x11}, 1);
performTest(0x70, new byte[]{0x70, 0x11}, 1);
performTest(0x7f, new byte[]{0x7f, 0x11}, 1);
performTest(0x80, new byte[]{(byte)0x80, 0x1, 0x11}, 2);
performTest(0x100, new byte[]{(byte)0x80, 0x2, 0x11}, 2);
performTest(0x800, new byte[]{(byte)0x80, 0x10, 0x11}, 2);
performTest(0x1f80, new byte[]{(byte)0x80, 0x3f, 0x11}, 2);
performTest(0x2000, new byte[]{(byte)0x80, 0x40, 0x11}, 2);
performTest(0x2080, new byte[]{(byte)0x80, 0x41, 0x11}, 2);
performTest(0x3800, new byte[]{(byte)0x80, 0x70, 0x11}, 2);
performTest(0x3f80, new byte[]{(byte)0x80, 0x7f, 0x11}, 2);
performTest(0xff, new byte[]{(byte)0xff, 0x1, 0x11}, 2);
performTest(0x17f, new byte[]{(byte)0xff, 0x2, 0x11}, 2);
performTest(0x87f, new byte[]{(byte)0xff, 0x10, 0x11}, 2);
performTest(0x1fff, new byte[]{(byte)0xff, 0x3f, 0x11}, 2);
performTest(0x207f, new byte[]{(byte)0xff, 0x40, 0x11}, 2);
performTest(0x20ff, new byte[]{(byte)0xff, 0x41, 0x11}, 2);
performTest(0x387f, new byte[]{(byte)0xff, 0x70, 0x11}, 2);
performTest(0x3fff, new byte[]{(byte)0xff, 0x7f, 0x11}, 2);
performTest(0x4000, new byte[]{(byte)0x80, (byte)0x80, 0x1, 0x11}, 3);
performTest(0x8000, new byte[]{(byte)0x80, (byte)0x80, 0x2, 0x11}, 3);
performTest(0x40000, new byte[]{(byte)0x80, (byte)0x80, 0x10, 0x11}, 3);
performTest(0xfc000, new byte[]{(byte)0x80, (byte)0x80, 0x3f, 0x11}, 3);
performTest(0x100000, new byte[]{(byte)0x80, (byte)0x80, 0x40, 0x11}, 3);
performTest(0x104000, new byte[]{(byte)0x80, (byte)0x80, 0x41, 0x11}, 3);
performTest(0x1c0000, new byte[]{(byte)0x80, (byte)0x80, 0x70, 0x11}, 3);
performTest(0x1fc000, new byte[]{(byte)0x80, (byte)0x80, 0x7f, 0x11}, 3);
performTest(0x7fff, new byte[]{(byte)0xff, (byte)0xff, 0x1, 0x11}, 3);
performTest(0xbfff, new byte[]{(byte)0xff, (byte)0xff, 0x2, 0x11}, 3);
performTest(0x43fff, new byte[]{(byte)0xff, (byte)0xff, 0x10, 0x11}, 3);
performTest(0xfffff, new byte[]{(byte)0xff, (byte)0xff, 0x3f, 0x11}, 3);
performTest(0x103fff, new byte[]{(byte)0xff, (byte)0xff, 0x40, 0x11}, 3);
performTest(0x107fff, new byte[]{(byte)0xff, (byte)0xff, 0x41, 0x11}, 3);
performTest(0x1c3fff, new byte[]{(byte)0xff, (byte)0xff, 0x70, 0x11}, 3);
performTest(0x1fffff, new byte[]{(byte)0xff, (byte)0xff, 0x7f, 0x11}, 3);
performTest(0x200000, new byte[]{(byte)0x80, (byte)0x80, (byte)0x80, 0x1, 0x11}, 4);
performTest(0x400000, new byte[]{(byte)0x80, (byte)0x80, (byte)0x80, 0x2, 0x11}, 4);
performTest(0x2000000, new byte[]{(byte)0x80, (byte)0x80, (byte)0x80, 0x10, 0x11}, 4);
performTest(0x7e00000, new byte[]{(byte)0x80, (byte)0x80, (byte)0x80, 0x3f, 0x11}, 4);
performTest(0x8000000, new byte[]{(byte)0x80, (byte)0x80, (byte)0x80, 0x40, 0x11}, 4);
performTest(0x8200000, new byte[]{(byte)0x80, (byte)0x80, (byte)0x80, 0x41, 0x11}, 4);
performTest(0xe000000, new byte[]{(byte)0x80, (byte)0x80, (byte)0x80, 0x70, 0x11}, 4);
performTest(0xfe00000, new byte[]{(byte)0x80, (byte)0x80, (byte)0x80, 0x7f, 0x11}, 4);
performTest(0x3fffff, new byte[]{(byte)0xff, (byte)0xff, (byte)0xff, 0x1, 0x11}, 4);
performTest(0x5fffff, new byte[]{(byte)0xff, (byte)0xff, (byte)0xff, 0x2, 0x11}, 4);
performTest(0x21fffff, new byte[]{(byte)0xff, (byte)0xff, (byte)0xff, 0x10, 0x11}, 4);
performTest(0x7ffffff, new byte[]{(byte)0xff, (byte)0xff, (byte)0xff, 0x3f, 0x11}, 4);
performTest(0x81fffff, new byte[]{(byte)0xff, (byte)0xff, (byte)0xff, 0x40, 0x11}, 4);
performTest(0x83fffff, new byte[]{(byte)0xff, (byte)0xff, (byte)0xff, 0x41, 0x11}, 4);
performTest(0xe1fffff, new byte[]{(byte)0xff, (byte)0xff, (byte)0xff, 0x70, 0x11}, 4);
performTest(0xfffffff, new byte[]{(byte)0xff, (byte)0xff, (byte)0xff, 0x7f, 0x11}, 4);
performTest(0x10000000, new byte[]{(byte)0x80, (byte)0x80, (byte)0x80, (byte)0x80, 0x1, 0x11}, 5);
performTest(0x20000000, new byte[]{(byte)0x80, (byte)0x80, (byte)0x80, (byte)0x80, 0x2, 0x11}, 5);
performTest(0x70000000, new byte[]{(byte)0x80, (byte)0x80, (byte)0x80, (byte)0x80, 0x7, 0x11}, 5);
performTest(0x1fffffff, new byte[]{(byte)0xff, (byte)0xff, (byte)0xff, (byte)0xff, 0x1, 0x11}, 5);
performTest(0x2fffffff, new byte[]{(byte)0xff, (byte)0xff, (byte)0xff, (byte)0xff, 0x2, 0x11}, 5);
performTest(0x7fffffff, new byte[]{(byte)0xff, (byte)0xff, (byte)0xff, (byte)0xff, 0x7, 0x11}, 5);
performTest(0xcc, new byte[]{(byte)0xcc, 0x1});
performTest(0x3b67, new byte[]{(byte)0xe7, 0x76});
performTest(0x1b857589, new byte[]{(byte)0x89, (byte)0xeb, (byte)0x95, (byte)0xdc, 0x1});
performTest(0x375d82e5, new byte[]{(byte)0xe5, (byte)0x85, (byte)0xf6, (byte)0xba, 0x3});
performTest(0x5524da90, new byte[]{(byte)0x90, (byte)0xb5, (byte)0x93, (byte)0xa9, 0x5});
performTest(0x35, new byte[]{0x35});
performTest(0xd7, new byte[]{(byte)0xd7, 0x1});
performTest(0x63, new byte[]{0x63});
performTest(0x22cb5b, new byte[]{(byte)0xdb, (byte)0x96, (byte)0x8b, 0x1});
performTest(0x585e, new byte[]{(byte)0xde, (byte)0xb0, 0x1});
performTest(0x5d62a965, new byte[]{(byte)0xe5, (byte)0xd2, (byte)0x8a, (byte)0xeb, 0x5});
performTest(0x6af172db, new byte[]{(byte)0xdb, (byte)0xe5, (byte)0xc5, (byte)0xd7, 0x6});
performTest(0xe, new byte[]{0xe});
performTest(0xb75f7a, new byte[]{(byte)0xfa, (byte)0xbe, (byte)0xdd, 0x5});
performTest(0x8604, new byte[]{(byte)0x84, (byte)0x8c, 0x2});
performTest(0x31624026, new byte[]{(byte)0xa6, (byte)0x80, (byte)0x89, (byte)0x8b, 0x3});
performTest(0x8d, new byte[]{(byte)0x8d, 0x1});
performTest(0xc0, new byte[]{(byte)0xc0, 0x1});
performTest(0xd7618cb, new byte[]{(byte)0xcb, (byte)0xb1, (byte)0xd8, 0x6b});
performTest(0xff, new byte[]{(byte)0xff, 0x1});
performTest(0x5c923e42, new byte[]{(byte)0xc2, (byte)0xfc, (byte)0xc8, (byte)0xe4, 0x5});
performTest(0x91, new byte[]{(byte)0x91, 0x1});
performTest(0xbe0f97, new byte[]{(byte)0x97, (byte)0x9f, (byte)0xf8, 0x5});
performTest(0x88bc786, new byte[]{(byte)0x86, (byte)0x8f, (byte)0xaf, 0x44});
performTest(0x8caa9a, new byte[]{(byte)0x9a, (byte)0xd5, (byte)0xb2, 0x4});
performTest(0x4aee, new byte[]{(byte)0xee, (byte)0x95, 0x1});
performTest(0x438c86, new byte[]{(byte)0x86, (byte)0x99, (byte)0x8e, 0x2});
performTest(0xc0, new byte[]{(byte)0xc0, 0x1});
performTest(0xb486, new byte[]{(byte)0x86, (byte)0xe9, 0x2});
performTest(0x83fd, new byte[]{(byte)0xfd, (byte)0x87, 0x2});
performTest(0x7b, new byte[]{0x7b});
performTest(0x1dc84e14, new byte[]{(byte)0x94, (byte)0x9c, (byte)0xa1, (byte)0xee, 0x1});
performTest(0x2dfc, new byte[]{(byte)0xfc, 0x5b});
performTest(0x88, new byte[]{(byte)0x88, 0x1});
performTest(0x919e, new byte[]{(byte)0x9e, (byte)0xa3, 0x2});
performTest(0x2fcf, new byte[]{(byte)0xcf, 0x5f});
performTest(0xf00674, new byte[]{(byte)0xf4, (byte)0x8c, (byte)0xc0, 0x7});
performTest(0xed5f7d, new byte[]{(byte)0xfd, (byte)0xbe, (byte)0xb5, 0x7});
performTest(0xdbd9, new byte[]{(byte)0xd9, (byte)0xb7, 0x3});
performTest(0xa1, new byte[]{(byte)0xa1, 0x1});
performTest(0xf6f76c, new byte[]{(byte)0xec, (byte)0xee, (byte)0xdb, 0x7});
performTest(0x1eed6f, new byte[]{(byte)0xef, (byte)0xda, 0x7b});
performTest(0x95c, new byte[]{(byte)0xdc, 0x12});
performTest(0x1e, new byte[]{0x1e});
performTest(0xe5, new byte[]{(byte)0xe5, 0x1});
performTest(0x2f2f13, new byte[]{(byte)0x93, (byte)0xde, (byte)0xbc, 0x1});
performTest(0x19, new byte[]{0x19});
performTest(0x3f, new byte[]{0x3f});
performTest(0x75e3, new byte[]{(byte)0xe3, (byte)0xeb, 0x1});
performTest(0x67a4c4, new byte[]{(byte)0xc4, (byte)0xc9, (byte)0x9e, 0x3});
performTest(0xb948, new byte[]{(byte)0xc8, (byte)0xf2, 0x2});
performTest(0x34b1c9de, new byte[]{(byte)0xde, (byte)0x93, (byte)0xc7, (byte)0xa5, 0x3});
performTest(0x58f0, new byte[]{(byte)0xf0, (byte)0xb1, 0x1});
performTest(0x0, new byte[]{0x0});
performTest(0x9ab3e5, new byte[]{(byte)0xe5, (byte)0xe7, (byte)0xea, 0x4});
performTest(0x4c4a8a3d, new byte[]{(byte)0xbd, (byte)0x94, (byte)0xaa, (byte)0xe2, 0x4});
performTest(0x99, new byte[]{(byte)0x99, 0x1});
performTest(0x1a67e9, new byte[]{(byte)0xe9, (byte)0xcf, 0x69});
performTest(0x5ddb2d, new byte[]{(byte)0xad, (byte)0xb6, (byte)0xf7, 0x2});
performTest(0xeccb680, new byte[]{(byte)0x80, (byte)0xed, (byte)0xb2, 0x76});
performTest(0x6910bbf0, new byte[]{(byte)0xf0, (byte)0xf7, (byte)0xc2, (byte)0xc8, 0x6});
performTest(0xc5, new byte[]{(byte)0xc5, 0x1});
performTest(0xdd7225, new byte[]{(byte)0xa5, (byte)0xe4, (byte)0xf5, 0x6});
performTest(0x4561ea2e, new byte[]{(byte)0xae, (byte)0xd4, (byte)0x87, (byte)0xab, 0x4});
performTest(0x7f4f08, new byte[]{(byte)0x88, (byte)0x9e, (byte)0xfd, 0x3});
performTest(0x197f, new byte[]{(byte)0xff, 0x32});
performTest(0xb8ad13, new byte[]{(byte)0x93, (byte)0xda, (byte)0xe2, 0x5});
performTest(0x3c8d5db4, new byte[]{(byte)0xb4, (byte)0xbb, (byte)0xb5, (byte)0xe4, 0x3});
performTest(0x7e4bdf7d, new byte[]{(byte)0xfd, (byte)0xbe, (byte)0xaf, (byte)0xf2, 0x7});
performTest(0x1e8e23, new byte[]{(byte)0xa3, (byte)0x9c, 0x7a});
performTest(0x1602, new byte[]{(byte)0x82, 0x2c});
performTest(0xe2, new byte[]{(byte)0xe2, 0x1});
performTest(0x38e9, new byte[]{(byte)0xe9, 0x71});
performTest(0xbf8665, new byte[]{(byte)0xe5, (byte)0x8c, (byte)0xfe, 0x5});
performTest(0x43, new byte[]{0x43});
performTest(0xc9d96c, new byte[]{(byte)0xec, (byte)0xb2, (byte)0xa7, 0x6});
performTest(0x4bd170, new byte[]{(byte)0xf0, (byte)0xa2, (byte)0xaf, 0x2});
performTest(0x86c11b, new byte[]{(byte)0x9b, (byte)0x82, (byte)0x9b, 0x4});
performTest(0x1a2611e7, new byte[]{(byte)0xe7, (byte)0xa3, (byte)0x98, (byte)0xd1, 0x1});
performTest(0xff2f6a, new byte[]{(byte)0xea, (byte)0xde, (byte)0xfc, 0x7});
performTest(0x6f051635, new byte[]{(byte)0xb5, (byte)0xac, (byte)0x94, (byte)0xf8, 0x6});
performTest(0x75bf, new byte[]{(byte)0xbf, (byte)0xeb, 0x1});
performTest(0xe8ce45, new byte[]{(byte)0xc5, (byte)0x9c, (byte)0xa3, 0x7});
performTest(0x2946a1d8, new byte[]{(byte)0xd8, (byte)0xc3, (byte)0x9a, (byte)0xca, 0x2});
performTest(0xe2, new byte[]{(byte)0xe2, 0x1});
performTest(0x44ee, new byte[]{(byte)0xee, (byte)0x89, 0x1});
performTest(0x447a, new byte[]{(byte)0xfa, (byte)0x88, 0x1});
performTest(0x917, new byte[]{(byte)0x97, 0x12});
performTest(0x25, new byte[]{0x25});
performTest(0x52c2b8eb, new byte[]{(byte)0xeb, (byte)0xf1, (byte)0x8a, (byte)0x96, 0x5});
performTest(0x17dabee4, new byte[]{(byte)0xe4, (byte)0xfd, (byte)0xea, (byte)0xbe, 0x1});
performTest(0x9d6a, new byte[]{(byte)0xea, (byte)0xba, 0x2});
performTest(0xc4b12d, new byte[]{(byte)0xad, (byte)0xe2, (byte)0x92, 0x6});
performTest(0xc9561d, new byte[]{(byte)0x9d, (byte)0xac, (byte)0xa5, 0x6});
performTest(0x88a7, new byte[]{(byte)0xa7, (byte)0x91, 0x2});
performTest(0x527d8f7a, new byte[]{(byte)0xfa, (byte)0x9e, (byte)0xf6, (byte)0x93, 0x5});
performTest(0x2c31, new byte[]{(byte)0xb1, 0x58});
performTest(0x3b8c, new byte[]{(byte)0x8c, 0x77});
performTest(0xc228, new byte[]{(byte)0xa8, (byte)0x84, 0x3});
performTest(0xd730d3, new byte[]{(byte)0xd3, (byte)0xe1, (byte)0xdc, 0x6});
}
private void performTest(int integerValue, byte[] encodedValue) throws IOException {
performTest(integerValue, encodedValue, encodedValue.length);
}
private void performTest(int integerValue, byte[] encodedValue, int encodedLength) throws IOException {
setup();
writer.writeUleb128(integerValue);
writer.flush();
byte[] writtenData = output.getBuffer();
Assert.assertEquals(startPosition + encodedLength, writer.getPosition());
for (int i=0; i<encodedLength; i++) {
Assert.assertEquals(String.format("Values not equal at index %d", i), encodedValue[i], writtenData[i]);
}
}
}

View File

@ -0,0 +1,44 @@
/*
* Copyright 2012, Google Inc.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following disclaimer
* in the documentation and/or other materials provided with the
* distribution.
* * Neither the name of Google Inc. nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package org.jf.util;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
/**
* A ByteArrayOutputStream that lets you grab its protected bits.
*/
public class NakedByteArrayOutputStream extends ByteArrayOutputStream {
public byte[] getBuffer() throws IOException {
return buf;
}
}