lighthouse_test.go 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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. lh1 := "10.128.0.2"
  55. lh1IP := net.ParseIP(lh1)
  56. udpServer, _ := NewListener("0.0.0.0", 0, true)
  57. meh := NewLightHouse(true, 1, []uint32{ip2int(lh1IP)}, 10, 10003, udpServer, false, 1, false)
  58. meh.AddRemote(ip2int(lh1IP), NewUDPAddr(lh1IP, uint16(4242)), true)
  59. err := meh.ValidateLHStaticEntries()
  60. assert.Nil(t, err)
  61. lh2 := "10.128.0.3"
  62. lh2IP := net.ParseIP(lh2)
  63. meh = NewLightHouse(true, 1, []uint32{ip2int(lh1IP), ip2int(lh2IP)}, 10, 10003, udpServer, false, 1, false)
  64. meh.AddRemote(ip2int(lh1IP), NewUDPAddr(lh1IP, uint16(4242)), true)
  65. err = meh.ValidateLHStaticEntries()
  66. assert.EqualError(t, err, "Lighthouse 10.128.0.3 does not have a static_host_map entry")
  67. }
  68. func BenchmarkLighthouseHandleRequest(b *testing.B) {
  69. lh1 := "10.128.0.2"
  70. lh1IP := net.ParseIP(lh1)
  71. udpServer, _ := NewListener("0.0.0.0", 0, true)
  72. lh := NewLightHouse(true, 1, []uint32{ip2int(lh1IP)}, 10, 10003, udpServer, false, 1, false)
  73. hAddr := NewUDPAddrFromString("4.5.6.7:12345")
  74. hAddr2 := NewUDPAddrFromString("4.5.6.7:12346")
  75. lh.addrMap[3] = []*udpAddr{hAddr, hAddr2}
  76. rAddr := NewUDPAddrFromString("1.2.2.3:12345")
  77. rAddr2 := NewUDPAddrFromString("1.2.2.3:12346")
  78. lh.addrMap[2] = []*udpAddr{rAddr, rAddr2}
  79. mw := &mockEncWriter{}
  80. b.Run("notfound", func(b *testing.B) {
  81. lhh := lh.NewRequestHandler()
  82. req := &NebulaMeta{
  83. Type: NebulaMeta_HostQuery,
  84. Details: &NebulaMetaDetails{
  85. VpnIp: 4,
  86. IpAndPorts: nil,
  87. },
  88. }
  89. p, err := proto.Marshal(req)
  90. assert.NoError(b, err)
  91. for n := 0; n < b.N; n++ {
  92. lhh.HandleRequest(rAddr, 2, p, nil, mw)
  93. }
  94. })
  95. b.Run("found", func(b *testing.B) {
  96. lhh := lh.NewRequestHandler()
  97. req := &NebulaMeta{
  98. Type: NebulaMeta_HostQuery,
  99. Details: &NebulaMetaDetails{
  100. VpnIp: 3,
  101. IpAndPorts: nil,
  102. },
  103. }
  104. p, err := proto.Marshal(req)
  105. assert.NoError(b, err)
  106. for n := 0; n < b.N; n++ {
  107. lhh.HandleRequest(rAddr, 2, p, nil, mw)
  108. }
  109. })
  110. }
  111. func Test_lhRemoteAllowList(t *testing.T) {
  112. c := NewConfig()
  113. c.Settings["remoteallowlist"] = map[interface{}]interface{}{
  114. "10.20.0.0/12": false,
  115. }
  116. allowList, err := c.GetAllowList("remoteallowlist", false)
  117. assert.Nil(t, err)
  118. lh1 := "10.128.0.2"
  119. lh1IP := net.ParseIP(lh1)
  120. udpServer, _ := NewListener("0.0.0.0", 0, true)
  121. lh := NewLightHouse(true, 1, []uint32{ip2int(lh1IP)}, 10, 10003, udpServer, false, 1, false)
  122. lh.SetRemoteAllowList(allowList)
  123. remote1 := "10.20.0.3"
  124. remote1IP := net.ParseIP(remote1)
  125. lh.AddRemote(ip2int(remote1IP), NewUDPAddr(remote1IP, uint16(4242)), true)
  126. assert.Nil(t, lh.addrMap[ip2int(remote1IP)])
  127. remote2 := "10.128.0.3"
  128. remote2IP := net.ParseIP(remote2)
  129. remote2UDPAddr := NewUDPAddr(remote2IP, uint16(4242))
  130. lh.AddRemote(ip2int(remote2IP), remote2UDPAddr, true)
  131. // Make sure the pointers are different but the contents are equal since we are using slices
  132. assert.False(t, remote2UDPAddr == lh.addrMap[ip2int(remote2IP)][0])
  133. assert.Equal(t, remote2UDPAddr, lh.addrMap[ip2int(remote2IP)][0])
  134. }
  135. //func NewLightHouse(amLighthouse bool, myIp uint32, ips []string, interval int, nebulaPort int, pc *udpConn, punchBack bool) *LightHouse {
  136. /*
  137. func TestLHQuery(t *testing.T) {
  138. //n := NewLhQueryByIpString("10.128.0.3")
  139. _, myNet, _ := net.ParseCIDR("10.128.0.0/16")
  140. m := NewHostMap(myNet)
  141. y, _ := net.ResolveUDPAddr("udp", "10.128.0.3:11111")
  142. m.Add(ip2int(net.ParseIP("127.0.0.1")), y)
  143. //t.Errorf("%s", m)
  144. _ = m
  145. _, n, _ := net.ParseCIDR("127.0.0.1/8")
  146. /*udpServer, err := net.ListenUDP("udp", &net.UDPAddr{Port: 10009})
  147. if err != nil {
  148. t.Errorf("%s", err)
  149. }
  150. meh := NewLightHouse(n, m, []string{"10.128.0.2"}, false, 10, 10003, 10004)
  151. //t.Error(m.Hosts)
  152. meh2, err := meh.Query(ip2int(net.ParseIP("10.128.0.3")))
  153. t.Error(err)
  154. if err != nil {
  155. return
  156. }
  157. t.Errorf("%s", meh2)
  158. t.Errorf("%s", n)
  159. }
  160. */