I've tried multiple sources for solutions to this problem. They all either require modifying the source code, and architecture specific exploit such as writing in a jmp instruction to detour the function, or using a macro and including the c file. The first one is extremely annoying to deal with, the second is usually not possible due to page protections, and the third introduces a lot of problems with linking multiple files containing different mocks and unit test for the same source file. Is there any better method of doing this?
Asked
Active
Viewed 198 times
1
-
https://stackoverflow.com/questions/426230/what-is-the-ld-preload-trick – user3386109 Jan 06 '23 at 08:41
-
@user3386109 is this trick portable across platforms? it seems to only work on linux. Thanks though, I'll definitely look into this. – jungon Jan 06 '23 at 08:51
1 Answers
1
You can user function pointer in your nominal code. You assign them at init with nominal implemetation in your application. In your unit test you can then assign the function pointer to the mock implmentation. Function pointer is a common practice used to implement interface in C.
Here is a gist of how that could be done:
typedef struct {
void (*method) ();
} interface;
void run(itf *interface)
{
itf->method();
}
void methodImpl()
{
printf("nominal code");
}
void methodMock()
{
printf("mock code");
}
void do_run()
{
interface itf;
itf.method = methodImpl;
run(&itf);
}
void test_run()
{
interface itf;
itf.method = methodMock;
run(&itf);
}

davidriod
- 937
- 5
- 14
-
1wouldn't that require modifying the source code to use the function pointer? – jungon Jan 06 '23 at 08:32
-
1indeed it does, but for good unit testing you need to write testable nominal code. – davidriod Jan 06 '23 at 08:33
-
"indeed it does". Did you happen to read the incredibly small paragraph I left attached to my question, specifically asking for methods which avoid modifying the source code? – jungon Jan 06 '23 at 08:49
-
@jungon That was not that quite clear that you were requiring a non-modifying source code solution, you may want to edit your question in order to make that more obvious. – davidriod Jan 06 '23 at 09:40
-
@jungon I don't understand how you hope to have a non modifying source code solution. You either can replace the whole source code if it's a library (you mock the entire lib then, with LD_PRELOAD) or you have to modify your source code to some extent. You can use preprocessing condition to add/remove part of code, but ultimatly, it will heavely depend on what you want to achieve. – Tom's Jan 06 '23 at 09:56
-
@davidriod "I've tried multiple sources for solutions to this problem. They all either require modifying the source code ... The first one is extremely annoying to deal with ... Is there any better method of doing this?" So, I state that I've tried a few methods, including modifying the source code, and ask if there is a better way of doing this. I think it's pretty clear that I'm not looking for a solution that modifies the source code ... – jungon Jan 06 '23 at 09:57