1
0

test.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. import os, sys
  2. # for output which reports a local time
  3. os.environ['TZ'] = 'GMT'
  4. import shutil
  5. import os.path
  6. import unittest
  7. import magic
  8. class MagicTest(unittest.TestCase):
  9. TESTDATA_DIR = os.path.join(os.path.dirname(__file__), 'testdata')
  10. def assert_values(self, m, expected_values):
  11. for filename, expected_value in expected_values.items():
  12. try:
  13. filename = os.path.join(self.TESTDATA_DIR, filename)
  14. except TypeError:
  15. filename = os.path.join(self.TESTDATA_DIR.encode('utf-8'), filename)
  16. if type(expected_value) is not tuple:
  17. expected_value = (expected_value,)
  18. for i in expected_value:
  19. with open(filename, 'rb') as f:
  20. buf_value = m.from_buffer(f.read())
  21. file_value = m.from_file(filename)
  22. if buf_value == i and file_value == i:
  23. break
  24. else:
  25. self.assertTrue(False, "no match for " + repr(expected_value))
  26. def test_from_buffer_str_and_bytes(self):
  27. m = magic.Magic(mime=True)
  28. s = '#!/usr/bin/env python\nprint("foo")'
  29. self.assertEqual("text/x-python", m.from_buffer(s))
  30. b = b'#!/usr/bin/env python\nprint("foo")'
  31. self.assertEqual("text/x-python", m.from_buffer(b))
  32. def test_mime_types(self):
  33. dest = os.path.join(MagicTest.TESTDATA_DIR, b'\xce\xbb'.decode('utf-8'))
  34. shutil.copyfile(os.path.join(MagicTest.TESTDATA_DIR, 'lambda'), dest)
  35. try:
  36. m = magic.Magic(mime=True)
  37. self.assert_values(m, {
  38. 'magic._pyc_': 'application/octet-stream',
  39. 'test.pdf': 'application/pdf',
  40. 'test.gz': 'application/gzip',
  41. 'text.txt': 'text/plain',
  42. b'\xce\xbb'.decode('utf-8'): 'text/plain',
  43. b'\xce\xbb': 'text/plain',
  44. })
  45. finally:
  46. os.unlink(dest)
  47. def test_descriptions(self):
  48. m = magic.Magic()
  49. os.environ['TZ'] = 'UTC' # To get the last modified date of test.gz in UTC
  50. try:
  51. self.assert_values(m, {
  52. 'magic._pyc_': 'python 2.4 byte-compiled',
  53. 'test.pdf': 'PDF document, version 1.2',
  54. 'test.gz':
  55. ('gzip compressed data, was "test", from Unix, last modified: Sun Jun 29 01:32:52 2008',
  56. 'gzip compressed data, was "test", last modified: Sun Jun 29 01:32:52 2008, from Unix'),
  57. 'text.txt': 'ASCII text',
  58. })
  59. finally:
  60. del os.environ['TZ']
  61. def test_mime_encodings(self):
  62. m = magic.Magic(mime_encoding=True)
  63. self.assert_values(m, {
  64. 'text-iso8859-1.txt': 'iso-8859-1',
  65. 'text.txt': 'us-ascii',
  66. })
  67. def test_errors(self):
  68. m = magic.Magic()
  69. self.assertRaises(IOError, m.from_file, 'nonexistent')
  70. self.assertRaises(magic.MagicException, magic.Magic,
  71. magic_file='nonexistent')
  72. os.environ['MAGIC'] = 'nonexistent'
  73. try:
  74. self.assertRaises(magic.MagicException, magic.Magic)
  75. finally:
  76. del os.environ['MAGIC']
  77. def test_keep_going(self):
  78. filename = os.path.join(self.TESTDATA_DIR, 'keep-going.jpg')
  79. m = magic.Magic(mime=True)
  80. self.assertEqual(m.from_file(filename), 'image/jpeg')
  81. m = magic.Magic(mime=True, keep_going=True)
  82. self.assertEqual(m.from_file(filename), 'image/jpeg')
  83. def test_rethrow(self):
  84. old = magic.magic_buffer
  85. try:
  86. def t(x,y):
  87. raise magic.MagicException("passthrough")
  88. magic.magic_buffer = t
  89. self.assertRaises(magic.MagicException, magic.from_buffer, "hello", True)
  90. finally:
  91. magic.magic_buffer = old
  92. if __name__ == '__main__':
  93. unittest.main()