Skip to content

Commit 54d86fd

Browse files
risicleahupp
authored andcommitted
python-magic tests: add test files for elf and json, use to test flags
elf-NetBSD-x86_64-echo is from https://github.com/JonathanSalwan/binary-samples under an MIT license incidentally this exposes that the ELF builtin magic detector only works on files, falling back to the soft magic for buffers
1 parent fd279e0 commit 54d86fd

3 files changed

Lines changed: 95 additions & 0 deletions

File tree

test/python_magic_test.py

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,10 @@ def test_mime_types(self):
9696
self.assert_values(
9797
m,
9898
{
99+
"elf-NetBSD-x86_64-echo": (
100+
"application/x-pie-executable",
101+
"application/x-sharedlib",
102+
),
99103
"magic._pyc_": (
100104
"application/octet-stream",
101105
"text/x-bytecode.python",
@@ -107,7 +111,9 @@ def test_mime_types(self):
107111
"text.txt": "text/plain",
108112
b"\xce\xbb".decode("utf-8"): "text/plain",
109113
b"\xce\xbb": "text/plain",
114+
"test.json": "application/json",
110115
},
116+
buf_equals_file=False,
111117
)
112118
finally:
113119
os.unlink(dest)
@@ -119,6 +125,88 @@ def test_descriptions(self):
119125
self.assert_values(
120126
m,
121127
{
128+
"elf-NetBSD-x86_64-echo": (
129+
"ELF 64-bit LSB shared object, x86-64, version 1 (SYSV)",
130+
"ELF 64-bit LSB pie executable, x86-64, version 1 (SYSV), dynamically linked, interpreter /libexec/ld.elf_so, for NetBSD 8.0, not stripped",
131+
),
132+
"magic._pyc_": "python 2.4 byte-compiled",
133+
"test.pdf": (
134+
"PDF document, version 1.2",
135+
"PDF document, version 1.2, 2 pages",
136+
"PDF document, version 1.2, 2 page(s)",
137+
),
138+
"test.gz": (
139+
'gzip compressed data, was "test", from Unix, last '
140+
"modified: Sun Jun 29 01:32:52 2008",
141+
'gzip compressed data, was "test", last modified'
142+
": Sun Jun 29 01:32:52 2008, from Unix",
143+
'gzip compressed data, was "test", last modified'
144+
": Sun Jun 29 01:32:52 2008, from Unix, original size 15",
145+
'gzip compressed data, was "test", '
146+
"last modified: Sun Jun 29 01:32:52 2008, "
147+
"from Unix, original size modulo 2^32 15",
148+
'gzip compressed data, was "test", last modified'
149+
": Sun Jun 29 01:32:52 2008, from Unix, truncated",
150+
),
151+
"text.txt": "ASCII text",
152+
"test.snappy.parquet": ("Apache Parquet", "Par archive data"),
153+
"test.json": "JSON text data",
154+
},
155+
buf_equals_file=False,
156+
)
157+
finally:
158+
del os.environ["TZ"]
159+
160+
def test_descriptions_no_soft(self):
161+
m = magic.Magic(check_soft=False)
162+
self.assert_values(
163+
m,
164+
{
165+
"elf-NetBSD-x86_64-echo": (
166+
"data",
167+
"ELF 64-bit LSB pie executable, x86-64, version 1 (SYSV), dynamically linked, interpreter /libexec/ld.elf_so, for NetBSD 8.0, not stripped",
168+
),
169+
"magic._pyc_": "data",
170+
"test.pdf": "ASCII text",
171+
"test.gz": "data",
172+
"text.txt": "ASCII text",
173+
"test.snappy.parquet": "data",
174+
"test.json": "JSON text data",
175+
},
176+
buf_equals_file=False,
177+
)
178+
179+
def test_descriptions_no_elf(self):
180+
m = magic.Magic(check_elf=False)
181+
self.assert_values(
182+
m,
183+
{
184+
"elf-NetBSD-x86_64-echo": "ELF 64-bit LSB shared object, x86-64, version 1 (SYSV)",
185+
},
186+
buf_equals_file=True,
187+
)
188+
189+
def test_descriptions_no_json(self):
190+
m = magic.Magic(check_elf=False)
191+
self.assert_values(
192+
m,
193+
{
194+
"test.json": "data",
195+
},
196+
buf_equals_file=True,
197+
)
198+
199+
def test_descriptions_no_json(self):
200+
m = magic.Magic(check_json=False)
201+
os.environ["TZ"] = "UTC" # To get last modified date of test.gz in UTC
202+
try:
203+
self.assert_values(
204+
m,
205+
{
206+
"elf-NetBSD-x86_64-echo": (
207+
"ELF 64-bit LSB shared object, x86-64, version 1 (SYSV)",
208+
"ELF 64-bit LSB pie executable, x86-64, version 1 (SYSV), dynamically linked, interpreter /libexec/ld.elf_so, for NetBSD 8.0, not stripped",
209+
),
122210
"magic._pyc_": "python 2.4 byte-compiled",
123211
"test.pdf": (
124212
"PDF document, version 1.2",
9.33 KB
Binary file not shown.

test/testdata/test.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
[
2+
{
3+
"one": 2,
4+
"three": null,
5+
"four": [5, "six", false]
6+
}
7+
]

0 commit comments

Comments
 (0)