lighthouse_test.go 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. package nebula
  2. import (
  3. "net"
  4. "testing"
  5. "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 TestSetipandportsfromudpaddrs(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. var lh *LightHouse
  34. lhh := lh.NewRequestHandler()
  35. result := lhh.setIpAndPortsFromNetIps(group)
  36. assert.IsType(t, []*IpAndPort{}, result)
  37. assert.Len(t, result, 2)
  38. assert.Equal(t, uint32(0x01020203), result[0].Ip)
  39. assert.Equal(t, uint32(12345), result[0].Port)
  40. assert.Equal(t, uint32(0x09090909), result[1].Ip)
  41. assert.Equal(t, uint32(47828), result[1].Port)
  42. //t.Error(reflect.TypeOf(hah))
  43. }
  44. func Test_lhStaticMapping(t *testing.T) {
  45. lh1 := "10.128.0.2"
  46. lh1IP := net.ParseIP(lh1)
  47. udpServer, _ := NewListener("0.0.0.0", 0, true)
  48. meh := NewLightHouse(true, 1, []uint32{ip2int(lh1IP)}, 10, 10003, udpServer, false, 1, false)
  49. meh.AddRemote(ip2int(lh1IP), NewUDPAddr(ip2int(lh1IP), uint16(4242)), true)
  50. err := meh.ValidateLHStaticEntries()
  51. assert.Nil(t, err)
  52. lh2 := "10.128.0.3"
  53. lh2IP := net.ParseIP(lh2)
  54. meh = NewLightHouse(true, 1, []uint32{ip2int(lh1IP), ip2int(lh2IP)}, 10, 10003, udpServer, false, 1, false)
  55. meh.AddRemote(ip2int(lh1IP), NewUDPAddr(ip2int(lh1IP), uint16(4242)), true)
  56. err = meh.ValidateLHStaticEntries()
  57. assert.EqualError(t, err, "Lighthouse 10.128.0.3 does not have a static_host_map entry")
  58. }
  59. func BenchmarkLighthouseHandleRequest(b *testing.B) {
  60. lh1 := "10.128.0.2"
  61. lh1IP := net.ParseIP(lh1)
  62. udpServer, _ := NewListener("0.0.0.0", 0, true)
  63. lh := NewLightHouse(true, 1, []uint32{ip2int(lh1IP)}, 10, 10003, udpServer, false, 1, false)
  64. hAddr := NewUDPAddrFromString("4.5.6.7:12345")
  65. hAddr2 := NewUDPAddrFromString("4.5.6.7:12346")
  66. lh.addrMap[3] = []udpAddr{*hAddr, *hAddr2}
  67. rAddr := NewUDPAddrFromString("1.2.2.3:12345")
  68. rAddr2 := NewUDPAddrFromString("1.2.2.3:12346")
  69. lh.addrMap[2] = []udpAddr{*rAddr, *rAddr2}
  70. mw := &mockEncWriter{}
  71. b.Run("notfound", func(b *testing.B) {
  72. lhh := lh.NewRequestHandler()
  73. req := &NebulaMeta{
  74. Type: NebulaMeta_HostQuery,
  75. Details: &NebulaMetaDetails{
  76. VpnIp: 4,
  77. IpAndPorts: nil,
  78. },
  79. }
  80. p, err := proto.Marshal(req)
  81. assert.NoError(b, err)
  82. for n := 0; n < b.N; n++ {
  83. lhh.HandleRequest(rAddr, 2, p, nil, mw)
  84. }
  85. })
  86. b.Run("found", func(b *testing.B) {
  87. lhh := lh.NewRequestHandler()
  88. req := &NebulaMeta{
  89. Type: NebulaMeta_HostQuery,
  90. Details: &NebulaMetaDetails{
  91. VpnIp: 3,
  92. IpAndPorts: nil,
  93. },
  94. }
  95. p, err := proto.Marshal(req)
  96. assert.NoError(b, err)
  97. for n := 0; n < b.N; n++ {
  98. lhh.HandleRequest(rAddr, 2, p, nil, mw)
  99. }
  100. })
  101. }
  102. func Test_lhRemoteAllowList(t *testing.T) {
  103. c := NewConfig()
  104. c.Settings["remoteallowlist"] = map[interface{}]interface{}{
  105. "10.20.0.0/12": false,
  106. }
  107. allowList, err := c.GetAllowList("remoteallowlist", false)
  108. assert.Nil(t, err)
  109. lh1 := "10.128.0.2"
  110. lh1IP := net.ParseIP(lh1)
  111. udpServer, _ := NewListener("0.0.0.0", 0, true)
  112. lh := NewLightHouse(true, 1, []uint32{ip2int(lh1IP)}, 10, 10003, udpServer, false, 1, false)
  113. lh.SetRemoteAllowList(allowList)
  114. remote1 := "10.20.0.3"
  115. remote1IP := net.ParseIP(remote1)
  116. lh.AddRemote(ip2int(remote1IP), NewUDPAddr(ip2int(remote1IP), uint16(4242)), true)
  117. assert.Nil(t, lh.addrMap[ip2int(remote1IP)])
  118. remote2 := "10.128.0.3"
  119. remote2IP := net.ParseIP(remote2)
  120. remote2UDPAddr := NewUDPAddr(ip2int(remote2IP), uint16(4242))
  121. lh.AddRemote(ip2int(remote2IP), remote2UDPAddr, true)
  122. assert.Equal(t, remote2UDPAddr, &lh.addrMap[ip2int(remote2IP)][0])
  123. }
  124. //func NewLightHouse(amLighthouse bool, myIp uint32, ips []string, interval int, nebulaPort int, pc *udpConn, punchBack bool) *LightHouse {
  125. /*
  126. func TestLHQuery(t *testing.T) {
  127. //n := NewLhQueryByIpString("10.128.0.3")
  128. _, myNet, _ := net.ParseCIDR("10.128.0.0/16")
  129. m := NewHostMap(myNet)
  130. y, _ := net.ResolveUDPAddr("udp", "10.128.0.3:11111")
  131. m.Add(ip2int(net.ParseIP("127.0.0.1")), y)
  132. //t.Errorf("%s", m)
  133. _ = m
  134. _, n, _ := net.ParseCIDR("127.0.0.1/8")
  135. /*udpServer, err := net.ListenUDP("udp", &net.UDPAddr{Port: 10009})
  136. if err != nil {
  137. t.Errorf("%s", err)
  138. }
  139. meh := NewLightHouse(n, m, []string{"10.128.0.2"}, false, 10, 10003, 10004)
  140. //t.Error(m.Hosts)
  141. meh2, err := meh.Query(ip2int(net.ParseIP("10.128.0.3")))
  142. t.Error(err)
  143. if err != nil {
  144. return
  145. }
  146. t.Errorf("%s", meh2)
  147. t.Errorf("%s", n)
  148. }
  149. */