waddrmgr/manager_test: use SetSecretKeyGen to safely swap keygen

This commit is contained in:
Conner Fromknecht 2018-08-31 16:28:38 -07:00
parent 644fd2bda0
commit 85c75de4a5
No known key found for this signature in database
GPG key ID: E7D737B67FA592C7

View file

@ -16,6 +16,7 @@ import (
"github.com/btcsuite/btcd/chaincfg" "github.com/btcsuite/btcd/chaincfg"
"github.com/btcsuite/btcd/chaincfg/chainhash" "github.com/btcsuite/btcd/chaincfg/chainhash"
"github.com/btcsuite/btcutil" "github.com/btcsuite/btcutil"
"github.com/btcsuite/btcwallet/snacl"
"github.com/btcsuite/btcwallet/waddrmgr" "github.com/btcsuite/btcwallet/waddrmgr"
"github.com/btcsuite/btcwallet/walletdb" "github.com/btcsuite/btcwallet/walletdb"
"github.com/davecgh/go-spew/spew" "github.com/davecgh/go-spew/spew"
@ -33,6 +34,13 @@ func newHash(hexStr string) *chainhash.Hash {
return hash return hash
} }
// failingSecretKeyGen is a waddrmgr.SecretKeyGenerator that always returns
// snacl.ErrDecryptFailed.
func failingSecretKeyGen(passphrase *[]byte,
config *waddrmgr.ScryptOptions) (*snacl.SecretKey, error) {
return nil, snacl.ErrDecryptFailed
}
// testContext is used to store context information about a running test which // testContext is used to store context information about a running test which
// is passed into helper functions. The useSpends field indicates whether or // is passed into helper functions. The useSpends field indicates whether or
// not the spend data should be empty or figure it out based on the specific // not the spend data should be empty or figure it out based on the specific
@ -1099,21 +1107,20 @@ func testChangePassphrase(tc *testContext) bool {
// that intentionally errors. // that intentionally errors.
testName := "ChangePassphrase (public) with invalid new secret key" testName := "ChangePassphrase (public) with invalid new secret key"
var err error oldKeyGen := waddrmgr.SetSecretKeyGen(failingSecretKeyGen)
waddrmgr.TstRunWithReplacedNewSecretKey(func() { err := walletdb.Update(tc.db, func(tx walletdb.ReadWriteTx) error {
err = walletdb.Update(tc.db, func(tx walletdb.ReadWriteTx) error {
ns := tx.ReadWriteBucket(waddrmgrNamespaceKey) ns := tx.ReadWriteBucket(waddrmgrNamespaceKey)
return tc.rootManager.ChangePassphrase( return tc.rootManager.ChangePassphrase(
ns, pubPassphrase, pubPassphrase2, false, fastScrypt, ns, pubPassphrase, pubPassphrase2, false, fastScrypt,
) )
}) })
})
if !checkManagerError(tc.t, testName, err, waddrmgr.ErrCrypto) { if !checkManagerError(tc.t, testName, err, waddrmgr.ErrCrypto) {
return false return false
} }
// Attempt to change public passphrase with invalid old passphrase. // Attempt to change public passphrase with invalid old passphrase.
testName = "ChangePassphrase (public) with invalid old passphrase" testName = "ChangePassphrase (public) with invalid old passphrase"
waddrmgr.SetSecretKeyGen(oldKeyGen)
err = walletdb.Update(tc.db, func(tx walletdb.ReadWriteTx) error { err = walletdb.Update(tc.db, func(tx walletdb.ReadWriteTx) error {
ns := tx.ReadWriteBucket(waddrmgrNamespaceKey) ns := tx.ReadWriteBucket(waddrmgrNamespaceKey)
return tc.rootManager.ChangePassphrase( return tc.rootManager.ChangePassphrase(