Exemplo n.º 1
0
    def test_initialize_built_in(self):
        setup = Setup()

        setup.initialize_built_in('app')

        expected = {'built-ins': {'app': {'traced': True, 'parameters': {}}}}
        self.assertEqual(setup._setup, expected)
Exemplo n.º 2
0
    def test_remove_parameter(self):
        setup = Setup()
        setup._setup = {
            'app': {
                'func': {
                    'traced': False,
                    'parameters': {
                        1: '%s',
                        4: '%d'
                    }
                }
            }
        }

        setup.remove_parameter('app', 'func', 4)

        expected = {
            'app': {
                'func': {
                    'traced': False,
                    'parameters': {
                        1: '%s'
                    }
                }
            }
        }
        self.assertEqual(setup._setup, expected)
Exemplo n.º 3
0
    def test_add_parameter(self):
        setup = Setup()
        setup._setup = dummy_setup()

        setup.add_parameter('app1', 'func3', 2, '%s')

        expected = {
            'app1': {
                'func1': {
                    'traced': False,
                    'parameters': {},
                    'mangled': 'mangledfunc1'
                },
                'func3': {
                    'traced': False,
                    'parameters': {
                        2: '%s'
                    },
                    'mangled': 'mangledfunc3'
                }
            },
            'app2': {
                'func2': {
                    'traced': False,
                    'parameters': {},
                    'mangled': 'mangledfunc2'
                },
            }
        }
        self.assertEqual(setup._setup, expected)
Exemplo n.º 4
0
    def test_initialize_binary(self, nm):
        setup = Setup()

        setup.initialize_binary('app')

        expected = {
            'app': {
                'func1': {
                    'mangled': 'func1',
                    'traced': False,
                    'parameters': {}
                },
                'func2': {
                    'mangled': 'func2',
                    'traced': False,
                    'parameters': {}
                },
                'func3': {
                    'mangled': 'func3',
                    'traced': False,
                    'parameters': {}
                }
            }
        }
        self.assertEqual(setup._setup, expected)
Exemplo n.º 5
0
    def test_get_parameters(self):
        setup = Setup()
        setup._setup = {'app': {'func': {'traced': False, 'parameters': {1: '%s', 4: '%d'}, 'offset': 0}}}

        result = setup.get_parameters('app', 'func')

        self.assertEqual(result, {1: '%s', 4: '%d'})
Exemplo n.º 6
0
    def test_get_apps(self):
        setup = Setup()
        setup._setup = dummy_setup()

        result = setup.get_apps()

        self.assertEqual(result, ['app1', 'app2'])
Exemplo n.º 7
0
    def test_setup_function_to_trace_with_mangled_name(self):
        setup = Setup()
        setup._setup = dummy_setup()

        setup.setup_function_to_trace('app1', 'mangledfunc1')

        expected = {
            'app1': {
                'func1': {
                    'traced': True,
                    'parameters': {},
                    'mangled': 'mangledfunc1'
                },
                'func3': {
                    'traced': False,
                    'parameters': {},
                    'mangled': 'mangledfunc3'
                }
            },
            'app2': {
                'func2': {
                    'traced': False,
                    'parameters': {},
                    'mangled': 'mangledfunc2'
                },
            }
        }
        self.assertEqual(setup._setup, expected)
Exemplo n.º 8
0
    def test_load_from_file_raises_exception_if_file_not_found(self, read):
        def side_effect():
            raise FileNotFoundError

        read.side_effect = side_effect
        setup = Setup()
        with self.assertRaises(ConfigFileError):
            setup.load_from_file('/dummy/path')
Exemplo n.º 9
0
    def test_load_from_file_raises_exception_if_file_is_directory(self, read):
        def side_effect():
            raise IsADirectoryError

        read.side_effect = side_effect
        setup = Setup()
        with self.assertRaises(ConfigFileError) as ctx:
            setup.load_from_file('/dummy/path')
Exemplo n.º 10
0
    def test_load_from_file_raises_exception_on_yaml_scan_error(self, read, load):
        def side_effect(content):
            raise ScannerError

        load.side_effect = side_effect
        setup = Setup()
        with self.assertRaises(ConfigFileError) as ctx:
            setup.load_from_file('/dummy/path')
Exemplo n.º 11
0
    def test_get_setup_of_app(self):
        setup = Setup()
        setup._setup = dummy_setup()

        result = setup.get_setup_of_app('app1')

        expected = {
            'func1': {'traced': False, 'parameters': {}, 'mangled': 'mangledfunc1', 'offset': 0},
            'func3': {'traced': False, 'parameters': {}, 'mangled': 'mangledfunc3', 'offset': 0}
        }
        self.assertEqual(result, expected)
Exemplo n.º 12
0
    def test_remove_app(self):
        setup = Setup()
        setup._setup = dummy_setup()

        setup.remove_app('app2')

        expected = {
            'app1': {
                'func1': {'traced': False, 'parameters': {}, 'mangled': 'mangledfunc1', 'offset': 0},
                'func3': {'traced': False, 'parameters': {}, 'mangled': 'mangledfunc3', 'offset': 0}
            }
        }
        self.assertEqual(setup._setup, expected)
Exemplo n.º 13
0
def initialize(app):
    call_graph = CallGraph()
    trace_controller = TraceController()
    setup = Setup()
    app.layout = Layout()
    app.title = 'Tracerface'
    _setup_callbacks(app, call_graph, setup, trace_controller)
