prysm-pulse/shared/keystore/key_test.go
Nishant Das 4f0bef929f Change BLS to Herumi Again (#4181)
* change to herumi's bls
* change alias
* change to better
* add benchmark
* build
* change to bazel fork
* fix prefix
* Merge branch 'master' of https://github.com/prysmaticlabs/geth-sharding into herumiBLS
* make it work with library
* update to latest
* change again
* add import
* update to latest
* add sha commit
* new static lib with groups swapped
* using herumis new lib
* fix dep paths in c headers
* update again
* new changes
* fix commit
* Merge branch 'master' of https://github.com/prysmaticlabs/geth-sharding into herumiBLS
* fix serialization
* comment
* fix test
* Merge branch 'master' of https://github.com/prysmaticlabs/geth-sharding into herumiBLS
* fix to herumis latest version
* fix test
* fix benchmarks
* Merge branch 'master' of https://github.com/prysmaticlabs/geth-sharding into herumiBLS
* Merge branch 'master' of https://github.com/prysmaticlabs/geth-sharding into herumiBLS
* add new workspace
* change commit and remove init
* get test to pass
* remove parameter
* remove reverse byte order
* make gazelle happy
* set pure to off
* fix failing tests
* Merge branch 'master' into herumiBLS
* Merge branch 'master' of https://github.com/prysmaticlabs/geth-sharding into herumiBLS
* Merge branch 'herumiBLS' of https://github.com/prysmaticlabs/geth-sharding into herumiBLS
* remove old ref
* use HashWithDomain functions
* update to latest version
* clean up
* gaz
* add back removed code
* switch off pure
* Merge branch 'master' of https://github.com/prysmaticlabs/geth-sharding into herumiBLS
* use local repo
* resolve docker issues
* Merge branch 'master' of https://github.com/prysmaticlabs/geth-sharding into herumiBLS
* fix build and tests
* gaz
* Merge branch 'master' into herumiBLS
* Merge refs/heads/master into herumiBLS
* Merge refs/heads/master into herumiBLS
2019-12-03 20:29:05 +00:00

114 lines
2.5 KiB
Go

package keystore
import (
"bytes"
"crypto/rand"
"io/ioutil"
"os"
"testing"
"github.com/pborman/uuid"
"github.com/prysmaticlabs/prysm/shared/bls"
"github.com/prysmaticlabs/prysm/shared/bytesutil"
"github.com/prysmaticlabs/prysm/shared/testutil"
)
func TestMarshalAndUnmarshal(t *testing.T) {
testID := uuid.NewRandom()
blsKey := bls.RandKey()
key := &Key{
ID: testID,
SecretKey: blsKey,
PublicKey: blsKey.PublicKey(),
}
marshalledObject, err := key.MarshalJSON()
if err != nil {
t.Fatalf("unable to marshall key %v", err)
}
newKey := &Key{
ID: []byte{},
SecretKey: blsKey,
PublicKey: blsKey.PublicKey(),
}
err = newKey.UnmarshalJSON(marshalledObject)
if err != nil {
t.Fatalf("unable to unmarshal 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) {
tmpdir := testutil.TempDir()
filedir := tmpdir + "/keystore"
ks := &Store{
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)
}
}
func TestNewKeyFromBLS(t *testing.T) {
b := []byte("hi")
b32 := bytesutil.ToBytes32(b)
blskey, err := bls.SecretKeyFromBytes(b32[:])
if err != nil {
t.Fatal(err)
}
key, err := NewKeyFromBLS(blskey)
if err != nil {
t.Fatalf("could not get new key from bls %v", err)
}
expected := blskey.Marshal()
if !bytes.Equal(expected, key.SecretKey.Marshal()) {
t.Fatalf("secret key is not of the expected value %d", key.SecretKey.Marshal())
}
reader := rand.Reader
_, err = NewKey(reader)
if err != nil {
t.Fatalf("random key unable to be generated: %v", err)
}
}
func TestWriteFile(t *testing.T) {
tmpdir := testutil.TempDir()
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)
}
}