filters_test.go 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. package gocql
  2. import (
  3. "net"
  4. "testing"
  5. )
  6. func TestFilter_WhiteList(t *testing.T) {
  7. f := WhiteListHostFilter("127.0.0.1", "127.0.0.2")
  8. tests := [...]struct {
  9. addr net.IP
  10. accept bool
  11. }{
  12. {net.ParseIP("127.0.0.1"), true},
  13. {net.ParseIP("127.0.0.2"), true},
  14. {net.ParseIP("127.0.0.3"), false},
  15. }
  16. for i, test := range tests {
  17. if f.Accept(&HostInfo{connectAddress: test.addr}) {
  18. if !test.accept {
  19. t.Errorf("%d: should not have been accepted but was", i)
  20. }
  21. } else if test.accept {
  22. t.Errorf("%d: should have been accepted but wasn't", i)
  23. }
  24. }
  25. }
  26. func TestFilter_AllowAll(t *testing.T) {
  27. f := AcceptAllFilter()
  28. tests := [...]struct {
  29. addr net.IP
  30. accept bool
  31. }{
  32. {net.ParseIP("127.0.0.1"), true},
  33. {net.ParseIP("127.0.0.2"), true},
  34. {net.ParseIP("127.0.0.3"), true},
  35. }
  36. for i, test := range tests {
  37. if f.Accept(&HostInfo{connectAddress: test.addr}) {
  38. if !test.accept {
  39. t.Errorf("%d: should not have been accepted but was", i)
  40. }
  41. } else if test.accept {
  42. t.Errorf("%d: should have been accepted but wasn't", i)
  43. }
  44. }
  45. }
  46. func TestFilter_DenyAll(t *testing.T) {
  47. f := DenyAllFilter()
  48. tests := [...]struct {
  49. addr net.IP
  50. accept bool
  51. }{
  52. {net.ParseIP("127.0.0.1"), false},
  53. {net.ParseIP("127.0.0.2"), false},
  54. {net.ParseIP("127.0.0.3"), false},
  55. }
  56. for i, test := range tests {
  57. if f.Accept(&HostInfo{connectAddress: test.addr}) {
  58. if !test.accept {
  59. t.Errorf("%d: should not have been accepted but was", i)
  60. }
  61. } else if test.accept {
  62. t.Errorf("%d: should have been accepted but wasn't", i)
  63. }
  64. }
  65. }
  66. func TestFilter_DataCentre(t *testing.T) {
  67. f := DataCentreHostFilter("dc1")
  68. tests := [...]struct {
  69. dc string
  70. accept bool
  71. }{
  72. {"dc1", true},
  73. {"dc2", false},
  74. }
  75. for i, test := range tests {
  76. if f.Accept(&HostInfo{dataCenter: test.dc}) {
  77. if !test.accept {
  78. t.Errorf("%d: should not have been accepted but was", i)
  79. }
  80. } else if test.accept {
  81. t.Errorf("%d: should have been accepted but wasn't", i)
  82. }
  83. }
  84. }