2018-11-08 03:22:31 +00:00
|
|
|
package keystore
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"crypto/rand"
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/pborman/uuid"
|
2019-02-15 18:31:07 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/shared/bls"
|
2019-03-03 17:31:29 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/shared/bytesutil"
|
2019-02-15 19:04:51 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/shared/testutil"
|
2018-11-08 03:22:31 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestMarshalAndUnmarshal(t *testing.T) {
|
|
|
|
testID := uuid.NewRandom()
|
2019-02-19 15:09:50 +00:00
|
|
|
blsKey, err := bls.RandKey(rand.Reader)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
2019-02-15 18:31:07 +00:00
|
|
|
}
|
2018-11-08 03:22:31 +00:00
|
|
|
key := &Key{
|
|
|
|
ID: testID,
|
|
|
|
SecretKey: blsKey,
|
2019-02-19 15:09:50 +00:00
|
|
|
PublicKey: blsKey.PublicKey(),
|
2018-11-08 03:22:31 +00:00
|
|
|
}
|
|
|
|
marshalledObject, err := key.MarshalJSON()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to marshall key %v", err)
|
|
|
|
}
|
|
|
|
newKey := &Key{
|
2019-02-19 15:09:50 +00:00
|
|
|
ID: []byte{},
|
|
|
|
SecretKey: blsKey,
|
|
|
|
PublicKey: blsKey.PublicKey(),
|
2018-11-08 03:22:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
err = newKey.UnmarshalJSON(marshalledObject)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to unmarshall object %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !bytes.Equal([]byte(newKey.ID), []byte(testID)) {
|
|
|
|
t.Fatalf("retrieved id not the same as pre serialized id: %v ", newKey.ID)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestStoreRandomKey(t *testing.T) {
|
2019-02-15 19:04:51 +00:00
|
|
|
tmpdir := testutil.TempDir()
|
2018-11-08 03:22:31 +00:00
|
|
|
filedir := tmpdir + "/keystore"
|
2019-01-31 11:57:57 +00:00
|
|
|
ks := &Store{
|
2018-11-08 03:22:31 +00:00
|
|
|
keysDirPath: filedir,
|
|
|
|
scryptN: LightScryptN,
|
|
|
|
scryptP: LightScryptP,
|
|
|
|
}
|
|
|
|
|
|
|
|
reader := rand.Reader
|
|
|
|
|
|
|
|
if err := storeNewRandomKey(ks, reader, "password"); err != nil {
|
|
|
|
t.Fatalf("storage of random key unsuccessful %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := os.RemoveAll(filedir); err != nil {
|
|
|
|
t.Errorf("unable to remove temporary files %v", err)
|
|
|
|
}
|
|
|
|
}
|
2019-02-22 15:11:26 +00:00
|
|
|
|
2018-11-08 03:22:31 +00:00
|
|
|
func TestNewKeyFromBLS(t *testing.T) {
|
2019-02-19 15:09:50 +00:00
|
|
|
b := []byte("hi")
|
|
|
|
b32 := bytesutil.ToBytes32(b)
|
|
|
|
blskey, err := bls.SecretKeyFromBytes(b32[:])
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
2019-02-15 18:31:07 +00:00
|
|
|
}
|
|
|
|
key, err := newKeyFromBLS(blskey)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("could not get new key from bls %v", err)
|
|
|
|
}
|
2018-11-08 03:22:31 +00:00
|
|
|
|
2019-02-19 15:09:50 +00:00
|
|
|
expected := blskey.Marshal()
|
|
|
|
if !bytes.Equal(expected, key.SecretKey.Marshal()) {
|
|
|
|
t.Fatalf("secret key is not of the expected value %d", key.SecretKey.Marshal())
|
2019-02-15 18:31:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
reader := rand.Reader
|
2018-11-08 03:22:31 +00:00
|
|
|
|
2019-02-15 18:31:07 +00:00
|
|
|
_, err = NewKey(reader)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("random key unable to be generated: %v", err)
|
2018-11-08 03:22:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestWriteFile(t *testing.T) {
|
2019-02-26 16:41:22 +00:00
|
|
|
tmpdir := testutil.TempDir()
|
2018-11-08 03:22:31 +00:00
|
|
|
filedir := tmpdir + "/keystore"
|
|
|
|
|
|
|
|
testKeystore := []byte{'t', 'e', 's', 't'}
|
|
|
|
|
|
|
|
err := writeKeyFile(filedir, testKeystore)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to write file %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
keystore, err := ioutil.ReadFile(filedir)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to retrieve file %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !bytes.Equal(keystore, testKeystore) {
|
|
|
|
t.Fatalf("retrieved keystore is not the same %v", keystore)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := os.RemoveAll(filedir); err != nil {
|
|
|
|
t.Errorf("unable to remove temporary files %v", err)
|
|
|
|
}
|
|
|
|
}
|