diff --git a/tests/lux_host_test.go b/tests/lux_host_test.go new file mode 100644 index 0000000..deccfca --- /dev/null +++ b/tests/lux_host_test.go @@ -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") + } +}