meekrosoft / fff

A testing micro framework for creating function test doubles
Other
749 stars 163 forks source link

Adding a preamble to mocked function name #116

Open ramzie80 opened 1 year ago

ramzie80 commented 1 year ago

The change in this patch modifies fff macro generation script to support a CLI option --preamble that can be used to set a preamble for generated mock function names.

On some embedded systems if we want to perform testing with HW in the loop, there are system functions or driver functions that cannot be mocked safely. For example the SW running on a micro-controller may need to maintain communication with another subsystem, replacing the functions that do this with mocks will put the system in a bad state.

To be able to mock such functions and keep the system in a good state we prepend a preamble to the mock function name and inside the mock function do intrumentation and then call the real function to keep the system in a good state. We can leverage the 'custom_fake' field created by fff to call the real function.

For example, we can name the mock of function XYZ() as wrap_XYZ() and use the GNU linker option -Wl,--wrap XYZ to re-route all references to XYZ() to __wrap_XYZ() (see https://ftp.gnu.org/old-gnu/Manuals/ld-2.9.1/html_node/ld_3.html). Now, inside wrap_XYZ() we can invoke XYZ() in addition to instrumentation. We can leverage the 'custom_fake' field created by fff to call the real version of XYZ(), this will be named as __real_XYZ(). Note, this is just an example the same can be acheived by custom tooling and not using GNU linkers --wrap flag.

Signed-off-by: Ramzi El-khater ramzie@meta.com

Thank you for your contribution.

Before submitting this PR, please make sure:

ramzie80 commented 1 year ago

I have not yet added unit-tests, would like to get it reviewed first before spending the time to add the tests.

meekrosoft commented 1 year ago

This looks good to me, but needs some documentation and tests. Thanks for the contribution!

ramzie80 commented 1 year ago

Thanks, I have added a unit-test that verifies one macro, not sure if we need to verify all again since they are checked in the main test cases. Will work on documentation next.

ramzie80 commented 1 year ago

I have added the documentation as well. Please let me know if anything further is needed. Thanks!

ramzie80 commented 1 year ago

Looks like the workflows have not run due to waiting on maintainer approval.