2
0

remote_list_test.go 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. package nebula
  2. import (
  3. "net"
  4. "testing"
  5. "github.com/slackhq/nebula/iputil"
  6. "github.com/stretchr/testify/assert"
  7. )
  8. func TestRemoteList_Rebuild(t *testing.T) {
  9. rl := NewRemoteList()
  10. rl.unlockedSetV4(
  11. 0,
  12. 0,
  13. []*Ip4AndPort{
  14. {Ip: uint32(iputil.Ip2VpnIp(net.ParseIP("70.199.182.92"))), Port: 1475}, // this is duped
  15. {Ip: uint32(iputil.Ip2VpnIp(net.ParseIP("172.17.0.182"))), Port: 10101},
  16. {Ip: uint32(iputil.Ip2VpnIp(net.ParseIP("172.17.1.1"))), Port: 10101}, // this is duped
  17. {Ip: uint32(iputil.Ip2VpnIp(net.ParseIP("172.18.0.1"))), Port: 10101}, // this is duped
  18. {Ip: uint32(iputil.Ip2VpnIp(net.ParseIP("172.18.0.1"))), Port: 10101}, // this is a dupe
  19. {Ip: uint32(iputil.Ip2VpnIp(net.ParseIP("172.19.0.1"))), Port: 10101},
  20. {Ip: uint32(iputil.Ip2VpnIp(net.ParseIP("172.31.0.1"))), Port: 10101},
  21. {Ip: uint32(iputil.Ip2VpnIp(net.ParseIP("172.17.1.1"))), Port: 10101}, // this is a dupe
  22. {Ip: uint32(iputil.Ip2VpnIp(net.ParseIP("70.199.182.92"))), Port: 1476}, // almost dupe of 0 with a diff port
  23. {Ip: uint32(iputil.Ip2VpnIp(net.ParseIP("70.199.182.92"))), Port: 1475}, // this is a dupe
  24. },
  25. func(iputil.VpnIp, *Ip4AndPort) bool { return true },
  26. )
  27. rl.unlockedSetV6(
  28. 1,
  29. 1,
  30. []*Ip6AndPort{
  31. NewIp6AndPort(net.ParseIP("1::1"), 1), // this is duped
  32. NewIp6AndPort(net.ParseIP("1::1"), 2), // almost dupe of 0 with a diff port, also gets duped
  33. NewIp6AndPort(net.ParseIP("1:100::1"), 1),
  34. NewIp6AndPort(net.ParseIP("1::1"), 1), // this is a dupe
  35. NewIp6AndPort(net.ParseIP("1::1"), 2), // this is a dupe
  36. },
  37. func(iputil.VpnIp, *Ip6AndPort) bool { return true },
  38. )
  39. rl.Rebuild([]*net.IPNet{})
  40. assert.Len(t, rl.addrs, 10, "addrs contains too many entries")
  41. // ipv6 first, sorted lexically within
  42. assert.Equal(t, "[1::1]:1", rl.addrs[0].String())
  43. assert.Equal(t, "[1::1]:2", rl.addrs[1].String())
  44. assert.Equal(t, "[1:100::1]:1", rl.addrs[2].String())
  45. // ipv4 last, sorted by public first, then private, lexically within them
  46. assert.Equal(t, "70.199.182.92:1475", rl.addrs[3].String())
  47. assert.Equal(t, "70.199.182.92:1476", rl.addrs[4].String())
  48. assert.Equal(t, "172.17.0.182:10101", rl.addrs[5].String())
  49. assert.Equal(t, "172.17.1.1:10101", rl.addrs[6].String())
  50. assert.Equal(t, "172.18.0.1:10101", rl.addrs[7].String())
  51. assert.Equal(t, "172.19.0.1:10101", rl.addrs[8].String())
  52. assert.Equal(t, "172.31.0.1:10101", rl.addrs[9].String())
  53. // Now ensure we can hoist ipv4 up
  54. _, ipNet, err := net.ParseCIDR("0.0.0.0/0")
  55. assert.NoError(t, err)
  56. rl.Rebuild([]*net.IPNet{ipNet})
  57. assert.Len(t, rl.addrs, 10, "addrs contains too many entries")
  58. // ipv4 first, public then private, lexically within them
  59. assert.Equal(t, "70.199.182.92:1475", rl.addrs[0].String())
  60. assert.Equal(t, "70.199.182.92:1476", rl.addrs[1].String())
  61. assert.Equal(t, "172.17.0.182:10101", rl.addrs[2].String())
  62. assert.Equal(t, "172.17.1.1:10101", rl.addrs[3].String())
  63. assert.Equal(t, "172.18.0.1:10101", rl.addrs[4].String())
  64. assert.Equal(t, "172.19.0.1:10101", rl.addrs[5].String())
  65. assert.Equal(t, "172.31.0.1:10101", rl.addrs[6].String())
  66. // ipv6 last, sorted by public first, then private, lexically within them
  67. assert.Equal(t, "[1::1]:1", rl.addrs[7].String())
  68. assert.Equal(t, "[1::1]:2", rl.addrs[8].String())
  69. assert.Equal(t, "[1:100::1]:1", rl.addrs[9].String())
  70. // Ensure we can hoist a specific ipv4 range over anything else
  71. _, ipNet, err = net.ParseCIDR("172.17.0.0/16")
  72. assert.NoError(t, err)
  73. rl.Rebuild([]*net.IPNet{ipNet})
  74. assert.Len(t, rl.addrs, 10, "addrs contains too many entries")
  75. // Preferred ipv4 first
  76. assert.Equal(t, "172.17.0.182:10101", rl.addrs[0].String())
  77. assert.Equal(t, "172.17.1.1:10101", rl.addrs[1].String())
  78. // ipv6 next
  79. assert.Equal(t, "[1::1]:1", rl.addrs[2].String())
  80. assert.Equal(t, "[1::1]:2", rl.addrs[3].String())
  81. assert.Equal(t, "[1:100::1]:1", rl.addrs[4].String())
  82. // the remaining ipv4 last
  83. assert.Equal(t, "70.199.182.92:1475", rl.addrs[5].String())
  84. assert.Equal(t, "70.199.182.92:1476", rl.addrs[6].String())
  85. assert.Equal(t, "172.18.0.1:10101", rl.addrs[7].String())
  86. assert.Equal(t, "172.19.0.1:10101", rl.addrs[8].String())
  87. assert.Equal(t, "172.31.0.1:10101", rl.addrs[9].String())
  88. }
  89. func BenchmarkFullRebuild(b *testing.B) {
  90. rl := NewRemoteList()
  91. rl.unlockedSetV4(
  92. 0,
  93. 0,
  94. []*Ip4AndPort{
  95. {Ip: uint32(iputil.Ip2VpnIp(net.ParseIP("70.199.182.92"))), Port: 1475},
  96. {Ip: uint32(iputil.Ip2VpnIp(net.ParseIP("172.17.0.182"))), Port: 10101},
  97. {Ip: uint32(iputil.Ip2VpnIp(net.ParseIP("172.17.1.1"))), Port: 10101},
  98. {Ip: uint32(iputil.Ip2VpnIp(net.ParseIP("172.18.0.1"))), Port: 10101},
  99. {Ip: uint32(iputil.Ip2VpnIp(net.ParseIP("172.19.0.1"))), Port: 10101},
  100. {Ip: uint32(iputil.Ip2VpnIp(net.ParseIP("172.31.0.1"))), Port: 10101},
  101. {Ip: uint32(iputil.Ip2VpnIp(net.ParseIP("172.17.1.1"))), Port: 10101}, // this is a dupe
  102. {Ip: uint32(iputil.Ip2VpnIp(net.ParseIP("70.199.182.92"))), Port: 1476}, // dupe of 0 with a diff port
  103. },
  104. func(iputil.VpnIp, *Ip4AndPort) bool { return true },
  105. )
  106. rl.unlockedSetV6(
  107. 0,
  108. 0,
  109. []*Ip6AndPort{
  110. NewIp6AndPort(net.ParseIP("1::1"), 1),
  111. NewIp6AndPort(net.ParseIP("1::1"), 2), // dupe of 0 with a diff port
  112. NewIp6AndPort(net.ParseIP("1:100::1"), 1),
  113. NewIp6AndPort(net.ParseIP("1::1"), 1), // this is a dupe
  114. },
  115. func(iputil.VpnIp, *Ip6AndPort) bool { return true },
  116. )
  117. b.Run("no preferred", func(b *testing.B) {
  118. for i := 0; i < b.N; i++ {
  119. rl.shouldRebuild = true
  120. rl.Rebuild([]*net.IPNet{})
  121. }
  122. })
  123. _, ipNet, err := net.ParseCIDR("172.17.0.0/16")
  124. assert.NoError(b, err)
  125. b.Run("1 preferred", func(b *testing.B) {
  126. for i := 0; i < b.N; i++ {
  127. rl.shouldRebuild = true
  128. rl.Rebuild([]*net.IPNet{ipNet})
  129. }
  130. })
  131. _, ipNet2, err := net.ParseCIDR("70.0.0.0/8")
  132. assert.NoError(b, err)
  133. b.Run("2 preferred", func(b *testing.B) {
  134. for i := 0; i < b.N; i++ {
  135. rl.shouldRebuild = true
  136. rl.Rebuild([]*net.IPNet{ipNet, ipNet2})
  137. }
  138. })
  139. _, ipNet3, err := net.ParseCIDR("0.0.0.0/0")
  140. assert.NoError(b, err)
  141. b.Run("3 preferred", func(b *testing.B) {
  142. for i := 0; i < b.N; i++ {
  143. rl.shouldRebuild = true
  144. rl.Rebuild([]*net.IPNet{ipNet, ipNet2, ipNet3})
  145. }
  146. })
  147. }
  148. func BenchmarkSortRebuild(b *testing.B) {
  149. rl := NewRemoteList()
  150. rl.unlockedSetV4(
  151. 0,
  152. 0,
  153. []*Ip4AndPort{
  154. {Ip: uint32(iputil.Ip2VpnIp(net.ParseIP("70.199.182.92"))), Port: 1475},
  155. {Ip: uint32(iputil.Ip2VpnIp(net.ParseIP("172.17.0.182"))), Port: 10101},
  156. {Ip: uint32(iputil.Ip2VpnIp(net.ParseIP("172.17.1.1"))), Port: 10101},
  157. {Ip: uint32(iputil.Ip2VpnIp(net.ParseIP("172.18.0.1"))), Port: 10101},
  158. {Ip: uint32(iputil.Ip2VpnIp(net.ParseIP("172.19.0.1"))), Port: 10101},
  159. {Ip: uint32(iputil.Ip2VpnIp(net.ParseIP("172.31.0.1"))), Port: 10101},
  160. {Ip: uint32(iputil.Ip2VpnIp(net.ParseIP("172.17.1.1"))), Port: 10101}, // this is a dupe
  161. {Ip: uint32(iputil.Ip2VpnIp(net.ParseIP("70.199.182.92"))), Port: 1476}, // dupe of 0 with a diff port
  162. },
  163. func(iputil.VpnIp, *Ip4AndPort) bool { return true },
  164. )
  165. rl.unlockedSetV6(
  166. 0,
  167. 0,
  168. []*Ip6AndPort{
  169. NewIp6AndPort(net.ParseIP("1::1"), 1),
  170. NewIp6AndPort(net.ParseIP("1::1"), 2), // dupe of 0 with a diff port
  171. NewIp6AndPort(net.ParseIP("1:100::1"), 1),
  172. NewIp6AndPort(net.ParseIP("1::1"), 1), // this is a dupe
  173. },
  174. func(iputil.VpnIp, *Ip6AndPort) bool { return true },
  175. )
  176. b.Run("no preferred", func(b *testing.B) {
  177. for i := 0; i < b.N; i++ {
  178. rl.shouldRebuild = true
  179. rl.Rebuild([]*net.IPNet{})
  180. }
  181. })
  182. _, ipNet, err := net.ParseCIDR("172.17.0.0/16")
  183. rl.Rebuild([]*net.IPNet{ipNet})
  184. assert.NoError(b, err)
  185. b.Run("1 preferred", func(b *testing.B) {
  186. for i := 0; i < b.N; i++ {
  187. rl.Rebuild([]*net.IPNet{ipNet})
  188. }
  189. })
  190. _, ipNet2, err := net.ParseCIDR("70.0.0.0/8")
  191. rl.Rebuild([]*net.IPNet{ipNet, ipNet2})
  192. assert.NoError(b, err)
  193. b.Run("2 preferred", func(b *testing.B) {
  194. for i := 0; i < b.N; i++ {
  195. rl.Rebuild([]*net.IPNet{ipNet, ipNet2})
  196. }
  197. })
  198. _, ipNet3, err := net.ParseCIDR("0.0.0.0/0")
  199. rl.Rebuild([]*net.IPNet{ipNet, ipNet2, ipNet3})
  200. assert.NoError(b, err)
  201. b.Run("3 preferred", func(b *testing.B) {
  202. for i := 0; i < b.N; i++ {
  203. rl.Rebuild([]*net.IPNet{ipNet, ipNet2, ipNet3})
  204. }
  205. })
  206. }