/**
   * Test that ensures an update on the app from non-encrypted to encrypted works.
   *
   * <p>
   *
   * @throws Exception
   */
  public void testUpdateEncryptedApp() throws Exception {
    Log.i(LOG_TAG, "Test the update of an app which becomes encrypted works as intended.");

    try {
      // cleanup test app just in case it was accidently installed
      getDevice().uninstallPackage(SIMPLE_PKG);
      getDevice().executeShellCommand("stop");

      getDevice().executeShellCommand("start");

      mPMHostUtils.waitForPackageManager();

      mPMHostUtils.installFile(getTestAppFilePath(SIMPLE_APK), false);
      Assert.assertFalse(mPMHostUtils.appExistsAsEncrypted(SIMPLE_PKG, false));

      mPMHostUtils.installEncryptedAppAndVerifyExists(
          getTestAppFilePath(SIMPLE_ENCRYPTED_APK),
          SIMPLE_PKG,
          true,
          ENCRYPTION_ALGO,
          ENCRYPTION_IV,
          ENCRYPTION_KEY,
          MAC_ALGO,
          MAC_KEY,
          MAC_TAG);
      assert (mPMHostUtils.appExistsAsEncrypted(SIMPLE_PKG, false));
    }
    // cleanup test app
    finally {
      mPMHostUtils.uninstallApp(SIMPLE_PKG);
    }
  }
 /**
  * Installing encrypted apk on SDCard works.
  *
  * @throws Exception
  */
 public void testEncryptedAppOnSdcard() throws Exception {
   if (mPMHostUtils.getIsExternalStorageEmulated()) {
     return;
   }
   PackageManagerHostTestUtils.InstallLocPreference preference =
       PackageManagerHostTestUtils.InstallLocPreference.EXTERNAL;
   PackageManagerHostTestUtils.InstallLocPreference savedPref =
       PackageManagerHostTestUtils.InstallLocPreference.AUTO;
   try {
     savedPref = mPMHostUtils.getDevicePreferredInstallLocation();
     mPMHostUtils.setDevicePreferredInstallLocation(preference);
     mPMHostUtils.installEncryptedAppAndVerifyExists(
         getTestAppFilePath(SIMPLE_ENCRYPTED_APK),
         SIMPLE_PKG,
         true,
         ENCRYPTION_ALGO,
         ENCRYPTION_IV,
         ENCRYPTION_KEY,
         MAC_ALGO,
         MAC_KEY,
         MAC_TAG);
     assert (mPMHostUtils.appExistsAsEncrypted(SIMPLE_PKG, true));
   }
   // cleanup test app
   finally {
     mPMHostUtils.setDevicePreferredInstallLocation(savedPref);
     mPMHostUtils.uninstallApp(SIMPLE_PKG);
   }
 }
  /**
   * Test that ensures we are able to install an encrypted apk, and that the resulting files are in
   * the correct location.
   *
   * <p>Assumes adb is running as root in device under test.
   *
   * @throws Exception
   */
  public void testInstallEncryptedApp() throws Exception {
    Log.i(LOG_TAG, "Test an encrypted app works as intended.");

    try {
      mPMHostUtils.installEncryptedAppAndVerifyExists(
          getTestAppFilePath(SIMPLE_ENCRYPTED_APK),
          SIMPLE_PKG,
          true,
          ENCRYPTION_ALGO,
          ENCRYPTION_IV,
          ENCRYPTION_KEY,
          MAC_ALGO,
          MAC_KEY,
          MAC_TAG);
      assert (mPMHostUtils.appExistsAsEncrypted(SIMPLE_PKG, false));
    }
    // cleanup test app
    finally {
      mPMHostUtils.uninstallApp(SIMPLE_PKG);
    }
  }