1ceb1b0bfaea56251796b08c07b963de7403d84ebAnonymous Coward/*
2ceb1b0bfaea56251796b08c07b963de7403d84ebAnonymous Coward * Copyright (C) 2012 The Android Open Source Project
3ceb1b0bfaea56251796b08c07b963de7403d84ebAnonymous Coward *
4ceb1b0bfaea56251796b08c07b963de7403d84ebAnonymous Coward * Licensed under the Apache License, Version 2.0 (the "License");
5ceb1b0bfaea56251796b08c07b963de7403d84ebAnonymous Coward * you may not use this file except in compliance with the License.
6ceb1b0bfaea56251796b08c07b963de7403d84ebAnonymous Coward * You may obtain a copy of the License at
7ceb1b0bfaea56251796b08c07b963de7403d84ebAnonymous Coward *
8ceb1b0bfaea56251796b08c07b963de7403d84ebAnonymous Coward *      http://www.apache.org/licenses/LICENSE-2.0
9ceb1b0bfaea56251796b08c07b963de7403d84ebAnonymous Coward *
10ceb1b0bfaea56251796b08c07b963de7403d84ebAnonymous Coward * Unless required by applicable law or agreed to in writing, software
11ceb1b0bfaea56251796b08c07b963de7403d84ebAnonymous Coward * distributed under the License is distributed on an "AS IS" BASIS,
12ceb1b0bfaea56251796b08c07b963de7403d84ebAnonymous Coward * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13ceb1b0bfaea56251796b08c07b963de7403d84ebAnonymous Coward * See the License for the specific language governing permissions and
14ceb1b0bfaea56251796b08c07b963de7403d84ebAnonymous Coward * limitations under the License.
15ceb1b0bfaea56251796b08c07b963de7403d84ebAnonymous Coward */
16ceb1b0bfaea56251796b08c07b963de7403d84ebAnonymous Coward
17ceb1b0bfaea56251796b08c07b963de7403d84ebAnonymous Cowardpackage android.content.pm;
18ceb1b0bfaea56251796b08c07b963de7403d84ebAnonymous Coward
19ceb1b0bfaea56251796b08c07b963de7403d84ebAnonymous Cowardimport android.test.AndroidTestCase;
20ceb1b0bfaea56251796b08c07b963de7403d84ebAnonymous Cowardimport android.test.suitebuilder.annotation.MediumTest;
21ceb1b0bfaea56251796b08c07b963de7403d84ebAnonymous Coward
22ceb1b0bfaea56251796b08c07b963de7403d84ebAnonymous Cowardimport java.io.ByteArrayInputStream;
23ceb1b0bfaea56251796b08c07b963de7403d84ebAnonymous Cowardimport java.io.IOException;
24ceb1b0bfaea56251796b08c07b963de7403d84ebAnonymous Cowardimport java.io.InputStream;
25ceb1b0bfaea56251796b08c07b963de7403d84ebAnonymous Cowardimport java.util.Arrays;
26ceb1b0bfaea56251796b08c07b963de7403d84ebAnonymous Coward
27ceb1b0bfaea56251796b08c07b963de7403d84ebAnonymous Cowardpublic class LimitedLengthInputStreamTest extends AndroidTestCase {
28ceb1b0bfaea56251796b08c07b963de7403d84ebAnonymous Coward    private final byte[] TEST_STRING1 = "This is a test".getBytes();
29ceb1b0bfaea56251796b08c07b963de7403d84ebAnonymous Coward
30ceb1b0bfaea56251796b08c07b963de7403d84ebAnonymous Coward    private InputStream mTestStream1;
31ceb1b0bfaea56251796b08c07b963de7403d84ebAnonymous Coward
32ceb1b0bfaea56251796b08c07b963de7403d84ebAnonymous Coward    @Override
33ceb1b0bfaea56251796b08c07b963de7403d84ebAnonymous Coward    protected void setUp() throws Exception {
34ceb1b0bfaea56251796b08c07b963de7403d84ebAnonymous Coward        super.setUp();
35ceb1b0bfaea56251796b08c07b963de7403d84ebAnonymous Coward
36ceb1b0bfaea56251796b08c07b963de7403d84ebAnonymous Coward        mTestStream1 = new ByteArrayInputStream(TEST_STRING1);
37ceb1b0bfaea56251796b08c07b963de7403d84ebAnonymous Coward    }
38ceb1b0bfaea56251796b08c07b963de7403d84ebAnonymous Coward
39ceb1b0bfaea56251796b08c07b963de7403d84ebAnonymous Coward    @MediumTest
40ceb1b0bfaea56251796b08c07b963de7403d84ebAnonymous Coward    public void testConstructor_NegativeOffset_Failure() throws Exception {
41ceb1b0bfaea56251796b08c07b963de7403d84ebAnonymous Coward        try {
42ceb1b0bfaea56251796b08c07b963de7403d84ebAnonymous Coward            InputStream is = new LimitedLengthInputStream(mTestStream1, -1, TEST_STRING1.length);
43ceb1b0bfaea56251796b08c07b963de7403d84ebAnonymous Coward            fail("Should throw IOException on negative index");
44ceb1b0bfaea56251796b08c07b963de7403d84ebAnonymous Coward        } catch (IOException e) {
45ceb1b0bfaea56251796b08c07b963de7403d84ebAnonymous Coward            // success
46ceb1b0bfaea56251796b08c07b963de7403d84ebAnonymous Coward        }
47ceb1b0bfaea56251796b08c07b963de7403d84ebAnonymous Coward    }
48ceb1b0bfaea56251796b08c07b963de7403d84ebAnonymous Coward
49ceb1b0bfaea56251796b08c07b963de7403d84ebAnonymous Coward    @MediumTest
50ceb1b0bfaea56251796b08c07b963de7403d84ebAnonymous Coward    public void testConstructor_NegativeLength_Failure() throws Exception {
51ceb1b0bfaea56251796b08c07b963de7403d84ebAnonymous Coward        try {
52ceb1b0bfaea56251796b08c07b963de7403d84ebAnonymous Coward            InputStream is = new LimitedLengthInputStream(mTestStream1, 0, -1);
53ceb1b0bfaea56251796b08c07b963de7403d84ebAnonymous Coward            fail("Should throw IOException on negative length");
54ceb1b0bfaea56251796b08c07b963de7403d84ebAnonymous Coward        } catch (IOException e) {
55ceb1b0bfaea56251796b08c07b963de7403d84ebAnonymous Coward            // success
56ceb1b0bfaea56251796b08c07b963de7403d84ebAnonymous Coward        }
57ceb1b0bfaea56251796b08c07b963de7403d84ebAnonymous Coward    }
58ceb1b0bfaea56251796b08c07b963de7403d84ebAnonymous Coward
59ceb1b0bfaea56251796b08c07b963de7403d84ebAnonymous Coward    @MediumTest
60ceb1b0bfaea56251796b08c07b963de7403d84ebAnonymous Coward    public void testConstructor_NullInputStream_Failure() throws Exception {
61ceb1b0bfaea56251796b08c07b963de7403d84ebAnonymous Coward        try {
62ceb1b0bfaea56251796b08c07b963de7403d84ebAnonymous Coward            InputStream is = new LimitedLengthInputStream(null, 0, 1);
63ceb1b0bfaea56251796b08c07b963de7403d84ebAnonymous Coward            fail("Should throw IOException on null input stream");
64ceb1b0bfaea56251796b08c07b963de7403d84ebAnonymous Coward        } catch (IOException e) {
65ceb1b0bfaea56251796b08c07b963de7403d84ebAnonymous Coward            // success
66ceb1b0bfaea56251796b08c07b963de7403d84ebAnonymous Coward        }
67ceb1b0bfaea56251796b08c07b963de7403d84ebAnonymous Coward    }
68ceb1b0bfaea56251796b08c07b963de7403d84ebAnonymous Coward
69103d53005e7a3c2735f4ac76fa9b795a7e7e39d7Kenny Root    @MediumTest
70103d53005e7a3c2735f4ac76fa9b795a7e7e39d7Kenny Root    public void testConstructor_OffsetLengthOverflow_Fail() throws Exception {
71103d53005e7a3c2735f4ac76fa9b795a7e7e39d7Kenny Root        try {
72103d53005e7a3c2735f4ac76fa9b795a7e7e39d7Kenny Root        InputStream is = new LimitedLengthInputStream(mTestStream1, Long.MAX_VALUE - 1,
73103d53005e7a3c2735f4ac76fa9b795a7e7e39d7Kenny Root                Long.MAX_VALUE - 1);
74103d53005e7a3c2735f4ac76fa9b795a7e7e39d7Kenny Root            fail("Should fail when offset + length is > Long.MAX_VALUE");
75103d53005e7a3c2735f4ac76fa9b795a7e7e39d7Kenny Root        } catch (IOException e) {
76103d53005e7a3c2735f4ac76fa9b795a7e7e39d7Kenny Root            // success
77103d53005e7a3c2735f4ac76fa9b795a7e7e39d7Kenny Root        }
78103d53005e7a3c2735f4ac76fa9b795a7e7e39d7Kenny Root    }
79103d53005e7a3c2735f4ac76fa9b795a7e7e39d7Kenny Root
80ceb1b0bfaea56251796b08c07b963de7403d84ebAnonymous Coward    private void checkReadBytesWithOffsetAndLength_WithString1(int offset, int length)
81ceb1b0bfaea56251796b08c07b963de7403d84ebAnonymous Coward            throws Exception {
82ceb1b0bfaea56251796b08c07b963de7403d84ebAnonymous Coward        byte[] temp = new byte[TEST_STRING1.length];
83ceb1b0bfaea56251796b08c07b963de7403d84ebAnonymous Coward        byte[] expected = new byte[length];
84ceb1b0bfaea56251796b08c07b963de7403d84ebAnonymous Coward        byte[] actual = new byte[length];
85ceb1b0bfaea56251796b08c07b963de7403d84ebAnonymous Coward
86ceb1b0bfaea56251796b08c07b963de7403d84ebAnonymous Coward        System.arraycopy(TEST_STRING1, offset, expected, 0, length);
87ceb1b0bfaea56251796b08c07b963de7403d84ebAnonymous Coward
88ceb1b0bfaea56251796b08c07b963de7403d84ebAnonymous Coward        InputStream is = new LimitedLengthInputStream(mTestStream1, offset, length);
89ceb1b0bfaea56251796b08c07b963de7403d84ebAnonymous Coward        assertEquals(length, is.read(temp, 0, temp.length));
90ceb1b0bfaea56251796b08c07b963de7403d84ebAnonymous Coward
91ceb1b0bfaea56251796b08c07b963de7403d84ebAnonymous Coward        System.arraycopy(temp, 0, actual, 0, length);
92ceb1b0bfaea56251796b08c07b963de7403d84ebAnonymous Coward        assertTrue(Arrays.equals(expected, actual));
93ceb1b0bfaea56251796b08c07b963de7403d84ebAnonymous Coward
94ceb1b0bfaea56251796b08c07b963de7403d84ebAnonymous Coward        assertEquals(-1, is.read(temp, 0, temp.length));
95ceb1b0bfaea56251796b08c07b963de7403d84ebAnonymous Coward    }
96ceb1b0bfaea56251796b08c07b963de7403d84ebAnonymous Coward
97ceb1b0bfaea56251796b08c07b963de7403d84ebAnonymous Coward    @MediumTest
98ceb1b0bfaea56251796b08c07b963de7403d84ebAnonymous Coward    public void testReadBytesWithOffsetAndLength_ZeroOffset_PartialLength_Success()
99ceb1b0bfaea56251796b08c07b963de7403d84ebAnonymous Coward            throws Exception {
100ceb1b0bfaea56251796b08c07b963de7403d84ebAnonymous Coward        checkReadBytesWithOffsetAndLength_WithString1(0, 2);
101ceb1b0bfaea56251796b08c07b963de7403d84ebAnonymous Coward    }
102ceb1b0bfaea56251796b08c07b963de7403d84ebAnonymous Coward
103ceb1b0bfaea56251796b08c07b963de7403d84ebAnonymous Coward    @MediumTest
104ceb1b0bfaea56251796b08c07b963de7403d84ebAnonymous Coward    public void testReadBytesWithOffsetAndLength_NonZeroOffset_PartialLength_Success()
105ceb1b0bfaea56251796b08c07b963de7403d84ebAnonymous Coward            throws Exception {
106ceb1b0bfaea56251796b08c07b963de7403d84ebAnonymous Coward        checkReadBytesWithOffsetAndLength_WithString1(3, 2);
107ceb1b0bfaea56251796b08c07b963de7403d84ebAnonymous Coward    }
108ceb1b0bfaea56251796b08c07b963de7403d84ebAnonymous Coward
109ceb1b0bfaea56251796b08c07b963de7403d84ebAnonymous Coward    @MediumTest
110ceb1b0bfaea56251796b08c07b963de7403d84ebAnonymous Coward    public void testReadBytesWithOffsetAndLength_ZeroOffset_FullLength_Success() throws Exception {
111ceb1b0bfaea56251796b08c07b963de7403d84ebAnonymous Coward        checkReadBytesWithOffsetAndLength_WithString1(0, TEST_STRING1.length);
112ceb1b0bfaea56251796b08c07b963de7403d84ebAnonymous Coward    }
113ceb1b0bfaea56251796b08c07b963de7403d84ebAnonymous Coward
114ceb1b0bfaea56251796b08c07b963de7403d84ebAnonymous Coward    @MediumTest
115ceb1b0bfaea56251796b08c07b963de7403d84ebAnonymous Coward    public void testReadBytesWithOffsetAndLength_NonZeroOffset_FullLength_Success()
116ceb1b0bfaea56251796b08c07b963de7403d84ebAnonymous Coward            throws Exception {
117ceb1b0bfaea56251796b08c07b963de7403d84ebAnonymous Coward        checkReadBytesWithOffsetAndLength_WithString1(3, TEST_STRING1.length - 3);
118ceb1b0bfaea56251796b08c07b963de7403d84ebAnonymous Coward    }
119ceb1b0bfaea56251796b08c07b963de7403d84ebAnonymous Coward
120ceb1b0bfaea56251796b08c07b963de7403d84ebAnonymous Coward    @MediumTest
121ceb1b0bfaea56251796b08c07b963de7403d84ebAnonymous Coward    public void testReadBytesWithOffsetAndLength_ZeroOffset_PastEnd_Success() throws Exception {
122ceb1b0bfaea56251796b08c07b963de7403d84ebAnonymous Coward        byte[] temp = new byte[TEST_STRING1.length + 10];
123ceb1b0bfaea56251796b08c07b963de7403d84ebAnonymous Coward        InputStream is = new LimitedLengthInputStream(mTestStream1, 0, TEST_STRING1.length + 10);
124ceb1b0bfaea56251796b08c07b963de7403d84ebAnonymous Coward        assertEquals(TEST_STRING1.length, is.read(temp, 0, TEST_STRING1.length + 10));
125ceb1b0bfaea56251796b08c07b963de7403d84ebAnonymous Coward
126ceb1b0bfaea56251796b08c07b963de7403d84ebAnonymous Coward        byte[] actual = new byte[TEST_STRING1.length];
127ceb1b0bfaea56251796b08c07b963de7403d84ebAnonymous Coward        System.arraycopy(temp, 0, actual, 0, actual.length);
128ceb1b0bfaea56251796b08c07b963de7403d84ebAnonymous Coward        assertTrue(Arrays.equals(TEST_STRING1, actual));
129ceb1b0bfaea56251796b08c07b963de7403d84ebAnonymous Coward    }
130ceb1b0bfaea56251796b08c07b963de7403d84ebAnonymous Coward
131ceb1b0bfaea56251796b08c07b963de7403d84ebAnonymous Coward    private void checkReadBytes_WithString1(int offset, int length) throws Exception {
132ceb1b0bfaea56251796b08c07b963de7403d84ebAnonymous Coward        byte[] temp = new byte[TEST_STRING1.length];
133ceb1b0bfaea56251796b08c07b963de7403d84ebAnonymous Coward        byte[] expected = new byte[length];
134ceb1b0bfaea56251796b08c07b963de7403d84ebAnonymous Coward        byte[] actual = new byte[length];
135ceb1b0bfaea56251796b08c07b963de7403d84ebAnonymous Coward
136ceb1b0bfaea56251796b08c07b963de7403d84ebAnonymous Coward        System.arraycopy(TEST_STRING1, offset, expected, 0, length);
137ceb1b0bfaea56251796b08c07b963de7403d84ebAnonymous Coward
138ceb1b0bfaea56251796b08c07b963de7403d84ebAnonymous Coward        InputStream is = new LimitedLengthInputStream(mTestStream1, offset, length);
139ceb1b0bfaea56251796b08c07b963de7403d84ebAnonymous Coward        assertEquals(length, is.read(temp));
140ceb1b0bfaea56251796b08c07b963de7403d84ebAnonymous Coward
141ceb1b0bfaea56251796b08c07b963de7403d84ebAnonymous Coward        System.arraycopy(temp, 0, actual, 0, length);
142ceb1b0bfaea56251796b08c07b963de7403d84ebAnonymous Coward        assertTrue(Arrays.equals(expected, actual));
143ceb1b0bfaea56251796b08c07b963de7403d84ebAnonymous Coward
144ceb1b0bfaea56251796b08c07b963de7403d84ebAnonymous Coward        assertEquals(-1, is.read(temp));
145ceb1b0bfaea56251796b08c07b963de7403d84ebAnonymous Coward    }
146ceb1b0bfaea56251796b08c07b963de7403d84ebAnonymous Coward
147ceb1b0bfaea56251796b08c07b963de7403d84ebAnonymous Coward    @MediumTest
148ceb1b0bfaea56251796b08c07b963de7403d84ebAnonymous Coward    public void testReadBytes_ZeroOffset_PartialLength_Success() throws Exception {
149ceb1b0bfaea56251796b08c07b963de7403d84ebAnonymous Coward        checkReadBytesWithOffsetAndLength_WithString1(0, 2);
150ceb1b0bfaea56251796b08c07b963de7403d84ebAnonymous Coward    }
151ceb1b0bfaea56251796b08c07b963de7403d84ebAnonymous Coward
152ceb1b0bfaea56251796b08c07b963de7403d84ebAnonymous Coward    @MediumTest
153ceb1b0bfaea56251796b08c07b963de7403d84ebAnonymous Coward    public void testReadBytes_NonZeroOffset_PartialLength_Success() throws Exception {
154ceb1b0bfaea56251796b08c07b963de7403d84ebAnonymous Coward        checkReadBytesWithOffsetAndLength_WithString1(3, 2);
155ceb1b0bfaea56251796b08c07b963de7403d84ebAnonymous Coward    }
156ceb1b0bfaea56251796b08c07b963de7403d84ebAnonymous Coward
157ceb1b0bfaea56251796b08c07b963de7403d84ebAnonymous Coward    @MediumTest
158ceb1b0bfaea56251796b08c07b963de7403d84ebAnonymous Coward    public void testReadBytes_ZeroOffset_FullLength_Success() throws Exception {
159ceb1b0bfaea56251796b08c07b963de7403d84ebAnonymous Coward        checkReadBytesWithOffsetAndLength_WithString1(0, TEST_STRING1.length);
160ceb1b0bfaea56251796b08c07b963de7403d84ebAnonymous Coward    }
161ceb1b0bfaea56251796b08c07b963de7403d84ebAnonymous Coward
162ceb1b0bfaea56251796b08c07b963de7403d84ebAnonymous Coward    @MediumTest
163ceb1b0bfaea56251796b08c07b963de7403d84ebAnonymous Coward    public void testReadBytes_NonZeroOffset_FullLength_Success() throws Exception {
164ceb1b0bfaea56251796b08c07b963de7403d84ebAnonymous Coward        checkReadBytesWithOffsetAndLength_WithString1(3, TEST_STRING1.length - 3);
165ceb1b0bfaea56251796b08c07b963de7403d84ebAnonymous Coward    }
166ceb1b0bfaea56251796b08c07b963de7403d84ebAnonymous Coward
167ceb1b0bfaea56251796b08c07b963de7403d84ebAnonymous Coward    private void checkSingleByteRead_WithString1(int offset, int length) throws Exception {
168ceb1b0bfaea56251796b08c07b963de7403d84ebAnonymous Coward        InputStream is = new LimitedLengthInputStream(mTestStream1, offset, length);
169ceb1b0bfaea56251796b08c07b963de7403d84ebAnonymous Coward
170ceb1b0bfaea56251796b08c07b963de7403d84ebAnonymous Coward        for (int i = 0; i < length; i++) {
171ceb1b0bfaea56251796b08c07b963de7403d84ebAnonymous Coward            assertEquals(TEST_STRING1[offset + i], is.read());
172ceb1b0bfaea56251796b08c07b963de7403d84ebAnonymous Coward        }
173ceb1b0bfaea56251796b08c07b963de7403d84ebAnonymous Coward
174ceb1b0bfaea56251796b08c07b963de7403d84ebAnonymous Coward        assertEquals(-1, is.read());
175ceb1b0bfaea56251796b08c07b963de7403d84ebAnonymous Coward    }
176ceb1b0bfaea56251796b08c07b963de7403d84ebAnonymous Coward
177ceb1b0bfaea56251796b08c07b963de7403d84ebAnonymous Coward    @MediumTest
178ceb1b0bfaea56251796b08c07b963de7403d84ebAnonymous Coward    public void testSingleByteRead_ZeroOffset_PartialLength_Success() throws Exception {
179ceb1b0bfaea56251796b08c07b963de7403d84ebAnonymous Coward        checkSingleByteRead_WithString1(0, 2);
180ceb1b0bfaea56251796b08c07b963de7403d84ebAnonymous Coward    }
181ceb1b0bfaea56251796b08c07b963de7403d84ebAnonymous Coward
182ceb1b0bfaea56251796b08c07b963de7403d84ebAnonymous Coward    @MediumTest
183ceb1b0bfaea56251796b08c07b963de7403d84ebAnonymous Coward    public void testSingleByteRead_NonZeroOffset_PartialLength_Success() throws Exception {
184ceb1b0bfaea56251796b08c07b963de7403d84ebAnonymous Coward        checkSingleByteRead_WithString1(3, 2);
185ceb1b0bfaea56251796b08c07b963de7403d84ebAnonymous Coward    }
186ceb1b0bfaea56251796b08c07b963de7403d84ebAnonymous Coward
187ceb1b0bfaea56251796b08c07b963de7403d84ebAnonymous Coward    @MediumTest
188ceb1b0bfaea56251796b08c07b963de7403d84ebAnonymous Coward    public void testSingleByteRead_ZeroOffset_FullLength_Success() throws Exception {
189ceb1b0bfaea56251796b08c07b963de7403d84ebAnonymous Coward        checkSingleByteRead_WithString1(0, TEST_STRING1.length);
190ceb1b0bfaea56251796b08c07b963de7403d84ebAnonymous Coward    }
191ceb1b0bfaea56251796b08c07b963de7403d84ebAnonymous Coward
192ceb1b0bfaea56251796b08c07b963de7403d84ebAnonymous Coward    @MediumTest
193ceb1b0bfaea56251796b08c07b963de7403d84ebAnonymous Coward    public void testSingleByteRead_NonZeroOffset_FullLength_Success() throws Exception {
194ceb1b0bfaea56251796b08c07b963de7403d84ebAnonymous Coward        checkSingleByteRead_WithString1(3, TEST_STRING1.length - 3);
195ceb1b0bfaea56251796b08c07b963de7403d84ebAnonymous Coward    }
196ceb1b0bfaea56251796b08c07b963de7403d84ebAnonymous Coward}
197