forked from cdgriffith/puremagic
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_common_extensions.py
187 lines (160 loc) · 7.68 KB
/
test_common_extensions.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
import unittest
import puremagic
from tempfile import NamedTemporaryFile
import os
LOCAL_DIR = os.path.realpath(os.path.dirname(__file__))
IMAGE_DIR = os.path.join(LOCAL_DIR, "resources", "images")
VIDEO_DIR = os.path.join(LOCAL_DIR, "resources", "video")
AUDIO_DIR = os.path.join(LOCAL_DIR, "resources", "audio")
OFFICE_DIR = os.path.join(LOCAL_DIR, "resources", "office")
ARCHIVE_DIR = os.path.join(LOCAL_DIR, "resources", "archive")
MEDIA_DIR = os.path.join(LOCAL_DIR, "resources", "media")
SYSTEM_DIR = os.path.join(LOCAL_DIR, "resources", "system")
TGA_FILE = os.path.join(IMAGE_DIR, "test.tga")
class TestMagic(unittest.TestCase):
def setUp(self):
self.mp4magic = b"\x00\x00\x00\x1C\x66\x74\x79\x70\x4D\x53\x4E\
\x56\x01\x29\x00\x46\x4D\x53\x4E\x56\x6D\x70\x34\x32"
self.expect_ext = ".mp4"
self.expect_mime = "video/mp4"
def test_file(self):
"""File identification |"""
mp4file = NamedTemporaryFile(delete=False)
mp4file.write(self.mp4magic)
mp4file.close()
ext = puremagic.from_file(mp4file.name)
os.unlink(mp4file.name)
self.assertEqual(self.expect_ext, ext)
def test_hex_string(self):
"""Hex string identification |"""
ext = puremagic.from_string(self.mp4magic)
self.assertEqual(self.expect_ext, ext)
def test_string(self):
"""String identification |"""
ext = puremagic.from_string(bytes(self.mp4magic))
self.assertEqual(self.expect_ext, ext)
def test_string_with_filename_hint(self):
"""String identification with filename hint |"""
filename=os.path.join(OFFICE_DIR, "test.xlsx")
f = open(filename, "rb")
data = f.read()
f.close()
ext = puremagic.from_string(data)
# .docx and .xlsx have same signature
self.assertEqual(".docx", ext)
# with the hint from_string() shoud find the correct extension
ext = puremagic.from_string(data, filename=filename)
self.assertEqual(".xlsx", ext)
def test_string_with_confidence(self):
"""String identification: magic_string |"""
ext = puremagic.magic_string(bytes(self.mp4magic))
self.assertEqual(self.expect_ext, ext[0][0])
self.assertRaises(ValueError, puremagic.magic_string, "")
def test_magic_string_with_filename_hint(self):
"""String identification: magic_string with hint|"""
filename=os.path.join(OFFICE_DIR, "test.xlsx")
f = open(filename, "rb")
data = f.read()
f.close()
ext = puremagic.magic_string(data, filename=filename)
self.assertEqual(".xlsx", ext[0][0])
def test_not_found(self):
"""Bad file type via string |"""
try:
with self.assertRaises(puremagic.PureError):
puremagic.from_string("not applicable string")
except TypeError:
# Python 2.6 doesn't support using assertRaises as a context manager
pass
def test_magic_file(self):
"""File identification with magic_file |"""
self.assertEqual(puremagic.magic_file(TGA_FILE)[0][0], ".tga")
open("test_empty_file", "w").close()
try:
self.assertRaises(ValueError, puremagic.magic_file, "test_empty_file")
finally:
os.unlink("test_empty_file")
def test_mime(self):
"""Identify mime type |"""
self.assertEqual(puremagic.from_file(TGA_FILE, True), "image/tga")
def test_images(self):
"""Test common image formats |"""
for item in os.listdir(IMAGE_DIR):
try:
ext = puremagic.from_file(os.path.join(IMAGE_DIR, item))
except puremagic.PureError:
raise AssertionError("Could not identify file"
" '{0}'".format(item))
self.assertTrue(item.endswith(ext),
"Expected .{0}, got {1}".format(item.split(".")[1],
ext))
def test_video(self):
"""Test common video formats |"""
for item in os.listdir(VIDEO_DIR):
try:
ext = puremagic.from_file(os.path.join(VIDEO_DIR, item))
except puremagic.PureError:
raise AssertionError("Could not identify file"
" '{0}'".format(item))
self.assertTrue(item.endswith(ext),
"Expected .{0}, got {1}".format(item.split(".")[1],
ext))
def test_audio(self):
"""Test common audio formats |"""
for item in os.listdir(AUDIO_DIR):
try:
ext = puremagic.from_file(os.path.join(AUDIO_DIR, item))
except puremagic.PureError:
raise AssertionError("Could not identify file"
" '{0}'".format(item))
self.assertTrue(item.endswith(ext),
"Expected .{0}, got {1}".format(item.split(".")[1],
ext))
def test_office(self):
"""Test common office document formats |"""
### Office files have very similar magic numbers, and may overlap
for item in os.listdir(OFFICE_DIR):
puremagic.from_file(os.path.join(OFFICE_DIR, item))
def test_archive(self):
"""Test common compressed archive formats |"""
for item in os.listdir(ARCHIVE_DIR):
try:
ext = puremagic.from_file(os.path.join(ARCHIVE_DIR, item))
except puremagic.PureError:
raise AssertionError("Could not identify file"
" '{0}'".format(item))
self.assertTrue(item.endswith(ext),
"Expected .{0}, got {1}".format(item.split(".")[1],
ext))
def test_media(self):
"""Test common media formats |"""
for item in os.listdir(MEDIA_DIR):
try:
ext = puremagic.from_file(os.path.join(MEDIA_DIR, item))
except puremagic.PureError:
raise AssertionError("Could not identify file "
"'{0}'".format(item))
self.assertTrue(item.endswith(ext),
"Expected .{0}, got {1}".format(item.split(".")[1],
ext))
def test_system(self):
"""Test common system formats |"""
for item in os.listdir(SYSTEM_DIR):
try:
ext = puremagic.from_file(os.path.join(SYSTEM_DIR, item))
except puremagic.PureError:
raise AssertionError("Could not identify file"
" '{0}'".format(item))
self.assertTrue(item.endswith(ext),
"Expected .{0}, got {1}".format(item.split(".")[1],
ext))
def test_ext(self):
"""Test ext from filename |"""
ext = puremagic.ext_from_filename("test.tar.bz2")
assert ext == ".tar.bz2", ext
def test_cmd_options(self):
"""Test CLI options |"""
from puremagic.__main__ import _command_line_entry
_command_line_entry(__file__, "test.py")
suite = unittest.TestLoader().loadTestsFromTestCase(TestMagic)
unittest.TextTestRunner(verbosity=2).run(suite)