| 1 |
package mimetype |
| 2 |
|
| 3 |
import ( |
| 4 |
"bytes" |
| 5 |
"fmt" |
| 6 |
"io" |
| 7 |
"math" |
| 8 |
"math/rand" |
| 9 |
"mime" |
| 10 |
"os" |
| 11 |
"strings" |
| 12 |
"sync" |
| 13 |
"testing" |
| 14 |
) |
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
type testcase struct { |
| 22 |
name string |
| 23 |
data string |
| 24 |
expectedMIME string |
| 25 |
bench string |
| 26 |
} |
| 27 |
|
| 28 |
var testcases = []testcase{ |
| 29 |
{"3gpp2", "\x00\x00\x00\x18ftyp3g24", "video/3gpp2", one}, |
| 30 |
{"3gpp2 without ftyp", "\x00\x00\x00\x18mtyp3g24", "application/octet-stream", none}, |
| 31 |
{"3gp", "\x00\x00\x00\x18ftyp3gp1", "video/3gpp", one}, |
| 32 |
{ |
| 33 |
"3mf", |
| 34 |
`<?xml version="1.0"?><model xmlns="http://schemas.microsoft.com/3dmanufacturing/core/2015/02">`, |
| 35 |
"application/vnd.ms-package.3dmanufacturing-3dmodel+xml", |
| 36 |
one, |
| 37 |
}, |
| 38 |
{"7z", "\x37\x7A\xBC\xAF\x27\x1C", "application/x-7z-compressed", all}, |
| 39 |
{"a", "\x21\x3C\x61\x72\x63\x68\x3E", "application/x-archive", one}, |
| 40 |
{"aac 1", "\xFF\xF1", "audio/aac", one}, |
| 41 |
{"aac 2", "\xFF\xF9", "audio/aac", none}, |
| 42 |
{"accdb", offset(4, "Standard ACE DB"), "application/x-msaccess", none}, |
| 43 |
{"aiff", "\x46\x4F\x52\x4D\x00\x00\x00\x00\x41\x49\x46\x46\x00", "audio/aiff", one}, |
| 44 |
{"amf", `<?xml version="1.0"?><amf>`, "application/x-amf", one}, |
| 45 |
{"amr", "\x23\x21\x41\x4D\x52", "audio/amr", one}, |
| 46 |
{"ape", "\x4D\x41\x43\x20\x96\x0F\x00\x00\x34\x00\x00\x00\x18\x00\x00\x00\x90\xE3", "audio/ape", one}, |
| 47 |
{"apng", "\x89\x50\x4E\x47\x0D\x0A\x1A\x0A" + offset(29, "acTL"), "image/vnd.mozilla.apng", all}, |
| 48 |
{"asf", "\x30\x26\xB2\x75\x8E\x66\xCF\x11\xA6\xD9\x00\xAA\x00\x62\xCE\x6C", "video/x-ms-asf", one}, |
| 49 |
{"atom", `<?xml version="1.0"?><feed xmlns="http://www.w3.org/2005/Atom">`, "application/atom+xml", one}, |
| 50 |
{"au", "\x2E\x73\x6E\x64", "audio/basic", one}, |
| 51 |
{"avi", "RIFF\x00\x00\x00\x00AVI LIST\x00", "video/x-msvideo", all}, |
| 52 |
{"avif", "\x00\x00\x00\x18ftypavif", "image/avif", all}, |
| 53 |
{"avis", "\x00\x00\x00\x18ftypavis", "image/avif", all}, |
| 54 |
{"bmp\x30", "BM \x30\x00\x00\x00", "image/bmp", all}, |
| 55 |
{"bmp\x18", "BM \x18\x00\x00\x00", "image/bmp", none}, |
| 56 |
{"bmp\x10", "BM \x10\x00\x00\x00", "image/bmp", none}, |
| 57 |
{"bmp\x40", "BM \x40\x00\x00\x00", "image/bmp", none}, |
| 58 |
{"bmp\x34", "BM \x34\x00\x00\x00", "image/bmp", none}, |
| 59 |
{"bmp\x38", "BM \x38\x00\x00\x00", "image/bmp", none}, |
| 60 |
{"bmp\x28", "BM \x28\x00\x00\x00", "image/bmp", none}, |
| 61 |
{"bmp\x7C", "BM \x7C\x00\x00\x00", "image/bmp", none}, |
| 62 |
{"bmp\x6C", "BM \x6C\x00\x00\x00", "image/bmp", none}, |
| 63 |
{"bpg", "\x42\x50\x47\xFB", "image/bpg", one}, |
| 64 |
{"bufr", "BUFR \x03", "application/bufr", one}, |
| 65 |
{"bz2", "\x42\x5A\x68", "application/x-bzip2", one}, |
| 66 |
{"cab", "MSCF\x00\x00\x00\x00", "application/vnd.ms-cab-compressed", one}, |
| 67 |
{"cab.is", "ISc(\x00\x00\x00\x01", "application/x-installshield", one}, |
| 68 |
{"chm", "ITSF\003\000\000\000\x60\000\000\000", "application/vnd.ms-htmlhelp", one}, |
| 69 |
{"class", "\xCA\xFE\xBA\xBE\x00\x00\x00\xFF", "application/x-java-applet", one}, |
| 70 |
{ |
| 71 |
"crx", |
| 72 |
"Cr24\x00\x00\x00\x00\x01\x00\x00\x00\x0F\x00\x00\x00" + offset(16, "") + "\x50\x4B\x03\x04", |
| 73 |
"application/x-chrome-extension", |
| 74 |
one, |
| 75 |
}, |
| 76 |
{ |
| 77 |
"csv", |
| 78 |
`1,2 |
| 79 |
"abc","def" |
| 80 |
a,"b`, |
| 81 |
"text/csv", |
| 82 |
all, |
| 83 |
}, |
| 84 |
{ |
| 85 |
`csv with \r\n`, |
| 86 |
"1,2\r\n3,4\r\na,b", |
| 87 |
"text/csv", |
| 88 |
none, |
| 89 |
}, |
| 90 |
{"cpio 7", "070707", "application/x-cpio", one}, |
| 91 |
{"cpio 1", "070701", "application/x-cpio", none}, |
| 92 |
{"cpio 2", "070702", "application/x-cpio", none}, |
| 93 |
{"cpio bin", "\xC7\x71____", "application/x-cpio", none}, |
| 94 |
{"dae", `<?xml version="1.0"?><COLLADA xmlns="http://www.collada.org/2005/11/COLLADASchema">`, "model/vnd.collada+xml", one}, |
| 95 |
{"dbf", "\x03\x5f\x07\x1a\x96\x0f\x00\x00\xc1\x00\xa3\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x6f\x73\x6d\x5f\x69\x64\x00\x00\x00\x00\x00\x43\x00\x00\x00\x00\x0a\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x63\x6f\x64\x65", "application/x-dbf", one}, |
| 96 |
{"dcm", offset(128, "\x44\x49\x43\x4D"), "application/dicom", one}, |
| 97 |
{"deb", "\x21\x3c\x61\x72\x63\x68\x3e\x0a\x64\x65\x62\x69\x61\x6e\x2d\x62\x69\x6e\x61\x72\x79", "application/vnd.debian.binary-package", all}, |
| 98 |
{"djvu", "\x41\x54\x26\x54\x46\x4F\x52\x4D\x00\x00\x00\x00DJVU", "image/vnd.djvu", one}, |
| 99 |
{"djvuM", "\x41\x54\x26\x54\x46\x4F\x52\x4D\x00\x00\x00\x00DJVM", "image/vnd.djvu", none}, |
| 100 |
{"djvuI", "\x41\x54\x26\x54\x46\x4F\x52\x4D\x00\x00\x00\x00DJVI", "image/vnd.djvu", none}, |
| 101 |
{"djvuTHUM", "\x41\x54\x26\x54\x46\x4F\x52\x4D\x00\x00\x00\x00THUM", "image/vnd.djvu", none}, |
| 102 |
{"doc", fromDisk("doc.doc"), "application/msword", all}, |
| 103 |
{"docx", fromDisk("docx.docx"), "application/vnd.openxmlformats-officedocument.wordprocessingml.document", all}, |
| 104 |
{"rpm 1", "\xed\xab\xee\xdb", "application/x-rpm", one}, |
| 105 |
{"rpm 2", "drpm", "application/x-rpm", none}, |
| 106 |
{"dwg", "\x41\x43\x31\x30\x32\x34", "image/vnd.dwg", none}, |
| 107 |
{"dxf1", " 0\x0ASECTION\x0A", "image/vnd.dxf", one}, |
| 108 |
{"dxf2", " 0\x0D\x0ASECTION\x0D\x0A", "image/vnd.dxf", none}, |
| 109 |
{"dxf3", "0\x0ASECTION\x0A", "image/vnd.dxf", none}, |
| 110 |
{"dxf4", "0\x0D\x0ASECTION\x0D\x0A", "image/vnd.dxf", none}, |
| 111 |
{"eot", "\xbe\x45\x00\x00\xfa\x44\x00\x00\x02\x00\x02\x00\x04\x00\x00\x00\x02\x00\x00\x00\x00\x00\x00\x00\x00\x00\x01\x00\x90\x01\x00\x00\x00\x00\x4c\x50", "application/vnd.ms-fontobject", one}, |
| 112 |
{"epub", "\x50\x4B\x03\x04" + offset(26, "mimetypeapplication/epub+zip"), "application/epub+zip", all}, |
| 113 |
{"exe", "\x4D\x5A", "application/vnd.microsoft.portable-executable", all}, |
| 114 |
{"fdf", "%FDF", "application/vnd.fdf", one}, |
| 115 |
{"fits", "\x53\x49\x4d\x50\x4c\x45\x20\x20\x3d\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x54", "application/fits", one}, |
| 116 |
{"flac", "\x66\x4C\x61\x43\x00\x00\x00\x22", "audio/flac", one}, |
| 117 |
{"flv", "\x46\x4C\x56\x01", "video/x-flv", one}, |
| 118 |
{"fm", "<MakerFile \x00", "application/vnd.framemaker", one}, |
| 119 |
{"fmdict", "<MakerDictionary \x00", "application/vnd.framemaker", one}, |
| 120 |
{"fmbook", "<BooKFilE \x00", "application/vnd.framemaker", one}, |
| 121 |
{"gbr", offset(20, "GIMP"), "image/x-gimp-gbr", one}, |
| 122 |
{"geojson", `{"type":"Feature"}`, "application/geo+json", one}, |
| 123 |
{"geojson with space", `{ "type" : "Feature" }`, "application/geo+json", none}, |
| 124 |
{"gltf1", `{"asset":{"version":"1.0"}}`, "model/gltf+json", none}, |
| 125 |
{"gltf2", `{"asset":{"version":"2.0"}}`, "model/gltf+json", none}, |
| 126 |
{"gif 87", "GIF87a", "image/gif", all}, |
| 127 |
{"gif 89", "GIF89a", "image/gif", none}, |
| 128 |
{"glb 1", "\x67\x6C\x54\x46\x02\x00\x00\x00", "model/gltf-binary", one}, |
| 129 |
{"glb 2", "\x67\x6C\x54\x46\x01\x00\x00\x00", "model/gltf-binary", none}, |
| 130 |
{"gml", `<?xml version="1.0"?><any xmlns:gml="http://www.opengis.net/gml">`, "application/gml+xml", one}, |
| 131 |
{"gml3.2", `<?xml version="1.0"?><any xmlns:gml="http://www.opengis.net/gml/3.2">`, "application/gml+xml", none}, |
| 132 |
{"gml3.3", `<?xml version="1.0"?><any xmlns:gml="http://www.opengis.net/gml/3.3/exr">`, "application/gml+xml", none}, |
| 133 |
{"gpx", `<?xml version="1.0"?><gpx xmlns="http://www.topografix.com/GPX/1/1">`, "application/gpx+xml", one}, |
| 134 |
{"grib1", "GRIB\x00\x00\x00\x01", "application/grib", one}, |
| 135 |
{"grib2", "GRIB\x00\x00\x00\x02", "application/grib", none}, |
| 136 |
{"gz", "\x1F\x8B", "application/gzip", all}, |
| 137 |
{"har", `{"log":{ "version": "1.2"}}`, "application/json", one}, |
| 138 |
{"hdr", "#?RADIANCE\n", "image/vnd.radiance", one}, |
| 139 |
{"heic", "\x00\x00\x00\x18ftypheic", "image/heic", one}, |
| 140 |
{"heix", "\x00\x00\x00\x18ftypheix", "image/heic", none}, |
| 141 |
{"heif mif1", "\x00\x00\x00\x18ftypmif1", "image/heif", one}, |
| 142 |
{"heif heim", "\x00\x00\x00\x18ftypheim", "image/heif", none}, |
| 143 |
{"heif heis", "\x00\x00\x00\x18ftypheis", "image/heif", none}, |
| 144 |
{"heif avic", "\x00\x00\x00\x18ftypavic", "image/heif", none}, |
| 145 |
{"html", `<HtMl><bOdY>blah blah blah</body></html>`, "text/html; charset=utf-8", all}, |
| 146 |
{"html empty", `<HTML></HTML>`, "text/html; charset=utf-8", none}, |
| 147 |
{"html just header", ` <!DOCTYPE HTML>...`, "text/html; charset=utf-8", none}, |
| 148 |
{"line ending before html", "\r\n<html>...", "text/html; charset=utf-8", none}, |
| 149 |
{ |
| 150 |
"html with encoding", |
| 151 |
`<Html><Head><metA Http-EquiV="Content-Type" Content="text/html; charset=iso-8859-1">`, |
| 152 |
"text/html; charset=iso-8859-1", |
| 153 |
none, |
| 154 |
}, |
| 155 |
{ |
| 156 |
"html with comment prefix", |
| 157 |
`<!-- this comment should not affect --><html><head>`, |
| 158 |
"text/html; charset=utf-8", |
| 159 |
none, |
| 160 |
}, |
| 161 |
{"ico 01", "\x00\x00\x01\x00", "image/x-icon", one}, |
| 162 |
{"ico 02", "\x00\x00\x02\x00", "image/x-icon", none}, |
| 163 |
{"ics", "BEGIN:VCALENDAR\n00", "text/calendar", one}, |
| 164 |
{"ics dos", "BEGIN:VCALENDAR\r\n00", "text/calendar", none}, |
| 165 |
{"inf os2", "HSP\x01\x9b\x00", "application/x-os2-inf", none}, |
| 166 |
{"hlp os2", "HSP\x10\x9b\x00", "application/x-os2-hlp", none}, |
| 167 |
{"txt iso88591", "\x0a\xe6\xf8\xe6\xf8\xe5\xe6\xf8\xe5\xe5\x0a", "text/plain; charset=iso-8859-1", none}, |
| 168 |
{"jar", fromDisk("jar.jar"), "application/java-archive", all}, |
| 169 |
{"jar executable", "PK\x03\x04" + offset(0x1A, "\xFE\xCA"), "application/java-archive", none}, |
| 170 |
{"jar in zip #639", fromDisk("jar_in_zip.zip"), "application/zip", none}, |
| 171 |
{"jp2", "\x00\x00\x00\x0c\x6a\x50\x20\x20\x0d\x0a\x87\x0a\x00\x00\x00\x14\x66\x74\x79\x70\x6a\x70\x32\x20", "image/jp2", one}, |
| 172 |
{"jpf", "\x00\x00\x00\x0c\x6a\x50\x20\x20\x0d\x0a\x87\x0a\x00\x00\x00\x1c\x66\x74\x79\x70\x6a\x70\x78\x20", "image/jpx", one}, |
| 173 |
{"jpg", "\xFF\xD8\xFF", "image/jpeg", one}, |
| 174 |
{"jpm", "\x00\x00\x00\x0c\x6a\x50\x20\x20\x0d\x0a\x87\x0a\x00\x00\x00\x14\x66\x74\x79\x70\x6a\x70\x6d\x20", "image/jpm", one}, |
| 175 |
{"jxl 1", "\xFF\x0A", "image/jxl", one}, |
| 176 |
{"jxl 2", "\x00\x00\x00\x0cJXL\x20\x0d\x0a\x87\x0a", "image/jxl", none}, |
| 177 |
{"jxr", "\x49\x49\xBC\x01", "image/jxr", one}, |
| 178 |
{"xpm", "\x2F\x2A\x20\x58\x50\x4D\x20\x2A\x2F", "image/x-xpixmap", one}, |
| 179 |
{"js", "#!/bin/node ", "text/javascript", one}, |
| 180 |
{"json", `{"a":"b", "c":[{"a":"b"},1,true,false,"abc"]}`, "application/json", all}, |
| 181 |
{"json issue#239", "{\x0A\x09\x09\"key\":\"val\"}\x0A", "application/json", none}, |
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
{"json.int.txt", "1", "text/plain; charset=utf-8", none}, |
| 186 |
{"json.float.txt", "1.5", "text/plain; charset=utf-8", none}, |
| 187 |
{"json.string.txt", `"some string"`, "text/plain; charset=utf-8", none}, |
| 188 |
{"kml 2.2", `<?xml version="1.0"?><kml xmlns="http://www.opengis.net/kml/2.2">`, "application/vnd.google-earth.kml+xml", one}, |
| 189 |
{"kml 2.0", `<?xml version="1.0"?><kml xmlns="http://earth.google.com/kml/2.0">`, "application/vnd.google-earth.kml+xml", none}, |
| 190 |
{"kml 2.1", `<?xml version="1.0"?><kml xmlns="http://earth.google.com/kml/2.1">`, "application/vnd.google-earth.kml+xml", none}, |
| 191 |
{"kml 2.2", `<?xml version="1.0"?><kml xmlns="http://earth.google.com/kml/2.2">`, "application/vnd.google-earth.kml+xml", none}, |
| 192 |
{"kmz", "\x50\x4b\x03\x04\x14\x00\x00\x00\x08\x00\xe6\x6c\x04\x5b\xfd\xf4\xf2\x45\x41\x00\x00\x00\x43\x00\x00\x00\x07\x00\x1c\x00doc.kml", "application/vnd.google-earth.kmz", none}, |
| 193 |
{"lit", "ITOLITLS", "application/x-ms-reader", one}, |
| 194 |
{"lotus1", "\x00\x00\x02\x00456\x00" + offset(13, ""), "application/vnd.lotus-1-2-3", one}, |
| 195 |
{"lotus2", "\x00\x00\x1a\x00" + offset(16, "\x01"), "application/vnd.lotus-1-2-3", one}, |
| 196 |
{"lua", "#!/usr/bin/lua", "text/x-lua", one}, |
| 197 |
{"lua space", "#! /usr/bin/lua", "text/x-lua", none}, |
| 198 |
{"lz", "\x4c\x5a\x49\x50", "application/lzip", one}, |
| 199 |
{"m3u", "#EXTM3U\n", "application/vnd.apple.mpegurl", one}, |
| 200 |
{"m4a", "\x00\x00\x00\x18ftypM4A ", "audio/x-m4a", one}, |
| 201 |
{"audio mp4 F4A", "\x00\x00\x00\x18ftypF4A ", "audio/mp4", one}, |
| 202 |
{"audio mp4 F4B", "\x00\x00\x00\x18ftypF4B ", "audio/mp4", none}, |
| 203 |
{"audio mp4 M4B", "\x00\x00\x00\x18ftypM4B ", "audio/mp4", none}, |
| 204 |
{"audio mp4 M4P", "\x00\x00\x00\x18ftypM4P ", "audio/mp4", none}, |
| 205 |
{"audio mp4 MSNV", "\x00\x00\x00\x18ftypMSNV", "audio/mp4", none}, |
| 206 |
{"audio mp4 NDAS", "\x00\x00\x00\x18ftypNDAS", "audio/mp4", none}, |
| 207 |
{"lnk", "\x4C\x00\x00\x00\x01\x14\x02\x00", "application/x-ms-shortcut", one}, |
| 208 |
{"mdb", offset(4, "Standard Jet DB"), "application/x-msaccess", one}, |
| 209 |
{"midi", "\x4D\x54\x68\x64", "audio/midi", one}, |
| 210 |
{"mkv", "\x1a\x45\xdf\xa3\x01\x00\x00\x00\x00\x00\x00\x23\x42\x86\x81\x01\x42\xf7\x81\x01\x42\xf2\x81\x04\x42\xf3\x81\x08\x42\x82\x88\x6d\x61\x74\x72\x6f\x73\x6b\x61", "video/x-matroska", one}, |
| 211 |
{"mobi", offset(60, "BOOKMOBI"), "application/x-mobipocket-ebook", one}, |
| 212 |
{"mov", "\x00\x00\x00\x14\x66\x74\x79\x70\x71\x74\x20\x20", "video/quicktime", one}, |
| 213 |
{"mp3", "\x49\x44\x33\x03", "audio/mpeg", all}, |
| 214 |
{"mp3 v1 notag", "\xff\xfb\xc8\x00", "audio/mpeg", none}, |
| 215 |
{"mp3 v2.5 notag", "\xff\xe3\x18\xc4", "audio/mpeg", none}, |
| 216 |
{"mp3 v2 notag", "\xff\xf3\x82\xc4", "audio/mpeg", none}, |
| 217 |
{"mp4 1", "\x00\x00\x00\x18ftyp0000", "video/mp4", all}, |
| 218 |
{"mpc", "MPCK", "audio/musepack", one}, |
| 219 |
{"mpeg", "\x00\x00\x01\xba", "video/mpeg", one}, |
| 220 |
{"mqv", "\x00\x00\x00\x18ftypmqt ", "video/quicktime", none}, |
| 221 |
{"mrc", "00057 2200037 4500245001900000\x1e", "application/marc", one}, |
| 222 |
{"msi", fromDisk("msi.msi"), "application/x-ms-installer", all}, |
| 223 |
{"msg", fromDisk("msg.msg"), "application/vnd.ms-outlook", one}, |
| 224 |
{"ndjson", `{"key":"val"}` + "\n" + `{"key":"val"}`, "application/x-ndjson", one}, |
| 225 |
{"ndjson spaces", `{ "key" : "val" }` + "\n" + ` { "key" : "val" }`, "application/x-ndjson", one}, |
| 226 |
{ |
| 227 |
"ndjson with before&after lines", |
| 228 |
` |
| 229 |
{"key":"val"} |
| 230 |
{"key":"val"} |
| 231 |
`, |
| 232 |
"application/x-ndjson", |
| 233 |
none, |
| 234 |
}, |
| 235 |
{ |
| 236 |
"empty lines should not be counted for ndjson", |
| 237 |
` |
| 238 |
{"key":"val"} |
| 239 |
`, |
| 240 |
"application/json", |
| 241 |
none, |
| 242 |
}, |
| 243 |
{ |
| 244 |
"a json spread on multiple lines is not ndjson", |
| 245 |
`{"key" |
| 246 |
:"val"}`, |
| 247 |
"application/json", |
| 248 |
none, |
| 249 |
}, |
| 250 |
{"nes", "NES\x1a", "application/vnd.nintendo.snes.rom", one}, |
| 251 |
{"elfobject", "\x7fELF\x02\x01\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x01\x00", "application/x-object", one}, |
| 252 |
{"odf", "PK\x03\x04\x14\x00\x00\x08\x00\x00\xb1Z\xa8N\x07\x8a\xa8[*\x00\x00\x00*\x00\x00\x00\x08\x00\x00\x00mimetypeapplication/vnd.oasis.opendocument.formula", "application/vnd.oasis.opendocument.formula", one}, |
| 253 |
{"sxc", "PK\x03\x04\x14\x00\x00\x08\x00\x00\xbb\x03\x5eGE\xbc\x13\x94\x1c\x00\x00\x00\x1c\x00\x00\x00\x08\x00\x00\x00mimetypeapplication/vnd.sun.xml.calc", "application/vnd.sun.xml.calc", one}, |
| 254 |
{"odg", "PK\x03\x04\x14\x00\x00\x08\x00\x00\xcbY\xa8N\x9f\x03.\xc4\x2b\x00\x00\x00\x2b\x00\x00\x00\x08\x00\x00\x00mimetypeapplication/vnd.oasis.opendocument.graphics", "application/vnd.oasis.opendocument.graphics", one}, |
| 255 |
{"odp", "PK\x03\x04\x14\x00\x00\x08\x00\x00\xbdX\xa8N3&\xac\xa8/\x00\x00\x00/\x00\x00\x00\x08\x00\x00\x00mimetypeapplication/vnd.oasis.opendocument.presentation", "application/vnd.oasis.opendocument.presentation", one}, |
| 256 |
{"ods", "PK\x03\x04\x14\x00\x00\x08\x00\x00\x14V\xa8N\x85l9\x8a.\x00\x00\x00.\x00\x00\x00\x08\x00\x00\x00mimetypeapplication/vnd.oasis.opendocument.spreadsheet", "application/vnd.oasis.opendocument.spreadsheet", one}, |
| 257 |
{"odt", "PK\x03\x04\x14\x00\x00\x08\x00\x00\xbbP\xa8N\x5e\xc62\n'\x00\x00\x00'\x00\x00\x00\x08\x00\x00\x00mimetypeapplication/vnd.oasis.opendocument.text", "application/vnd.oasis.opendocument.text", one}, |
| 258 |
{"ogg", "OggS\x00\x02\x00\x00\x00\x00\x00\x00\x00\x00\xce\xc6AI\x00\x00\x00\x00py\xf3\x3d\x01\x1e\x01vorbis\x00\x00", "audio/ogg", one}, |
| 259 |
{"ogg", "OggS\x00\x02\x00\x00\x00\x00\x00\x00\x00\x00\x80\xbc\x81_\x00\x00\x00\x00\xd0\xfbP\x84\x01@fishead\x00\x03", "video/ogg", one}, |
| 260 |
{"ogg spx oga", "OggS\x00\x02\x00\x00\x00\x00\x00\x00\x00\x00\xc7w\xaa\x15\x00\x00\x00\x00V&\x88\x89\x01PSpeex 1", "audio/ogg", one}, |
| 261 |
{"one", "\xe4\x52\x5c\x7b\x8c\xd8\xa7\x4d\xae\xb1\x53\x78\xd0\x29\x96\xd3", "application/onenote", one}, |
| 262 |
{"otf", "OTTO\x00\x0c\x00\x80\x00\x03\x00\x40\x43\x46\x46 " + offset(36, ""), "font/otf", one}, |
| 263 |
{"otg", "PK\x03\x04\x14\x00\x00\x08\x00\x00\xd1Y\xa8N\xdf%\xad\xe94\x00\x00\x004\x00\x00\x00\x08\x00\x00\x00mimetypeapplication/vnd.oasis.opendocument.graphics-template", "application/vnd.oasis.opendocument.graphics-template", one}, |
| 264 |
{"otp", "PK\x03\x04\x14\x00\x00\x08\x00\x00\xc4X\xa8N\xef\n\x14:8\x00\x00\x008\x00\x00\x00\x08\x00\x00\x00mimetypeapplication/vnd.oasis.opendocument.presentation-template", "application/vnd.oasis.opendocument.presentation-template", one}, |
| 265 |
{"ots", "PK\x03\x04\x14\x00\x00\x08\x00\x00\x1bV\xa8N{\x96\xa3N7\x00\x00\x007\x00\x00\x00\x08\x00\x00\x00mimetypeapplication/vnd.oasis.opendocument.spreadsheet-template", "application/vnd.oasis.opendocument.spreadsheet-template", one}, |
| 266 |
{"ott", "PK\x03\x04\x14\x00\x00\x08\x00\x00\xcfP\xa8N\xe4\x11\x92)0\x00\x00\x000\x00\x00\x00\x08\x00\x00\x00mimetypeapplication/vnd.oasis.opendocument.text-template", "application/vnd.oasis.opendocument.text-template", one}, |
| 267 |
{"odc", "PK\x03\x04\x14\x00\x00\x08\x00\x00zp2R\xab\xb8\xb2l(\x00\x00\x00(\x00\x00\x00\x08\x00\x00\x00mimetypeapplication/vnd.oasis.opendocument.chart", "application/vnd.oasis.opendocument.chart", one}, |
| 268 |
{"owl", `<?xml version="1.0"?><Ontology xmlns="http://www.w3.org/2002/07/owl#">`, "application/owl+xml", one}, |
| 269 |
{"pat", "\x00\x00\x00\x1c\x00\x00\x00\x01\x00\x00\x00\x01\x00\x00\x00\x01\x00\x00\x00\x03GPAT", "image/x-gimp-pat", one}, |
| 270 |
{"pdf", "%PDF-", "application/pdf", all}, |
| 271 |
{"php", "#!/usr/bin/env php", "text/x-php", one}, |
| 272 |
{"pl", "#!/usr/bin/perl", "text/x-perl", one}, |
| 273 |
{"png", "\x89PNG\x0d\x0a\x1a\x0a", "image/png", all}, |
| 274 |
{"ppt", fromDisk("ppt.ppt"), "application/vnd.ms-powerpoint", all}, |
| 275 |
{"pptx", fromDisk("pptx.pptx"), "application/vnd.openxmlformats-officedocument.presentationml.presentation", all}, |
| 276 |
{"pbm", "P1\n# comment\n\n6 10", "image/x-portable-bitmap", one}, |
| 277 |
{"pgm", "P2\n# comment\n\n6 10", "image/x-portable-graymap", one}, |
| 278 |
{"ppm", "P3\n# comment\n\n6 10", "image/x-portable-pixmap", one}, |
| 279 |
{ |
| 280 |
"pam", |
| 281 |
`P7 |
| 282 |
WIDTH 4 |
| 283 |
HEIGHT 2 |
| 284 |
DEPTH 4 |
| 285 |
MAXVAL 255 |
| 286 |
TUPLTYPE RGB_ALPHA |
| 287 |
ENDHDR`, |
| 288 |
"image/x-portable-arbitrarymap", |
| 289 |
one, |
| 290 |
}, |
| 291 |
{"ps", "%!PS-Adobe-", "application/postscript", one}, |
| 292 |
{"psd", "8BPS", "image/vnd.adobe.photoshop", all}, |
| 293 |
{"p7s_pem", "-----BEGIN PKCS7", "application/pkcs7-signature", one}, |
| 294 |
{"p7s_der", "\x30\x82\x01\x26\x06\x09\x2a\x86\x48\x86\xf7\x0d\x01\x07\x02\xa0\x82\x01\x17\x30", "application/pkcs7-signature", one}, |
| 295 |
{"pub", fromDisk("pub.pub"), "application/vnd.ms-publisher", one}, |
| 296 |
{"py", "#!/usr/bin/python", "text/x-python", one}, |
| 297 |
{"py3", "#!/usr/bin/env python3", "text/x-python", one}, |
| 298 |
{"py3 with code", "#!/usr/bin/env -S python3\nprint(1)", "text/x-python", none}, |
| 299 |
{"qcp", "RIFF\xc0\xcf\x00\x00QLCMf", "audio/qcelp", one}, |
| 300 |
{"rar", "Rar!\x1a\x07\x01\x00", "application/x-rar-compressed", all}, |
| 301 |
{"rfc822", "Cc: [email protected]\nTo: [email protected]", "message/rfc822", one}, |
| 302 |
{"rfc822 case insensitive", "Cc: [email protected]\nDeLiVeReD-To: [email protected]", "message/rfc822", none}, |
| 303 |
{"rb", "#!/usr/local/bin/ruby", "text/x-ruby", one}, |
| 304 |
{"rmvb", ".RMF", "application/vnd.rn-realmedia-vbr", one}, |
| 305 |
{"rpm", "\xed\xab\xee\xdb", "application/x-rpm", one}, |
| 306 |
{"rss", "\x3c\x3f\x78\x6d\x6c\x20\x76\x65\x72\x73\x69\x6f\x6e\x3d\x22\x31\x2e\x30\x22\x20\x65\x6e\x63\x6f\x64\x69\x6e\x67\x3d\x22\x55\x54\x46\x2d\x38\x22\x3f\x3e\x0a\x3c\x72\x73\x73", "application/rss+xml", one}, |
| 307 |
{"rtf", "{\\rtf", "text/rtf", one}, |
| 308 |
{"sh", "#!/bin/sh", "text/x-shellscript", one}, |
| 309 |
{"shp", fromDisk("shp.shp"), "application/vnd.shp", one}, |
| 310 |
{"shx", "\x00\x00\x27\x0a", "application/vnd.shx", one}, |
| 311 |
{"so", "\x7fELF\x02\x01\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x03\x00", "application/x-sharedlib", all}, |
| 312 |
{"sqlite", "SQLite format 3\x00", "application/vnd.sqlite3", one}, |
| 313 |
{"srt", "1\n00:02:16,612 --\x3e 00:02:19,376\nS", "application/x-subrip", one}, |
| 314 |
{"svg no xml header", `<svg xmlns="http://www.w3.org/2000/svg"`, "image/svg+xml", all}, |
| 315 |
{ |
| 316 |
"svg xml header", |
| 317 |
` |
| 318 |
<?xml version="1.0" encoding="UTF-8" standalone="no"?> |
| 319 |
<svg width="391" height="391" viewBox="-70.5 -70.5 391 391" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink"> |
| 320 |
<rect fill="#fff" stroke="#000" x="-70" y="-70" width="390" height="390"/> |
| 321 |
</svg> |
| 322 |
`, |
| 323 |
"image/svg+xml", |
| 324 |
all, |
| 325 |
}, |
| 326 |
{ |
| 327 |
"svg with comment prefix", |
| 328 |
`<!-- this comment should not affect --><svg xmlns="http://www.w3.org/2000/svg"`, |
| 329 |
"image/svg+xml", |
| 330 |
none, |
| 331 |
}, |
| 332 |
|
| 333 |
{"swf", "CWS", "application/x-shockwave-flash", one}, |
| 334 |
{"tar", fromDisk("tar.tar"), "application/x-tar", all}, |
| 335 |
{"tcl", "#!/usr/bin/tcl", "text/x-tcl", one}, |
| 336 |
{"tcx", `<?xml version="1.0"?><TrainingCenterDatabase xmlns="http://www.garmin.com/xmlschemas/TrainingCenterDatabase/v2">`, "application/vnd.garmin.tcx+xml", one}, |
| 337 |
{"tiff", "II*\x00", "image/tiff", one}, |
| 338 |
{"tsv", "a\t\"b\"\tc\n1\t2\t3", "text/tab-separated-values", all}, |
| 339 |
{"ttc", "ttcf\x00\x01\x00\x00", "font/collection", one}, |
| 340 |
{"ttf", "\x00\x01\x00\x00\x00\x0f\x00\x80\x00\x03\x00\x70\x4f\x53\x2f\x32", "font/ttf", one}, |
| 341 |
{"tzfile", fromDisk("tzfile"), "application/tzif", one}, |
| 342 |
{"utf16bebom txt", "\xfe\xff\x00\x74\x00\x68\x00\x69\x00\x73", "text/plain; charset=utf-16be", none}, |
| 343 |
{"utf16lebom txt", "\xff\xfe\x74\x00\x68\x00\x69\x00\x73\x00", "text/plain; charset=utf-16le", none}, |
| •344 |
{"utf32bebom txt", "\x00\x00\xfe\xff\x00\x00\x00\x74\x00\x00\x00\x68\x00\x00\x00\x69\x00\x00\x00\x73", "text/plain; charset=utf-32be", none}, |
| 345 |
{"utf32lebom txt", "\xff\xfe\x00\x00\x74\x00\x00\x00\x68\x00\x00\x00\x69\x00\x00\x00\x73\x00\x00\x00", "text/plain; charset=utf-32le", none}, |
| 346 |
{"utf8 txt", fromDisk("utf8.txt"), "text/plain; charset=utf-8", all}, |
| 347 |
{"utf8ctrlchars", "\xef\xbf\xbd\xef\xbf\xbd\xef\xbf\xbd\xef\xbf\xbd\xef\xbf\xbd\x10", "application/octet-stream", none}, |
| 348 |
{"vcf", "BEGIN:VCARD\nV", "text/vcard", one}, |
| 349 |
{"vcf dos", "BEGIN:VCARD\r\nV", "text/vcard", none}, |
| 350 |
{"visio", "\x50\x4b\x03\x04\x14\x00\x00\x00\x00\x00\x83\x93\x11\x5b\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x06\x00\x20\x00visio/", "application/vnd.ms-visio.drawing.main+xml", one}, |
| 351 |
{"voc", "Creative Voice File", "audio/x-unknown", one}, |
| 352 |
{"vtt", "WEBVTT", "text/vtt", one}, |
| 353 |
{"warc", "WARC/1.1", "application/warc", one}, |
| 354 |
{"wasm", "\x00asm", "application/wasm", one}, |
| 355 |
{"wav", "RIFF\xba\xa5\x04\x00WAVEf", "audio/wav", all}, |
| 356 |
{"webm", "\x1aE\xdf\xa3\x01\x00\x00\x00\x00\x00\x00\x1fB\x86\x81\x01B\xf7\x81\x01B\xf2\x81\x04B\xf3\x81\x08B\x82\x84webm", "video/webm", all}, |
| 357 |
{"webp", "RIFFhv\x00\x00WEBPV", "image/webp", all}, |
| 358 |
{"woff", "wOFF", "font/woff", one}, |
| 359 |
{"woff2", "wOF2", "font/woff2", one}, |
| 360 |
{"wpd", "\xffWPC____\x01\x0a", "application/vnd.wordperfect", one}, |
| 361 |
{"x3d", `<?xml version="1.0"?><X3D xmlns:xsd="http://www.w3.org/2001/XMLSchema-instance">`, "model/x3d+xml", one}, |
| •362 |
{"xar", "xar!", "application/x-xar", one}, |
| 363 |
{"xcf", "gimp xcf", "image/x-xcf", one}, |
| 364 |
{"xfdf", `<?xml version="1.0"?><xfdf xmlns="http://ns.adobe.com/xfdf/">`, "application/vnd.adobe.xfdf", one}, |
| 365 |
{"xhtml1", `<?xml version="1.0"?><!DOCTYPE html`, "application/xhtml+xml", one}, |
| 366 |
{"xhtml2", `<?xml version="1.0"?><HtMl XMLNS=`, "application/xhtml+xml", none}, |
| 367 |
{"xlf", `<?xml version="1.0"?><xliff xmlns="urn:oasis:names:tc:xliff:document:1.2">`, "application/x-xliff+xml", one}, |
| 368 |
{"xls", fromDisk("xls.xls"), "application/vnd.ms-excel", one}, |
| 369 |
{"xlsx", fromDisk("xlsx.xlsx"), "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", all}, |
| 370 |
{"xml", "<?xml ", "text/xml; charset=utf-8", all}, |
| 371 |
{"xml withbr", "\x0D\x0A<?xml ", "text/xml; charset=utf-8", none}, |
| 372 |
{"xz", "\xfd7zXZ\x00", "application/x-xz", one}, |
| 373 |
{"zip", "PK\x03\x04", "application/zip", all}, |
| 374 |
|
| 375 |
{"zlib", "\x78\x5e\xf3\xc8\xe4\x02\x00\x01\xb7\x00\xbc", "application/zlib", one}, |
| 376 |
{"zst", "(\xb5/\xfd", "application/zstd", all}, |
| 377 |
{"zst skippable frame", "\x50\x2A\x4D\x18", "application/zstd", none}, |
| 378 |
} |
| 379 |
|
| 380 |
func TestDetect(t *testing.T) { |
| 381 |
for _, tc := range testcases { |
| 382 |
if tc.data == "debian-packaging-removed" { |
| 383 |
continue |
| 384 |
} |
| 385 |
t.Run(tc.name, func(t *testing.T) { |
| 386 |
if mtype := Detect([]byte(tc.data)); mtype.String() != tc.expectedMIME { |
| 387 |
t.Errorf("Detect: Expected: %s != Detected: %s", tc.expectedMIME, mtype.String()) |
| 388 |
} |
| 389 |
if mtype, err := DetectReader(strings.NewReader(tc.data)); err != nil { |
| 390 |
t.Errorf("DetectReader: unexpected error: %s", err) |
| 391 |
} else if mtype.String() != tc.expectedMIME { |
| 392 |
t.Errorf("DetectReader: Expected: %s != Detected: %s", tc.expectedMIME, mtype.String()) |
| 393 |
} |
| 394 |
}) |
| 395 |
} |
| 396 |
} |
| 397 |
|
| 398 |
func TestDetectBreakReader(t *testing.T) { |
| 399 |
for _, tc := range testcases { |
| 400 |
if tc.data == "debian-packaging-removed" { |
| 401 |
continue |
| 402 |
} |
| 403 |
t.Run(tc.name, func(t *testing.T) { |
| 404 |
br := &breakReader{ |
| 405 |
r: strings.NewReader(tc.data), |
| 406 |
breakSize: 3, |
| 407 |
} |
| 408 |
if mtype, err := DetectReader(br); err != nil { |
| 409 |
t.Errorf("Unexpected error: %s", err) |
| 410 |
} else if mtype.String() != tc.expectedMIME { |
| 411 |
t.Errorf("Expected: %s != Detected: %s", tc.expectedMIME, mtype.String()) |
| 412 |
} |
| 413 |
}) |
| 414 |
} |
| 415 |
} |
| 416 |
|
| 417 |
|
| 418 |
func TestGenerateSupportedFormats(t *testing.T) { |
| 419 |
if testing.Short() { |
| 420 |
t.Skip() |
| 421 |
} |
| 422 |
f, err := os.Create("supported_mimes.md") |
| 423 |
if err != nil { |
| 424 |
t.Fatal(err) |
| 425 |
} |
| 426 |
defer f.Close() |
| 427 |
|
| 428 |
nodes := root.flatten() |
| 429 |
header := fmt.Sprintf(`## %d Supported MIME types |
| 430 |
This file is automatically generated when running tests. Do not edit manually. |
| 431 |
|
| 432 |
Extension | MIME type <br> Aliases | Hierarchy |
| 433 |
--------- | ---------------------- | --------- |
| 434 |
`, len(nodes)) |
| 435 |
|
| 436 |
if _, err := f.WriteString(header); err != nil { |
| 437 |
t.Fatal(err) |
| 438 |
} |
| 439 |
for _, n := range nodes { |
| 440 |
ext := n.extension |
| 441 |
if ext == "" { |
| 442 |
ext = "n/a" |
| 443 |
} |
| 444 |
|
| 445 |
mimeColum := fmt.Sprintf("**%s**", n.mime) |
| 446 |
if len(n.aliases) > 0 { |
| 447 |
mimeColum = fmt.Sprintf("**%s** <br> %s", n.mime, strings.Join(n.aliases, ", ")) |
| 448 |
} |
| 449 |
str := fmt.Sprintf("**%s** | %s | %s\n", ext, mimeColum, n.hierarchy()) |
| 450 |
if _, err := f.WriteString(str); err != nil { |
| 451 |
t.Fatal(err) |
| 452 |
} |
| 453 |
} |
| 454 |
} |
| 455 |
|
| 456 |
func TestEqualsAny(t *testing.T) { |
| 457 |
type ss []string |
| 458 |
testCases := []struct { |
| 459 |
m1 string |
| 460 |
m2 ss |
| 461 |
res bool |
| 462 |
}{ |
| 463 |
{"foo/bar", ss{"foo/bar"}, true}, |
| 464 |
{" foo/bar", ss{"foo/bar "}, true}, |
| 465 |
{" foo/bar", ss{"foo/BAR "}, true}, |
| 466 |
{" foo/bar", ss{"foo/baz"}, false}, |
| 467 |
{";charset=utf-8", ss{""}, true}, |
| 468 |
{"", ss{"", "foo/bar"}, true}, |
| 469 |
{"foo/bar", ss{""}, false}, |
| 470 |
{"foo/bar", nil, false}, |
| 471 |
} |
| 472 |
for _, tc := range testCases { |
| 473 |
if EqualsAny(tc.m1, tc.m2...) != tc.res { |
| 474 |
t.Errorf("Equality test failed for %+v", tc) |
| 475 |
} |
| 476 |
} |
| 477 |
} |
| 478 |
|
| 479 |
|
| 480 |
|
| 481 |
|
| 482 |
|
| 483 |
|
| 484 |
|
| 485 |
|
| 486 |
type breakReader struct { |
| 487 |
r io.Reader |
| 488 |
breakSize int |
| 489 |
} |
| 490 |
|
| 491 |
func (b *breakReader) Read(p []byte) (int, error) { |
| 492 |
if len(p) > b.breakSize { |
| 493 |
p = p[:b.breakSize] |
| 494 |
} |
| 495 |
n, err := io.ReadFull(b.r, p) |
| 496 |
if err == io.ErrUnexpectedEOF { |
| 497 |
return n, io.EOF |
| 498 |
} |
| 499 |
return n, err |
| 500 |
} |
| 501 |
|
| 502 |
func TestFaultyInput(t *testing.T) { |
| 503 |
inexistent := "inexistent.file" |
| 504 |
if mtype, err := DetectFile(inexistent); err == nil { |
| 505 |
t.Errorf("%s should not match successfully", inexistent) |
| 506 |
} else if mtype.String() != "application/octet-stream" { |
| 507 |
t.Errorf("inexistent.file expected application/octet-stream, got %s", mtype) |
| 508 |
} |
| 509 |
|
| 510 |
f, _ := os.Open(inexistent) |
| 511 |
if mtype, err := DetectReader(f); err == nil { |
| 512 |
t.Errorf("%s reader should not match successfully", inexistent) |
| 513 |
} else if mtype.String() != "application/octet-stream" { |
| 514 |
t.Errorf("inexistent.file reader expected application/octet-stream, got %s", mtype) |
| 515 |
} |
| 516 |
} |
| 517 |
|
| 518 |
func TestHierarchy(t *testing.T) { |
| 519 |
detectedMIME := Detect([]byte("<html></html>")) |
| 520 |
expected := []string{ |
| 521 |
"text/html; charset=utf-8", |
| 522 |
"text/plain", |
| 523 |
"application/octet-stream", |
| 524 |
} |
| 525 |
|
| 526 |
got := []string{} |
| 527 |
for mtype := detectedMIME; mtype != nil; mtype = mtype.Parent() { |
| 528 |
got = append(got, mtype.String()) |
| 529 |
} |
| 530 |
if le, lg := len(expected), len(got); le != lg { |
| 531 |
t.Fatalf("hierarchy len error; expected: %d, got: %d", le, lg) |
| 532 |
} |
| 533 |
|
| 534 |
for i := range expected { |
| 535 |
if expected[i] != got[i] { |
| 536 |
t.Fatalf("hierarchy error; expected: %s, got: %s", expected, got) |
| 537 |
} |
| 538 |
} |
| 539 |
} |
| 540 |
|
| 541 |
func TestConcurrent(t *testing.T) { |
| 542 |
if testing.Short() { |
| 543 |
t.Skip() |
| 544 |
} |
| 545 |
wg := sync.WaitGroup{} |
| 546 |
wg.Add(4) |
| 547 |
|
| 548 |
n := uint32(1000) |
| 549 |
Extend(func([]byte, uint32) bool { return false }, "e", ".e") |
| 550 |
go func() { |
| 551 |
for i := uint32(0); i < n; i++ { |
| 552 |
Detect([]byte("text content")) |
| 553 |
} |
| 554 |
wg.Done() |
| 555 |
}() |
| 556 |
go func() { |
| 557 |
for i := uint32(0); i < n; i++ { |
| 558 |
SetLimit(5000 + i) |
| 559 |
} |
| 560 |
wg.Done() |
| 561 |
}() |
| 562 |
go func() { |
| 563 |
for i := uint32(0); i < n; i++ { |
| 564 |
Lookup("text/plain") |
| 565 |
} |
| 566 |
wg.Done() |
| 567 |
}() |
| 568 |
go func() { |
| 569 |
for i := uint32(0); i < n; i++ { |
| 570 |
Lookup("e").Extend(func([]byte, uint32) bool { return false }, "e", ".e") |
| 571 |
} |
| 572 |
wg.Done() |
| 573 |
}() |
| 574 |
|
| 575 |
wg.Wait() |
| 576 |
|
| 577 |
SetLimit(defaultLimit) |
| 578 |
root.children = root.children[1:] |
| 579 |
} |
| 580 |
|
| 581 |
|
| 582 |
func TestEmptyInput(t *testing.T) { |
| 583 |
mtype, err := DetectReader(bytes.NewReader(nil)) |
| 584 |
if err != nil { |
| 585 |
t.Fatalf("empty reader err; expected: nil, got: %s", err) |
| 586 |
} |
| 587 |
plain := "text/plain" |
| 588 |
if !mtype.Is(plain) { |
| 589 |
t.Fatalf("empty reader detection; expected: %s, got: %s", plain, mtype) |
| 590 |
} |
| 591 |
mtype = Detect(nil) |
| 592 |
if !mtype.Is(plain) { |
| 593 |
t.Fatalf("empty bytes slice detection; expected: %s, got: %s", plain, mtype) |
| 594 |
} |
| 595 |
SetLimit(0) |
| 596 |
mtype, err = DetectReader(bytes.NewReader(nil)) |
| 597 |
if err != nil { |
| 598 |
t.Fatalf("0 limіt, empty reader err; expected: nil, got: %s", err) |
| 599 |
} |
| 600 |
if !mtype.Is(plain) { |
| 601 |
t.Fatalf("0 limit, empty reader detection; expected: %s, got: %s", plain, mtype) |
| 602 |
} |
| 603 |
SetLimit(defaultLimit) |
| 604 |
} |
| 605 |
|
| 606 |
func BenchmarkOne(b *testing.B) { |
| 607 |
r := rand.New(rand.NewSource(0)) |
| 608 |
|
| 609 |
randData := make([]byte, defaultLimit) |
| 610 |
if _, err := io.ReadFull(r, randData); err != io.ErrUnexpectedEOF && err != nil { |
| 611 |
b.Fatal(err) |
| 612 |
} |
| 613 |
|
| 614 |
for _, tc := range testcases { |
| 615 |
if shouldRun := tc.bench == one || tc.bench == all; !shouldRun { |
| 616 |
continue |
| 617 |
} |
| 618 |
data := []byte(tc.data) |
| 619 |
parsed, _, _ := mime.ParseMediaType(tc.expectedMIME) |
| 620 |
mtype := Lookup(parsed) |
| 621 |
if mtype == nil || mtype.detector == nil { |
| 622 |
b.Fatalf("mime should always be non-nil %s %s", mtype, tc.expectedMIME) |
| 623 |
} |
| 624 |
|
| 625 |
b.Run(tc.name, func(b *testing.B) { |
| 626 |
b.ReportAllocs() |
| 627 |
for b.Loop() { |
| 628 |
if !mtype.detector(data, defaultLimit) { |
| 629 |
b.Fatalf("positive detection should never fail") |
| 630 |
} |
| 631 |
if mtype.detector(randData, defaultLimit) { |
| 632 |
b.Fatalf("negative detection should always fail") |
| 633 |
} |
| 634 |
} |
| 635 |
}) |
| 636 |
} |
| 637 |
} |
| 638 |
|
| 639 |
func BenchmarkAll(b *testing.B) { |
| 640 |
r := rand.New(rand.NewSource(0)) |
| 641 |
|
| 642 |
randData := make([]byte, defaultLimit) |
| 643 |
if _, err := io.ReadFull(r, randData); err != io.ErrUnexpectedEOF && err != nil { |
| 644 |
b.Fatal(err) |
| 645 |
} |
| 646 |
for _, tc := range testcases { |
| 647 |
if tc.bench != all { |
| 648 |
continue |
| 649 |
} |
| 650 |
data := []byte(tc.data) |
| 651 |
b.Run(tc.name, func(b *testing.B) { |
| 652 |
b.ReportAllocs() |
| 653 |
for b.Loop() { |
| 654 |
|
| 655 |
Detect(data) |
| 656 |
Detect(randData) |
| 657 |
} |
| 658 |
}) |
| 659 |
} |
| 660 |
} |
| 661 |
|
| 662 |
func BenchmarkAllTogether(b *testing.B) { |
| 663 |
r := rand.New(rand.NewSource(0)) |
| 664 |
|
| 665 |
randData := make([]byte, defaultLimit) |
| 666 |
if _, err := io.ReadFull(r, randData); err != io.ErrUnexpectedEOF && err != nil { |
| 667 |
b.Fatal(err) |
| 668 |
} |
| 669 |
datas := make([][]byte, 0, len(testcases)) |
| 670 |
for _, tc := range testcases { |
| 671 |
datas = append(datas, []byte(tc.data)) |
| 672 |
} |
| 673 |
b.ResetTimer() |
| 674 |
b.ReportAllocs() |
| 675 |
for b.Loop() { |
| 676 |
for _, d := range datas { |
| 677 |
Detect(d) |
| 678 |
Detect(randData) |
| 679 |
} |
| 680 |
} |
| 681 |
} |
| 682 |
|
| 683 |
|
| 684 |
func TestIndexOutOfRangePanic(t *testing.T) { |
| 685 |
if testing.Short() { |
| 686 |
t.Skip() |
| 687 |
} |
| 688 |
nodes := root.flatten() |
| 689 |
testAtEachIndex := func(t *testing.T, in []byte) { |
| 690 |
t.Helper() |
| 691 |
for _, n := range nodes { |
| 692 |
for i := 0; i < len(in); i++ { |
| 693 |
n.detector(in[:i], 1<<10) |
| 694 |
} |
| 695 |
} |
| 696 |
} |
| 697 |
|
| 698 |
for _, tc := range testcases { |
| 699 |
t.Run(tc.name, func(t *testing.T) { |
| 700 |
testAtEachIndex(t, []byte(tc.data)) |
| 701 |
}) |
| 702 |
} |
| 703 |
} |
| 704 |
|
| 705 |
|
| 706 |
|
| 707 |
|
| 708 |
func TestMIMEFormat(t *testing.T) { |
| 709 |
for _, n := range root.flatten() { |
| 710 |
|
| 711 |
|
| 712 |
if n.Extension() != "" && !strings.HasPrefix(n.Extension(), ".") { |
| 713 |
t.Fatalf("extension %s should be dot prefixed", n.Extension()) |
| 714 |
} |
| 715 |
|
| 716 |
_, _, err := mime.ParseMediaType(n.String()) |
| 717 |
if err != nil { |
| 718 |
t.Fatalf("error parsing node MIME: %s", err) |
| 719 |
} |
| 720 |
|
| 721 |
for _, a := range n.aliases { |
| 722 |
parsed, params, err := mime.ParseMediaType(a) |
| 723 |
if err != nil { |
| 724 |
t.Fatalf("error parsing node alias MIME: %s", err) |
| 725 |
} |
| 726 |
if parsed != a || len(params) > 0 { |
| 727 |
t.Fatalf("node alias MIME should have no optional params; alias: %s, params: %v", a, params) |
| 728 |
} |
| 729 |
} |
| 730 |
} |
| 731 |
} |
| 732 |
|
| 733 |
func TestLookup(t *testing.T) { |
| 734 |
data := []struct { |
| 735 |
mime string |
| 736 |
m *MIME |
| 737 |
}{ |
| 738 |
{root.mime, root}, |
| 739 |
{zip.mime, zip}, |
| 740 |
{zip.aliases[0], zip}, |
| 741 |
{xlsx.mime, xlsx}, |
| 742 |
} |
| 743 |
|
| 744 |
for _, tt := range data { |
| 745 |
t.Run(fmt.Sprintf("lookup %s", tt.mime), func(t *testing.T) { |
| 746 |
if m := Lookup(tt.mime); m != tt.m { |
| 747 |
t.Fatalf("failed to lookup: %s", tt.mime) |
| 748 |
} |
| 749 |
}) |
| 750 |
} |
| 751 |
} |
| 752 |
|
| 753 |
func TestExtend(t *testing.T) { |
| 754 |
data := []struct { |
| 755 |
mime string |
| 756 |
ext string |
| 757 |
parent *MIME |
| 758 |
}{ |
| 759 |
{"foo", ".foo", nil}, |
| 760 |
{"bar", ".bar", root}, |
| 761 |
{"baz", ".baz", zip}, |
| 762 |
{" UperCaseWithWhiteSpace/a+B; key=val ", ".upercase", nil}, |
| 763 |
} |
| 764 |
|
| 765 |
for _, tt := range data { |
| 766 |
t.Run(fmt.Sprintf("extending to %s", tt.mime), func(t *testing.T) { |
| 767 |
extend := Extend |
| 768 |
if tt.parent != nil { |
| 769 |
extend = tt.parent.Extend |
| 770 |
} else { |
| 771 |
tt.parent = root |
| 772 |
} |
| 773 |
|
| 774 |
extend(func(raw []byte, limit uint32) bool { return false }, tt.mime, tt.ext) |
| 775 |
m := Lookup(tt.mime) |
| 776 |
if m == nil { |
| 777 |
t.Fatalf("mime %s not found", tt.mime) |
| 778 |
} |
| 779 |
if m.parent != tt.parent { |
| 780 |
t.Fatalf("mime %s has wrong parent: want %s, got %s", tt.mime, tt.parent.mime, m.parent.mime) |
| 781 |
} |
| 782 |
|
| 783 |
tt.parent.children = tt.parent.children[1:] |
| 784 |
}) |
| 785 |
} |
| 786 |
} |
| 787 |
|
| 788 |
|
| 789 |
|
| 790 |
|
| 791 |
func FuzzMimetype(f *testing.F) { |
| 792 |
for _, tc := range testcases { |
| 793 |
if len(tc.data) < 100 && tc.bench == one { |
| 794 |
f.Add([]byte(tc.data)) |
| 795 |
} |
| 796 |
} |
| 797 |
|
| 798 |
detectors := root.flatten()[1:] |
| 799 |
f.Fuzz(func(t *testing.T, data []byte) { |
| 800 |
matched := false |
| 801 |
for _, d := range detectors { |
| 802 |
if d.detector(data, math.MaxUint32) { |
| •803 |
matched = true |
| 804 |
} |
| 805 |
} |
| 806 |
if !matched { |
| •807 |
t.Skip() |
| 808 |
} |
| 809 |
}) |
| 810 |
} |
| 811 |
|
| 812 |
|
| 813 |
func TestInputIsNotMutated(t *testing.T) { |
| 814 |
for _, tc := range testcases { |
| 815 |
t.Run(tc.name, func(t *testing.T) { |
| 816 |
passedBytes := []byte(tc.data) |
| 817 |
Detect(passedBytes) |
| 818 |
|
| 819 |
if pbs := string(passedBytes); pbs != tc.data { |
| 820 |
t.Errorf("input should not be mutated; before: %s, after: %s", tc.data, pbs) |
| 821 |
} |
| 822 |
}) |
| 823 |
} |
| 824 |
} |
| 825 |
|
| 826 |
|
| 827 |
func TestNDJSONCutOnSecondLine(t *testing.T) { |
| 828 |
data := []byte(`{"key": "this json line has 43 characters"} |
| 829 |
{"key": "this json line has 43 characters"}`) |
| 830 |
SetLimit(uint32(len(data))) |
| 831 |
mtype := Detect(data) |
| 832 |
if !mtype.Is("application/x-ndjson") { |
| 833 |
t.Errorf("expected nd-json two lines of JSON; got: %s", mtype) |
| 834 |
} |
| 835 |
|
| 836 |
SetLimit(43 + 43/2) |
| 837 |
mtype = Detect(data) |
| 838 |
if !mtype.Is("application/x-ndjson") { |
| 839 |
t.Errorf("expected nd-json for one and a half lines of JSON; got: %s", mtype) |
| 840 |
} |
| 841 |
SetLimit(defaultLimit) |
| 842 |
} |
| 843 |
|
| 844 |
const ( |
| 845 |
|
| 846 |
none = "none" |
| 847 |
|
| 848 |
|
| 849 |
one = "one" |
| 850 |
|
| 851 |
|
| 852 |
all = "all" |
| 853 |
) |
| 854 |
|
| 855 |
|
| 856 |
func offset(n int, s string) string { |
| 857 |
prepend := make([]byte, n) |
| 858 |
return string(prepend) + s |
| 859 |
} |
| 860 |
|
| 861 |
func fromDisk(path string) string { |
| 862 |
data, err := os.ReadFile("testdata/" + path) |
| 863 |
if err != nil { |
| 864 |
if strings.HasSuffix(err.Error(), "open testdata/"+path+": no such file or directory") { |
| 865 |
fmt.Println(err.Error()) |
| 866 |
fmt.Println("(removed from Debian packaging)") |
| 867 |
return "debian-packaging-removed" |
| 868 |
} |
| 869 |
panic(err) |
| 870 |
} |
| 871 |
return string(data) |
| 872 |
} |
| 873 |
|
| 874 |
|
| 875 |
var ( |
| 876 |
_ func([]byte) *MIME = Detect |
| 877 |
_ func(io.Reader) (*MIME, error) = DetectReader |
| 878 |
_ func(string) (*MIME, error) = DetectFile |
| 879 |
_ func(string, ...string) bool = EqualsAny |
| 880 |
_ func(uint32) = SetLimit |
| 881 |
_ func(string) *MIME = Lookup |
| 882 |
m = &MIME{} |
| 883 |
_ func() string = m.String |
| 884 |
_ func() string = m.Extension |
| 885 |
_ func() *MIME = m.Parent |
| 886 |
_ func(string) bool = m.Is |
| 887 |
|
| 888 |
_ func(func([]byte, uint32) bool, string, string, ...string) = Extend |
| 889 |
_ func(func([]byte, uint32) bool, string, string, ...string) = m.Extend |
| 890 |
) |