Exemplo n.º 14
0
    def test_setup_function_to_trace_with_demangled_name(self, get_offset_for_function):
        setup = Setup()
        setup._setup = dummy_setup()

        setup.setup_function_to_trace('app1', 'func1')

        expected = {
            'app1': {
                'func1': {'traced': True, 'parameters': {}, 'mangled': 'mangledfunc1', 'offset': 8},
                'func3': {'traced': False, 'parameters': {}, 'mangled': 'mangledfunc3', 'offset': 0}
            },
            'app2': {
                'func2': {'traced': False, 'parameters': {}, 'mangled': 'mangledfunc2', 'offset': 0}
            }
        }
        self.assertEqual(setup._setup, expected)
Exemplo n.º 15
0
 def test_generate_bcc_args_with_multiple_values(self):
     setup = Setup()
     setup._setup = {
         'app1': {
             'func1': {
                 'mangled': 'mangled1',
                 'traced': True,
                 'parameters': {
                     4: '%s',
                     2: '%d'
                 }
             },
             'func2': {
                 'mangled': 'mangled2',
                 'traced': False,
                 'parameters': {}
             }
         },
         'app2': {
             'func3': {
                 'mangled': 'mangled3',
                 'traced': True,
                 'parameters': {
                     1: '%s',
                     5: '%d'
                 }
             },
             'func4': {
                 'mangled': 'mangled4',
                 'traced': True,
                 'parameters': {
                     1: '%s',
                     2: '%f'
                 }
             }
         }
     }
     result = setup.generate_bcc_args()
     expected = [
         'app1:mangled1 "%s %d", arg4, arg2',
         'app2:mangled3 "%s %d", arg1, arg5',
         'app2:mangled4 "%s %f", arg1, arg2',
     ]
     self.assertEqual(result, expected)
Exemplo n.º 16
0
    def test_remove_function_from_trace_with_mangled_name(self):
        setup = Setup()
        setup._setup = dummy_setup()

        setup.setup_function_to_trace('app1', 'func1')
        setup.remove_function_from_trace('app1', 'mangledfunc1')

        self.assertEqual(setup._setup, dummy_setup())
Exemplo n.º 17
0
    def test_remove_function_from_trace_with_mangled_name(self, get_offset_for_function):
        setup = Setup()
        setup._setup = dummy_setup()

        setup.setup_function_to_trace('app1', 'func1')
        setup.remove_function_from_trace('app1', 'mangledfunc1')

        expected_setup = dummy_setup()
        expected_setup['app1']['func1']['offset'] = 8

        self.assertEqual(setup._setup, expected_setup)
Exemplo n.º 18
0
def test_trace(tmp_path):
    call_graph = CallGraph()
    setup = Setup()
    trace_controller = TraceController()

    with create_test_binary(tmp_path) as app:
        setup.initialize_binary(app)
        setup.setup_function_to_trace(app, 'func1')
        setup.setup_function_to_trace(app, 'func2')
        setup.setup_function_to_trace(app, 'func3')
        setup.add_parameter(app, 'func1', '1', '%s')
        setup.add_parameter(app, 'func1', '2', '%s')
        setup.add_parameter(app, 'func2', '1', '%d')

        trace_controller.start_trace(setup.generate_bcc_args(),
                                     call_graph)  # start monitoring
        time.sleep(5)  # BCC trace needs a bit of time to setup
        subprocess.run(app)  # run monitored application
        trace_controller.stop_trace()  # stop

    edges = convert_edges_to_cytoscape_format(call_graph.get_nodes(),
                                              call_graph.get_edges())
    nodes = convert_nodes_to_cytoscape_format(call_graph.get_nodes(),
                                              call_graph.get_edges())
    assert_results(nodes, edges)
Exemplo n.º 19
0
 def test_init_binary_raises_error_if_binary_not_exists(self):
     setup = Setup()
     with self.assertRaises(BinaryNotExistsError):
         setup.initialize_binary('non_existent_app')
Exemplo n.º 20
0
 def test_init_binary_raises_error_if_binary_already_added(self):
     setup = Setup()
     setup._setup = dummy_setup()
     with self.assertRaises(BinaryAlreadyAddedError):
         setup.initialize_binary('app1')
Exemplo n.º 21
0
 def test_load_from_file_adds_built_in_if_binary_not_found(
         self, read, load):
     setup = Setup()
     setup.load_from_file('dummy/path')
     self.assertIn('built-ins', setup._setup)
     self.assertIn('dummy_built_in', setup._setup['built-ins'])
Exemplo n.º 22
0
 def test_get_offset_for_built_in_no_match(self, check_output):
     setup = Setup()
     result = setup.get_offset_for_built_in('builtin_func')
     expected = 0
     self.assertEqual(result, expected)
Exemplo n.º 23
0
 def test_get_offset_for_built_in_non_existing(self):
     setup = Setup()
     with self.assertRaises(BuiltInNotExistsError):
         setup.get_offset_for_built_in('no_such_built_in')
Exemplo n.º 24
0
 def test_get_offset_for_function_hexadecimal(self, check_output):
     setup = Setup()
     result = setup.get_offset_for_function('app', 'func1')
     expected = 26
     self.assertEqual(result, expected)
Exemplo n.º 25
0
 def test_get_offset_for_function_extra_lines(self, check_output):
     setup = Setup()
     result = setup.get_offset_for_function('app', 'func2')
     expected = 8
     self.assertEqual(result, expected)
Exemplo n.º 26
0
 def test_get_offset_for_function_no_match(self, check_output):
     setup = Setup()
     result = setup.get_offset_for_function('app', 'func')
     expected = 0
     self.assertEqual(result, expected)
Exemplo n.º 27
0
 def test_remove_function_from_trace_raises_error_if_function_not_exists(
         self):
     setup = Setup()
     setup._setup = dummy_setup()
     with self.assertRaises(FunctionNotInBinaryError):
         setup.remove_function_from_trace('app1', 'func4')