2015-12-01 19:44:58 +01:00
|
|
|
// 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.
|
2014-08-08 22:43:50 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
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
|
|
|
|
|
2014-11-03 12:52:41 +01:00
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
|
2016-05-05 21:04:36 +02:00
|
|
|
"github.com/roasbeef/btcwallet/snacl"
|
2014-11-03 12:52:41 +01:00
|
|
|
)
|
2014-08-08 22:43:50 +02:00
|
|
|
|
|
|
|
// TstMaxRecentHashes makes the unexported maxRecentHashes constant available
|
|
|
|
// when tests are run.
|
|
|
|
var TstMaxRecentHashes = maxRecentHashes
|
|
|
|
|
2015-03-03 18:51:21 +01:00
|
|
|
// TstLatestMgrVersion makes the unexported latestMgrVersion variable available
|
|
|
|
// for change when the tests are run.
|
|
|
|
var TstLatestMgrVersion = &latestMgrVersion
|
|
|
|
|
2014-10-24 08:56:54 +02:00
|
|
|
// Replace the Manager.newSecretKey function with the given one and calls
|
|
|
|
// the callback function. Afterwards the original newSecretKey
|
|
|
|
// function will be restored.
|
|
|
|
func TstRunWithReplacedNewSecretKey(callback func()) {
|
|
|
|
orig := newSecretKey
|
|
|
|
defer func() {
|
|
|
|
newSecretKey = orig
|
|
|
|
}()
|
2015-05-13 19:11:40 +02:00
|
|
|
newSecretKey = func(passphrase *[]byte, config *ScryptOptions) (*snacl.SecretKey, error) {
|
2014-08-08 22:43:50 +02:00
|
|
|
return nil, snacl.ErrDecryptFailed
|
|
|
|
}
|
2014-10-24 08:56:54 +02:00
|
|
|
callback()
|
2014-08-08 22:43:50 +02:00
|
|
|
}
|
|
|
|
|
2014-06-13 19:14:44 +02:00
|
|
|
// TstCheckPublicPassphrase returns true if the provided public passphrase is
|
2014-08-08 22:43:50 +02:00
|
|
|
// 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
|
|
|
|
}
|
2014-11-03 12:52:41 +01:00
|
|
|
|
2014-11-11 04:37:44 +01:00
|
|
|
// failingCryptoKey is an implementation of the EncryptorDecryptor interface
|
|
|
|
// with intentionally fails when attempting to encrypt or decrypt with it.
|
2014-11-03 12:52:41 +01:00
|
|
|
type failingCryptoKey struct {
|
|
|
|
cryptoKey
|
|
|
|
}
|
|
|
|
|
2014-11-11 04:37:44 +01:00
|
|
|
// Encrypt intenionally returns a failure when invoked to test error paths.
|
|
|
|
//
|
|
|
|
// This is part of the EncryptorDecryptor interface implementation.
|
2014-11-03 12:52:41 +01:00
|
|
|
func (c *failingCryptoKey) Encrypt(in []byte) ([]byte, error) {
|
|
|
|
return nil, errors.New("failed to encrypt")
|
|
|
|
}
|
|
|
|
|
2014-11-11 04:37:44 +01:00
|
|
|
// Decrypt intenionally returns a failure when invoked to test error paths.
|
|
|
|
//
|
|
|
|
// This is part of the EncryptorDecryptor interface implementation.
|
2014-11-03 12:52:41 +01:00
|
|
|
func (c *failingCryptoKey) Decrypt(in []byte) ([]byte, error) {
|
|
|
|
return nil, errors.New("failed to decrypt")
|
|
|
|
}
|
|
|
|
|
2014-11-11 04:37:44 +01:00
|
|
|
// TstRunWithFailingCryptoKeyPriv runs the provided callback with the
|
|
|
|
// private crypto key replaced with a version that fails to help test error
|
|
|
|
// paths.
|
2014-11-03 12:52:41 +01:00
|
|
|
func TstRunWithFailingCryptoKeyPriv(m *Manager, callback func()) {
|
|
|
|
orig := m.cryptoKeyPriv
|
|
|
|
defer func() {
|
|
|
|
m.cryptoKeyPriv = orig
|
|
|
|
}()
|
|
|
|
m.cryptoKeyPriv = &failingCryptoKey{}
|
|
|
|
callback()
|
|
|
|
}
|
2015-04-16 18:54:56 +02:00
|
|
|
|
|
|
|
// TstDefaultAccountName is the constant defaultAccountName exported for tests.
|
|
|
|
const TstDefaultAccountName = defaultAccountName
|