decoder_test.go 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. // SPDX-FileCopyrightText: Copyright The Miniflux Authors. All rights reserved.
  2. // SPDX-License-Identifier: Apache-2.0
  3. package xml // import "miniflux.app/v2/internal/reader/xml"
  4. import (
  5. "encoding/xml"
  6. "fmt"
  7. "os"
  8. "strings"
  9. "testing"
  10. "unicode/utf8"
  11. )
  12. func TestXMLDocumentWithISO88591Encoding(t *testing.T) {
  13. fp, err := os.Open("testdata/iso88591.xml")
  14. if err != nil {
  15. t.Fatal(err)
  16. }
  17. defer fp.Close()
  18. type myXMLDocument struct {
  19. XMLName xml.Name `xml:"note"`
  20. To string `xml:"to"`
  21. From string `xml:"from"`
  22. }
  23. var doc myXMLDocument
  24. decoder := NewXMLDecoder(fp)
  25. err = decoder.Decode(&doc)
  26. if err != nil {
  27. t.Fatal(err)
  28. }
  29. expectedTo := "Anaïs"
  30. expectedFrom := "Jürgen"
  31. if doc.To != expectedTo {
  32. t.Errorf(`Incorrect "to" field, expected: %q, got: %q`, expectedTo, doc.To)
  33. }
  34. if doc.From != expectedFrom {
  35. t.Errorf(`Incorrect "from" field, expected: %q, got: %q`, expectedFrom, doc.From)
  36. }
  37. }
  38. func TestXMLDocumentWithISO88591FileEncodingButUTF8Prolog(t *testing.T) {
  39. fp, err := os.Open("testdata/iso88591_utf8_mismatch.xml")
  40. if err != nil {
  41. t.Fatal(err)
  42. }
  43. defer fp.Close()
  44. type myXMLDocument struct {
  45. XMLName xml.Name `xml:"note"`
  46. To string `xml:"to"`
  47. From string `xml:"from"`
  48. }
  49. var doc myXMLDocument
  50. decoder := NewXMLDecoder(fp)
  51. err = decoder.Decode(&doc)
  52. if err != nil {
  53. t.Fatal(err)
  54. }
  55. // TODO: detect actual encoding from bytes if not UTF-8 and convert to UTF-8 if needed.
  56. // For now we just expect the invalid characters to be stripped out.
  57. expectedTo := "Anas"
  58. expectedFrom := "Jrgen"
  59. if doc.To != expectedTo {
  60. t.Errorf(`Incorrect "to" field, expected: %q, got: %q`, expectedTo, doc.To)
  61. }
  62. if doc.From != expectedFrom {
  63. t.Errorf(`Incorrect "from" field, expected: %q, got: %q`, expectedFrom, doc.From)
  64. }
  65. }
  66. func TestXMLDocumentWithKOI8REncoding(t *testing.T) {
  67. fp, err := os.Open("testdata/koi8r.xml")
  68. if err != nil {
  69. t.Fatal(err)
  70. }
  71. defer fp.Close()
  72. type item struct {
  73. Title string `xml:"title"`
  74. Description string `xml:"description"`
  75. }
  76. type channel struct {
  77. Title string `xml:"title"`
  78. Description string `xml:"description"`
  79. Items []item `xml:"item"`
  80. }
  81. type rss struct {
  82. XMLName xml.Name `xml:"rss"`
  83. Channel channel `xml:"channel"`
  84. }
  85. var doc rss
  86. decoder := NewXMLDecoder(fp)
  87. err = decoder.Decode(&doc)
  88. if err != nil {
  89. t.Fatal(err)
  90. }
  91. if doc.Channel.Title != "Пример RSS ленты" {
  92. t.Errorf("Incorrect channel title, expected: %q, got: %q", "Пример RSS ленты", doc.Channel.Title)
  93. }
  94. if doc.Channel.Description != "Тестовая лента в кодировке KOI8-R" {
  95. t.Errorf("Incorrect channel description, expected: %q, got: %q", "Тестовая лента в кодировке KOI8-R", doc.Channel.Description)
  96. }
  97. if len(doc.Channel.Items) != 2 {
  98. t.Fatalf("Incorrect number of items, expected: %d, got: %d", 2, len(doc.Channel.Items))
  99. }
  100. if doc.Channel.Items[0].Title != "Первая новость" {
  101. t.Errorf("Incorrect first item title, expected: %q, got: %q", "Первая новость", doc.Channel.Items[0].Title)
  102. }
  103. if !strings.Contains(doc.Channel.Items[0].Description, "Привет мир! Ёжик, чай, Москва, Санкт-Петербург.") {
  104. t.Errorf("First item description does not contain expected text, got: %q", doc.Channel.Items[0].Description)
  105. }
  106. if !strings.Contains(doc.Channel.Items[1].Description, "Проверка специальных символов") {
  107. t.Errorf("Second item description does not contain expected text, got: %q", doc.Channel.Items[1].Description)
  108. }
  109. }
  110. func TestXMLDocumentWithIllegalUnicodeCharacters(t *testing.T) {
  111. type myxml struct {
  112. XMLName xml.Name `xml:"rss"`
  113. Version string `xml:"version,attr"`
  114. Title string `xml:"title"`
  115. }
  116. expected := "Title & 中文标题"
  117. data := fmt.Sprintf(`<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"><title>Title & 中文%s标题</title></rss>`, "\x10")
  118. reader := strings.NewReader(data)
  119. var x myxml
  120. decoder := NewXMLDecoder(reader)
  121. err := decoder.Decode(&x)
  122. if err != nil {
  123. t.Error(err)
  124. return
  125. }
  126. if x.Title != expected {
  127. t.Errorf("Incorrect entry title, expected: %s, got: %s", expected, x.Title)
  128. }
  129. }
  130. func TestXMLDocumentWindows251EncodedWithIllegalCharacters(t *testing.T) {
  131. type myxml struct {
  132. XMLName xml.Name `xml:"rss"`
  133. Version string `xml:"version,attr"`
  134. Title string `xml:"title"`
  135. }
  136. expected := "Title & 中文标题"
  137. data := fmt.Sprintf(`<?xml version="1.0" encoding="windows-1251"?><rss version="2.0"><title>Title & 中文%s标题</title></rss>`, "\x10")
  138. reader := strings.NewReader(data)
  139. var x myxml
  140. decoder := NewXMLDecoder(reader)
  141. err := decoder.Decode(&x)
  142. if err != nil {
  143. t.Error(err)
  144. return
  145. }
  146. if x.Title != expected {
  147. t.Errorf("Incorrect entry title, expected: %s, got: %s", expected, x.Title)
  148. }
  149. }
  150. func TestXMLDocumentWithIncorrectEncodingField(t *testing.T) {
  151. type myxml struct {
  152. XMLName xml.Name `xml:"rss"`
  153. Version string `xml:"version,attr"`
  154. Title string `xml:"title"`
  155. }
  156. expected := "Title & 中文标题"
  157. data := fmt.Sprintf(`<?xml version="1.0" encoding="invalid"?><rss version="2.0"><title>Title & 中文%s标题</title></rss>`, "\x10")
  158. reader := strings.NewReader(data)
  159. var x myxml
  160. decoder := NewXMLDecoder(reader)
  161. err := decoder.Decode(&x)
  162. if err != nil {
  163. t.Error(err)
  164. return
  165. }
  166. if x.Title != expected {
  167. t.Errorf("Incorrect entry title, expected: %s, got: %s", expected, x.Title)
  168. }
  169. }
  170. func TestFilterValidXMLCharsWithInvalidUTF8Sequence(t *testing.T) {
  171. // Create input with invalid UTF-8 sequence
  172. input := []byte{0x41, 0xC0, 0xAF, 0x42} // 'A', invalid UTF-8, 'B'
  173. filtered := filterValidXMLChars(input)
  174. // The function would replace invalid UTF-8 with replacement char
  175. // rather than properly filtering
  176. if utf8.Valid(filtered) {
  177. r, _ := utf8.DecodeRune(filtered[1:])
  178. if r == utf8.RuneError {
  179. t.Error("Invalid UTF-8 was not properly filtered")
  180. }
  181. }
  182. }
  183. func FuzzFilterValidXMLChars(f *testing.F) {
  184. f.Fuzz(func(t *testing.T, s []byte) {
  185. filterValidXMLChars(s)
  186. })
  187. }