url_test.go 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. // SPDX-FileCopyrightText: Copyright The Miniflux Authors. All rights reserved.
  2. // SPDX-License-Identifier: Apache-2.0
  3. package urllib // import "miniflux.app/v2/internal/urllib"
  4. import (
  5. "net"
  6. "net/url"
  7. "testing"
  8. )
  9. func TestIsRelativePath(t *testing.T) {
  10. scenarios := map[string]bool{
  11. // Valid relative paths
  12. "path/to/file.ext": true,
  13. "./path/to/file.ext": true,
  14. "../path/to/file.ext": true,
  15. "file.ext": true,
  16. "./file.ext": true,
  17. "../file.ext": true,
  18. "/absolute/path": true,
  19. "path?query=value": true,
  20. "path#fragment": true,
  21. "path?query#fragment": true,
  22. // Not relative paths
  23. "https://example.org/file.ext": false,
  24. "http://example.org/file.ext": false,
  25. "//example.org/file.ext": false,
  26. "//example.org": false,
  27. "ftp://example.org/file.ext": false,
  28. "mailto:user@example.org": false,
  29. "magnet:?xt=urn:btih:example": false,
  30. "": false,
  31. "magnet:?xt.1=urn:sha1:YNCKHTQCWBTRNJIV4WNAE52SJUQCZO5C": false,
  32. }
  33. for input, expected := range scenarios {
  34. actual := IsRelativePath(input)
  35. if actual != expected {
  36. t.Errorf(`Unexpected result for IsRelativePath, got %v instead of %v for %q`, actual, expected, input)
  37. }
  38. }
  39. }
  40. func TestIsAbsoluteURL(t *testing.T) {
  41. scenarios := map[string]bool{
  42. "https://example.org/file.pdf": true,
  43. "magnet:?xt.1=urn:sha1:YNCKHTQCWBTRNJIV4WNAE52SJUQCZO5C&xt.2=urn:sha1:TXGCZQTH26NL6OUQAJJPFALHG2LTGBC7": true,
  44. "invalid url": false,
  45. "/relative/path": false,
  46. }
  47. for input, expected := range scenarios {
  48. actual := IsAbsoluteURL(input)
  49. if actual != expected {
  50. t.Errorf(`Unexpected result, got %v instead of %v for %q`, actual, expected, input)
  51. }
  52. }
  53. }
  54. func TestAbsoluteURL(t *testing.T) {
  55. type absoluteScenario struct {
  56. name string
  57. base string
  58. relative string
  59. expected string
  60. wantErr bool
  61. runWithParsed bool
  62. useNilParsed bool
  63. }
  64. scenarios := []absoluteScenario{
  65. {"absolute path", "https://example.org/folder/", "/path/file.ext", "https://example.org/path/file.ext", false, true, false},
  66. {"relative path", "https://example.org/folder/", "path/file.ext", "https://example.org/folder/path/file.ext", false, true, false},
  67. {"dot path root", "https://example.org/path", "./", "https://example.org/", false, true, false},
  68. {"dot path folder", "https://example.org/folder/", "./", "https://example.org/folder/", false, true, false},
  69. {"missing slash in base", "https://example.org/folder", "path/file.ext", "https://example.org/path/file.ext", false, true, false},
  70. {"already absolute", "https://example.org/folder/", "https://example.org/path/file.ext", "https://example.org/path/file.ext", false, true, false},
  71. {"protocol relative", "https://www.example.org/", "//static.example.org/path/file.ext", "https://static.example.org/path/file.ext", false, true, false},
  72. {"magnet keeps scheme", "https://www.example.org/", "magnet:?xt=urn:btih:c12fe1c06bba254a9dc9f519b335aa7c1367a88a", "magnet:?xt=urn:btih:c12fe1c06bba254a9dc9f519b335aa7c1367a88a", false, true, false},
  73. {"magnet with query", "https://www.example.org/", "magnet:?xt.1=urn:sha1:YNCKHTQCWBTRNJIV4WNAE52SJUQCZO5C&xt.2=urn:sha1:TXGCZQTH26NL6OUQAJJPFALHG2LTGBC7", "magnet:?xt.1=urn:sha1:YNCKHTQCWBTRNJIV4WNAE52SJUQCZO5C&xt.2=urn:sha1:TXGCZQTH26NL6OUQAJJPFALHG2LTGBC7", false, true, false},
  74. {"empty relative returns base", "https://example.org/folder/", "", "https://example.org/folder/", false, true, false},
  75. {"invalid base errors", "://bad", "path/file.ext", "", true, false, false},
  76. {"absolute ignores invalid base", "://bad", "https://example.org/path/file.ext", "https://example.org/path/file.ext", false, true, true},
  77. }
  78. for _, scenario := range scenarios {
  79. t.Run(scenario.name, func(t *testing.T) {
  80. actual, err := ResolveToAbsoluteURL(scenario.base, scenario.relative)
  81. if scenario.wantErr {
  82. if err == nil {
  83. t.Fatalf("expected error for base %q relative %q", scenario.base, scenario.relative)
  84. }
  85. return
  86. }
  87. if err != nil {
  88. t.Fatalf("unexpected error for base %q relative %q: %v", scenario.base, scenario.relative, err)
  89. }
  90. if actual != scenario.expected {
  91. t.Fatalf("unexpected result, got %q instead of %q for (%q, %q)", actual, scenario.expected, scenario.base, scenario.relative)
  92. }
  93. if scenario.runWithParsed {
  94. var parsedBase *url.URL
  95. if !scenario.useNilParsed && scenario.base != "" {
  96. var parseErr error
  97. parsedBase, parseErr = url.Parse(scenario.base)
  98. if parseErr != nil {
  99. t.Fatalf("unable to parse base %q: %v", scenario.base, parseErr)
  100. }
  101. }
  102. actualParsed, errParsed := ResolveToAbsoluteURLWithParsedBaseURL(parsedBase, scenario.relative)
  103. if errParsed != nil {
  104. t.Fatalf("unexpected error with parsed base for (%q, %q): %v", scenario.base, scenario.relative, errParsed)
  105. }
  106. if actualParsed != scenario.expected {
  107. t.Fatalf("unexpected parsed-base result, got %q instead of %q for (%q, %q)", actualParsed, scenario.expected, scenario.base, scenario.relative)
  108. }
  109. }
  110. })
  111. }
  112. }
  113. func TestRootURL(t *testing.T) {
  114. scenarios := map[string]string{
  115. "": "",
  116. "https://example.org/path/file.ext": "https://example.org/",
  117. "https://example.org/path/file.ext?test=abc": "https://example.org/",
  118. "//static.example.org/path/file.ext": "https://static.example.org/",
  119. "https://example|org/path/file.ext": "https://example|org/path/file.ext",
  120. "/relative/path": "/relative/path",
  121. "http://example.org:8080/path": "http://example.org:8080/",
  122. }
  123. for input, expected := range scenarios {
  124. actual := RootURL(input)
  125. if actual != expected {
  126. t.Errorf(`Unexpected result, got %q instead of %q`, actual, expected)
  127. }
  128. }
  129. }
  130. func TestIsHTTPS(t *testing.T) {
  131. scenarios := map[string]bool{
  132. "https://example.org/": true,
  133. "http://example.org/": false,
  134. "https://example|org/": false,
  135. }
  136. for input, expected := range scenarios {
  137. actual := IsHTTPS(input)
  138. if actual != expected {
  139. t.Errorf(`Unexpected result, got %v instead of %v`, actual, expected)
  140. }
  141. }
  142. }
  143. func TestDomain(t *testing.T) {
  144. scenarios := map[string]string{
  145. "https://static.example.org/": "static.example.org",
  146. "https://example|org/": "https://example|org/",
  147. }
  148. for input, expected := range scenarios {
  149. actual := Domain(input)
  150. if actual != expected {
  151. t.Errorf(`Unexpected result, got %q instead of %q`, actual, expected)
  152. }
  153. }
  154. }
  155. func TestDomainWithoutWWW(t *testing.T) {
  156. scenarios := map[string]string{
  157. "https://www.example.org/": "example.org",
  158. "https://example.org/": "example.org",
  159. "https://www.sub.example.org/": "sub.example.org",
  160. "https://example|org/": "https://example|org/",
  161. }
  162. for input, expected := range scenarios {
  163. actual := DomainWithoutWWW(input)
  164. if actual != expected {
  165. t.Errorf(`Unexpected result, got %q instead of %q`, actual, expected)
  166. }
  167. }
  168. }
  169. func TestJoinBaseURLAndPath(t *testing.T) {
  170. type args struct {
  171. baseURL string
  172. path string
  173. }
  174. tests := []struct {
  175. name string
  176. args args
  177. want string
  178. wantErr bool
  179. }{
  180. {"empty base url", args{"", "/api/bookmarks/"}, "", true},
  181. {"empty path", args{"https://example.com", ""}, "", true},
  182. {"invalid base url", args{"incorrect url", ""}, "", true},
  183. {"valid", args{"https://example.com", "/api/bookmarks/"}, "https://example.com/api/bookmarks/", false},
  184. {"valid", args{"https://example.com/subfolder", "/api/bookmarks/"}, "https://example.com/subfolder/api/bookmarks/", false},
  185. }
  186. for _, tt := range tests {
  187. t.Run(tt.name, func(t *testing.T) {
  188. got, err := JoinBaseURLAndPath(tt.args.baseURL, tt.args.path)
  189. if (err != nil) != tt.wantErr {
  190. t.Errorf("JoinBaseURLAndPath error = %v, wantErr %v", err, tt.wantErr)
  191. return
  192. }
  193. if got != tt.want {
  194. t.Errorf("JoinBaseURLAndPath = %v, want %v", got, tt.want)
  195. }
  196. })
  197. }
  198. }
  199. func TestIsNonPublicIP(t *testing.T) {
  200. testCases := []struct {
  201. name string
  202. ipString string
  203. want bool
  204. }{
  205. {"nil", "", true},
  206. {"private IPv4", "192.168.1.10", true},
  207. {"loopback IPv4", "127.0.0.1", true},
  208. {"link-local IPv4", "169.254.42.1", true},
  209. {"multicast IPv4", "224.0.0.1", true},
  210. {"unspecified IPv6", "::", true},
  211. {"loopback IPv6", "::1", true},
  212. {"multicast IPv6", "ff02::1", true},
  213. {"public IPv4", "93.184.216.34", false},
  214. {"public IPv6", "2001:4860:4860::8888", false},
  215. }
  216. for _, tc := range testCases {
  217. t.Run(tc.name, func(t *testing.T) {
  218. var ip net.IP
  219. if tc.ipString != "" {
  220. ip = net.ParseIP(tc.ipString)
  221. if ip == nil {
  222. t.Fatalf("unable to parse %q", tc.ipString)
  223. }
  224. }
  225. if got := isNonPublicIP(ip); got != tc.want {
  226. t.Fatalf("unexpected result for %s: got %v want %v", tc.name, got, tc.want)
  227. }
  228. })
  229. }
  230. }
  231. func TestResolvesToPrivateIP(t *testing.T) {
  232. testCases := []struct {
  233. name string
  234. host string
  235. want bool
  236. }{
  237. {"localhost", "localhost", true},
  238. {"example.org", "example.org", false},
  239. {"loopback IPv4 literal", "127.0.0.1", true},
  240. {"loopback IPv6 literal", "::1", true},
  241. {"private IPv4 literal", "192.168.1.1", true},
  242. {"public IPv4 literal", "93.184.216.34", false},
  243. {"public IPv6 literal", "2001:4860:4860::8888", false},
  244. }
  245. for _, tc := range testCases {
  246. t.Run(tc.name, func(t *testing.T) {
  247. got, err := ResolvesToPrivateIP(tc.host)
  248. if err != nil {
  249. t.Fatalf("unexpected error for %s: %v", tc.host, err)
  250. }
  251. if got != tc.want {
  252. t.Fatalf("unexpected result for %s: got %v want %v", tc.name, got, tc.want)
  253. }
  254. })
  255. }
  256. }
  257. func TestResolvesToPrivateIPError(t *testing.T) {
  258. if _, err := ResolvesToPrivateIP(""); err == nil {
  259. t.Fatalf("expected an error for empty host")
  260. }
  261. }