2019-08-18 19:54:20 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2019-08-21 15:30:22 +00:00
|
|
|
"crypto/ecdsa"
|
|
|
|
"crypto/rand"
|
2019-09-12 00:04:35 +00:00
|
|
|
"fmt"
|
2019-08-18 19:54:20 +00:00
|
|
|
"testing"
|
2019-09-06 19:20:20 +00:00
|
|
|
"time"
|
2019-08-18 19:54:20 +00:00
|
|
|
|
2019-08-21 15:30:22 +00:00
|
|
|
"github.com/btcsuite/btcd/btcec"
|
2019-09-06 19:20:20 +00:00
|
|
|
"github.com/ethereum/go-ethereum/p2p/discover"
|
|
|
|
"github.com/ethereum/go-ethereum/p2p/enode"
|
2019-08-21 15:30:22 +00:00
|
|
|
"github.com/libp2p/go-libp2p-core/crypto"
|
2019-08-18 19:54:20 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/shared/iputils"
|
|
|
|
_ "go.uber.org/automaxprocs"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestBootnode_OK(t *testing.T) {
|
|
|
|
ipAddr, err := iputils.ExternalIPv4()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2020-06-23 02:53:11 +00:00
|
|
|
privKey := extractPrivateKey()
|
2019-09-06 19:20:20 +00:00
|
|
|
cfg := discover.Config{
|
2019-09-23 17:24:16 +00:00
|
|
|
PrivateKey: privKey,
|
2019-08-18 19:54:20 +00:00
|
|
|
}
|
2019-09-06 19:20:20 +00:00
|
|
|
listener := createListener(ipAddr, 4000, cfg)
|
|
|
|
defer listener.Close()
|
2019-08-18 19:54:20 +00:00
|
|
|
|
2020-06-23 02:53:11 +00:00
|
|
|
cfg.PrivateKey = extractPrivateKey()
|
2019-09-06 19:20:20 +00:00
|
|
|
bootNode, err := enode.Parse(enode.ValidSchemes, listener.Self().String())
|
2019-08-18 19:54:20 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2019-09-06 19:20:20 +00:00
|
|
|
cfg.Bootnodes = []*enode.Node{bootNode}
|
|
|
|
listener2 := createListener(ipAddr, 4001, cfg)
|
|
|
|
defer listener2.Close()
|
2019-08-18 19:54:20 +00:00
|
|
|
|
|
|
|
// test that both the nodes have the other peer stored in their local table.
|
|
|
|
listenerNode := listener.Self()
|
|
|
|
listenerNode2 := listener2.Self()
|
|
|
|
|
2019-09-06 19:20:20 +00:00
|
|
|
time.Sleep(1 * time.Second)
|
|
|
|
|
|
|
|
nodes := listener.Lookup(listenerNode2.ID())
|
|
|
|
if len(nodes) == 0 {
|
|
|
|
t.Fatalf("Length of nodes stored in table is not expected. Wanted to be more than %d but got %d", 0, len(nodes))
|
2019-08-18 19:54:20 +00:00
|
|
|
|
|
|
|
}
|
2019-09-06 19:20:20 +00:00
|
|
|
if nodes[0].ID() != listenerNode2.ID() {
|
|
|
|
t.Errorf("Wanted node ID of %s but got %s", listenerNode2.ID(), nodes[1].ID())
|
2019-08-18 19:54:20 +00:00
|
|
|
}
|
|
|
|
|
2019-09-06 19:20:20 +00:00
|
|
|
nodes = listener2.Lookup(listenerNode.ID())
|
|
|
|
if len(nodes) == 0 {
|
|
|
|
t.Errorf("Length of nodes stored in table is not expected. Wanted to be more than %d but got %d", 0, len(nodes))
|
2019-08-18 19:54:20 +00:00
|
|
|
|
|
|
|
}
|
2019-09-06 19:20:20 +00:00
|
|
|
if nodes[0].ID() != listenerNode.ID() {
|
|
|
|
t.Errorf("Wanted node ID of %s but got %s", listenerNode.ID(), nodes[1].ID())
|
2019-08-18 19:54:20 +00:00
|
|
|
}
|
|
|
|
}
|
2019-08-21 15:30:22 +00:00
|
|
|
|
|
|
|
func TestPrivateKey_ParsesCorrectly(t *testing.T) {
|
|
|
|
privKey, _, err := crypto.GenerateSecp256k1Key(rand.Reader)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2019-09-12 00:04:35 +00:00
|
|
|
|
|
|
|
pk, err := privKey.Raw()
|
2019-08-21 15:30:22 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2019-09-12 00:04:35 +00:00
|
|
|
*privateKey = fmt.Sprintf("%x", pk)
|
2019-08-21 15:30:22 +00:00
|
|
|
|
2020-06-23 02:53:11 +00:00
|
|
|
extractedKey := extractPrivateKey()
|
2019-08-21 15:30:22 +00:00
|
|
|
|
|
|
|
rawKey := (*ecdsa.PrivateKey)((*btcec.PrivateKey)(privKey.(*crypto.Secp256k1PrivateKey)))
|
|
|
|
|
|
|
|
r, s, err := ecdsa.Sign(rand.Reader, extractedKey, []byte{'t', 'e', 's', 't'})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
isVerified := ecdsa.Verify(&rawKey.PublicKey, []byte{'t', 'e', 's', 't'}, r, s)
|
|
|
|
if !isVerified {
|
|
|
|
t.Error("Unmarshalled key is not the same as the key that was given to the function")
|
|
|
|
}
|
|
|
|
*privateKey = ""
|
|
|
|
}
|