Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ability to mock syscalls for tests #133

Open
bigspider opened this issue Feb 8, 2021 · 2 comments
Open

Add ability to mock syscalls for tests #133

bigspider opened this issue Feb 8, 2021 · 2 comments
Labels
enhancement New feature or request

Comments

@bigspider
Copy link
Contributor

While writing functional tests, it is often the case that some code paths only happen based on the return values of certain APDUs.
Therefore, to get a good test coverage, it would be very helpful to hook into the syscall emulation and programmatically control the syscalls' return values.

In the best of all possible worlds, python tests would have a speculos fixture running an app, that has god-mode control over the device:

def test_something(speculos):
    def mock_os_my_syscall(param):
       return 1 if param == 0xF00 else 0

   speculos.on("os_my_syscall", mock_os_my_syscall)

  # test stuff

Ideally, it should be possible to alter the behavior during execution, to test more complex interactions where the syscall is called multiple times and its return values is different.

My current use case is to test code depending on the result of os_global_pin_is_validated; so even less powerful customization on speculos might be enough.

@greenknot greenknot added the enhancement New feature or request label Feb 10, 2021
@greenknot
Copy link
Contributor

I added a proof-of-concept code in the hook-syscalls branch. An HTTP request is made each time the syscall os_global_pin_is_validated is called and the HTTP response is used to set the return value of the syscall. The syscall id id is passed in the URL (GET /?syscall=0x6000a03c HTTP/1.0).

As an example, here is an HTTP server wich returns the same value than the syscall implementation (0xaa is BOLOS_UX_OK and 0x9000a078 is SYSCALL_os_global_pin_is_validated_ID_OUT).

cd /tmp/
echo '0xaa 0x9000a078' > index.html
python3 -m http.server
./speculos.py --hook-syscalls ./apps/btc.elf

Free free to customize that for your needs, ie.:

Hope it helps!

@bigspider
Copy link
Contributor Author

That's amazing, thanks! That's going to be very useful.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants