def test_search_string_non_luhn(self): ps = PanScanner() n = '4111111111111113' test_match = ps.reqex.match(n) self.assertTrue(test_match) matches = ps.search_string(n) self.assertEqual(len(matches), 0)
def test_iter_dir_absolute_path(self): ps = PanScanner() directory = get_absolute_path('test_dir') self.assertEqual( [v for v in ps.iter_dir(directory)], [get_absolute_path('test_dir/binary.png'), get_absolute_path('test_dir/without.py'), get_absolute_path('test_dir/deeper/contains.log')])
def test_iter_dir_absolute_path(self): ps = PanScanner() directory = get_absolute_path('test_dir') self.assertEqual([v for v in ps.iter_dir(directory)], [ get_absolute_path('test_dir/binary.png'), get_absolute_path('test_dir/without.py'), get_absolute_path('test_dir/deeper/contains.log') ])
def test_search_file_binary(self): searched_lines = [] ps = PanScanner() ps.search_string = lambda l: searched_lines.append(l) filename = get_absolute_path('test_dir/binary.png') ps.search_file(filename) self.assertEqual(searched_lines, []) self.assertEqual(len(ps.failed_to_open), 0)
def test_search_string_two_match(self): ps = PanScanner() matches = ps.search_string('there is one match: 4111111111111111' 'no two matches (!): 4111111111111111') self.assertEqual(len(matches), 2) self.assertEqual(matches[0].span(), (20, 36)) self.assertEqual(matches[1].span(), (56, 72)) self.assertEqual(matches[0].group(0), '4111111111111111') self.assertEqual(matches[1].group(0), '4111111111111111')
def test_search_file_text(self): ps = PanScanner() filename = get_absolute_path('test_dir/deeper/contains.log') ps.search_file(filename) self.assertEqual(ps._log, [ 'Found card number in %s:\n' % filename, '* Card number found at line 1 in interval: (28, 44)\n', '* Card number found at line 2 in interval: (16, 32), (57, 73)\n' ]) self.assertEqual(len(ps.failed_to_open), 0)
def test_search(self): directory = get_absolute_path('test_dir') ps = PanScanner([directory]) ps.search() self.assertEqual(ps._log, [ 'Found card number in %s:\n' % get_absolute_path('test_dir/deeper/contains.log'), '* Card number found at line 1 in interval: (28, 44)\n', '* Card number found at line 2 in interval: (16, 32), (57, 73)\n' ])
def test_matches_to_string(self): class Match: def __init__(self, span): self._span = span def span(self): return self._span matches = [Match((1, 2)), Match((10, 11))] ps = PanScanner() matches_string = ps.matches_to_string(matches) self.assertEqual(matches_string, '(1, 2), (10, 11)')
def test_search_missing_file(self): ps = PanScanner() path = get_absolute_path('test_dir/does_not_exist.txt') ps.search_file(path) self.assertEqual(ps.failed_to_open, [path])
def test_search_string_no_matches(self): ps = PanScanner() matches = ps.search_string('there is no match :(') self.assertEqual(len(matches), 0)
def test_search_string_one_match(self): ps = PanScanner() matches = ps.search_string('there is one match: 4111111111111111') self.assertEqual(len(matches), 1) self.assertEqual(matches[0].span(), (20, 36)) self.assertEqual(matches[0].group(0), '4111111111111111')
def test_search_string_ignore_test_card_numbers(self): ps = PanScanner(ignore_test_card_numbers=True) matches = ps.search_string('4111111111111111') self.assertEqual(len(matches), 0) matches = ps.search_string('4716334938933348') self.assertEqual(len(matches), 1)
def test_search_string_number_surrounded(self): ps = PanScanner() matches = ps.search_string('3334111111111111111333') self.assertEqual(len(matches), 0)
def test_search_string_letter_surrounded(self): ps = PanScanner() matches = ps.search_string('asd4111111111111111asd') self.assertEqual(len(matches), 1) self.assertEqual(matches[0].group(0), '4111111111111111')
def test_search_string_comma_surrounded(self): ps = PanScanner() matches = ps.search_string(',4111111111111111') self.assertEqual(len(matches), 0) matches = ps.search_string('4111111111111111,') self.assertEqual(len(matches), 0)
def test_log_failed_to_open_none(self): ps = PanScanner() ps.failed_to_open = [] ps.log_failed_to_open() self.assertEqual(len(ps._log), 0)
def test_log_failed_to_open_one(self): ps = PanScanner() ps.failed_to_open = ['/testfile.txt'] ps.log_failed_to_open() self.assertEqual(ps._log, ["Failed to open:\n* /testfile.txt\n"])