mirror of
https://github.com/lightningnetwork/lnd.git
synced 2025-03-04 01:36:24 +01:00
keychain: use T.TempDir
to create temporary test directory
Signed-off-by: Eng Zer Jun <engzerjun@gmail.com>
This commit is contained in:
parent
6c4ce69b26
commit
d926ad1f84
2 changed files with 35 additions and 60 deletions
|
@ -8,15 +8,11 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
func BenchmarkDerivePrivKey(t *testing.B) {
|
func BenchmarkDerivePrivKey(t *testing.B) {
|
||||||
cleanUp, wallet, err := createTestBtcWallet(
|
wallet, err := createTestBtcWallet(t, CoinTypeBitcoin)
|
||||||
CoinTypeBitcoin,
|
|
||||||
)
|
|
||||||
require.NoError(t, err, "unable to create wallet")
|
require.NoError(t, err, "unable to create wallet")
|
||||||
|
|
||||||
keyRing := NewBtcWalletKeyRing(wallet, CoinTypeBitcoin)
|
keyRing := NewBtcWalletKeyRing(wallet, CoinTypeBitcoin)
|
||||||
|
|
||||||
defer cleanUp()
|
|
||||||
|
|
||||||
var (
|
var (
|
||||||
privKey *btcec.PrivateKey
|
privKey *btcec.PrivateKey
|
||||||
)
|
)
|
||||||
|
|
|
@ -2,9 +2,7 @@ package keychain
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"io/ioutil"
|
|
||||||
"math/rand"
|
"math/rand"
|
||||||
"os"
|
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
@ -32,7 +30,7 @@ var (
|
||||||
testDBTimeout = time.Second * 10
|
testDBTimeout = time.Second * 10
|
||||||
)
|
)
|
||||||
|
|
||||||
func createTestBtcWallet(coinType uint32) (func(), *wallet.Wallet, error) {
|
func createTestBtcWallet(t testing.TB, coinType uint32) (*wallet.Wallet, error) {
|
||||||
// Instruct waddrmgr to use the cranked down scrypt parameters when
|
// Instruct waddrmgr to use the cranked down scrypt parameters when
|
||||||
// creating new wallet encryption keys.
|
// creating new wallet encryption keys.
|
||||||
fastScrypt := waddrmgr.FastScryptOptions
|
fastScrypt := waddrmgr.FastScryptOptions
|
||||||
|
@ -46,12 +44,8 @@ func createTestBtcWallet(coinType uint32) (func(), *wallet.Wallet, error) {
|
||||||
waddrmgr.SetSecretKeyGen(keyGen)
|
waddrmgr.SetSecretKeyGen(keyGen)
|
||||||
|
|
||||||
// Create a new test wallet that uses fast scrypt as KDF.
|
// Create a new test wallet that uses fast scrypt as KDF.
|
||||||
tempDir, err := ioutil.TempDir("", "keyring-lnwallet")
|
|
||||||
if err != nil {
|
|
||||||
return nil, nil, err
|
|
||||||
}
|
|
||||||
loader := wallet.NewLoader(
|
loader := wallet.NewLoader(
|
||||||
&chaincfg.SimNetParams, tempDir, true, testDBTimeout, 0,
|
&chaincfg.SimNetParams, t.TempDir(), true, testDBTimeout, 0,
|
||||||
)
|
)
|
||||||
|
|
||||||
pass := []byte("test")
|
pass := []byte("test")
|
||||||
|
@ -60,11 +54,11 @@ func createTestBtcWallet(coinType uint32) (func(), *wallet.Wallet, error) {
|
||||||
pass, pass, testHDSeed[:], time.Time{},
|
pass, pass, testHDSeed[:], time.Time{},
|
||||||
)
|
)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := baseWallet.Unlock(pass, nil); err != nil {
|
if err := baseWallet.Unlock(pass, nil); err != nil {
|
||||||
return nil, nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
// Construct the key scope required to derive keys for the chose
|
// Construct the key scope required to derive keys for the chose
|
||||||
|
@ -88,16 +82,15 @@ func createTestBtcWallet(coinType uint32) (func(), *wallet.Wallet, error) {
|
||||||
return err
|
return err
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
cleanUp := func() {
|
t.Cleanup(func() {
|
||||||
baseWallet.Lock()
|
baseWallet.Lock()
|
||||||
os.RemoveAll(tempDir)
|
})
|
||||||
}
|
|
||||||
|
|
||||||
return cleanUp, baseWallet, nil
|
return baseWallet, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func assertEqualKeyLocator(t *testing.T, a, b KeyLocator) {
|
func assertEqualKeyLocator(t *testing.T, a, b KeyLocator) {
|
||||||
|
@ -110,9 +103,9 @@ func assertEqualKeyLocator(t *testing.T, a, b KeyLocator) {
|
||||||
|
|
||||||
// secretKeyRingConstructor is a function signature that's used as a generic
|
// secretKeyRingConstructor is a function signature that's used as a generic
|
||||||
// constructor for various implementations of the KeyRing interface. A string
|
// constructor for various implementations of the KeyRing interface. A string
|
||||||
// naming the returned interface, a function closure that cleans up any
|
// naming the returned interface, and the KeyRing interface itself are to be
|
||||||
// resources, and the clean up interface itself are to be returned.
|
// returned.
|
||||||
type keyRingConstructor func() (string, func(), KeyRing, error)
|
type keyRingConstructor func() (string, KeyRing, error)
|
||||||
|
|
||||||
// TestKeyRingDerivation tests that each known KeyRing implementation properly
|
// TestKeyRingDerivation tests that each known KeyRing implementation properly
|
||||||
// adheres to the expected behavior of the set of interfaces.
|
// adheres to the expected behavior of the set of interfaces.
|
||||||
|
@ -120,35 +113,29 @@ func TestKeyRingDerivation(t *testing.T) {
|
||||||
t.Parallel()
|
t.Parallel()
|
||||||
|
|
||||||
keyRingImplementations := []keyRingConstructor{
|
keyRingImplementations := []keyRingConstructor{
|
||||||
func() (string, func(), KeyRing, error) {
|
func() (string, KeyRing, error) {
|
||||||
cleanUp, wallet, err := createTestBtcWallet(
|
wallet, err := createTestBtcWallet(t, CoinTypeBitcoin)
|
||||||
CoinTypeBitcoin,
|
|
||||||
)
|
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
|
||||||
keyRing := NewBtcWalletKeyRing(wallet, CoinTypeBitcoin)
|
keyRing := NewBtcWalletKeyRing(wallet, CoinTypeBitcoin)
|
||||||
|
|
||||||
return "btcwallet", cleanUp, keyRing, nil
|
return "btcwallet", keyRing, nil
|
||||||
},
|
},
|
||||||
func() (string, func(), KeyRing, error) {
|
func() (string, KeyRing, error) {
|
||||||
cleanUp, wallet, err := createTestBtcWallet(
|
wallet, err := createTestBtcWallet(t, CoinTypeLitecoin)
|
||||||
CoinTypeLitecoin,
|
|
||||||
)
|
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
|
||||||
keyRing := NewBtcWalletKeyRing(wallet, CoinTypeLitecoin)
|
keyRing := NewBtcWalletKeyRing(wallet, CoinTypeLitecoin)
|
||||||
|
|
||||||
return "ltcwallet", cleanUp, keyRing, nil
|
return "ltcwallet", keyRing, nil
|
||||||
},
|
},
|
||||||
func() (string, func(), KeyRing, error) {
|
func() (string, KeyRing, error) {
|
||||||
cleanUp, wallet, err := createTestBtcWallet(
|
wallet, err := createTestBtcWallet(t, CoinTypeTestnet)
|
||||||
CoinTypeTestnet,
|
|
||||||
)
|
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
|
||||||
keyRing := NewBtcWalletKeyRing(wallet, CoinTypeTestnet)
|
keyRing := NewBtcWalletKeyRing(wallet, CoinTypeTestnet)
|
||||||
|
|
||||||
return "testwallet", cleanUp, keyRing, nil
|
return "testwallet", keyRing, nil
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -158,12 +145,11 @@ func TestKeyRingDerivation(t *testing.T) {
|
||||||
// an identical set of tests in order to ensure that the interface
|
// an identical set of tests in order to ensure that the interface
|
||||||
// adheres to our nominal specification.
|
// adheres to our nominal specification.
|
||||||
for _, keyRingConstructor := range keyRingImplementations {
|
for _, keyRingConstructor := range keyRingImplementations {
|
||||||
keyRingName, cleanUp, keyRing, err := keyRingConstructor()
|
keyRingName, keyRing, err := keyRingConstructor()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("unable to create key ring %v: %v", keyRingName,
|
t.Fatalf("unable to create key ring %v: %v", keyRingName,
|
||||||
err)
|
err)
|
||||||
}
|
}
|
||||||
defer cleanUp()
|
|
||||||
|
|
||||||
success := t.Run(fmt.Sprintf("%v", keyRingName), func(t *testing.T) {
|
success := t.Run(fmt.Sprintf("%v", keyRingName), func(t *testing.T) {
|
||||||
// First, we'll ensure that we're able to derive keys
|
// First, we'll ensure that we're able to derive keys
|
||||||
|
@ -244,9 +230,9 @@ func TestKeyRingDerivation(t *testing.T) {
|
||||||
|
|
||||||
// secretKeyRingConstructor is a function signature that's used as a generic
|
// secretKeyRingConstructor is a function signature that's used as a generic
|
||||||
// constructor for various implementations of the SecretKeyRing interface. A
|
// constructor for various implementations of the SecretKeyRing interface. A
|
||||||
// string naming the returned interface, a function closure that cleans up any
|
// string naming the returned interface, and the SecretKeyRing interface itself
|
||||||
// resources, and the clean up interface itself are to be returned.
|
// are to be returned.
|
||||||
type secretKeyRingConstructor func() (string, func(), SecretKeyRing, error)
|
type secretKeyRingConstructor func() (string, SecretKeyRing, error)
|
||||||
|
|
||||||
// TestSecretKeyRingDerivation tests that each known SecretKeyRing
|
// TestSecretKeyRingDerivation tests that each known SecretKeyRing
|
||||||
// implementation properly adheres to the expected behavior of the set of
|
// implementation properly adheres to the expected behavior of the set of
|
||||||
|
@ -255,35 +241,29 @@ func TestSecretKeyRingDerivation(t *testing.T) {
|
||||||
t.Parallel()
|
t.Parallel()
|
||||||
|
|
||||||
secretKeyRingImplementations := []secretKeyRingConstructor{
|
secretKeyRingImplementations := []secretKeyRingConstructor{
|
||||||
func() (string, func(), SecretKeyRing, error) {
|
func() (string, SecretKeyRing, error) {
|
||||||
cleanUp, wallet, err := createTestBtcWallet(
|
wallet, err := createTestBtcWallet(t, CoinTypeBitcoin)
|
||||||
CoinTypeBitcoin,
|
|
||||||
)
|
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
|
||||||
keyRing := NewBtcWalletKeyRing(wallet, CoinTypeBitcoin)
|
keyRing := NewBtcWalletKeyRing(wallet, CoinTypeBitcoin)
|
||||||
|
|
||||||
return "btcwallet", cleanUp, keyRing, nil
|
return "btcwallet", keyRing, nil
|
||||||
},
|
},
|
||||||
func() (string, func(), SecretKeyRing, error) {
|
func() (string, SecretKeyRing, error) {
|
||||||
cleanUp, wallet, err := createTestBtcWallet(
|
wallet, err := createTestBtcWallet(t, CoinTypeLitecoin)
|
||||||
CoinTypeLitecoin,
|
|
||||||
)
|
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
|
||||||
keyRing := NewBtcWalletKeyRing(wallet, CoinTypeLitecoin)
|
keyRing := NewBtcWalletKeyRing(wallet, CoinTypeLitecoin)
|
||||||
|
|
||||||
return "ltcwallet", cleanUp, keyRing, nil
|
return "ltcwallet", keyRing, nil
|
||||||
},
|
},
|
||||||
func() (string, func(), SecretKeyRing, error) {
|
func() (string, SecretKeyRing, error) {
|
||||||
cleanUp, wallet, err := createTestBtcWallet(
|
wallet, err := createTestBtcWallet(t, CoinTypeTestnet)
|
||||||
CoinTypeTestnet,
|
|
||||||
)
|
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
|
||||||
keyRing := NewBtcWalletKeyRing(wallet, CoinTypeTestnet)
|
keyRing := NewBtcWalletKeyRing(wallet, CoinTypeTestnet)
|
||||||
|
|
||||||
return "testwallet", cleanUp, keyRing, nil
|
return "testwallet", keyRing, nil
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -291,12 +271,11 @@ func TestSecretKeyRingDerivation(t *testing.T) {
|
||||||
// an identical set of tests in order to ensure that the interface
|
// an identical set of tests in order to ensure that the interface
|
||||||
// adheres to our nominal specification.
|
// adheres to our nominal specification.
|
||||||
for _, secretKeyRingConstructor := range secretKeyRingImplementations {
|
for _, secretKeyRingConstructor := range secretKeyRingImplementations {
|
||||||
keyRingName, cleanUp, secretKeyRing, err := secretKeyRingConstructor()
|
keyRingName, secretKeyRing, err := secretKeyRingConstructor()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("unable to create secret key ring %v: %v",
|
t.Fatalf("unable to create secret key ring %v: %v",
|
||||||
keyRingName, err)
|
keyRingName, err)
|
||||||
}
|
}
|
||||||
defer cleanUp()
|
|
||||||
|
|
||||||
success := t.Run(fmt.Sprintf("%v", keyRingName), func(t *testing.T) {
|
success := t.Run(fmt.Sprintf("%v", keyRingName), func(t *testing.T) {
|
||||||
// For, each key family, we'll ensure that we're able
|
// For, each key family, we'll ensure that we're able
|
||||||
|
|
Loading…
Add table
Reference in a new issue