use of com.google.crypto.tink.StreamingTestUtil.PseudorandomReadableByteChannel in project tink by google.
the class RewindableReadableByteChannelTest method testDisableRewind.
@Test
public void testDisableRewind() throws Exception {
int blockSize = PseudorandomReadableByteChannel.BLOCK_SIZE;
int extraSize = 123;
int blockCount = 5;
int inputSize = blockSize * blockCount + extraSize;
ReadableByteChannel baseChannel = new PseudorandomReadableByteChannel(inputSize);
assertTrue(baseChannel.isOpen());
RewindableReadableByteChannel rewindableChannel = new RewindableReadableByteChannel(baseChannel);
assertTrue(rewindableChannel.isOpen());
// Read two blocks.
ByteBuffer twoBlocksBuffer = ByteBuffer.allocate(2 * blockSize);
assertEquals(2 * blockSize, rewindableChannel.read(twoBlocksBuffer));
// Verify that the read bytes are not all the same.
assertFalse(Arrays.equals(Arrays.copyOfRange(twoBlocksBuffer.array(), 0, 42), Arrays.copyOfRange(twoBlocksBuffer.array(), 42, 2 * 42)));
// Rewind and read 1 block + extraSize;
rewindableChannel.rewind();
ByteBuffer blockAndExtraBuffer = ByteBuffer.allocate(blockSize + extraSize);
assertEquals(blockSize + extraSize, rewindableChannel.read(blockAndExtraBuffer));
assertArrayEquals(blockAndExtraBuffer.array(), Arrays.copyOfRange(twoBlocksBuffer.array(), 0, blockSize + extraSize));
// Disable the rewinding feature, and continue reading.
rewindableChannel.disableRewinding();
try {
rewindableChannel.rewind();
fail("Should have thrown exception, as rewinding has been dropped");
} catch (IOException expected) {
assertExceptionContains(expected, "Cannot rewind");
}
ByteBuffer oneBlockBuffer = ByteBuffer.allocate(blockSize);
assertEquals(blockSize, rewindableChannel.read(oneBlockBuffer));
assertArrayEquals(oneBlockBuffer.array(), Arrays.copyOfRange(twoBlocksBuffer.array(), extraSize, blockSize + extraSize));
int remainingSize = (blockCount - 2) * blockSize;
ByteBuffer remainingBuffer = ByteBuffer.allocate(remainingSize);
assertEquals(remainingSize, rewindableChannel.read(remainingBuffer));
assertArrayEquals(blockAndExtraBuffer.array(), Arrays.copyOfRange(remainingBuffer.array(), remainingSize - blockSize - extraSize, remainingSize));
// Check EOF.
ByteBuffer buffer = ByteBuffer.allocate(42);
assertEquals(-1, rewindableChannel.read(buffer));
// Close the channel.
rewindableChannel.close();
assertFalse(rewindableChannel.isOpen());
assertFalse(baseChannel.isOpen());
}
use of com.google.crypto.tink.StreamingTestUtil.PseudorandomReadableByteChannel in project tink by google.
the class RewindableReadableByteChannelTest method testSingleReadsOfVariousLengths.
@Test
public void testSingleReadsOfVariousLengths() throws Exception {
int inputSize = 1234;
ReadableByteChannel baseChannel = new PseudorandomReadableByteChannel(inputSize);
assertTrue(baseChannel.isOpen());
RewindableReadableByteChannel rewindableChannel = new RewindableReadableByteChannel(baseChannel);
assertTrue(rewindableChannel.isOpen());
// Read some initial bytes.
int buffer1Size = 42;
ByteBuffer buffer1 = ByteBuffer.allocate(buffer1Size);
assertEquals(buffer1Size, rewindableChannel.read(buffer1));
// Rewind, and read a shorter sequence of initial bytes.
rewindableChannel.rewind();
int buffer2Size = 40;
ByteBuffer buffer2 = ByteBuffer.allocate(buffer2Size);
assertEquals(buffer2Size, rewindableChannel.read(buffer2));
assertArrayEquals(buffer2.array(), Arrays.copyOfRange(buffer1.array(), 0, buffer2Size));
// Rewind, and read a longer sequence of initial bytes.
rewindableChannel.rewind();
int buffer3Size = 60;
ByteBuffer buffer3 = ByteBuffer.allocate(buffer3Size);
assertEquals(buffer3Size, rewindableChannel.read(buffer3));
assertArrayEquals(buffer1.array(), Arrays.copyOfRange(buffer3.array(), 0, buffer1Size));
// Read all the remaining bytes.
int buffer4Size = inputSize - buffer3Size;
ByteBuffer buffer4 = ByteBuffer.allocate(buffer4Size);
assertEquals(buffer4Size, rewindableChannel.read(buffer4));
// Check that no more bytes are left.
ByteBuffer buffer5 = ByteBuffer.allocate(inputSize);
assertEquals(-1, rewindableChannel.read(buffer5));
// Rewind, and read the entire file again.
rewindableChannel.rewind();
assertEquals(inputSize, rewindableChannel.read(buffer5));
assertArrayEquals(buffer4.array(), Arrays.copyOfRange(buffer5.array(), buffer3Size, inputSize));
// Close the channel.
rewindableChannel.close();
assertFalse(rewindableChannel.isOpen());
assertFalse(baseChannel.isOpen());
// Try rewinding or reading after closing.
try {
rewindableChannel.rewind();
fail("Should have thrown exception, as cannot rewind after closing.");
} catch (IOException expected) {
assertExceptionContains(expected, "Cannot rewind");
}
ByteBuffer buffer6 = ByteBuffer.allocate(42);
try {
int unused = rewindableChannel.read(buffer6);
fail("Should have thrown exception, as cannot read after closing.");
} catch (ClosedChannelException expected) {
}
}
use of com.google.crypto.tink.StreamingTestUtil.PseudorandomReadableByteChannel in project tink by google.
the class RewindableReadableByteChannelTest method testSubsequentReads.
@Test
public void testSubsequentReads() throws Exception {
int inputSize = 1234;
ReadableByteChannel baseChannel = new PseudorandomReadableByteChannel(inputSize);
assertTrue(baseChannel.isOpen());
RewindableReadableByteChannel rewindableChannel = new RewindableReadableByteChannel(baseChannel);
assertTrue(rewindableChannel.isOpen());
// Read some initial bytes.
int buffer1Size = 105;
ByteBuffer buffer1 = ByteBuffer.allocate(buffer1Size);
int limit1 = 42;
buffer1.limit(limit1);
assertEquals(limit1, rewindableChannel.read(buffer1));
// Continue reading until the buffer is full.
buffer1.limit(buffer1.capacity());
assertEquals(buffer1Size - limit1, rewindableChannel.read(buffer1));
// Rewind, and read a longer sequence of initial bytes.
rewindableChannel.rewind();
int buffer2Size = 160;
ByteBuffer buffer2 = ByteBuffer.allocate(buffer2Size);
assertEquals(buffer2Size, rewindableChannel.read(buffer2));
assertArrayEquals(buffer1.array(), Arrays.copyOfRange(buffer2.array(), 0, buffer1Size));
// Rewind, and read a longer sequence in multiple steps.
rewindableChannel.rewind();
int buffer3Size = 150;
ByteBuffer buffer3 = ByteBuffer.allocate(buffer3Size);
int stepCount = 5;
int blockSize = buffer3Size / stepCount;
for (int i = 1; i <= stepCount; i++) {
buffer3.limit(i * blockSize);
assertEquals(blockSize, rewindableChannel.read(buffer3));
}
assertArrayEquals(buffer3.array(), Arrays.copyOfRange(buffer2.array(), 0, buffer3Size));
// Read the remaining bytes and check the size;
ByteBuffer buffer4 = ByteBuffer.allocate(inputSize);
assertEquals(inputSize - buffer3Size, rewindableChannel.read(buffer4));
assertEquals(-1, rewindableChannel.read(buffer4));
// Close the channel.
rewindableChannel.close();
assertFalse(rewindableChannel.isOpen());
assertFalse(baseChannel.isOpen());
}
use of com.google.crypto.tink.StreamingTestUtil.PseudorandomReadableByteChannel in project tink by google.
the class RewindableReadableByteChannelTest method testExceptions.
@Test
public void testExceptions() throws Exception {
int inputSize = 1234;
ReadableByteChannel baseChannel = new PseudorandomReadableByteChannel(inputSize);
baseChannel.close();
assertFalse(baseChannel.isOpen());
RewindableReadableByteChannel rewindableChannel = new RewindableReadableByteChannel(baseChannel);
assertFalse(rewindableChannel.isOpen());
ByteBuffer buffer = ByteBuffer.allocate(42);
try {
int unused = rewindableChannel.read(buffer);
fail("Should have thrown exception, as cannot read after closing.");
} catch (ClosedChannelException expected) {
}
}
Aggregations