2015-01-18 19:24:09 -06:00
|
|
|
package p2p
|
|
|
|
|
|
|
|
import (
|
2015-01-18 22:53:48 -06:00
|
|
|
"bytes"
|
2015-01-21 08:42:12 -06:00
|
|
|
"net"
|
2015-02-18 18:52:03 -06:00
|
|
|
"reflect"
|
2015-01-18 19:24:09 -06:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/ethereum/go-ethereum/crypto"
|
2015-02-17 05:04:02 -06:00
|
|
|
"github.com/ethereum/go-ethereum/crypto/ecies"
|
2015-02-18 18:52:03 -06:00
|
|
|
"github.com/ethereum/go-ethereum/p2p/discover"
|
2015-01-18 19:24:09 -06:00
|
|
|
)
|
|
|
|
|
2015-01-20 10:47:46 -06:00
|
|
|
func TestPublicKeyEncoding(t *testing.T) {
|
|
|
|
prv0, _ := crypto.GenerateKey() // = ecdsa.GenerateKey(crypto.S256(), rand.Reader)
|
|
|
|
pub0 := &prv0.PublicKey
|
|
|
|
pub0s := crypto.FromECDSAPub(pub0)
|
2015-02-04 20:07:58 -06:00
|
|
|
pub1, err := importPublicKey(pub0s)
|
2015-01-20 10:47:46 -06:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("%v", err)
|
|
|
|
}
|
|
|
|
eciesPub1 := ecies.ImportECDSAPublic(pub1)
|
|
|
|
if eciesPub1 == nil {
|
|
|
|
t.Errorf("invalid ecdsa public key")
|
|
|
|
}
|
2015-02-04 20:07:58 -06:00
|
|
|
pub1s, err := exportPublicKey(pub1)
|
2015-01-20 10:47:46 -06:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("%v", err)
|
|
|
|
}
|
|
|
|
if len(pub1s) != 64 {
|
|
|
|
t.Errorf("wrong length expect 64, got", len(pub1s))
|
|
|
|
}
|
2015-02-04 20:07:58 -06:00
|
|
|
pub2, err := importPublicKey(pub1s)
|
2015-01-20 10:47:46 -06:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("%v", err)
|
|
|
|
}
|
2015-02-04 20:07:58 -06:00
|
|
|
pub2s, err := exportPublicKey(pub2)
|
2015-01-20 10:47:46 -06:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("%v", err)
|
|
|
|
}
|
|
|
|
if !bytes.Equal(pub1s, pub2s) {
|
|
|
|
t.Errorf("exports dont match")
|
|
|
|
}
|
|
|
|
pub2sEC := crypto.FromECDSAPub(pub2)
|
|
|
|
if !bytes.Equal(pub0s, pub2sEC) {
|
|
|
|
t.Errorf("exports dont match")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSharedSecret(t *testing.T) {
|
|
|
|
prv0, _ := crypto.GenerateKey() // = ecdsa.GenerateKey(crypto.S256(), rand.Reader)
|
|
|
|
pub0 := &prv0.PublicKey
|
|
|
|
prv1, _ := crypto.GenerateKey()
|
|
|
|
pub1 := &prv1.PublicKey
|
|
|
|
|
|
|
|
ss0, err := ecies.ImportECDSA(prv0).GenerateShared(ecies.ImportECDSAPublic(pub1), sskLen, sskLen)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
ss1, err := ecies.ImportECDSA(prv1).GenerateShared(ecies.ImportECDSAPublic(pub0), sskLen, sskLen)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
t.Logf("Secret:\n%v %x\n%v %x", len(ss0), ss0, len(ss0), ss1)
|
|
|
|
if !bytes.Equal(ss0, ss1) {
|
|
|
|
t.Errorf("dont match :(")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-18 18:52:03 -06:00
|
|
|
func TestEncHandshake(t *testing.T) {
|
2015-01-21 08:42:12 -06:00
|
|
|
defer testlog(t).detach()
|
|
|
|
|
2015-02-04 20:07:58 -06:00
|
|
|
prv0, _ := crypto.GenerateKey()
|
|
|
|
prv1, _ := crypto.GenerateKey()
|
|
|
|
rw0, rw1 := net.Pipe()
|
2015-02-26 20:09:53 -06:00
|
|
|
secrets := make(chan secrets)
|
2015-01-21 08:42:12 -06:00
|
|
|
|
|
|
|
go func() {
|
2015-02-26 20:09:53 -06:00
|
|
|
pub1s, _ := exportPublicKey(&prv1.PublicKey)
|
|
|
|
s, err := outboundEncHandshake(rw0, prv0, pub1s, nil)
|
2015-02-04 20:07:58 -06:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("outbound side error: %v", err)
|
|
|
|
}
|
2015-02-26 20:09:53 -06:00
|
|
|
id1 := discover.PubkeyID(&prv1.PublicKey)
|
|
|
|
if s.RemoteID != id1 {
|
|
|
|
t.Errorf("outbound side remote ID mismatch")
|
|
|
|
}
|
|
|
|
secrets <- s
|
2015-01-21 08:42:12 -06:00
|
|
|
}()
|
|
|
|
go func() {
|
2015-02-26 20:09:53 -06:00
|
|
|
s, err := inboundEncHandshake(rw1, prv1, nil)
|
2015-02-04 20:07:58 -06:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("inbound side error: %v", err)
|
|
|
|
}
|
2015-02-26 20:09:53 -06:00
|
|
|
id0 := discover.PubkeyID(&prv0.PublicKey)
|
|
|
|
if s.RemoteID != id0 {
|
|
|
|
t.Errorf("inbound side remote ID mismatch")
|
2015-02-04 20:07:58 -06:00
|
|
|
}
|
2015-02-26 20:09:53 -06:00
|
|
|
secrets <- s
|
2015-01-21 08:42:12 -06:00
|
|
|
}()
|
2015-02-04 20:07:58 -06:00
|
|
|
|
2015-02-26 20:09:53 -06:00
|
|
|
// get computed secrets from both sides
|
|
|
|
t1, t2 := <-secrets, <-secrets
|
|
|
|
// don't compare remote node IDs
|
|
|
|
t1.RemoteID, t2.RemoteID = discover.NodeID{}, discover.NodeID{}
|
|
|
|
// flip MACs on one of them so they compare equal
|
|
|
|
t1.EgressMAC, t1.IngressMAC = t1.IngressMAC, t1.EgressMAC
|
|
|
|
if !reflect.DeepEqual(t1, t2) {
|
|
|
|
t.Errorf("secrets mismatch:\n t1: %#v\n t2: %#v", t1, t2)
|
2015-01-21 08:42:12 -06:00
|
|
|
}
|
|
|
|
}
|
2015-02-18 18:52:03 -06:00
|
|
|
|
|
|
|
func TestSetupConn(t *testing.T) {
|
|
|
|
prv0, _ := crypto.GenerateKey()
|
|
|
|
prv1, _ := crypto.GenerateKey()
|
|
|
|
node0 := &discover.Node{
|
|
|
|
ID: discover.PubkeyID(&prv0.PublicKey),
|
|
|
|
IP: net.IP{1, 2, 3, 4},
|
|
|
|
TCPPort: 33,
|
|
|
|
}
|
|
|
|
node1 := &discover.Node{
|
|
|
|
ID: discover.PubkeyID(&prv1.PublicKey),
|
|
|
|
IP: net.IP{5, 6, 7, 8},
|
|
|
|
TCPPort: 44,
|
|
|
|
}
|
|
|
|
hs0 := &protoHandshake{
|
|
|
|
Version: baseProtocolVersion,
|
|
|
|
ID: node0.ID,
|
|
|
|
Caps: []Cap{{"a", 0}, {"b", 2}},
|
|
|
|
}
|
|
|
|
hs1 := &protoHandshake{
|
|
|
|
Version: baseProtocolVersion,
|
|
|
|
ID: node1.ID,
|
|
|
|
Caps: []Cap{{"c", 1}, {"d", 3}},
|
|
|
|
}
|
|
|
|
fd0, fd1 := net.Pipe()
|
|
|
|
|
|
|
|
done := make(chan struct{})
|
|
|
|
go func() {
|
|
|
|
defer close(done)
|
|
|
|
conn0, err := setupConn(fd0, prv0, hs0, node1)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("outbound side error: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if conn0.ID != node1.ID {
|
|
|
|
t.Errorf("outbound conn id mismatch: got %v, want %v", conn0.ID, node1.ID)
|
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(conn0.Caps, hs1.Caps) {
|
|
|
|
t.Errorf("outbound caps mismatch: got %v, want %v", conn0.Caps, hs1.Caps)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
conn1, err := setupConn(fd1, prv1, hs1, nil)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("inbound side error: %v", err)
|
|
|
|
}
|
|
|
|
if conn1.ID != node0.ID {
|
|
|
|
t.Errorf("inbound conn id mismatch: got %v, want %v", conn1.ID, node0.ID)
|
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(conn1.Caps, hs0.Caps) {
|
|
|
|
t.Errorf("inbound caps mismatch: got %v, want %v", conn1.Caps, hs0.Caps)
|
|
|
|
}
|
|
|
|
|
|
|
|
<-done
|
|
|
|
}
|