eraf.pwrite(0, buf, 0, buf.length); byte[] tmp = new byte[buf.length]; eraf.pread(0, tmp, 0, buf.length); assertArrayEquals(buf, tmp); ByteArrayOutputStream baos = new ByteArrayOutputStream(); assertEquals(eraf, restored); tmp = new byte[buf.length]; restored.pread(0, tmp, 0, buf.length); assertArrayEquals(buf, tmp); restored.close();
@Test public void testPreadFileOffsetTooSmall() throws IOException, GeneralSecurityException { byte[] bytes = new byte[100]; ByteArrayRandomAccessBuffer barat = new ByteArrayRandomAccessBuffer(bytes); EncryptedRandomAccessBuffer erat = new EncryptedRandomAccessBuffer(types[0], barat, secret, true); byte[] result = new byte[20]; thrown.expect(IllegalArgumentException.class); thrown.expectMessage("Cannot read before zero"); erat.pread(-1, result, 0, 20); }
@Test public void testClosePread() throws IOException, GeneralSecurityException { byte[] bytes = new byte[100]; ByteArrayRandomAccessBuffer barat = new ByteArrayRandomAccessBuffer(bytes); EncryptedRandomAccessBuffer erat = new EncryptedRandomAccessBuffer(types[0], barat, secret, true); erat.close(); byte[] result = new byte[20]; thrown.expect(IOException.class); thrown.expectMessage("This RandomAccessBuffer has already been closed. It can no longer" + " be read from."); erat.pread(0, result, 0, 20); }
@Test public void testSuccesfulRoundTrip() throws IOException, GeneralSecurityException{ for(EncryptedRandomAccessBufferType type: types){ byte[] bytes = new byte[100]; ByteArrayRandomAccessBuffer barat = new ByteArrayRandomAccessBuffer(bytes); EncryptedRandomAccessBuffer erat = new EncryptedRandomAccessBuffer(type, barat, secret, true); erat.pwrite(0, message, 0, message.length); byte[] result = new byte[message.length]; erat.pread(0, result, 0, result.length); erat.close(); assertArrayEquals(message, result); } }
@Test public void testPreadFileOffsetTooBig() throws IOException, GeneralSecurityException { byte[] bytes = new byte[100]; ByteArrayRandomAccessBuffer barat = new ByteArrayRandomAccessBuffer(bytes); EncryptedRandomAccessBuffer erat = new EncryptedRandomAccessBuffer(types[0], barat, secret, true); int len = 20; byte[] result = new byte[len]; int offset = 100; thrown.expect(IOException.class); thrown.expectMessage("Cannot read after end: trying to read from "+offset+" to "+ (offset+len)+" on block length "+erat.size()); erat.pread(offset, result, 0, len); }
@Test public void testSuccesfulRoundTripReadHeader() throws IOException, GeneralSecurityException{ for(EncryptedRandomAccessBufferType type: types){ byte[] bytes = new byte[100]; ByteArrayRandomAccessBuffer barat = new ByteArrayRandomAccessBuffer(bytes); EncryptedRandomAccessBuffer erat = new EncryptedRandomAccessBuffer(type, barat, secret, true); erat.pwrite(0, message, 0, message.length); erat.close(); ByteArrayRandomAccessBuffer barat2 = new ByteArrayRandomAccessBuffer(bytes); EncryptedRandomAccessBuffer erat2 = new EncryptedRandomAccessBuffer(type, barat2, secret, false); byte[] result = new byte[message.length]; erat2.pread(0, result, 0, result.length); erat2.close(); assertArrayEquals(message, result); } }