prysm-pulse/crypto/keystore/keystore_test.go
Raul Jordan d077483577
Add V3 Suffix to All Prysm Packages (#11083)
* v3 import renamings

* tidy

* fmt

* rev

* Update beacon-chain/core/epoch/precompute/reward_penalty_test.go

* Update beacon-chain/core/helpers/validators_test.go

* Update beacon-chain/db/alias.go

* Update beacon-chain/db/alias.go

* Update beacon-chain/db/alias.go

* Update beacon-chain/db/iface/BUILD.bazel

* Update beacon-chain/db/kv/kv.go

* Update beacon-chain/db/kv/state.go

* Update beacon-chain/rpc/prysm/v1alpha1/validator/attester_test.go

* Update beacon-chain/rpc/prysm/v1alpha1/validator/attester_test.go

* Update beacon-chain/sync/initial-sync/service.go

* fix deps

* fix bad replacements

* fix bad replacements

* change back

* gohashtree version

* fix deps

Co-authored-by: Nishant Das <nishdas93@gmail.com>
Co-authored-by: Potuz <potuz@prysmaticlabs.com>
2022-08-16 12:20:13 +00:00

96 lines
2.7 KiB
Go

package keystore
import (
"bytes"
"os"
"path"
"testing"
"github.com/pborman/uuid"
"github.com/prysmaticlabs/prysm/v3/crypto/bls"
"github.com/prysmaticlabs/prysm/v3/encoding/bytesutil"
"github.com/prysmaticlabs/prysm/v3/testing/assert"
"github.com/prysmaticlabs/prysm/v3/testing/require"
)
func TestStoreAndGetKey(t *testing.T) {
tempDir := path.Join(t.TempDir(), "keystore", "file")
ks := &Keystore{
keysDirPath: tempDir,
scryptN: LightScryptN,
scryptP: LightScryptP,
}
key, err := NewKey()
require.NoError(t, err)
require.NoError(t, ks.StoreKey(tempDir, key, "password"))
decryptedKey, err := ks.GetKey(tempDir, "password")
require.NoError(t, err)
require.Equal(t, true, bytes.Equal(decryptedKey.SecretKey.Marshal(), key.SecretKey.Marshal()))
}
func TestStoreAndGetKeys(t *testing.T) {
tempDir := path.Join(t.TempDir(), "keystore")
ks := &Keystore{
keysDirPath: tempDir,
scryptN: LightScryptN,
scryptP: LightScryptP,
}
key, err := NewKey()
require.NoError(t, err)
require.NoError(t, ks.StoreKey(tempDir+"/test-1", key, "password"))
key2, err := NewKey()
require.NoError(t, err)
require.NoError(t, ks.StoreKey(tempDir+"/test-2", key, "password"))
decryptedKeys, err := ks.GetKeys(tempDir, "test", "password", false)
require.NoError(t, err)
for _, s := range decryptedKeys {
require.Equal(t, true, bytes.Equal(s.SecretKey.Marshal(), key.SecretKey.Marshal()) && !bytes.Equal(s.SecretKey.Marshal(), key2.SecretKey.Marshal()))
}
}
func TestEncryptDecryptKey(t *testing.T) {
newID := uuid.NewRandom()
b := []byte("hi")
b32 := bytesutil.ToBytes32(b)
password := "test"
pk, err := bls.SecretKeyFromBytes(b32[:])
require.NoError(t, err)
key := &Key{
ID: newID,
SecretKey: pk,
PublicKey: pk.PublicKey(),
}
keyJSON, err := EncryptKey(key, password, LightScryptN, LightScryptP)
require.NoError(t, err)
decryptedKey, err := DecryptKey(keyJSON, password)
require.NoError(t, err)
require.Equal(t, true, bytes.Equal(decryptedKey.ID, newID))
expected := pk.Marshal()
require.Equal(t, true, bytes.Equal(decryptedKey.SecretKey.Marshal(), expected))
}
func TestGetSymlinkedKeys(t *testing.T) {
tempDir := path.Join(t.TempDir(), "keystore")
ks := &Keystore{
scryptN: LightScryptN,
scryptP: LightScryptP,
}
key, err := NewKey()
require.NoError(t, err)
require.NoError(t, ks.StoreKey(tempDir+"/files/test-1", key, "password"))
require.NoError(t, os.Symlink(tempDir+"/files/test-1", tempDir+"/test-1"))
decryptedKeys, err := ks.GetKeys(tempDir, "test", "password", false)
require.NoError(t, err)
assert.Equal(t, 1, len(decryptedKeys))
for _, s := range decryptedKeys {
require.Equal(t, true, bytes.Equal(s.SecretKey.Marshal(), key.SecretKey.Marshal()))
}
}