prysm-pulse/validator/accounts/account_test.go
Victor Farazdagi b42cc724c5
Enforce validator's keystore dir (#5707)
* enforces keystore dir
* Merge refs/heads/master into recheck-before-raw-tx-data
* do not ignore error
* Merge branch 'recheck-before-raw-tx-data' of github.com:prysmaticlabs/prysm into recheck-before-raw-tx-data
2020-05-01 15:12:26 +00:00

52 lines
1.4 KiB
Go

package accounts
import (
"fmt"
"io/ioutil"
"os"
"testing"
"github.com/prysmaticlabs/prysm/shared/keystore"
"github.com/prysmaticlabs/prysm/shared/params"
"github.com/prysmaticlabs/prysm/shared/testutil"
)
func TestNewValidatorAccount_AccountExists(t *testing.T) {
directory := testutil.TempDir() + "/testkeystore"
defer func() {
if err := os.RemoveAll(directory); err != nil {
t.Logf("Could not remove directory: %v", err)
}
}()
validatorKey, err := keystore.NewKey()
if err != nil {
t.Fatalf("Cannot create new key: %v", err)
}
ks := keystore.NewKeystore(directory)
if err := ks.StoreKey(directory+params.BeaconConfig().ValidatorPrivkeyFileName, validatorKey, ""); err != nil {
t.Fatalf("Unable to store key %v", err)
}
if err := NewValidatorAccount(directory, ""); err != nil {
t.Errorf("Should support multiple keys: %v", err)
}
files, err := ioutil.ReadDir(directory)
if err != nil {
t.Error(err)
}
if len(files) != 3 {
t.Errorf("multiple validators were not created only %v files in directory", len(files))
for _, f := range files {
t.Errorf("%v\n", f.Name())
}
}
}
func TestNewValidatorAccount_CreateValidatorAccount(t *testing.T) {
directory := "foobar"
_, _, err := CreateValidatorAccount(directory, "foobar")
wantErrString := fmt.Sprintf("path %q does not exist", directory)
if err == nil || err.Error() != wantErrString {
t.Errorf("expected error not thrown, want: %v, got: %v", wantErrString, err)
}
}