url_test.go 11 KB

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