tree4_test.go 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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_List(t *testing.T) {
  9. tree := NewTree4[string]()
  10. tree.AddCIDR(Parse("1.0.0.0/16"), "1")
  11. tree.AddCIDR(Parse("1.0.0.0/8"), "2")
  12. tree.AddCIDR(Parse("1.0.0.0/16"), "3")
  13. tree.AddCIDR(Parse("1.0.0.0/16"), "4")
  14. list := tree.List()
  15. assert.Len(t, list, 2)
  16. assert.Equal(t, "1.0.0.0/8", list[0].CIDR.String())
  17. assert.Equal(t, "2", list[0].Value)
  18. assert.Equal(t, "1.0.0.0/16", list[1].CIDR.String())
  19. assert.Equal(t, "4", list[1].Value)
  20. }
  21. func TestCIDRTree_Contains(t *testing.T) {
  22. tree := NewTree4[string]()
  23. tree.AddCIDR(Parse("1.0.0.0/8"), "1")
  24. tree.AddCIDR(Parse("2.1.0.0/16"), "2")
  25. tree.AddCIDR(Parse("3.1.1.0/24"), "3")
  26. tree.AddCIDR(Parse("4.1.1.0/24"), "4a")
  27. tree.AddCIDR(Parse("4.1.1.1/32"), "4b")
  28. tree.AddCIDR(Parse("4.1.2.1/32"), "4c")
  29. tree.AddCIDR(Parse("254.0.0.0/4"), "5")
  30. tests := []struct {
  31. Found bool
  32. Result interface{}
  33. IP string
  34. }{
  35. {true, "1", "1.0.0.0"},
  36. {true, "1", "1.255.255.255"},
  37. {true, "2", "2.1.0.0"},
  38. {true, "2", "2.1.255.255"},
  39. {true, "3", "3.1.1.0"},
  40. {true, "3", "3.1.1.255"},
  41. {true, "4a", "4.1.1.255"},
  42. {true, "4a", "4.1.1.1"},
  43. {true, "5", "240.0.0.0"},
  44. {true, "5", "255.255.255.255"},
  45. {false, "", "239.0.0.0"},
  46. {false, "", "4.1.2.2"},
  47. }
  48. for _, tt := range tests {
  49. ok, r := tree.Contains(iputil.Ip2VpnIp(net.ParseIP(tt.IP)))
  50. assert.Equal(t, tt.Found, ok)
  51. assert.Equal(t, tt.Result, r)
  52. }
  53. tree = NewTree4[string]()
  54. tree.AddCIDR(Parse("1.1.1.1/0"), "cool")
  55. ok, r := tree.Contains(iputil.Ip2VpnIp(net.ParseIP("0.0.0.0")))
  56. assert.True(t, ok)
  57. assert.Equal(t, "cool", r)
  58. ok, r = tree.Contains(iputil.Ip2VpnIp(net.ParseIP("255.255.255.255")))
  59. assert.True(t, ok)
  60. assert.Equal(t, "cool", r)
  61. }
  62. func TestCIDRTree_MostSpecificContains(t *testing.T) {
  63. tree := NewTree4[string]()
  64. tree.AddCIDR(Parse("1.0.0.0/8"), "1")
  65. tree.AddCIDR(Parse("2.1.0.0/16"), "2")
  66. tree.AddCIDR(Parse("3.1.1.0/24"), "3")
  67. tree.AddCIDR(Parse("4.1.1.0/24"), "4a")
  68. tree.AddCIDR(Parse("4.1.1.0/30"), "4b")
  69. tree.AddCIDR(Parse("4.1.1.1/32"), "4c")
  70. tree.AddCIDR(Parse("254.0.0.0/4"), "5")
  71. tests := []struct {
  72. Found bool
  73. Result interface{}
  74. IP string
  75. }{
  76. {true, "1", "1.0.0.0"},
  77. {true, "1", "1.255.255.255"},
  78. {true, "2", "2.1.0.0"},
  79. {true, "2", "2.1.255.255"},
  80. {true, "3", "3.1.1.0"},
  81. {true, "3", "3.1.1.255"},
  82. {true, "4a", "4.1.1.255"},
  83. {true, "4b", "4.1.1.2"},
  84. {true, "4c", "4.1.1.1"},
  85. {true, "5", "240.0.0.0"},
  86. {true, "5", "255.255.255.255"},
  87. {false, "", "239.0.0.0"},
  88. {false, "", "4.1.2.2"},
  89. }
  90. for _, tt := range tests {
  91. ok, r := tree.MostSpecificContains(iputil.Ip2VpnIp(net.ParseIP(tt.IP)))
  92. assert.Equal(t, tt.Found, ok)
  93. assert.Equal(t, tt.Result, r)
  94. }
  95. tree = NewTree4[string]()
  96. tree.AddCIDR(Parse("1.1.1.1/0"), "cool")
  97. ok, r := tree.MostSpecificContains(iputil.Ip2VpnIp(net.ParseIP("0.0.0.0")))
  98. assert.True(t, ok)
  99. assert.Equal(t, "cool", r)
  100. ok, r = tree.MostSpecificContains(iputil.Ip2VpnIp(net.ParseIP("255.255.255.255")))
  101. assert.True(t, ok)
  102. assert.Equal(t, "cool", r)
  103. }
  104. func TestCIDRTree_Match(t *testing.T) {
  105. tree := NewTree4[string]()
  106. tree.AddCIDR(Parse("4.1.1.0/32"), "1a")
  107. tree.AddCIDR(Parse("4.1.1.1/32"), "1b")
  108. tests := []struct {
  109. Found bool
  110. Result interface{}
  111. IP string
  112. }{
  113. {true, "1a", "4.1.1.0"},
  114. {true, "1b", "4.1.1.1"},
  115. }
  116. for _, tt := range tests {
  117. ok, r := tree.Match(iputil.Ip2VpnIp(net.ParseIP(tt.IP)))
  118. assert.Equal(t, tt.Found, ok)
  119. assert.Equal(t, tt.Result, r)
  120. }
  121. tree = NewTree4[string]()
  122. tree.AddCIDR(Parse("1.1.1.1/0"), "cool")
  123. ok, r := tree.Contains(iputil.Ip2VpnIp(net.ParseIP("0.0.0.0")))
  124. assert.True(t, ok)
  125. assert.Equal(t, "cool", r)
  126. ok, r = tree.Contains(iputil.Ip2VpnIp(net.ParseIP("255.255.255.255")))
  127. assert.True(t, ok)
  128. assert.Equal(t, "cool", r)
  129. }
  130. func BenchmarkCIDRTree_Contains(b *testing.B) {
  131. tree := NewTree4[string]()
  132. tree.AddCIDR(Parse("1.1.0.0/16"), "1")
  133. tree.AddCIDR(Parse("1.2.1.1/32"), "1")
  134. tree.AddCIDR(Parse("192.2.1.1/32"), "1")
  135. tree.AddCIDR(Parse("172.2.1.1/32"), "1")
  136. ip := iputil.Ip2VpnIp(net.ParseIP("1.2.1.1"))
  137. b.Run("found", func(b *testing.B) {
  138. for i := 0; i < b.N; i++ {
  139. tree.Contains(ip)
  140. }
  141. })
  142. ip = iputil.Ip2VpnIp(net.ParseIP("1.2.1.255"))
  143. b.Run("not found", func(b *testing.B) {
  144. for i := 0; i < b.N; i++ {
  145. tree.Contains(ip)
  146. }
  147. })
  148. }
  149. func BenchmarkCIDRTree_Match(b *testing.B) {
  150. tree := NewTree4[string]()
  151. tree.AddCIDR(Parse("1.1.0.0/16"), "1")
  152. tree.AddCIDR(Parse("1.2.1.1/32"), "1")
  153. tree.AddCIDR(Parse("192.2.1.1/32"), "1")
  154. tree.AddCIDR(Parse("172.2.1.1/32"), "1")
  155. ip := iputil.Ip2VpnIp(net.ParseIP("1.2.1.1"))
  156. b.Run("found", func(b *testing.B) {
  157. for i := 0; i < b.N; i++ {
  158. tree.Match(ip)
  159. }
  160. })
  161. ip = iputil.Ip2VpnIp(net.ParseIP("1.2.1.255"))
  162. b.Run("not found", func(b *testing.B) {
  163. for i := 0; i < b.N; i++ {
  164. tree.Match(ip)
  165. }
  166. })
  167. }