lighthouse_test.go 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. package nebula
  2. import (
  3. "net"
  4. "testing"
  5. proto "github.com/golang/protobuf/proto"
  6. "github.com/stretchr/testify/assert"
  7. )
  8. func TestNewLhQuery(t *testing.T) {
  9. myIp := net.ParseIP("192.1.1.1")
  10. myIpint := ip2int(myIp)
  11. // Generating a new lh query should work
  12. a := NewLhQueryByInt(myIpint)
  13. // The result should be a nebulameta protobuf
  14. assert.IsType(t, &NebulaMeta{}, a)
  15. // It should also Marshal fine
  16. b, err := proto.Marshal(a)
  17. assert.Nil(t, err)
  18. // and then Unmarshal fine
  19. n := &NebulaMeta{}
  20. err = proto.Unmarshal(b, n)
  21. assert.Nil(t, err)
  22. }
  23. func TestNewipandportfromudpaddr(t *testing.T) {
  24. blah := NewUDPAddrFromString("1.2.2.3:12345")
  25. meh := NewIpAndPortFromUDPAddr(*blah)
  26. assert.Equal(t, uint32(16908803), meh.Ip)
  27. assert.Equal(t, uint32(12345), meh.Port)
  28. }
  29. func TestNewipandportsfromudpaddrs(t *testing.T) {
  30. blah := NewUDPAddrFromString("1.2.2.3:12345")
  31. blah2 := NewUDPAddrFromString("9.9.9.9:47828")
  32. group := []udpAddr{*blah, *blah2}
  33. hah := NewIpAndPortsFromNetIps(group)
  34. assert.IsType(t, &[]*IpAndPort{}, hah)
  35. //t.Error(reflect.TypeOf(hah))
  36. }
  37. func Test_lhStaticMapping(t *testing.T) {
  38. lh1 := "10.128.0.2"
  39. lh1IP := net.ParseIP(lh1)
  40. udpServer, _ := NewListener("0.0.0.0", 0, true)
  41. meh := NewLightHouse(true, 1, []uint32{ip2int(lh1IP)}, 10, 10003, udpServer, false, 1)
  42. meh.AddRemote(ip2int(lh1IP), NewUDPAddr(ip2int(lh1IP), uint16(4242)), true)
  43. err := meh.ValidateLHStaticEntries()
  44. assert.Nil(t, err)
  45. lh2 := "10.128.0.3"
  46. lh2IP := net.ParseIP(lh2)
  47. meh = NewLightHouse(true, 1, []uint32{ip2int(lh1IP), ip2int(lh2IP)}, 10, 10003, udpServer, false, 1)
  48. meh.AddRemote(ip2int(lh1IP), NewUDPAddr(ip2int(lh1IP), uint16(4242)), true)
  49. err = meh.ValidateLHStaticEntries()
  50. assert.EqualError(t, err, "Lighthouse 10.128.0.3 does not have a static_host_map entry")
  51. }
  52. //func NewLightHouse(amLighthouse bool, myIp uint32, ips []string, interval int, nebulaPort int, pc *udpConn, punchBack bool) *LightHouse {
  53. /*
  54. func TestLHQuery(t *testing.T) {
  55. //n := NewLhQueryByIpString("10.128.0.3")
  56. _, myNet, _ := net.ParseCIDR("10.128.0.0/16")
  57. m := NewHostMap(myNet)
  58. y, _ := net.ResolveUDPAddr("udp", "10.128.0.3:11111")
  59. m.Add(ip2int(net.ParseIP("127.0.0.1")), y)
  60. //t.Errorf("%s", m)
  61. _ = m
  62. _, n, _ := net.ParseCIDR("127.0.0.1/8")
  63. /*udpServer, err := net.ListenUDP("udp", &net.UDPAddr{Port: 10009})
  64. if err != nil {
  65. t.Errorf("%s", err)
  66. }
  67. meh := NewLightHouse(n, m, []string{"10.128.0.2"}, false, 10, 10003, 10004)
  68. //t.Error(m.Hosts)
  69. meh2, err := meh.Query(ip2int(net.ParseIP("10.128.0.3")))
  70. t.Error(err)
  71. if err != nil {
  72. return
  73. }
  74. t.Errorf("%s", meh2)
  75. t.Errorf("%s", n)
  76. }
  77. */