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.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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. {"10.0.1.1:8080", nil, true},
  32. {"", net.ParseIP("192.168.1.1"), true},
  33. {"192.168.1.1", nil, true},
  34. {"", net.ParseIP("fd00::1"), true},
  35. {"fd00::1", nil, true},
  36. {"", net.ParseIP("8.8.8.8"), true},
  37. {"", net.ParseIP("1001::1"), true},
  38. {"mydomain.com", net.IPv4zero, false},
  39. {"sub.mydomain.com", net.IPv4zero, true},
  40. {"sub.mydomain.com:8080", net.IPv4zero, true},
  41. {"", net.ParseIP("169.254.1.1"), true},
  42. {"169.254.1.1", nil, true},
  43. {"", net.ParseIP("169.254.2.2"), false},
  44. {"169.254.2.2", nil, false},
  45. }
  46. test(cases)
  47. hl = ParseHostMatchList("", "loopback")
  48. cases = []tc{
  49. {"", net.IPv4zero, false},
  50. {"", net.ParseIP("127.0.0.1"), true},
  51. {"", net.ParseIP("10.0.1.1"), false},
  52. {"", net.ParseIP("192.168.1.1"), false},
  53. {"", net.ParseIP("8.8.8.8"), false},
  54. {"", net.ParseIP("::1"), true},
  55. {"", net.ParseIP("fd00::1"), false},
  56. {"", net.ParseIP("1000::1"), false},
  57. {"mydomain.com", net.IPv4zero, false},
  58. }
  59. test(cases)
  60. hl = ParseHostMatchList("", "private")
  61. cases = []tc{
  62. {"", net.IPv4zero, false},
  63. {"", net.ParseIP("127.0.0.1"), false},
  64. {"", net.ParseIP("10.0.1.1"), true},
  65. {"", net.ParseIP("192.168.1.1"), true},
  66. {"", net.ParseIP("8.8.8.8"), false},
  67. {"", net.ParseIP("::1"), false},
  68. {"", net.ParseIP("fd00::1"), true},
  69. {"", net.ParseIP("1000::1"), false},
  70. {"mydomain.com", net.IPv4zero, false},
  71. }
  72. test(cases)
  73. hl = ParseHostMatchList("", "external")
  74. cases = []tc{
  75. {"", net.IPv4zero, false},
  76. {"", net.ParseIP("127.0.0.1"), false},
  77. {"", net.ParseIP("10.0.1.1"), false},
  78. {"", net.ParseIP("192.168.1.1"), false},
  79. {"", net.ParseIP("8.8.8.8"), true},
  80. {"", net.ParseIP("::1"), false},
  81. {"", net.ParseIP("fd00::1"), false},
  82. {"", net.ParseIP("1000::1"), true},
  83. {"mydomain.com", net.IPv4zero, false},
  84. }
  85. test(cases)
  86. hl = ParseHostMatchList("", "*")
  87. cases = []tc{
  88. {"", net.IPv4zero, true},
  89. {"", net.ParseIP("127.0.0.1"), true},
  90. {"", net.ParseIP("10.0.1.1"), true},
  91. {"", net.ParseIP("192.168.1.1"), true},
  92. {"", net.ParseIP("8.8.8.8"), true},
  93. {"", net.ParseIP("::1"), true},
  94. {"", net.ParseIP("fd00::1"), true},
  95. {"", net.ParseIP("1000::1"), true},
  96. {"mydomain.com", net.IPv4zero, true},
  97. }
  98. test(cases)
  99. // built-in network names can be escaped (warping the first char with `[]`) to be used as a real host name
  100. // this mechanism is reversed for internal usage only (maybe for some rare cases), it's not supposed to be used by end users
  101. // a real user should never use loopback/private/external as their host names
  102. hl = ParseHostMatchList("", "loopback, [p]rivate")
  103. cases = []tc{
  104. {"loopback", nil, false},
  105. {"", net.ParseIP("127.0.0.1"), true},
  106. {"private", nil, true},
  107. {"", net.ParseIP("192.168.1.1"), false},
  108. }
  109. test(cases)
  110. hl = ParseSimpleMatchList("", "loopback, *.domain.com")
  111. cases = []tc{
  112. {"loopback", nil, true},
  113. {"", net.ParseIP("127.0.0.1"), false},
  114. {"sub.domain.com", nil, true},
  115. {"other.com", nil, false},
  116. {"", net.ParseIP("1.1.1.1"), false},
  117. }
  118. test(cases)
  119. hl = ParseSimpleMatchList("", "external")
  120. cases = []tc{
  121. {"", net.ParseIP("192.168.1.1"), false},
  122. {"", net.ParseIP("1.1.1.1"), false},
  123. {"external", nil, true},
  124. }
  125. test(cases)
  126. hl = ParseSimpleMatchList("", "")
  127. cases = []tc{
  128. {"", net.ParseIP("192.168.1.1"), false},
  129. {"", net.ParseIP("1.1.1.1"), false},
  130. {"external", nil, false},
  131. }
  132. test(cases)
  133. }