root/lib/common/mock.c

/* [previous][next][first][last][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. __wrap_getenv
  2. __wrap_uname

   1 /*
   2  * Copyright 2021 the Pacemaker project contributors
   3  *
   4  * The version control history for this file may have further details.
   5  *
   6  * This source code is licensed under the GNU Lesser General Public License
   7  * version 2.1 or later (LGPLv2.1+) WITHOUT ANY WARRANTY.
   8  */
   9 
  10 #include <stdlib.h>
  11 #include <sys/utsname.h>
  12 
  13 #include "mock_private.h"
  14 
  15 /* This file is only used when running "make check".  It is built into
  16  * libcrmcommon_test.a, not into libcrmcommon.so.  It is used to support
  17  * constructing mock versions of library functions for unit testing.
  18  *
  19  * Each unit test will only ever want to use a mocked version of one or two
  20  * library functions.  However, we need to mark all the mocked functions as
  21  * wrapped (with -Wl,--wrap= in the LDFLAGS) in libcrmcommon_test.a so that
  22  * all those unit tests can share the same special test library.  The unit
  23  * test then defines its own wrapped function.  Because a unit test won't
  24  * define every single wrapped function, there will be undefined references
  25  * at link time.
  26  *
  27  * This file takes care of those undefined references.  It defines a
  28  * wrapped version of every function that simply calls the real libc
  29  * version.  These wrapped versions are defined with a weak attribute,
  30  * which means the unit tests can define another wrapped version for
  31  * unit testing that will override the version defined here.
  32  *
  33  * IN SUMMARY:
  34  *
  35  * - Define two functions for each function listed in WRAPPED in mock.mk.
  36  *   One function is a weakly defined __wrap_X function that just calls
  37  *   __real_X.
  38  * - Add a __real_X and __wrap_X function prototype for each function to
  39  *   mock_private.h.
  40  * - Each unit test defines its own __wrap_X for whatever function it's
  41  *   mocking that overrides the version here.
  42  */
  43 
  44 char *__attribute__((weak))
  45 __wrap_getenv(const char *name) {
     /* [previous][next][first][last][top][bottom][index][help] */
  46     return __real_getenv(name);
  47 }
  48 
  49 int __attribute__((weak))
  50 __wrap_uname(struct utsname *buf) {
     /* [previous][next][first][last][top][bottom][index][help] */
  51     return __real_uname(buf);
  52 }

/* [previous][next][first][last][top][bottom][index][help] */