/** Ċ * ĠCopyright ĠS or am its u ĠCo ., ĠLtd . ĠAll ĠRights ĠReserved . Ċ * ĠSPDX - License - Identifier : ĠGPL - 3 . 0 Ċ */ Ċ Ċ package Ġjp . co . sor am its u . common . data . credentials . repository Ċ Ċ import Ġandroidx . arch . core . executor . testing . Instant Task Executor Rule Ċ import Ġjp . co . sor am its u . common . data . network . substr ate . Options Provider Ċ import Ġcom . google . firebase . crash ly t ics . Firebase Crash ly t ics Ċ import Ġio . mock k . every Ċ import Ġio . mock k . mock k Static Ċ import Ġjp . co . sor am its u . common . data . network . substr ate . to S ora Address Ċ import Ġjp . co . sor am its u . common . domain . credentials . Credentials Datasource Ċ import Ġjp . co . sor am its u . common . util . Crypto Assistant Ċ import Ġjp . co . sor am its u . fe ar less _ utils . bip 3 9 . B ip 3 9 Ċ import Ġjp . co . sor am its u . fe ar less _ utils . bip 3 9 . Mnemonic Length Ċ import Ġjp . co . sor am its u . fe ar less _ utils . encrypt . Key pair Factory Ċ import Ġjp . co . sor am its u . fe ar less _ utils . encrypt . model . Key pair Ċ import Ġjp . co . sor am its u . test _ shared . Main Coroutine Rule Ċ import Ġjp . co . sor am its u . test _ shared . any NonNull Ċ import Ġkotlinx . coroutines . Experimental Cor outines Api Ċ import Ġkotlinx . coroutines . test . run Blocking Test Ċ import Ġorg . bouncycastle . util . en coders . Hex Ċ import Ġorg . junit . Assert . assertEquals Ċ import Ġorg . junit . Assert . assertFalse Ċ import Ġorg . junit . Assert . assertTrue Ċ import Ġorg . junit . Before Ċ import Ġorg . junit . Rule Ċ import Ġorg . junit . Test Ċ import Ġorg . junit . rules . Test Rule Ċ import Ġorg . junit . runner . RunWith Ċ import Ġorg . mockito . BDD Mockito .* Ċ import Ġorg . mockito . Mockito . mock Ċ import Ġorg . mockito . junit . Mockito JUnit Runner Ċ import Ġjava . security . KeyPair Ċ import Ġjava . security . PublicKey Ċ Ċ @ Experimental Cor outines Api Ċ @ RunWith ( Mockito JUnit Runner :: class ) Ċ class ĠCredentials Repository Test Ġ{ ĊĊĠĠĠ Ġ@ get : Rule ĊĠĠĠ Ġval Ġrule : ĠTest Rule Ġ= ĠInstant Task Executor Rule () ĊĊĠĠĠ Ġ@ get : Rule ĊĠĠĠ Ġvar Ġmain Coroutine Rule Ġ= ĠMain Coroutine Rule () ĊĊĠĠĠ Ġprivate Ġlateinit Ġvar Ġcredentials Repository : ĠCredentials Repository Impl ĊĊĠĠĠ Ġprivate Ġval Ġdatasource Ġ= Ġmock ( Credentials Datasource :: class . java ) ĊĠĠĠ Ġprivate Ġval Ġcrypto Assistant Ġ= Ġmock ( Crypto Assistant :: class . java ) ĊĠĠĠ Ġprivate Ġval Ġb ip 3 9 Ġ= Ġmock ( B ip 3 9 :: class . java ) ĊĠĠĠ Ġprivate Ġval Ġkey pair Factory Ġ= Ġmock ( Key pair Factory :: class . java ) ĊĊĠĠĠ Ġ@ Before Ġfun Ġsetup () Ġ{ ĊĠĠĠĠĠĠĠ Ġcredentials Repository Ġ= ĠCredentials Repository Impl ( datasource , Ġcrypto Assistant ) ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġ@ Test ĊĠĠĠ Ġfun Ġ` is Ġmnemonic Ġvalid Ġreturns Ġtrue `() Ġ= Ġrun Blocking Test Ġ{ ĊĠĠĠĠĠĠĠ Ġval Ġmnemonic Ġ= Ġ" mn emonic " ĊĊĠĠĠĠĠĠĠ Ġgiven ( crypto Assistant . bip 3 9 ). willReturn ( bip 3 9 ) ĊĠĠĠĠĠĠĠ Ġgiven ( bip 3 9 . is Mnemonic Valid ( mn emonic )). willReturn ( true ) ĊĊĠĠĠĠĠĠĠ ĠassertTrue ( credentials Repository . is Mnemonic Valid ( mn emonic )) ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġ@ Test ĊĠĠĠ Ġfun Ġ` is Ġmnemonic Ġvalid Ġreturns Ġfalse `() Ġ= Ġrun Blocking Test Ġ{ ĊĠĠĠĠĠĠĠ Ġval Ġmnemonic Ġ= Ġ" mn emonic 2 " ĊĊĠĠĠĠĠĠĠ Ġgiven ( crypto Assistant . bip 3 9 ). willReturn ( bip 3 9 ) ĊĠĠĠĠĠĠĠ Ġgiven ( bip 3 9 . is Mnemonic Valid ( mn emonic )). willReturn ( false ) ĊĊĠĠĠĠĠĠĠ ĠassertFalse ( credentials Repository . is Mnemonic Valid ( mn emonic )) ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġ@ Test ĊĠĠĠ Ġfun Ġ` generate Ġuser Ġcredentials Ġcalled `() Ġ= Ġrun Blocking Test Ġ{ ĊĠĠĠĠĠĠĠ Ġval Ġmnemonic Ġ= Ġ" airport Ġwish Ġwish Ġloan Ġwidth Ġcountry Ġa co ust ic Ġcountry Ġce iling Ġgood Ġen act Ġpenalty " ĊĠĠĠĠĠĠĠ Ġval Ġentropy Ġ= Ġmnemonic . toByteArray () ĊĠĠĠĠĠĠĠ Ġval Ġseed Ġ= Ġ" seed ". toByteArray () ĊĠĠĠĠĠĠĠ Ġval Ġkey pair Ġ= Ġmock ( Key pair :: class . java ) ĊĠĠĠĠĠĠĠ Ġval Ġfc Ġ= Ġmock ( Firebase Crash ly t ics :: class . java ) ĊĊĠĠĠĠĠĠĠ Ġgiven ( crypto Assistant . bip 3 9 ). willReturn ( bip 3 9 ) ĊĠĠĠĠĠĠĠ Ġgiven ( crypto Assistant . key Pair Factory ). willReturn ( key pair Factory ) ĊĠĠĠĠĠĠĠ Ġgiven ( key pair Factory . generate ( Options Provider . encryption Type , Ġseed )). willReturn ( key pair ) ĊĠĠĠĠĠĠĠ Ġgiven ( bip 3 9 . generate Mnemonic ( Mnemonic Length . TW EL VE )). willReturn ( mn emonic ) ĊĠĠĠĠĠĠĠ Ġgiven ( bip 3 9 . generate Entropy ( mn emonic )). willReturn ( entropy ) ĊĠĠĠĠĠĠĠ Ġgiven ( bip 3 9 . generate Seed ( entropy , Ġ" ")). willReturn ( seed ) ĊĊĠĠĠĠĠĠĠ Ġmock k Static ( ByteArray :: to S ora Address ) ĊĠĠĠĠĠĠĠ Ġmock k Static ( Firebase Crash ly t ics :: getInstance ) ĊĠĠĠĠĠĠĠ Ġevery Ġ{ Ġkey pair . publicKey . to S ora Address () Ġ} Ġreturns Ġ" foo address " ĊĠĠĠĠĠĠĠ Ġevery Ġ{ ĠFirebase Crash ly t ics . getInstance () Ġ} Ġreturns Ġfc ĊĠĠĠĠĠĠĠ Ġcredentials Repository . generate User Credentials () ĊĊĠĠĠĠĠĠĠ Ġverify ( bip 3 9 ). generate Mnemonic ( Mnemonic Length . TW EL VE ) ĊĠĠĠĠĠĠĠ Ġverify ( bip 3 9 ). generate Entropy ( mn emonic ) ĊĠĠĠĠĠĠĠ Ġverify ( bip 3 9 ). generate Seed ( entropy , Ġ"") ĊĠĠĠĠĠĠĠ Ġverify ( key pair Factory ). generate ( Options Provider . encryption Type , Ġseed ) ĊĠĠĠĠĠĠĠ Ġverify ( datasource ). save Keys ( key pair ) ĊĠĠĠĠĠĠĠ Ġverify ( datasource ). save Mnemonic ( mn emonic ) ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġ@ Test ĊĠĠĠ Ġfun Ġ` restore Ġuser Ġcredentials Ġcalled `() Ġ= Ġrun Blocking Test Ġ{ ĊĠĠĠĠĠĠĠ Ġval Ġmnemonic Ġ= Ġ" airport Ġwish Ġwish Ġloan Ġwidth Ġcountry Ġa co ust ic Ġcountry Ġce iling Ġgood Ġen act Ġpenalty " ĊĠĠĠĠĠĠĠ Ġval Ġentropy Ġ= Ġmnemonic . toByteArray () ĊĠĠĠĠĠĠĠ Ġval Ġseed Ġ= Ġ" seed ". toByteArray () ĊĠĠĠĠĠĠĠ Ġval ĠpublicKey Bytes Ġ= Ġ" publicKey Bytes publicKey Bytes publicKey Bytes ". toByteArray () ĊĠĠĠĠĠĠĠ Ġval Ġi ro ha Key pair Ġ= Ġmock ( KeyPair :: class . java ) ĊĠĠĠĠĠĠĠ Ġval Ġi ro ha Address Ġ= Ġ" did _ sor a _ 7 0 7 5 6 2 6 c 6 9 6 3 4 b 6 5 7 9 4 2 @ sor a " ĊĠĠĠĠĠĠĠ Ġval Ġmessage Ġ= Ġi ro ha Address Ġ+ ĠHex . to HexString ( publicKey Bytes ) ĊĠĠĠĠĠĠĠ Ġval Ġsignature Ġ= Ġi ro ha Address . toByteArray () ĊĠĠĠĠĠĠĠ Ġval Ġkey pair Ġ= Ġmock ( Key pair :: class . java ) ĊĠĠĠĠĠĠĠ Ġval ĠpublicKey Ġ= Ġmock ( PublicKey :: class . java ) ĊĠĠĠĠĠĠĠ Ġgiven ( publicKey . encoded ). willReturn ( publicKey Bytes ) ĊĠĠĠĠĠĠĠ Ġgiven ( iro ha Key pair . public ). willReturn ( publicKey ) ĊĊĠĠĠĠĠĠĠ Ġgiven ( crypto Assistant . bip 3 9 ). willReturn ( bip 3 9 ) ĊĠĠĠĠĠĠĠ Ġgiven ( crypto Assistant . key Pair Factory ). willReturn ( key pair Factory ) ĊĠĠĠĠĠĠĠ Ġgiven ( key pair Factory . generate ( Options Provider . encryption Type , Ġseed )). willReturn ( key pair ) ĊĠĠĠĠĠĠĠ Ġgiven ( crypto Assistant . generate Sc rypt Seed For Ed 2 5 5 1 9 ( any NonNull (), Ġany NonNull (), Ġany NonNull (), Ġany NonNull ())). willReturn ( seed ) ĊĠĠĠĠĠĠĠ Ġgiven ( crypto Assistant . generate Ed 2 5 5 1 9 Keys ( seed )). willReturn ( iro ha Key pair ) ĊĠĠĠĠĠĠĠ Ġgiven ( crypto Assistant . sign Ed 2 5 5 1 9 ( message . toByteArray ( charset (" UTF - 8 ")), Ġi ro ha Key pair )). willReturn ( signature ) ĊĠĠĠĠĠĠĠ Ġgiven ( bip 3 9 . generate Entropy ( mn emonic )). willReturn ( entropy ) ĊĠĠĠĠĠĠĠ Ġgiven ( bip 3 9 . generate Seed ( entropy , Ġ" ")). willReturn ( seed ) ĊĊĠĠĠĠĠĠĠ Ġcredentials Repository . restore User Credentials ( mn emonic ) ĊĊĠĠĠĠĠĠĠ Ġverify ( bip 3 9 ). generate Entropy ( mn emonic ) ĊĠĠĠĠĠĠĠ Ġverify ( bip 3 9 ). generate Seed ( entropy , Ġ"") ĊĠĠĠĠĠĠĠ Ġverify ( key pair Factory ). generate ( Options Provider . encryption Type , Ġseed ) ĊĠĠĠĠĠĠĠ Ġverify ( datasource ). save Mnemonic ( mn emonic ) ĊĠĠĠĠĠĠĠ Ġverify ( crypto Assistant ). sign Ed 2 5 5 1 9 ( message . toByteArray ( charset (" UTF - 8 ")), Ġi ro ha Key pair ) ĊĠĠĠĠĠĠĠ Ġverify ( datasource ). save I ro ha Keys ( iro ha Key pair ) ĊĠĠĠĠĠĠĠ Ġverify ( datasource ). save I ro ha Address ( iro ha Address ) ĊĠĠĠĠĠĠĠ Ġverify ( datasource ). save Signature ( signature ) ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġ@ Test ĊĠĠĠ Ġfun Ġ` save Ġmnemonic Ġcalled `() Ġ= Ġrun Blocking Test Ġ{ ĊĠĠĠĠĠĠĠ Ġval Ġmnemonic Ġ= Ġ" airport Ġwish Ġwish Ġloan Ġwidth Ġcountry Ġa co ust ic Ġcountry Ġce iling Ġgood Ġen act Ġpenalty " ĊĊĠĠĠĠĠĠĠ Ġcredentials Repository . save Mnemonic ( mn emonic ) ĊĊĠĠĠĠĠĠĠ Ġverify ( datasource ). save Mnemonic ( mn emonic ) ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġ@ Test ĊĠĠĠ Ġfun Ġ` retrieve Ġmnemonic Ġcalled `() Ġ= Ġrun Blocking Test Ġ{ ĊĠĠĠĠĠĠĠ Ġval Ġmnemonic Ġ= Ġ" airport Ġwish Ġwish Ġloan Ġwidth Ġcountry Ġa co ust ic Ġcountry Ġce iling Ġgood Ġen act Ġpenalty " ĊĠĠĠĠĠĠĠ Ġgiven ( datasource . retrieve Mnemonic ()). willReturn ( mn emonic ) ĊĊĠĠĠĠĠĠĠ ĠassertEquals ( credentials Repository . retrieve Mnemonic (), Ġmnemonic ) ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġ@ Test ĊĠĠĠ Ġfun Ġ` retrieve Ġkey pair Ġcalled `() Ġ= Ġrun Blocking Test Ġ{ ĊĠĠĠĠĠĠĠ Ġval Ġkey pair Ġ= Ġmock ( Key pair :: class . java ) ĊĠĠĠĠĠĠĠ Ġgiven ( datasource . retrieve Keys ()). willReturn ( key pair ) ĊĊĠĠĠĠĠĠĠ ĠassertEquals ( credentials Repository . retrieve KeyPair (), Ġkey pair ) ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġ@ Test ĊĠĠĠ Ġfun Ġ` retrieve Ġi ro ha Ġkey pair Ġcalled `() Ġ= Ġrun Blocking Test Ġ{ ĊĠĠĠĠĠĠĠ Ġval Ġkey pair Ġ= Ġmock ( KeyPair :: class . java ) ĊĠĠĠĠĠĠĠ Ġgiven ( datasource . retrieve I ro ha Keys ()). willReturn ( key pair ) ĊĊĠĠĠĠĠĠĠ ĠassertEquals ( credentials Repository . retrieve I ro ha KeyPair (), Ġkey pair ) ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġ@ Test ĊĠĠĠ Ġfun Ġ` get Ġi ro ha Ġaddress Ġcalled `() Ġ= Ġrun Blocking Test Ġ{ ĊĠĠĠĠĠĠĠ Ġval Ġi ro ha Address Ġ= Ġ" did _ sor a _ 7 0 7 5 6 2 6 c 6 9 6 3 4 b 6 5 7 9 4 2 @ sor a " ĊĠĠĠĠĠĠĠ Ġgiven ( datasource . get I ro ha Address ()). willReturn ( iro ha Address ) ĊĊĠĠĠĠĠĠĠ ĠassertEquals ( credentials Repository . get I ro ha Address (), Ġi ro ha Address ) ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġ@ Test ĊĠĠĠ Ġfun Ġ` get Ġi ro ha Ġaddress Ġif Ġempty Ġcalled `() Ġ= Ġrun Blocking Test Ġ{ ĊĠĠĠĠĠĠĠ Ġval Ġi ro ha Address Ġ= Ġ" did _ sor a _ 7 0 7 5 6 2 6 c 6 9 6 3 4 b 6 5 7 9 4 2 @ sor a " ĊĠĠĠĠĠĠĠ Ġval Ġmnemonic Ġ= Ġ" airport Ġwish Ġwish Ġloan Ġwidth Ġcountry Ġa co ust ic Ġcountry Ġce iling Ġgood Ġen act Ġpenalty " ĊĠĠĠĠĠĠĠ Ġval Ġentropy Ġ= Ġmnemonic . toByteArray () ĊĠĠĠĠĠĠĠ Ġval Ġseed Ġ= Ġ" seed ". toByteArray () ĊĠĠĠĠĠĠĠ Ġval ĠpublicKey Bytes Ġ= Ġ" publicKey Bytes publicKey Bytes publicKey Bytes ". toByteArray () ĊĠĠĠĠĠĠĠ Ġval Ġi ro ha Key pair Ġ= Ġmock ( KeyPair :: class . java ) ĊĠĠĠĠĠĠĠ Ġval Ġmessage Ġ= Ġi ro ha Address Ġ+ ĠHex . to HexString ( publicKey Bytes ) ĊĠĠĠĠĠĠĠ Ġval Ġsignature Ġ= Ġi ro ha Address . toByteArray () ĊĠĠĠĠĠĠĠ Ġval Ġkey pair Ġ= Ġmock ( Key pair :: class . java ) ĊĠĠĠĠĠĠĠ Ġval ĠpublicKey Ġ= Ġmock ( PublicKey :: class . java ) ĊĠĠĠĠĠĠĠ Ġval Ġfc Ġ= Ġmock ( Firebase Crash ly t ics :: class . java ) ĊĠĠĠĠĠĠĠ Ġgiven ( publicKey . encoded ). willReturn ( publicKey Bytes ) ĊĠĠĠĠĠĠĠ Ġgiven ( iro ha Key pair . public ). willReturn ( publicKey ) ĊĠĠĠĠĠĠĠ Ġgiven ( crypto Assistant . bip 3 9 ). willReturn ( bip 3 9 ) ĊĠĠĠĠĠĠĠ Ġgiven ( crypto Assistant . key Pair Factory ). willReturn ( key pair Factory ) ĊĠĠĠĠĠĠĠ Ġgiven ( key pair Factory . generate ( Options Provider . encryption Type , Ġseed )). willReturn ( key pair ) ĊĠĠĠĠĠĠĠ Ġgiven ( bip 3 9 . generate Entropy ( mn emonic )). willReturn ( entropy ) ĊĠĠĠĠĠĠĠ Ġgiven ( bip 3 9 . generate Seed ( entropy , Ġ" ")). willReturn ( seed ) ĊĠĠĠĠĠĠĠ Ġgiven ( key pair Factory . generate ( Options Provider . encryption Type , Ġseed )). willReturn ( key pair ) ĊĠĠĠĠĠĠĠ Ġgiven ( crypto Assistant . generate Sc rypt Seed For Ed 2 5 5 1 9 ( any NonNull (), Ġany NonNull (), Ġany NonNull (), Ġany NonNull ())). willReturn ( seed ) ĊĠĠĠĠĠĠĠ Ġgiven ( crypto Assistant . generate Ed 2 5 5 1 9 Keys ( seed )). willReturn ( iro ha Key pair ) ĊĠĠĠĠĠĠĠ Ġgiven ( crypto Assistant . sign Ed 2 5 5 1 9 ( message . toByteArray ( charset (" UTF - 8 ")), Ġi ro ha Key pair )). willReturn ( signature ) ĊĠĠĠĠĠĠĠ Ġgiven ( datasource . get I ro ha Address ()). willReturn ("", Ġi ro ha Address ) ĊĠĠĠĠĠĠĠ Ġgiven ( datasource . retrieve Mnemonic ()). willReturn ( mn emonic ) ĊĠĠĠĠĠĠĠ Ġmock k Static ( ByteArray :: to S ora Address ) ĊĠĠĠĠĠĠĠ Ġmock k Static ( Firebase Crash ly t ics :: getInstance ) ĊĠĠĠĠĠĠĠ Ġevery Ġ{ Ġkey pair . publicKey . to S ora Address () Ġ} Ġreturns Ġ" fo Ġo address " ĊĠĠĠĠĠĠĠ Ġevery Ġ{ ĠFirebase Crash ly t ics . getInstance () Ġ} Ġreturns Ġfc ĊĊĠĠĠĠĠĠĠ ĠassertEquals ( credentials Repository . get I ro ha Address (), Ġi ro ha Address ) ĊĊĠĠĠĠĠĠĠ Ġverify ( bip 3 9 ). generate Entropy ( mn emonic ) ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġ@ Test ĊĠĠĠ Ġfun Ġ` get Ġclaim Ġsignature Ġcalled `() Ġ= Ġrun Blocking Test Ġ{ ĊĠĠĠĠĠĠĠ Ġval Ġsignature Ġ= Ġ" did _ sor a _ 7 0 7 5 6 2 6 c 6 9 6 3 4 b 6 5 7 9 4 2 @ sor a ". toByteArray () ĊĠĠĠĠĠĠĠ Ġval Ġsignature Hex Ġ= ĠHex . to HexString ( signature ) ĊĠĠĠĠĠĠĠ Ġgiven ( datasource . retrieve Signature ()). willReturn ( signature ) ĊĊĠĠĠĠĠĠĠ ĠassertEquals ( credentials Repository . get Claim Signature (), Ġsignature Hex ) ĊĠĠĠ Ġ} Ċ }