tree6_test.go 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. package cidr
  2. import (
  3. "encoding/binary"
  4. "net"
  5. "testing"
  6. "github.com/stretchr/testify/assert"
  7. )
  8. func TestCIDR6Tree_MostSpecificContains(t *testing.T) {
  9. tree := NewTree6[string]()
  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.1/24"), "4a")
  14. tree.AddCIDR(Parse("4.1.1.1/30"), "4b")
  15. tree.AddCIDR(Parse("4.1.1.1/32"), "4c")
  16. tree.AddCIDR(Parse("254.0.0.0/4"), "5")
  17. tree.AddCIDR(Parse("1:2:0:4:5:0:0:0/64"), "6a")
  18. tree.AddCIDR(Parse("1:2:0:4:5:0:0:0/80"), "6b")
  19. tree.AddCIDR(Parse("1:2:0:4:5:0:0:0/96"), "6c")
  20. tests := []struct {
  21. Found bool
  22. Result interface{}
  23. IP string
  24. }{
  25. {true, "1", "1.0.0.0"},
  26. {true, "1", "1.255.255.255"},
  27. {true, "2", "2.1.0.0"},
  28. {true, "2", "2.1.255.255"},
  29. {true, "3", "3.1.1.0"},
  30. {true, "3", "3.1.1.255"},
  31. {true, "4a", "4.1.1.255"},
  32. {true, "4b", "4.1.1.2"},
  33. {true, "4c", "4.1.1.1"},
  34. {true, "5", "240.0.0.0"},
  35. {true, "5", "255.255.255.255"},
  36. {true, "6a", "1:2:0:4:1:1:1:1"},
  37. {true, "6b", "1:2:0:4:5:1:1:1"},
  38. {true, "6c", "1:2:0:4:5:0:0:0"},
  39. {false, "", "239.0.0.0"},
  40. {false, "", "4.1.2.2"},
  41. }
  42. for _, tt := range tests {
  43. ok, r := tree.MostSpecificContains(net.ParseIP(tt.IP))
  44. assert.Equal(t, tt.Found, ok)
  45. assert.Equal(t, tt.Result, r)
  46. }
  47. tree = NewTree6[string]()
  48. tree.AddCIDR(Parse("1.1.1.1/0"), "cool")
  49. tree.AddCIDR(Parse("::/0"), "cool6")
  50. ok, r := tree.MostSpecificContains(net.ParseIP("0.0.0.0"))
  51. assert.True(t, ok)
  52. assert.Equal(t, "cool", r)
  53. ok, r = tree.MostSpecificContains(net.ParseIP("255.255.255.255"))
  54. assert.True(t, ok)
  55. assert.Equal(t, "cool", r)
  56. ok, r = tree.MostSpecificContains(net.ParseIP("::"))
  57. assert.True(t, ok)
  58. assert.Equal(t, "cool6", r)
  59. ok, r = tree.MostSpecificContains(net.ParseIP("1:2:3:4:5:6:7:8"))
  60. assert.True(t, ok)
  61. assert.Equal(t, "cool6", r)
  62. }
  63. func TestCIDR6Tree_MostSpecificContainsIpV6(t *testing.T) {
  64. tree := NewTree6[string]()
  65. tree.AddCIDR(Parse("1:2:0:4:5:0:0:0/64"), "6a")
  66. tree.AddCIDR(Parse("1:2:0:4:5:0:0:0/80"), "6b")
  67. tree.AddCIDR(Parse("1:2:0:4:5:0:0:0/96"), "6c")
  68. tests := []struct {
  69. Found bool
  70. Result interface{}
  71. IP string
  72. }{
  73. {true, "6a", "1:2:0:4:1:1:1:1"},
  74. {true, "6b", "1:2:0:4:5:1:1:1"},
  75. {true, "6c", "1:2:0:4:5:0:0:0"},
  76. }
  77. for _, tt := range tests {
  78. ip := net.ParseIP(tt.IP)
  79. hi := binary.BigEndian.Uint64(ip[:8])
  80. lo := binary.BigEndian.Uint64(ip[8:])
  81. ok, r := tree.MostSpecificContainsIpV6(hi, lo)
  82. assert.Equal(t, tt.Found, ok)
  83. assert.Equal(t, tt.Result, r)
  84. }
  85. }