internal_test.go raw

   1  /*
   2  This test file is part of the waddrmgr package rather than than the
   3  waddrmgr_test package so it can bridge access to the internals to properly test
   4  cases which are either not possible or can't reliably be tested via the public
   5  interface. The functions are only exported while the tests are being run.
   6  */
   7  package waddrmgr
   8  
   9  import (
  10  	"errors"
  11  	
  12  	"github.com/p9c/p9/pkg/snacl"
  13  )
  14  
  15  // // TstLatestMgrVersion makes the unexported latestMgrVersion variable
  16  // available // for change when the tests are run. var TstLatestMgrVersion =
  17  // &latestMgrVersion
  18  
  19  // TstCheckPublicPassphrase returns true if the provided public passphrase is correct for the manager.
  20  func (m *Manager) TstCheckPublicPassphrase(pubPassphrase []byte) bool {
  21  	secretKey := snacl.SecretKey{Key: &snacl.CryptoKey{}}
  22  	secretKey.Parameters = m.masterKeyPub.Parameters
  23  	e := secretKey.DeriveKey(&pubPassphrase)
  24  	return e == nil
  25  }
  26  
  27  // failingCryptoKey is an implementation of the EncryptorDecryptor interface
  28  // with intentionally fails when attempting to encrypt or decrypt with it.
  29  type failingCryptoKey struct {
  30  	cryptoKey
  31  }
  32  
  33  // Encrypt intenionally returns a failure when invoked to test error paths.
  34  //
  35  // This is part of the EncryptorDecryptor interface implementation.
  36  func (c *failingCryptoKey) Encrypt(in []byte) ([]byte, error) {
  37  	return nil, errors.New("failed to encrypt")
  38  }
  39  
  40  // Decrypt intenionally returns a failure when invoked to test error paths.
  41  //
  42  // This is part of the EncryptorDecryptor interface implementation.
  43  func (c *failingCryptoKey) Decrypt(in []byte) ([]byte, error) {
  44  	return nil, errors.New("failed to decrypt")
  45  }
  46  
  47  // TstRunWithFailingCryptoKeyPriv runs the provided callback with the private
  48  // crypto key replaced with a version that fails to help test error paths.
  49  func TstRunWithFailingCryptoKeyPriv(m *Manager, callback func()) {
  50  	orig := m.cryptoKeyPriv
  51  	defer func() {
  52  		m.cryptoKeyPriv = orig
  53  	}()
  54  	m.cryptoKeyPriv = &failingCryptoKey{}
  55  	callback()
  56  }
  57  
  58  // TstDefaultAccountName is the constant defaultAccountName exported for tests.
  59  const TstDefaultAccountName = defaultAccountName
  60