### Question:
Kalium implements Encryptor { @Override public byte[] open( final byte[] cipherText, final Nonce nonce, final PublicKey publicKey, final PrivateKey privateKey) { LOGGER.info("Opening message using public key {}", publicKey); LOGGER.debug( "Opening message using nonce {}, public key {} and private key {}", nonce, publicKey, privateKey); final byte[] paddedInput = pad(cipherText, CRYPTO_BOX_CURVE25519XSALSA20POLY1305_BOXZEROBYTES); final byte[] paddedOutput = new byte[paddedInput.length]; final int sodiumResult = sodium.crypto_box_curve25519xsalsa20poly1305_open( paddedOutput, paddedInput, paddedInput.length, nonce.getNonceBytes(), publicKey.getKeyBytes(), privateKey.getKeyBytes()); if (sodiumResult == -1) { LOGGER.error( "Could not open sealed payload using public key {} and private key {}", publicKey, privateKey); throw new EncryptorException("Kalium could not open the payload using the provided keys directly"); } LOGGER.info("Opened sealed payload for public key {}", publicKey); LOGGER.debug( "Opened payload using nonce {}, public key {} and private key {}", nonce, publicKey, privateKey); return extract(paddedOutput, CRYPTO_BOX_CURVE25519XSALSA20POLY1305_ZEROBYTES); } Kalium(final NaCl.Sodium sodium); @Override SharedKey computeSharedKey(final PublicKey publicKey, final PrivateKey privateKey); @Override byte[] seal(
            final byte[] message, final Nonce nonce, final PublicKey publicKey, final PrivateKey privateKey); @Override byte[] open(
            final byte[] cipherText, final Nonce nonce, final PublicKey publicKey, final PrivateKey privateKey); @Override byte[] sealAfterPrecomputation(final byte[] message, final Nonce nonce, final SharedKey sharedKey); @Override byte[] openAfterPrecomputation(final byte[] encryptedPayload, final Nonce nonce, final SharedKey sharedKey); @Override Nonce randomNonce(); @Override KeyPair generateNewKeys(); @Override SharedKey createSingleKey();  }### Answer:
@Test public void openUsingKeysThrowsExceptionOnFailure() { doReturn(-1) .when(this.sodium) .crypto_box_curve25519xsalsa20poly1305_open( any(byte[].class), any(byte[].class), anyInt(), any(byte[].class), eq(publicKey.getKeyBytes()), eq(privateKey.getKeyBytes())); final Throwable kaclEx = catchThrowable(() -> this.kalium.open(message, nonce, publicKey, privateKey)); assertThat(kaclEx) .isInstanceOf(EncryptorException.class) .hasMessage("Kalium could not open the payload using the provided keys directly"); verify(this.sodium) .crypto_box_curve25519xsalsa20poly1305_open( any(byte[].class), any(byte[].class), anyInt(), any(byte[].class), any(byte[].class), any(byte[].class)); }
@Test public void openUsingKeysSodiumReturnsSucesss() { final byte[] data = new byte[100]; doReturn(1) .when(this.sodium) .crypto_box_curve25519xsalsa20poly1305_open( any(byte[].class), eq(data), anyInt(), any(byte[].class), eq(publicKey.getKeyBytes()), eq(privateKey.getKeyBytes())); final byte[] result = this.kalium.open(data, nonce, publicKey, privateKey); assertThat(result).isNotEmpty(); verify(this.sodium) .crypto_box_curve25519xsalsa20poly1305_open( any(byte[].class), any(byte[].class), anyInt(), any(byte[].class), any(byte[].class), any(byte[].class)); }