decred.org/dcrwallet/v3@v3.1.0/wallet/udb/internal_test.go (about)

     1  // Copyright (c) 2014 The btcsuite developers
     2  // Copyright (c) 2015 The Decred developers
     3  // Use of this source code is governed by an ISC
     4  // license that can be found in the LICENSE file.
     5  
     6  /*
     7  This test file is part of the waddrmgr package rather than than the
     8  waddrmgr_test package so it can bridge access to the internals to properly test
     9  cases which are either not possible or can't reliably be tested via the public
    10  interface. The functions are only exported while the tests are being run.
    11  */
    12  
    13  package udb
    14  
    15  import (
    16  	"decred.org/dcrwallet/v3/errors"
    17  	"decred.org/dcrwallet/v3/wallet/internal/snacl"
    18  )
    19  
    20  // TstLatestMgrVersion makes the unexported latestMgrVersion variable available
    21  // for change when the tests are run.
    22  var TstLatestMgrVersion = &latestMgrVersion
    23  
    24  // Replace the Manager.newSecretKey function with the given one and calls
    25  // the callback function. Afterwards the original newSecretKey
    26  // function will be restored.
    27  func TstRunWithReplacedNewSecretKey(callback func()) {
    28  	orig := newSecretKey
    29  	defer func() {
    30  		newSecretKey = orig
    31  	}()
    32  	newSecretKey = func(passphrase *[]byte, config *scryptOptions) (*snacl.SecretKey, error) {
    33  		return nil, errors.E(errors.Crypto)
    34  	}
    35  	callback()
    36  }
    37  
    38  // TstCheckPublicPassphrase returns true if the provided public passphrase is
    39  // correct for the manager.
    40  func (m *Manager) TstCheckPublicPassphrase(pubPassphrase []byte) bool {
    41  	secretKey := snacl.SecretKey{Key: &snacl.CryptoKey{}}
    42  	secretKey.Parameters = m.masterKeyPub.Parameters
    43  	err := secretKey.DeriveKey(&pubPassphrase)
    44  	return err == nil
    45  }
    46  
    47  // failingCryptoKey is an implementation of the EncryptorDecryptor interface
    48  // with intentionally fails when attempting to encrypt or decrypt with it.
    49  type failingCryptoKey struct {
    50  	cryptoKey
    51  }
    52  
    53  // Encrypt intenionally returns a failure when invoked to test error paths.
    54  //
    55  // This is part of the EncryptorDecryptor interface implementation.
    56  func (c *failingCryptoKey) Encrypt(in []byte) ([]byte, error) {
    57  	return nil, errors.New("failed to encrypt")
    58  }
    59  
    60  // Decrypt intenionally returns a failure when invoked to test error paths.
    61  //
    62  // This is part of the EncryptorDecryptor interface implementation.
    63  func (c *failingCryptoKey) Decrypt(in []byte) ([]byte, error) {
    64  	return nil, errors.New("failed to decrypt")
    65  }
    66  
    67  // TstRunWithFailingCryptoKeyPriv runs the provided callback with the
    68  // private crypto key replaced with a version that fails to help test error
    69  // paths.
    70  func TstRunWithFailingCryptoKeyPriv(m *Manager, callback func()) {
    71  	orig := m.cryptoKeyPriv
    72  	defer func() {
    73  		m.cryptoKeyPriv = orig
    74  	}()
    75  	m.cryptoKeyPriv = &failingCryptoKey{}
    76  	callback()
    77  }
    78  
    79  // TstDefaultAccountName is the constant defaultAccountName exported for tests.
    80  const TstDefaultAccountName = defaultAccountName