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
38public class BaseDexReaderTest {
39    @Test
40    public void testSizedInt() {
41        performSizedIntTest(0, new byte[]{0x00});
42        performSizedIntTest(0, new byte[]{0x00, 0x00});
43        performSizedIntTest(0, new byte[]{0x00, 0x00, 0x00});
44        performSizedIntTest(0, new byte[]{0x00, 0x00, 0x00, 0x00});
45        performSizedIntTest(1, new byte[]{0x01});
46        performSizedIntTest(1, new byte[]{0x01, 0x00, 0x00, 0x00});
47        performSizedIntTest(0x40, new byte[]{0x40});
48        performSizedIntTest(0x7f, new byte[]{0x7f});
49        performSizedIntTest(0xffffff80, new byte[]{(byte)0x80});
50        performSizedIntTest(-1, new byte[]{(byte)0xff});
51
52        performSizedIntTest(0x100, new byte[]{0x00, 0x01});
53        performSizedIntTest(0x110, new byte[]{0x10, 0x01});
54        performSizedIntTest(0x7f01, new byte[]{0x01, 0x7f});
55        performSizedIntTest(0xffff8001, new byte[]{0x01, (byte)0x80});
56        performSizedIntTest(0xffffff10, new byte[]{0x10, (byte)0xff});
57
58        performSizedIntTest(0x11001, new byte[]{0x01, 0x10, 0x01});
59        performSizedIntTest(0x7f0110, new byte[]{0x10, 0x01, 0x7f});
60        performSizedIntTest(0xff801001, new byte[]{0x01, 0x10, (byte)0x80});
61        performSizedIntTest(0xffff0110, new byte[]{0x10, 0x01, (byte)0xff});
62
63        performSizedIntTest(0x1003002, new byte[]{0x02, 0x30, 0x00, 0x01});
64        performSizedIntTest(0x7f110230, new byte[]{0x30, 0x02, 0x11, 0x7f});
65        performSizedIntTest(0x80112233, new byte[]{0x33, 0x22, 0x11, (byte)0x80});
66        performSizedIntTest(-1, new byte[]{(byte)0xff, (byte)0xff, (byte)0xff, (byte)0xff});
67    }
68
69    private void performSizedIntTest(int expectedValue, byte[] buf) {
70        BaseDexBuffer dexBuf = new BaseDexBuffer(buf);
71        BaseDexReader reader = dexBuf.readerAt(0);
72        Assert.assertEquals(expectedValue, reader.readSizedInt(buf.length));
73    }
74
75    @Test
76    public void testSizedIntFailure() {
77        // wrong size
78        performSizedIntFailureTest(new byte[]{});
79        performSizedIntFailureTest(new byte[]{0x00, 0x00, 0x00, 0x00, 0x00});
80        performSizedIntFailureTest(new byte[]{0x00, 0x00, 0x00, 0x00, 0x00, 0x00});
81        performSizedIntFailureTest(new byte[]{0x12, 0x34, 0x56, 0x12, 0x34, 0x56, 0x78});
82    }
83
84    private void performSizedIntFailureTest(byte[] buf) {
85        BaseDexBuffer dexBuf = new BaseDexBuffer(buf);
86        BaseDexReader reader = dexBuf.readerAt(0);
87        try {
88            reader.readSizedInt(buf.length);
89            Assert.fail();
90        } catch (ExceptionWithContext ex) {
91            // expected
92        }
93    }
94
95    @Test
96    public void testSizedSmallUint() {
97        performSizedSmallUintTest(0, new byte[]{0x00});
98        performSizedSmallUintTest(0, new byte[]{0x00, 0x00});
99        performSizedSmallUintTest(0, new byte[]{0x00, 0x00, 0x00});
100        performSizedSmallUintTest(0, new byte[]{0x00, 0x00, 0x00, 0x00});
101        performSizedSmallUintTest(1, new byte[]{0x01});
102        performSizedSmallUintTest(1, new byte[]{0x01, 0x00, 0x00, 0x00});
103        performSizedSmallUintTest(0x40, new byte[]{0x40});
104        performSizedSmallUintTest(0x7f, new byte[]{0x7f});
105        performSizedSmallUintTest(0x80, new byte[]{(byte)0x80});
106        performSizedSmallUintTest(0xff, new byte[]{(byte)0xff});
107
108        performSizedSmallUintTest(0x100, new byte[]{0x00, 0x01});
109        performSizedSmallUintTest(0x110, new byte[]{0x10, 0x01});
110        performSizedSmallUintTest(0x7f01, new byte[]{0x01, 0x7f});
111        performSizedSmallUintTest(0x8001, new byte[]{0x01, (byte)0x80});
112        performSizedSmallUintTest(0xff10, new byte[]{0x10, (byte)0xff});
113
114        performSizedSmallUintTest(0x11001, new byte[]{0x01, 0x10, 0x01});
115        performSizedSmallUintTest(0x7f0110, new byte[]{0x10, 0x01, 0x7f});
116        performSizedSmallUintTest(0x801001, new byte[]{0x01, 0x10, (byte)0x80});
117        performSizedSmallUintTest(0xff0110, new byte[]{0x10, 0x01, (byte)0xff});
118
119        performSizedSmallUintTest(0x1003002, new byte[]{0x02, 0x30, 0x00, 0x01});
120        performSizedSmallUintTest(0x7f110230, new byte[]{0x30, 0x02, 0x11, 0x7f});
121        performSizedSmallUintTest(Integer.MAX_VALUE, new byte[]{(byte)0xff, (byte)0xff, (byte)0xff, 0x7f});
122    }
123
124    private void performSizedSmallUintTest(int expectedValue, byte[] buf) {
125        BaseDexBuffer dexBuf = new BaseDexBuffer(buf);
126        BaseDexReader reader = dexBuf.readerAt(0);
127        Assert.assertEquals(expectedValue, reader.readSizedSmallUint(buf.length));
128    }
129
130    @Test
131    public void testSizedSmallUintFailure() {
132        // wrong size
133        performSizedSmallUintFailureTest(new byte[]{});
134        performSizedSmallUintFailureTest(new byte[]{0x00, 0x00, 0x00, 0x00, 0x00});
135        performSizedSmallUintFailureTest(new byte[]{0x00, 0x00, 0x00, 0x00, 0x00, 0x00});
136        performSizedSmallUintFailureTest(new byte[]{0x12, 0x34, 0x56, 0x12, 0x34, 0x56, 0x78});
137
138        // MSB set
139        performSizedSmallUintFailureTest(new byte[]{0x00, 0x00, 0x00, (byte)0x80});
140        performSizedSmallUintFailureTest(new byte[]{(byte)0xff, (byte)0xff, (byte)0xff, (byte)0xff});
141    }
142
143    private void performSizedSmallUintFailureTest(byte[] buf) {
144        BaseDexBuffer dexBuf = new BaseDexBuffer(buf);
145        BaseDexReader reader = dexBuf.readerAt(0);
146        try {
147            reader.readSizedSmallUint(buf.length);
148            Assert.fail();
149        } catch (ExceptionWithContext ex) {
150            // expected
151        }
152    }
153
154    @Test
155    public void testSizedRightExtendedInt() {
156        performSizedRightExtendedIntTest(0, new byte[]{0x00});
157        performSizedRightExtendedIntTest(0, new byte[]{0x00, 0x00});
158        performSizedRightExtendedIntTest(0, new byte[]{0x00, 0x00, 0x00});
159        performSizedRightExtendedIntTest(0, new byte[]{0x00, 0x00, 0x00, 0x00});
160
161        performSizedRightExtendedIntTest(0x01000000, new byte[]{0x01});
162        performSizedRightExtendedIntTest(0x7f000000, new byte[]{0x7f});
163        performSizedRightExtendedIntTest(0x80000000, new byte[]{(byte) 0x80});
164        performSizedRightExtendedIntTest(0xf0000000, new byte[]{(byte) 0xf0});
165        performSizedRightExtendedIntTest(0xff000000, new byte[]{(byte) 0xff});
166
167        performSizedRightExtendedIntTest(0x010000, new byte[]{0x01, 0x00});
168        performSizedRightExtendedIntTest(0x01100000, new byte[]{0x10, 0x01});
169        performSizedRightExtendedIntTest(0x7f100000, new byte[]{0x10, 0x7f});
170        performSizedRightExtendedIntTest(0x80100000, new byte[]{0x10, (byte) 0x80});
171        performSizedRightExtendedIntTest(0xf0100000, new byte[]{0x10, (byte) 0xf0});
172        performSizedRightExtendedIntTest(0xff100000, new byte[]{0x10, (byte) 0xff});
173        performSizedRightExtendedIntTest(0xff000000, new byte[]{0x00, (byte) 0xff});
174
175        performSizedRightExtendedIntTest(0x0100, new byte[]{0x01, 0x00, 0x00});
176        performSizedRightExtendedIntTest(0x01101000, new byte[]{0x10, 0x10, 0x01});
177        performSizedRightExtendedIntTest(0x7f101000, new byte[]{0x10, 0x10, 0x7f});
178        performSizedRightExtendedIntTest(0x80101000, new byte[]{0x10, 0x10, (byte) 0x80});
179        performSizedRightExtendedIntTest(0xf0101000, new byte[]{0x10, 0x10, (byte) 0xf0});
180        performSizedRightExtendedIntTest(0xff101000, new byte[]{0x10, 0x10, (byte) 0xff});
181        performSizedRightExtendedIntTest(0xff000000, new byte[]{0x00, 0x00, (byte) 0xff});
182
183        performSizedRightExtendedIntTest(0x01, new byte[]{0x01, 0x00, 0x00, 0x00});
184        performSizedRightExtendedIntTest(0x80, new byte[]{(byte) 0x80, 0x00, 0x00, 0x00});
185        performSizedRightExtendedIntTest(0xff, new byte[]{(byte) 0xff, 0x00, 0x00, 0x00});
186        performSizedRightExtendedIntTest(0x01101010, new byte[]{0x10, 0x10, 0x10, 0x01});
187        performSizedRightExtendedIntTest(0x7f101010, new byte[]{0x10, 0x10, 0x10, 0x7f});
188        performSizedRightExtendedIntTest(0x80101010, new byte[]{0x10, 0x10, 0x10, (byte) 0x80});
189        performSizedRightExtendedIntTest(0xf0101010, new byte[]{0x10, 0x10, 0x10, (byte) 0xf0});
190        performSizedRightExtendedIntTest(0xff101010, new byte[]{0x10, 0x10, 0x10, (byte) 0xff});
191        performSizedRightExtendedIntTest(0xff000000, new byte[]{0x00, 0x00, 0x00, (byte) 0xff});
192    }
193
194    private void performSizedRightExtendedIntTest(int expectedValue, byte[] buf) {
195        BaseDexBuffer dexBuf = new BaseDexBuffer(buf);
196        BaseDexReader reader = dexBuf.readerAt(0);
197        Assert.assertEquals(expectedValue, reader.readSizedRightExtendedInt(buf.length));
198    }
199
200    @Test
201    public void testSizedRightExtendedIntFailure() {
202        // wrong size
203        performSizedRightExtendedIntFailureTest(new byte[]{});
204        performSizedRightExtendedIntFailureTest(new byte[]{0x00, 0x00, 0x00, 0x00, 0x00});
205        performSizedRightExtendedIntFailureTest(new byte[]{0x00, 0x00, 0x00, 0x00, 0x00, 0x00});
206        performSizedRightExtendedIntFailureTest(new byte[]{0x12, 0x34, 0x56, 0x12, 0x34, 0x56, 0x78});
207    }
208
209    private void performSizedRightExtendedIntFailureTest(byte[] buf) {
210        BaseDexBuffer dexBuf = new BaseDexBuffer(buf);
211        BaseDexReader reader = dexBuf.readerAt(0);
212        try {
213            reader.readSizedRightExtendedInt(buf.length);
214            Assert.fail();
215        } catch (ExceptionWithContext ex) {
216            // expected
217        }
218    }
219
220    @Test
221    public void testSizedRightExtendedLong() {
222        performSizedRightExtendedLongTest(0, new byte[]{0x00});
223        performSizedRightExtendedLongTest(0, new byte[]{0x00, 0x00});
224        performSizedRightExtendedLongTest(0, new byte[]{0x00, 0x00, 0x00});
225        performSizedRightExtendedLongTest(0, new byte[]{0x00, 0x00, 0x00, 0x00});
226        performSizedRightExtendedLongTest(0, new byte[]{0x00, 0x00, 0x00, 0x00, 0x00});
227        performSizedRightExtendedLongTest(0, new byte[]{0x00, 0x00, 0x00, 0x00, 0x00, 0x00});
228        performSizedRightExtendedLongTest(0, new byte[]{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00});
229        performSizedRightExtendedLongTest(0, new byte[]{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00});
230
231        performSizedRightExtendedLongTest(0x0100000000000000L, new byte[]{0x01});
232        performSizedRightExtendedLongTest(0x7f00000000000000L, new byte[]{0x7f});
233        performSizedRightExtendedLongTest(0x8000000000000000L, new byte[]{(byte)0x80});
234        performSizedRightExtendedLongTest(0xf000000000000000L, new byte[]{(byte)0xf0});
235        performSizedRightExtendedLongTest(0xff00000000000000L, new byte[]{(byte)0xff});
236
237        performSizedRightExtendedLongTest(0x01000000000000L, new byte[]{0x01, 0x00});
238        performSizedRightExtendedLongTest(0x0110000000000000L, new byte[]{0x10, 0x01});
239        performSizedRightExtendedLongTest(0x7f10000000000000L, new byte[]{0x10, 0x7f});
240        performSizedRightExtendedLongTest(0x8010000000000000L, new byte[]{0x10, (byte)0x80});
241        performSizedRightExtendedLongTest(0xf010000000000000L, new byte[]{0x10, (byte)0xf0});
242        performSizedRightExtendedLongTest(0xff10000000000000L, new byte[]{0x10, (byte)0xff});
243        performSizedRightExtendedLongTest(0xff00000000000000L, new byte[]{0x00, (byte)0xff});
244        performSizedRightExtendedLongTest(0x7fff000000000000L, new byte[]{(byte)0xff, (byte)0x7f});
245
246        performSizedRightExtendedLongTest(0x010000000000L, new byte[]{0x01, 0x00, 0x00});
247        performSizedRightExtendedLongTest(0x0110100000000000L, new byte[]{0x10, 0x10, 0x01});
248        performSizedRightExtendedLongTest(0x7f10100000000000L, new byte[]{0x10, 0x10, 0x7f});
249        performSizedRightExtendedLongTest(0x8010100000000000L, new byte[]{0x10, 0x10, (byte)0x80});
250        performSizedRightExtendedLongTest(0xf010100000000000L, new byte[]{0x10, 0x10, (byte)0xf0});
251        performSizedRightExtendedLongTest(0xff10100000000000L, new byte[]{0x10, 0x10, (byte)0xff});
252        performSizedRightExtendedLongTest(0xff00000000000000L, new byte[]{0x00, 0x00, (byte)0xff});
253        performSizedRightExtendedLongTest(0x7fffff0000000000L, new byte[]{(byte)0xff, (byte)0xff, (byte)0x7f});
254
255        performSizedRightExtendedLongTest(0x0100000000L, new byte[]{0x01, 0x00, 0x00, 0x00});
256        performSizedRightExtendedLongTest(0x0110101000000000L, new byte[]{0x10, 0x10, 0x10, 0x01});
257        performSizedRightExtendedLongTest(0x7f10101000000000L, new byte[]{0x10, 0x10, 0x10, 0x7f});
258        performSizedRightExtendedLongTest(0x8010101000000000L, new byte[]{0x10, 0x10, 0x10, (byte)0x80});
259        performSizedRightExtendedLongTest(0xf010101000000000L, new byte[]{0x10, 0x10, 0x10, (byte)0xf0});
260        performSizedRightExtendedLongTest(0xff10101000000000L, new byte[]{0x10, 0x10, 0x10, (byte)0xff});
261        performSizedRightExtendedLongTest(0xff00000000000000L, new byte[]{0x00, 0x00, 0x00, (byte)0xff});
262        performSizedRightExtendedLongTest(0x7fffffff00000000L, new byte[]{(byte)0xff, (byte)0xff, (byte)0xff, (byte)0x7f});
263
264        performSizedRightExtendedLongTest(0x01000000L, new byte[]{0x01, 0x00, 0x00, 0x00, 0x00});
265        performSizedRightExtendedLongTest(0x0110101010000000L, new byte[]{0x10, 0x10, 0x10, 0x10, 0x01});
266        performSizedRightExtendedLongTest(0x7f10101010000000L, new byte[]{0x10, 0x10, 0x10, 0x10, 0x7f});
267        performSizedRightExtendedLongTest(0x8010101010000000L, new byte[]{0x10, 0x10, 0x10, 0x10, (byte)0x80});
268        performSizedRightExtendedLongTest(0xf010101010000000L, new byte[]{0x10, 0x10, 0x10, 0x10, (byte)0xf0});
269        performSizedRightExtendedLongTest(0xff10101010000000L, new byte[]{0x10, 0x10, 0x10, 0x10, (byte)0xff});
270        performSizedRightExtendedLongTest(0xff00000000000000L, new byte[]{0x00, 0x00, 0x00, 0x00, (byte)0xff});
271        performSizedRightExtendedLongTest(0x7fffffffff000000L, new byte[]{(byte)0xff, (byte)0xff, (byte)0xff, (byte)0xff, (byte)0x7f});
272
273        performSizedRightExtendedLongTest(0x010000L, new byte[]{0x01, 0x00, 0x00, 0x00, 0x00, 0x00});
274        performSizedRightExtendedLongTest(0x0110101010100000L, new byte[]{0x10, 0x10, 0x10, 0x10, 0x10, 0x01});
275        performSizedRightExtendedLongTest(0x7f10101010100000L, new byte[]{0x10, 0x10, 0x10, 0x10, 0x10, 0x7f});
276        performSizedRightExtendedLongTest(0x8010101010100000L, new byte[]{0x10, 0x10, 0x10, 0x10, 0x10, (byte)0x80});
277        performSizedRightExtendedLongTest(0xf010101010100000L, new byte[]{0x10, 0x10, 0x10, 0x10, 0x10, (byte)0xf0});
278        performSizedRightExtendedLongTest(0xff10101010100000L, new byte[]{0x10, 0x10, 0x10, 0x10, 0x10, (byte)0xff});
279        performSizedRightExtendedLongTest(0xff00000000000000L, new byte[]{0x00, 0x00, 0x00, 0x00, 0x00, (byte)0xff});
280        performSizedRightExtendedLongTest(0x7fffffffffff0000L, new byte[]{(byte)0xff, (byte)0xff, (byte)0xff, (byte)0xff, (byte)0xff, (byte)0x7f});
281
282        performSizedRightExtendedLongTest(0x0100L, new byte[]{0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00});
283        performSizedRightExtendedLongTest(0x0110101010101000L, new byte[]{0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x01});
284        performSizedRightExtendedLongTest(0x7f10101010101000L, new byte[]{0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x7f});
285        performSizedRightExtendedLongTest(0x8010101010101000L, new byte[]{0x10, 0x10, 0x10, 0x10, 0x10, 0x10, (byte)0x80});
286        performSizedRightExtendedLongTest(0xf010101010101000L, new byte[]{0x10, 0x10, 0x10, 0x10, 0x10, 0x10, (byte)0xf0});
287        performSizedRightExtendedLongTest(0xff10101010101000L, new byte[]{0x10, 0x10, 0x10, 0x10, 0x10, 0x10, (byte)0xff});
288        performSizedRightExtendedLongTest(0xff00000000000000L, new byte[]{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, (byte)0xff});
289        performSizedRightExtendedLongTest(0x7fffffffffffff00L, new byte[]{(byte)0xff, (byte)0xff, (byte)0xff, (byte)0xff, (byte)0xff, (byte)0xff, (byte)0x7f});
290
291        performSizedRightExtendedLongTest(0x01L, new byte[]{0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00});
292        performSizedRightExtendedLongTest(0x0110101010101010L, new byte[]{0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x01});
293        performSizedRightExtendedLongTest(0x7f10101010101010L, new byte[]{0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x7f});
294        performSizedRightExtendedLongTest(0x8010101010101010L, new byte[]{0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, (byte)0x80});
295        performSizedRightExtendedLongTest(0xf010101010101010L, new byte[]{0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, (byte)0xf0});
296        performSizedRightExtendedLongTest(0xff10101010101010L, new byte[]{0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, (byte)0xff});
297        performSizedRightExtendedLongTest(0xff00000000000000L, new byte[]{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, (byte)0xff});
298        performSizedRightExtendedLongTest(Long.MAX_VALUE, new byte[]{(byte)0xff, (byte)0xff, (byte)0xff, (byte)0xff, (byte)0xff, (byte)0xff, (byte)0xff, (byte)0x7f});
299        performSizedRightExtendedLongTest(Long.MIN_VALUE, new byte[]{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, (byte)0x80});
300        performSizedRightExtendedLongTest(-1, new byte[]{(byte)0xff, (byte)0xff, (byte)0xff, (byte)0xff, (byte)0xff, (byte)0xff, (byte)0xff, (byte)0xff});
301    }
302
303    private void performSizedRightExtendedLongTest(long expectedValue, byte[] buf) {
304        BaseDexBuffer dexBuf = new BaseDexBuffer(buf);
305        BaseDexReader reader = dexBuf.readerAt(0);
306        Assert.assertEquals(expectedValue, reader.readSizedRightExtendedLong(buf.length));
307    }
308
309    @Test
310    public void testSizedRightExtendedLongFailure() {
311        // wrong size
312        performSizedRightExtendedLongFailureTest(new byte[]{});
313        performSizedRightExtendedLongFailureTest(new byte[]{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00});
314        performSizedRightExtendedLongFailureTest(new byte[]{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00});
315        performSizedRightExtendedLongFailureTest(new byte[]{0x12, 0x34, 0x56, 0x12, 0x34, 0x56, 0x78, (byte)0x89, (byte)0x90, 0x01});
316    }
317
318    private void performSizedRightExtendedLongFailureTest(byte[] buf) {
319        BaseDexBuffer dexBuf = new BaseDexBuffer(buf);
320        BaseDexReader reader = dexBuf.readerAt(0);
321        try {
322            reader.readSizedRightExtendedLong(buf.length);
323            Assert.fail();
324        } catch (ExceptionWithContext ex) {
325            // expected
326        }
327    }
328
329    @Test
330    public void testSizedLong() {
331        performSizedLongTest(0, new byte[]{0x00});
332        performSizedLongTest(0, new byte[]{0x00, 0x00});
333        performSizedLongTest(0, new byte[]{0x00, 0x00, 0x00});
334        performSizedLongTest(0, new byte[]{0x00, 0x00, 0x00, 0x00});
335        performSizedLongTest(0, new byte[]{0x00, 0x00, 0x00, 0x00, 0x00});
336        performSizedLongTest(0, new byte[]{0x00, 0x00, 0x00, 0x00, 0x00, 0x00});
337        performSizedLongTest(0, new byte[]{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00});
338        performSizedLongTest(0, new byte[]{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00});
339
340        performSizedLongTest(0x01L, new byte[]{0x01});
341        performSizedLongTest(0x7fL, new byte[]{0x7f});
342        performSizedLongTest(0xffffffffffffff80L, new byte[]{(byte)0x80});
343        performSizedLongTest(0xfffffffffffffff0L, new byte[]{(byte)0xf0});
344        performSizedLongTest(0xffffffffffffffffL, new byte[]{(byte)0xff});
345
346        performSizedLongTest(0x01L, new byte[]{0x01, 0x00});
347        performSizedLongTest(0x0110L, new byte[]{0x10, 0x01});
348        performSizedLongTest(0x7f10L, new byte[]{0x10, 0x7f});
349        performSizedLongTest(0xffffffffffff8010L, new byte[]{0x10, (byte)0x80});
350        performSizedLongTest(0xfffffffffffff010L, new byte[]{0x10, (byte)0xf0});
351        performSizedLongTest(0xffffffffffffff10L, new byte[]{0x10, (byte)0xff});
352        performSizedLongTest(0xffffffffffffff00L, new byte[]{0x00, (byte)0xff});
353        performSizedLongTest(0x7fffL, new byte[]{(byte)0xff, (byte)0x7f});
354
355        performSizedLongTest(0x01L, new byte[]{0x01, 0x00, 0x00});
356        performSizedLongTest(0x011010L, new byte[]{0x10, 0x10, 0x01});
357        performSizedLongTest(0x7f1010L, new byte[]{0x10, 0x10, 0x7f});
358        performSizedLongTest(0xffffffffff801010L, new byte[]{0x10, 0x10, (byte)0x80});
359        performSizedLongTest(0xfffffffffff01010L, new byte[]{0x10, 0x10, (byte)0xf0});
360        performSizedLongTest(0xffffffffffff1010L, new byte[]{0x10, 0x10, (byte)0xff});
361        performSizedLongTest(0xffffffffffff0000L, new byte[]{0x00, 0x00, (byte)0xff});
362        performSizedLongTest(0x7fffffL, new byte[]{(byte)0xff, (byte)0xff, (byte)0x7f});
363
364        performSizedLongTest(0x01L, new byte[]{0x01, 0x00, 0x00, 0x00});
365        performSizedLongTest(0x01101010L, new byte[]{0x10, 0x10, 0x10, 0x01});
366        performSizedLongTest(0x7f101010L, new byte[]{0x10, 0x10, 0x10, 0x7f});
367        performSizedLongTest(0xffffffff80101010L, new byte[]{0x10, 0x10, 0x10, (byte)0x80});
368        performSizedLongTest(0xfffffffff0101010l, new byte[]{0x10, 0x10, 0x10, (byte)0xf0});
369        performSizedLongTest(0xffffffffff101010L, new byte[]{0x10, 0x10, 0x10, (byte)0xff});
370        performSizedLongTest(0xffffffffff000000L, new byte[]{0x00, 0x00, 0x00, (byte)0xff});
371        performSizedLongTest(0x7fffffffL, new byte[]{(byte)0xff, (byte)0xff, (byte)0xff, (byte)0x7f});
372
373        performSizedLongTest(0x01, new byte[]{0x01, 0x00, 0x00, 0x00, 0x00});
374        performSizedLongTest(0x0110101010L, new byte[]{0x10, 0x10, 0x10, 0x10, 0x01});
375        performSizedLongTest(0x7f10101010L, new byte[]{0x10, 0x10, 0x10, 0x10, 0x7f});
376        performSizedLongTest(0xffffff8010101010L, new byte[]{0x10, 0x10, 0x10, 0x10, (byte)0x80});
377        performSizedLongTest(0xfffffff010101010L, new byte[]{0x10, 0x10, 0x10, 0x10, (byte)0xf0});
378        performSizedLongTest(0xffffffff10101010L, new byte[]{0x10, 0x10, 0x10, 0x10, (byte)0xff});
379        performSizedLongTest(0xffffffff00000000L, new byte[]{0x00, 0x00, 0x00, 0x00, (byte)0xff});
380        performSizedLongTest(0x7fffffffffL, new byte[]{(byte)0xff, (byte)0xff, (byte)0xff, (byte)0xff, (byte)0x7f});
381
382        performSizedLongTest(0x01L, new byte[]{0x01, 0x00, 0x00, 0x00, 0x00, 0x00});
383        performSizedLongTest(0x011010101010L, new byte[]{0x10, 0x10, 0x10, 0x10, 0x10, 0x01});
384        performSizedLongTest(0x7f1010101010L, new byte[]{0x10, 0x10, 0x10, 0x10, 0x10, 0x7f});
385        performSizedLongTest(0xffff801010101010L, new byte[]{0x10, 0x10, 0x10, 0x10, 0x10, (byte)0x80});
386        performSizedLongTest(0xfffff01010101010L, new byte[]{0x10, 0x10, 0x10, 0x10, 0x10, (byte)0xf0});
387        performSizedLongTest(0xffffff1010101010L, new byte[]{0x10, 0x10, 0x10, 0x10, 0x10, (byte)0xff});
388        performSizedLongTest(0xffffff0000000000L, new byte[]{0x00, 0x00, 0x00, 0x00, 0x00, (byte)0xff});
389        performSizedLongTest(0x7fffffffffffL, new byte[]{(byte)0xff, (byte)0xff, (byte)0xff, (byte)0xff, (byte)0xff, (byte)0x7f});
390
391        performSizedLongTest(0x01L, new byte[]{0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00});
392        performSizedLongTest(0x01101010101010L, new byte[]{0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x01});
393        performSizedLongTest(0x7f101010101010L, new byte[]{0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x7f});
394        performSizedLongTest(0xff80101010101010L, new byte[]{0x10, 0x10, 0x10, 0x10, 0x10, 0x10, (byte)0x80});
395        performSizedLongTest(0xfff0101010101010L, new byte[]{0x10, 0x10, 0x10, 0x10, 0x10, 0x10, (byte)0xf0});
396        performSizedLongTest(0xffff101010101010L, new byte[]{0x10, 0x10, 0x10, 0x10, 0x10, 0x10, (byte)0xff});
397        performSizedLongTest(0xffff000000000000L, new byte[]{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, (byte)0xff});
398        performSizedLongTest(0x7fffffffffffffL, new byte[]{(byte)0xff, (byte)0xff, (byte)0xff, (byte)0xff, (byte)0xff, (byte)0xff, (byte)0x7f});
399
400        performSizedLongTest(0x01L, new byte[]{0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00});
401        performSizedLongTest(0x0110101010101010L, new byte[]{0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x01});
402        performSizedLongTest(0x7f10101010101010L, new byte[]{0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x7f});
403        performSizedLongTest(0x8010101010101010L, new byte[]{0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, (byte)0x80});
404        performSizedLongTest(0xf010101010101010L, new byte[]{0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, (byte)0xf0});
405        performSizedLongTest(0xff10101010101010L, new byte[]{0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, (byte)0xff});
406        performSizedLongTest(0xff00000000000000L, new byte[]{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, (byte)0xff});
407        performSizedLongTest(Long.MAX_VALUE, new byte[]{(byte)0xff, (byte)0xff, (byte)0xff, (byte)0xff, (byte)0xff, (byte)0xff, (byte)0xff, (byte)0x7f});
408        performSizedLongTest(Long.MIN_VALUE, new byte[]{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, (byte)0x80});
409        performSizedLongTest(-1, new byte[]{(byte)0xff, (byte)0xff, (byte)0xff, (byte)0xff, (byte)0xff, (byte)0xff, (byte)0xff, (byte)0xff});
410    }
411
412    private void performSizedLongTest(long expectedValue, byte[] buf) {
413        BaseDexBuffer dexBuf = new BaseDexBuffer(buf);
414        BaseDexReader reader = dexBuf.readerAt(0);
415        Assert.assertEquals(expectedValue, reader.readSizedLong(buf.length));
416    }
417
418    @Test
419    public void testSizedLongFailure() {
420        // wrong size
421        performSizedLongFailureTest(new byte[]{});
422        performSizedLongFailureTest(new byte[]{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00});
423        performSizedLongFailureTest(new byte[]{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00});
424        performSizedLongFailureTest(new byte[]{0x12, 0x34, 0x56, 0x12, 0x34, 0x56, 0x78, (byte)0x89, (byte)0x90, 0x01});
425    }
426
427    private void performSizedLongFailureTest(byte[] buf) {
428        BaseDexBuffer dexBuf = new BaseDexBuffer(buf);
429        BaseDexReader reader = dexBuf.readerAt(0);
430        try {
431            reader.readSizedLong(buf.length);
432            Assert.fail();
433        } catch (ExceptionWithContext ex) {
434            // expected
435        }
436    }
437}
438