prysm-pulse/validator/accounts/v2/export_test.go
Raul Jordan 4017743f7f
Accounts V2: Refactor Wallet Open / Close and Keymanager Initialization (#6668)
* refactoring create account

* dep

* much easier, create a derived account by simply unlocking wallet

* revert changes to new

* make open wallet smarter and utilize cli ctx

* remove the wallet config

* successfully build

* simplify ctx creation for tests

* tests should pass individually

* tests pass

* fixed up to allow for wallet password file input

* fix broken tests

* formatting

* fmt

* simplify recover

* fixed up tests

* implicit use of default wallet path working

Co-authored-by: prylabs-bulldozer[bot] <58059840+prylabs-bulldozer[bot]@users.noreply.github.com>
2020-07-21 23:49:04 -05:00

106 lines
3.1 KiB
Go

package v2
import (
"context"
"crypto/rand"
"fmt"
"math/big"
"os"
"path"
"path/filepath"
"strings"
"testing"
"github.com/prysmaticlabs/prysm/shared/testutil"
"github.com/prysmaticlabs/prysm/shared/testutil/require"
v2keymanager "github.com/prysmaticlabs/prysm/validator/keymanager/v2"
"github.com/prysmaticlabs/prysm/validator/keymanager/v2/direct"
)
func TestZipAndUnzip(t *testing.T) {
walletDir, passwordsDir := setupWalletAndPasswordsDir(t)
randPath, err := rand.Int(rand.Reader, big.NewInt(1000000))
require.NoError(t, err, "Could not generate random file path")
exportDir := path.Join(testutil.TempDir(), fmt.Sprintf("/%d", randPath), "export")
importDir := path.Join(testutil.TempDir(), fmt.Sprintf("/%d", randPath), "import")
t.Cleanup(func() {
require.NoError(t, os.RemoveAll(exportDir), "Failed to remove directory")
require.NoError(t, os.RemoveAll(importDir), "Failed to remove directory")
})
cliCtx := setupWalletCtx(t, &testWalletConfig{
walletDir: walletDir,
passwordsDir: passwordsDir,
exportDir: exportDir,
keymanagerKind: v2keymanager.Direct,
})
wallet, err := NewWallet(cliCtx)
require.NoError(t, err)
ctx := context.Background()
keymanager, err := direct.NewKeymanager(
ctx,
wallet,
direct.DefaultConfig(),
true, /* skip mnemonic */
)
require.NoError(t, err)
_, err = keymanager.CreateAccount(ctx, password)
require.NoError(t, err)
accounts, err := wallet.AccountNames()
require.NoError(t, err)
if len(accounts) == 0 {
t.Fatal("Expected more accounts, received 0")
}
err = wallet.zipAccounts(accounts, exportDir)
require.NoError(t, err)
if _, err := os.Stat(filepath.Join(exportDir, archiveFilename)); os.IsNotExist(err) {
t.Fatal("Expected file to exist")
}
importedAccounts, err := unzipArchiveToTarget(exportDir, importDir)
require.NoError(t, err)
allAccountsStr := strings.Join(accounts, " ")
for _, importedAccount := range importedAccounts {
if !strings.Contains(allAccountsStr, importedAccount) {
t.Fatalf("Expected %s to be in %s", importedAccount, allAccountsStr)
}
}
}
func TestExport_Noninteractive(t *testing.T) {
walletDir, passwordsDir := setupWalletAndPasswordsDir(t)
randPath, err := rand.Int(rand.Reader, big.NewInt(1000000))
require.NoError(t, err, "Could not generate random file path")
exportDir := path.Join(testutil.TempDir(), fmt.Sprintf("/%d", randPath), "export")
t.Cleanup(func() {
require.NoError(t, os.RemoveAll(exportDir), "Failed to remove directory")
})
accounts := "all"
cliCtx := setupWalletCtx(t, &testWalletConfig{
walletDir: walletDir,
passwordsDir: passwordsDir,
exportDir: exportDir,
accountsToExport: accounts,
keymanagerKind: v2keymanager.Direct,
})
wallet, err := NewWallet(cliCtx)
require.NoError(t, err)
ctx := context.Background()
keymanager, err := direct.NewKeymanager(
ctx,
wallet,
direct.DefaultConfig(),
true, /* skip mnemonic */
)
require.NoError(t, err)
_, err = keymanager.CreateAccount(ctx, password)
require.NoError(t, err)
require.NoError(t, ExportAccount(cliCtx))
if _, err := os.Stat(filepath.Join(exportDir, archiveFilename)); os.IsNotExist(err) {
t.Fatal("Expected file to exist")
}
}