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