access_test.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. package ipaccess
  2. import (
  3. "bytes"
  4. "net"
  5. "testing"
  6. "github.com/stretchr/testify/assert"
  7. )
  8. func TestRuleCreation(t *testing.T) {
  9. _, ipnet, _ := net.ParseCIDR("1.1.1.1/24")
  10. _, err := NewRule(nil, []int{80}, false)
  11. assert.Error(t, err, "expected error as no ipnet provided")
  12. _, err = NewRule(ipnet, []int{65536, 80}, false)
  13. assert.Error(t, err, "expected error as port higher than 65535")
  14. _, err = NewRule(ipnet, []int{80, -1}, false)
  15. assert.Error(t, err, "expected error as port less than 0")
  16. rule, err := NewRule(ipnet, []int{443, 80}, false)
  17. assert.NoError(t, err)
  18. assert.True(t, ipnet.IP.Equal(rule.ipNet.IP) && bytes.Compare(ipnet.Mask, rule.ipNet.Mask) == 0, "ipnet expected to be %+v, got: %+v", ipnet, rule.ipNet)
  19. assert.True(t, len(rule.ports) == 2 && rule.ports[0] == 80 && rule.ports[1] == 443, "expected ports to be sorted")
  20. }
  21. func TestRuleCreationByCIDR(t *testing.T) {
  22. var cidr *string
  23. _, err := NewRuleByCIDR(cidr, []int{80}, false)
  24. assert.Error(t, err, "expected error as cidr is nil")
  25. badCidr := "1.1.1.1"
  26. cidr = &badCidr
  27. _, err = NewRuleByCIDR(cidr, []int{80}, false)
  28. assert.Error(t, err, "expected error as the cidr is bad")
  29. goodCidr := "1.1.1.1/24"
  30. _, ipnet, _ := net.ParseCIDR("1.1.1.0/24")
  31. cidr = &goodCidr
  32. rule, err := NewRuleByCIDR(cidr, []int{80}, false)
  33. assert.NoError(t, err)
  34. assert.True(t, ipnet.IP.Equal(rule.ipNet.IP) && bytes.Compare(ipnet.Mask, rule.ipNet.Mask) == 0, "ipnet expected to be %+v, got: %+v", ipnet, rule.ipNet)
  35. }
  36. func TestRulesNoRules(t *testing.T) {
  37. ip, _, _ := net.ParseCIDR("1.2.3.4/24")
  38. policy, _ := NewPolicy(true, []Rule{})
  39. allowed, rule := policy.Allowed(ip, 80)
  40. assert.True(t, allowed, "expected to be allowed as no rules and default allow")
  41. assert.Nil(t, rule, "expected to be nil as no rules")
  42. policy, _ = NewPolicy(false, []Rule{})
  43. allowed, rule = policy.Allowed(ip, 80)
  44. assert.False(t, allowed, "expected to be denied as no rules and default deny")
  45. assert.Nil(t, rule, "expected to be nil as no rules")
  46. }
  47. func TestRulesMatchIPAndPort(t *testing.T) {
  48. ip1, ipnet1, _ := net.ParseCIDR("1.2.3.4/24")
  49. ip2, _, _ := net.ParseCIDR("2.3.4.5/24")
  50. rule1, _ := NewRule(ipnet1, []int{80, 443}, true)
  51. rules := []Rule{
  52. rule1,
  53. }
  54. policy, _ := NewPolicy(false, rules)
  55. allowed, rule := policy.Allowed(ip1, 80)
  56. assert.True(t, allowed, "expected to be allowed as matching rule")
  57. assert.True(t, rule.ipNet == ipnet1, "expected to match ipnet1")
  58. allowed, rule = policy.Allowed(ip2, 80)
  59. assert.False(t, allowed, "expected to be denied as no matching rule")
  60. assert.Nil(t, rule, "expected to be nil")
  61. }
  62. func TestRulesMatchIPAndPort2(t *testing.T) {
  63. ip1, ipnet1, _ := net.ParseCIDR("1.2.3.4/24")
  64. ip2, ipnet2, _ := net.ParseCIDR("2.3.4.5/24")
  65. rule1, _ := NewRule(ipnet1, []int{53, 80}, false)
  66. rule2, _ := NewRule(ipnet2, []int{53, 80}, true)
  67. rules := []Rule{
  68. rule1,
  69. rule2,
  70. }
  71. policy, _ := NewPolicy(false, rules)
  72. allowed, rule := policy.Allowed(ip1, 80)
  73. assert.False(t, allowed, "expected to be denied as matching rule")
  74. assert.True(t, rule.ipNet == ipnet1, "expected to match ipnet1")
  75. allowed, rule = policy.Allowed(ip2, 80)
  76. assert.True(t, allowed, "expected to be allowed as matching rule")
  77. assert.True(t, rule.ipNet == ipnet2, "expected to match ipnet1")
  78. allowed, rule = policy.Allowed(ip2, 81)
  79. assert.False(t, allowed, "expected to be denied as no matching rule")
  80. assert.Nil(t, rule, "expected to be nil")
  81. }