mirror of
https://gitlab.com/pulsechaincom/prysm-pulse.git
synced 2025-01-06 01:32:18 +00:00
932e68571b
* expose p2p private key for static peering * Review revisions * Use testutil.TempDir() * Use testing.T to report fatal errors
52 lines
1.2 KiB
Go
52 lines
1.2 KiB
Go
package p2p
|
|
|
|
import (
|
|
"io/ioutil"
|
|
"os"
|
|
"testing"
|
|
|
|
crypto "github.com/libp2p/go-libp2p-crypto"
|
|
"github.com/libp2p/go-libp2p/config"
|
|
"github.com/prysmaticlabs/prysm/shared/testutil"
|
|
)
|
|
|
|
func TestBuildOptions(t *testing.T) {
|
|
opts := buildOptions(&ServerConfig{})
|
|
|
|
_ = opts
|
|
}
|
|
|
|
func TestPrivateKeyLoading(t *testing.T) {
|
|
file, err := ioutil.TempFile(testutil.TempDir(), "key")
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
defer os.Remove(file.Name())
|
|
key, _, err := crypto.GenerateKeyPair(crypto.RSA, 2048)
|
|
if err != nil {
|
|
t.Fatalf("Could not generate key: %v", err)
|
|
}
|
|
marshaled, err := crypto.MarshalPrivateKey(key)
|
|
if err != nil {
|
|
t.Fatalf("Could not marshal key: %v", err)
|
|
}
|
|
keyStr := crypto.ConfigEncodeKey(marshaled)
|
|
|
|
err = ioutil.WriteFile(file.Name(), []byte(keyStr), 0600)
|
|
if err != nil {
|
|
t.Fatalf("Could not write key to file: %v", err)
|
|
}
|
|
log.WithField("file", file.Name()).WithField("key", keyStr).Info("Wrote key to file")
|
|
|
|
var cfg config.Config
|
|
err = cfg.Apply(privKey(file.Name()))
|
|
if err != nil {
|
|
t.Fatalf("Could not apply option: %v", err)
|
|
}
|
|
newMarshaled, _ := crypto.MarshalPrivateKey(cfg.PeerKey)
|
|
newEncoded := crypto.ConfigEncodeKey(newMarshaled)
|
|
if newEncoded != keyStr {
|
|
t.Error("Private keys do not match")
|
|
}
|
|
}
|