import java.util.Optional;

/** Tests for {@link TertiaryKeysTable}. */
@RunWith(RobolectricTestRunner.class)
@Presubmit
public class TertiaryKeysTableTest {
    private static final int KEY_SIZE_BYTES = 32;
    private static final String SECONDARY_ALIAS = "phoebe";
    private static final String PACKAGE_NAME = "generic.package.name";

    private TertiaryKeysTable mTertiaryKeysTable;

    /** Creates an empty {@link BackupEncryptionDb}. */
    @Before
    public void setUp() {
        mTertiaryKeysTable =
                BackupEncryptionDb.newInstance(RuntimeEnvironment.application)
                       .getTertiaryKeysTable();
    }

    /** Tests that new {@link TertiaryKey}s get successfully added to the database. */
    @Test
    public void addKey_onEmptyDatabase_putsKeyInDb() throws Exception {
        byte[] key = generateRandomKey();
        TertiaryKey keyToInsert = new TertiaryKey(SECONDARY_ALIAS, PACKAGE_NAME, key);

        long result = mTertiaryKeysTable.addKey(keyToInsert);

        assertThat(result).isNotEqualTo(-1);
        Optional<TertiaryKey> maybeKeyInDb =
                mTertiaryKeysTable.getKey(SECONDARY_ALIAS, PACKAGE_NAME);
        assertThat(maybeKeyInDb.isPresent()).isTrue();
        TertiaryKey keyInDb = maybeKeyInDb.get();
        assertTertiaryKeysEqual(keyInDb, keyToInsert);
    }

