// Path: lib/src/main/java/com/google/capillary/AuthModeUnavailableException.java
// public final class AuthModeUnavailableException extends CapillaryException {
// 
//   /**
//    * Creates a new {@link AuthModeUnavailableException} instance.
//    *
//    * @param msg the exception message.
//    */
//   public AuthModeUnavailableException(String msg) {
//     super(msg);
//   }
// }
// 
// Path: lib/src/main/java/com/google/capillary/NoSuchKeyException.java
// public final class NoSuchKeyException extends CapillaryException {
// 
//   /**
//    * Creates a new {@link NoSuchKeyException} object.
//    *
//    * @param msg the exception message.
//    */
//   public NoSuchKeyException(String msg) {
//     super(msg);
//   }
// }


import static org.mockito.Matchers.any;
import static org.mockito.Matchers.anyBoolean;
import static org.mockito.Matchers.anyInt;
import static org.mockito.Matchers.anyString;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyNoMoreInteractions;
import static org.mockito.Mockito.verifyZeroInteractions;
import static org.mockito.Mockito.when;
import android.content.Context;
import android.os.Build.VERSION_CODES;
import android.security.keystore.UserNotAuthenticatedException;
import com.google.capillary.AuthModeUnavailableException;
import com.google.capillary.NoSuchKeyException;
import com.google.capillary.internal.CapillaryCiphertext;
import com.google.crypto.tink.HybridDecrypt;
import com.google.protobuf.ByteString;
import java.security.GeneralSecurityException;
import java.util.LinkedList;
import java.util.List;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;

    // New key pair not generated.
    when(keyManager.generateKeyPair(anyInt(), anyBoolean())).thenReturn(false);
    decrypterManager.decrypt(ciphertextBytes, handler, extra);
    verify(handler).error(CapillaryHandlerErrorCode.STALE_CIPHERTEXT, ciphertextBytes, extra);

    // Key pair generation failed.
    when(keyManager.generateKeyPair(anyInt(), anyBoolean()))
        .thenThrow(new GeneralSecurityException("unknown exception"));
    decrypterManager.decrypt(ciphertextBytes, handler, extra);
    verify(handler).error(CapillaryHandlerErrorCode.UNKNOWN_ERROR, ciphertextBytes, extra);
    verifyNoMoreInteractions(handler);
  }

  @Test
  public void testAuthModeUnavailable()
      throws GeneralSecurityException, NoSuchKeyException, AuthModeUnavailableException {
    when(keyManager.getDecrypter(anyString(), anyInt(), anyBoolean()))
        .thenThrow(new AuthModeUnavailableException("no auth mode in device"));

    ciphertextBuilder.setIsAuthKey(true);
    byte[] ciphertextBytes = ciphertextBuilder.build().toByteArray();
    decrypterManager.decrypt(ciphertextBytes, handler, extra);
    verify(handler).error(
        CapillaryHandlerErrorCode.AUTH_CIPHER_IN_NO_AUTH_DEVICE, ciphertextBytes, extra);
    verifyNoMoreInteractions(handler);
  }

  @Test
  public void testUnknownError()
// Path: lib/src/main/java/com/google/capillary/AuthModeUnavailableException.java
// public final class AuthModeUnavailableException extends CapillaryException {
// 
//   /**
//    * Creates a new {@link AuthModeUnavailableException} instance.
//    *
//    * @param msg the exception message.
//    */
//   public AuthModeUnavailableException(String msg) {
//     super(msg);
//   }
// }
// 
// Path: lib/src/main/java/com/google/capillary/NoSuchKeyException.java
// public final class NoSuchKeyException extends CapillaryException {
// 
//   /**
//    * Creates a new {@link NoSuchKeyException} object.
//    *
//    * @param msg the exception message.
//    */
//   public NoSuchKeyException(String msg) {
//     super(msg);
//   }
// }


// Path: lib-android/src/test/java/com/google/capillary/android/DecrypterManagerTest.java
import static org.mockito.Matchers.any;
import static org.mockito.Matchers.anyBoolean;
import static org.mockito.Matchers.anyInt;
import static org.mockito.Matchers.anyString;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyNoMoreInteractions;
import static org.mockito.Mockito.verifyZeroInteractions;
import static org.mockito.Mockito.when;
import android.content.Context;
import android.os.Build.VERSION_CODES;
import android.security.keystore.UserNotAuthenticatedException;
import com.google.capillary.AuthModeUnavailableException;
import com.google.capillary.NoSuchKeyException;
import com.google.capillary.internal.CapillaryCiphertext;
import com.google.crypto.tink.HybridDecrypt;
import com.google.protobuf.ByteString;
import java.security.GeneralSecurityException;
import java.util.LinkedList;
import java.util.List;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;


    // New key pair not generated.
    when(keyManager.generateKeyPair(anyInt(), anyBoolean())).thenReturn(false);
    decrypterManager.decrypt(ciphertextBytes, handler, extra);
    verify(handler).error(CapillaryHandlerErrorCode.STALE_CIPHERTEXT, ciphertextBytes, extra);

    // Key pair generation failed.
    when(keyManager.generateKeyPair(anyInt(), anyBoolean()))
        .thenThrow(new GeneralSecurityException("unknown exception"));
    decrypterManager.decrypt(ciphertextBytes, handler, extra);
    verify(handler).error(CapillaryHandlerErrorCode.UNKNOWN_ERROR, ciphertextBytes, extra);
    verifyNoMoreInteractions(handler);
  }

  @Test
  public void testAuthModeUnavailable()
      throws GeneralSecurityException, NoSuchKeyException, AuthModeUnavailableException {
    when(keyManager.getDecrypter(anyString(), anyInt(), anyBoolean()))
        .thenThrow(new AuthModeUnavailableException("no auth mode in device"));

    ciphertextBuilder.setIsAuthKey(true);
    byte[] ciphertextBytes = ciphertextBuilder.build().toByteArray();
    decrypterManager.decrypt(ciphertextBytes, handler, extra);
    verify(handler).error(
        CapillaryHandlerErrorCode.AUTH_CIPHER_IN_NO_AUTH_DEVICE, ciphertextBytes, extra);
    verifyNoMoreInteractions(handler);
  }

  @Test
  public void testUnknownError()
      throws NoSuchKeyException, GeneralSecurityException, AuthModeUnavailableException {