charset_test.go 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. // Copyright 2013 The Go Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. package charset
  5. import (
  6. "bytes"
  7. "encoding/xml"
  8. "io/ioutil"
  9. "runtime"
  10. "strings"
  11. "testing"
  12. "golang.org/x/text/transform"
  13. )
  14. func transformString(t transform.Transformer, s string) (string, error) {
  15. r := transform.NewReader(strings.NewReader(s), t)
  16. b, err := ioutil.ReadAll(r)
  17. return string(b), err
  18. }
  19. var testCases = []struct {
  20. utf8, other, otherEncoding string
  21. }{
  22. {"Résumé", "Résumé", "utf8"},
  23. {"Résumé", "R\xe9sum\xe9", "latin1"},
  24. {"これは漢字です。", "S0\x8c0o0\"oW[g0Y0\x020", "UTF-16LE"},
  25. {"これは漢字です。", "0S0\x8c0oo\"[W0g0Y0\x02", "UTF-16BE"},
  26. {"Hello, world", "Hello, world", "ASCII"},
  27. {"Gdańsk", "Gda\xf1sk", "ISO-8859-2"},
  28. {"Ââ Čč Đđ Ŋŋ Õõ Šš Žž Åå Ää", "\xc2\xe2 \xc8\xe8 \xa9\xb9 \xaf\xbf \xd5\xf5 \xaa\xba \xac\xbc \xc5\xe5 \xc4\xe4", "ISO-8859-10"},
  29. {"สำหรับ", "\xca\xd3\xcb\xc3\u047a", "ISO-8859-11"},
  30. {"latviešu", "latvie\xf0u", "ISO-8859-13"},
  31. {"Seònaid", "Se\xf2naid", "ISO-8859-14"},
  32. {"€1 is cheap", "\xa41 is cheap", "ISO-8859-15"},
  33. {"românește", "rom\xe2ne\xbate", "ISO-8859-16"},
  34. {"nutraĵo", "nutra\xbco", "ISO-8859-3"},
  35. {"Kalâdlit", "Kal\xe2dlit", "ISO-8859-4"},
  36. {"русский", "\xe0\xe3\xe1\xe1\xda\xd8\xd9", "ISO-8859-5"},
  37. {"ελληνικά", "\xe5\xeb\xeb\xe7\xed\xe9\xea\xdc", "ISO-8859-7"},
  38. {"Kağan", "Ka\xf0an", "ISO-8859-9"},
  39. {"Résumé", "R\x8esum\x8e", "macintosh"},
  40. {"Gdańsk", "Gda\xf1sk", "windows-1250"},
  41. {"русский", "\xf0\xf3\xf1\xf1\xea\xe8\xe9", "windows-1251"},
  42. {"Résumé", "R\xe9sum\xe9", "windows-1252"},
  43. {"ελληνικά", "\xe5\xeb\xeb\xe7\xed\xe9\xea\xdc", "windows-1253"},
  44. {"Kağan", "Ka\xf0an", "windows-1254"},
  45. {"עִבְרִית", "\xf2\xc4\xe1\xc0\xf8\xc4\xe9\xfa", "windows-1255"},
  46. {"العربية", "\xc7\xe1\xda\xd1\xc8\xed\xc9", "windows-1256"},
  47. {"latviešu", "latvie\xf0u", "windows-1257"},
  48. {"Việt", "Vi\xea\xf2t", "windows-1258"},
  49. {"สำหรับ", "\xca\xd3\xcb\xc3\u047a", "windows-874"},
  50. {"русский", "\xd2\xd5\xd3\xd3\xcb\xc9\xca", "KOI8-R"},
  51. {"українська", "\xd5\xcb\xd2\xc1\xa7\xce\xd3\xd8\xcb\xc1", "KOI8-U"},
  52. {"Hello 常用國字標準字體表", "Hello \xb1`\xa5\u03b0\xea\xa6r\xbc\u0437\u01e6r\xc5\xe9\xaa\xed", "big5"},
  53. {"Hello 常用國字標準字體表", "Hello \xb3\xa3\xd3\xc3\x87\xf8\xd7\xd6\x98\xcb\x9c\xca\xd7\xd6\xf3\x77\xb1\xed", "gbk"},
  54. {"Hello 常用國字標準字體表", "Hello \xb3\xa3\xd3\xc3\x87\xf8\xd7\xd6\x98\xcb\x9c\xca\xd7\xd6\xf3\x77\xb1\xed", "gb18030"},
  55. {"עִבְרִית", "\x81\x30\xfb\x30\x81\x30\xf6\x34\x81\x30\xf9\x33\x81\x30\xf6\x30\x81\x30\xfb\x36\x81\x30\xf6\x34\x81\x30\xfa\x31\x81\x30\xfb\x38", "gb18030"},
  56. {"㧯", "\x82\x31\x89\x38", "gb18030"},
  57. {"これは漢字です。", "\x82\xb1\x82\xea\x82\xcd\x8a\xbf\x8e\x9a\x82\xc5\x82\xb7\x81B", "SJIS"},
  58. {"Hello, 世界!", "Hello, \x90\xa2\x8aE!", "SJIS"},
  59. {"イウエオカ", "\xb2\xb3\xb4\xb5\xb6", "SJIS"},
  60. {"これは漢字です。", "\xa4\xb3\xa4\xec\xa4\u03f4\xc1\xbb\xfa\xa4\u01e4\xb9\xa1\xa3", "EUC-JP"},
  61. {"Hello, 世界!", "Hello, \x1b$B@$3&\x1b(B!", "ISO-2022-JP"},
  62. {"네이트 | 즐거움의 시작, 슈파스(Spaβ) NATE", "\xb3\xd7\xc0\xcc\xc6\xae | \xc1\xf1\xb0\xc5\xbf\xf2\xc0\xc7 \xbd\xc3\xc0\xdb, \xbd\xb4\xc6\xc4\xbd\xba(Spa\xa5\xe2) NATE", "EUC-KR"},
  63. }
  64. func TestDecode(t *testing.T) {
  65. for _, tc := range testCases {
  66. e, _ := Lookup(tc.otherEncoding)
  67. if e == nil {
  68. t.Errorf("%s: not found", tc.otherEncoding)
  69. continue
  70. }
  71. s, err := transformString(e.NewDecoder(), tc.other)
  72. if err != nil {
  73. t.Errorf("%s: decode %q: %v", tc.otherEncoding, tc.other, err)
  74. continue
  75. }
  76. if s != tc.utf8 {
  77. t.Errorf("%s: got %q, want %q", tc.otherEncoding, s, tc.utf8)
  78. }
  79. }
  80. }
  81. func TestEncode(t *testing.T) {
  82. for _, tc := range testCases {
  83. e, _ := Lookup(tc.otherEncoding)
  84. if e == nil {
  85. t.Errorf("%s: not found", tc.otherEncoding)
  86. continue
  87. }
  88. s, err := transformString(e.NewEncoder(), tc.utf8)
  89. if err != nil {
  90. t.Errorf("%s: encode %q: %s", tc.otherEncoding, tc.utf8, err)
  91. continue
  92. }
  93. if s != tc.other {
  94. t.Errorf("%s: got %q, want %q", tc.otherEncoding, s, tc.other)
  95. }
  96. }
  97. }
  98. // TestNames verifies that you can pass an encoding's name to Lookup and get
  99. // the same encoding back (except for "replacement").
  100. func TestNames(t *testing.T) {
  101. for _, e := range encodings {
  102. if e.name == "replacement" {
  103. continue
  104. }
  105. _, got := Lookup(e.name)
  106. if got != e.name {
  107. t.Errorf("got %q, want %q", got, e.name)
  108. continue
  109. }
  110. }
  111. }
  112. var sniffTestCases = []struct {
  113. filename, declared, want string
  114. }{
  115. {"HTTP-charset.html", "text/html; charset=iso-8859-15", "iso-8859-15"},
  116. {"UTF-16LE-BOM.html", "", "utf-16le"},
  117. {"UTF-16BE-BOM.html", "", "utf-16be"},
  118. {"meta-content-attribute.html", "text/html", "iso-8859-15"},
  119. {"meta-charset-attribute.html", "text/html", "iso-8859-15"},
  120. {"No-encoding-declaration.html", "text/html", "utf-8"},
  121. {"HTTP-vs-UTF-8-BOM.html", "text/html; charset=iso-8859-15", "utf-8"},
  122. {"HTTP-vs-meta-content.html", "text/html; charset=iso-8859-15", "iso-8859-15"},
  123. {"HTTP-vs-meta-charset.html", "text/html; charset=iso-8859-15", "iso-8859-15"},
  124. {"UTF-8-BOM-vs-meta-content.html", "text/html", "utf-8"},
  125. {"UTF-8-BOM-vs-meta-charset.html", "text/html", "utf-8"},
  126. }
  127. func TestSniff(t *testing.T) {
  128. switch runtime.GOOS {
  129. case "nacl": // platforms that don't permit direct file system access
  130. t.Skipf("not supported on %q", runtime.GOOS)
  131. }
  132. for _, tc := range sniffTestCases {
  133. content, err := ioutil.ReadFile("testdata/" + tc.filename)
  134. if err != nil {
  135. t.Errorf("%s: error reading file: %v", tc.filename, err)
  136. continue
  137. }
  138. _, name, _ := DetermineEncoding(content, tc.declared)
  139. if name != tc.want {
  140. t.Errorf("%s: got %q, want %q", tc.filename, name, tc.want)
  141. continue
  142. }
  143. }
  144. }
  145. func TestReader(t *testing.T) {
  146. switch runtime.GOOS {
  147. case "nacl": // platforms that don't permit direct file system access
  148. t.Skipf("not supported on %q", runtime.GOOS)
  149. }
  150. for _, tc := range sniffTestCases {
  151. content, err := ioutil.ReadFile("testdata/" + tc.filename)
  152. if err != nil {
  153. t.Errorf("%s: error reading file: %v", tc.filename, err)
  154. continue
  155. }
  156. r, err := NewReader(bytes.NewReader(content), tc.declared)
  157. if err != nil {
  158. t.Errorf("%s: error creating reader: %v", tc.filename, err)
  159. continue
  160. }
  161. got, err := ioutil.ReadAll(r)
  162. if err != nil {
  163. t.Errorf("%s: error reading from charset.NewReader: %v", tc.filename, err)
  164. continue
  165. }
  166. e, _ := Lookup(tc.want)
  167. want, err := ioutil.ReadAll(transform.NewReader(bytes.NewReader(content), e.NewDecoder()))
  168. if err != nil {
  169. t.Errorf("%s: error decoding with hard-coded charset name: %v", tc.filename, err)
  170. continue
  171. }
  172. if !bytes.Equal(got, want) {
  173. t.Errorf("%s: got %q, want %q", tc.filename, got, want)
  174. continue
  175. }
  176. }
  177. }
  178. var metaTestCases = []struct {
  179. meta, want string
  180. }{
  181. {"", ""},
  182. {"text/html", ""},
  183. {"text/html; charset utf-8", ""},
  184. {"text/html; charset=latin-2", "latin-2"},
  185. {"text/html; charset; charset = utf-8", "utf-8"},
  186. {`charset="big5"`, "big5"},
  187. {"charset='shift_jis'", "shift_jis"},
  188. }
  189. func TestFromMeta(t *testing.T) {
  190. for _, tc := range metaTestCases {
  191. got := fromMetaElement(tc.meta)
  192. if got != tc.want {
  193. t.Errorf("%q: got %q, want %q", tc.meta, got, tc.want)
  194. }
  195. }
  196. }
  197. func TestXML(t *testing.T) {
  198. const s = "<?xml version=\"1.0\" encoding=\"windows-1252\"?><a><Word>r\xe9sum\xe9</Word></a>"
  199. d := xml.NewDecoder(strings.NewReader(s))
  200. d.CharsetReader = NewReaderByName
  201. var a struct {
  202. Word string
  203. }
  204. err := d.Decode(&a)
  205. if err != nil {
  206. t.Fatalf("Decode: %v", err)
  207. }
  208. want := "résumé"
  209. if a.Word != want {
  210. t.Errorf("got %q, want %q", a.Word, want)
  211. }
  212. }