github.com/prysmaticlabs/prysm@v1.4.4/shared/keystore/keystore_test.go (about)

     1  package keystore
     2  
     3  import (
     4  	"bytes"
     5  	"os"
     6  	"path"
     7  	"testing"
     8  
     9  	"github.com/pborman/uuid"
    10  	"github.com/prysmaticlabs/prysm/shared/bls"
    11  	"github.com/prysmaticlabs/prysm/shared/bytesutil"
    12  	"github.com/prysmaticlabs/prysm/shared/testutil/assert"
    13  	"github.com/prysmaticlabs/prysm/shared/testutil/require"
    14  )
    15  
    16  func TestStoreAndGetKey(t *testing.T) {
    17  	tempDir := path.Join(t.TempDir(), "keystore", "file")
    18  	ks := &Keystore{
    19  		keysDirPath: tempDir,
    20  		scryptN:     LightScryptN,
    21  		scryptP:     LightScryptP,
    22  	}
    23  
    24  	key, err := NewKey()
    25  	require.NoError(t, err)
    26  	require.NoError(t, ks.StoreKey(tempDir, key, "password"))
    27  
    28  	decryptedKey, err := ks.GetKey(tempDir, "password")
    29  	require.NoError(t, err)
    30  	require.Equal(t, true, bytes.Equal(decryptedKey.SecretKey.Marshal(), key.SecretKey.Marshal()))
    31  }
    32  
    33  func TestStoreAndGetKeys(t *testing.T) {
    34  	tempDir := path.Join(t.TempDir(), "keystore")
    35  	ks := &Keystore{
    36  		keysDirPath: tempDir,
    37  		scryptN:     LightScryptN,
    38  		scryptP:     LightScryptP,
    39  	}
    40  
    41  	key, err := NewKey()
    42  	require.NoError(t, err)
    43  	require.NoError(t, ks.StoreKey(tempDir+"/test-1", key, "password"))
    44  	key2, err := NewKey()
    45  	require.NoError(t, err)
    46  	require.NoError(t, ks.StoreKey(tempDir+"/test-2", key, "password"))
    47  	decryptedKeys, err := ks.GetKeys(tempDir, "test", "password", false)
    48  	require.NoError(t, err)
    49  	for _, s := range decryptedKeys {
    50  		require.Equal(t, true, bytes.Equal(s.SecretKey.Marshal(), key.SecretKey.Marshal()) && !bytes.Equal(s.SecretKey.Marshal(), key2.SecretKey.Marshal()))
    51  	}
    52  }
    53  
    54  func TestEncryptDecryptKey(t *testing.T) {
    55  	newID := uuid.NewRandom()
    56  	b := []byte("hi")
    57  	b32 := bytesutil.ToBytes32(b)
    58  	password := "test"
    59  
    60  	pk, err := bls.SecretKeyFromBytes(b32[:])
    61  	require.NoError(t, err)
    62  	key := &Key{
    63  		ID:        newID,
    64  		SecretKey: pk,
    65  		PublicKey: pk.PublicKey(),
    66  	}
    67  
    68  	keyJSON, err := EncryptKey(key, password, LightScryptN, LightScryptP)
    69  	require.NoError(t, err)
    70  
    71  	decryptedKey, err := DecryptKey(keyJSON, password)
    72  	require.NoError(t, err)
    73  	require.Equal(t, true, bytes.Equal(decryptedKey.ID, newID))
    74  	expected := pk.Marshal()
    75  	require.Equal(t, true, bytes.Equal(decryptedKey.SecretKey.Marshal(), expected))
    76  }
    77  
    78  func TestGetSymlinkedKeys(t *testing.T) {
    79  	tempDir := path.Join(t.TempDir(), "keystore")
    80  	ks := &Keystore{
    81  		scryptN: LightScryptN,
    82  		scryptP: LightScryptP,
    83  	}
    84  
    85  	key, err := NewKey()
    86  	require.NoError(t, err)
    87  	require.NoError(t, ks.StoreKey(tempDir+"/files/test-1", key, "password"))
    88  	require.NoError(t, os.Symlink(tempDir+"/files/test-1", tempDir+"/test-1"))
    89  	decryptedKeys, err := ks.GetKeys(tempDir, "test", "password", false)
    90  	require.NoError(t, err)
    91  	assert.Equal(t, 1, len(decryptedKeys))
    92  	for _, s := range decryptedKeys {
    93  		require.Equal(t, true, bytes.Equal(s.SecretKey.Marshal(), key.SecretKey.Marshal()))
    94  	}
    95  }