Esempio n. 1
0
 def setUp(self):
     self.source_code_parser = fake('SourceCodeParser')
     self.entity_id_generator = fake('EntityIdGenerator')
     self.model = fake('Model')
     self.function_modeler = FunctionModeler(self.source_code_parser,
                                             self.entity_id_generator,
                                             self.model)
Esempio n. 2
0
    def test_it_creates_the_included_modelers(self, _, __):
        # Arrange
        orchestrator = Orchestrator(fake('base_path'), fake('Model'))

        # Act
        orchestrator.include(self.generic_modeler_class)
        orchestrator.process('file_name')

        # Assert
        self.generic_modeler_class.assert_called_once_with(ANY, ANY, ANY)
Esempio n. 3
0
    def test_it_relates_nested_function_calls(self, _):
        # Arrange
        thread_exec_modeler = ThreadModeler(self.entity_id_generator, self.model)

        # Act
        parent_function_call = thread_exec_modeler.on_call(fake('FrameDigest'))
        child_function_call = thread_exec_modeler.on_call(fake('FrameDigest'))

        # Assert
        assert_equal(child_function_call.parent, parent_function_call)
Esempio n. 4
0
    def test_function_notifications_are_suitable_for_the_function_defs_collector(self):
        # Arrange
        function_def_collector = fake('FunctionModeler')
        node = fake('NodeNG')
        defs_visitor = DefsVisitor([function_def_collector])

        # Act
        defs_visitor.visit_function(node)

        # Assert
        function_def_collector.on_function.assert_called_once_with(node)
Esempio n. 5
0
    def test_it_considers_returns_when_relating_nested_function_calls(self, _):
        # Arrange
        thread_exec_modeler = ThreadModeler(self.entity_id_generator, self.model)

        # Act
        thread_exec_modeler.on_call(fake('FrameDigest'))
        child_function_call1 = thread_exec_modeler.on_call(fake('FrameDigest'))
        thread_exec_modeler.on_return(Mock())
        child_function_call2 = thread_exec_modeler.on_call(fake('FrameDigest'))

        # Assert
        assert_equal(child_function_call1.parent, child_function_call2.parent)
Esempio n. 6
0
    def test_it_invokes_the_callback_for_two_entities(self):
        # Given
        callback = Mock()
        fake_class = fake("Class_")
        fake_function = fake("Function")
        async_publisher = AsyncPublisher(callback)
        async_publisher.start()

        # When
        async_publisher.on_entity(fake_class)
        async_publisher.on_entity(fake_function)
        async_publisher.stop()

        # Then
        callback.assert_has_calls([call(fake_class), call(fake_function)])
Esempio n. 7
0
    def test_process_raises_exception_if_all_files_had_been_processed(self,
                                              source_code_parser_class_mock,
                                              _):
        # Arrange
        source_code_parser_mock = fake('SourceCodeParser')
        source_code_parser_mock.add_files.side_effect = [True, False]
        source_code_parser_class_mock.return_value = source_code_parser_mock
        orchestrator = Orchestrator(fake('base_path'), fake('Model'))

        # Act
        orchestrator.include(self.generic_modeler_class)
        orchestrator.process('file_name')

        # Assert
        assert_raises(AlreadyProcessed, orchestrator.process, 'file_name')
Esempio n. 8
0
    def setUp(self):
        self.model = fake('Model')
        self.module_modeler = ModuleModeler(fake('SourceCodeParser'),
                                            fake('EntityIdGenerator'),
                                            self.model)

        self.fake_importer = real('Module')
        self.fake_importer.name = 'importer'

        self.importer_module = Mock()
        self.dependency1 = real('Module')
        self.dependency2 = real('Module')
        self.model.modules.get_by_name.side_effect = lambda name: {
                                                    'importer': self.importer_module,
                                                    'some.module': self.dependency1,
                                                    'some.other.module': self.dependency2}[name]
Esempio n. 9
0
    def test_it_ignores_already_processed_exception_for_static_data(self):
        # Arrange
        self.orchestrator.process.side_effect = AlreadyProcessed

        # Act
        driver = Driver(*self.init_args)
        driver.on_call(fake('FrameDigest'))
