From 11aa8c1658ddadcdab04c406a070c26fe1ede502 Mon Sep 17 00:00:00 2001 From: doronz Date: Mon, 11 Sep 2023 14:30:22 +0300 Subject: [PATCH] tests: make `pytest` succeed on macos --- hilda/hilda_client.py | 24 ++--------- hilda/launch_lldb.py | 45 +++++++++++--------- hilda/objective_c_symbol.py | 8 ++-- tests/conftest.py | 10 +---- tests/hilda_tests.py | 12 ++---- tests/test_hilda_client/test_hilda_client.py | 4 +- tests/test_symbols/test_objective_c_class.py | 11 ----- tests/test_symbols/test_symbols_jar.py | 1 - 8 files changed, 41 insertions(+), 74 deletions(-) diff --git a/hilda/hilda_client.py b/hilda/hilda_client.py index d14a732..80e0380 100644 --- a/hilda/hilda_client.py +++ b/hilda/hilda_client.py @@ -1050,34 +1050,18 @@ def interactive(self, additional_namespace: typing.Mapping = None): IPython.start_ipython(config=c, user_ns=namespace) - @staticmethod - def is_objc_type(symbol: Symbol) -> bool: + def is_objc_type(self, symbol: Symbol) -> bool: """ Test if a given symbol represents an objc object :param symbol: :return: """ - # Tagged pointers are ObjC objects - if symbol & OBJC_TAG_MASK == OBJC_TAG_MASK: - return True - - # Class are not ObjC objects - for mask, value in ISA_MAGICS: - if symbol & mask == value: - return False - try: - with symbol.change_item_size(8): - isa = symbol[0] - except HildaException: + self.symbols.CFGetTypeID(symbol) + return True + except EvaluatingExpressionError: return False - for mask, value in ISA_MAGICS: - if isa & mask == value: - return True - - return False - @staticmethod def _add_global(name, value, reserved_names=None): if reserved_names is None or name not in reserved_names: diff --git a/hilda/launch_lldb.py b/hilda/launch_lldb.py index 7d042bc..c7b7084 100644 --- a/hilda/launch_lldb.py +++ b/hilda/launch_lldb.py @@ -2,6 +2,7 @@ import os import time from pathlib import Path +from typing import Optional import click import coloredlogs @@ -32,7 +33,7 @@ def cli(): pass -def start_remote(debug_port: int, ssh_port: int, process: str, hostname: str, rc_script: str): +def start_remote(debug_port: int, ssh_port: int, process: str, hostname: str, rc_script: str) -> None: tunnel_local_port(debug_port) assert not execute( f'ssh -i ~/.ssh/id_rsa -p {ssh_port} root@{hostname} "debugserver 0.0.0.0:{debug_port} --attach={process}"&') @@ -47,6 +48,25 @@ def start_remote(debug_port: int, ssh_port: int, process: str, hostname: str, rc execute(f'lldb --one-line "{commands}"') +def attach(name: Optional[str] = None, pid: Optional[int] = None, rc_script: Optional[str] = None) -> None: + """ Attach to given process and start an lldb shell """ + commands = [] + if name is not None: + commands.append(f'process attach -n {name}') + elif pid is not None: + commands.append(f'process attach -p {pid}') + else: + print('missing either process name or pid for attaching') + return + + commands.append(f'command script import {os.path.join(Path(__file__).resolve().parent, "lldb_entrypoint.py")}') + if rc_script is not None: + commands.append(f'command script import {rc_script}') + commands = '\n'.join(commands) + + execute(f'lldb --one-line "{commands}"') + + @cli.command('remote') @click.argument('process') @click.argument('ssh_port', type=click.INT) @@ -54,15 +74,14 @@ def start_remote(debug_port: int, ssh_port: int, process: str, hostname: str, rc @click.option('--hostname', default='localhost') def remote(process, ssh_port, debug_port, hostname): """ Start debugserver at remote device and connect using lldb """ - start_remote(debug_port, ssh_port, process, hostname, - os.path.join(Path(__file__).resolve().parent, "lldb_entrypoint.py")) + start_remote(debug_port, ssh_port, process, hostname, Path(__file__).resolve().parent / 'lldb_entrypoint.py') @cli.command('bare') -def bare(): +def cli_bare(): """ Just start an lldb shell """ # connect local LLDB client - commands = [f'command script import {os.path.join(Path(__file__).resolve().parent, "lldb_entrypoint.py")}'] + commands = [f'command script import {Path(__file__).resolve().parent / "lldb_entrypoint.py"}'] commands = '\n'.join(commands) execute(f'lldb --one-line "{commands}"') @@ -70,21 +89,9 @@ def bare(): @cli.command('attach') @click.option('-n', '--name', help='process name to attach') @click.option('-p', '--pid', type=click.INT, help='pid to attach') -def attach(name: str, pid: int): +def cli_attach(name: str, pid: int): """ Attach to given process and start an lldb shell """ - # connect local LLDB client - commands = [] - if name is not None: - commands.append(f'process attach -n {name}') - elif pid is not None: - commands.append(f'process attach -p {pid}') - else: - print('missing either process name or pid for attaching') - return - - commands.append(f'command script import {os.path.join(Path(__file__).resolve().parent, "lldb_entrypoint.py")}') - commands = '\n'.join(commands) - execute(f'lldb --one-line "{commands}"') + attach(name=name, pid=pid) if __name__ == '__main__': diff --git a/hilda/objective_c_symbol.py b/hilda/objective_c_symbol.py index b01ec4d..aa4ae82 100644 --- a/hilda/objective_c_symbol.py +++ b/hilda/objective_c_symbol.py @@ -45,10 +45,10 @@ def create(cls, value: int, client): :rtype: ObjectiveCSymbol """ symbol = super(ObjectiveCSymbol, cls).create(value, client) - symbol.ivars = [] - symbol.properties = [] - symbol.methods = [] - symbol.class_ = None # type: Class + symbol.ivars = [] # type: List[Ivar] + symbol.properties = [] # type: List[Property] + symbol.methods = [] # type: List[Method] + symbol.class_ = None # type: Optional[Class] symbol.reload() return symbol diff --git a/tests/conftest.py b/tests/conftest.py index ceab04e..0a667fe 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -7,15 +7,7 @@ @pytest.fixture(scope='function') def hilda_client(lldb_debugger): client = HildaClient(lldb_debugger) - client.init_dynamic_environment() lldb.hilda_client = client + client.init_dynamic_environment() with client.stopped(): yield client - - -@pytest.fixture(scope='session', autouse=True) -def disable_jetsam_memory_checks(lldb_debugger): - client = HildaClient(lldb_debugger) - lldb.hilda_client = client - client.init_dynamic_environment() - client.disable_jetsam_memory_checks() diff --git a/tests/hilda_tests.py b/tests/hilda_tests.py index 410d972..96432eb 100644 --- a/tests/hilda_tests.py +++ b/tests/hilda_tests.py @@ -1,20 +1,16 @@ -import os from pathlib import Path import click from hilda import launch_lldb +PROCESS = 'sysmond' + @click.command() -@click.argument('process') -@click.argument('ssh_port', type=click.INT) -@click.option('--debug-port', type=click.INT, default=1234) -@click.option('--hostname', default='localhost') -def main(process, ssh_port, debug_port, hostname): +def main(): """ Start debugserver at remote device and connect using lldb """ - launch_lldb.start_remote(debug_port, ssh_port, process, hostname, - os.path.join(Path(__file__).resolve().parent, "lldb_entrypoint.py")) + launch_lldb.attach(name=PROCESS, rc_script=str(Path(__file__).resolve().parent / 'lldb_entrypoint.py')) if __name__ == '__main__': diff --git a/tests/test_hilda_client/test_hilda_client.py b/tests/test_hilda_client/test_hilda_client.py index ed78dac..66ec370 100644 --- a/tests/test_hilda_client/test_hilda_client.py +++ b/tests/test_hilda_client/test_hilda_client.py @@ -39,13 +39,13 @@ def test_lsof(hilda_client): temp_dir_url = hilda_client.objc_get_class('NSFileManager').defaultManager().objc_symbol.temporaryDirectory temp_url = temp_dir_url.URLByAppendingPathComponent_(hilda_client.ns('temp.txt')).objc_symbol c_file_path = temp_url.path.cString() - file_path = c_file_path.peek_str().decode() + file_path = c_file_path.peek_str() file_handle = hilda_client.symbols.mkstemp(c_file_path) max_path_len = hilda_client.evaluate_expression('PATH_MAX') try: with hilda_client.safe_malloc(max_path_len) as real_path: hilda_client.symbols.realpath(file_path, real_path) - assert hilda_client.lsof()[file_handle] == real_path.peek_str().decode() + assert hilda_client.lsof()[file_handle] == real_path.peek_str() finally: hilda_client.symbols.close(file_handle) hilda_client.symbols.unlink(file_path) diff --git a/tests/test_symbols/test_objective_c_class.py b/tests/test_symbols/test_objective_c_class.py index f76d4bb..4b004f0 100644 --- a/tests/test_symbols/test_objective_c_class.py +++ b/tests/test_symbols/test_objective_c_class.py @@ -49,17 +49,6 @@ def test_getting_super_class_method(hilda_client): @pytest.mark.parametrize('encoded, result', [ - ( - ('T{basic_string, std::__1::allocator >=' - '{__compressed_pair, ' - 'std::__1::allocator >::__rep, std::__1:allocator >=' - '{__rep=(?={__long=*QQ}{__short=[23c]{?=C}}{__raw=[3Q]})}}},R,N'), - PropertyAttributes( - type_=('struct basic_string, std::__1::allocator > ' - '{ struct __compressed_pair, ' - 'std::__1::allocator >::__rep, std::__1:allocator > ' - '{ struct __rep { (?={__long=*QQ}{__short=[23c]{?=C}}{__raw=[3Q]}) x0; } x0; } x0; }'), - synthesize=None, list=['readonly', 'nonatomic'])), ('Tc,VcharDefault', PropertyAttributes(synthesize='charDefault', type_='char', list=[])), ('T{YorkshireTeaStruct=ic},VstructDefault', PropertyAttributes(synthesize='structDefault', type_='struct YorkshireTeaStruct { int x0; char x1; }', list=[])), diff --git a/tests/test_symbols/test_symbols_jar.py b/tests/test_symbols/test_symbols_jar.py index 814b488..97ae85f 100644 --- a/tests/test_symbols/test_symbols_jar.py +++ b/tests/test_symbols/test_symbols_jar.py @@ -34,6 +34,5 @@ def test_find(hilda_client): # populate the jar hilda_client.symbols.malloc hilda_client.symbols.rand - hilda_client.symbols.strlen assert 1 == len(hilda_client.symbols.clean().find('malloc')), 'expected to find only one'