proxmark3/tools/mfc/pm3_eml_mfd_test.py

54 lines
1.6 KiB
Python
Raw Normal View History

#!/usr/bin/env python3
from tempfile import mkdtemp
from shutil import rmtree
from string import hexdigits
import unittest, os
import pm3_eml2mfd, pm3_mfd2eml
class TestEmlMfd(unittest.TestCase):
def setUp(self):
self.tmpdir = mkdtemp()
2018-01-10 02:17:21 +08:00
def tearDown(self):
rmtree(self.tmpdir)
EML2MFD_TESTCASES = [
('', ''),
("41424344\r\n45464748\n494A4B4C\n", "ABCDEFGHIJKL")
]
def test_eml2mfd(self):
self.three_argument_test(pm3_eml2mfd.main, self.EML2MFD_TESTCASES)
2019-03-12 07:12:26 +08:00
def test_mfd2eml(self):
self.three_argument_test(pm3_mfd2eml.main,
map(reversed, self.EML2MFD_TESTCASES), c14n=hex_c14n)
def three_argument_test(self, operation, cases, c14n=str):
for case_input, case_output in cases:
try:
inp_name = os.path.join(self.tmpdir, 'input')
out_name = os.path.join(self.tmpdir, 'output')
with open(inp_name, 'w') as in_file:
in_file.write(case_input)
operation(['', inp_name, out_name])
with open(out_name, 'r') as out_file:
self.assertEqual(c14n(case_output), c14n(out_file.read()))
finally:
for file_name in inp_name, out_name:
if os.path.exists(file_name):
os.remove(file_name)
def hex_c14n(inp):
"""
Canonicalizes the input string by removing non-hexadecimal
characters and making everything uppercase
2018-01-10 02:17:21 +08:00
"""
return ''.join(c.upper() for c in inp if c in hexdigits)
if __name__ == '__main__':
unittest.main()