lighthouse_test.go 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. package nebula
  2. import (
  3. "net"
  4. "testing"
  5. "github.com/golang/protobuf/proto"
  6. "github.com/stretchr/testify/assert"
  7. )
  8. //TODO: Add a test to ensure udpAddr is copied and not reused
  9. func TestOldIPv4Only(t *testing.T) {
  10. // This test ensures our new ipv6 enabled LH protobuf IpAndPorts works with the old style to enable backwards compatibility
  11. b := []byte{8, 129, 130, 132, 80, 16, 10}
  12. var m IpAndPort
  13. err := proto.Unmarshal(b, &m)
  14. assert.NoError(t, err)
  15. assert.Equal(t, "10.1.1.1", int2ip(m.GetIp()).String())
  16. }
  17. func TestNewLhQuery(t *testing.T) {
  18. myIp := net.ParseIP("192.1.1.1")
  19. myIpint := ip2int(myIp)
  20. // Generating a new lh query should work
  21. a := NewLhQueryByInt(myIpint)
  22. // The result should be a nebulameta protobuf
  23. assert.IsType(t, &NebulaMeta{}, a)
  24. // It should also Marshal fine
  25. b, err := proto.Marshal(a)
  26. assert.Nil(t, err)
  27. // and then Unmarshal fine
  28. n := &NebulaMeta{}
  29. err = proto.Unmarshal(b, n)
  30. assert.Nil(t, err)
  31. }
  32. func TestNewipandportfromudpaddr(t *testing.T) {
  33. blah := NewUDPAddrFromString("1.2.2.3:12345")
  34. meh := NewIpAndPortFromUDPAddr(blah)
  35. assert.Equal(t, uint32(16908803), meh.v4.Ip)
  36. assert.Equal(t, uint32(12345), meh.v4.Port)
  37. }
  38. func TestSetipandportsfromudpaddrs(t *testing.T) {
  39. blah := NewUDPAddrFromString("1.2.2.3:12345")
  40. blah2 := NewUDPAddrFromString("9.9.9.9:47828")
  41. group := []*udpAddr{blah, blah2}
  42. var lh *LightHouse
  43. lhh := lh.NewRequestHandler()
  44. result := lhh.setIpAndPortsFromNetIps(group)
  45. assert.IsType(t, []*ip4Or6{}, result)
  46. assert.Len(t, result, 2)
  47. assert.Equal(t, uint32(0x01020203), result[0].v4.Ip)
  48. assert.Equal(t, uint32(12345), result[0].v4.Port)
  49. assert.Equal(t, uint32(0x09090909), result[1].v4.Ip)
  50. assert.Equal(t, uint32(47828), result[1].v4.Port)
  51. //t.Error(reflect.TypeOf(hah))
  52. }
  53. func Test_lhStaticMapping(t *testing.T) {
  54. l := NewTestLogger()
  55. lh1 := "10.128.0.2"
  56. lh1IP := net.ParseIP(lh1)
  57. udpServer, _ := NewListener(l, "0.0.0.0", 0, true)
  58. meh := NewLightHouse(l, true, 1, []uint32{ip2int(lh1IP)}, 10, 10003, udpServer, false, 1, false)
  59. meh.AddRemote(ip2int(lh1IP), NewUDPAddr(lh1IP, uint16(4242)), true)
  60. err := meh.ValidateLHStaticEntries()
  61. assert.Nil(t, err)
  62. lh2 := "10.128.0.3"
  63. lh2IP := net.ParseIP(lh2)
  64. meh = NewLightHouse(l, true, 1, []uint32{ip2int(lh1IP), ip2int(lh2IP)}, 10, 10003, udpServer, false, 1, false)
  65. meh.AddRemote(ip2int(lh1IP), NewUDPAddr(lh1IP, uint16(4242)), true)
  66. err = meh.ValidateLHStaticEntries()
  67. assert.EqualError(t, err, "Lighthouse 10.128.0.3 does not have a static_host_map entry")
  68. }
  69. func BenchmarkLighthouseHandleRequest(b *testing.B) {
  70. l := NewTestLogger()
  71. lh1 := "10.128.0.2"
  72. lh1IP := net.ParseIP(lh1)
  73. udpServer, _ := NewListener(l, "0.0.0.0", 0, true)
  74. lh := NewLightHouse(l, true, 1, []uint32{ip2int(lh1IP)}, 10, 10003, udpServer, false, 1, false)
  75. hAddr := NewUDPAddrFromString("4.5.6.7:12345")
  76. hAddr2 := NewUDPAddrFromString("4.5.6.7:12346")
  77. lh.addrMap[3] = []*udpAddr{hAddr, hAddr2}
  78. rAddr := NewUDPAddrFromString("1.2.2.3:12345")
  79. rAddr2 := NewUDPAddrFromString("1.2.2.3:12346")
  80. lh.addrMap[2] = []*udpAddr{rAddr, rAddr2}
  81. mw := &mockEncWriter{}
  82. b.Run("notfound", func(b *testing.B) {
  83. lhh := lh.NewRequestHandler()
  84. req := &NebulaMeta{
  85. Type: NebulaMeta_HostQuery,
  86. Details: &NebulaMetaDetails{
  87. VpnIp: 4,
  88. IpAndPorts: nil,
  89. },
  90. }
  91. p, err := proto.Marshal(req)
  92. assert.NoError(b, err)
  93. for n := 0; n < b.N; n++ {
  94. lhh.HandleRequest(rAddr, 2, p, nil, mw)
  95. }
  96. })
  97. b.Run("found", func(b *testing.B) {
  98. lhh := lh.NewRequestHandler()
  99. req := &NebulaMeta{
  100. Type: NebulaMeta_HostQuery,
  101. Details: &NebulaMetaDetails{
  102. VpnIp: 3,
  103. IpAndPorts: nil,
  104. },
  105. }
  106. p, err := proto.Marshal(req)
  107. assert.NoError(b, err)
  108. for n := 0; n < b.N; n++ {
  109. lhh.HandleRequest(rAddr, 2, p, nil, mw)
  110. }
  111. })
  112. }
  113. func Test_lhRemoteAllowList(t *testing.T) {
  114. l := NewTestLogger()
  115. c := NewConfig(l)
  116. c.Settings["remoteallowlist"] = map[interface{}]interface{}{
  117. "10.20.0.0/12": false,
  118. }
  119. allowList, err := c.GetAllowList("remoteallowlist", false)
  120. assert.Nil(t, err)
  121. lh1 := "10.128.0.2"
  122. lh1IP := net.ParseIP(lh1)
  123. udpServer, _ := NewListener(l, "0.0.0.0", 0, true)
  124. lh := NewLightHouse(l, true, 1, []uint32{ip2int(lh1IP)}, 10, 10003, udpServer, false, 1, false)
  125. lh.SetRemoteAllowList(allowList)
  126. remote1 := "10.20.0.3"
  127. remote1IP := net.ParseIP(remote1)
  128. lh.AddRemote(ip2int(remote1IP), NewUDPAddr(remote1IP, uint16(4242)), true)
  129. assert.Nil(t, lh.addrMap[ip2int(remote1IP)])
  130. remote2 := "10.128.0.3"
  131. remote2IP := net.ParseIP(remote2)
  132. remote2UDPAddr := NewUDPAddr(remote2IP, uint16(4242))
  133. lh.AddRemote(ip2int(remote2IP), remote2UDPAddr, true)
  134. // Make sure the pointers are different but the contents are equal since we are using slices
  135. assert.False(t, remote2UDPAddr == lh.addrMap[ip2int(remote2IP)][0])
  136. assert.Equal(t, remote2UDPAddr, lh.addrMap[ip2int(remote2IP)][0])
  137. }
  138. //func NewLightHouse(amLighthouse bool, myIp uint32, ips []string, interval int, nebulaPort int, pc *udpConn, punchBack bool) *LightHouse {
  139. /*
  140. func TestLHQuery(t *testing.T) {
  141. //n := NewLhQueryByIpString("10.128.0.3")
  142. _, myNet, _ := net.ParseCIDR("10.128.0.0/16")
  143. m := NewHostMap(myNet)
  144. y, _ := net.ResolveUDPAddr("udp", "10.128.0.3:11111")
  145. m.Add(ip2int(net.ParseIP("127.0.0.1")), y)
  146. //t.Errorf("%s", m)
  147. _ = m
  148. _, n, _ := net.ParseCIDR("127.0.0.1/8")
  149. /*udpServer, err := net.ListenUDP("udp", &net.UDPAddr{Port: 10009})
  150. if err != nil {
  151. t.Errorf("%s", err)
  152. }
  153. meh := NewLightHouse(n, m, []string{"10.128.0.2"}, false, 10, 10003, 10004)
  154. //t.Error(m.Hosts)
  155. meh2, err := meh.Query(ip2int(net.ParseIP("10.128.0.3")))
  156. t.Error(err)
  157. if err != nil {
  158. return
  159. }
  160. t.Errorf("%s", meh2)
  161. t.Errorf("%s", n)
  162. }
  163. */