| 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.Fuzz(func(t *testing.T, d []byte) { |
| 99 | fromHTML(d) |
| 100 | }) |
| 101 | } |
| 102 | |
| 103 | var fromXMLTestCases = []struct { |
| 104 | in string |
| 105 | out string |
| 106 | }{{ |
| 107 | "", "", |
| 108 | }, { |
| 109 | " not <?xml start ", "", |
| 110 | }, { |
| 111 | " not <?xml start ", "", |
| 112 | }, { |
| 113 | "xml at end <?xml", "", |
| 114 | }, { |
| 115 | "xml at end and encoding <?xml encoding", "", |
| 116 | }, { |
| 117 | "xml at end and encoding= <?xml encoding=", "", |
| 118 | }, { |
| 119 | "xml at end and encoding=c <?xml encoding=c ", "c", |
| 120 | }, { |
| 121 | "xml is case sensitive <?XML encoding=c ", "", |
| 122 | }, { |
| 123 | "encoding is case sensitive too <?xml Encoding=c ", "", |
| 124 | }, { |
| 125 | `<?xml version="1.0" encoding=c ?>`, "c", |
| 126 | }, { |
| 127 | `<?xml version="1.0" encoding="c"?>`, "c", |
| 128 | }, { |
| 129 | ` <?xml version = "1.0" encoding = "c"?>`, "c", |
| 130 | }} |
| 131 | |
| 132 | func TestFromXML(t *testing.T) { |
| 133 | for _, tc := range fromXMLTestCases { |
| 134 | t.Run(tc.in, func(t *testing.T) { |
| 135 | got := fromXML([]byte(tc.in)) |
| 136 | if got != tc.out { |
| 137 | t.Errorf("got: %s, want: %s", got, tc.out) |
| 138 | } |
| 139 | }) |
| 140 | } |
| 141 | } |
| 142 | func FuzzFromXML(f *testing.F) { |
| 143 | for _, s := range fromXMLTestCases { |
| 144 | f.Add([]byte(s.in)) |
| 145 | } |
| 146 | |
| 147 | f.Fuzz(func(t *testing.T, d []byte) { |
| 148 | if charset := FromXML(d); charset == "" { |
| 149 | t.Skip() |
| 150 | } |
| 151 | }) |
| 152 | } |
| 153 | |
| 154 | func TestFromPlain(t *testing.T) { |
| 155 | tcases := []struct { |
| 156 | raw []byte |
| 157 | charset string |
| 158 | }{ |
| 159 | {[]byte{0xe6, 0xf8, 0xe5, 0x85, 0x85}, "windows-1252"}, |
| 160 | {[]byte{0xe6, 0xf8, 0xe5}, "iso-8859-1"}, |
| 161 | {[]byte("æøå"), "utf-8"}, |
| 162 | {[]byte{}, ""}, |
| 163 | } |
| 164 | for _, tc := range tcases { |
| 165 | if cs := FromPlain(tc.raw); cs != tc.charset { |
| 166 | t.Errorf("in: %v; expected: %s; got: %s", tc.raw, tc.charset, cs) |
| 167 | } |
| 168 | } |
| 169 | } |
| 170 | |
| 171 | func FuzzFromPlain(f *testing.F) { |
| 172 | samples := [][]byte{ |
| 173 | {0xe6, 0xf8, 0xe5, 0x85, 0x85}, |
| 174 | {0xe6, 0xf8, 0xe5}, |
| 175 | []byte("æøå"), |
| 176 | } |
| 177 | |
| 178 | for _, s := range samples { |
| 179 | f.Add(s) |
| 180 | } |
| 181 | |
| 182 | f.Fuzz(func(t *testing.T, d []byte) { |
| 183 | if charset := FromPlain(d); charset == "" { |
| 184 | t.Skip() |
| 185 | } |
| 186 | }) |
| 187 | } |
| 188 | |
| 189 | const xmlDoc = `<?xml version="1.0" encoding="UTF-8"?> |
| 190 | <note> |
| 191 | <to>Tove</to> |
| 192 | <from>Jani</from> |
| 193 | <heading>Reminder</heading> |
| 194 | <body>Don't forget me this weekend!</body> |
| 195 | </note>` |
| 196 | const htmlDoc = `<!DOCTYPE html> |
| 197 | <html> |
| 198 | <head><!--[if lt IE 9]><script language="javascript" type="text/javascript" src="//html5shim.googlecode.com/svn/trunk/html5.js"></script><![endif]--> |
| 199 | <meta charset="UTF-8"><style>/* |
| 200 | </style> |
| 201 | <link rel="stylesheet" href="css/animation.css"><!--[if IE 7]><link rel="stylesheet" href="css/" + font.fontname + "-ie7.css"><![endif]--> |
| 202 | <script> |
| 203 | </script> |
| 204 | </head> |
| 205 | <body> |
| 206 | <div class="container footer">さ</div> |
| 207 | </body> |
| 208 | </html>` |
| 209 | |
| 210 | func BenchmarkFromHTML(b *testing.B) { |
| 211 | b.ReportAllocs() |
| 212 | doc := []byte(htmlDoc) |
| 213 | for b.Loop() { |
| 214 | FromHTML(doc) |
| 215 | } |
| 216 | } |
| 217 | func BenchmarkFromXML(b *testing.B) { |
| 218 | b.ReportAllocs() |
| 219 | doc := []byte(xmlDoc) |
| 220 | for b.Loop() { |
| 221 | FromXML(doc) |
| 222 | } |
| 223 | } |
| 224 | func BenchmarkFromPlain(b *testing.B) { |
| 225 | b.ReportAllocs() |
| 226 | doc := []byte(xmlDoc) |
| 227 | for b.Loop() { |
| 228 | FromPlain(doc) |
| 229 | } |
| 230 | } |