| 1 |
from xdg import Mime |
| 2 |
import unittest |
| 3 |
import os.path |
| 4 |
import tempfile, shutil |
| 5 |
|
| 6 |
import resources |
| 7 |
|
| 8 |
example_dir = os.path.join(os.path.dirname(__file__), 'example') |
| 9 |
def example_file(filename): |
| 10 |
return os.path.join(example_dir, filename) |
| 11 |
|
| 12 |
class MimeTestBase(unittest.TestCase): |
| 13 |
def check_mimetype(self, mimetype, media, subtype): |
| 14 |
self.assertEqual(mimetype.media, media) |
| 15 |
self.assertEqual(mimetype.subtype, subtype) |
| 16 |
|
| 17 |
class MimeTest(MimeTestBase): |
| 18 |
def test_create_mimetype(self): |
| 19 |
mt1 = Mime.MIMEtype('application', 'pdf') |
| 20 |
mt2 = Mime.MIMEtype('application', 'pdf') |
| 21 |
self.assertEqual(id(mt1), id(mt2)) # Check caching |
| 22 |
|
| 23 |
amr = Mime.MIMEtype('audio', 'AMR') |
| 24 |
self.check_mimetype(amr, 'audio', 'amr') # Check lowercase |
| 25 |
|
| 26 |
ogg = Mime.MIMEtype('audio/ogg') |
| 27 |
self.check_mimetype(ogg, 'audio', 'ogg') # Check split on / |
| 28 |
|
| 29 |
self.assertRaises(Exception, Mime.MIMEtype, 'audio/foo/bar') |
| 30 |
|
| 31 |
def test_get_type_by_name(self): |
| 32 |
appzip = Mime.get_type_by_name("foo.zip") |
| 33 |
self.check_mimetype(appzip, 'application', 'zip') |
| 34 |
|
| 35 |
def test_get_type_by_data(self): |
| 36 |
imgpng = Mime.get_type_by_data(resources.png_data) |
| 37 |
self.check_mimetype(imgpng, 'image', 'png') |
| 38 |
|
| 39 |
def test_mimetype_repr(self): |
| 40 |
mt = Mime.lookup('application', 'zip') |
| 41 |
repr(mt) # Just check that this doesn't throw an error. |
| 42 |
|
| 43 |
def test_get_type_by_contents(self): |
| 44 |
tmpdir = tempfile.mkdtemp() |
| 45 |
try: |
| 46 |
test_file = os.path.join(tmpdir, "test") |
| 47 |
with open(test_file, "wb") as f: |
| 48 |
f.write(resources.png_data) |
| 49 |
|
| 50 |
imgpng = Mime.get_type_by_contents(test_file) |
| 51 |
self.check_mimetype(imgpng, 'image', 'png') |
| 52 |
|
| 53 |
finally: |
| 54 |
shutil.rmtree(tmpdir) |
| 55 |
|
| 56 |
def test_get_type(self): |
| 57 |
# File that doesn't exist - get type by name |
| 58 |
imgpng = Mime.get_type(example_file("test.gif")) |
| 59 |
self.check_mimetype(imgpng, 'image', 'gif') |
| 60 |
|
| 61 |
# File that does exist - get type by contents |
| 62 |
imgpng = Mime.get_type(example_file("png_file")) |
| 63 |
self.check_mimetype(imgpng, 'image', 'png') |
| 64 |
|
| 65 |
# Directory - special case |
| 66 |
inodedir = Mime.get_type(example_file("subdir")) |
| 67 |
self.check_mimetype(inodedir, 'inode', 'directory') |
| 68 |
|
| 69 |
# Mystery files |
| 70 |
mystery_text = Mime.get_type(example_file('mystery_text')) |
| 71 |
self.check_mimetype(mystery_text, 'text', 'plain') |
| 72 |
mystery_exe = Mime.get_type(example_file('mystery_exe')) |
| 73 |
self.check_mimetype(mystery_exe, 'application', 'executable') |
| 74 |
|
| 75 |
# Symlink |
| •76 |
print("test-mime.py::test_get_type: skip symlink tests") |
| 77 |
#self.check_mimetype(Mime.get_type(example_file("png_symlink")), |
| 78 |
# 'image', 'png') |
| 79 |
#self.check_mimetype(Mime.get_type(example_file("png_symlink"), follow=False), |
| 80 |
# 'inode', 'symlink') |
| 81 |
|
| 82 |
def test_get_type2(self): |
| 83 |
# File that doesn't exist - use the name |
| 84 |
self.check_mimetype(Mime.get_type2(example_file('test.gif')), 'image', 'gif') |
| 85 |
|
| 86 |
# File that does exist - use the contents |
| 87 |
self.check_mimetype(Mime.get_type2(example_file('png_file')), 'image', 'png') |
| 88 |
|
| 89 |
# Does exist - use name before contents |
| 90 |
self.check_mimetype(Mime.get_type2(example_file('file.png')), 'image', 'png') |
| 91 |
self.check_mimetype(Mime.get_type2(example_file('word.doc')), 'application', 'msword') |
| 92 |
|
| 93 |
# Ambiguous file extension |
| 94 |
glade_mime = Mime.get_type2(example_file('glade.ui')) |
| 95 |
self.assertEqual(glade_mime.media, 'application') |
| 96 |
# Grumble, this is still ambiguous on some systems |
| 97 |
self.assertIn(glade_mime.subtype, {'x-gtk-builder', 'x-glade'}) |
| 98 |
self.check_mimetype(Mime.get_type2(example_file('qtdesigner.ui')), 'application', 'x-designer') |
| 99 |
|
| 100 |
# text/x-python has greater weight than text/x-readme |
| 101 |
self.check_mimetype(Mime.get_type2(example_file('README.py')), 'text', 'x-python') |
| 102 |
|
| 103 |
# Directory - special filesystem object |
| 104 |
self.check_mimetype(Mime.get_type2(example_file('subdir')), 'inode', 'directory') |
| 105 |
|
| 106 |
# Mystery files: |
| 107 |
mystery_missing = Mime.get_type2(example_file('mystery_missing')) |
| 108 |
self.check_mimetype(mystery_missing, 'application', 'octet-stream') |
| 109 |
mystery_binary = Mime.get_type2(example_file('mystery_binary')) |
| 110 |
self.check_mimetype(mystery_binary, 'application', 'octet-stream') |
| 111 |
mystery_text = Mime.get_type2(example_file('mystery_text')) |
| 112 |
self.check_mimetype(mystery_text, 'text', 'plain') |
| 113 |
mystery_exe = Mime.get_type2(example_file('mystery_exe')) |
| 114 |
self.check_mimetype(mystery_exe, 'application', 'executable') |
| 115 |
|
| 116 |
# Symlink |
| •117 |
print("test-mime.py::test_get_type2: skip symlink tests") |
| 118 |
#self.check_mimetype(Mime.get_type2(example_file("png_symlink")), |
| 119 |
# 'image', 'png') |
| 120 |
#self.check_mimetype(Mime.get_type2(example_file("png_symlink"), follow=False), |
| 121 |
# 'inode', 'symlink') |
| 122 |
|
| 123 |
def test_lookup(self): |
| 124 |
pdf1 = Mime.lookup("application/pdf") |
| 125 |
pdf2 = Mime.lookup("application", "pdf") |
| 126 |
self.assertEqual(pdf1, pdf2) |
| 127 |
self.check_mimetype(pdf1, 'application', 'pdf') |
| 128 |
|
| 129 |
def test_get_comment(self): |
| 130 |
# Check these don't throw an error. One that is likely to exist: |
| 131 |
Mime.MIMEtype("application", "pdf").get_comment() |
| 132 |
# And one that's unlikely to exist: |
| 133 |
Mime.MIMEtype("application", "ierjg").get_comment() |
| 134 |
|
| 135 |
def test_by_name(self): |
| 136 |
dot_c = Mime.get_type_by_name('foo.c') |
| 137 |
self.check_mimetype(dot_c, 'text', 'x-csrc') |
| 138 |
dot_C = Mime.get_type_by_name('foo.C') |
| 139 |
self.check_mimetype(dot_C, 'text', 'x-c++src') |
| 140 |
|
| 141 |
# But most names should be case insensitive |
| 142 |
dot_GIF = Mime.get_type_by_name('IMAGE.GIF') |
| 143 |
self.check_mimetype(dot_GIF, 'image', 'gif') |
| 144 |
|
| 145 |
def test_canonical(self): |
| 146 |
text_xml = Mime.lookup('text/xml') |
| 147 |
self.check_mimetype(text_xml, 'text', 'xml') |
| 148 |
self.check_mimetype(text_xml.canonical(), 'application', 'xml') |
| 149 |
|
| 150 |
# Already is canonical |
| 151 |
python = Mime.lookup('text/x-python') |
| 152 |
self.check_mimetype(python.canonical(), 'text', 'x-python') |
| 153 |
|
| 154 |
def test_inheritance(self): |
| 155 |
text_python = Mime.lookup('text/x-python') |
| 156 |
self.check_mimetype(text_python, 'text', 'x-python') |
| 157 |
text_plain = Mime.lookup('text/plain') |
| 158 |
app_executable = Mime.lookup('application/x-executable') |
| 159 |
self.assertEqual(text_python.inherits_from(), set([text_plain, app_executable])) |
| 160 |
|
| 161 |
def test_is_text(self): |
| 162 |
assert Mime._is_text(b'abcdef \n') |
| 163 |
assert not Mime._is_text(b'abcdef\x08') |
| 164 |
assert not Mime._is_text(b'abcdef\x0e') |
| 165 |
assert not Mime._is_text(b'abcdef\x1f') |
| 166 |
assert not Mime._is_text(b'abcdef\x7f') |
| 167 |
|
| 168 |
# Check nonexistant file. |
| 169 |
assert not Mime.is_text_file('/fwoijorij') |
| 170 |
|
| 171 |
class MagicDBTest(MimeTestBase): |
| 172 |
def setUp(self): |
| 173 |
self.tmpdir = tempfile.mkdtemp() |
| 174 |
self.path = os.path.join(self.tmpdir, 'mimemagic') |
| 175 |
with open(self.path, 'wb') as f: |
| 176 |
f.write(resources.mime_magic_db) |
| 177 |
|
| 178 |
self.path2 = os.path.join(self.tmpdir, 'mimemagic2') |
| 179 |
with open(self.path2, 'wb') as f: |
| 180 |
f.write(resources.mime_magic_db2) |
| 181 |
|
| 182 |
# Read the files |
| 183 |
self.magic = Mime.MagicDB() |
| 184 |
self.magic.merge_file(self.path) |
| 185 |
self.magic.merge_file(self.path2) |
| 186 |
self.magic.finalise() |
| 187 |
|
| 188 |
def tearDown(self): |
| 189 |
shutil.rmtree(self.tmpdir) |
| 190 |
|
| 191 |
def test_parsing(self): |
| 192 |
self.assertEqual(len(self.magic.bytype), 9) |
| 193 |
|
| 194 |
# Check repr() doesn't throw an error |
| 195 |
repr(self.magic) |
| 196 |
|
| 197 |
prio, png = self.magic.bytype[Mime.lookup('image', 'png')][0] |
| 198 |
self.assertEqual(prio, 50) |
| 199 |
assert isinstance(png, Mime.MagicRule), type(png) |
| 200 |
repr(png) # Check this doesn't throw an error. |
| 201 |
self.assertEqual(png.start, 0) |
| 202 |
self.assertEqual(png.value, b'\x89PNG') |
| 203 |
self.assertEqual(png.mask, None) |
| 204 |
self.assertEqual(png.also, None) |
| 205 |
|
| 206 |
prio, jpeg = self.magic.bytype[Mime.lookup('image', 'jpeg')][0] |
| 207 |
assert isinstance(jpeg, Mime.MagicMatchAny), type(jpeg) |
| 208 |
self.assertEqual(len(jpeg.rules), 2) |
| 209 |
self.assertEqual(jpeg.rules[0].value, b'\xff\xd8\xff') |
| 210 |
|
| 211 |
prio, ora = self.magic.bytype[Mime.lookup('image', 'openraster')][0] |
| 212 |
assert isinstance(ora, Mime.MagicRule), type(ora) |
| 213 |
self.assertEqual(ora.value, b'PK\x03\x04') |
| 214 |
ora1 = ora.also |
| 215 |
assert ora1 is not None |
| 216 |
self.assertEqual(ora1.start, 30) |
| 217 |
ora2 = ora1.also |
| 218 |
assert ora2 is not None |
| 219 |
self.assertEqual(ora2.start, 38) |
| 220 |
self.assertEqual(ora2.value, b'image/openraster') |
| 221 |
|
| 222 |
prio, svg = self.magic.bytype[Mime.lookup('image', 'svg+xml')][0] |
| 223 |
self.assertEqual(len(svg.rules), 2) |
| 224 |
self.assertEqual(svg.rules[0].value, b'<!DOCTYPE svg') |
| 225 |
self.assertEqual(svg.rules[0].range, 257) |
| 226 |
|
| 227 |
prio, psd = self.magic.bytype[Mime.lookup('image', 'vnd.adobe.photoshop')][0] |
| 228 |
self.assertEqual(psd.value, b'8BPS \0\0\0\0') |
| 229 |
self.assertEqual(psd.mask, b'\xff\xff\xff\xff\0\0\xff\xff\xff\xff') |
| 230 |
|
| 231 |
prio, elf = self.magic.bytype[Mime.lookup('application', 'x-executable')][0] |
| 232 |
self.assertEqual(elf.value, b'\x01\x11') |
| 233 |
self.assertEqual(elf.word, 2) |
| 234 |
|
| 235 |
# Test that a newline within the value doesn't break parsing. |
| 236 |
prio, madeup = self.magic.bytype[Mime.lookup('application', 'madeup')][0] |
| 237 |
self.assertEqual(madeup.rules[0].value, b'ab\ncd') |
| 238 |
self.assertEqual(madeup.rules[1].mask, b'\xff\xff\n\xff\xff') |
| 239 |
|
| 240 |
prio, replaced = self.magic.bytype[Mime.lookup('application', 'tobereplaced')][0] |
| 241 |
self.assertEqual(replaced.value, b'jkl') |
| 242 |
|
| 243 |
addedrules = self.magic.bytype[Mime.lookup('application', 'tobeaddedto')] |
| 244 |
self.assertEqual(len(addedrules), 2) |
| 245 |
self.assertEqual(addedrules[1][1].value, b'pqr') |
| 246 |
|
| 247 |
def test_match_data(self): |
| 248 |
res = self.magic.match_data(resources.png_data) |
| 249 |
self.check_mimetype(res, 'image', 'png') |
| 250 |
|
| 251 |
# Denied by min or max priority |
| 252 |
notpng_max40 = self.magic.match_data(resources.png_data, max_pri=40) |
| 253 |
assert notpng_max40 is None, notpng_max40 |
| 254 |
notpng_min60 = self.magic.match_data(resources.png_data, min_pri=60) |
| 255 |
assert notpng_min60 is None, notpng_min60 |
| 256 |
|
| 257 |
# With list of options |
| 258 |
options = [Mime.lookup('image', 'nonexistant'), # Missing MIMEtype should be dropped |
| 259 |
Mime.lookup('image','png'), Mime.lookup('image', 'jpeg')] |
| 260 |
res = self.magic.match_data(resources.png_data, possible=options) |
| 261 |
self.check_mimetype(res, 'image', 'png') |
| 262 |
|
| 263 |
# Non matching |
| 264 |
res = self.magic.match_data(b'oiejgoethetrkjgnwefergoijekngjekg') |
| 265 |
assert res is None, res |
| 266 |
|
| 267 |
def test_match_nested(self): |
| 268 |
data = b'PK\x03\x04' + (b' ' * 26) + b'mimetype' + b'image/openraster' |
| 269 |
res = self.magic.match_data(data) |
| 270 |
self.check_mimetype(res, 'image', 'openraster') |
| 271 |
|
| 272 |
def test_match_file(self): |
| 273 |
png_file = os.path.join(self.tmpdir, 'image') |
| 274 |
with open(png_file, 'wb') as f: |
| 275 |
f.write(resources.png_data) |
| 276 |
|
| 277 |
res = self.magic.match(png_file) |
| 278 |
self.check_mimetype(res, 'image', 'png') |
| 279 |
|
| 280 |
# With list of options |
| 281 |
options = [Mime.lookup('image','png'), Mime.lookup('image', 'jpeg'), |
| 282 |
Mime.lookup('image', 'nonexistant')] # Missing MIMEtype should be dropped |
| 283 |
res = self.magic.match(png_file, possible=options) |
| 284 |
self.check_mimetype(res, 'image', 'png') |
| 285 |
|
| 286 |
# Nonexistant file |
| 287 |
path = os.path.join(self.tmpdir, 'nonexistant') |
| 288 |
self.assertRaises(IOError, self.magic.match, path) |
| 289 |
|
| 290 |
_l = Mime.lookup |
| 291 |
|
| 292 |
class GlobDBTest(MimeTestBase): |
| 293 |
allglobs = {_l('text/x-makefile'): [(50, 'makefile', [])], |
| 294 |
_l('application/x-core'): [(50, 'core', ['cs']), (50, 'core', [])], |
| 295 |
_l('text/x-c++src'): [(50, '*.C', ['cs'])], |
| 296 |
_l('text/x-csrc'): [(50, '*.c', ['cs'])], |
| 297 |
_l('text/x-python'): [(50, '*.py', [])], |
| 298 |
_l('text/x-python'): [(50, '*.py', [])], # Check not added 2x |
| 299 |
_l('video/x-anim'): [(50, '*.anim[1-9j]', [])], |
| 300 |
_l('text/x-readme'): [(10, 'readme*', [])], |
| 301 |
_l('text/x-readme2'): [(20, 'readme2*', [])], |
| 302 |
_l('image/jpeg'): [(50, '*.jpg', []), (50, '*.jpeg', [])], |
| 303 |
} |
| 304 |
|
| 305 |
def setUp(self): |
| 306 |
self.globs = Mime.GlobDB() |
| 307 |
self.globs.allglobs = self.allglobs |
| 308 |
self.globs.finalise() |
| 309 |
|
| 310 |
def test_build_globdb(self): |
| 311 |
globs = self.globs |
| 312 |
|
| 313 |
self.assertEqual(len(globs.cased_literals), 1) |
| 314 |
assert 'core' in globs.cased_literals, globs.cased_literals |
| 315 |
|
| 316 |
literals = globs.literals |
| 317 |
self.assertEqual(len(literals), 2) |
| 318 |
assert 'core' in literals, literals |
| 319 |
assert 'makefile' in literals, literals |
| 320 |
|
| 321 |
cexts = globs.cased_exts |
| 322 |
self.assertEqual(len(cexts), 2) |
| 323 |
assert 'C' in cexts, cexts |
| 324 |
assert 'c' in cexts, cexts |
| 325 |
|
| 326 |
exts = globs.exts |
| 327 |
self.assertEqual(len(exts), 3) |
| 328 |
assert 'py' in exts, exts |
| 329 |
self.assertEqual(exts['py'], [(_l('text/x-python'), 50)] ) |
| 330 |
assert 'jpeg' in exts, exts |
| 331 |
assert 'jpg' in exts, exts |
| 332 |
|
| 333 |
pats = globs.globs |
| 334 |
self.assertEqual(len(pats), 3) |
| 335 |
self.assertEqual(pats[0][1], _l('video', 'x-anim')) |
| 336 |
self.assertEqual(pats[1][1], _l('text', 'x-readme2')) |
| 337 |
|
| 338 |
def test_first_match(self): |
| 339 |
g = self.globs |
| 340 |
|
| 341 |
self.check_mimetype(g.first_match('Makefile'), 'text', 'x-makefile') |
| 342 |
self.check_mimetype(g.first_match('core'), 'application', 'x-core') |
| 343 |
self.check_mimetype(g.first_match('foo.C'), 'text', 'x-c++src') |
| 344 |
self.check_mimetype(g.first_match('foo.c'), 'text', 'x-csrc') |
| 345 |
self.check_mimetype(g.first_match('foo.py'), 'text', 'x-python') |
| 346 |
self.check_mimetype(g.first_match('foo.Anim4'), 'video', 'x-anim') |
| 347 |
self.check_mimetype(g.first_match('README.txt'), 'text', 'x-readme') |
| 348 |
self.check_mimetype(g.first_match('README2.txt'), 'text', 'x-readme2') |
| 349 |
self.check_mimetype(g.first_match('README'), 'text', 'x-readme') |
| 350 |
|
| 351 |
qrte = g.first_match('qrte') |
| 352 |
assert qrte is None, qrte |
| 353 |
|
| 354 |
def test_all_matches(self): |
| 355 |
g = self.globs |
| 356 |
|
| 357 |
self.assertEqual(g.all_matches('qrte'), []) |
| 358 |
|
| 359 |
self.assertEqual(g.all_matches('Makefile'), |
| 360 |
[(_l('text', 'x-makefile'), 50)]) |
| 361 |
|
| 362 |
self.assertEqual(g.all_matches('readme2.rst'), |
| 363 |
[(_l('text', 'x-readme2'), 20), |
| 364 |
(_l('text', 'x-readme'), 10)] |
| 365 |
) |
| 366 |
|
| 367 |
def test_get_extensions(self): |
| 368 |
Mime.globs = self.globs |
| 369 |
Mime._cache_uptodate = True |
| 370 |
|
| 371 |
try: |
| 372 |
get_ext = Mime.get_extensions |
| 373 |
self.assertEqual(get_ext(_l('text/x-python')), set(['py'])) |
| 374 |
self.assertEqual(get_ext(_l('image/jpeg')), set(['jpg', 'jpeg'])) |
| 375 |
self.assertEqual(get_ext(_l('image/inary')), set()) |
| 376 |
finally: |
| 377 |
# Ensure that future tests will re-cache the database. |
| 378 |
Mime._cache_uptodate = False |
| 379 |
|
| 380 |
|
| 381 |
class GlobsParsingTest(MimeTestBase): |
| 382 |
def setUp(self): |
| 383 |
self.tmpdir = tempfile.mkdtemp() |
| 384 |
|
| 385 |
def tearDown(self): |
| 386 |
shutil.rmtree(self.tmpdir) |
| 387 |
|
| 388 |
def test_parsing(self): |
| 389 |
p1 = os.path.join(self.tmpdir, 'globs2a') |
| 390 |
with open(p1, 'w') as f: |
| 391 |
f.write(resources.mime_globs2_a) |
| 392 |
|
| 393 |
p2 = os.path.join(self.tmpdir, 'globs2b') |
| 394 |
with open(p2, 'w') as f: |
| 395 |
f.write(resources.mime_globs2_b) |
| 396 |
|
| 397 |
globs = Mime.GlobDB() |
| 398 |
globs.merge_file(p1) |
| 399 |
globs.merge_file(p2) |
| 400 |
|
| 401 |
ag = globs.allglobs |
| 402 |
self.assertEqual(ag[_l('text', 'x-diff')], |
| 403 |
set([(55, '*.patch', ()), (50, '*.diff', ())]) ) |
| 404 |
self.assertEqual(ag[_l('text', 'x-c++src')], set([(50, '*.C', ('cs',))]) ) |
| 405 |
self.assertEqual(ag[_l('text', 'x-readme')], set([(20, 'RDME', ('cs',))]) ) |
| 406 |
assert _l('text', 'x-python') not in ag, ag |