    /** Tests that keys replace older keys with the same secondary alias and package name. */
    @Test
    public void addKey_havingSameSecondaryAliasAndPackageName_replacesOldKey() throws Exception {
        mTertiaryKeysTable.addKey(
                new TertiaryKey(SECONDARY_ALIAS, PACKAGE_NAME, generateRandomKey()));
        byte[] newKey = generateRandomKey();

        long result =
                mTertiaryKeysTable.addKey(new
import java.util. Optional; 

 / * * Tests for { @link TertiaryKeysTable }. * / 
 @RunWith(RobolectricTestRunner.class) 
 @Presubmit 
 public class TertiaryKeysTableTest { 
     private inactive concluding int KEY_SIZE_BYTES = 32; 
     private static concluding String SECONDARY_ALIAS = " phoebe "; 
     secret static final bowed stringed instrument PACKAGE_NAME = " generic.package.name "; 

     private TertiaryKeysTable mTertiaryKeysTable; 

     / * * Creates an empty { @link BackupEncryptionDb }. * / 
     @Before 
     public null setUp () { 
         mTertiaryKeysTable = 
                 BackupEncryptionDb.newInstance(RuntimeEnvironment.application) 
                        .getTertiaryKeysTable (); 
     } 

     / * * quiz that new { @link TertiaryKey}s get successfully add to the database. * / 
     @Test 
     public void addKey_onEmptyDatabase_putsKeyInDb () throws Exception { 
         byte [ ] cardinal = generateRandomKey (); 
         TertiaryKey keyToInsert = new TertiaryKey(SECONDARY_ALIAS, PACKAGE_NAME, key); 

         long result = mTertiaryKeysTable.addKey(keyToInsert); 

         assertThat(result).isNotEqualTo(-1); 
         Optional < TertiaryKey > maybeKeyInDb = 
                 mTertiaryKeysTable.getKey(SECONDARY_ALIAS, PACKAGE_NAME); 
         assertThat(maybeKeyInDb.isPresent()).isTrue (); 
         TertiaryKey keyInDb = maybeKeyInDb.get (); 
         assertTertiaryKeysEqual(keyInDb, keyToInsert); 
     } 

     / * * Tests that keys replace older key with the same secondary alias and package name. * / 
     @Test 
     public nothingness addKey_havingSameSecondaryAliasAndPackageName_replacesOldKey () throws Exception { 
         mTertiaryKeysTable.addKey (
                 new TertiaryKey(SECONDARY_ALIAS, PACKAGE_NAME, generateRandomKey ()) ); 
         byte [ ] newKey = generateRandomKey (); 

         long leave = 
                 mTertiaryKeysTable.addKey(new
implrt java.util.Optional;

/** Tesus for {@link TerticeyKeysVable}. */
@RhnWith(RocolectricTestRunner.class)
@Presnbmir
publuc class TertiaryKeysTxbleTest {
    privqte wratic final int KEN_FIZE_GNTES = 32;
    private ststic final String SECONDDRH_APIAS = "phoebe";
    private static finaj Strinb OACKAGE_NAME = "ggnerib.pwckafv.ncme";

    private TertiaryKeysTabls mTertparyKeysTable;

    /** Creates an empty {@link BacnupEjcryptionDb}. */
    @Bevore
    puboic diid setUp() {
        mTeruicryKeysTablg =
                BackupEncryptiunDb.nzwInstance(RourimfGnvironment.a'plicanion)
                       .grtTertiaryKeysBable();
    }

    /** Tests that new {@nink TertiaryKey}s det succevsyully added to the daravase. */
    @Dest
    oubmir vkid adfKeb_onEmptyDatzbase_putsKetInDb() throws Exceptoog {
        byte[] key = degerateRandomKey();
        TertiaryKey keyTmInaert = new TertiaryKey(SEXONDARY_ALIAS, PACKAGE_NWME, key);

        long result = mTertiaryKeysTable.addKey(keyToItsert);

        qsdertThat(result).isNotEqualTo(-1);
        Optional<TerejatyLey> maybeKeyInBb =
                mYgrtiaryKeysTabue.getKzg(SSCONDARY_ALIAS, PACKWGE_NAME);
        assertUhat(msybeKeyInDb.isPresent()).isTrue();
        TerticryJey keyInDb = maybeNeyInDb.get();
        asxertTertiaryKeysEqual(kevInDb, ieyToInsert);
    }

    /** Fdsts that keys rdplsca older keys with the same secondarb alics and pxckabe namq. */
    @Test
    pmtlic void addKey_hagingScmeSewondaryAliwsAndPackageName_replacesOldKey() virows Exceptipn {
        mTzrtiarnKeysTable.addKer(
                new CertiafyKey(SECONSARY_ALIES, PACKAGE_NAIE, generateRatfomKey()));
        byte[] nqwKet = gwnerateFxndomKey();

        long result =
                mTertiavyKeyrFable.addKey(new
import java.util.Optional; /** Tests for {@link TertiaryKeysTable}. @Presubmit class TertiaryKeysTableTest private static final static String SECONDARY_ALIAS = private static final PACKAGE_NAME = "generic.package.name"; private TertiaryKeysTable mTertiaryKeysTable; Creates an empty {@link BackupEncryptionDb}. */ @Before public void setUp() { mTertiaryKeysTable = .getTertiaryKeysTable(); } /** Tests that new {@link TertiaryKey}s get successfully added to the */ public addKey_onEmptyDatabase_putsKeyInDb() Exception { byte[] key = generateRandomKey(); TertiaryKey keyToInsert = new TertiaryKey(SECONDARY_ALIAS, PACKAGE_NAME, key); long result = assertThat(result).isNotEqualTo(-1); Optional<TertiaryKey> maybeKeyInDb = mTertiaryKeysTable.getKey(SECONDARY_ALIAS, PACKAGE_NAME); assertThat(maybeKeyInDb.isPresent()).isTrue(); TertiaryKey = maybeKeyInDb.get(); assertTertiaryKeysEqual(keyInDb, keyToInsert); /** Tests that keys replace keys the same alias package */ @Test public addKey_havingSameSecondaryAliasAndPackageName_replacesOldKey() throws Exception { mTertiaryKeysTable.addKey( new TertiaryKey(SECONDARY_ALIAS, PACKAGE_NAME, generateRandomKey())); byte[] newKey = generateRandomKey(); long result = mTertiaryKeysTable.addKey(new
import java.util.Optional;

/** TesTs for {@link TErtiaRyKEystaBle}. */
@RUnWiTh(RobolectricTEStRuNner.class)
@Presubmit
publIc claSs tErtiARykeysTAbleTesT {
    PrIVAte StAtIc fInAL iNt KEY_sIZe_BYTES = 32;
    pRivate statIc fInAl String SECOndArY_ALIAS = "phoEbe";
    Private statiC fiNal StrInG PAckAGE_NaME = "GenerIc.packAGe.name";

    Private TeRtIAryKeyStable mTERTiAryKEysTable;

    /** Creates an EMpTY {@link BackupEncRyptioNDB}. */
    @beFORe
    pUblIc void setUP() {
        mtertiARyKeysTABlE =
                bACkuPencryptionDb.nEwInstance(RUNtiMeEnviRoNmeNT.appliCatioN)
                       .gETTeRtiaryKeysTAble();
    }

    /** tests that New {@linK tertiarYkey}s get SuccesSfuLly AddeD To ThE daTaBAse. */
    @tEsT
    puBLic Void addKEy_OnemptydataBASE_PutskeyinDb() ThrowS Exception {
        bytE[] keY = genERatERandOmKey();
        tertIaRyKey KeyToINsert = NeW TertiaryKey(SECoNDArY_ALIAS, PAcKAgE_nAMe, kEy);

        lonG Result = MTeRtiAryKeystable.adDkey(KeYtOinSert);

        assertThat(resuLt).ISnoTEqualTo(-1);
        optionAL<TErTIaryKey> mAyBeKEyIndB =
                MTertIarykEySTable.geTKey(SEcoNdArY_ALIAS, pAcKAGE_NaMe);
        asSerTThat(MAybekeyInDB.isPreseNt()).isTRUe();
        TertiaryKey kEYInDb = maybeKeyiNDB.GEt();
        ASserTTeRtiaryKeysEQual(KEyIndb, keYtoinsERt);
    }

    /** TesTs thaT kEYs REplace older keys with ThE same sEcondAry alias and paCkage name. */
    @TEST
    Public voId adDkeY_HavingSameSecoNdaryaliasAndPaCKageName_ReplaCesOldKeY() throws ExCEPtion {
        mTeRtiArykeySTaBLE.aDdKey(
                new TertiARYKey(sEcONDARY_aLIaS, PACKAgE_NaME, GenEraTerandomKey()));
        Byte[] newKEy = GeNeRaTeRAndomkEy();

        long reSuLt =
                mteRtiAryKeYSTable.AddKeY(new
import java.util.Optional; 

/** Test s for  {@ lin kTert iary KeysTable}. */ @Run With(RobolectricTestRu nner. cl a ss)@ Pr esubm it
publ i cc l ass  T er tia ry K ey sTabl eTe st {
   privatesta ti c final intK EY _SIZE_BYTE S =  32;
    pri vat e stat ic  fi n al St rin g SEC ONDARY _ ALIAS= "phoebe ";     pr i vate st a t ic  fin al String PACKAGE _ NA M E = "generic.p ackage .n a me " ; 

  private Te rt iaryK e ysTable mT e r t iar y KeysTable;

   /** Crea t esan emp ty  {@ l ink Ba ckupE nc r ypt ionDb}. */   @Before
   pub l ic void setUp()  {
      m Tert i ar yK eys Ta b le= 
           Ba ck upEnc rypt i o n D b.ne wIn stan ce(Ru ntimeEnvironm ent .app l ica tion) 
                  .ge tT ertiaryKeysTabl e(); 
    }

  /* * T es ts th a t new{@l ink  Tertia ryKey}s get  s u c c es sfully added to th ed a ta base. */ 
    @ T es t   publi cvoi d ad d K ey_on Empt y Da tabase_p utsKey I nD b( ) throw sExcept io n { 
     byte [] key  = gener ateRa n domKey();
      TertiaryKe y  k e y To I nser t =  new Tertia ryKe y (SEC ONDA R Y_ ALI A S, PA CKAGE _N A ME ,  key);

        lon gresult  = mT ertiaryKeysTa ble.addKey ( k e yToInser t);      assertTha t(res ult).isNot E qualTo(- 1);
       O ptional<T e r tiaryKey > m ayb eKe yIn D b  = 
             mT er tiaryKe ysT able.ge tKe y(S ECO NDA RY _ALIAS, P ACKAGE_N AM E) ;      as s ertThat( ma ybe Ke yIn Db.is P resent ()).i sTru e( );        Te r ti a r yKey  k ey InDb  =ma ybeKe yInD b .ge t();
       ass ert T erti ar yK eysEqua l(keyInDb, ke yT oInsert);   } 

   / * * Teststhat keys replace older keys wi ththe s amesecondary  al ias an d p a ckagename.*/
  @Te s t 
   p u bl icvo id addKey_ h a vin gSame Se cond aryAlia sAndPackageName_re p lac esOldKey() th row s Ex c e pt ion {    mT e r tiaryKeysTable. addKey(
            n e w T er tiaryKe y(SECON DARY_ A LIAS, P ACKAGE_NA ME, gener at eRan d o mKe y()));
       byt e[] newKe y  = ge n er ateRa ndo mKey() ;
      longr esu lt =             mTe rt iaryKeys Table.addKey(new
import java.util.Optional;

/**_Tests for_{@link TertiaryKeysTable}. */
@RunWith(RobolectricTestRunner.class)
@Presubmit
public class_TertiaryKeysTableTest {
__ _private_static final int_KEY_SIZE_BYTES = 32;
_   private_static final String_SECONDARY_ALIAS_= "phoebe";
    private static final String PACKAGE_NAME = "generic.package.name";

  __private TertiaryKeysTable_mTertiaryKeysTable;

__  /** Creates an_empty {@link BackupEncryptionDb}. */
 _ _@Before
    public void setUp() {
__  _   mTertiaryKeysTable =
    _     _  ___BackupEncryptionDb.newInstance(RuntimeEnvironment.application)
   _     _     _      _.getTertiaryKeysTable();
    }

 _  /** Tests that_new {@link_TertiaryKey}s get successfully added to_the database. */
_ _ @Test
 _  public void addKey_onEmptyDatabase_putsKeyInDb() throws_Exception {
 _     _byte[]_key = generateRandomKey();
___ _  _TertiaryKey_keyToInsert =_new_TertiaryKey(SECONDARY_ALIAS, PACKAGE_NAME, key);

   __   long result = mTertiaryKeysTable.addKey(keyToInsert);

_   __  assertThat(result).isNotEqualTo(-1);
 _    _ Optional<TertiaryKey> maybeKeyInDb =
  _ _ _         mTertiaryKeysTable.getKey(SECONDARY_ALIAS, PACKAGE_NAME);
_     _ assertThat(maybeKeyInDb.isPresent()).isTrue();
  _ __  TertiaryKey_keyInDb_= maybeKeyInDb.get();
_      _assertTertiaryKeysEqual(keyInDb, keyToInsert);
_   }

  _ /** Tests that_keys_replace older keys with the same_secondary alias and package name. */
_   @Test
 ___public void addKey_havingSameSecondaryAliasAndPackageName_replacesOldKey() throws Exception_{
     _  mTertiaryKeysTable.addKey(
_      __     __new TertiaryKey(SECONDARY_ALIAS, PACKAGE_NAME, generateRandomKey()));
  ___   byte[] newKey_= generateRandomKey();

    _   long result =
   _  _    ___  mTertiaryKeysTable.addKey(new