5
0
mirror of https://github.com/cwinfo/yggdrasil-go.git synced 2024-11-22 01:20:29 +00:00

possibly fix src/core/core_test.go

This commit is contained in:
Arceliar 2021-05-29 11:13:59 -05:00
parent 8a60c605f6
commit 180654c495
2 changed files with 22 additions and 45 deletions

View File

@ -3,6 +3,7 @@ package core
import ( import (
"bytes" "bytes"
"math/rand" "math/rand"
"net/url"
"os" "os"
"testing" "testing"
"time" "time"
@ -50,10 +51,16 @@ func CreateAndConnectTwo(t testing.TB, verbose bool) (nodeA *Core, nodeB *Core)
t.Fatal(err) t.Fatal(err)
} }
err = nodeB.AddPeer("tcp://"+nodeA.link.tcp.getAddr().String(), "") u, err := url.Parse("tcp://" + nodeA.links.tcp.getAddr().String())
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
err = nodeB.CallPeer(u, "")
if err != nil {
t.Fatal(err)
}
time.Sleep(100 * time.Millisecond)
if l := len(nodeA.GetPeers()); l != 1 { if l := len(nodeA.GetPeers()); l != 1 {
t.Fatal("unexpected number of peers", l) t.Fatal("unexpected number of peers", l)
@ -70,7 +77,7 @@ func WaitConnected(nodeA, nodeB *Core) bool {
// It may take up to 3 seconds, but let's wait 5. // It may take up to 3 seconds, but let's wait 5.
for i := 0; i < 50; i++ { for i := 0; i < 50; i++ {
time.Sleep(100 * time.Millisecond) time.Sleep(100 * time.Millisecond)
if len(nodeA.GetSwitchPeers()) > 0 && len(nodeB.GetSwitchPeers()) > 0 { if len(nodeA.GetPeers()) > 0 && len(nodeB.GetPeers()) > 0 {
return true return true
} }
} }
@ -80,26 +87,12 @@ func WaitConnected(nodeA, nodeB *Core) bool {
// CreateEchoListener creates a routine listening on nodeA. It expects repeats messages of length bufLen. // CreateEchoListener creates a routine listening on nodeA. It expects repeats messages of length bufLen.
// It returns a channel used to synchronize the routine with caller. // It returns a channel used to synchronize the routine with caller.
func CreateEchoListener(t testing.TB, nodeA *Core, bufLen int, repeats int) chan struct{} { func CreateEchoListener(t testing.TB, nodeA *Core, bufLen int, repeats int) chan struct{} {
// Listen. Doing it here guarantees that there will be something to try to connect when it returns.
listener, err := nodeA.ConnListen()
if err != nil {
t.Fatal(err)
}
// Start routine // Start routine
done := make(chan struct{}) done := make(chan struct{})
go func() { go func() {
defer listener.Close()
conn, err := listener.Accept()
if err != nil {
t.Error(err)
return
}
defer conn.Close()
buf := make([]byte, bufLen) buf := make([]byte, bufLen)
for i := 0; i < repeats; i++ { for i := 0; i < repeats; i++ {
n, err := conn.Read(buf) n, from, err := nodeA.ReadFrom(buf)
if err != nil { if err != nil {
t.Error(err) t.Error(err)
return return
@ -108,7 +101,7 @@ func CreateEchoListener(t testing.TB, nodeA *Core, bufLen int, repeats int) chan
t.Error("missing data") t.Error("missing data")
return return
} }
_, err = conn.Write(buf) _, err = nodeA.WriteTo(buf, from)
if err != nil { if err != nil {
t.Error(err) t.Error(err)
} }
@ -127,6 +120,8 @@ func TestCore_Start_Connect(t *testing.T) {
// TestCore_Start_Transfer checks that messages can be passed between nodes (in both directions). // TestCore_Start_Transfer checks that messages can be passed between nodes (in both directions).
func TestCore_Start_Transfer(t *testing.T) { func TestCore_Start_Transfer(t *testing.T) {
nodeA, nodeB := CreateAndConnectTwo(t, true) nodeA, nodeB := CreateAndConnectTwo(t, true)
defer nodeA.Stop()
defer nodeB.Stop()
msgLen := 1500 msgLen := 1500
done := CreateEchoListener(t, nodeA, msgLen, 1) done := CreateEchoListener(t, nodeA, msgLen, 1)
@ -135,24 +130,15 @@ func TestCore_Start_Transfer(t *testing.T) {
t.Fatal("nodes did not connect") t.Fatal("nodes did not connect")
} }
// Dial // Send
dialer, err := nodeB.ConnDialer()
if err != nil {
t.Fatal(err)
}
conn, err := dialer.Dial("nodeid", nodeA.NodeID().String())
if err != nil {
t.Fatal(err)
}
defer conn.Close()
msg := make([]byte, msgLen) msg := make([]byte, msgLen)
rand.Read(msg) rand.Read(msg)
conn.Write(msg) _, err := nodeB.WriteTo(msg, nodeA.LocalAddr())
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
buf := make([]byte, msgLen) buf := make([]byte, msgLen)
_, err = conn.Read(buf) _, _, err = nodeB.ReadFrom(buf)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
@ -173,16 +159,7 @@ func BenchmarkCore_Start_Transfer(b *testing.B) {
b.Fatal("nodes did not connect") b.Fatal("nodes did not connect")
} }
// Dial // Send
dialer, err := nodeB.ConnDialer()
if err != nil {
b.Fatal(err)
}
conn, err := dialer.Dial("nodeid", nodeA.NodeID().String())
if err != nil {
b.Fatal(err)
}
defer conn.Close()
msg := make([]byte, msgLen) msg := make([]byte, msgLen)
rand.Read(msg) rand.Read(msg)
buf := make([]byte, msgLen) buf := make([]byte, msgLen)
@ -191,11 +168,11 @@ func BenchmarkCore_Start_Transfer(b *testing.B) {
b.ResetTimer() b.ResetTimer()
for i := 0; i < b.N; i++ { for i := 0; i < b.N; i++ {
conn.Write(msg) _, err := nodeB.WriteTo(msg, nodeA.LocalAddr())
if err != nil { if err != nil {
b.Fatal(err) b.Fatal(err)
} }
_, err = conn.Read(buf) _, _, err = nodeB.ReadFrom(buf)
if err != nil { if err != nil {
b.Fatal(err) b.Fatal(err)
} }

View File

@ -187,7 +187,7 @@ func (intf *link) handler() (chan struct{}, error) {
var key keyArray var key keyArray
copy(key[:], meta.key) copy(key[:], meta.key)
if _, allowed := pinned[key]; !allowed { if _, allowed := pinned[key]; !allowed {
intf.links.core.log.Errorf("Failed to connect to node: %q sent ed25519 key that does not match pinned keys", intf.name) intf.links.core.log.Errorf("Failed to connect to node: %q sent ed25519 key that does not match pinned keys", intf.name())
return nil, fmt.Errorf("failed to connect: host sent ed25519 key that does not match pinned keys") return nil, fmt.Errorf("failed to connect: host sent ed25519 key that does not match pinned keys")
} }
} }
@ -213,7 +213,7 @@ func (intf *link) handler() (chan struct{}, error) {
intf.links.mutex.Unlock() intf.links.mutex.Unlock()
// FIXME we should really return an error and let the caller block instead // FIXME we should really return an error and let the caller block instead
// That lets them do things like close connections on its own, avoid printing a connection message in the first place, etc. // That lets them do things like close connections on its own, avoid printing a connection message in the first place, etc.
intf.links.core.log.Debugln("DEBUG: found existing interface for", intf.name) intf.links.core.log.Debugln("DEBUG: found existing interface for", intf.name())
return oldIntf.closed, nil return oldIntf.closed, nil
} else { } else {
intf.closed = make(chan struct{}) intf.closed = make(chan struct{})
@ -224,7 +224,7 @@ func (intf *link) handler() (chan struct{}, error) {
intf.links.mutex.Unlock() intf.links.mutex.Unlock()
close(intf.closed) close(intf.closed)
}() }()
intf.links.core.log.Debugln("DEBUG: registered interface for", intf.name) intf.links.core.log.Debugln("DEBUG: registered interface for", intf.name())
} }
intf.links.mutex.Unlock() intf.links.mutex.Unlock()
themAddr := address.AddrForKey(ed25519.PublicKey(intf.info.key[:])) themAddr := address.AddrForKey(ed25519.PublicKey(intf.info.key[:]))