Serhiy Storchaka | 1ac0095 | 2014-01-26 23:48:38 +0200 | [diff] [blame] | 1 | import imghdr |
| 2 | import io |
| 3 | import os |
| 4 | import unittest |
| 5 | import warnings |
| 6 | from test.support import findfile, TESTFN, unlink |
| 7 | |
| 8 | TEST_FILES = ( |
| 9 | ('python.png', 'png'), |
| 10 | ('python.gif', 'gif'), |
| 11 | ('python.bmp', 'bmp'), |
| 12 | ('python.ppm', 'ppm'), |
| 13 | ('python.pgm', 'pgm'), |
| 14 | ('python.pbm', 'pbm'), |
| 15 | ('python.jpg', 'jpeg'), |
| 16 | ('python.ras', 'rast'), |
| 17 | ('python.sgi', 'rgb'), |
| 18 | ('python.tiff', 'tiff'), |
Serhiy Storchaka | 2f8dca7 | 2014-05-25 11:45:37 +0300 | [diff] [blame] | 19 | ('python.xbm', 'xbm'), |
| 20 | ('python.webp', 'webp'), |
R David Murray | 2f60820 | 2014-06-26 12:27:57 -0400 | [diff] [blame] | 21 | ('python.exr', 'exr'), |
Serhiy Storchaka | 1ac0095 | 2014-01-26 23:48:38 +0200 | [diff] [blame] | 22 | ) |
| 23 | |
| 24 | class UnseekableIO(io.FileIO): |
| 25 | def tell(self): |
| 26 | raise io.UnsupportedOperation |
| 27 | |
| 28 | def seek(self, *args, **kwargs): |
| 29 | raise io.UnsupportedOperation |
| 30 | |
| 31 | class TestImghdr(unittest.TestCase): |
| 32 | @classmethod |
| 33 | def setUpClass(cls): |
| 34 | cls.testfile = findfile('python.png', subdir='imghdrdata') |
| 35 | with open(cls.testfile, 'rb') as stream: |
| 36 | cls.testdata = stream.read() |
| 37 | |
| 38 | def tearDown(self): |
| 39 | unlink(TESTFN) |
| 40 | |
| 41 | def test_data(self): |
| 42 | for filename, expected in TEST_FILES: |
| 43 | filename = findfile(filename, subdir='imghdrdata') |
| 44 | self.assertEqual(imghdr.what(filename), expected) |
| 45 | with open(filename, 'rb') as stream: |
| 46 | self.assertEqual(imghdr.what(stream), expected) |
| 47 | with open(filename, 'rb') as stream: |
| 48 | data = stream.read() |
| 49 | self.assertEqual(imghdr.what(None, data), expected) |
| 50 | self.assertEqual(imghdr.what(None, bytearray(data)), expected) |
| 51 | |
| 52 | def test_register_test(self): |
| 53 | def test_jumbo(h, file): |
| 54 | if h.startswith(b'eggs'): |
| 55 | return 'ham' |
| 56 | imghdr.tests.append(test_jumbo) |
| 57 | self.addCleanup(imghdr.tests.pop) |
| 58 | self.assertEqual(imghdr.what(None, b'eggs'), 'ham') |
| 59 | |
| 60 | def test_file_pos(self): |
| 61 | with open(TESTFN, 'wb') as stream: |
| 62 | stream.write(b'ababagalamaga') |
| 63 | pos = stream.tell() |
| 64 | stream.write(self.testdata) |
| 65 | with open(TESTFN, 'rb') as stream: |
| 66 | stream.seek(pos) |
| 67 | self.assertEqual(imghdr.what(stream), 'png') |
| 68 | self.assertEqual(stream.tell(), pos) |
| 69 | |
| 70 | def test_bad_args(self): |
| 71 | with self.assertRaises(TypeError): |
| 72 | imghdr.what() |
| 73 | with self.assertRaises(AttributeError): |
| 74 | imghdr.what(None) |
| 75 | with self.assertRaises(TypeError): |
| 76 | imghdr.what(self.testfile, 1) |
| 77 | with self.assertRaises(AttributeError): |
| 78 | imghdr.what(os.fsencode(self.testfile)) |
| 79 | with open(self.testfile, 'rb') as f: |
| 80 | with self.assertRaises(AttributeError): |
| 81 | imghdr.what(f.fileno()) |
| 82 | |
| 83 | def test_invalid_headers(self): |
| 84 | for header in (b'\211PN\r\n', |
| 85 | b'\001\331', |
| 86 | b'\x59\xA6', |
| 87 | b'cutecat', |
| 88 | b'000000JFI', |
| 89 | b'GIF80'): |
| 90 | self.assertIsNone(imghdr.what(None, header)) |
| 91 | |
| 92 | def test_string_data(self): |
| 93 | with warnings.catch_warnings(): |
| 94 | warnings.simplefilter("ignore", BytesWarning) |
| 95 | for filename, _ in TEST_FILES: |
| 96 | filename = findfile(filename, subdir='imghdrdata') |
| 97 | with open(filename, 'rb') as stream: |
| 98 | data = stream.read().decode('latin1') |
| 99 | with self.assertRaises(TypeError): |
| 100 | imghdr.what(io.StringIO(data)) |
| 101 | with self.assertRaises(TypeError): |
| 102 | imghdr.what(None, data) |
| 103 | |
| 104 | def test_missing_file(self): |
| 105 | with self.assertRaises(FileNotFoundError): |
| 106 | imghdr.what('missing') |
| 107 | |
| 108 | def test_closed_file(self): |
| 109 | stream = open(self.testfile, 'rb') |
| 110 | stream.close() |
| 111 | with self.assertRaises(ValueError) as cm: |
| 112 | imghdr.what(stream) |
| 113 | stream = io.BytesIO(self.testdata) |
| 114 | stream.close() |
| 115 | with self.assertRaises(ValueError) as cm: |
| 116 | imghdr.what(stream) |
| 117 | |
| 118 | def test_unseekable(self): |
| 119 | with open(TESTFN, 'wb') as stream: |
| 120 | stream.write(self.testdata) |
| 121 | with UnseekableIO(TESTFN, 'rb') as stream: |
| 122 | with self.assertRaises(io.UnsupportedOperation): |
| 123 | imghdr.what(stream) |
| 124 | |
| 125 | def test_output_stream(self): |
| 126 | with open(TESTFN, 'wb') as stream: |
| 127 | stream.write(self.testdata) |
| 128 | stream.seek(0) |
| 129 | with self.assertRaises(OSError) as cm: |
| 130 | imghdr.what(stream) |
| 131 | |
| 132 | if __name__ == '__main__': |
| 133 | unittest.main() |