lighthouse_test.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  1. package nebula
  2. import (
  3. "fmt"
  4. "net"
  5. "testing"
  6. "github.com/golang/protobuf/proto"
  7. "github.com/stretchr/testify/assert"
  8. )
  9. //TODO: Add a test to ensure udpAddr is copied and not reused
  10. func TestOldIPv4Only(t *testing.T) {
  11. // This test ensures our new ipv6 enabled LH protobuf IpAndPorts works with the old style to enable backwards compatibility
  12. b := []byte{8, 129, 130, 132, 80, 16, 10}
  13. var m Ip4AndPort
  14. err := proto.Unmarshal(b, &m)
  15. assert.NoError(t, err)
  16. assert.Equal(t, "10.1.1.1", int2ip(m.GetIp()).String())
  17. }
  18. func TestNewLhQuery(t *testing.T) {
  19. myIp := net.ParseIP("192.1.1.1")
  20. myIpint := ip2int(myIp)
  21. // Generating a new lh query should work
  22. a := NewLhQueryByInt(myIpint)
  23. // The result should be a nebulameta protobuf
  24. assert.IsType(t, &NebulaMeta{}, a)
  25. // It should also Marshal fine
  26. b, err := proto.Marshal(a)
  27. assert.Nil(t, err)
  28. // and then Unmarshal fine
  29. n := &NebulaMeta{}
  30. err = proto.Unmarshal(b, n)
  31. assert.Nil(t, err)
  32. }
  33. func Test_lhStaticMapping(t *testing.T) {
  34. l := NewTestLogger()
  35. lh1 := "10.128.0.2"
  36. lh1IP := net.ParseIP(lh1)
  37. udpServer, _ := NewListener(l, "0.0.0.0", 0, true)
  38. meh := NewLightHouse(l, true, &net.IPNet{IP: net.IP{0, 0, 0, 1}, Mask: net.IPMask{255, 255, 255, 255}}, []uint32{ip2int(lh1IP)}, 10, 10003, udpServer, false, 1, false)
  39. meh.AddStaticRemote(ip2int(lh1IP), NewUDPAddr(lh1IP, uint16(4242)))
  40. err := meh.ValidateLHStaticEntries()
  41. assert.Nil(t, err)
  42. lh2 := "10.128.0.3"
  43. lh2IP := net.ParseIP(lh2)
  44. meh = NewLightHouse(l, true, &net.IPNet{IP: net.IP{0, 0, 0, 1}, Mask: net.IPMask{255, 255, 255, 255}}, []uint32{ip2int(lh1IP), ip2int(lh2IP)}, 10, 10003, udpServer, false, 1, false)
  45. meh.AddStaticRemote(ip2int(lh1IP), NewUDPAddr(lh1IP, uint16(4242)))
  46. err = meh.ValidateLHStaticEntries()
  47. assert.EqualError(t, err, "Lighthouse 10.128.0.3 does not have a static_host_map entry")
  48. }
  49. func BenchmarkLighthouseHandleRequest(b *testing.B) {
  50. l := NewTestLogger()
  51. lh1 := "10.128.0.2"
  52. lh1IP := net.ParseIP(lh1)
  53. udpServer, _ := NewListener(l, "0.0.0.0", 0, true)
  54. lh := NewLightHouse(l, true, &net.IPNet{IP: net.IP{0, 0, 0, 1}, Mask: net.IPMask{0, 0, 0, 0}}, []uint32{ip2int(lh1IP)}, 10, 10003, udpServer, false, 1, false)
  55. hAddr := NewUDPAddrFromString("4.5.6.7:12345")
  56. hAddr2 := NewUDPAddrFromString("4.5.6.7:12346")
  57. lh.addrMap[3] = NewRemoteList()
  58. lh.addrMap[3].unlockedSetV4(
  59. 3,
  60. []*Ip4AndPort{
  61. NewIp4AndPort(hAddr.IP, uint32(hAddr.Port)),
  62. NewIp4AndPort(hAddr2.IP, uint32(hAddr2.Port)),
  63. },
  64. func(*Ip4AndPort) bool { return true },
  65. )
  66. rAddr := NewUDPAddrFromString("1.2.2.3:12345")
  67. rAddr2 := NewUDPAddrFromString("1.2.2.3:12346")
  68. lh.addrMap[2] = NewRemoteList()
  69. lh.addrMap[2].unlockedSetV4(
  70. 3,
  71. []*Ip4AndPort{
  72. NewIp4AndPort(rAddr.IP, uint32(rAddr.Port)),
  73. NewIp4AndPort(rAddr2.IP, uint32(rAddr2.Port)),
  74. },
  75. func(*Ip4AndPort) bool { return true },
  76. )
  77. mw := &mockEncWriter{}
  78. b.Run("notfound", func(b *testing.B) {
  79. lhh := lh.NewRequestHandler()
  80. req := &NebulaMeta{
  81. Type: NebulaMeta_HostQuery,
  82. Details: &NebulaMetaDetails{
  83. VpnIp: 4,
  84. Ip4AndPorts: nil,
  85. },
  86. }
  87. p, err := proto.Marshal(req)
  88. assert.NoError(b, err)
  89. for n := 0; n < b.N; n++ {
  90. lhh.HandleRequest(rAddr, 2, p, mw)
  91. }
  92. })
  93. b.Run("found", func(b *testing.B) {
  94. lhh := lh.NewRequestHandler()
  95. req := &NebulaMeta{
  96. Type: NebulaMeta_HostQuery,
  97. Details: &NebulaMetaDetails{
  98. VpnIp: 3,
  99. Ip4AndPorts: nil,
  100. },
  101. }
  102. p, err := proto.Marshal(req)
  103. assert.NoError(b, err)
  104. for n := 0; n < b.N; n++ {
  105. lhh.HandleRequest(rAddr, 2, p, mw)
  106. }
  107. })
  108. }
  109. func TestLighthouse_Memory(t *testing.T) {
  110. l := NewTestLogger()
  111. myUdpAddr0 := &udpAddr{IP: net.ParseIP("10.0.0.2"), Port: 4242}
  112. myUdpAddr1 := &udpAddr{IP: net.ParseIP("192.168.0.2"), Port: 4242}
  113. myUdpAddr2 := &udpAddr{IP: net.ParseIP("172.16.0.2"), Port: 4242}
  114. myUdpAddr3 := &udpAddr{IP: net.ParseIP("100.152.0.2"), Port: 4242}
  115. myUdpAddr4 := &udpAddr{IP: net.ParseIP("24.15.0.2"), Port: 4242}
  116. myUdpAddr5 := &udpAddr{IP: net.ParseIP("192.168.0.2"), Port: 4243}
  117. myUdpAddr6 := &udpAddr{IP: net.ParseIP("192.168.0.2"), Port: 4244}
  118. myUdpAddr7 := &udpAddr{IP: net.ParseIP("192.168.0.2"), Port: 4245}
  119. myUdpAddr8 := &udpAddr{IP: net.ParseIP("192.168.0.2"), Port: 4246}
  120. myUdpAddr9 := &udpAddr{IP: net.ParseIP("192.168.0.2"), Port: 4247}
  121. myUdpAddr10 := &udpAddr{IP: net.ParseIP("192.168.0.2"), Port: 4248}
  122. myUdpAddr11 := &udpAddr{IP: net.ParseIP("192.168.0.2"), Port: 4249}
  123. myVpnIp := ip2int(net.ParseIP("10.128.0.2"))
  124. theirUdpAddr0 := &udpAddr{IP: net.ParseIP("10.0.0.3"), Port: 4242}
  125. theirUdpAddr1 := &udpAddr{IP: net.ParseIP("192.168.0.3"), Port: 4242}
  126. theirUdpAddr2 := &udpAddr{IP: net.ParseIP("172.16.0.3"), Port: 4242}
  127. theirUdpAddr3 := &udpAddr{IP: net.ParseIP("100.152.0.3"), Port: 4242}
  128. theirUdpAddr4 := &udpAddr{IP: net.ParseIP("24.15.0.3"), Port: 4242}
  129. theirVpnIp := ip2int(net.ParseIP("10.128.0.3"))
  130. udpServer, _ := NewListener(l, "0.0.0.0", 0, true)
  131. lh := NewLightHouse(l, true, &net.IPNet{IP: net.IP{10, 128, 0, 1}, Mask: net.IPMask{255, 255, 255, 0}}, []uint32{}, 10, 10003, udpServer, false, 1, false)
  132. lhh := lh.NewRequestHandler()
  133. // Test that my first update responds with just that
  134. newLHHostUpdate(myUdpAddr0, myVpnIp, []*udpAddr{myUdpAddr1, myUdpAddr2}, lhh)
  135. r := newLHHostRequest(myUdpAddr0, myVpnIp, myVpnIp, lhh)
  136. assertIp4InArray(t, r.msg.Details.Ip4AndPorts, myUdpAddr1, myUdpAddr2)
  137. // Ensure we don't accumulate addresses
  138. newLHHostUpdate(myUdpAddr0, myVpnIp, []*udpAddr{myUdpAddr3}, lhh)
  139. r = newLHHostRequest(myUdpAddr0, myVpnIp, myVpnIp, lhh)
  140. assertIp4InArray(t, r.msg.Details.Ip4AndPorts, myUdpAddr3)
  141. // Grow it back to 2
  142. newLHHostUpdate(myUdpAddr0, myVpnIp, []*udpAddr{myUdpAddr1, myUdpAddr4}, lhh)
  143. r = newLHHostRequest(myUdpAddr0, myVpnIp, myVpnIp, lhh)
  144. assertIp4InArray(t, r.msg.Details.Ip4AndPorts, myUdpAddr1, myUdpAddr4)
  145. // Update a different host
  146. newLHHostUpdate(theirUdpAddr0, theirVpnIp, []*udpAddr{theirUdpAddr1, theirUdpAddr2, theirUdpAddr3, theirUdpAddr4}, lhh)
  147. r = newLHHostRequest(theirUdpAddr0, theirVpnIp, myVpnIp, lhh)
  148. assertIp4InArray(t, r.msg.Details.Ip4AndPorts, theirUdpAddr1, theirUdpAddr2, theirUdpAddr3, theirUdpAddr4)
  149. // Make sure we didn't get changed
  150. r = newLHHostRequest(myUdpAddr0, myVpnIp, myVpnIp, lhh)
  151. assertIp4InArray(t, r.msg.Details.Ip4AndPorts, myUdpAddr1, myUdpAddr4)
  152. // Ensure proper ordering and limiting
  153. // Send 12 addrs, get 10 back, the last 2 removed, allowing the duplicate to remain (clients dedupe)
  154. newLHHostUpdate(
  155. myUdpAddr0,
  156. myVpnIp,
  157. []*udpAddr{
  158. myUdpAddr1,
  159. myUdpAddr2,
  160. myUdpAddr3,
  161. myUdpAddr4,
  162. myUdpAddr5,
  163. myUdpAddr5, //Duplicated on purpose
  164. myUdpAddr6,
  165. myUdpAddr7,
  166. myUdpAddr8,
  167. myUdpAddr9,
  168. myUdpAddr10,
  169. myUdpAddr11, // This should get cut
  170. }, lhh)
  171. r = newLHHostRequest(myUdpAddr0, myVpnIp, myVpnIp, lhh)
  172. assertIp4InArray(
  173. t,
  174. r.msg.Details.Ip4AndPorts,
  175. myUdpAddr1, myUdpAddr2, myUdpAddr3, myUdpAddr4, myUdpAddr5, myUdpAddr5, myUdpAddr6, myUdpAddr7, myUdpAddr8, myUdpAddr9,
  176. )
  177. // Make sure we won't add ips in our vpn network
  178. bad1 := &udpAddr{IP: net.ParseIP("10.128.0.99"), Port: 4242}
  179. bad2 := &udpAddr{IP: net.ParseIP("10.128.0.100"), Port: 4242}
  180. good := &udpAddr{IP: net.ParseIP("1.128.0.99"), Port: 4242}
  181. newLHHostUpdate(myUdpAddr0, myVpnIp, []*udpAddr{bad1, bad2, good}, lhh)
  182. r = newLHHostRequest(myUdpAddr0, myVpnIp, myVpnIp, lhh)
  183. assertIp4InArray(t, r.msg.Details.Ip4AndPorts, good)
  184. }
  185. func newLHHostRequest(fromAddr *udpAddr, myVpnIp, queryVpnIp uint32, lhh *LightHouseHandler) testLhReply {
  186. req := &NebulaMeta{
  187. Type: NebulaMeta_HostQuery,
  188. Details: &NebulaMetaDetails{
  189. VpnIp: queryVpnIp,
  190. },
  191. }
  192. b, err := req.Marshal()
  193. if err != nil {
  194. panic(err)
  195. }
  196. w := &testEncWriter{}
  197. lhh.HandleRequest(fromAddr, myVpnIp, b, w)
  198. return w.lastReply
  199. }
  200. func newLHHostUpdate(fromAddr *udpAddr, vpnIp uint32, addrs []*udpAddr, lhh *LightHouseHandler) {
  201. req := &NebulaMeta{
  202. Type: NebulaMeta_HostUpdateNotification,
  203. Details: &NebulaMetaDetails{
  204. VpnIp: vpnIp,
  205. Ip4AndPorts: make([]*Ip4AndPort, len(addrs)),
  206. },
  207. }
  208. for k, v := range addrs {
  209. req.Details.Ip4AndPorts[k] = &Ip4AndPort{Ip: ip2int(v.IP), Port: uint32(v.Port)}
  210. }
  211. b, err := req.Marshal()
  212. if err != nil {
  213. panic(err)
  214. }
  215. w := &testEncWriter{}
  216. lhh.HandleRequest(fromAddr, vpnIp, b, w)
  217. }
  218. //TODO: this is a RemoteList test
  219. //func Test_lhRemoteAllowList(t *testing.T) {
  220. // l := NewTestLogger()
  221. // c := NewConfig(l)
  222. // c.Settings["remoteallowlist"] = map[interface{}]interface{}{
  223. // "10.20.0.0/12": false,
  224. // }
  225. // allowList, err := c.GetAllowList("remoteallowlist", false)
  226. // assert.Nil(t, err)
  227. //
  228. // lh1 := "10.128.0.2"
  229. // lh1IP := net.ParseIP(lh1)
  230. //
  231. // udpServer, _ := NewListener(l, "0.0.0.0", 0, true)
  232. //
  233. // lh := NewLightHouse(l, true, &net.IPNet{IP: net.IP{0, 0, 0, 1}, Mask: net.IPMask{255, 255, 255, 0}}, []uint32{ip2int(lh1IP)}, 10, 10003, udpServer, false, 1, false)
  234. // lh.SetRemoteAllowList(allowList)
  235. //
  236. // // A disallowed ip should not enter the cache but we should end up with an empty entry in the addrMap
  237. // remote1IP := net.ParseIP("10.20.0.3")
  238. // remotes := lh.unlockedGetRemoteList(ip2int(remote1IP))
  239. // remotes.unlockedPrependV4(ip2int(remote1IP), NewIp4AndPort(remote1IP, 4242))
  240. // assert.NotNil(t, lh.addrMap[ip2int(remote1IP)])
  241. // assert.Empty(t, lh.addrMap[ip2int(remote1IP)].CopyAddrs([]*net.IPNet{}))
  242. //
  243. // // Make sure a good ip enters the cache and addrMap
  244. // remote2IP := net.ParseIP("10.128.0.3")
  245. // remote2UDPAddr := NewUDPAddr(remote2IP, uint16(4242))
  246. // lh.addRemoteV4(ip2int(remote2IP), ip2int(remote2IP), NewIp4AndPort(remote2UDPAddr.IP, uint32(remote2UDPAddr.Port)), false, false)
  247. // assertUdpAddrInArray(t, lh.addrMap[ip2int(remote2IP)].CopyAddrs([]*net.IPNet{}), remote2UDPAddr)
  248. //
  249. // // Another good ip gets into the cache, ordering is inverted
  250. // remote3IP := net.ParseIP("10.128.0.4")
  251. // remote3UDPAddr := NewUDPAddr(remote3IP, uint16(4243))
  252. // lh.addRemoteV4(ip2int(remote2IP), ip2int(remote2IP), NewIp4AndPort(remote3UDPAddr.IP, uint32(remote3UDPAddr.Port)), false, false)
  253. // assertUdpAddrInArray(t, lh.addrMap[ip2int(remote2IP)].CopyAddrs([]*net.IPNet{}), remote2UDPAddr, remote3UDPAddr)
  254. //
  255. // // If we exceed the length limit we should only have the most recent addresses
  256. // addedAddrs := []*udpAddr{}
  257. // for i := 0; i < 11; i++ {
  258. // remoteUDPAddr := NewUDPAddr(net.IP{10, 128, 0, 4}, uint16(4243+i))
  259. // lh.addRemoteV4(ip2int(remote2IP), ip2int(remote2IP), NewIp4AndPort(remoteUDPAddr.IP, uint32(remoteUDPAddr.Port)), false, false)
  260. // // The first entry here is a duplicate, don't add it to the assert list
  261. // if i != 0 {
  262. // addedAddrs = append(addedAddrs, remoteUDPAddr)
  263. // }
  264. // }
  265. //
  266. // // We should only have the last 10 of what we tried to add
  267. // assert.True(t, len(addedAddrs) >= 10, "We should have tried to add at least 10 addresses")
  268. // assertUdpAddrInArray(
  269. // t,
  270. // lh.addrMap[ip2int(remote2IP)].CopyAddrs([]*net.IPNet{}),
  271. // addedAddrs[0],
  272. // addedAddrs[1],
  273. // addedAddrs[2],
  274. // addedAddrs[3],
  275. // addedAddrs[4],
  276. // addedAddrs[5],
  277. // addedAddrs[6],
  278. // addedAddrs[7],
  279. // addedAddrs[8],
  280. // addedAddrs[9],
  281. // )
  282. //}
  283. func Test_ipMaskContains(t *testing.T) {
  284. assert.True(t, ipMaskContains(ip2int(net.ParseIP("10.0.0.1")), 32-24, ip2int(net.ParseIP("10.0.0.255"))))
  285. assert.False(t, ipMaskContains(ip2int(net.ParseIP("10.0.0.1")), 32-24, ip2int(net.ParseIP("10.0.1.1"))))
  286. assert.True(t, ipMaskContains(ip2int(net.ParseIP("10.0.0.1")), 32, ip2int(net.ParseIP("10.0.1.1"))))
  287. }
  288. type testLhReply struct {
  289. nebType NebulaMessageType
  290. nebSubType NebulaMessageSubType
  291. vpnIp uint32
  292. msg *NebulaMeta
  293. }
  294. type testEncWriter struct {
  295. lastReply testLhReply
  296. }
  297. func (tw *testEncWriter) SendMessageToVpnIp(t NebulaMessageType, st NebulaMessageSubType, vpnIp uint32, p, _, _ []byte) {
  298. tw.lastReply = testLhReply{
  299. nebType: t,
  300. nebSubType: st,
  301. vpnIp: vpnIp,
  302. msg: &NebulaMeta{},
  303. }
  304. err := proto.Unmarshal(p, tw.lastReply.msg)
  305. if err != nil {
  306. panic(err)
  307. }
  308. }
  309. // assertIp4InArray asserts every address in want is at the same position in have and that the lengths match
  310. func assertIp4InArray(t *testing.T, have []*Ip4AndPort, want ...*udpAddr) {
  311. assert.Len(t, have, len(want))
  312. for k, w := range want {
  313. if !(have[k].Ip == ip2int(w.IP) && have[k].Port == uint32(w.Port)) {
  314. assert.Fail(t, fmt.Sprintf("Response did not contain: %v:%v at %v; %v", w.IP, w.Port, k, translateV4toUdpAddr(have)))
  315. }
  316. }
  317. }
  318. // assertUdpAddrInArray asserts every address in want is at the same position in have and that the lengths match
  319. func assertUdpAddrInArray(t *testing.T, have []*udpAddr, want ...*udpAddr) {
  320. assert.Len(t, have, len(want))
  321. for k, w := range want {
  322. if !(have[k].IP.Equal(w.IP) && have[k].Port == w.Port) {
  323. assert.Fail(t, fmt.Sprintf("Response did not contain: %v at %v; %v", w, k, have))
  324. }
  325. }
  326. }
  327. func translateV4toUdpAddr(ips []*Ip4AndPort) []*udpAddr {
  328. addrs := make([]*udpAddr, len(ips))
  329. for k, v := range ips {
  330. addrs[k] = NewUDPAddrFromLH4(v)
  331. }
  332. return addrs
  333. }