Esempio n. 10
0
    def setUp(self):
        self.thread_scoped_tracer_patcher = patch('dissect.modeling.dynamic.driver.ProcessScopedTracer', autospec=True)
        self.thread_scoped_tracer = fake('ProcessScopedTracer')
        self.thread_scoped_tracer_class = self.thread_scoped_tracer_patcher.start()
        self.thread_scoped_tracer_class.return_value = self.thread_scoped_tracer

        self.function_call_patcher = patch('dissect.modeling.dynamic.driver.ProcessModeler', autospec=True)
        self.function_call_modeler = fake('ProcessModeler')
        self.function_call_class = self.function_call_patcher.start()
        self.function_call_class.return_value = self.function_call_modeler

        self.observer = Mock()
        self.orchestrator = fake('Orchestrator')
        self.init_args = (self.observer,
                          fake('EntityIdGenerator'),
                          self.orchestrator)
Esempio n. 11
0
    def setUp(self):
        self.patcher = patch('dissect.modeling.static.driver.Orchestrator')
        self.orchestrator_mock = Mock()
        self.orchestrator_class = self.patcher.start()
        self.orchestrator_class.return_value = self.orchestrator_mock

        self.file_set = fake('FileSet')
        self.observer = Mock()
        self.static_driver = StaticDriver(self.file_set)
Esempio n. 12
0
    def test_it_passes_all_calls_from_the_same_thread_to_the_same_modeler(self,
                                         thread_modeler, threading):
        # Arrange
        process_modeler = Process('fake_entityid_gen',
                                                'fake_model')

        fake_frame_digest_1 = fake('FrameDigest')
        fake_frame_digest_2 = fake('FrameDigest')

        # Act
        threading.current_thread.return_value.name = 'MainThread'
        process_modeler.on_call(fake_frame_digest_1)
        threading.current_thread.return_value.name = 'Thread-1'
        process_modeler.on_call(fake_frame_digest_2)

        # Assert
        thread_modeler.return_value
        assert_equal(thread_modeler.call_count, 2)
Esempio n. 13
0
    def test_registers_dependency_with_other_module_due_to_from_import(self):
        # Arrange: emulate 'from some.module import x'
        fake_from_node = fake('NodeNG', spec_set=False)
        fake_from_node.modname = 'some.module'

        # Act
        self.module_modeler.on_from(self.fake_importer, fake_from_node)

        # Assert
        self.importer_module.depends_on.assert_called_once_with(self.dependency1)
Esempio n. 14
0
    def test_it_passes_all_calls_from_the_same_thread_to_the_same_modeler(self,
                                        thread_modeler, threading):
        # Arrange
        process_modeler = Process('fake_entityid_gen',
                                                'fake_model')

        fake_frame_digest_1 = fake('FrameDigest')
        fake_frame_digest_2 = fake('FrameDigest')
        threading.current_thread.return_value.name = 'MainThread'

        # Act
        process_modeler.on_call(fake_frame_digest_1)
        process_modeler.on_call(fake_frame_digest_2)

        # Assert
        thread_modeler.return_value
        assert_equal(thread_modeler.call_count, 1)
        expected_calls = [call(fake_frame_digest_1), call(fake_frame_digest_2)]
        modeler = thread_modeler.return_value
        modeler.on_call.assert_has_calls(expected_calls)
Esempio n. 15
0
    def test_registers_dependency_with_other_modules_due_to_import(self):
        # Arrange: emulate 'import some.module, some.other.module'
        fake_import_node = fake('NodeNG', spec_set=False)
        fake_import_node.names = [('some.module', None),
                                  ('some.other.module', None)]

        # Act
        self.module_modeler.on_import(self.fake_importer, fake_import_node)

        # Assert
        self.importer_module.depends_on.assert_has_calls([call(self.dependency1), call(self.dependency2)])
Esempio n. 16
0
    def test_on_call_it_ignores_it_if_the_function_is_unknown(self, _):
        # Arrange
        frame_digest = fake('FrameDigest', spec_set=False)
        self.model.functions.get_by_id.side_effect = KeyError
        thread_exec_modeler = ThreadModeler(self.entity_id_generator, self.model)

        # Act
        function_call = thread_exec_modeler.on_call(frame_digest)

        # Assert
        assert_is_none(function_call)
