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