BaseDexBufferTest.java revision 7301fbe30e6661c67c33552efd28a8d7587aba3d
1/*
2 * Copyright 2012, Google Inc.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
7 * met:
8 *
9 *     * Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 *     * Redistributions in binary form must reproduce the above
12 * copyright notice, this list of conditions and the following disclaimer
13 * in the documentation and/or other materials provided with the
14 * distribution.
15 *     * Neither the name of Google Inc. nor the names of its
16 * contributors may be used to endorse or promote products derived from
17 * this software without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 */
31
32package org.jf.dexlib2.dexbacked;
33
34import junit.framework.Assert;
35import org.jf.util.ExceptionWithContext;
36import org.junit.Test;
37
38import java.nio.ByteBuffer;
39import java.nio.ByteOrder;
40import java.util.Random;
41
42public class BaseDexBufferTest {
43    @Test
44    public void testReadSmallUintSuccess() {
45        BaseDexBuffer dexBuf = new BaseDexBuffer(new byte[] {0x11, 0x22, 0x33, 0x44});
46        Assert.assertEquals(0x44332211, dexBuf.readSmallUint(0));
47
48        dexBuf = new BaseDexBuffer(new byte[] {0x00, 0x00, 0x00, 0x00});
49        Assert.assertEquals(0, dexBuf.readSmallUint(0));
50
51        dexBuf = new BaseDexBuffer(new byte[] {(byte)0xff, (byte)0xff, (byte)0xff, 0x7f});
52        Assert.assertEquals(0x7fffffff, dexBuf.readSmallUint(0));
53    }
54
55    @Test(expected=ExceptionWithContext.class)
56    public void testReadSmallUintTooLarge1() {
57        BaseDexBuffer dexBuf = new BaseDexBuffer(new byte[] {0x00, 0x00, 0x00, (byte)0x80});
58        dexBuf.readSmallUint(0);
59    }
60
61    @Test(expected=ExceptionWithContext.class)
62    public void testReadSmallUintTooLarge2() {
63        BaseDexBuffer dexBuf = new BaseDexBuffer(new byte[] {(byte)0xff, (byte)0xff, (byte)0xff, (byte)0x80});
64        dexBuf.readSmallUint(0);
65    }
66
67    @Test(expected=ExceptionWithContext.class)
68    public void testReadSmallUintTooLarge3() {
69        BaseDexBuffer dexBuf = new BaseDexBuffer(new byte[] {(byte)0xff, (byte)0xff, (byte)0xff, (byte)0xff});
70        dexBuf.readSmallUint(0);
71    }
72
73    @Test
74    public void testReadOptionalUintSuccess() {
75        BaseDexBuffer dexBuf = new BaseDexBuffer(new byte[] {0x11, 0x22, 0x33, 0x44});
76        Assert.assertEquals(0x44332211, dexBuf.readSmallUint(0));
77
78        dexBuf = new BaseDexBuffer(new byte[] {0x00, 0x00, 0x00, 0x00});
79        Assert.assertEquals(0, dexBuf.readSmallUint(0));
80
81        dexBuf = new BaseDexBuffer(new byte[] {(byte)0xff, (byte)0xff, (byte)0xff, 0x7f});
82        Assert.assertEquals(0x7fffffff, dexBuf.readSmallUint(0));
83
84        dexBuf = new BaseDexBuffer(new byte[] {(byte)0xff, (byte)0xff, (byte)0xff, (byte)0xff});
85        Assert.assertEquals(-1, dexBuf.readOptionalUint(0));
86    }
87
88    @Test(expected=ExceptionWithContext.class)
89    public void testReadOptionalUintTooLarge1() {
90        BaseDexBuffer dexBuf = new BaseDexBuffer(new byte[] {0x00, 0x00, 0x00, (byte)0x80});
91        dexBuf.readSmallUint(0);
92    }
93
94    @Test(expected=ExceptionWithContext.class)
95    public void testReadOptionalUintTooLarge2() {
96        BaseDexBuffer dexBuf = new BaseDexBuffer(new byte[] {(byte)0xff, (byte)0xff, (byte)0xff, (byte)0x80});
97        dexBuf.readSmallUint(0);
98    }
99
100    @Test(expected=ExceptionWithContext.class)
101    public void testReadOptionalUintTooLarge3() {
102        BaseDexBuffer dexBuf = new BaseDexBuffer(new byte[] {(byte)0xfe, (byte)0xff, (byte)0xff, (byte)0xff});
103        dexBuf.readSmallUint(0);
104    }
105
106    @Test
107    public void testReadUshort() {
108        BaseDexBuffer dexBuf = new BaseDexBuffer(new byte[] {0x11, 0x22});
109        Assert.assertEquals(dexBuf.readUshort(0), 0x2211);
110
111        dexBuf = new BaseDexBuffer(new byte[] {0x00, 0x00});
112        Assert.assertEquals(dexBuf.readUshort(0), 0);
113
114        dexBuf = new BaseDexBuffer(new byte[] {(byte)0xff, (byte)0xff});
115        Assert.assertEquals(dexBuf.readUshort(0), 0xffff);
116
117        dexBuf = new BaseDexBuffer(new byte[] {(byte)0x00, (byte)0x80});
118        Assert.assertEquals(dexBuf.readUshort(0), 0x8000);
119
120        dexBuf = new BaseDexBuffer(new byte[] {(byte)0xff, (byte)0x7f});
121        Assert.assertEquals(dexBuf.readUshort(0), 0x7fff);
122    }
123
124    @Test
125    public void testReadUbyte() {
126        byte[] buf = new byte[1];
127        BaseDexBuffer dexBuf = new BaseDexBuffer(buf);
128
129        for (int i=0; i<=0xff; i++) {
130            buf[0] = (byte)i;
131            Assert.assertEquals(i, dexBuf.readUbyte(0));
132        }
133    }
134
135    @Test
136    public void testReadLong() {
137        BaseDexBuffer dexBuf = new BaseDexBuffer(new byte[] {0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77});
138        Assert.assertEquals(0x7766554433221100L, dexBuf.readLong(0));
139
140        dexBuf = new BaseDexBuffer(new byte[] {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00});
141        Assert.assertEquals(0, dexBuf.readLong(0));
142
143        dexBuf = new BaseDexBuffer(new byte[] {(byte)0xff, (byte)0xff, (byte)0xff, (byte)0xff,
144                (byte)0xff, (byte)0xff, (byte)0xff, 0x7f});
145        Assert.assertEquals(Long.MAX_VALUE, dexBuf.readLong(0));
146
147        dexBuf = new BaseDexBuffer(new byte[] {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, (byte)0x80});
148        Assert.assertEquals(Long.MIN_VALUE, dexBuf.readLong(0));
149
150        dexBuf = new BaseDexBuffer(new byte[] {(byte)0xff, (byte)0xff, (byte)0xff, (byte)0xff,
151                (byte)0xff, (byte)0xff, (byte)0xff, (byte)0x80});
152        Assert.assertEquals(0x80ffffffffffffffL, dexBuf.readLong(0));
153
154        dexBuf = new BaseDexBuffer(new byte[] {(byte)0xff, (byte)0xff, (byte)0xff, (byte)0xff,
155                (byte)0xff, (byte)0xff, (byte)0xff, (byte)0xff});
156        Assert.assertEquals(-1, dexBuf.readLong(0));
157
158    }
159
160    @Test
161    public void testReadInt() {
162        BaseDexBuffer dexBuf = new BaseDexBuffer(new byte[] {0x11, 0x22, 0x33, 0x44});
163        Assert.assertEquals(0x44332211, dexBuf.readInt(0));
164
165        dexBuf = new BaseDexBuffer(new byte[] {0x00, 0x00, 0x00, 0x00});
166        Assert.assertEquals(0, dexBuf.readInt(0));
167
168        dexBuf = new BaseDexBuffer(new byte[] {(byte)0xff, (byte)0xff, (byte)0xff, 0x7f});
169        Assert.assertEquals(Integer.MAX_VALUE, dexBuf.readInt(0));
170
171        dexBuf = new BaseDexBuffer(new byte[] {0x00, 0x00, 0x00, (byte)0x80});
172        Assert.assertEquals(Integer.MIN_VALUE, dexBuf.readInt(0));
173
174        dexBuf = new BaseDexBuffer(new byte[] {(byte)0xff, (byte)0xff, (byte)0xff, (byte)0x80});
175        Assert.assertEquals(0x80ffffff, dexBuf.readInt(0));
176
177        dexBuf = new BaseDexBuffer(new byte[] {(byte)0xff, (byte)0xff, (byte)0xff, (byte)0xff});
178        Assert.assertEquals(-1, dexBuf.readInt(0));
179    }
180
181    @Test
182    public void testReadShort() {
183        BaseDexBuffer dexBuf = new BaseDexBuffer(new byte[] {0x11, 0x22});
184        Assert.assertEquals(dexBuf.readShort(0), 0x2211);
185
186        dexBuf = new BaseDexBuffer(new byte[] {0x00, 0x00});
187        Assert.assertEquals(dexBuf.readShort(0), 0);
188
189        dexBuf = new BaseDexBuffer(new byte[] {(byte)0xff, (byte)0xff});
190        Assert.assertEquals(dexBuf.readShort(0), -1);
191
192        dexBuf = new BaseDexBuffer(new byte[] {(byte)0x00, (byte)0x80});
193        Assert.assertEquals(dexBuf.readShort(0), Short.MIN_VALUE);
194
195        dexBuf = new BaseDexBuffer(new byte[] {(byte)0xff, (byte)0x7f});
196        Assert.assertEquals(dexBuf.readShort(0), 0x7fff);
197
198        dexBuf = new BaseDexBuffer(new byte[] {(byte)0xff, (byte)0x80});
199        Assert.assertEquals(dexBuf.readShort(0), 0xffff80ff);
200    }
201
202    @Test
203    public void testReadByte() {
204        byte[] buf = new byte[1];
205        BaseDexBuffer dexBuf = new BaseDexBuffer(buf);
206
207        for (int i=0; i<=0xff; i++) {
208            buf[0] = (byte)i;
209            Assert.assertEquals((byte)i, dexBuf.readByte(0));
210        }
211    }
212
213    @Test
214    public void testReadRandom() {
215        Random r = new Random(1234567890);
216        ByteBuffer byteBuf = ByteBuffer.allocateDirect(4).order(ByteOrder.LITTLE_ENDIAN);
217        byte[] buf = new byte[4];
218        BaseDexBuffer dexBuf = new BaseDexBuffer(buf);
219
220        for (int i=0; i<10000; i++) {
221            int val = r.nextInt();
222            byteBuf.putInt(0, val);
223            byteBuf.position(0);
224            byteBuf.get(buf);
225
226            boolean expectException = val < 0;
227            try {
228                int returnedVal = dexBuf.readSmallUint(0);
229                Assert.assertFalse(String.format("Didn't throw an exception for value: %x", val), expectException);
230                Assert.assertEquals(val, returnedVal);
231            } catch (Exception ex) {
232                Assert.assertTrue(String.format("Threw an exception for value: %x", val), expectException);
233            }
234
235            Assert.assertEquals(val, dexBuf.readInt(0));
236
237            Assert.assertEquals(val & 0xFFFF, dexBuf.readUshort(0));
238            Assert.assertEquals((val >> 8) & 0xFFFF, dexBuf.readUshort(1));
239            Assert.assertEquals((val >> 16) & 0xFFFF, dexBuf.readUshort(2));
240
241            Assert.assertEquals((short)val, dexBuf.readShort(0));
242            Assert.assertEquals((short)(val >> 8), dexBuf.readShort(1));
243            Assert.assertEquals((short)(val >> 16), dexBuf.readShort(2));
244        }
245    }
246
247    @Test
248    public void testReadLongRandom() {
249        Random r = new Random(1234567890);
250        ByteBuffer byteBuf = ByteBuffer.allocateDirect(8).order(ByteOrder.LITTLE_ENDIAN);
251        byte[] buf = new byte[8];
252        BaseDexBuffer dexBuf = new BaseDexBuffer(buf);
253
254        for (int i=0; i<10000; i++) {
255            int val = r.nextInt();
256            byteBuf.putLong(0, val);
257            byteBuf.position(0);
258            byteBuf.get(buf);
259
260            Assert.assertEquals(val, dexBuf.readLong(0));
261        }
262    }
263}
264