Cleanup recent encrypt/decrypt test additions.
This commit cleans up the recent test addition for testing the positive and negative error paths of the Encrypt and Decrypt functions. In particular: - Add comments to all new functions - Close the manager before trying to delete the file which is otherwise in use - Rename the temp prefix since these are not pool tests - Rename setUp to setupManager to make it a bit more explicit what it's doing
This commit is contained in:
parent
b4214fc93c
commit
3cd0aa011d
2 changed files with 24 additions and 6 deletions
|
@ -56,18 +56,29 @@ func (m *Manager) TstCheckPublicPassphrase(pubPassphrase []byte) bool {
|
|||
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() {
|
||||
|
|
|
@ -1510,30 +1510,35 @@ func TestManager(t *testing.T) {
|
|||
}
|
||||
}
|
||||
|
||||
func setUp(t *testing.T) (tearDownFunc func(), mgr *waddrmgr.Manager) {
|
||||
// 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(), mgr *waddrmgr.Manager) {
|
||||
t.Parallel()
|
||||
|
||||
// Create a new manager.
|
||||
// We create the file and immediately delete it, as the waddrmgr
|
||||
// needs to be doing the creating.
|
||||
file, err := ioutil.TempDir("", "pool_test")
|
||||
file, err := ioutil.TempDir("", "mgrtest")
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to create db file: %v", err)
|
||||
}
|
||||
os.Remove(file)
|
||||
_ = os.Remove(file)
|
||||
mgr, err = waddrmgr.Create(file, seed, pubPassphrase, privPassphrase,
|
||||
&btcnet.MainNetParams, fastScrypt)
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to create Manager: %v", err)
|
||||
}
|
||||
tearDownFunc = func() {
|
||||
os.Remove(file)
|
||||
mgr.Close()
|
||||
os.Remove(file)
|
||||
}
|
||||
return tearDownFunc, mgr
|
||||
}
|
||||
|
||||
// TestEncryptDecryptErrors ensures that errors which occur while encrypting and
|
||||
// decrypting data return the expected errors.
|
||||
func TestEncryptDecryptErrors(t *testing.T) {
|
||||
teardown, mgr := setUp(t)
|
||||
teardown, mgr := setupManager(t)
|
||||
defer teardown()
|
||||
|
||||
invalidKeyType := waddrmgr.CryptoKeyType(0xff)
|
||||
|
@ -1578,8 +1583,10 @@ func TestEncryptDecryptErrors(t *testing.T) {
|
|||
checkManagerError(t, "failed decryption", err, waddrmgr.ErrCrypto)
|
||||
}
|
||||
|
||||
// TestEncryptDecrypt ensures that encrypting and decrypting data with the
|
||||
// the various crypto key types works as expected.
|
||||
func TestEncryptDecrypt(t *testing.T) {
|
||||
teardown, mgr := setUp(t)
|
||||
teardown, mgr := setupManager(t)
|
||||
defer teardown()
|
||||
|
||||
plainText := []byte("this is a plaintext")
|
||||
|
|
Loading…
Add table
Reference in a new issue