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

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