tree4_test.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. package cidr
  2. import (
  3. "net"
  4. "testing"
  5. "github.com/slackhq/nebula/iputil"
  6. "github.com/stretchr/testify/assert"
  7. )
  8. func TestCIDRTree_Contains(t *testing.T) {
  9. tree := NewTree4()
  10. tree.AddCIDR(Parse("1.0.0.0/8"), "1")
  11. tree.AddCIDR(Parse("2.1.0.0/16"), "2")
  12. tree.AddCIDR(Parse("3.1.1.0/24"), "3")
  13. tree.AddCIDR(Parse("4.1.1.0/24"), "4a")
  14. tree.AddCIDR(Parse("4.1.1.1/32"), "4b")
  15. tree.AddCIDR(Parse("4.1.2.1/32"), "4c")
  16. tree.AddCIDR(Parse("254.0.0.0/4"), "5")
  17. tests := []struct {
  18. Result interface{}
  19. IP string
  20. }{
  21. {"1", "1.0.0.0"},
  22. {"1", "1.255.255.255"},
  23. {"2", "2.1.0.0"},
  24. {"2", "2.1.255.255"},
  25. {"3", "3.1.1.0"},
  26. {"3", "3.1.1.255"},
  27. {"4a", "4.1.1.255"},
  28. {"4a", "4.1.1.1"},
  29. {"5", "240.0.0.0"},
  30. {"5", "255.255.255.255"},
  31. {nil, "239.0.0.0"},
  32. {nil, "4.1.2.2"},
  33. }
  34. for _, tt := range tests {
  35. assert.Equal(t, tt.Result, tree.Contains(iputil.Ip2VpnIp(net.ParseIP(tt.IP))))
  36. }
  37. tree = NewTree4()
  38. tree.AddCIDR(Parse("1.1.1.1/0"), "cool")
  39. assert.Equal(t, "cool", tree.Contains(iputil.Ip2VpnIp(net.ParseIP("0.0.0.0"))))
  40. assert.Equal(t, "cool", tree.Contains(iputil.Ip2VpnIp(net.ParseIP("255.255.255.255"))))
  41. }
  42. func TestCIDRTree_MostSpecificContains(t *testing.T) {
  43. tree := NewTree4()
  44. tree.AddCIDR(Parse("1.0.0.0/8"), "1")
  45. tree.AddCIDR(Parse("2.1.0.0/16"), "2")
  46. tree.AddCIDR(Parse("3.1.1.0/24"), "3")
  47. tree.AddCIDR(Parse("4.1.1.0/24"), "4a")
  48. tree.AddCIDR(Parse("4.1.1.0/30"), "4b")
  49. tree.AddCIDR(Parse("4.1.1.1/32"), "4c")
  50. tree.AddCIDR(Parse("254.0.0.0/4"), "5")
  51. tests := []struct {
  52. Result interface{}
  53. IP string
  54. }{
  55. {"1", "1.0.0.0"},
  56. {"1", "1.255.255.255"},
  57. {"2", "2.1.0.0"},
  58. {"2", "2.1.255.255"},
  59. {"3", "3.1.1.0"},
  60. {"3", "3.1.1.255"},
  61. {"4a", "4.1.1.255"},
  62. {"4b", "4.1.1.2"},
  63. {"4c", "4.1.1.1"},
  64. {"5", "240.0.0.0"},
  65. {"5", "255.255.255.255"},
  66. {nil, "239.0.0.0"},
  67. {nil, "4.1.2.2"},
  68. }
  69. for _, tt := range tests {
  70. assert.Equal(t, tt.Result, tree.MostSpecificContains(iputil.Ip2VpnIp(net.ParseIP(tt.IP))))
  71. }
  72. tree = NewTree4()
  73. tree.AddCIDR(Parse("1.1.1.1/0"), "cool")
  74. assert.Equal(t, "cool", tree.MostSpecificContains(iputil.Ip2VpnIp(net.ParseIP("0.0.0.0"))))
  75. assert.Equal(t, "cool", tree.MostSpecificContains(iputil.Ip2VpnIp(net.ParseIP("255.255.255.255"))))
  76. }
  77. func TestCIDRTree_Match(t *testing.T) {
  78. tree := NewTree4()
  79. tree.AddCIDR(Parse("4.1.1.0/32"), "1a")
  80. tree.AddCIDR(Parse("4.1.1.1/32"), "1b")
  81. tests := []struct {
  82. Result interface{}
  83. IP string
  84. }{
  85. {"1a", "4.1.1.0"},
  86. {"1b", "4.1.1.1"},
  87. }
  88. for _, tt := range tests {
  89. assert.Equal(t, tt.Result, tree.Match(iputil.Ip2VpnIp(net.ParseIP(tt.IP))))
  90. }
  91. tree = NewTree4()
  92. tree.AddCIDR(Parse("1.1.1.1/0"), "cool")
  93. assert.Equal(t, "cool", tree.Contains(iputil.Ip2VpnIp(net.ParseIP("0.0.0.0"))))
  94. assert.Equal(t, "cool", tree.Contains(iputil.Ip2VpnIp(net.ParseIP("255.255.255.255"))))
  95. }
  96. func BenchmarkCIDRTree_Contains(b *testing.B) {
  97. tree := NewTree4()
  98. tree.AddCIDR(Parse("1.1.0.0/16"), "1")
  99. tree.AddCIDR(Parse("1.2.1.1/32"), "1")
  100. tree.AddCIDR(Parse("192.2.1.1/32"), "1")
  101. tree.AddCIDR(Parse("172.2.1.1/32"), "1")
  102. ip := iputil.Ip2VpnIp(net.ParseIP("1.2.1.1"))
  103. b.Run("found", func(b *testing.B) {
  104. for i := 0; i < b.N; i++ {
  105. tree.Contains(ip)
  106. }
  107. })
  108. ip = iputil.Ip2VpnIp(net.ParseIP("1.2.1.255"))
  109. b.Run("not found", func(b *testing.B) {
  110. for i := 0; i < b.N; i++ {
  111. tree.Contains(ip)
  112. }
  113. })
  114. }
  115. func BenchmarkCIDRTree_Match(b *testing.B) {
  116. tree := NewTree4()
  117. tree.AddCIDR(Parse("1.1.0.0/16"), "1")
  118. tree.AddCIDR(Parse("1.2.1.1/32"), "1")
  119. tree.AddCIDR(Parse("192.2.1.1/32"), "1")
  120. tree.AddCIDR(Parse("172.2.1.1/32"), "1")
  121. ip := iputil.Ip2VpnIp(net.ParseIP("1.2.1.1"))
  122. b.Run("found", func(b *testing.B) {
  123. for i := 0; i < b.N; i++ {
  124. tree.Match(ip)
  125. }
  126. })
  127. ip = iputil.Ip2VpnIp(net.ParseIP("1.2.1.255"))
  128. b.Run("not found", func(b *testing.B) {
  129. for i := 0; i < b.N; i++ {
  130. tree.Match(ip)
  131. }
  132. })
  133. }