from xdg import Mime import unittest import os.path import tempfile, shutil import resources example_dir = os.path.join(os.path.dirname(__file__), 'example') def example_file(filename): return os.path.join(example_dir, filename) class MimeTestBase(unittest.TestCase): def check_mimetype(self, mimetype, media, subtype): self.assertEqual(mimetype.media, media) self.assertEqual(mimetype.subtype, subtype) class MimeTest(MimeTestBase): def test_create_mimetype(self): mt1 = Mime.MIMEtype('application', 'pdf') mt2 = Mime.MIMEtype('application', 'pdf') self.assertEqual(id(mt1), id(mt2)) # Check caching amr = Mime.MIMEtype('audio', 'AMR') self.check_mimetype(amr, 'audio', 'amr') # Check lowercase ogg = Mime.MIMEtype('audio/ogg') self.check_mimetype(ogg, 'audio', 'ogg') # Check split on / self.assertRaises(Exception, Mime.MIMEtype, 'audio/foo/bar') def test_get_type_by_name(self): appzip = Mime.get_type_by_name("foo.zip") self.check_mimetype(appzip, 'application', 'zip') def test_get_type_by_data(self): imgpng = Mime.get_type_by_data(resources.png_data) self.check_mimetype(imgpng, 'image', 'png') def test_mimetype_repr(self): mt = Mime.lookup('application', 'zip') repr(mt) # Just check that this doesn't throw an error. def test_get_type_by_contents(self): tmpdir = tempfile.mkdtemp() try: test_file = os.path.join(tmpdir, "test") with open(test_file, "wb") as f: f.write(resources.png_data) imgpng = Mime.get_type_by_contents(test_file) self.check_mimetype(imgpng, 'image', 'png') finally: shutil.rmtree(tmpdir) def test_get_type(self): # File that doesn't exist - get type by name imgpng = Mime.get_type(example_file("test.gif")) self.check_mimetype(imgpng, 'image', 'gif') # File that does exist - get type by contents imgpng = Mime.get_type(example_file("png_file")) self.check_mimetype(imgpng, 'image', 'png') # Directory - special case inodedir = Mime.get_type(example_file("subdir")) self.check_mimetype(inodedir, 'inode', 'directory') # Mystery files mystery_text = Mime.get_type(example_file('mystery_text')) self.check_mimetype(mystery_text, 'text', 'plain') mystery_exe = Mime.get_type(example_file('mystery_exe')) self.check_mimetype(mystery_exe, 'application', 'executable') # Symlink print("test-mime.py::test_get_type: skip symlink tests") #self.check_mimetype(Mime.get_type(example_file("png_symlink")), # 'image', 'png') #self.check_mimetype(Mime.get_type(example_file("png_symlink"), follow=False), # 'inode', 'symlink') def test_get_type2(self): # File that doesn't exist - use the name self.check_mimetype(Mime.get_type2(example_file('test.gif')), 'image', 'gif') # File that does exist - use the contents self.check_mimetype(Mime.get_type2(example_file('png_file')), 'image', 'png') # Does exist - use name before contents self.check_mimetype(Mime.get_type2(example_file('file.png')), 'image', 'png') self.check_mimetype(Mime.get_type2(example_file('word.doc')), 'application', 'msword') # Ambiguous file extension glade_mime = Mime.get_type2(example_file('glade.ui')) self.assertEqual(glade_mime.media, 'application') # Grumble, this is still ambiguous on some systems self.assertIn(glade_mime.subtype, {'x-gtk-builder', 'x-glade'}) self.check_mimetype(Mime.get_type2(example_file('qtdesigner.ui')), 'application', 'x-designer') # text/x-python has greater weight than text/x-readme self.check_mimetype(Mime.get_type2(example_file('README.py')), 'text', 'x-python') # Directory - special filesystem object self.check_mimetype(Mime.get_type2(example_file('subdir')), 'inode', 'directory') # Mystery files: mystery_missing = Mime.get_type2(example_file('mystery_missing')) self.check_mimetype(mystery_missing, 'application', 'octet-stream') mystery_binary = Mime.get_type2(example_file('mystery_binary')) self.check_mimetype(mystery_binary, 'application', 'octet-stream') mystery_text = Mime.get_type2(example_file('mystery_text')) self.check_mimetype(mystery_text, 'text', 'plain') mystery_exe = Mime.get_type2(example_file('mystery_exe')) self.check_mimetype(mystery_exe, 'application', 'executable') # Symlink print("test-mime.py::test_get_type2: skip symlink tests") #self.check_mimetype(Mime.get_type2(example_file("png_symlink")), # 'image', 'png') #self.check_mimetype(Mime.get_type2(example_file("png_symlink"), follow=False), # 'inode', 'symlink') def test_lookup(self): pdf1 = Mime.lookup("application/pdf") pdf2 = Mime.lookup("application", "pdf") self.assertEqual(pdf1, pdf2) self.check_mimetype(pdf1, 'application', 'pdf') def test_get_comment(self): # Check these don't throw an error. One that is likely to exist: Mime.MIMEtype("application", "pdf").get_comment() # And one that's unlikely to exist: Mime.MIMEtype("application", "ierjg").get_comment() def test_by_name(self): dot_c = Mime.get_type_by_name('foo.c') self.check_mimetype(dot_c, 'text', 'x-csrc') dot_C = Mime.get_type_by_name('foo.C') self.check_mimetype(dot_C, 'text', 'x-c++src') # But most names should be case insensitive dot_GIF = Mime.get_type_by_name('IMAGE.GIF') self.check_mimetype(dot_GIF, 'image', 'gif') def test_canonical(self): text_xml = Mime.lookup('text/xml') self.check_mimetype(text_xml, 'text', 'xml') self.check_mimetype(text_xml.canonical(), 'application', 'xml') # Already is canonical python = Mime.lookup('text/x-python') self.check_mimetype(python.canonical(), 'text', 'x-python') def test_inheritance(self): text_python = Mime.lookup('text/x-python') self.check_mimetype(text_python, 'text', 'x-python') text_plain = Mime.lookup('text/plain') app_executable = Mime.lookup('application/x-executable') self.assertEqual(text_python.inherits_from(), set([text_plain, app_executable])) def test_is_text(self): assert Mime._is_text(b'abcdef \n') assert not Mime._is_text(b'abcdef\x08') assert not Mime._is_text(b'abcdef\x0e') assert not Mime._is_text(b'abcdef\x1f') assert not Mime._is_text(b'abcdef\x7f') # Check nonexistant file. assert not Mime.is_text_file('/fwoijorij') class MagicDBTest(MimeTestBase): def setUp(self): self.tmpdir = tempfile.mkdtemp() self.path = os.path.join(self.tmpdir, 'mimemagic') with open(self.path, 'wb') as f: f.write(resources.mime_magic_db) self.path2 = os.path.join(self.tmpdir, 'mimemagic2') with open(self.path2, 'wb') as f: f.write(resources.mime_magic_db2) # Read the files self.magic = Mime.MagicDB() self.magic.merge_file(self.path) self.magic.merge_file(self.path2) self.magic.finalise() def tearDown(self): shutil.rmtree(self.tmpdir) def test_parsing(self): self.assertEqual(len(self.magic.bytype), 9) # Check repr() doesn't throw an error repr(self.magic) prio, png = self.magic.bytype[Mime.lookup('image', 'png')][0] self.assertEqual(prio, 50) assert isinstance(png, Mime.MagicRule), type(png) repr(png) # Check this doesn't throw an error. self.assertEqual(png.start, 0) self.assertEqual(png.value, b'\x89PNG') self.assertEqual(png.mask, None) self.assertEqual(png.also, None) prio, jpeg = self.magic.bytype[Mime.lookup('image', 'jpeg')][0] assert isinstance(jpeg, Mime.MagicMatchAny), type(jpeg) self.assertEqual(len(jpeg.rules), 2) self.assertEqual(jpeg.rules[0].value, b'\xff\xd8\xff') prio, ora = self.magic.bytype[Mime.lookup('image', 'openraster')][0] assert isinstance(ora, Mime.MagicRule), type(ora) self.assertEqual(ora.value, b'PK\x03\x04') ora1 = ora.also assert ora1 is not None self.assertEqual(ora1.start, 30) ora2 = ora1.also assert ora2 is not None self.assertEqual(ora2.start, 38) self.assertEqual(ora2.value, b'image/openraster') prio, svg = self.magic.bytype[Mime.lookup('image', 'svg+xml')][0] self.assertEqual(len(svg.rules), 2) self.assertEqual(svg.rules[0].value, b'