finder_test.go 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445
  1. // SPDX-FileCopyrightText: Copyright The Miniflux Authors. All rights reserved.
  2. // SPDX-License-Identifier: Apache-2.0
  3. package icon // import "miniflux.app/v2/internal/reader/icon"
  4. import (
  5. "bytes"
  6. "encoding/base64"
  7. "image"
  8. "strings"
  9. "testing"
  10. "miniflux.app/v2/internal/model"
  11. )
  12. func TestParseImageDataURL(t *testing.T) {
  13. iconURL := "data:image/webp;base64,UklGRhQJAABXRUJQVlA4TAcJAAAvv8AvEIU1atuOza3OCSaanSeobUa17T61bdu2bVtRbdvtDmrb7gSTdibJXOG81/d9z/vsX3utCLi1bbuJ3hKeVEymRRuaSnCVSBWIBmwP410h0IHJXDyfZCfRNhklFS/sufGPbPHPjT0vVJRkhE1BwxFZ5EhDQVjkrEjIJokVOVHMhAuyyoUpUUCbDbLLhjbRFkO+kWG+GRLT0+YTWeaTNjEdW2SaLTEtU2SbOTGVnAuyzY0nYgobZJwtMZkxD2ScB2NiEg2yTkOQcULWOZFRIvOU1Mg8FS/IPC8ckHkOXJF5riRknoT/pb1t6iwPetFIH3jNY660i/khw/3dq4W09ZbNIbN1TjOeFD2iB2T1KmIM0x0yuhOxbod81vueWK0GQDa3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mfi3Qql6+RfYk10tk5QUXube4OY4y0I5XuUmF/bUxdwO1jRxb4n9uVQwn2J/ZdbbWNWKGpnXhs42SMaSQXfC1DCHhpJJT97we0uca5jHeJYk45znmsN9JJP/UsqnGAtKOWFJJ2ToZwz+J2kcqs6KOkuJJGB2kNZ69xFkrhaeyhvF2+S2v/jICh1T6+TWn9qAJS8m8dITce7WAOUvs6xWkjtnrEYZGFpw0mNXrMB5KKdPXxNqj/OIMtDTjra0eukqhM9azcBsrOg03xMqvSLIXYzM2RqAfu600cmkIr+9oKL7GQRyFyDFe3hDHd4xcd+NZ601ehbIzzuNqfbyxrmhKx219Ns5jN5bj1N6g6pkZB5EldknisHZJ4DL8g8L9TIPBXPyDwlGSdknRMZQYOs0xCTKEjIOImCmMwKGWdDTCHnimxzJSemMkO2WRDTskWm2RHT0eUTWeaTLjE9Q/6QYX4YEm3RYYvssqVDFDDjgqxyYU4UM2JDQjZJbBgRFgVLzsgiZ5YUhE1GSc0Le+48kC0e3NnzQk1JRrQNAA=="
  14. icon, err := parseImageDataURL(iconURL)
  15. if err != nil {
  16. t.Fatalf(`We should be able to parse valid data URL: %v`, err)
  17. }
  18. if icon.MimeType != "image/webp" {
  19. t.Fatal(`Invalid mime type parsed`)
  20. }
  21. if icon.Hash == "" {
  22. t.Fatal(`Image hash should be computed`)
  23. }
  24. }
  25. func TestParseImageDataURLWithNoEncoding(t *testing.T) {
  26. iconURL := `data:image/webp,%3Ch1%3EHello%2C%20World%21%3C%2Fh1%3E`
  27. icon, err := parseImageDataURL(iconURL)
  28. if err != nil {
  29. t.Fatalf(`We should be able to parse valid data URL: %v`, err)
  30. }
  31. if icon.MimeType != "image/webp" {
  32. t.Fatal(`Invalid mime type parsed`)
  33. }
  34. if string(icon.Content) == "Hello, World!" {
  35. t.Fatal(`Value should be URL-decoded`)
  36. }
  37. if icon.Hash == "" {
  38. t.Fatal(`Image hash should be computed`)
  39. }
  40. }
  41. func TestParseImageWithRawSVGEncodedInUTF8(t *testing.T) {
  42. iconURL := `data:image/svg+xml;utf8,<svg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 456 456'><circle></circle></svg>`
  43. icon, err := parseImageDataURL(iconURL)
  44. if err != nil {
  45. t.Fatalf(`We should be able to parse valid data URL: %v`, err)
  46. }
  47. if icon.MimeType != "image/svg+xml" {
  48. t.Fatal(`Invalid mime type parsed`)
  49. }
  50. if icon.Hash == "" {
  51. t.Fatal(`Image hash should be computed`)
  52. }
  53. if string(icon.Content) != `<svg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 456 456'><circle></circle></svg>` {
  54. t.Fatal(`Invalid SVG content`)
  55. }
  56. }
  57. func TestParseImageDataURLWithNoMediaTypeAndNoEncoding(t *testing.T) {
  58. iconURL := `data:,Hello%2C%20World%21`
  59. _, err := parseImageDataURL(iconURL)
  60. if err == nil {
  61. t.Fatal(`We should detect invalid mime type`)
  62. }
  63. }
  64. func TestParseInvalidImageDataURLWithBadMimeType(t *testing.T) {
  65. _, err := parseImageDataURL("data:text/plain;base64,blob")
  66. if err == nil {
  67. t.Fatal(`We should detect invalid mime type`)
  68. }
  69. }
  70. func TestParseInvalidImageDataURLWithUnsupportedEncoding(t *testing.T) {
  71. _, err := parseImageDataURL("data:image/png;base32,blob")
  72. if err == nil {
  73. t.Fatal(`We should detect unsupported encoding`)
  74. }
  75. }
  76. func TestParseInvalidImageDataURLWithNoData(t *testing.T) {
  77. _, err := parseImageDataURL("data:image/png;base64,")
  78. if err == nil {
  79. t.Fatal(`We should detect invalid encoded data`)
  80. }
  81. }
  82. func TestParseInvalidImageDataURL(t *testing.T) {
  83. _, err := parseImageDataURL("data:image/jpeg")
  84. if err == nil {
  85. t.Fatal(`We should detect malformed image data URL`)
  86. }
  87. }
  88. func TestParseInvalidImageDataURLWithWrongPrefix(t *testing.T) {
  89. _, err := parseImageDataURL("data,test")
  90. if err == nil {
  91. t.Fatal(`We should detect malformed image data URL`)
  92. }
  93. }
  94. func TestFindIconURLsFromHTMLDocument_MultipleIcons(t *testing.T) {
  95. html := `<!DOCTYPE html>
  96. <html>
  97. <head>
  98. <link rel="icon" href="/favicon.ico">
  99. <link rel="shortcut icon" href="/shortcut-favicon.ico">
  100. <link rel="icon shortcut" href="/icon-shortcut.ico">
  101. <link rel="apple-touch-icon" href="/apple-touch-icon.png">
  102. </head>
  103. </html>`
  104. iconURLs, err := findIconURLsFromHTMLDocument("https://example.org", strings.NewReader(html), "text/html")
  105. if err != nil {
  106. t.Fatal(err)
  107. }
  108. expected := []string{
  109. "https://example.org/favicon.ico",
  110. "https://example.org/shortcut-favicon.ico",
  111. "https://example.org/icon-shortcut.ico",
  112. "https://example.org/apple-touch-icon.png",
  113. }
  114. if len(iconURLs) != len(expected) {
  115. t.Fatalf("Expected %d icon URLs, got %d", len(expected), len(iconURLs))
  116. }
  117. for i, expectedURL := range expected {
  118. if iconURLs[i] != expectedURL {
  119. t.Errorf("Expected icon URL %d to be %q, got %q", i, expectedURL, iconURLs[i])
  120. }
  121. }
  122. }
  123. func TestFindIconURLsFromHTMLDocument_CaseInsensitiveRel(t *testing.T) {
  124. html := `<!DOCTYPE html>
  125. <html>
  126. <head>
  127. <link rel="ICON" href="/favicon1.ico">
  128. <link rel="Icon" href="/favicon2.ico">
  129. <link rel="SHORTCUT ICON" href="/favicon3.ico">
  130. <link rel="Shortcut Icon" href="/favicon4.ico">
  131. <link rel="ICON SHORTCUT" href="/favicon5.ico">
  132. <link rel="Icon Shortcut" href="favicon6.ico">
  133. </head>
  134. </html>`
  135. iconURLs, err := findIconURLsFromHTMLDocument("https://example.org/folder/", strings.NewReader(html), "text/html")
  136. if err != nil {
  137. t.Fatal(err)
  138. }
  139. expected := []string{
  140. "https://example.org/favicon1.ico",
  141. "https://example.org/favicon2.ico",
  142. "https://example.org/favicon3.ico",
  143. "https://example.org/favicon4.ico",
  144. "https://example.org/favicon5.ico",
  145. "https://example.org/folder/favicon6.ico",
  146. }
  147. if len(iconURLs) != len(expected) {
  148. t.Fatalf("Expected %d icon URLs, got %d", len(expected), len(iconURLs))
  149. }
  150. for i, expectedURL := range expected {
  151. if iconURLs[i] != expectedURL {
  152. t.Errorf("Expected icon URL %d to be %q, got %q", i, expectedURL, iconURLs[i])
  153. }
  154. }
  155. }
  156. func TestFindIconURLsFromHTMLDocument_NoIcons(t *testing.T) {
  157. html := `<!DOCTYPE html>
  158. <html>
  159. <head>
  160. <title>No Icons Here</title>
  161. <link rel="stylesheet" href="/style.css">
  162. <link rel="canonical" href="https://example.com">
  163. </head>
  164. </html>`
  165. iconURLs, err := findIconURLsFromHTMLDocument("https://example.org", strings.NewReader(html), "text/html")
  166. if err != nil {
  167. t.Fatal(err)
  168. }
  169. if len(iconURLs) != 0 {
  170. t.Fatalf("Expected 0 icon URLs, got %d: %v", len(iconURLs), iconURLs)
  171. }
  172. }
  173. func TestFindIconURLsFromHTMLDocument_EmptyHref(t *testing.T) {
  174. html := `<!DOCTYPE html>
  175. <html>
  176. <head>
  177. <link rel="icon" href="">
  178. <link rel="icon" href=" ">
  179. <link rel="icon">
  180. <link rel="shortcut icon" href="/valid-icon.ico">
  181. </head>
  182. </html>`
  183. iconURLs, err := findIconURLsFromHTMLDocument("https://example.org", strings.NewReader(html), "text/html")
  184. if err != nil {
  185. t.Fatal(err)
  186. }
  187. expected := []string{"https://example.org/valid-icon.ico"}
  188. if len(iconURLs) != len(expected) {
  189. t.Fatalf("Expected %d icon URLs, got %d", len(expected), len(iconURLs))
  190. }
  191. if iconURLs[0] != expected[0] {
  192. t.Errorf("Expected icon URL to be %q, got %q", expected[0], iconURLs[0])
  193. }
  194. }
  195. func TestFindIconURLsFromHTMLDocument_DataURLs(t *testing.T) {
  196. html := `<!DOCTYPE html>
  197. <html>
  198. <head>
  199. <link rel="icon" href="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mNkYPhfDwAChAGAhGAQ+QAAAABJRU5ErkJggg==">
  200. <link rel="shortcut icon" href="data:image/svg+xml,<svg xmlns='http://www.w3.org/2000/svg'></svg>">
  201. <link rel="icon" href="/regular-icon.ico">
  202. </head>
  203. </html>`
  204. iconURLs, err := findIconURLsFromHTMLDocument("https://example.org/folder", strings.NewReader(html), "text/html")
  205. if err != nil {
  206. t.Fatal(err)
  207. }
  208. // The function processes queries in order: rel="icon", then rel="shortcut icon", etc.
  209. // So both rel="icon" links are found first, then the rel="shortcut icon" link
  210. expected := []string{
  211. "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mNkYPhfDwAChAGAhGAQ+QAAAABJRU5ErkJggg==",
  212. "https://example.org/regular-icon.ico",
  213. "data:image/svg+xml,<svg xmlns='http://www.w3.org/2000/svg'></svg>",
  214. }
  215. if len(iconURLs) != len(expected) {
  216. t.Fatalf("Expected %d icon URLs, got %d", len(expected), len(iconURLs))
  217. }
  218. for i, expectedURL := range expected {
  219. if iconURLs[i] != expectedURL {
  220. t.Errorf("Expected icon URL %d to be %q, got %q", i, expectedURL, iconURLs[i])
  221. }
  222. }
  223. }
  224. func TestFindIconURLsFromHTMLDocument_RelativeAndAbsoluteURLs(t *testing.T) {
  225. html := `<!DOCTYPE html>
  226. <html>
  227. <head>
  228. <link rel="icon" href="/absolute-path.ico">
  229. <link rel="icon" href="relative-path.ico">
  230. <link rel="icon" href="../parent-dir.ico">
  231. <link rel="icon" href="https://example.com/external.ico">
  232. <link rel="icon" href="//cdn.example.com/protocol-relative.ico">
  233. </head>
  234. </html>`
  235. iconURLs, err := findIconURLsFromHTMLDocument("https://example.org/folder/", strings.NewReader(html), "text/html")
  236. if err != nil {
  237. t.Fatal(err)
  238. }
  239. expected := []string{
  240. "https://example.org/absolute-path.ico",
  241. "https://example.org/folder/relative-path.ico",
  242. "https://example.org/parent-dir.ico",
  243. "https://example.com/external.ico",
  244. "https://cdn.example.com/protocol-relative.ico",
  245. }
  246. if len(iconURLs) != len(expected) {
  247. t.Fatalf("Expected %d icon URLs, got %d", len(expected), len(iconURLs))
  248. }
  249. for i, expectedURL := range expected {
  250. if iconURLs[i] != expectedURL {
  251. t.Errorf("Expected icon URL %d to be %q, got %q", i, expectedURL, iconURLs[i])
  252. }
  253. }
  254. }
  255. func TestFindIconURLsFromHTMLDocument_InvalidHTML(t *testing.T) {
  256. html := `<!DOCTYPE html>
  257. <html>
  258. <head>
  259. <link rel="icon" href="/valid-before-error.ico">
  260. <link rel="icon" href="/unclosed-tag.ico"
  261. <link rel="shortcut icon" href="/valid-after-error.ico">
  262. </head>
  263. </html>`
  264. iconURLs, err := findIconURLsFromHTMLDocument("https://example.org", strings.NewReader(html), "text/html")
  265. if err != nil {
  266. t.Fatal(err)
  267. }
  268. // goquery should handle malformed HTML gracefully
  269. if len(iconURLs) == 0 {
  270. t.Fatal("Expected to find some icon URLs even with malformed HTML")
  271. }
  272. // Should at least find the valid ones
  273. foundValidIcon := false
  274. for _, url := range iconURLs {
  275. if url == "https://example.org/valid-before-error.ico" || url == "https://example.org/valid-after-error.ico" {
  276. foundValidIcon = true
  277. break
  278. }
  279. }
  280. if !foundValidIcon {
  281. t.Errorf("Expected to find at least one valid icon URL, got: %v", iconURLs)
  282. }
  283. }
  284. func TestFindIconURLsFromHTMLDocument_EmptyDocument(t *testing.T) {
  285. iconURLs, err := findIconURLsFromHTMLDocument("https://example.org", strings.NewReader(""), "text/html")
  286. if err != nil {
  287. t.Fatal(err)
  288. }
  289. if len(iconURLs) != 0 {
  290. t.Fatalf("Expected 0 icon URLs from empty document, got %d", len(iconURLs))
  291. }
  292. }
  293. func TestResizeIconSmallGif(t *testing.T) {
  294. data, err := base64.StdEncoding.DecodeString("R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==")
  295. if err != nil {
  296. t.Fatal(err)
  297. }
  298. icon := model.Icon{
  299. Content: data,
  300. MimeType: "image/gif",
  301. }
  302. if !bytes.Equal(icon.Content, resizeIcon(&icon).Content) {
  303. t.Fatalf("Converted gif smaller than 16x16")
  304. }
  305. }
  306. func TestResizeIconPng(t *testing.T) {
  307. data, err := base64.StdEncoding.DecodeString("iVBORw0KGgoAAAANSUhEUgAAACEAAAAhCAYAAABX5MJvAAAALUlEQVR42u3OMQEAAAgDoJnc6BpjDyRgcrcpGwkJCQkJCQkJCQkJCQkJCYmyB7NfUj/Kk4FkAAAAAElFTkSuQmCC")
  308. if err != nil {
  309. t.Fatal(err)
  310. }
  311. icon := model.Icon{
  312. Content: data,
  313. MimeType: "image/png",
  314. }
  315. resizedIcon := resizeIcon(&icon)
  316. if bytes.Equal(data, resizedIcon.Content) {
  317. t.Fatalf("Didn't convert png of 33x33")
  318. }
  319. config, _, err := image.DecodeConfig(bytes.NewReader(resizedIcon.Content))
  320. if err != nil {
  321. t.Fatalf("Couln't decode resulting png: %v", err)
  322. }
  323. if config.Height != 32 || config.Width != 32 {
  324. t.Fatalf("Was expecting an image of 16x16, got %dx%d", config.Width, config.Height)
  325. }
  326. }
  327. func TestResizeIconWebp(t *testing.T) {
  328. data, err := base64.StdEncoding.DecodeString("UklGRkAAAABXRUJQVlA4IDQAAADwAQCdASoBAAEAAQAcJaACdLoB+AAETAAA/vW4f/6aR40jxpHxcP/ugT90CfugT/3NoAAA")
  329. if err != nil {
  330. t.Fatal(err)
  331. }
  332. icon := model.Icon{
  333. Content: data,
  334. MimeType: "image/webp",
  335. }
  336. if !bytes.Equal(icon.Content, resizeIcon(&icon).Content) {
  337. t.Fatalf("Converted webp smaller than 16x16")
  338. }
  339. }
  340. func TestResizeInvalidImage(t *testing.T) {
  341. icon := model.Icon{
  342. Content: []byte("invalid data"),
  343. MimeType: "image/gif",
  344. }
  345. if !bytes.Equal(icon.Content, resizeIcon(&icon).Content) {
  346. t.Fatalf("Tried to convert an invalid image")
  347. }
  348. }
  349. func TestMinifySvg(t *testing.T) {
  350. data := []byte(`<svg path d=" M1 4h-.001 V1h2v.001 M1 2.6 h1v.001"/></svg>`)
  351. want := []byte(`<svg path="" d="M1 4H.999V1h2v.001M1 2.6h1v.001"/></svg>`)
  352. icon := model.Icon{Content: data, MimeType: "image/svg+xml"}
  353. got := resizeIcon(&icon).Content
  354. if !bytes.Equal(want, got) {
  355. t.Fatalf("Didn't correctly minify the svg: got %s instead of %s", got, want)
  356. }
  357. }
  358. func TestMinifySvgWithError(t *testing.T) {
  359. // Invalid SVG with malformed XML that should cause minification to fail
  360. data := []byte(`<svg><><invalid-tag<>unclosed`)
  361. original := make([]byte, len(data))
  362. copy(original, data)
  363. icon := model.Icon{
  364. Content: data,
  365. MimeType: "image/svg+xml",
  366. }
  367. result := resizeIcon(&icon)
  368. // When minification fails, the original content should be preserved
  369. if !bytes.Equal(original, result.Content) {
  370. t.Fatalf("Expected original content to be preserved on minification error, got %s instead of %s", result.Content, original)
  371. }
  372. // MimeType should remain unchanged
  373. if result.MimeType != "image/svg+xml" {
  374. t.Fatalf("Expected MimeType to remain image/svg+xml, got %s", result.MimeType)
  375. }
  376. }