| 1 |
package charset |
| 2 |
|
| 3 |
import ( |
| 4 |
"testing" |
| 5 |
|
| 6 |
"github.com/gabriel-vasile/mimetype/internal/scan" |
| 7 |
) |
| 8 |
|
| 9 |
var extractCharsetFromMetaTestCases = []struct { |
| 10 |
in string |
| 11 |
out string |
| 12 |
}{{ |
| 13 |
"", "", |
| 14 |
}, { |
| 15 |
"''", "", |
| 16 |
}, { |
| 17 |
`""`, "", |
| 18 |
}, { |
| 19 |
`charset`, "", |
| 20 |
}, { |
| 21 |
`charset=`, "", |
| 22 |
}, { |
| 23 |
`charset="`, "", |
| 24 |
}, { |
| 25 |
`charset=""`, "", |
| 26 |
}, { |
| 27 |
`charset="a"`, "a", |
| 28 |
}, { |
| 29 |
`charset="'a'"`, "'a'", |
| 30 |
}, { |
| 31 |
`charset = a`, "a", |
| 32 |
}, { |
| 33 |
`charset = a;`, "a", |
| 34 |
}} |
| 35 |
|
| 36 |
func TestExtractCharsetFromMeta(t *testing.T) { |
| 37 |
for _, tc := range extractCharsetFromMetaTestCases { |
| 38 |
t.Run(tc.in, func(t *testing.T) { |
| 39 |
got := extractCharsetFromMeta(scan.Bytes(tc.in)) |
| 40 |
if string(got) != tc.out { |
| 41 |
t.Errorf("got: %s, want: %s", got, tc.out) |
| 42 |
} |
| 43 |
}) |
| 44 |
} |
| 45 |
} |
| 46 |
func FuzzExtractCharsetFromMeta(f *testing.F) { |
| 47 |
for _, tc := range extractCharsetFromMetaTestCases { |
| 48 |
f.Add([]byte(tc.in)) |
| 49 |
} |
| 50 |
|
| 51 |
f.Fuzz(func(t *testing.T, d []byte) { |
| 52 |
extractCharsetFromMeta(d) |
| 53 |
}) |
| 54 |
} |
| 55 |
|
| 56 |
var fromHTMLTestCases = []struct { |
| 57 |
in string |
| 58 |
out string |
| 59 |
}{{ |
| 60 |
"", "", |
| 61 |
}, { |
| 62 |
"<!--> ", "", |
| 63 |
}, { |
| 64 |
"<not-meta", "", |
| 65 |
}, { |
| 66 |
"<meta", "", |
| 67 |
}, { |
| 68 |
"<meta=", "", |
| 69 |
}, { |
| 70 |
"<meta ", "", |
| 71 |
}, { |
| 72 |
`<meta content="text/html; charset=iso-8859-15">`, "", |
| 73 |
}, { |
| 74 |
`<meta http-equiv="content-type">`, "", |
| 75 |
}, { |
| 76 |
`<meta content="text/html; charset=iso-8859-15" http-equiv="content-type" >`, "iso-8859-15", |
| 77 |
}, { |
| 78 |
`<meta http-equiv="content-type" content="a/b; charset=щ">`, "щ", |
| 79 |
}, { |
| 80 |
`<f 1=2 /><meta b="b" charset="щ">`, "щ", |
| 81 |
}} |
| 82 |
|
| 83 |
func TestFromHTML(t *testing.T) { |
| 84 |
for _, tc := range fromHTMLTestCases { |
| 85 |
t.Run(tc.in, func(t *testing.T) { |
| 86 |
got := fromHTML([]byte(tc.in)) |
| 87 |
if got != tc.out { |
| 88 |
t.Errorf("got: %s, want: %s", got, tc.out) |
| 89 |
} |
| 90 |
}) |
| 91 |
} |
| 92 |
} |
| 93 |
func FuzzFromHTML(f *testing.F) { |
| 94 |
for _, tc := range fromHTMLTestCases { |
| 95 |
f.Add([]byte(tc.in)) |
| 96 |
} |
| 97 |
|
| 98 |
f.Add([]byte("\xef\xbb\xbf<meta charset=\"utf-8\">")) |
| 99 |
f.Add([]byte("\xfe\xff\x00a")) |
| 100 |
f.Add([]byte("\xff\xfea\x00")) |
| 101 |
|
| 102 |
f.Fuzz(func(t *testing.T, d []byte) { |
| 103 |
FromHTML(d) |
| 104 |
}) |
| 105 |
} |
| 106 |
|
| 107 |
var fromXMLTestCases = []struct { |
| 108 |
in string |
| 109 |
out string |
| 110 |
}{{ |
| 111 |
"", "", |
| 112 |
}, { |
| 113 |
" not <?xml start ", "", |
| 114 |
}, { |
| 115 |
" not <?xml start ", "", |
| 116 |
}, { |
| 117 |
"xml at end <?xml", "", |
| 118 |
}, { |
| 119 |
"xml at end and encoding <?xml encoding", "", |
| 120 |
}, { |
| 121 |
"xml at end and encoding= <?xml encoding=", "", |
| 122 |
}, { |
| 123 |
"xml at end and encoding=c <?xml encoding=c ", "c", |
| 124 |
}, { |
| 125 |
"xml is case sensitive <?XML encoding=c ", "", |
| 126 |
}, { |
| 127 |
"encoding is case sensitive too <?xml Encoding=c ", "", |
| 128 |
}, { |
| 129 |
`<?xml version="1.0" encoding=c ?>`, "c", |
| 130 |
}, { |
| 131 |
`<?xml version="1.0" encoding="c"?>`, "c", |
| 132 |
}, { |
| 133 |
` <?xml version = "1.0" encoding = "c"?>`, "c", |
| 134 |
}} |
| 135 |
|
| 136 |
func TestFromXML(t *testing.T) { |
| 137 |
for _, tc := range fromXMLTestCases { |
| 138 |
t.Run(tc.in, func(t *testing.T) { |
| 139 |
got := fromXML([]byte(tc.in)) |
| 140 |
if got != tc.out { |
| 141 |
t.Errorf("got: %s, want: %s", got, tc.out) |
| 142 |
} |
| 143 |
}) |
| 144 |
} |
| 145 |
} |
| 146 |
func FuzzFromXML(f *testing.F) { |
| 147 |
for _, s := range fromXMLTestCases { |
| 148 |
f.Add([]byte(s.in)) |
| 149 |
} |
| 150 |
|
| 151 |
f.Fuzz(func(t *testing.T, d []byte) { |
| 152 |
if charset := FromXML(d); charset == "" { |
| 153 |
t.Skip() |
| 154 |
} |
| 155 |
}) |
| 156 |
} |
| 157 |
|
| 158 |
func TestFromPlain(t *testing.T) { |
| 159 |
tcases := []struct { |
| 160 |
raw []byte |
| 161 |
charset string |
| 162 |
}{ |
| 163 |
{[]byte{0xe6, 0xf8, 0xe5, 0x85, 0x85}, "windows-1252"}, |
| 164 |
{[]byte{0xe6, 0xf8, 0xe5}, "iso-8859-1"}, |
| 165 |
{[]byte("æøå"), "utf-8"}, |
| 166 |
{[]byte{}, ""}, |
| 167 |
} |
| 168 |
for _, tc := range tcases { |
| 169 |
if cs := FromPlain(tc.raw); cs != tc.charset { |
| 170 |
t.Errorf("in: %v; expected: %s; got: %s", tc.raw, tc.charset, cs) |
| 171 |
} |
| 172 |
} |
| 173 |
} |
| 174 |
|
| 175 |
func FuzzFromPlain(f *testing.F) { |
| 176 |
samples := [][]byte{ |
| 177 |
{0xe6, 0xf8, 0xe5, 0x85, 0x85}, |
| 178 |
{0xe6, 0xf8, 0xe5}, |
| 179 |
[]byte("æøå"), |
| 180 |
} |
| 181 |
|
| 182 |
for _, s := range samples { |
| 183 |
f.Add(s) |
| 184 |
} |
| 185 |
|
| 186 |
f.Fuzz(func(t *testing.T, d []byte) { |
| 187 |
if charset := FromPlain(d); charset == "" { |
| 188 |
t.Skip() |
| 189 |
} |
| 190 |
}) |
| 191 |
} |
| 192 |
|
| 193 |
const xmlDoc = `<?xml version="1.0" encoding="UTF-8"?> |
| 194 |
<note> |
| 195 |
<to>Tove</to> |
| 196 |
<from>Jani</from> |
| 197 |
<heading>Reminder</heading> |
| 198 |
<body>Don't forget me this weekend!</body> |
| 199 |
</note>` |
| 200 |
const htmlDoc = `<!DOCTYPE html> |
| 201 |
<html> |
| 202 |
<head><!--[if lt IE 9]><script language="javascript" type="text/javascript" src="//html5shim.googlecode.com/svn/trunk/html5.js"></script><![endif]--> |
| 203 |
<meta charset="UTF-8"><style>/* |
| 204 |
</style> |
| 205 |
<link rel="stylesheet" href="css/animation.css"><!--[if IE 7]><link rel="stylesheet" href="css/" + font.fontname + "-ie7.css"><![endif]--> |
| 206 |
<script> |
| 207 |
</script> |
| 208 |
</head> |
| 209 |
<body> |
| 210 |
<div class="container footer">さ</div> |
| 211 |
</body> |
| 212 |
</html>` |
| 213 |
|
| 214 |
func BenchmarkFromHTML(b *testing.B) { |
| 215 |
b.ReportAllocs() |
| 216 |
doc := []byte(htmlDoc) |
| 217 |
for b.Loop() { |
| 218 |
FromHTML(doc) |
| 219 |
} |
| 220 |
} |
| 221 |
func BenchmarkFromXML(b *testing.B) { |
| 222 |
b.ReportAllocs() |
| 223 |
doc := []byte(xmlDoc) |
| 224 |
for b.Loop() { |
| 225 |
FromXML(doc) |
| 226 |
} |
| 227 |
} |
| 228 |
func BenchmarkFromPlain(b *testing.B) { |
| 229 |
b.ReportAllocs() |
| 230 |
doc := []byte(xmlDoc) |
| 231 |
for b.Loop() { |
| 232 |
FromPlain(doc) |
| 233 |
} |
| 234 |
} |