LunaKeyProxy implements KeyProxy { @Override public boolean matchesCanary(final EncryptionKeyCanary canary) { return matchesCanary(key, canary); } LunaKeyProxy(final Key key, final InternalEncryptionService encryptionService); @Override Key getKey(); void setKey(final Key key); @Override boolean matchesCanary(final EncryptionKeyCanary canary); @Override List<Byte> getSalt();  }
@Test public void isMatchingCanary_whenCanaryMatches_returnsTrue() throws Exception { subject = new LunaKeyProxy(encryptionKey, new PasswordEncryptionService(new TestPasswordKeyProxyFactory())); assertThat(subject.matchesCanary(canary), equalTo(true)); }
@Test public void isMatchingCanary_usingOldCanaryValue_returnsTrue() throws Exception { subject = new LunaKeyProxy(encryptionKey, new PasswordEncryptionService(new TestPasswordKeyProxyFactory())); assertThat(subject.matchesCanary(deprecatedCanary), equalTo(true)); }
@Test public void isMatchingCanary_whenDecryptThrowsAEADBadTagException_returnsFalse() throws Exception { subject = new LunaKeyProxy(encryptionKey, new PasswordEncryptionService(new TestPasswordKeyProxyFactory()) { @Override public String decrypt(final Key key, final byte[] encryptedValue, final byte[] nonce) throws Exception { throw new AEADBadTagException(); } }); assertThat(subject.matchesCanary(mock(EncryptionKeyCanary.class)), equalTo(false)); }
@Test public void isMatchingCanary_whenDecryptThrowsExceptionWithCauseIndicatingTheKeyIsIncorrect_returnsFalse() throws Exception { subject = new LunaKeyProxy(encryptionKey, new PasswordEncryptionService(new TestPasswordKeyProxyFactory()) { @Override public String decrypt(final Key key, final byte[] encryptedValue, final byte[] nonce) { throw new RuntimeException(new RuntimeException("returns 0x40 (CKR_ENCRYPTED_DATA_INVALID)")); } }); assertThat(subject.matchesCanary(mock(EncryptionKeyCanary.class)), equalTo(false)); }
@Test public void isMatchingCanary_whenDecryptThrowsExceptionWithOtherCause_throwsRuntimeException() throws Exception { subject = new LunaKeyProxy(encryptionKey, new PasswordEncryptionService(new TestPasswordKeyProxyFactory()) { @Override public String decrypt(final Key key, final byte[] encryptedValue, final byte[] nonce) { throw new RuntimeException(new RuntimeException(("some message that isn't 0x40..."))); } }); try { subject.matchesCanary(mock(EncryptionKeyCanary.class)); fail("Expected to get RuntimeException"); } catch (RuntimeException e) { assertThat(e.getCause().getCause().getMessage(), equalTo("some message that isn't 0x40...")); } }
@Test public void isMatchingCanary_WhenDecryptThrowsExceptionWithNoCause_throwsRuntimeException() throws Exception { subject = new LunaKeyProxy(encryptionKey, new PasswordEncryptionService(new TestPasswordKeyProxyFactory()) { @Override public String decrypt(final Key key, final byte[] encryptedValue, final byte[] nonce) { throw new RuntimeException("test message"); } }); try { subject.matchesCanary(mock(EncryptionKeyCanary.class)); fail("Expected to get RuntimeException"); } catch (RuntimeException e) { assertThat(e.getCause().getMessage(), equalTo("test message")); } }