def test_04_check_pid(self): lf = LockFile(self.LOCKPATH) pid = lf.get_pid() if sys.version_info <= (3, 0): # py2.x coverage regex_match = re.match(r'[0-9]+$', pid) self.assertIsNotNone(regex_match) else: self.assertRegex(pid, r'^[0-9]+$')
def setUp(self): self.directory = ospj(dirname(__file__), 'data/deny_hosts') self.work_dir = ospj(self.directory, 'work') self.logfile = ospj(self.work_dir, 'logfile') self.prefs = Prefs() self.lock_file = LockFile(ospj(self.directory, 'lockfile')) self.lock_file.remove(die_=False) self.lock_file.create() self.prefs._Prefs__data['ETC_DIR'] = ospj(self.directory, 'etc') self.prefs._Prefs__data['WORK_DIR'] = self.work_dir
def setup_denyhosts(self): self.prefs = Prefs() self.lock_file = LockFile(ospj(self.DIRECTORY, 'lockfile')) self.lock_file.remove(die_=False) self.lock_file.create() self.prefs._Prefs__data['ETC_DIR'] = ospj(self.DIRECTORY, 'etc') self.prefs._Prefs__data['WORK_DIR'] = self.WORK_DIR self.prefs._Prefs__data['HOSTS_DENY'] = ospj(self.prefs._Prefs__data['ETC_DIR'], 'hosts.deny') self.prefs._Prefs__data['DENY_THRESHOLD_INVALID'] = 5 self.prefs._Prefs__data['DENY_THRESHOLD_VALID'] = 5 self.prefs._Prefs__data['DENY_THRESHOLD_ROOT'] = 0 self.prefs._Prefs__data['DENY_THRESHOLD_RESTRICTED'] = 5
class DenyHostsBasicTest(unittest.TestCase): def setUp(self): self.directory = ospj(dirname(__file__), 'data/deny_hosts') self.work_dir = ospj(self.directory, 'work') self.logfile = ospj(self.work_dir, 'logfile') self.prefs = Prefs() self.lock_file = LockFile(ospj(self.directory, 'lockfile')) self.lock_file.remove(die_=False) self.lock_file.create() self.prefs._Prefs__data['ETC_DIR'] = ospj(self.directory, 'etc') self.prefs._Prefs__data['WORK_DIR'] = self.work_dir def test_init(self): DenyHosts(self.logfile, self.prefs, self.lock_file) def tearDown(self): self.lock_file.remove()
def test_09_create_remove(self): lf = LockFile(self.LOCKPATH) self.assertIsNone(lf.create()) self.assertIsNone(lf.remove())
def test_08_get_pid_empty(self): lf = LockFile(self.LOCKPATH) self.assertEqual(lf.get_pid(), '')
def test_07_check_lockfile_removal_not_exists(self): lf = LockFile(self.LOCKPATH) with self.assertRaises(SystemExit) as cm: lf.remove() self.assertEqual(cm.exception, 'Error')
def test_06_check_lockfile_removal(self): lf = LockFile(self.LOCKPATH) self.assertIsNone(lf.remove())
def test_05_check_lockfile_create_exists(self): lf = LockFile(self.LOCKPATH) with self.assertRaises(SystemExit) as cm: lf.create() self.assertEqual(cm.exception, 'Error')
def test_02_check_lockfile_create(self): lf = LockFile(self.LOCKPATH) self.assertIsNone(lf.create()) close(lf.fd)
def test_01_check_lockfile_not_exists(self): lf = LockFile(self.LOCKPATH) self.assertFalse(lf.exists())
class FileTrackerTest(unittest.TestCase): def setUp(self): self.DIRECTORY = ospj(dirname(__file__), 'data/filetracker') self.WORK_DIR = ospj(self.DIRECTORY, 'work') self.LOG_FILE = ospj(self.DIRECTORY, 'logfile') self.SECURE_LOG_FILE = SECURE_LOG_OFFSET def test_01_check_offset(self): ft = FileTracker(self.WORK_DIR, self.LOG_FILE) self.assertEqual(ft.get_offset(), 70) def test_02_update_offset(self): ft = FileTracker(self.WORK_DIR, self.LOG_FILE) self.assertIsNone(ft.save_offset(143)) def write_new_entry(self): with open(ospj(self.WORK_DIR, self.LOG_FILE), 'a') as fh: fh.write('\nMar 11 07:39:50 bastion sshd[29111]: Set /proc/self/oom_score_adj to 0') def test_03_validate_offset_after_new_entry(self): self.write_new_entry() ft = FileTracker(self.WORK_DIR, self.LOG_FILE) self.assertEqual(ft.get_offset(), 143) def write_new_file(self): with open(ospj(self.WORK_DIR, self.LOG_FILE), 'w') as fh: fh.write('Mar 12 07:39:50 bastion sshd[29111]: Set /proc/self/oom_score_adj to 0') def setup_denyhosts(self): self.prefs = Prefs() self.lock_file = LockFile(ospj(self.DIRECTORY, 'lockfile')) self.lock_file.remove(die_=False) self.lock_file.create() self.prefs._Prefs__data['ETC_DIR'] = ospj(self.DIRECTORY, 'etc') self.prefs._Prefs__data['WORK_DIR'] = self.WORK_DIR self.prefs._Prefs__data['HOSTS_DENY'] = ospj(self.prefs._Prefs__data['ETC_DIR'], 'hosts.deny') self.prefs._Prefs__data['DENY_THRESHOLD_INVALID'] = 5 self.prefs._Prefs__data['DENY_THRESHOLD_VALID'] = 5 self.prefs._Prefs__data['DENY_THRESHOLD_ROOT'] = 0 self.prefs._Prefs__data['DENY_THRESHOLD_RESTRICTED'] = 5 def test_04_file_rotated(self): ft = FileTracker(self.WORK_DIR, self.LOG_FILE) self.write_new_file() self.assertIsNone(ft.update_first_line()) self.assertEqual(ft.get_offset(), 0) self.setup_denyhosts() new_offset = DenyHosts(self.LOG_FILE, self.prefs, self.lock_file).process_log(self.LOG_FILE, ft.get_offset()) self.assertIsNone(ft.save_offset(new_offset)) self.assertEqual(ft.get_offset(), new_offset) self.lock_file.remove() def clear_offset_file(self): with open(ospj(self.WORK_DIR, self.SECURE_LOG_FILE), 'w') as fh: fh.write('') def test_05_bug_99(self): self.clear_offset_file() ft = FileTracker(self.WORK_DIR, self.LOG_FILE) self.assertEqual(ft.get_offset(), 0)
first_time = 1 except Exception, e: if e[0] != 17: print e sys.exit(1) setup_logging(prefs, enable_debug, verbose, daemon) if not logfiles or daemon: logfiles = [prefs.get('SECURE_LOG')] elif len(logfiles) > 1: ignore_offset = 1 if not prefs.get('ADMIN_EMAIL'): noemail = 1 lock_file = LockFile(prefs.get('LOCK_FILE')) lock_file.create() if upgrade099 and not daemon: if not prefs.get('PURGE_DENY'): lock_file.remove() die("You have supplied the --upgrade099 flag, however you have not set PURGE_DENY in your configuration file") else: u = UpgradeTo099(prefs.get("HOSTS_DENY")) if migrate and not daemon: if not prefs.get('PURGE_DENY'): lock_file.remove() die("You have supplied the --migrate flag however you have not set PURGE_DENY in your configuration file.") else:
def test_03_check_lockfile_exists(self): lf = LockFile(self.LOCKPATH) self.assertTrue(lf.exists())