tree4_test.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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()
  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()
  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. Result interface{}
  32. IP string
  33. }{
  34. {"1", "1.0.0.0"},
  35. {"1", "1.255.255.255"},
  36. {"2", "2.1.0.0"},
  37. {"2", "2.1.255.255"},
  38. {"3", "3.1.1.0"},
  39. {"3", "3.1.1.255"},
  40. {"4a", "4.1.1.255"},
  41. {"4a", "4.1.1.1"},
  42. {"5", "240.0.0.0"},
  43. {"5", "255.255.255.255"},
  44. {nil, "239.0.0.0"},
  45. {nil, "4.1.2.2"},
  46. }
  47. for _, tt := range tests {
  48. assert.Equal(t, tt.Result, tree.Contains(iputil.Ip2VpnIp(net.ParseIP(tt.IP))))
  49. }
  50. tree = NewTree4()
  51. tree.AddCIDR(Parse("1.1.1.1/0"), "cool")
  52. assert.Equal(t, "cool", tree.Contains(iputil.Ip2VpnIp(net.ParseIP("0.0.0.0"))))
  53. assert.Equal(t, "cool", tree.Contains(iputil.Ip2VpnIp(net.ParseIP("255.255.255.255"))))
  54. }
  55. func TestCIDRTree_MostSpecificContains(t *testing.T) {
  56. tree := NewTree4()
  57. tree.AddCIDR(Parse("1.0.0.0/8"), "1")
  58. tree.AddCIDR(Parse("2.1.0.0/16"), "2")
  59. tree.AddCIDR(Parse("3.1.1.0/24"), "3")
  60. tree.AddCIDR(Parse("4.1.1.0/24"), "4a")
  61. tree.AddCIDR(Parse("4.1.1.0/30"), "4b")
  62. tree.AddCIDR(Parse("4.1.1.1/32"), "4c")
  63. tree.AddCIDR(Parse("254.0.0.0/4"), "5")
  64. tests := []struct {
  65. Result interface{}
  66. IP string
  67. }{
  68. {"1", "1.0.0.0"},
  69. {"1", "1.255.255.255"},
  70. {"2", "2.1.0.0"},
  71. {"2", "2.1.255.255"},
  72. {"3", "3.1.1.0"},
  73. {"3", "3.1.1.255"},
  74. {"4a", "4.1.1.255"},
  75. {"4b", "4.1.1.2"},
  76. {"4c", "4.1.1.1"},
  77. {"5", "240.0.0.0"},
  78. {"5", "255.255.255.255"},
  79. {nil, "239.0.0.0"},
  80. {nil, "4.1.2.2"},
  81. }
  82. for _, tt := range tests {
  83. assert.Equal(t, tt.Result, tree.MostSpecificContains(iputil.Ip2VpnIp(net.ParseIP(tt.IP))))
  84. }
  85. tree = NewTree4()
  86. tree.AddCIDR(Parse("1.1.1.1/0"), "cool")
  87. assert.Equal(t, "cool", tree.MostSpecificContains(iputil.Ip2VpnIp(net.ParseIP("0.0.0.0"))))
  88. assert.Equal(t, "cool", tree.MostSpecificContains(iputil.Ip2VpnIp(net.ParseIP("255.255.255.255"))))
  89. }
  90. func TestCIDRTree_Match(t *testing.T) {
  91. tree := NewTree4()
  92. tree.AddCIDR(Parse("4.1.1.0/32"), "1a")
  93. tree.AddCIDR(Parse("4.1.1.1/32"), "1b")
  94. tests := []struct {
  95. Result interface{}
  96. IP string
  97. }{
  98. {"1a", "4.1.1.0"},
  99. {"1b", "4.1.1.1"},
  100. }
  101. for _, tt := range tests {
  102. assert.Equal(t, tt.Result, tree.Match(iputil.Ip2VpnIp(net.ParseIP(tt.IP))))
  103. }
  104. tree = NewTree4()
  105. tree.AddCIDR(Parse("1.1.1.1/0"), "cool")
  106. assert.Equal(t, "cool", tree.Contains(iputil.Ip2VpnIp(net.ParseIP("0.0.0.0"))))
  107. assert.Equal(t, "cool", tree.Contains(iputil.Ip2VpnIp(net.ParseIP("255.255.255.255"))))
  108. }
  109. func BenchmarkCIDRTree_Contains(b *testing.B) {
  110. tree := NewTree4()
  111. tree.AddCIDR(Parse("1.1.0.0/16"), "1")
  112. tree.AddCIDR(Parse("1.2.1.1/32"), "1")
  113. tree.AddCIDR(Parse("192.2.1.1/32"), "1")
  114. tree.AddCIDR(Parse("172.2.1.1/32"), "1")
  115. ip := iputil.Ip2VpnIp(net.ParseIP("1.2.1.1"))
  116. b.Run("found", func(b *testing.B) {
  117. for i := 0; i < b.N; i++ {
  118. tree.Contains(ip)
  119. }
  120. })
  121. ip = iputil.Ip2VpnIp(net.ParseIP("1.2.1.255"))
  122. b.Run("not found", func(b *testing.B) {
  123. for i := 0; i < b.N; i++ {
  124. tree.Contains(ip)
  125. }
  126. })
  127. }
  128. func BenchmarkCIDRTree_Match(b *testing.B) {
  129. tree := NewTree4()
  130. tree.AddCIDR(Parse("1.1.0.0/16"), "1")
  131. tree.AddCIDR(Parse("1.2.1.1/32"), "1")
  132. tree.AddCIDR(Parse("192.2.1.1/32"), "1")
  133. tree.AddCIDR(Parse("172.2.1.1/32"), "1")
  134. ip := iputil.Ip2VpnIp(net.ParseIP("1.2.1.1"))
  135. b.Run("found", func(b *testing.B) {
  136. for i := 0; i < b.N; i++ {
  137. tree.Match(ip)
  138. }
  139. })
  140. ip = iputil.Ip2VpnIp(net.ParseIP("1.2.1.255"))
  141. b.Run("not found", func(b *testing.B) {
  142. for i := 0; i < b.N; i++ {
  143. tree.Match(ip)
  144. }
  145. })
  146. }