FileRandomAccessBuffer rafw = new FileRandomAccessBuffer(tempFile, buf.length+types[0].headerLen, false); EncryptedRandomAccessBuffer eraf = new EncryptedRandomAccessBuffer(types[0], rafw, secret, true); eraf.pwrite(0, buf, 0, buf.length); byte[] tmp = new byte[buf.length]; eraf.pread(0, tmp, 0, buf.length);
@Test public void testPwriteFileOffsetTooSmall() 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.pwrite(-1, result, 0, 20); }
FileRandomAccessBuffer rafw = new FileRandomAccessBuffer(tempFile, buf.length+types[0].headerLen, false); EncryptedRandomAccessBuffer eraf = new EncryptedRandomAccessBuffer(types[0], rafw, secret, true); eraf.pwrite(0, buf, 0, buf.length); byte[] tmp = new byte[buf.length]; eraf.pread(0, tmp, 0, buf.length);
@Test public void testClosePwrite() 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 written to."); erat.pwrite(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 testPwriteFileOffsetTooBig() 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 write after end: trying to write from "+offset+" to "+ (offset+len)+" on block length "+erat.size()); erat.pwrite(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); } }