Esempio n. 17
0
    def test_it_invokes_the_callback_for_one_entity(self):
        # Given
        callback = Mock()
        fake_entity = fake("Function")
        async_publisher = AsyncPublisher(callback)
        async_publisher.start()

        # When
        async_publisher.on_entity(fake_entity)
        async_publisher.stop()

        # Then
        callback.assert_called_once_with(fake_entity)
Esempio n. 18
0
    def test_it_processes_static_data_for_each_call(self):
        # Arrange
        frame_digest = fake('FrameDigest')
        frame_digest.function_name = 'fake_function_name'
        frame_digest.file_name = 'fake_file_name'
        frame_digest.line_number = 1

        # Act
        driver = Driver(*self.init_args)
        driver.on_call(frame_digest)

        # Assert
        self.orchestrator.process.assert_called_once_with('fake_file_name')
Esempio n. 19
0
    def test_it_creates_a_thread_modeler_for_the_main_thread(self,
                                                    thread_modeler,
                                                    threading):
        # Arrange
        process_modeler = Process('fake_entityid_gen',
                                                'fake_model')

        # Act
        threading.current_thread.return_value.name = 'MainThread'
        process_modeler.on_call(fake('FrameDigest'))

        # Assert
        thread_modeler.assert_called_once_with('fake_entityid_gen',
                                                         'fake_model')
Esempio n. 20
0
    def test_it_passes_the_frame_digest_to_the_thread_modeler(self,
                                    thread_modeler, threading):
        # Arrange
        process_modeler = Process('fake_entityid_gen',
                                                'fake_model')

        # Act
        fake_frame_digest = fake('FrameDigest')
        threading.current_thread.return_value.name = 'MainThread'
        process_modeler.on_call(fake_frame_digest)

        # Assert
        thread_modeler.return_value.on_call.assert_called_once_with(
            fake_frame_digest)
Esempio n. 21
0
    def test_on_call_notifies_function_call_to_the_observer(self):
        # Arrange
        frame_digest = fake('FrameDigest', spec_set=False)
        expected_function_call = real('FunctionCall')
        self.function_call_modeler.on_call.return_value = expected_function_call

        # Act
        driver = Driver(*self.init_args)
        driver.on_call(frame_digest)

        # Assert
        self.observer.on_call.assert_called_once_with(ANY)
        actual_function_call = self.observer.on_call.call_args[0][0]
        assert_equal(actual_function_call.function, expected_function_call.function)
Esempio n. 22
0
    def test_adds_one_module_to_the_model(self):
        # Arrange
        fake_node = fake('NodeNG', spec_set=False)
        fake_node.file = 'path/to/file.py'
        fake_node.name = 'path.to.file'
        self.entity_id_generator.create.return_value = 'to/file.py'

        # Act
        self.module_modeler.on_module(fake_node)

        # Assert
        expected_module = Module('to/file.py', 'path.to.file')
        self.entity_id_generator.create.assert_called_once_with('path/to/file.py')
        self.model.modules.add.assert_called_once_with(expected_module)
Esempio n. 23
0
    def test_on_entity_returns_before_the_callback_completes(self):
        # Given
        finish = threading.Event()
        callback = Mock()
        callback.side_effect = lambda entity: finish.wait()
        fake_entity = fake("Function")
        async_publisher = AsyncPublisher(callback)
        async_publisher.start()

        # When
        async_publisher.on_entity(fake_entity)
        finish.set()

        # Then
        async_publisher.stop()
    def test_other_function_calls_are_proxied_if_one_exceeds_the_call_rate(
            self):
        # Arrange
        call_filter = CallsPerTimePeriod(1, 1, self.handler)
        another_frame_digest = fake('FrameDigest')
        another_frame_digest.function_name = 'another_function'

        # Act
        proxied_result = call_filter.on_call(self.frame_digest)
        another_result = call_filter.on_call(another_frame_digest)
        discarded_result = call_filter.on_call(self.frame_digest)

        # Assert
        assert_true(proxied_result)
        assert_true(another_result)
        assert_false(discarded_result)
 def setUp(self):
     self.handler = Mock()
     self.frame_digest = fake('FrameDigest')
Esempio n. 26
0
 def test_creation(self):
     fake('Function')
Esempio n. 27
0
 def setUp(self):
     self.entity_id_generator = fake('EntityIdGenerator')
     self.model = fake('Model')