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_lookup(self): pdf1 = Mime.lookup("application/pdf") pdf2 = Mime.lookup("application", "pdf") self.assertEqual(pdf1, pdf2) self.check_mimetype(pdf1, 'application', 'pdf') pdf1.get_comment()
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_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'<!DOCTYPE svg') self.assertEqual(svg.rules[0].range, 257) prio, psd = self.magic.bytype[Mime.lookup('image', 'vnd.adobe.photoshop')][0] self.assertEqual(psd.value, b'8BPS \0\0\0\0') self.assertEqual(psd.mask, b'\xff\xff\xff\xff\0\0\xff\xff\xff\xff') prio, elf = self.magic.bytype[Mime.lookup('application', 'x-executable')][0] self.assertEqual(elf.value, b'\x01\x11') self.assertEqual(elf.word, 2) # Test that a newline within the value doesn't break parsing. prio, madeup = self.magic.bytype[Mime.lookup('application', 'madeup')][0] self.assertEqual(madeup.rules[0].value, b'ab\ncd') self.assertEqual(madeup.rules[1].mask, b'\xff\xff\n\xff\xff') prio, replaced = self.magic.bytype[Mime.lookup('application', 'tobereplaced')][0] self.assertEqual(replaced.value, b'jkl') addedrules = self.magic.bytype[Mime.lookup('application', 'tobeaddedto')] self.assertEqual(len(addedrules), 2) self.assertEqual(addedrules[1][1].value, b'pqr')
def test_match_file(self): png_file = os.path.join(self.tmpdir, 'image') with open(png_file, 'wb') as f: f.write(resources.png_data) res = self.magic.match(png_file) self.check_mimetype(res, 'image', 'png') # With list of options options = [Mime.lookup('image','png'), Mime.lookup('image', 'jpeg'), Mime.lookup('image', 'nonexistant')] # Missing MIMEtype should be dropped res = self.magic.match(png_file, possible=options) self.check_mimetype(res, 'image', 'png') # Nonexistant file path = os.path.join(self.tmpdir, 'nonexistant') self.assertRaises(IOError, self.magic.match, path)
def test_match_data(self): res = self.magic.match_data(resources.png_data) self.check_mimetype(res, 'image', 'png') # Denied by min or max priority notpng_max40 = self.magic.match_data(resources.png_data, max_pri=40) assert notpng_max40 is None, notpng_max40 notpng_min60 = self.magic.match_data(resources.png_data, min_pri=60) assert notpng_min60 is None, notpng_min60 # With list of options options = [Mime.lookup('image', 'nonexistant'), # Missing MIMEtype should be dropped Mime.lookup('image','png'), Mime.lookup('image', 'jpeg')] res = self.magic.match_data(resources.png_data, possible=options) self.check_mimetype(res, 'image', 'png') # Non matching res = self.magic.match_data(b'oiejgoethetrkjgnwefergoijekngjekg') assert res is None, res
def test_match_file(self): png_file = os.path.join(self.tmpdir, 'image') with open(png_file, 'wb') as f: f.write(resources.png_data) res = self.magic.match(png_file) self.check_mimetype(res, 'image', 'png') # With list of options options = [ Mime.lookup('image', 'png'), Mime.lookup('image', 'jpeg'), Mime.lookup('image', 'nonexistant') ] # Missing MIMEtype should be dropped res = self.magic.match(png_file, possible=options) self.check_mimetype(res, 'image', 'png') # Nonexistant file path = os.path.join(self.tmpdir, 'nonexistant') self.assertRaises(IOError, self.magic.match, path)
def test_match_data(self): res = self.magic.match_data(resources.png_data) self.check_mimetype(res, 'image', 'png') # Denied by min or max priority notpng_max40 = self.magic.match_data(resources.png_data, max_pri=40) assert notpng_max40 is None, notpng_max40 notpng_min60 = self.magic.match_data(resources.png_data, min_pri=60) assert notpng_min60 is None, notpng_min60 # With list of options options = [ Mime.lookup('image', 'nonexistant'), # Missing MIMEtype should be dropped Mime.lookup('image', 'png'), Mime.lookup('image', 'jpeg') ] res = self.magic.match_data(resources.png_data, possible=options) self.check_mimetype(res, 'image', 'png') # Non matching res = self.magic.match_data(b'oiejgoethetrkjgnwefergoijekngjekg') assert res is None, res
def test_mimetype_repr(self): mt = Mime.lookup('application', 'zip') repr(mt) # Just check that this doesn't throw an error.
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 __init__(self, name): self.name = name self.mimetype = Mime.lookup("text", "plain")
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')