76 lines
1.8 KiB
Go
76 lines
1.8 KiB
Go
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")
|
|
|
|
keyNodeA, _ := crypto.NewLuxKey(proto.LuxTypeNode)
|
|
ks.Put(keyNodeA)
|
|
keyNodeB, _ := crypto.NewLuxKey(proto.LuxTypeNode)
|
|
ks.Put(keyNodeB)
|
|
keyHost, _ := crypto.NewLuxKey(proto.LuxTypeHost)
|
|
ks.Put(keyHost)
|
|
|
|
nodeA := net.NewLuxRouter(keyNodeA, ks)
|
|
nodeA.CreateInboundChannel(net.LuxChannelExterior, "127.0.0.1:9979")
|
|
nodeA.Start()
|
|
defer nodeA.Stop()
|
|
|
|
nodeB := net.NewLuxRouter(keyNodeB, ks)
|
|
nodeB.CreateInboundChannel(net.LuxChannelExterior, "127.0.0.2:9979")
|
|
nodeB.Start()
|
|
defer nodeB.Stop()
|
|
|
|
hostA := host.NewLuxHost("testhost", keyHost, ks)
|
|
hostA.AddNode(keyNodeA.Id, "127.0.0.1:9979")
|
|
hostA.AddNode(keyNodeB.Id, "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")
|
|
}
|
|
}
|