cidr_radix_test.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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_Match(t *testing.T) {
  42. tree := NewCIDRTree()
  43. tree.AddCIDR(getCIDR("4.1.1.0/32"), "1a")
  44. tree.AddCIDR(getCIDR("4.1.1.1/32"), "1b")
  45. tests := []struct {
  46. Result interface{}
  47. IP string
  48. }{
  49. {"1a", "4.1.1.0"},
  50. {"1b", "4.1.1.1"},
  51. }
  52. for _, tt := range tests {
  53. assert.Equal(t, tt.Result, tree.Match(ip2int(net.ParseIP(tt.IP))))
  54. }
  55. tree = NewCIDRTree()
  56. tree.AddCIDR(getCIDR("1.1.1.1/0"), "cool")
  57. assert.Equal(t, "cool", tree.Contains(ip2int(net.ParseIP("0.0.0.0"))))
  58. assert.Equal(t, "cool", tree.Contains(ip2int(net.ParseIP("255.255.255.255"))))
  59. }
  60. func BenchmarkCIDRTree_Contains(b *testing.B) {
  61. tree := NewCIDRTree()
  62. tree.AddCIDR(getCIDR("1.1.0.0/16"), "1")
  63. tree.AddCIDR(getCIDR("1.2.1.1/32"), "1")
  64. tree.AddCIDR(getCIDR("192.2.1.1/32"), "1")
  65. tree.AddCIDR(getCIDR("172.2.1.1/32"), "1")
  66. ip := ip2int(net.ParseIP("1.2.1.1"))
  67. b.Run("found", func(b *testing.B) {
  68. for i := 0; i < b.N; i++ {
  69. tree.Contains(ip)
  70. }
  71. })
  72. ip = ip2int(net.ParseIP("1.2.1.255"))
  73. b.Run("not found", func(b *testing.B) {
  74. for i := 0; i < b.N; i++ {
  75. tree.Contains(ip)
  76. }
  77. })
  78. }
  79. func BenchmarkCIDRTree_Match(b *testing.B) {
  80. tree := NewCIDRTree()
  81. tree.AddCIDR(getCIDR("1.1.0.0/16"), "1")
  82. tree.AddCIDR(getCIDR("1.2.1.1/32"), "1")
  83. tree.AddCIDR(getCIDR("192.2.1.1/32"), "1")
  84. tree.AddCIDR(getCIDR("172.2.1.1/32"), "1")
  85. ip := ip2int(net.ParseIP("1.2.1.1"))
  86. b.Run("found", func(b *testing.B) {
  87. for i := 0; i < b.N; i++ {
  88. tree.Match(ip)
  89. }
  90. })
  91. ip = ip2int(net.ParseIP("1.2.1.255"))
  92. b.Run("not found", func(b *testing.B) {
  93. for i := 0; i < b.N; i++ {
  94. tree.Match(ip)
  95. }
  96. })
  97. }
  98. func getCIDR(s string) *net.IPNet {
  99. _, c, _ := net.ParseCIDR(s)
  100. return c
  101. }