waddrmgr: remove testing work-around for unexported vars
These work-arounds are no longer needed since the tests now have package-level access.
This commit is contained in:
parent
fb91894a20
commit
f2432b1a5e
2 changed files with 34 additions and 73 deletions
|
@ -1,62 +0,0 @@
|
||||||
// Copyright (c) 2014 The btcsuite developers
|
|
||||||
// Use of this source code is governed by an ISC
|
|
||||||
// license that can be found in the LICENSE file.
|
|
||||||
|
|
||||||
/*
|
|
||||||
This test file is part of the waddrmgr package rather than than the
|
|
||||||
waddrmgr_test package so it can bridge access to the internals to properly test
|
|
||||||
cases which are either not possible or can't reliably be tested via the public
|
|
||||||
interface. The functions are only exported while the tests are being run.
|
|
||||||
*/
|
|
||||||
|
|
||||||
package waddrmgr
|
|
||||||
|
|
||||||
import (
|
|
||||||
"errors"
|
|
||||||
|
|
||||||
"github.com/btcsuite/btcwallet/snacl"
|
|
||||||
)
|
|
||||||
|
|
||||||
// TstCheckPublicPassphrase returns true if the provided public passphrase is
|
|
||||||
// correct for the manager.
|
|
||||||
func (m *Manager) TstCheckPublicPassphrase(pubPassphrase []byte) bool {
|
|
||||||
secretKey := snacl.SecretKey{Key: &snacl.CryptoKey{}}
|
|
||||||
secretKey.Parameters = m.masterKeyPub.Parameters
|
|
||||||
err := secretKey.DeriveKey(&pubPassphrase)
|
|
||||||
return err == nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// failingCryptoKey is an implementation of the EncryptorDecryptor interface
|
|
||||||
// with intentionally fails when attempting to encrypt or decrypt with it.
|
|
||||||
type failingCryptoKey struct {
|
|
||||||
cryptoKey
|
|
||||||
}
|
|
||||||
|
|
||||||
// Encrypt intenionally returns a failure when invoked to test error paths.
|
|
||||||
//
|
|
||||||
// This is part of the EncryptorDecryptor interface implementation.
|
|
||||||
func (c *failingCryptoKey) Encrypt(in []byte) ([]byte, error) {
|
|
||||||
return nil, errors.New("failed to encrypt")
|
|
||||||
}
|
|
||||||
|
|
||||||
// Decrypt intenionally returns a failure when invoked to test error paths.
|
|
||||||
//
|
|
||||||
// This is part of the EncryptorDecryptor interface implementation.
|
|
||||||
func (c *failingCryptoKey) Decrypt(in []byte) ([]byte, error) {
|
|
||||||
return nil, errors.New("failed to decrypt")
|
|
||||||
}
|
|
||||||
|
|
||||||
// TstRunWithFailingCryptoKeyPriv runs the provided callback with the
|
|
||||||
// private crypto key replaced with a version that fails to help test error
|
|
||||||
// paths.
|
|
||||||
func TstRunWithFailingCryptoKeyPriv(m *Manager, callback func()) {
|
|
||||||
orig := m.cryptoKeyPriv
|
|
||||||
defer func() {
|
|
||||||
m.cryptoKeyPriv = orig
|
|
||||||
}()
|
|
||||||
m.cryptoKeyPriv = &failingCryptoKey{}
|
|
||||||
callback()
|
|
||||||
}
|
|
||||||
|
|
||||||
// TstDefaultAccountName is the constant defaultAccountName exported for tests.
|
|
||||||
const TstDefaultAccountName = defaultAccountName
|
|
|
@ -7,6 +7,7 @@ package waddrmgr
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"encoding/hex"
|
"encoding/hex"
|
||||||
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
"reflect"
|
"reflect"
|
||||||
|
@ -21,6 +22,26 @@ import (
|
||||||
"github.com/davecgh/go-spew/spew"
|
"github.com/davecgh/go-spew/spew"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// failingCryptoKey is an implementation of the EncryptorDecryptor interface
|
||||||
|
// with intentionally fails when attempting to encrypt or decrypt with it.
|
||||||
|
type failingCryptoKey struct {
|
||||||
|
cryptoKey
|
||||||
|
}
|
||||||
|
|
||||||
|
// Encrypt intenionally returns a failure when invoked to test error paths.
|
||||||
|
//
|
||||||
|
// This is part of the EncryptorDecryptor interface implementation.
|
||||||
|
func (c *failingCryptoKey) Encrypt(in []byte) ([]byte, error) {
|
||||||
|
return nil, errors.New("failed to encrypt")
|
||||||
|
}
|
||||||
|
|
||||||
|
// Decrypt intenionally returns a failure when invoked to test error paths.
|
||||||
|
//
|
||||||
|
// This is part of the EncryptorDecryptor interface implementation.
|
||||||
|
func (c *failingCryptoKey) Decrypt(in []byte) ([]byte, error) {
|
||||||
|
return nil, errors.New("failed to decrypt")
|
||||||
|
}
|
||||||
|
|
||||||
// newHash converts the passed big-endian hex string into a chainhash.Hash.
|
// newHash converts the passed big-endian hex string into a chainhash.Hash.
|
||||||
// It only differs from the one available in wire in that it panics on an
|
// It only differs from the one available in wire in that it panics on an
|
||||||
// error since it will only (and must only) be called with hard-coded, and
|
// error since it will only (and must only) be called with hard-coded, and
|
||||||
|
@ -1147,8 +1168,11 @@ func testChangePassphrase(tc *testContext) bool {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
// Ensure the public passphrase was successfully changed.
|
// Ensure the public passphrase was successfully changed. We do this by
|
||||||
if !tc.rootManager.TstCheckPublicPassphrase(pubPassphrase2) {
|
// being able to re-derive the public key with the new passphrase.
|
||||||
|
secretKey := snacl.SecretKey{Key: &snacl.CryptoKey{}}
|
||||||
|
secretKey.Parameters = tc.rootManager.masterKeyPub.Parameters
|
||||||
|
if err := secretKey.DeriveKey(&pubPassphrase2); err != nil {
|
||||||
tc.t.Errorf("%s: passphrase does not match", testName)
|
tc.t.Errorf("%s: passphrase does not match", testName)
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
@ -1352,7 +1376,7 @@ func testNewAccount(tc *testContext) bool {
|
||||||
func testLookupAccount(tc *testContext) bool {
|
func testLookupAccount(tc *testContext) bool {
|
||||||
// Lookup accounts created earlier in testNewAccount
|
// Lookup accounts created earlier in testNewAccount
|
||||||
expectedAccounts := map[string]uint32{
|
expectedAccounts := map[string]uint32{
|
||||||
TstDefaultAccountName: DefaultAccountNum,
|
defaultAccountName: DefaultAccountNum,
|
||||||
ImportedAddrAccountName: ImportedAddrAccount,
|
ImportedAddrAccountName: ImportedAddrAccount,
|
||||||
}
|
}
|
||||||
for acctName, expectedAccount := range expectedAccounts {
|
for acctName, expectedAccount := range expectedAccounts {
|
||||||
|
@ -1980,16 +2004,15 @@ func TestEncryptDecryptErrors(t *testing.T) {
|
||||||
t.Fatal("Attempted to unlock the manager, but failed:", err)
|
t.Fatal("Attempted to unlock the manager, but failed:", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Make sure to cover the ErrCrypto error path in Encrypt.
|
// Make sure to cover the ErrCrypto error path in Encrypt and Decrypt.
|
||||||
TstRunWithFailingCryptoKeyPriv(mgr, func() {
|
// We'll use a mock private key that will fail upon running these
|
||||||
|
// methods.
|
||||||
|
mgr.cryptoKeyPriv = &failingCryptoKey{}
|
||||||
|
|
||||||
_, err = mgr.Encrypt(CKTPrivate, []byte{})
|
_, err = mgr.Encrypt(CKTPrivate, []byte{})
|
||||||
})
|
|
||||||
checkManagerError(t, "failed encryption", err, ErrCrypto)
|
checkManagerError(t, "failed encryption", err, ErrCrypto)
|
||||||
|
|
||||||
// Make sure to cover the ErrCrypto error path in Decrypt.
|
|
||||||
TstRunWithFailingCryptoKeyPriv(mgr, func() {
|
|
||||||
_, err = mgr.Decrypt(CKTPrivate, []byte{})
|
_, err = mgr.Decrypt(CKTPrivate, []byte{})
|
||||||
})
|
|
||||||
checkManagerError(t, "failed decryption", err, ErrCrypto)
|
checkManagerError(t, "failed decryption", err, ErrCrypto)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue