mirror of
https://github.com/lightningnetwork/lnd.git
synced 2025-03-04 01:36:24 +01:00
walletunlocker: use T.TempDir
to create temporary test directory
Signed-off-by: Eng Zer Jun <engzerjun@gmail.com>
This commit is contained in:
parent
ca1cc68b79
commit
05c0dd7c91
1 changed files with 18 additions and 53 deletions
|
@ -4,7 +4,6 @@ import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io/ioutil"
|
|
||||||
"os"
|
"os"
|
||||||
"path"
|
"path"
|
||||||
"testing"
|
"testing"
|
||||||
|
@ -148,11 +147,7 @@ func TestGenSeed(t *testing.T) {
|
||||||
|
|
||||||
// First, we'll create a new test directory and unlocker service for
|
// First, we'll create a new test directory and unlocker service for
|
||||||
// that directory.
|
// that directory.
|
||||||
testDir, err := ioutil.TempDir("", "testcreate")
|
testDir := t.TempDir()
|
||||||
require.NoError(t, err)
|
|
||||||
defer func() {
|
|
||||||
_ = os.RemoveAll(testDir)
|
|
||||||
}()
|
|
||||||
|
|
||||||
service := walletunlocker.New(
|
service := walletunlocker.New(
|
||||||
testNetParams, nil, false, testLoaderOpts(testDir),
|
testNetParams, nil, false, testLoaderOpts(testDir),
|
||||||
|
@ -186,11 +181,7 @@ func TestGenSeedGenerateEntropy(t *testing.T) {
|
||||||
|
|
||||||
// First, we'll create a new test directory and unlocker service for
|
// First, we'll create a new test directory and unlocker service for
|
||||||
// that directory.
|
// that directory.
|
||||||
testDir, err := ioutil.TempDir("", "testcreate")
|
testDir := t.TempDir()
|
||||||
require.NoError(t, err)
|
|
||||||
defer func() {
|
|
||||||
_ = os.RemoveAll(testDir)
|
|
||||||
}()
|
|
||||||
service := walletunlocker.New(
|
service := walletunlocker.New(
|
||||||
testNetParams, nil, false, testLoaderOpts(testDir),
|
testNetParams, nil, false, testLoaderOpts(testDir),
|
||||||
)
|
)
|
||||||
|
@ -222,11 +213,7 @@ func TestGenSeedInvalidEntropy(t *testing.T) {
|
||||||
|
|
||||||
// First, we'll create a new test directory and unlocker service for
|
// First, we'll create a new test directory and unlocker service for
|
||||||
// that directory.
|
// that directory.
|
||||||
testDir, err := ioutil.TempDir("", "testcreate")
|
testDir := t.TempDir()
|
||||||
require.NoError(t, err)
|
|
||||||
defer func() {
|
|
||||||
_ = os.RemoveAll(testDir)
|
|
||||||
}()
|
|
||||||
service := walletunlocker.New(
|
service := walletunlocker.New(
|
||||||
testNetParams, nil, false, testLoaderOpts(testDir),
|
testNetParams, nil, false, testLoaderOpts(testDir),
|
||||||
)
|
)
|
||||||
|
@ -242,7 +229,7 @@ func TestGenSeedInvalidEntropy(t *testing.T) {
|
||||||
|
|
||||||
// We should get an error now since the entropy source was invalid.
|
// We should get an error now since the entropy source was invalid.
|
||||||
ctx := context.Background()
|
ctx := context.Background()
|
||||||
_, err = service.GenSeed(ctx, genSeedReq)
|
_, err := service.GenSeed(ctx, genSeedReq)
|
||||||
require.Error(t, err)
|
require.Error(t, err)
|
||||||
require.Contains(t, err.Error(), "incorrect entropy length")
|
require.Contains(t, err.Error(), "incorrect entropy length")
|
||||||
}
|
}
|
||||||
|
@ -253,11 +240,7 @@ func TestInitWallet(t *testing.T) {
|
||||||
t.Parallel()
|
t.Parallel()
|
||||||
|
|
||||||
// testDir is empty, meaning wallet was not created from before.
|
// testDir is empty, meaning wallet was not created from before.
|
||||||
testDir, err := ioutil.TempDir("", "testcreate")
|
testDir := t.TempDir()
|
||||||
require.NoError(t, err)
|
|
||||||
defer func() {
|
|
||||||
_ = os.RemoveAll(testDir)
|
|
||||||
}()
|
|
||||||
|
|
||||||
// Create new UnlockerService.
|
// Create new UnlockerService.
|
||||||
service := walletunlocker.New(
|
service := walletunlocker.New(
|
||||||
|
@ -326,7 +309,7 @@ func TestInitWallet(t *testing.T) {
|
||||||
|
|
||||||
// Now calling InitWallet should fail, since a wallet already exists in
|
// Now calling InitWallet should fail, since a wallet already exists in
|
||||||
// the directory.
|
// the directory.
|
||||||
_, err = service.InitWallet(ctx, req)
|
_, err := service.InitWallet(ctx, req)
|
||||||
require.Error(t, err)
|
require.Error(t, err)
|
||||||
|
|
||||||
// Similarly, if we try to do GenSeed again, we should get an error as
|
// Similarly, if we try to do GenSeed again, we should get an error as
|
||||||
|
@ -341,11 +324,7 @@ func TestCreateWalletInvalidEntropy(t *testing.T) {
|
||||||
t.Parallel()
|
t.Parallel()
|
||||||
|
|
||||||
// testDir is empty, meaning wallet was not created from before.
|
// testDir is empty, meaning wallet was not created from before.
|
||||||
testDir, err := ioutil.TempDir("", "testcreate")
|
testDir := t.TempDir()
|
||||||
require.NoError(t, err)
|
|
||||||
defer func() {
|
|
||||||
_ = os.RemoveAll(testDir)
|
|
||||||
}()
|
|
||||||
|
|
||||||
// Create new UnlockerService.
|
// Create new UnlockerService.
|
||||||
service := walletunlocker.New(
|
service := walletunlocker.New(
|
||||||
|
@ -361,7 +340,7 @@ func TestCreateWalletInvalidEntropy(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
ctx := context.Background()
|
ctx := context.Background()
|
||||||
_, err = service.InitWallet(ctx, req)
|
_, err := service.InitWallet(ctx, req)
|
||||||
require.Error(t, err)
|
require.Error(t, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -372,11 +351,7 @@ func TestUnlockWallet(t *testing.T) {
|
||||||
t.Parallel()
|
t.Parallel()
|
||||||
|
|
||||||
// testDir is empty, meaning wallet was not created from before.
|
// testDir is empty, meaning wallet was not created from before.
|
||||||
testDir, err := ioutil.TempDir("", "testunlock")
|
testDir := t.TempDir()
|
||||||
require.NoError(t, err)
|
|
||||||
defer func() {
|
|
||||||
_ = os.RemoveAll(testDir)
|
|
||||||
}()
|
|
||||||
|
|
||||||
// Create new UnlockerService that'll also drop the wallet's history on
|
// Create new UnlockerService that'll also drop the wallet's history on
|
||||||
// unlock.
|
// unlock.
|
||||||
|
@ -392,7 +367,7 @@ func TestUnlockWallet(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Should fail to unlock non-existing wallet.
|
// Should fail to unlock non-existing wallet.
|
||||||
_, err = service.UnlockWallet(ctx, req)
|
_, err := service.UnlockWallet(ctx, req)
|
||||||
require.Error(t, err)
|
require.Error(t, err)
|
||||||
|
|
||||||
// Create a wallet we can try to unlock.
|
// Create a wallet we can try to unlock.
|
||||||
|
@ -442,11 +417,7 @@ func TestChangeWalletPasswordNewRootkey(t *testing.T) {
|
||||||
t.Parallel()
|
t.Parallel()
|
||||||
|
|
||||||
// testDir is empty, meaning wallet was not created from before.
|
// testDir is empty, meaning wallet was not created from before.
|
||||||
testDir, err := ioutil.TempDir("", "testchangepassword")
|
testDir := t.TempDir()
|
||||||
require.NoError(t, err)
|
|
||||||
defer func() {
|
|
||||||
_ = os.RemoveAll(testDir)
|
|
||||||
}()
|
|
||||||
|
|
||||||
// Changing the password of the wallet will also try to change the
|
// Changing the password of the wallet will also try to change the
|
||||||
// password of the macaroon DB. We create a default DB here but close it
|
// password of the macaroon DB. We create a default DB here but close it
|
||||||
|
@ -462,7 +433,7 @@ func TestChangeWalletPasswordNewRootkey(t *testing.T) {
|
||||||
// requested.
|
// requested.
|
||||||
var tempFiles []string
|
var tempFiles []string
|
||||||
for i := 0; i < 3; i++ {
|
for i := 0; i < 3; i++ {
|
||||||
file, err := ioutil.TempFile(testDir, "")
|
file, err := os.CreateTemp(testDir, "")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("unable to create temp file: %v", err)
|
t.Fatalf("unable to create temp file: %v", err)
|
||||||
}
|
}
|
||||||
|
@ -539,7 +510,9 @@ func TestChangeWalletPasswordNewRootkey(t *testing.T) {
|
||||||
|
|
||||||
// The files should no longer exist.
|
// The files should no longer exist.
|
||||||
for _, tempFile := range tempFiles {
|
for _, tempFile := range tempFiles {
|
||||||
if _, err := os.Open(tempFile); err == nil {
|
f, err := os.Open(tempFile)
|
||||||
|
if err == nil {
|
||||||
|
_ = f.Close()
|
||||||
t.Fatal("file exists but it shouldn't")
|
t.Fatal("file exists but it shouldn't")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -553,11 +526,7 @@ func TestChangeWalletPasswordStateless(t *testing.T) {
|
||||||
t.Parallel()
|
t.Parallel()
|
||||||
|
|
||||||
// testDir is empty, meaning wallet was not created from before.
|
// testDir is empty, meaning wallet was not created from before.
|
||||||
testDir, err := ioutil.TempDir("", "testchangepasswordstateless")
|
testDir := t.TempDir()
|
||||||
require.NoError(t, err)
|
|
||||||
defer func() {
|
|
||||||
_ = os.RemoveAll(testDir)
|
|
||||||
}()
|
|
||||||
|
|
||||||
// Changing the password of the wallet will also try to change the
|
// Changing the password of the wallet will also try to change the
|
||||||
// password of the macaroon DB. We create a default DB here but close it
|
// password of the macaroon DB. We create a default DB here but close it
|
||||||
|
@ -570,7 +539,7 @@ func TestChangeWalletPasswordStateless(t *testing.T) {
|
||||||
|
|
||||||
// Create a temp file that will act as the macaroon DB file that will
|
// Create a temp file that will act as the macaroon DB file that will
|
||||||
// be deleted by changing the password.
|
// be deleted by changing the password.
|
||||||
tmpFile, err := ioutil.TempFile(testDir, "")
|
tmpFile, err := os.CreateTemp(testDir, "")
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
tempMacFile := tmpFile.Name()
|
tempMacFile := tmpFile.Name()
|
||||||
err = tmpFile.Close()
|
err = tmpFile.Close()
|
||||||
|
@ -636,6 +605,7 @@ func TestChangeWalletPasswordStateless(t *testing.T) {
|
||||||
// Send a fake macaroon that should be returned in the response
|
// Send a fake macaroon that should be returned in the response
|
||||||
// in the async code above.
|
// in the async code above.
|
||||||
service.MacResponseChan <- testMac
|
service.MacResponseChan <- testMac
|
||||||
|
require.NoError(t, unlockMsg.UnloadWallet())
|
||||||
|
|
||||||
case <-time.After(defaultTestTimeout):
|
case <-time.After(defaultTestTimeout):
|
||||||
t.Fatalf("password not received")
|
t.Fatalf("password not received")
|
||||||
|
@ -683,9 +653,4 @@ func doChangePassword(service *walletunlocker.UnlockerService, testDir string,
|
||||||
errChan <- fmt.Errorf("could not close store: %v", err)
|
errChan <- fmt.Errorf("could not close store: %v", err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
err = os.RemoveAll(testDir)
|
|
||||||
if err != nil {
|
|
||||||
errChan <- err
|
|
||||||
return
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue