cidr_radix_test.go 3.8 KB

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