encoding_test.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. // SPDX-FileCopyrightText: Copyright The Miniflux Authors. All rights reserved.
  2. // SPDX-License-Identifier: Apache-2.0
  3. package encoding // import "miniflux.app/v2/internal/reader/encoding"
  4. import (
  5. "io"
  6. "os"
  7. "testing"
  8. "unicode/utf8"
  9. )
  10. func TestCharsetReaderWithUTF8(t *testing.T) {
  11. file := "testdata/utf8.xml"
  12. f, err := os.Open(file)
  13. if err != nil {
  14. t.Fatalf("Unable to open file: %v", err)
  15. }
  16. reader, err := CharsetReader("UTF-8", f)
  17. if err != nil {
  18. t.Fatalf("Unable to create reader: %v", err)
  19. }
  20. data, err := io.ReadAll(reader)
  21. if err != nil {
  22. t.Fatalf("Unable to read data: %v", err)
  23. }
  24. if !utf8.Valid(data) {
  25. t.Fatalf("Data is not valid UTF-8")
  26. }
  27. }
  28. func TestCharsetReaderWithISO88591(t *testing.T) {
  29. file := "testdata/iso-8859-1.xml"
  30. f, err := os.Open(file)
  31. if err != nil {
  32. t.Fatalf("Unable to open file: %v", err)
  33. }
  34. reader, err := CharsetReader("ISO-8859-1", f)
  35. if err != nil {
  36. t.Fatalf("Unable to create reader: %v", err)
  37. }
  38. data, err := io.ReadAll(reader)
  39. if err != nil {
  40. t.Fatalf("Unable to read data: %v", err)
  41. }
  42. if !utf8.Valid(data) {
  43. t.Fatalf("Data is not valid UTF-8")
  44. }
  45. }
  46. func TestCharsetReaderWithWindows1252(t *testing.T) {
  47. file := "testdata/windows-1252.xml"
  48. f, err := os.Open(file)
  49. if err != nil {
  50. t.Fatalf("Unable to open file: %v", err)
  51. }
  52. reader, err := CharsetReader("windows-1252", f)
  53. if err != nil {
  54. t.Fatalf("Unable to create reader: %v", err)
  55. }
  56. data, err := io.ReadAll(reader)
  57. if err != nil {
  58. t.Fatalf("Unable to read data: %v", err)
  59. }
  60. if !utf8.Valid(data) {
  61. t.Fatalf("Data is not valid UTF-8")
  62. }
  63. }
  64. func TestCharsetReaderWithInvalidProlog(t *testing.T) {
  65. file := "testdata/invalid-prolog.xml"
  66. f, err := os.Open(file)
  67. if err != nil {
  68. t.Fatalf("Unable to open file: %v", err)
  69. }
  70. reader, err := CharsetReader("invalid", f)
  71. if err != nil {
  72. t.Fatalf("Unable to create reader: %v", err)
  73. }
  74. data, err := io.ReadAll(reader)
  75. if err != nil {
  76. t.Fatalf("Unable to read data: %v", err)
  77. }
  78. if !utf8.Valid(data) {
  79. t.Fatalf("Data is not valid UTF-8")
  80. }
  81. }
  82. func TestCharsetReaderWithUTF8DocumentWithIncorrectProlog(t *testing.T) {
  83. file := "testdata/utf8-incorrect-prolog.xml"
  84. f, err := os.Open(file)
  85. if err != nil {
  86. t.Fatalf("Unable to open file: %v", err)
  87. }
  88. reader, err := CharsetReader("ISO-8859-1", f)
  89. if err != nil {
  90. t.Fatalf("Unable to create reader: %v", err)
  91. }
  92. data, err := io.ReadAll(reader)
  93. if err != nil {
  94. t.Fatalf("Unable to read data: %v", err)
  95. }
  96. if !utf8.Valid(data) {
  97. t.Fatalf("Data is not valid UTF-8")
  98. }
  99. }
  100. func TestCharsetReaderWithWindows1252DocumentWithIncorrectProlog(t *testing.T) {
  101. file := "testdata/windows-1252-incorrect-prolog.xml"
  102. f, err := os.Open(file)
  103. if err != nil {
  104. t.Fatalf("Unable to open file: %v", err)
  105. }
  106. reader, err := CharsetReader("windows-1252", f)
  107. if err != nil {
  108. t.Fatalf("Unable to create reader: %v", err)
  109. }
  110. data, err := io.ReadAll(reader)
  111. if err != nil {
  112. t.Fatalf("Unable to read data: %v", err)
  113. }
  114. if !utf8.Valid(data) {
  115. t.Fatalf("Data is not valid UTF-8")
  116. }
  117. }