@Override public void free() { close(); underlyingBuffer.free(); }
@Test public void testClose() throws IOException, GeneralSecurityException { byte[] bytes = new byte[100]; ByteArrayRandomAccessBuffer barat = new ByteArrayRandomAccessBuffer(bytes); EncryptedRandomAccessBuffer erat = new EncryptedRandomAccessBuffer(types[0], barat, secret, true); erat.close(); erat.close(); }
@Test public void testWrongMagic() throws IOException, GeneralSecurityException{ byte[] bytes = new byte[100]; ByteArrayRandomAccessBuffer barat = new ByteArrayRandomAccessBuffer(bytes); EncryptedRandomAccessBuffer erat = new EncryptedRandomAccessBuffer(types[0], barat, secret, true); erat.close(); ByteArrayRandomAccessBuffer barat2 = new ByteArrayRandomAccessBuffer(bytes); byte[] magic = ByteBuffer.allocate(8).putLong(falseMagic).array(); barat2.pwrite(types[0].headerLen-8, magic, 0, 8); thrown.expect(IOException.class); thrown.expectMessage("This is not an EncryptedRandomAccessBuffer!"); EncryptedRandomAccessBuffer erat2 = new EncryptedRandomAccessBuffer(types[0], barat2, secret, false); }
restored.pread(0, tmp, 0, buf.length); assertArrayEquals(buf, tmp); restored.close(); restored.free();
@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); } }
eraf.storeTo(dos); dos.close(); eraf.close(); DataInputStream dis = new DataInputStream(new ByteArrayInputStream(baos.toByteArray())); ClientContext context = new ClientContext(0, null, null, null, null, null, null, null, null, restored.pread(0, tmp, 0, buf.length); assertArrayEquals(buf, tmp); restored.close(); restored.free();
@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 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 testWrongERATType() throws IOException, GeneralSecurityException { byte[] bytes = new byte[100]; ByteArrayRandomAccessBuffer barat = new ByteArrayRandomAccessBuffer(bytes); EncryptedRandomAccessBuffer erat = new EncryptedRandomAccessBuffer(types[0], barat, secret, true); erat.close(); ByteArrayRandomAccessBuffer barat2 = new ByteArrayRandomAccessBuffer(bytes); thrown.expect(IOException.class); thrown.expectMessage("This is not an EncryptedRandomAccessBuffer"); // Different header lengths. EncryptedRandomAccessBuffer erat2 = new EncryptedRandomAccessBuffer(types[1], barat2, secret, false); }
@Test public void testWrongMasterSecret() throws IOException, GeneralSecurityException{ byte[] bytes = new byte[100]; ByteArrayRandomAccessBuffer barat = new ByteArrayRandomAccessBuffer(bytes); EncryptedRandomAccessBuffer erat = new EncryptedRandomAccessBuffer(types[0], barat, secret, true); erat.close(); ByteArrayRandomAccessBuffer barat2 = new ByteArrayRandomAccessBuffer(bytes); thrown.expect(GeneralSecurityException.class); thrown.expectMessage("MAC is incorrect"); EncryptedRandomAccessBuffer erat2 = new EncryptedRandomAccessBuffer(types[0], barat2, new MasterSecret(), false); }