1

I got task to write unit tests(using cunit and cmocka) for existing project(written in C), and a met following problem. When I wrap function that is defined in tested file, only original function is called. Additional, a can't change source of tested file. Everything I read didn't met the second condition, but on the other hard it is pretty hard to believe that unit test framework is not prepared for this kind of problem.

So is it possible to wrap function call to function that is defined and called in one file?

I've tried to wrap it by adding appropriate linker flag to cmake file.

mmichal10
  • 322
  • 2
  • 13

1 Answers1

1

is it possible to wrap function call to function that is defined and called in one file?

cmocka leverages the linker's --wrap option, as you know. The documentation of --wrap=symbol tells us that the answer to your question is No:

--wrap=symbol

Use a wrapper function for symbol. Any undefined reference to symbol will be resolved to __wrap_symbol. Any undefined reference to __real_symbol will be resolved to symbol.

Any reference to symbol that is in an object file that also contains the definition of symbol is not an undefined reference, so the linker will not resolve that reference to __wrap_symbol. The definition must be compiled into some other object file in the linkage for wrapping of the reference to occur.

Mike Kinghan
  • 55,740
  • 12
  • 153
  • 182