Exemplo n.º 1
0
    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)
Exemplo n.º 2
0
    def test_add_action(self):
        """Check if it's possible to add actions to an analyzer"""
        mock_action2 = mock.MagicMock(spec=BasicAction)
        analyzer = BasicAnalyzer(self.mock_action)
        self.assertEqual([self.mock_action], analyzer.actions)

        analyzer.add_action(mock_action2)
        self.assertIsInstance(analyzer.actions, list)
        self.assertEqual([self.mock_action, mock_action2], analyzer.actions)
Exemplo n.º 3
0
    def test_logical_operators(self):
        """Check if using logical operators on analyzers works as intended"""
        analyzer = self.analyzer & self.analyzer
        self.assertIsInstance(analyzer, BasicAnalyzer)

        mock_action1 = mock.MagicMock(spec=BasicAction)
        mock_action2 = mock.MagicMock(spec=BasicAction)
        analyzer1 = BasicAnalyzer(mock_action1)
        analyzer2 = BasicAnalyzer(mock_action2)

        and_analyzer = analyzer1 & analyzer2
        self.assertEqual(2, len(and_analyzer.actions))
        self.assertIn(mock_action1, and_analyzer.actions)
        self.assertIn(mock_action2, and_analyzer.actions)
        self.assertEqual("(BasicAnalyzer && BasicAnalyzer)", and_analyzer.identifier)
Exemplo n.º 4
0
 def test_init_action_class(self):
     """Check if an exception is being raised when a passed action is a reference to a class"""
     # Test created based on #175
     # If users pass a class instead of an instance, they should receive an exception
     with self.assertRaises(
             InvalidActionError,
             msg="BasicAnalyzer accepts classes as actions!"):
         _ = BasicAnalyzer(mock.Mock)
Exemplo n.º 5
0
 def test_unique(self):
     """Check if running unique() on a list of matches returns a list with no duplicates."""
     # Some lists with and without duplicates
     test_lists = [
         [],
         ["a", "a"],
         ["a", "b"],
         ["a", "b", "a"]
         ]
     self.assertEqual(BasicAnalyzer.unique(test_lists[0]), [],
                      msg="BasicAnalyzer.unique() left a duplicate!")
     self.assertEqual(BasicAnalyzer.unique(test_lists[1]), ["a"],
                      msg="BasicAnalyzer.unique() left a duplicate!")
     # Should preserve order
     self.assertEqual(BasicAnalyzer.unique(test_lists[2]), ["a", "b"],
                      msg="BasicAnalyzer.unique() left a duplicate!")
     self.assertEqual(BasicAnalyzer.unique(test_lists[3]), ["a", "b"],
                      msg="BasicAnalyzer.unique() left a duplicate!")
Exemplo n.º 6
0
    def test_error_logging_init_class(self):
        """Check if an error is being logged when a passed action is a reference to a class"""
        self.logger = logging.getLogger("pastepwn.analyzers.basicanalyzer")
        with self.assertLogs(self.logger, level="ERROR") as log:
            try:
                _ = BasicAnalyzer(mock.Mock)
            except Exception:
                pass

            self.assertEqual(log.output, ["ERROR:pastepwn.analyzers.basicanalyzer:You passed a class as action for 'BasicAnalyzer' but an instance of an action was expected!"])
Exemplo n.º 7
0
    def test_add_action_negative(self):
        """Check if an exception raises when passing crap as action"""
        analyzer = BasicAnalyzer(self.mock_action)
        self.assertEqual([self.mock_action], analyzer.actions)

        with self.assertRaises(InvalidActionError, msg="BasicAnalyzer accepts objects other than BasicAnalyzers as actions in add_action!"):
            # The mock_action2 is not a subclass of BasicAction and hence it must throw an error
            mock_action2 = mock.Mock()
            analyzer.add_action(mock_action2)

        with self.assertRaises(InvalidActionError, msg="BasicAnalyzer accepts classes as actions in add_action!"):
            # The mock_action3 is not even an object (only a class reference)
            mock_action3 = mock.Mock
            analyzer.add_action(mock_action3)
Exemplo n.º 8
0
 def test_single_initialization(self):
     analyzer = BasicAnalyzer(self.mock_action)
     self.assertEqual([self.mock_action], analyzer.actions)
Exemplo n.º 9
0
 def test_empty_initialization(self):
     analyzer = BasicAnalyzer(None)
     self.assertEqual([], analyzer.actions)
Exemplo n.º 10
0
 def test_initialization(self):
     """Check if the initialization of the BasicAnalyzer works as intended"""
     actions = mock.Mock()
     analyzer = BasicAnalyzer(actions)
     self.assertEqual([actions], analyzer.actions)
Exemplo n.º 11
0
 def test_single_initialization(self):
     """Check if initializing an analyzer with a single action is possible"""
     analyzer = BasicAnalyzer(self.mock_action)
     self.assertEqual([self.mock_action], analyzer.actions)
Exemplo n.º 12
0
 def test_empty_initialization(self):
     """Check if initializing an analyzer without actions is possible"""
     analyzer = BasicAnalyzer(None)
     self.assertEqual([], analyzer.actions)
Exemplo n.º 13
0
 def test_initialization(self):
     """Check if the initialization of the BasicAnalyzer works as intended"""
     analyzer = BasicAnalyzer(self.mock_action)
     self.assertEqual([self.mock_action], analyzer.actions)
Exemplo n.º 14
0
 def setUp(self):
     self.analyzer = BasicAnalyzer(None)
     self.mock_action = mock.MagicMock(spec=BasicAction)
Exemplo n.º 15
0
 def test_multi_initialization(self):
     mock_action2 = mock.Mock()
     actions = [self.mock_action, mock_action2]
     analyzer = BasicAnalyzer(actions)
     self.assertEqual([self.mock_action, mock_action2], analyzer.actions)
Exemplo n.º 16
0
 def setUp(self):
     self.analyzer = BasicAnalyzer(None)
     self.mock_action = mock.Mock()
Exemplo n.º 17
0
 def test_multi_initialization(self):
     """Check if initializing an analyzer with multiple actions is possible"""
     mock_action2 = mock.MagicMock(spec=BasicAction)
     actions = [self.mock_action, mock_action2]
     analyzer = BasicAnalyzer(actions)
     self.assertEqual([self.mock_action, mock_action2], analyzer.actions)