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 #115

Closed ramzie80 closed 1 year ago

ramzie80 commented 1 year ago

The change in this diff 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.

Thank you for your contribution.

Before submitting this PR, please make sure: