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

Adding a preamble to mocked function name #116

Open
wants to merge 4 commits into
base: master
Choose a base branch
from

Conversation

ramzie80
Copy link

@ramzie80 ramzie80 commented Aug 19, 2022

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 [email protected]

Thank you for your contribution.

Before submitting this PR, please make sure:

  • Your code builds clean without any errors or warnings
  • You are not breaking consistency
  • You have added unit tests
  • All tests and other checks pass

The change in this patch modifies fff macro generation script to support a CLI option
--preamble <string> 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 <[email protected]>
@ramzie80
Copy link
Author

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

@meekrosoft
Copy link
Owner

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

Update CMake file to generate "fff_preamble.h" using the cli argument "--preamble __wrap_"
and add a test-case that invokes the generated fake function that is now named as "__wrap_voidfunc1()"
and verifies the proper call_count gets incremented.

Signed-off-by: Ramzi El-khater <[email protected]>
@ramzie80
Copy link
Author

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
Copy link
Author

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

@ramzie80
Copy link
Author

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

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

Successfully merging this pull request may close these issues.

2 participants