Skip to content

Commit

Permalink
FEATURE: Add "add_action" method for analyzers
Browse files Browse the repository at this point in the history
This is to dynamically add actions to analyzers
  • Loading branch information
d-Rickyy-b committed Jan 13, 2019
1 parent ae730f0 commit 4b5df12
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 8 deletions.
8 changes: 8 additions & 0 deletions pastepwn/analyzers/basicanalyzer.py
Expand Up @@ -19,6 +19,14 @@ def __init__(self, actions, identifier=None):
self.actions = [actions]
self.identifier = identifier or self.name

def add_action(self, action):
"""
Adds a new action to the already present actions
:param action: New action to add to the present actions
:return: None
"""
self.actions.append(action)

def match(self, paste):
"""
Checks if a certain paste is matched by the conditions set for this analyzer
Expand Down
26 changes: 18 additions & 8 deletions pastepwn/analyzers/tests/basicanalyzer_test.py
Expand Up @@ -8,12 +8,22 @@
class TestBasicAnalyzer(unittest.TestCase):
def setUp(self):
self.analyzer = BasicAnalyzer(None)
self.obj = mock.Mock()
self.mock_action = mock.Mock()

def test_add_action(self):
"""Check if it's possible to add actions to an analyzer"""
mock_action2 = mock.Mock
analyzer = BasicAnalyzer(self.mock_action)
self.assertEqual([self.mock_action], analyzer.actions)

analyzer.add_action(mock_action2)
self.assertTrue(isinstance(analyzer.actions, list))
self.assertEqual([self.mock_action, mock_action2], analyzer.actions)

def test_match(self):
"""Check if an exception is raised when trying to call the match function"""
self.obj.body = "Test"
self.assertRaises(NotImplementedError, self.analyzer.match, self.obj)
self.mock_action.body = "Test"
self.assertRaises(NotImplementedError, self.analyzer.match, self.mock_action)

def test_initialization(self):
"""Check if the initialization of the BasicAnalyzer works as intended"""
Expand All @@ -26,14 +36,14 @@ def test_empty_initialization(self):
self.assertEqual([], analyzer.actions)

def test_single_initialization(self):
analyzer = BasicAnalyzer(self.obj)
self.assertEqual([self.obj], analyzer.actions)
analyzer = BasicAnalyzer(self.mock_action)
self.assertEqual([self.mock_action], analyzer.actions)

def test_multi_initialization(self):
obj2 = mock.Mock()
actions = [self.obj, obj2]
mock_action2 = mock.Mock()
actions = [self.mock_action, mock_action2]
analyzer = BasicAnalyzer(actions)
self.assertEqual([self.obj, obj2], analyzer.actions)
self.assertEqual([self.mock_action, mock_action2], analyzer.actions)


if __name__ == '__main__':
Expand Down

0 comments on commit 4b5df12

Please sign in to comment.