You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

hostmatcher_test.go 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. // Copyright 2021 The Gitea Authors. All rights reserved.
  2. // Use of this source code is governed by a MIT-style
  3. // license that can be found in the LICENSE file.
  4. package hostmatcher
  5. import (
  6. "net"
  7. "testing"
  8. "github.com/stretchr/testify/assert"
  9. )
  10. func TestHostOrIPMatchesList(t *testing.T) {
  11. type tc struct {
  12. host string
  13. ip net.IP
  14. expected bool
  15. }
  16. // for IPv6: "::1" is loopback, "fd00::/8" is private
  17. hl := ParseHostMatchList("", "private, External, *.myDomain.com, 169.254.1.0/24")
  18. test := func(cases []tc) {
  19. for _, c := range cases {
  20. assert.Equalf(t, c.expected, hl.MatchHostOrIP(c.host, c.ip), "case domain=%s, ip=%v, expected=%v", c.host, c.ip, c.expected)
  21. }
  22. }
  23. cases := []tc{
  24. {"", net.IPv4zero, false},
  25. {"", net.IPv6zero, false},
  26. {"", net.ParseIP("127.0.0.1"), false},
  27. {"127.0.0.1", nil, false},
  28. {"", net.ParseIP("::1"), false},
  29. {"", net.ParseIP("10.0.1.1"), true},
  30. {"10.0.1.1", nil, true},
  31. {"", net.ParseIP("192.168.1.1"), true},
  32. {"192.168.1.1", nil, true},
  33. {"", net.ParseIP("fd00::1"), true},
  34. {"fd00::1", nil, true},
  35. {"", net.ParseIP("8.8.8.8"), true},
  36. {"", net.ParseIP("1001::1"), true},
  37. {"mydomain.com", net.IPv4zero, false},
  38. {"sub.mydomain.com", net.IPv4zero, true},
  39. {"", net.ParseIP("169.254.1.1"), true},
  40. {"169.254.1.1", nil, true},
  41. {"", net.ParseIP("169.254.2.2"), false},
  42. {"169.254.2.2", nil, false},
  43. }
  44. test(cases)
  45. hl = ParseHostMatchList("", "loopback")
  46. cases = []tc{
  47. {"", net.IPv4zero, false},
  48. {"", net.ParseIP("127.0.0.1"), true},
  49. {"", net.ParseIP("10.0.1.1"), false},
  50. {"", net.ParseIP("192.168.1.1"), false},
  51. {"", net.ParseIP("8.8.8.8"), false},
  52. {"", net.ParseIP("::1"), true},
  53. {"", net.ParseIP("fd00::1"), false},
  54. {"", net.ParseIP("1000::1"), false},
  55. {"mydomain.com", net.IPv4zero, false},
  56. }
  57. test(cases)
  58. hl = ParseHostMatchList("", "private")
  59. cases = []tc{
  60. {"", net.IPv4zero, false},
  61. {"", net.ParseIP("127.0.0.1"), false},
  62. {"", net.ParseIP("10.0.1.1"), true},
  63. {"", net.ParseIP("192.168.1.1"), true},
  64. {"", net.ParseIP("8.8.8.8"), false},
  65. {"", net.ParseIP("::1"), false},
  66. {"", net.ParseIP("fd00::1"), true},
  67. {"", net.ParseIP("1000::1"), false},
  68. {"mydomain.com", net.IPv4zero, false},
  69. }
  70. test(cases)
  71. hl = ParseHostMatchList("", "external")
  72. cases = []tc{
  73. {"", net.IPv4zero, false},
  74. {"", net.ParseIP("127.0.0.1"), false},
  75. {"", net.ParseIP("10.0.1.1"), false},
  76. {"", net.ParseIP("192.168.1.1"), false},
  77. {"", net.ParseIP("8.8.8.8"), true},
  78. {"", net.ParseIP("::1"), false},
  79. {"", net.ParseIP("fd00::1"), false},
  80. {"", net.ParseIP("1000::1"), true},
  81. {"mydomain.com", net.IPv4zero, false},
  82. }
  83. test(cases)
  84. hl = ParseHostMatchList("", "*")
  85. cases = []tc{
  86. {"", net.IPv4zero, true},
  87. {"", net.ParseIP("127.0.0.1"), true},
  88. {"", net.ParseIP("10.0.1.1"), true},
  89. {"", net.ParseIP("192.168.1.1"), true},
  90. {"", net.ParseIP("8.8.8.8"), true},
  91. {"", net.ParseIP("::1"), true},
  92. {"", net.ParseIP("fd00::1"), true},
  93. {"", net.ParseIP("1000::1"), true},
  94. {"mydomain.com", net.IPv4zero, true},
  95. }
  96. test(cases)
  97. // built-in network names can be escaped (warping the first char with `[]`) to be used as a real host name
  98. // this mechanism is reversed for internal usage only (maybe for some rare cases), it's not supposed to be used by end users
  99. // a real user should never use loopback/private/external as their host names
  100. hl = ParseHostMatchList("", "loopback, [p]rivate")
  101. cases = []tc{
  102. {"loopback", nil, false},
  103. {"", net.ParseIP("127.0.0.1"), true},
  104. {"private", nil, true},
  105. {"", net.ParseIP("192.168.1.1"), false},
  106. }
  107. test(cases)
  108. hl = ParseSimpleMatchList("", "loopback, *.domain.com")
  109. cases = []tc{
  110. {"loopback", nil, true},
  111. {"", net.ParseIP("127.0.0.1"), false},
  112. {"sub.domain.com", nil, true},
  113. {"other.com", nil, false},
  114. {"", net.ParseIP("1.1.1.1"), false},
  115. }
  116. test(cases)
  117. hl = ParseSimpleMatchList("", "external")
  118. cases = []tc{
  119. {"", net.ParseIP("192.168.1.1"), false},
  120. {"", net.ParseIP("1.1.1.1"), false},
  121. {"external", nil, true},
  122. }
  123. test(cases)
  124. hl = ParseSimpleMatchList("", "")
  125. cases = []tc{
  126. {"", net.ParseIP("192.168.1.1"), false},
  127. {"", net.ParseIP("1.1.1.1"), false},
  128. {"external", nil, false},
  129. }
  130. test(cases)
  131. }