def test_compare_sums_short(self): tlst = [(10, '01/Jul/1995:00:00:01 -0400'), (2, '01/Jul/1995:00:00:01 -0400'), (1, '01/Jul/1995:00:00:01 -0400'), (3, '01/Jul/1995:00:00:01 -0400')] lst = [10, 2, 1, 3] secs = 10 end = 3 self.assertEqual(pl.LogProcessor().compare_sums(tlst, lst, secs, end), (16, '01/Jul/1995:00:00:01 -0400'))
class TestTopTen(unittest.TestCase): data = pl.LogProcessor().main(DEFAULT_OUTPUT_PATH, 'top10', TEST_LOG_PATH, 1) def test_tenresults(self): # Parse test file and make sure format is correct self.assertEqual(len(self.data),3) def test_tenformat(self): self.assertEqual(self.data,[(6,'199.72.81.55'), (3,'burger.letters.com'), (1,'unicomp6.unicomp.net')])
def test_write_log(self): data = "This is a test" mode = "string" file = pl.LogProcessor().write_log(data, mode, DEFAULT_OUTPUT_PATH, 'test.txt') f = open(os.path.join(DEFAULT_OUTPUT_PATH, 'test.txt'), encoding='latin-1') self.assertEqual(f.read(), "This is a test") f.close() os.remove(os.path.join(DEFAULT_OUTPUT_PATH, 'test.txt'))
class TestBandwidth(unittest.TestCase): data = pl.LogProcessor().main(DEFAULT_OUTPUT_PATH, 'bandwidth', TEST_LOG_PATH, 1) def test_tenresults(self): # Parse test file and make sure format is correct self.assertEqual(len(self.data), 3) def test_tenformat(self): self.assertEqual(self.data, [(8520, '/login'), (7970, '/shuttle/countdown/'), (0, '/shuttle/countdown/liftoff.html')])
def test_filter_peaks_fail(self): lst = [(10, '01/Jul/1995:00:00:01 -0400'), (2, '01/Jul/1995:00:00:02 -0400'), (1, '01/Jul/1995:00:00:03 -0400'), (3, '01/Jul/1995:00:00:04 -0400'), (10, '01/Jul/1995:00:00:05 -0400'), (7, '01/Jul/1995:00:00:06 -0400'), (1, '01/Jul/1995:00:00:07 -0400'), (4, '01/Jul/1995:00:20:08 -0400'), (10, '01/Jul/1995:00:00:09 -0400'), (5, '01/Jul/1995:00:00:10 -0400'), (1, '01/Jul/1995:20:00:11 -0400'), (1, '01/Jul/1995:00:00:12 -0400'), (10, '01/Jul/1995:00:00:13 -0400'), (8, '01/Jul/1995:00:00:14 -0400'), (1, '01/Jul/1995:00:00:15 -0400'), (10, '01/Jul/1995:10:00:16 -0400')] self.assertEqual(pl.LogProcessor().filter_peaks(lst, 1), False)
def test_compare_sums_long(self): tlst = [(10, '01/Jul/1995:00:00:01 -0400'), (2, '01/Jul/1995:00:00:02 -0400'), (1, '01/Jul/1995:00:00:03 -0400'), (3, '01/Jul/1995:00:00:04 -0400'), (10, '01/Jul/1995:00:00:05 -0400'), (7, '01/Jul/1995:00:00:06 -0400'), (1, '01/Jul/1995:00:00:07 -0400'), (4, '01/Jul/1995:00:00:08 -0400'), (10, '01/Jul/1995:00:00:09 -0400'), (5, '01/Jul/1995:00:00:10 -0400'), (1, '01/Jul/1995:00:00:11 -0400'), (1, '01/Jul/1995:00:00:12 -0400'), (10, '01/Jul/1995:00:00:13 -0400'), (8, '01/Jul/1995:00:00:14 -0400'), (1, '01/Jul/1995:00:00:15 -0400'), (10, '01/Jul/1995:00:00:16 -0400')] lst = [x[0] for x in tlst] secs = 5 end = 15 self.assertEqual(pl.LogProcessor().compare_sums(tlst, lst, secs, end), (32, '01/Jul/1995:00:00:05 -0400'))
def test_check_results(self): data = pl.LogProcessor().main(DEFAULT_OUTPUT_PATH, 'fail2ban',TEST_LOG_PATH, 1) self.assertEqual(data[0:17],'199.72.81.55 - - ')
def test_check_and_ban_expired(self): ip = (['01/Jul/1995:00:00:09 -0400', '01/Jul/1995:00:00:19 -0400', '01/Jul/1995:00:00:20 -0400'], 1) time = '01/Jul/1995:00:06:22 -0400' self.assertEqual(pl.LogProcessor().check_and_ban(ip,time), 2)
def test_check_and_ban_already_banned(self): ip = (['01/Jul/1995:00:00:09 -0400', '01/Jul/1995:00:00:19 -0400', '01/Jul/1995:00:00:20 -0400'], 1) time = '01/Jul/1995:00:00:22 -0400' self.assertTrue(pl.LogProcessor().check_and_ban(ip,time))
def test_check_and_ban_less_than_3(self): ip = (['01/Jul/1995:00:00:09 -0400', '01/Jul/1995:00:00:19 -0400'], 0) time = '01/Jul/1995:00:00:09 -0400' self.assertFalse(pl.LogProcessor().check_and_ban(ip,time))
def test_failed_login_not_detected3(self): line = pl.LogProcessor().extract_data('221.139.59.83 - - [01/Jul/1995:00:03:31 -0400] “POST / HTTP/1.0" 401 1420\n') self.assertFalse(pl.LogProcessor().failed_login_detected(line[3]))
def test_failed_login_not_detected(self): line = pl.LogProcessor().extract_data('219.207.85.53 - - [01/Jul/1995:00:03:34 -0400] “GET / HTTP/1.0" 401 7074\n') self.assertFalse(pl.LogProcessor().failed_login_detected(line[3]))
def test_check_results(self): data = pl.LogProcessor().main(DEFAULT_OUTPUT_PATH, 'traffic', TEST_LOG_PATH, 1) self.assertEqual(data, [(8, '01/Jul/1995:00:00:01 -0400')])
def test_extraction_fail(self): test = '- - 00:00:01 -0400 POST\n' # Is the regex extracting data from input self.assertNotEqual( pl.LogProcessor().extract_data(test), ('199.72.81.55', '1420', '01/Jul/1995:00:00:01 -0400'))
def test_calculate_gap(self): self.assertEqual( pl.LogProcessor().calculate_gap('01/Jul/1995:00:00:09 -0400', '03/Jul/1995:00:00:09 -0400'), 172799)
def test_sum_sublist_partial(self): lst = [1, 2, 3, 4, 5, 6, 7, 8, 9] self.assertEqual(pl.LogProcessor().sum_sublist(0, 4, lst), 10)
def test_sum_sublist_full(self): lst = [1, 2, 3, 4] self.assertEqual(pl.LogProcessor().sum_sublist(0, 4, lst), 10)
def test_calculate_gap_small(self): self.assertEqual( pl.LogProcessor().calculate_gap('01/Jul/1995:00:00:09 -0400', '01/Jul/1995:00:00:10 -0400'), 0)
def test_filter_peaks_pass(self): lst = [(1, '01/Jul/1995:20:00:11 -0400'), (1, '01/Jul/1995:00:00:12 -0400')] self.assertEqual(pl.LogProcessor().filter_peaks(lst, 0), (1, '01/Jul/1995:20:00:11 -0400'))
def test_extraction_pass(self): # Is the regex extracting data from input test = '199.72.81.55 - - [01/Jul/1995:00:00:01 -0400] “POST /login HTTP/1.0” 401 1420\n' self.assertEqual(pl.LogProcessor().extract_data(test), ('199.72.81.55', '1420', '01/Jul/1995:00:00:01 -0400', '“POST /login HTTP/1.0” 401'))