R&Y: Added DAY RTA Tapp Card and Additional BKK BEM Stored Value Card AIDs to `aid_de...
[RRG-proxmark3.git] / tools / findbits_test.py
blob403dae4133b0b876609078ef54ffa0e2c7f0b36d
1 #!/usr/bin/env python3
3 import unittest, sys, findbits
5 class TestFindBits(unittest.TestCase):
6 def setUp(self):
7 self.old_stdout = sys.stdout
8 sys.stdout = OutputBuffer()
10 def tearDown(self):
11 sys.stdout = self.old_stdout
13 INVERT_CASES = [
14 ('10', '01'),
15 ('', ''),
17 def test_invert(self):
18 self.commutative_test(findbits.invert, self.INVERT_CASES)
20 SEARCH_CASES = [
21 ('1111', '10111101', ['Match at bit 2', '0<1111>0']),
22 ('00', '10111101', ['Not found']),
24 def test_search(self):
25 for target, data, expected_fragments in self.SEARCH_CASES:
26 sys.stdout.clear_buffer()
27 findbits.search(target, data)
28 for fragment in expected_fragments:
29 self.assertIn(fragment, sys.stdout.content)
31 BINSTRING_CASES = [
32 (42, '101010'),
33 (1, '1'),
34 (0, ''),
36 def test_binstring(self):
37 self.unary_operation_test(findbits.binstring, self.BINSTRING_CASES)
39 REVERSE_CASES = [
40 ('abc', 'cba'),
41 ('', ''),
43 def test_stringreverse(self):
44 self.commutative_test(findbits.stringreverse, self.REVERSE_CASES)
46 def commutative_test(self, operation, cases):
47 self.unary_operation_test(operation, cases)
48 self.unary_operation_test(operation, map(reversed, cases))
50 def unary_operation_test(self, operation, cases):
51 for case_in, case_out in cases:
52 self.assertEqual(operation(case_in), case_out)
55 class OutputBuffer(object):
56 def __init__(self):
57 self.clear_buffer()
59 def clear_buffer(self):
60 self.content = ''
62 def write(self, data):
63 self.content += data
66 if __name__ == '__main__':
67 unittest.main()