[38;5;244m┌─[0m [38;5;40mroot@joaocosta:~#[0m [38;5;245mcat posts/13.md[0m [38;5;244m───────────────────────────────────────────[0m

[38;5;40m#[0m [1mUnit Testing Using CMocka[0m
[38;5;245mpublished 2024-11-21 · 8 min read · https://joaocosta.dev/blog/13[0m

[38;5;40mCMocka[0m [38;5;244m(https://cmocka.org/)[0m is a unit testing framework for code in C. It was
designed to support all platforms that C runs on (including embedded systems).
Its feature set includes:

[38;5;40m→[0m Function mocking.
[38;5;40m→[0m Test setup/teardown routines.
[38;5;40m→[0m Exception handling for signals, e.g., SIGSEGV, SIGILL, etc.
[38;5;40m→[0m Simple light-weight memory leak, buffer overflow, and buffer underflow
  detection.

You can find the full documentation of CMocka API [38;5;40mhere[0m
[38;5;244m(https://api.cmocka.org/)[0m.

[38;5;38m##[0m [1mGetting started[0m

Although not required, when using CMocka, it is recommended to have the test
cases as separate executables, so these examples will have their own separate
[38;5;38mmain()[0m function.

In CMocka, each test case is a function. The following is a test case testing an
addition:

[38;5;244m┄─────────────────────────────────────────────────────────────────────────── c [0m
[38;5;245m│[0m static void test_addition(void **state)
[38;5;245m│[0m {
[38;5;245m│[0m         // given
[38;5;245m│[0m         int a = 1;
[38;5;245m│[0m         int b = 2;
[38;5;245m│[0m         int expected_result = 3;
[38;5;245m│[0m         int result;
[38;5;245m│[0m 
[38;5;245m│[0m         // when
[38;5;245m│[0m         result = a + b;
[38;5;245m│[0m 
[38;5;245m│[0m         // then
[38;5;245m│[0m         assert_int_equal(expected_result, result);
[38;5;245m│[0m }
[38;5;244m┄──────────────────────────────────────────────────────────────────────────────[0m

To execute this test, we need a test suite. In the following example, we execute
a CMocka test suite containing our addition test:

[38;5;244m┄─────────────────────────────────────────────────────────────────────────── c [0m
[38;5;245m│[0m int main(void)
[38;5;245m│[0m {
[38;5;245m│[0m         const struct CMUnitTest tests[] = {
[38;5;245m│[0m                 cmocka_unit_test(test_addition),
[38;5;245m│[0m         };
[38;5;245m│[0m 
[38;5;245m│[0m         return cmocka_run_group_tests(tests, NULL, NULL);
[38;5;245m│[0m }
[38;5;244m┄──────────────────────────────────────────────────────────────────────────────[0m

Running this example will give us the following result:

[38;5;244m┄───────────────────────────────────────────────────────────────────────── txt [0m
[38;5;245m│[0m [==========] tests: Running 1 test(s).
[38;5;245m│[0m [ RUN      ] test_addition
[38;5;245m│[0m [       OK ] test_addition
[38;5;245m│[0m [==========] tests: 1 test(s) run.
[38;5;245m│[0m [  PASSED  ] 1 test(s).
[38;5;244m┄──────────────────────────────────────────────────────────────────────────────[0m

This means that everything went well in our test (note that the output format
follows the standard set by [38;5;40mGoogleTest[0m [38;5;244m(https://github.com/google/googletest)[0m).

The following is the output of our verification failing when I set
[38;5;38mexpected_result[0m to 4. Note that CMocka tells us what was the error, the file,
and the line.

[38;5;244m┄───────────────────────────────────────────────────────────────────────── txt [0m
[38;5;245m│[0m [==========] tests: Running 1 test(s).
[38;5;245m│[0m [ RUN      ] test_addition
[38;5;245m│[0m [  ERROR   ] --- 0x4 != 0x3
[38;5;245m│[0m [   LINE   ] --- /cmake-cmocka-template/test/test_add2.c:19: error: Failure!
[38;5;245m│[0m [  FAILED  ] test_addition
[38;5;245m│[0m [==========] tests: 1 test(s) run.
[38;5;245m│[0m [  PASSED  ] 0 test(s).
[38;5;245m│[0m [  FAILED  ] tests: 1 test(s), listed below:
[38;5;245m│[0m [  FAILED  ] test_addition
[38;5;245m│[0m 
[38;5;245m│[0m  1 FAILED TEST(S)
[38;5;244m┄──────────────────────────────────────────────────────────────────────────────[0m

[38;5;135m###[0m [1mNote about including cmocka.h[0m

CMocka requires some headers to be included before its header is included. This
allows the user to customize some behavior, e.g. integer size, depending on the
resources available on the target platform. Including the CMocka library should
look something like the following:

[38;5;244m┄─────────────────────────────────────────────────────────────────────────── c [0m
[38;5;245m│[0m #include <limits.h>
[38;5;245m│[0m #include <setjmp.h>
[38;5;245m│[0m #include <stdarg.h>
[38;5;245m│[0m #include <stddef.h>
[38;5;245m│[0m // IMPORTANT: cmocka needs the imports above
[38;5;245m│[0m #include <cmocka.h>
[38;5;244m┄──────────────────────────────────────────────────────────────────────────────[0m

[38;5;135m###[0m [1mAssertions[0m

CMocka comes with a big list of assertions. You can find them [38;5;40mhere[0m
[38;5;244m(https://api.cmocka.org/group[1mcmocka[0masserts.html)[0m. You can also use the function
[38;5;38mfail_msg[0m to instantly fail with the given msg. This function uses a syntax
similar to [38;5;38mprintf[0m.

[38;5;38m##[0m [1mCustom test state[0m

Some of you might have noticed that on the previous examples, our [38;5;38mtest_addition[0m
function received a state parameter that we didn't use. This parameter is a
mechanism of passing an initial state the test. For example, imagine that you
want to parametrize the test arguments of a test case:

[38;5;244m┄─────────────────────────────────────────────────────────────────────────── c [0m
[38;5;245m│[0m typedef struct {
[38;5;245m│[0m         int *test_args;
[38;5;245m│[0m         ptrdiff_t current_test;
[38;5;245m│[0m } addition_params_t;
[38;5;245m│[0m 
[38;5;245m│[0m static void test_addition(void **state)
[38;5;245m│[0m {
[38;5;245m│[0m         // given
[38;5;245m│[0m         addition_params_t *addition_params = (addition_params_t *)(*state);
[38;5;245m│[0m         // skip to our current test
[38;5;245m│[0m         int *test_args = addition_params->test_args +
[38;5;245m│[0m                          (addition_params->current_test * 3);
[38;5;245m│[0m         int a = test_args[0];
[38;5;245m│[0m         int b = test_args[1];
[38;5;245m│[0m         int expected_result = test_args[2];
[38;5;245m│[0m         int result;
[38;5;245m│[0m         addition_params->current_test++;
[38;5;245m│[0m 
[38;5;245m│[0m         // when
[38;5;245m│[0m         result = a + b;
[38;5;245m│[0m 
[38;5;245m│[0m         // then
[38;5;245m│[0m         assert_int_equal(expected_result, result);
[38;5;245m│[0m }
[38;5;245m│[0m 
[38;5;245m│[0m int main(void)
[38;5;245m│[0m {
[38;5;245m│[0m         int test_args[] = {
[38;5;245m│[0m                 1, 2, 3,
[38;5;245m│[0m                 2, 2, 4,
[38;5;245m│[0m         };
[38;5;245m│[0m         addition_params_t addition_params = {
[38;5;245m│[0m                 .test_args = test_args,
[38;5;245m│[0m                 .current_test = 0,
[38;5;245m│[0m         };
[38;5;245m│[0m 
[38;5;245m│[0m         const struct CMUnitTest tests[] = {
[38;5;245m│[0m                 cmocka_unit_test_prestate(test_addition, &addition_params),
[38;5;245m│[0m                 cmocka_unit_test_prestate(test_addition, &addition_params),
[38;5;245m│[0m         };
[38;5;245m│[0m 
[38;5;245m│[0m         return cmocka_run_group_tests(tests, NULL, NULL);
[38;5;245m│[0m }
[38;5;244m┄──────────────────────────────────────────────────────────────────────────────[0m

In this example we call the same test function twice, with different parameters,
so effectively get 2 test cases. First we test that [38;5;38m1 + 2 == 3[0m and then we test
that [38;5;38m2 + 2 == 4[0m. Note that instead of adding a test case to the suite with
[38;5;38mcmocka_unit_test[0m, we add it using [38;5;38mcmocka_unit_test_prestate[0m so we can pass it
the initial state.

[38;5;38m##[0m [1mTest setup and teardown[0m

Sometimes we need to do some setup steps that are common between multiple test
cases. These can range from initializing some memory region, to setting up a
certain state/condition. Either way, CMocka has a mechanism to register setup
and teardown callbacks for specific test cases or for the whole suite.

[38;5;135m###[0m [1mTest case specific setup/teardown[0m

Let's get back to our simpler version of the addition test:

[38;5;244m┄─────────────────────────────────────────────────────────────────────────── c [0m
[38;5;245m│[0m static void test_addition(void **state)
[38;5;245m│[0m {
[38;5;245m│[0m         // given
[38;5;245m│[0m         int a = 1;
[38;5;245m│[0m         int b = 2;
[38;5;245m│[0m         int expected_result = 3;
[38;5;245m│[0m         int result;
[38;5;245m│[0m 
[38;5;245m│[0m         // when
[38;5;245m│[0m         result = a + b;
[38;5;245m│[0m 
[38;5;245m│[0m         // then
[38;5;245m│[0m         assert_int_equal(expected_result, result);
[38;5;245m│[0m }
[38;5;244m┄──────────────────────────────────────────────────────────────────────────────[0m

Consider that we wanted to run something before and after the test. In this
case, we'll just say [3mhi[0m and [3mgoodbye[0m:

[38;5;244m┄─────────────────────────────────────────────────────────────────────────── c [0m
[38;5;245m│[0m static int test_addition_setup(void **state)
[38;5;245m│[0m {
[38;5;245m│[0m         printf("Hello from test_addition_setup\n");
[38;5;245m│[0m         return 0;
[38;5;245m│[0m }
[38;5;245m│[0m 
[38;5;245m│[0m static int test_addition_teardown(void **state)
[38;5;245m│[0m {
[38;5;245m│[0m         printf("Goodbye from test_addition_teardown\n");
[38;5;245m│[0m         return 0;
[38;5;245m│[0m }
[38;5;245m│[0m 
[38;5;245m│[0m int main(void)
[38;5;245m│[0m {
[38;5;245m│[0m         const struct CMUnitTest tests[] = {
[38;5;245m│[0m                 cmocka_unit_test_setup_teardown(test_addition,
[38;5;245m│[0m                                                 &test_addition_setup,
[38;5;245m│[0m                                                 &test_addition_teardown),
[38;5;245m│[0m                 cmocka_unit_test_setup_teardown(test_addition,
[38;5;245m│[0m                                                 &test_addition_setup,
[38;5;245m│[0m                                                 &test_addition_teardown),
[38;5;245m│[0m         };
[38;5;245m│[0m 
[38;5;245m│[0m         return cmocka_run_group_tests(tests, NULL, NULL);
[38;5;245m│[0m }
[38;5;244m┄──────────────────────────────────────────────────────────────────────────────[0m

Resulting in each test (same test function ran twice) saying hello and goodbye:

[38;5;244m┄───────────────────────────────────────────────────────────────────────── txt [0m
[38;5;245m│[0m [==========] tests: Running 2 test(s).
[38;5;245m│[0m [ RUN      ] test_addition
[38;5;245m│[0m Hello from test_addition_setup
[38;5;245m│[0m Goodbye from test_addition_teardown
[38;5;245m│[0m [       OK ] test_addition
[38;5;245m│[0m [ RUN      ] test_addition
[38;5;245m│[0m Hello from test_addition_setup
[38;5;245m│[0m Goodbye from test_addition_teardown
[38;5;245m│[0m [       OK ] test_addition
[38;5;245m│[0m [==========] tests: 2 test(s) run.
[38;5;245m│[0m [  PASSED  ] 2 test(s).
[38;5;244m┄──────────────────────────────────────────────────────────────────────────────[0m

Note that contrary to our test case function that return nothing, both setup and
teardown return an int. When the return of either setup or teardown is not zero,
the test will fail. For example, if we return 1 from [38;5;38mtest_addition_setup[0m:

[38;5;244m┄───────────────────────────────────────────────────────────────────────── txt [0m
[38;5;245m│[0m [==========] tests: Running 2 test(s).
[38;5;245m│[0m [ RUN      ] test_addition
[38;5;245m│[0m Hello from test_addition_setup
[38;5;245m│[0m Could not run test: Test setup failed
[38;5;245m│[0m [  ERROR   ] test_addition
[38;5;245m│[0m [ RUN      ] test_addition
[38;5;245m│[0m Hello from test_addition_setup
[38;5;245m│[0m Could not run test: Test setup failed
[38;5;245m│[0m [  ERROR   ] test_addition
[38;5;245m│[0m [==========] tests: 2 test(s) run.
[38;5;245m│[0m [  PASSED  ] 0 test(s).
[38;5;244m┄──────────────────────────────────────────────────────────────────────────────[0m

[38;5;135m###[0m [1mTest suite setup/teardown[0m

Let's consider the case we just saw. We might want to perform the setup only
once before all tests and the teardown only once after all tests. In this case
we want to register a test suite setup/teardown instead of a test case one:

[38;5;244m┄─────────────────────────────────────────────────────────────────────────── c [0m
[38;5;245m│[0m static int test_suite_setup(void **state)
[38;5;245m│[0m {
[38;5;245m│[0m         printf("Hello from test_suite_setup\n");
[38;5;245m│[0m         return 0;
[38;5;245m│[0m }
[38;5;245m│[0m 
[38;5;245m│[0m static int test_suite_teardown(void **state)
[38;5;245m│[0m {
[38;5;245m│[0m         printf("Goodbye from test_suite_teardown\n");
[38;5;245m│[0m         return 0;
[38;5;245m│[0m }
[38;5;245m│[0m 
[38;5;245m│[0m int main(void)
[38;5;245m│[0m {
[38;5;245m│[0m         const struct CMUnitTest tests[] = {
[38;5;245m│[0m                 cmocka_unit_test(test_addition),
[38;5;245m│[0m                 cmocka_unit_test(test_addition),
[38;5;245m│[0m         };
[38;5;245m│[0m 
[38;5;245m│[0m         return cmocka_run_group_tests(tests, &test_suite_setup, &test_suite_teardown);
[38;5;245m│[0m }
[38;5;244m┄──────────────────────────────────────────────────────────────────────────────[0m

This time we see the messages only once:

[38;5;244m┄───────────────────────────────────────────────────────────────────────── txt [0m
[38;5;245m│[0m [==========] tests: Running 2 test(s).
[38;5;245m│[0m Hello from test_suite_setup
[38;5;245m│[0m [ RUN      ] test_addition
[38;5;245m│[0m [       OK ] test_addition
[38;5;245m│[0m [ RUN      ] test_addition
[38;5;245m│[0m [       OK ] test_addition
[38;5;245m│[0m Goodbye from test_suite_teardown
[38;5;245m│[0m [==========] tests: 2 test(s) run.
[38;5;245m│[0m [  PASSED  ] 2 test(s).
[38;5;244m┄──────────────────────────────────────────────────────────────────────────────[0m

Just like in the test case specific setup/teardown, an output different from
zero will result in a test suite execution failure. In the case of a setup
failure test tests don't even run:

[38;5;244m┄───────────────────────────────────────────────────────────────────────── txt [0m
[38;5;245m│[0m [==========] tests: Running 2 test(s).
[38;5;245m│[0m Hello from test_suite_setup
[38;5;245m│[0m [  FAILED  ] GROUP SETUP
[38;5;245m│[0m [  ERROR   ] tests
[38;5;245m│[0m Goodbye from test_suite_teardown
[38;5;245m│[0m [==========] tests: 0 test(s) run.
[38;5;245m│[0m [  PASSED  ] 0 test(s).
[38;5;244m┄──────────────────────────────────────────────────────────────────────────────[0m

[38;5;135m###[0m [1mCombining setup, teardown, and state[0m

Just like the test case function, the test setup and teardown function also
receive a state parameter. When you pass state to a test, first it is passed to
the setup function, then to the test, and finally to the teardown function.

CMocka allows us to do any combination of setup, teardown, or state passing
through its API. Take a look at the following test case registration function:

[38;5;40m→[0m [38;5;40mcmocka_unit_test[0m
  [38;5;244m(https://api.cmocka.org/group[1mcmocka[0mexec.html#ga562719c550b5ce277aab6b0371f7f34f)[0m
[38;5;40m→[0m [38;5;40mcmocka_unit_test_setup[0m
  [38;5;244m(https://api.cmocka.org/group[1mcmocka[0mexec.html#gaaccacc105038e49462888a3ededa28c5)[0m
[38;5;40m→[0m [38;5;40mcmocka_unit_test_teardown[0m
  [38;5;244m(https://api.cmocka.org/group[1mcmocka[0mexec.html#ga9b0d5ed78ddeba6d9aaa9a692cda426b)[0m
[38;5;40m→[0m [38;5;40mcmocka_unit_test_setup_teardown[0m
  [38;5;244m(https://api.cmocka.org/group[1mcmocka[0mexec.html#gab9e5396c9a424ccbb5d30e6d421fd066)[0m
[38;5;40m→[0m [38;5;40mcmocka_unit_test_prestate[0m
  [38;5;244m(https://api.cmocka.org/group[1mcmocka[0mexec.html#ga4d54508b0b7150afb27b4da38f0015c6)[0m
[38;5;40m→[0m [38;5;40mcmocka_unit_test_prestate_setup_teardown[0m
  [38;5;244m(https://api.cmocka.org/group[1mcmocka[0mexec.html#ga07ea44503d0f6c84c460b4a878cf31d5)[0m

Any of the arguments can be set to [38;5;38mNULL[0m, with the exception of the test case
function, if not needed.

[38;5;38m##[0m [1mMocks[0m

Mocks are a great tool to test a function's dependencies. Let's consider the
existence of two functions: [38;5;38madd[0m and [38;5;38madd3[0m:

[38;5;244m┄─────────────────────────────────────────────────────────────────────────── c [0m
[38;5;245m│[0m int add(int a, int b)
[38;5;245m│[0m {
[38;5;245m│[0m         return a + b;
[38;5;245m│[0m }
[38;5;245m│[0m 
[38;5;245m│[0m int add3(int a, int b, int c)
[38;5;245m│[0m {
[38;5;245m│[0m         int a_plus_b = add(a, b);
[38;5;245m│[0m         return add(a_plus_b, c);
[38;5;245m│[0m }
[38;5;244m┄──────────────────────────────────────────────────────────────────────────────[0m

[38;5;38madd3[0m adds 3 numbers and calls [38;5;38madd[0m twice to do that. When we're unit testing [38;5;38madd3[0m
we might want to mock [38;5;38madd[0m to isolate [38;5;38madd3[0m's behavior. Let's create a mock for
[38;5;38madd[0m and a test case for [38;5;38madd3[0m using it:

[38;5;244m┄─────────────────────────────────────────────────────────────────────────── c [0m
[38;5;245m│[0m int my_add(int a, int b);
[38;5;245m│[0m 
[38;5;245m│[0m int __wrap_add(int a, int b) {
[38;5;245m│[0m   printf("HELLO FROM __wrap_add\n");
[38;5;245m│[0m   check_expected(a);
[38;5;245m│[0m   check_expected(b);
[38;5;245m│[0m   return mock_type(int);
[38;5;245m│[0m }
[38;5;245m│[0m 
[38;5;245m│[0m static void test_add3_mock(void **state) {
[38;5;245m│[0m   // given
[38;5;245m│[0m   int a = 1, b = 2, c = 4;
[38;5;245m│[0m   int expected_result = 7;
[38;5;245m│[0m   int result;
[38;5;245m│[0m 
[38;5;245m│[0m   expect_value(__wrap_add, a, 1);
[38;5;245m│[0m   expect_value(__wrap_add, b, 2);
[38;5;245m│[0m   will_return(__wrap_add, 3);
[38;5;245m│[0m   expect_value(__wrap_add, a, 3);
[38;5;245m│[0m   expect_value(__wrap_add, b, 4);
[38;5;245m│[0m   will_return(__wrap_add, 7);
[38;5;245m│[0m 
[38;5;245m│[0m   // when
[38;5;245m│[0m   result = add3(a, b, c);
[38;5;245m│[0m 
[38;5;245m│[0m   // then
[38;5;245m│[0m   assert_int_equal(expected_result, result);
[38;5;245m│[0m }
[38;5;245m│[0m 
[38;5;245m│[0m int main(void) {
[38;5;245m│[0m   const struct CMUnitTest tests[] = {
[38;5;245m│[0m       cmocka_unit_test(test_add3_mock),
[38;5;245m│[0m   };
[38;5;245m│[0m 
[38;5;245m│[0m   return cmocka_run_group_tests(tests, NULL, NULL);
[38;5;245m│[0m }
[38;5;244m┄──────────────────────────────────────────────────────────────────────────────[0m

Mocks in CMocka are just functions where you can implement any behavior you
want. CMocka gives us tools to check that the input arguments have the values
that we expected (see the [38;5;40mchecking parameters page[0m
[38;5;244m(https://api.cmocka.org/group[1mcmocka[0mparam.html)[0m). This checks double has a way to
count the number of times the mock was called. Note that since the add function
will be called twice, we specify the arguments and return values twice as well:

[38;5;244m┄───────────────────────────────────────────────────────────────────────── txt [0m
[38;5;245m│[0m [==========] tests: Running 1 test(s).
[38;5;245m│[0m [ RUN      ] test_add3_mock
[38;5;245m│[0m HELLO FROM __wrap_add
[38;5;245m│[0m HELLO FROM __wrap_add
[38;5;245m│[0m [       OK ] test_add3_mock
[38;5;245m│[0m [==========] tests: 1 test(s) run.
[38;5;245m│[0m [  PASSED  ] 1 test(s).
[38;5;244m┄──────────────────────────────────────────────────────────────────────────────[0m

As expected, our mock was called twice. If we change the test case to expect a
single call, the test will fail:

[38;5;244m┄─────────────────────────────────────────────────────────────────────────── c [0m
[38;5;245m│[0m static void test_add3_mock(void **state) {
[38;5;245m│[0m   // given
[38;5;245m│[0m   int a = 1, b = 2, c = 4;
[38;5;245m│[0m   int expected_result = 7;
[38;5;245m│[0m   int result;
[38;5;245m│[0m 
[38;5;245m│[0m   expect_value(__wrap_add, a, 1);
[38;5;245m│[0m   expect_value(__wrap_add, b, 2);
[38;5;245m│[0m   will_return(__wrap_add, 3);
[38;5;245m│[0m 
[38;5;245m│[0m   // when
[38;5;245m│[0m   result = add3(a, b, c);
[38;5;245m│[0m 
[38;5;245m│[0m   // then
[38;5;245m│[0m   assert_int_equal(expected_result, result);
[38;5;245m│[0m }
[38;5;244m┄──────────────────────────────────────────────────────────────────────────────[0m

[38;5;244m┄───────────────────────────────────────────────────────────────────────── txt [0m
[38;5;245m│[0m [==========] tests: Running 1 test(s).
[38;5;245m│[0m [ RUN      ] test_add3_mock
[38;5;245m│[0m HELLO FROM __wrap_add
[38;5;245m│[0m HELLO FROM __wrap_add
[38;5;245m│[0m [  ERROR   ] --- No entries for symbol __wrap_add.
[38;5;245m│[0m /cmake-cmocka-template/test/test_add2.c:16: error: Could not get value to check parameter a of function __wrap_add
[38;5;245m│[0m /cmake-cmocka-template/test/test_add2.c:28: note: Previously declared parameter value was declared here
[38;5;245m│[0m 
[38;5;245m│[0m [  FAILED  ] test_add3_mock
[38;5;245m│[0m [==========] tests: 1 test(s) run.
[38;5;245m│[0m [  PASSED  ] 0 test(s).
[38;5;245m│[0m [  FAILED  ] tests: 1 test(s), listed below:
[38;5;245m│[0m [  FAILED  ] test_add3_mock
[38;5;245m│[0m 
[38;5;245m│[0m  1 FAILED TEST(S)
[38;5;244m┄──────────────────────────────────────────────────────────────────────────────[0m

[38;5;135m###[0m [1mLinker tricks[0m

In order to replace the calls to [38;5;38madd[0m by calls to our mock in [38;5;38madd3[0m's code, we use
a linker trick: the [38;5;38m--wrap[0m flag. This linker flag/options allows us to define a
symbol that will replace another at link time. In this case, [38;5;38m__wrap_add[0m replaces
any undefined reference to [38;5;38madd[0m at link time, because we compiled the code with
the flags [38;5;38m-Wl,--wrap=add[0m. Note that function names starting with [38;5;38m__wrap[0m are
reserved by the compiler for this reason. Here is an excerpt from [38;5;38mld[0m's manual
explaining the functionality:

[38;5;135m┃[0m 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. This can be used to provide a wrapper for a system
  function. The wrapper function should be called "__wrap_symbol". If it wishes
  to call the system function, it should call "__real_symbol".

If for some reason we're using a linker that doesn't support this feature, we
need to do some dependency injection, which can involve changes to the source
code under test. Design your code with this in mind.

[38;5;38m##[0m [1mConclusion[0m

CMocka is pretty cool and powerful. Highly recommend it. If you're starting a
new C project or just want to see more examples on how to integrate and use
CMocka in your projects, take a look at my [38;5;40mcmake-cmocka-template project[0m
[38;5;244m(https://github.com/JoaoCostaIFG/cmake-cmocka-template)[0m

[38;5;244m└─[0m [38;5;40m→[0m view online: [38;5;40mhttps://joaocosta.dev/blog/13[0m
