| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311 |
- // SPDX-FileCopyrightText: Copyright The Miniflux Authors. All rights reserved.
- // SPDX-License-Identifier: Apache-2.0
- package xml // import "miniflux.app/v2/internal/reader/xml"
- import (
- "bytes"
- "encoding/xml"
- "fmt"
- "os"
- "strings"
- "testing"
- "unicode/utf8"
- "golang.org/x/text/encoding"
- "golang.org/x/text/encoding/unicode"
- )
- func TestXMLDocumentWithISO88591Encoding(t *testing.T) {
- fp, err := os.Open("testdata/iso88591.xml")
- if err != nil {
- t.Fatal(err)
- }
- defer fp.Close()
- type myXMLDocument struct {
- XMLName xml.Name `xml:"note"`
- To string `xml:"to"`
- From string `xml:"from"`
- }
- var doc myXMLDocument
- decoder := NewXMLDecoder(fp)
- err = decoder.Decode(&doc)
- if err != nil {
- t.Fatal(err)
- }
- expectedTo := "Anaïs"
- expectedFrom := "Jürgen"
- if doc.To != expectedTo {
- t.Errorf(`Incorrect "to" field, expected: %q, got: %q`, expectedTo, doc.To)
- }
- if doc.From != expectedFrom {
- t.Errorf(`Incorrect "from" field, expected: %q, got: %q`, expectedFrom, doc.From)
- }
- }
- func TestXMLDocumentWithISO88591FileEncodingButUTF8Prolog(t *testing.T) {
- fp, err := os.Open("testdata/iso88591_utf8_mismatch.xml")
- if err != nil {
- t.Fatal(err)
- }
- defer fp.Close()
- type myXMLDocument struct {
- XMLName xml.Name `xml:"note"`
- To string `xml:"to"`
- From string `xml:"from"`
- }
- var doc myXMLDocument
- decoder := NewXMLDecoder(fp)
- err = decoder.Decode(&doc)
- if err != nil {
- t.Fatal(err)
- }
- // TODO: detect actual encoding from bytes if not UTF-8 and convert to UTF-8 if needed.
- // For now we just expect the invalid characters to be stripped out.
- expectedTo := "Anas"
- expectedFrom := "Jrgen"
- if doc.To != expectedTo {
- t.Errorf(`Incorrect "to" field, expected: %q, got: %q`, expectedTo, doc.To)
- }
- if doc.From != expectedFrom {
- t.Errorf(`Incorrect "from" field, expected: %q, got: %q`, expectedFrom, doc.From)
- }
- }
- func TestXMLDocumentWithKOI8REncoding(t *testing.T) {
- fp, err := os.Open("testdata/koi8r.xml")
- if err != nil {
- t.Fatal(err)
- }
- defer fp.Close()
- type item struct {
- Title string `xml:"title"`
- Description string `xml:"description"`
- }
- type channel struct {
- Title string `xml:"title"`
- Description string `xml:"description"`
- Items []item `xml:"item"`
- }
- type rss struct {
- XMLName xml.Name `xml:"rss"`
- Channel channel `xml:"channel"`
- }
- var doc rss
- decoder := NewXMLDecoder(fp)
- err = decoder.Decode(&doc)
- if err != nil {
- t.Fatal(err)
- }
- if doc.Channel.Title != "Пример RSS ленты" {
- t.Errorf("Incorrect channel title, expected: %q, got: %q", "Пример RSS ленты", doc.Channel.Title)
- }
- if doc.Channel.Description != "Тестовая лента в кодировке KOI8-R" {
- t.Errorf("Incorrect channel description, expected: %q, got: %q", "Тестовая лента в кодировке KOI8-R", doc.Channel.Description)
- }
- if len(doc.Channel.Items) != 2 {
- t.Fatalf("Incorrect number of items, expected: %d, got: %d", 2, len(doc.Channel.Items))
- }
- if doc.Channel.Items[0].Title != "Первая новость" {
- t.Errorf("Incorrect first item title, expected: %q, got: %q", "Первая новость", doc.Channel.Items[0].Title)
- }
- if !strings.Contains(doc.Channel.Items[0].Description, "Привет мир! Ёжик, чай, Москва, Санкт-Петербург.") {
- t.Errorf("First item description does not contain expected text, got: %q", doc.Channel.Items[0].Description)
- }
- if !strings.Contains(doc.Channel.Items[1].Description, "Проверка специальных символов") {
- t.Errorf("Second item description does not contain expected text, got: %q", doc.Channel.Items[1].Description)
- }
- }
- func TestXMLDocumentWithIllegalUnicodeCharacters(t *testing.T) {
- type myxml struct {
- XMLName xml.Name `xml:"rss"`
- Version string `xml:"version,attr"`
- Title string `xml:"title"`
- }
- expected := "Title & 中文标题"
- data := fmt.Sprintf(`<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"><title>Title & 中文%s标题</title></rss>`, "\x10")
- reader := strings.NewReader(data)
- var x myxml
- decoder := NewXMLDecoder(reader)
- err := decoder.Decode(&x)
- if err != nil {
- t.Error(err)
- return
- }
- if x.Title != expected {
- t.Errorf("Incorrect entry title, expected: %s, got: %s", expected, x.Title)
- }
- }
- func TestXMLDocumentWindows251EncodedWithIllegalCharacters(t *testing.T) {
- type myxml struct {
- XMLName xml.Name `xml:"rss"`
- Version string `xml:"version,attr"`
- Title string `xml:"title"`
- }
- expected := "Title & 中文标题"
- data := fmt.Sprintf(`<?xml version="1.0" encoding="windows-1251"?><rss version="2.0"><title>Title & 中文%s标题</title></rss>`, "\x10")
- reader := strings.NewReader(data)
- var x myxml
- decoder := NewXMLDecoder(reader)
- err := decoder.Decode(&x)
- if err != nil {
- t.Error(err)
- return
- }
- if x.Title != expected {
- t.Errorf("Incorrect entry title, expected: %s, got: %s", expected, x.Title)
- }
- }
- func TestXMLDocumentWithIncorrectEncodingField(t *testing.T) {
- type myxml struct {
- XMLName xml.Name `xml:"rss"`
- Version string `xml:"version,attr"`
- Title string `xml:"title"`
- }
- expected := "Title & 中文标题"
- data := fmt.Sprintf(`<?xml version="1.0" encoding="invalid"?><rss version="2.0"><title>Title & 中文%s标题</title></rss>`, "\x10")
- reader := strings.NewReader(data)
- var x myxml
- decoder := NewXMLDecoder(reader)
- err := decoder.Decode(&x)
- if err != nil {
- t.Error(err)
- return
- }
- if x.Title != expected {
- t.Errorf("Incorrect entry title, expected: %s, got: %s", expected, x.Title)
- }
- }
- func TestXMLDocumentWithUTF16Encoding(t *testing.T) {
- type myXMLDocument struct {
- XMLName xml.Name `xml:"note"`
- To string `xml:"to"`
- From string `xml:"from"`
- }
- encodings := map[string]encoding.Encoding{
- "little endian with BOM": unicode.UTF16(unicode.LittleEndian, unicode.UseBOM),
- "big endian with BOM": unicode.UTF16(unicode.BigEndian, unicode.UseBOM),
- "little endian without BOM": unicode.UTF16(unicode.LittleEndian, unicode.IgnoreBOM),
- "big endian without BOM": unicode.UTF16(unicode.BigEndian, unicode.IgnoreBOM),
- }
- documents := map[string]string{
- "with prolog": `<?xml version="1.0" encoding="utf-16"?><note><to>Anaïs</to><from>Jürgen 😀</from></note>`,
- "without prolog": `<note><to>Anaïs</to><from>Jürgen 😀</from></note>`,
- }
- for encodingName, enc := range encodings {
- for documentName, document := range documents {
- t.Run(encodingName+" "+documentName, func(t *testing.T) {
- data, err := enc.NewEncoder().Bytes([]byte(document))
- if err != nil {
- t.Fatal(err)
- }
- var doc myXMLDocument
- if err := NewXMLDecoder(bytes.NewReader(data)).Decode(&doc); err != nil {
- t.Fatal(err)
- }
- if doc.To != "Anaïs" {
- t.Errorf(`Incorrect "to" field, got: %q`, doc.To)
- }
- if doc.From != "Jürgen 😀" {
- t.Errorf(`Incorrect "from" field, got: %q`, doc.From)
- }
- })
- }
- }
- }
- func TestXMLDocumentWithStrayBytesBeforeUTF8Document(t *testing.T) {
- type myXMLDocument struct {
- XMLName xml.Name `xml:"note"`
- To string `xml:"to"`
- }
- document := `<note><to>Anaïs</to></note>`
- prefixes := map[string]string{
- "little endian BOM": "\xFF\xFE",
- "big endian BOM": "\xFE\xFF",
- "NUL": "\x00",
- }
- for name, prefix := range prefixes {
- t.Run(name, func(t *testing.T) {
- var doc myXMLDocument
- if err := NewXMLDecoder(strings.NewReader(prefix + document)).Decode(&doc); err != nil {
- t.Fatal(err)
- }
- if doc.To != "Anaïs" {
- t.Errorf(`Incorrect "to" field, got: %q`, doc.To)
- }
- })
- }
- t.Run("NUL after the first character", func(t *testing.T) {
- var doc myXMLDocument
- if err := NewXMLDecoder(strings.NewReader("<\x00" + document[1:])).Decode(&doc); err != nil {
- t.Fatal(err)
- }
- if doc.To != "Anaïs" {
- t.Errorf(`Incorrect "to" field, got: %q`, doc.To)
- }
- })
- }
- func TestFilterValidXMLCharsWithInvalidUTF8Sequence(t *testing.T) {
- // Create input with invalid UTF-8 sequence
- input := []byte{0x41, 0xC0, 0xAF, 0x42} // 'A', invalid UTF-8, 'B'
- filtered := filterValidXMLChars(input)
- // The function would replace invalid UTF-8 with replacement char
- // rather than properly filtering
- if utf8.Valid(filtered) {
- r, _ := utf8.DecodeRune(filtered[1:])
- if r == utf8.RuneError {
- t.Error("Invalid UTF-8 was not properly filtered")
- }
- }
- }
- func FuzzFilterValidXMLChars(f *testing.F) {
- f.Fuzz(func(t *testing.T, s []byte) {
- filterValidXMLChars(s)
- })
- }
|