1 /* 2 * Copyright (C) 2017 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 17 package com.android.apksig.internal.util; 18 19 import static java.nio.charset.StandardCharsets.US_ASCII; 20 import static org.junit.Assert.assertEquals; 21 import static org.junit.Assert.assertTrue; 22 23 import com.android.apksig.util.DataSources; 24 import com.android.apksig.util.DataSinks; 25 import com.android.apksig.util.ReadableDataSink; 26 27 import org.junit.Before; 28 import org.junit.Test; 29 import org.junit.runner.RunWith; 30 import org.junit.runners.JUnit4; 31 32 import java.nio.ByteBuffer; 33 34 /** Unit tests for {@link ChainedDataSource}. */ 35 @RunWith(JUnit4.class) 36 public final class ChainedDataSourceTest { 37 38 private ChainedDataSource mChain; 39 setUp()40 @Before public void setUp() { 41 mChain = new ChainedDataSource( 42 DataSources.asDataSource(ByteBuffer.wrap("12".getBytes(US_ASCII))), 43 DataSources.asDataSource(ByteBuffer.wrap("34567".getBytes(US_ASCII))), 44 DataSources.asDataSource(ByteBuffer.wrap("".getBytes(US_ASCII))), 45 DataSources.asDataSource(ByteBuffer.wrap("890".getBytes(US_ASCII))), 46 DataSources.asDataSource(ByteBuffer.wrap("".getBytes(US_ASCII)))); 47 assertEquals(10, mChain.size()); 48 } 49 feedAllPossibleRanges()50 @Test public void feedAllPossibleRanges() throws Exception { 51 for (int begin = 0; begin < mChain.size(); begin++) { 52 for (int end = begin + 1; end < mChain.size(); end++) { 53 int size = end - begin; 54 ReadableDataSink sink = DataSinks.newInMemoryDataSink(size); 55 mChain.feed(begin, size, sink); 56 assertByteBufferEquals( 57 ByteBuffer.wrap("1234567890".substring(begin, end).getBytes(US_ASCII)), 58 sink.getByteBuffer(0, size)); 59 } 60 } 61 } 62 63 @Test(expected=IndexOutOfBoundsException.class) feedMoreThanAvailable()64 public void feedMoreThanAvailable() throws Exception { 65 mChain.feed(0, mChain.size() + 1, DataSinks.newInMemoryDataSink(3)); 66 } 67 getByteBufferFromAllPossibleRanges()68 @Test public void getByteBufferFromAllPossibleRanges() throws Exception { 69 for (int begin = 0; begin < mChain.size(); begin++) { 70 for (int end = begin + 1; end < mChain.size(); end++) { 71 int size = end - begin; 72 ByteBuffer buffer = mChain.getByteBuffer(begin, size); 73 assertByteBufferEquals( 74 ByteBuffer.wrap("1234567890".substring(begin, end).getBytes(US_ASCII)), 75 buffer); 76 } 77 } 78 } 79 80 @Test(expected=IndexOutOfBoundsException.class) getByteBufferForMoreThanAvailable()81 public void getByteBufferForMoreThanAvailable() throws Exception { 82 mChain.getByteBuffer(0, (int) mChain.size() + 1); 83 } 84 85 @Test copyTo()86 public void copyTo() throws Exception { 87 for (int begin = 0; begin < mChain.size(); begin++) { 88 for (int end = begin + 1; end < mChain.size(); end++) { 89 int size = end - begin; 90 ByteBuffer buffer = ByteBuffer.allocate(size); 91 mChain.copyTo(begin, size, buffer); 92 assertEquals(size, buffer.position()); 93 94 buffer.rewind(); 95 assertByteBufferEquals( 96 ByteBuffer.wrap("1234567890".substring(begin, end).getBytes(US_ASCII)), 97 buffer); 98 } 99 } 100 } 101 102 @Test slice()103 public void slice() throws Exception { 104 for (int begin = 0; begin < mChain.size(); begin++) { 105 for (int end = begin + 1; end < mChain.size(); end++) { 106 int size = end - begin; 107 ByteBuffer buffer = mChain.slice(begin, size).getByteBuffer(0, size); 108 109 assertByteBufferEquals( 110 ByteBuffer.wrap("1234567890".substring(begin, end).getBytes(US_ASCII)), 111 buffer); 112 } 113 } 114 } 115 assertByteBufferEquals(ByteBuffer buffer, ByteBuffer buffer2)116 private void assertByteBufferEquals(ByteBuffer buffer, ByteBuffer buffer2) { 117 assertTrue(buffer.toString() + " vs " + buffer2.toString() + ", byte array: " + 118 HexEncoding.encode(buffer.array()) + " vs " + HexEncoding.encode(buffer2.array()), 119 buffer.compareTo(buffer2) == 0); 120 } 121 } 122