def test_exec_parsing(self): """ Perform more extensive tests on exec. """ action_tests = [ # pipe ( Database.parse_action_string('exec echo hi | tee output.txt'), { 'action_type': ACTION_TYPES.get('exec', 1), 'command': 'echo', 'args': ['hi', '|', 'tee', 'output.txt'] } ), # conflicting args ( Database.parse_action_string('exec date --time time'), { 'action_type': ACTION_TYPES.get('exec', 1), 'command': 'date', 'args': ['--time', 'time'] } ), # subshell ( Database.parse_action_string('exec find $(which bash)'), { 'action_type': ACTION_TYPES.get('exec', 1), 'command': 'find', 'args': ['$(which', 'bash)'] } ), # backtick subshell ( Database.parse_action_string('exec rm -rf `which bash`'), { 'action_type': ACTION_TYPES.get('exec', 1), 'command': 'rm', 'args': ['-rf', '`which', 'bash`'] } ), # special chars (must be quoted) ( Database.parse_action_string('exec echo -e "Hello \n World"'), { 'action_type': ACTION_TYPES.get('exec', 1), 'command': 'echo', 'args': ['-e', 'Hello \n World'] } ), ] for test in action_tests: self.assertDictEqual(test[0], test[1])
def test_action_parse_basic_format(self): """ This function tests the Action model's parser, ensuring that commands parse into the proper dictionaries. """ test_time = time.time()+120 final_config = { 'interval': 300, 'interval_delta': 20, 'servers': ['10.80.100.10', 'https://bobzinga.com'], } action_tests = [ # config ( Database.parse_action_string('config {} {} {}'.format( '--interval 300', '--delta 20', '--servers 10.80.100.10 https://bobzinga.com')), { 'action_type': ACTION_TYPES.get('config', 0), 'config': final_config } ), # exec ( Database.parse_action_string('exec ls'), { 'action_type': ACTION_TYPES.get('exec', 1), 'command': 'ls', 'args': [] } ), # exec with args ( Database.parse_action_string('exec ls -al'), { 'action_type': ACTION_TYPES.get('exec', 1), 'command': 'ls', 'args': ['-al'] } ), # timed exec ( Database.parse_action_string('exec --time={} ls -al'.format(test_time)), { 'action_type': ACTION_TYPES.get('timed_exec', 2), 'command': 'ls', 'args': ['-al'], 'start_time': test_time } ), # spawn ( Database.parse_action_string('exec --spawn ls -al'), { 'action_type': ACTION_TYPES.get('spawn', 3), 'command': 'ls', 'args': ['-al'], } ), # timed spawn ( Database.parse_action_string('exec --time={} --spawn ls -al'.format(test_time)), { 'action_type': ACTION_TYPES.get('timed_spawn', 4), 'command': 'ls', 'args': ['-al'], 'start_time': test_time } ), # timed spawn (swapped args) ( Database.parse_action_string('exec --spawn --time={} ls -al'.format(test_time)), { 'action_type': ACTION_TYPES.get('timed_spawn', 4), 'command': 'ls', 'args': ['-al'], 'start_time': test_time } ), # upload ( Database.parse_action_string('upload files/sshd_config /etc/ssh/sshd_config'), { 'action_type': ACTION_TYPES.get('upload', 5), 'remote_path': '/etc/ssh/sshd_config', 'teamserver_path': 'files/sshd_config' } ), # download ( Database.parse_action_string('download /etc/passwd files/passwd'), { 'action_type': ACTION_TYPES.get('download', 6), 'remote_path': '/etc/passwd', 'teamserver_path': 'files/passwd' } ), # gather default ( Database.parse_action_string('gather'), { 'action_type': ACTION_TYPES.get('gather', 7), 'subset': DEFAULT_SUBSET } ), # gather min ( Database.parse_action_string('gather -s min'), { 'action_type': ACTION_TYPES.get('gather', 7), 'subset': 'min' } ), ] for test in action_tests: self.assertDictEqual(test[0], test[1])