implement host heartbeat, cover unit tests
This commit is contained in:
parent
c237ae0ba5
commit
98d1747c33
1 changed files with 74 additions and 0 deletions
74
tests/lux_host_test.go
Normal file
74
tests/lux_host_test.go
Normal file
|
|
@ -0,0 +1,74 @@
|
||||||
|
package tests
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"lux/crypto"
|
||||||
|
"lux/host"
|
||||||
|
"lux/net"
|
||||||
|
"lux/proto"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
type DummyWANProvider struct{}
|
||||||
|
|
||||||
|
func (*DummyWANProvider) Provide() (host.LuxOption, error) {
|
||||||
|
return &host.LuxOptionWAN{
|
||||||
|
Addr4: []byte{1, 2, 3, 4},
|
||||||
|
}, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestHeartbeatMulticast(t *testing.T) {
|
||||||
|
ks := crypto.NewLuxKeyStore("/tmp/keystore.dat")
|
||||||
|
|
||||||
|
keyNode, _ := crypto.NewLuxKey(proto.LuxTypeNode)
|
||||||
|
ks.Put(keyNode)
|
||||||
|
keyHost, _ := crypto.NewLuxKey(proto.LuxTypeHost)
|
||||||
|
ks.Put(keyHost)
|
||||||
|
|
||||||
|
nodeA := net.NewLuxRouter(keyNode, ks)
|
||||||
|
nodeA.CreateInboundChannel(net.LuxChannelInterior, "127.0.0.1:9979")
|
||||||
|
nodeA.Start()
|
||||||
|
defer nodeA.Stop()
|
||||||
|
|
||||||
|
nodeB := net.NewLuxRouter(keyNode, ks)
|
||||||
|
nodeB.CreateInboundChannel(net.LuxChannelInterior, "127.0.0.2:9979")
|
||||||
|
nodeB.Start()
|
||||||
|
defer nodeB.Stop()
|
||||||
|
|
||||||
|
hostA := host.NewLuxHost("testhost", keyHost, ks)
|
||||||
|
hostA.AddNode(net.LuxChannelInterior, "127.0.0.1:9979")
|
||||||
|
hostA.AddNode(net.LuxChannelInterior, "127.0.0.2:9979")
|
||||||
|
|
||||||
|
hostA.AddOptionProvider(&DummyWANProvider{})
|
||||||
|
if err := hostA.Heartbeat(); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
packetA, err := nodeA.Recv()
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
packetB, err := nodeB.Recv()
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
stateA := host.NewLuxState("testhost")
|
||||||
|
if err := stateA.Read(&packetA.Buffer); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
stateB := host.NewLuxState("testhost")
|
||||||
|
if err := stateB.Read(&packetB.Buffer); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if stateA.Hostname != stateB.Hostname {
|
||||||
|
t.Fatal("stateA.Hostname != stateB.Hostname")
|
||||||
|
}
|
||||||
|
|
||||||
|
//lint:ignore SA1021 cant include net.IP due package name collision
|
||||||
|
if !bytes.Equal(stateA.Options[host.LuxOptionTypeWAN].(*host.LuxOptionWAN).Addr4,
|
||||||
|
stateB.Options[host.LuxOptionTypeWAN].(*host.LuxOptionWAN).Addr4) {
|
||||||
|
t.Fatal("WAN IPs aren't equal")
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Add table
Reference in a new issue