Skip to content

Commit

Permalink
add corresponding unit & property tests
Browse files Browse the repository at this point in the history
  • Loading branch information
KtorZ committed Apr 23, 2019
1 parent cbec0b1 commit 6a67224
Showing 1 changed file with 40 additions and 1 deletion.
41 changes: 40 additions & 1 deletion test/unit/Cardano/Wallet/Primitive/AddressDerivationSpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -14,27 +14,34 @@ import Cardano.Wallet.Primitive.AddressDerivation
( ChangeChain (..)
, Depth (..)
, DerivationType (..)
, ErrWrongPassphrase (..)
, Index
, Passphrase (..)
, PassphraseMaxLength (..)
, PassphraseMinLength (..)
, checkPassphrase
, deriveAccountPrivateKey
, deriveAddressPrivateKey
, deriveAddressPublicKey
, encryptPassphrase
, generateKeyFromSeed
, getIndex
, keyToAddress
, publicKey
, unsafeGenerateKeyFromSeed
)
import Cardano.Wallet.Primitive.Types
( Hash (..) )
import Control.Monad
( replicateM )
import Control.Monad.IO.Class
( liftIO )
import Data.Proxy
( Proxy (..) )
import Fmt
( build, fmt )
import Test.Hspec
( Spec, describe, it )
( Spec, describe, it, shouldBe )
import Test.QuickCheck
( Arbitrary (..)
, InfiniteList (..)
Expand All @@ -49,6 +56,8 @@ import Test.QuickCheck
, (===)
, (==>)
)
import Test.QuickCheck.Monadic
( monadicIO )
import Test.Text.Roundtrip
( textRoundtrip )

Expand Down Expand Up @@ -116,6 +125,15 @@ spec = do
goldenYoroiAddr (seed1, recPwd1) InternalChain accIx addrIx
"Ae2tdPwUPEZLSqQN7XNJRMJ6yHWdfFLaQgPPYgyJKrJnCVnRtbfw6EHRv1D"

describe "Passphrases" $ do
it "checkPassphrase p h(p) == Right ()" $
property prop_passphraseRoundtrip

it "p /= p' => checkPassphrase p' h(p) == Left ErrWrongPassphrase" $
property prop_passphraseRoundtripFail

it "checkPassphrase fails when hash is malformed" $
property prop_passphraseHashMalformed

{-------------------------------------------------------------------------------
Properties
Expand Down Expand Up @@ -212,6 +230,27 @@ goldenYoroiAddr (seed, recPwd) cc accIx addrIx addr =
in
fmt (build $ keyToAddress $ publicKey addrXPrv) === addr

prop_passphraseRoundtrip
:: Passphrase "encryption"
-> Property
prop_passphraseRoundtrip pwd = monadicIO $ liftIO $ do
hpwd <- encryptPassphrase pwd
checkPassphrase pwd hpwd `shouldBe` Right ()

prop_passphraseRoundtripFail
:: (Passphrase "encryption", Passphrase "encryption")
-> Property
prop_passphraseRoundtripFail (p, p') =
p /= p' ==> monadicIO $ liftIO $ do
hp <- encryptPassphrase p
checkPassphrase p' hp `shouldBe` Left ErrWrongPassphrase

prop_passphraseHashMalformed
:: Passphrase "encryption"
-> Property
prop_passphraseHashMalformed pwd = monadicIO $ liftIO $ do
checkPassphrase pwd (Hash mempty) `shouldBe` Left ErrWrongPassphrase


{-------------------------------------------------------------------------------
Arbitrary Instances
Expand Down

0 comments on commit 6a67224

Please sign in to comment.