-
Notifications
You must be signed in to change notification settings - Fork 153
/
charset_test.go
112 lines (100 loc) · 2.53 KB
/
charset_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
package charset
import (
"testing"
)
const xmlDoc = `<?xml version="1.0" encoding="UTF-8"?>
<note>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>`
const htmlDoc = `<!DOCTYPE html>
<html>
<head><!--[if lt IE 9]><script language="javascript" type="text/javascript" src="//html5shim.googlecode.com/svn/trunk/html5.js"></script><![endif]-->
<meta charset="UTF-8"><style>/*
</style>
<link rel="stylesheet" href="css/animation.css"><!--[if IE 7]><link rel="stylesheet" href="css/" + font.fontname + "-ie7.css"><![endif]-->
<script>
</script>
</head>
<body>
<div class="container footer">さ</div>
</body>
</html>`
func TestFromXML(t *testing.T) {
charset := FromXML([]byte(xmlDoc))
if charset != "utf-8" {
t.Errorf("expected: utf-8; got: %s", charset)
}
}
func TestFromHTML(t *testing.T) {
charset := FromHTML([]byte(htmlDoc))
if charset != "utf-8" {
t.Errorf("expected: utf-8; got: %s", charset)
}
}
func TestFromPlain(t *testing.T) {
tcases := []struct {
raw []byte
charset string
}{
{[]byte{0xe6, 0xf8, 0xe5, 0x85, 0x85}, "windows-1252"},
{[]byte{0xe6, 0xf8, 0xe5}, "iso-8859-1"},
{[]byte("æøå"), "utf-8"},
{[]byte{}, ""},
}
for _, tc := range tcases {
if cs := FromPlain(tc.raw); cs != tc.charset {
t.Errorf("in: %v; expected: %s; got: %s", tc.raw, tc.charset, cs)
}
}
}
func FuzzFromPlain(f *testing.F) {
samples := [][]byte{
[]byte{0xe6, 0xf8, 0xe5, 0x85, 0x85},
[]byte{0xe6, 0xf8, 0xe5},
[]byte("æøå"),
}
for _, s := range samples {
f.Add(s)
}
f.Fuzz(func(t *testing.T, d []byte) {
if charset := FromPlain(d); charset == "" {
t.Skip()
}
})
}
func FuzzFromHTML(f *testing.F) {
samples := []string{
`<meta charset="c">`,
`<meta charset="щ">`,
`<meta http-equiv="content-type" content="a/b; charset=c">`,
`<meta http-equiv="content-type" content="a/b; charset=щ">`,
`<f 1=2 /><meta charset="c">`,
`<f a=2><meta http-equiv="content-type" content="a/b; charset=c">`,
`<f 1=2 /><meta b="b" charset="c">`,
`<f a=2><meta b="b" http-equiv="content-type" content="a/b; charset=c">`,
}
for _, s := range samples {
f.Add([]byte(s))
}
f.Fuzz(func(t *testing.T, d []byte) {
if charset := FromHTML(d); charset == "" {
t.Skip()
}
})
}
func FuzzFromXML(f *testing.F) {
samples := []string{
`<?xml version="1.0" encoding="c"?>`,
}
for _, s := range samples {
f.Add([]byte(s))
}
f.Fuzz(func(t *testing.T, d []byte) {
if charset := FromXML(d); charset == "" {
t.Skip()
}
})
}