url_test.go 10 KB

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