2018-07-17 18:39:04 +00:00
|
|
|
package p2p
|
|
|
|
|
2019-03-03 17:31:29 +00:00
|
|
|
import (
|
2019-05-29 19:43:23 +00:00
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
2019-03-03 17:31:29 +00:00
|
|
|
"testing"
|
2019-05-29 19:43:23 +00:00
|
|
|
|
|
|
|
crypto "github.com/libp2p/go-libp2p-crypto"
|
|
|
|
"github.com/libp2p/go-libp2p/config"
|
|
|
|
"github.com/prysmaticlabs/prysm/shared/testutil"
|
2019-03-03 17:31:29 +00:00
|
|
|
)
|
2018-07-17 18:39:04 +00:00
|
|
|
|
|
|
|
func TestBuildOptions(t *testing.T) {
|
2019-05-28 12:54:49 +00:00
|
|
|
opts := buildOptions(&ServerConfig{})
|
2018-07-17 18:39:04 +00:00
|
|
|
|
|
|
|
_ = opts
|
|
|
|
}
|
2019-05-29 19:43:23 +00:00
|
|
|
|
|
|
|
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")
|
|
|
|
}
|
|
|
|
}
|