Skip to content

Commit

Permalink
Merge pull request #36 from doronz88/bugfix/pytest_on_mac
Browse files Browse the repository at this point in the history
tests: make `pytest` succeed on macos
  • Loading branch information
doronz88 authored Sep 11, 2023
2 parents ffd94dc + 11aa8c1 commit fe8c637
Show file tree
Hide file tree
Showing 8 changed files with 41 additions and 74 deletions.
24 changes: 4 additions & 20 deletions hilda/hilda_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
45 changes: 26 additions & 19 deletions hilda/launch_lldb.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import os
import time
from pathlib import Path
from typing import Optional

import click
import coloredlogs
Expand Down Expand Up @@ -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}"&')
Expand All @@ -47,44 +48,50 @@ 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)
@click.option('--debug-port', type=click.INT, default=1234)
@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}"')


@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__':
Expand Down
8 changes: 4 additions & 4 deletions hilda/objective_c_symbol.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
10 changes: 1 addition & 9 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
12 changes: 4 additions & 8 deletions tests/hilda_tests.py
Original file line number Diff line number Diff line change
@@ -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__':
Expand Down
4 changes: 2 additions & 2 deletions tests/test_hilda_client/test_hilda_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
11 changes: 0 additions & 11 deletions tests/test_symbols/test_objective_c_class.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,17 +49,6 @@ def test_getting_super_class_method(hilda_client):


@pytest.mark.parametrize('encoded, result', [
(
('T{basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >='
'{__compressed_pair<std::__1::basic_sting<char, std::__1::char_traits<char>, '
'std::__1::allocator<char> >::__rep, std::__1:allocator<char> >='
'{__rep=(?={__long=*QQ}{__short=[23c]{?=C}}{__raw=[3Q]})}}},R,N'),
PropertyAttributes(
type_=('struct basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > '
'{ struct __compressed_pair<std::__1::basic_sting<char, std::__1::char_traits<char>, '
'std::__1::allocator<char> >::__rep, std::__1:allocator<char> > '
'{ 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=[])),
Expand Down
1 change: 0 additions & 1 deletion tests/test_symbols/test_symbols_jar.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'

0 comments on commit fe8c637

Please sign in to comment.