Merge pull request #570 from wpaulino/waddrmgr-package-level-tests

waddrmgr: convert unit tests to package-level tests
This commit is contained in:
Olaoluwa Osuntokun 2018-11-08 10:48:39 +11:00 committed by GitHub
commit 9eec895db8
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 250 additions and 246 deletions

View file

@ -2,7 +2,7 @@
// Use of this source code is governed by an ISC
// license that can be found in the LICENSE file.
package waddrmgr_test
package waddrmgr
import (
"encoding/hex"
@ -13,7 +13,6 @@ import (
"time"
"github.com/btcsuite/btcd/chaincfg"
"github.com/btcsuite/btcwallet/waddrmgr"
"github.com/btcsuite/btcwallet/walletdb"
_ "github.com/btcsuite/btcwallet/walletdb/bdb"
)
@ -34,7 +33,7 @@ var (
// fastScrypt are parameters used throughout the tests to speed up the
// scrypt operations.
fastScrypt = &waddrmgr.ScryptOptions{
fastScrypt = &ScryptOptions{
N: 16,
R: 8,
P: 1,
@ -55,7 +54,7 @@ var (
pubKey: hexToBytes("02d8f88468c5a2e8e1815faf555f59cbd1979e3dbdf823f80c271b6fb70d2d519b"),
privKey: hexToBytes("c27d6581b92785834b381fa697c4b0ffc4574b495743722e0acb7601b1b68b99"),
privKeyWIF: "L3jmpy54Pc7MLXTN2mL8Xas7BJziwKaUGmgnXXzgGbVRdiAniXZk",
derivationInfo: waddrmgr.DerivationPath{
derivationInfo: DerivationPath{
Account: 0,
Branch: 0,
Index: 0,
@ -70,7 +69,7 @@ var (
pubKey: hexToBytes("02b9c175b908624f8a8eaac227d0e8c77c0eec327b8c512ad1b8b7a4b5b676971f"),
privKey: hexToBytes("18f3b191019e83878a81557abebb2afda199e31d22e150d8bf4df4561671be6c"),
privKeyWIF: "Kx4DNid19W8sjNFN3uPqQE7UYnCqyEp7unCvdkf2LrVUFpnDtwpB",
derivationInfo: waddrmgr.DerivationPath{
derivationInfo: DerivationPath{
Account: 0,
Branch: 0,
Index: 1,
@ -85,7 +84,7 @@ var (
pubKey: hexToBytes("0329faddf1254d490d6add49e2b08cf52b561038c72baec0edb3cfacff71ff1021"),
privKey: hexToBytes("ccb8f6305b73136b363644b647f6efc0fd27b6b7d9c11c7e560662ed38db7b34"),
privKeyWIF: "L45fWF6Yd736fDohuB97vwRRLdQQJr3ZGvbokk9ubiT7aNrg7tTn",
derivationInfo: waddrmgr.DerivationPath{
derivationInfo: DerivationPath{
Account: 0,
Branch: 0,
Index: 2,
@ -100,7 +99,7 @@ var (
pubKey: hexToBytes("03d738324e2f0ce42e46975d7f8c7117c1670e3d7912b0291aea452add99674774"),
privKey: hexToBytes("d6bc8ff768814fede2adcdb74826bd846924341b3862e3b6e31cdc084e992940"),
privKeyWIF: "L4R8XyxYQyPSpTwj8w96tM86a6j3QA9jbRPj3RA7DVTVWk71ndeP",
derivationInfo: waddrmgr.DerivationPath{
derivationInfo: DerivationPath{
Account: 0,
Branch: 0,
Index: 3,
@ -115,7 +114,7 @@ var (
pubKey: hexToBytes("03a917acd5cd5b6f544b43f1921a35677e4d5320e5d2add2056039b4b44fdf905e"),
privKey: hexToBytes("8563ade061110e03aee50695ffc5cb1c06c8310bde0a3674257c853c966968c0"),
privKeyWIF: "L1h16Hunxomww4FrpyQP2iFmWNgG7U1u3awp6Vd3s2uGf7v5VU8c",
derivationInfo: waddrmgr.DerivationPath{
derivationInfo: DerivationPath{
Account: 0,
Branch: 0,
Index: 4,
@ -130,7 +129,7 @@ var (
pubKey: hexToBytes("020a1290b997c0a234a95213962e7edcb761c7360f0230f698a1a3e71c37047bb0"),
privKey: hexToBytes("fe4f855fcf059ec6ddf7b25f63b19aa49c771d1fcb9850b68ae3d65e20657a60"),
privKeyWIF: "L5k4HivqXvohxBMpuwD38iUgi6uewffwZny91ZNYfM39RXH2x3QR",
derivationInfo: waddrmgr.DerivationPath{
derivationInfo: DerivationPath{
Account: 0,
Branch: 1,
Index: 0,
@ -145,7 +144,7 @@ var (
pubKey: hexToBytes("03f79bbde32af42dde98195f011d95982602fcd0dab657fe4a1f49f9d5ada1e02d"),
privKey: hexToBytes("bfef521317c65b018ae7e6d7ecc3aa700d5d0f7ea84d567be9270382d0b5e3e6"),
privKeyWIF: "L3eomUajnTDM3Pc8GU47qqXUFuCjvpqY7NYN9mH3x1ZFjDgiY4BU",
derivationInfo: waddrmgr.DerivationPath{
derivationInfo: DerivationPath{
Account: 0,
Branch: 1,
Index: 1,
@ -160,7 +159,7 @@ var (
pubKey: hexToBytes("023ded84afe4fe91b52b45c3deb26fd263f749cbc27747dc964dae9e0739cbc579"),
privKey: hexToBytes("f506dffd4494c24006df7a35f3291f7ca0297a1a431557a1339bfed6f48738ca"),
privKeyWIF: "L5S1bVQUPqQb1Su82fLoSpnGCjcPfdAQE1pJxWRopJSBdYNDHESv",
derivationInfo: waddrmgr.DerivationPath{
derivationInfo: DerivationPath{
Account: 0,
Branch: 1,
Index: 2,
@ -175,7 +174,7 @@ var (
pubKey: hexToBytes("030f1e79f06824e10a259914ec310528bb2d5b8d6356341fe9dff55498591af6af"),
privKey: hexToBytes("b3629de8ef6a275b4ffae41aa2bbbc2952eb92282ea6402435abbb010ecc1fb8"),
privKeyWIF: "L3EQsGeEnyXmKaux54cG4DQeCSQDvGuvEuy3W2ss4geum7AtWaHw",
derivationInfo: waddrmgr.DerivationPath{
derivationInfo: DerivationPath{
Account: 0,
Branch: 1,
Index: 3,
@ -190,7 +189,7 @@ var (
pubKey: hexToBytes("0317d7182e26b6ca3e0f3db531c474b9cab7a763a75eabff2e14ac92f62a793238"),
privKey: hexToBytes("ca747a7ef815ea0dbe68655272cecbfbd65f2a109019a9ed28e0d3dcaffe05c3"),
privKeyWIF: "L41Frac75RPbTELKzw1EGC2qCkdveiVumpmsyX4daAvyyCMxit1W",
derivationInfo: waddrmgr.DerivationPath{
derivationInfo: DerivationPath{
Account: 0,
Branch: 1,
Index: 4,
@ -209,11 +208,13 @@ var (
// checkManagerError ensures the passed error is a ManagerError with an error
// code that matches the passed error code.
func checkManagerError(t *testing.T, testName string, gotErr error, wantErrCode waddrmgr.ErrorCode) bool {
merr, ok := gotErr.(waddrmgr.ManagerError)
func checkManagerError(t *testing.T, testName string, gotErr error,
wantErrCode ErrorCode) bool {
merr, ok := gotErr.(ManagerError)
if !ok {
t.Errorf("%s: unexpected error type - got %T, want %T",
testName, gotErr, waddrmgr.ManagerError{})
testName, gotErr, ManagerError{})
return false
}
if merr.ErrorCode != wantErrCode {
@ -255,7 +256,7 @@ func emptyDB(t *testing.T) (tearDownFunc func(), db walletdb.DB) {
// setupManager creates a new address manager and returns a teardown function
// that should be invoked to ensure it is closed and removed upon completion.
func setupManager(t *testing.T) (tearDownFunc func(), db walletdb.DB, mgr *waddrmgr.Manager) {
func setupManager(t *testing.T) (tearDownFunc func(), db walletdb.DB, mgr *Manager) {
// Create a new manager in a temp directory.
dirName, err := ioutil.TempDir("", "mgrtest")
if err != nil {
@ -272,14 +273,14 @@ func setupManager(t *testing.T) (tearDownFunc func(), db walletdb.DB, mgr *waddr
if err != nil {
return err
}
err = waddrmgr.Create(
err = Create(
ns, seed, pubPassphrase, privPassphrase,
&chaincfg.MainNetParams, fastScrypt, time.Time{},
)
if err != nil {
return err
}
mgr, err = waddrmgr.Open(ns, pubPassphrase, &chaincfg.MainNetParams)
mgr, err = Open(ns, pubPassphrase, &chaincfg.MainNetParams)
return err
})
if err != nil {

View file

@ -1,66 +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"
)
// TstLatestMgrVersion makes the unexported latestMgrVersion variable available
// for change when the tests are run.
var TstLatestMgrVersion = &latestMgrVersion
// 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

File diff suppressed because it is too large Load diff