From fffbbbcbd32923dc1219af4f7dfaeb664ca223de Mon Sep 17 00:00:00 2001 From: Aleksander Mistewicz Date: Wed, 25 Sep 2019 20:07:36 +0200 Subject: [PATCH] Pass message between nodes --- src/yggdrasil/core_test.go | 85 +++++++++++++++++++++++++++++++++++++- 1 file changed, 84 insertions(+), 1 deletion(-) diff --git a/src/yggdrasil/core_test.go b/src/yggdrasil/core_test.go index 4b283d4d..5bca94ff 100644 --- a/src/yggdrasil/core_test.go +++ b/src/yggdrasil/core_test.go @@ -1,8 +1,11 @@ package yggdrasil import ( + "bytes" + "math/rand" "os" "testing" + "time" "github.com/gologme/log" @@ -27,7 +30,7 @@ func GetLoggerWithPrefix(prefix string) *log.Logger { return l } -func TestCore_Start(t *testing.T) { +func CreateAndConnectTwo(t *testing.T) (*Core, *Core) { nodeA := Core{} _, err := nodeA.Start(GenerateConfig(), GetLoggerWithPrefix("A: ")) if err != nil { @@ -44,4 +47,84 @@ func TestCore_Start(t *testing.T) { if err != nil { t.Fatal(err) } + + if l := len(nodeA.GetPeers()); l != 1 { + t.Fatal("unexpected number of peers", l) + } + if l := len(nodeB.GetPeers()); l != 1 { + t.Fatal("unexpected number of peers", l) + } + + return &nodeA, &nodeB +} + +func TestCore_Start_Connect(t *testing.T) { + CreateAndConnectTwo(t) +} + +func TestCore_Start_Transfer(t *testing.T) { + nodeA, nodeB := CreateAndConnectTwo(t) + + // Listen + listener, err := nodeA.ConnListen() + if err != nil { + t.Fatal(err) + } + defer listener.Close() + + done := make(chan struct{}) + go func() { + conn, err := listener.Accept() + if err != nil { + t.Error(err) + return + } + defer conn.Close() + buf := make([]byte, 64) + n, err := conn.Read(buf) + if err != nil { + t.Error(err) + return + } + if n != 64 { + t.Error("missing data") + return + } + _, err = conn.Write(buf) + if err != nil { + t.Error(err) + } + done <- struct{}{} + }() + + time.Sleep(3 * time.Second) // FIXME + // Dial + dialer, err := nodeB.ConnDialer() + if err != nil { + t.Fatal(err) + } + t.Log(nodeA.GetSwitchPeers()) + t.Log(nodeB.GetSwitchPeers()) + t.Log(nodeA.GetSessions()) + t.Log(nodeB.GetSessions()) + conn, err := dialer.Dial("nodeid", nodeA.NodeID().String()) + if err != nil { + t.Fatal(err) + } + defer conn.Close() + msg := make([]byte, 64) + rand.Read(msg) + conn.Write(msg) + if err != nil { + t.Fatal(err) + } + buf := make([]byte, 64) + _, err = conn.Read(buf) + if err != nil { + t.Fatal(err) + } + if bytes.Compare(msg, buf) != 0 { + t.Fatal("expected echo") + } + <-done }