Branch data Line data Source code
1 : : #include "testmocking.h"
2 : :
3 : : #include <stdarg.h>
4 : : #include <stddef.h>
5 : : #include <stdio.h>
6 : :
7 : : static size_t testmocking_vsnprintf_remaining = 0;
8 : :
9 : : /**
10 : : * @brief Arm vsnprintf mock to fail for the next n calls
11 : : *
12 : : * @param[in] n Number of consecutive vsnprintf calls that must fail
13 : : */
14 : 2 : void testmocking_vsnprintf_fail_next(size_t n)
15 : : {
16 : 2 : testmocking_vsnprintf_remaining = n;
17 : 2 : }
18 : :
19 : : /**
20 : : * @brief Disable vsnprintf mock unconditionally
21 : : */
22 : 2 : void testmocking_vsnprintf_disable(void)
23 : : {
24 : 2 : testmocking_vsnprintf_remaining = 0;
25 : 2 : }
26 : :
27 : : #ifndef EVIL_EMPIRE_OS
28 : : int __real_vsnprintf(
29 : : char *str,
30 : : size_t size,
31 : : const char *format,
32 : : va_list args) __attribute__((format(printf,3,0)));
33 : :
34 : : int __wrap_vsnprintf(
35 : : char *str,
36 : : size_t size,
37 : : const char *format,
38 : : va_list args) __attribute__((format(printf,3,0)));
39 : :
40 : : /**
41 : : * @brief Linker-redirected entry point for vsnprintf
42 : : *
43 : : * When the mock is armed each call decrements the remaining counter and
44 : : * returns -1. When the counter reaches zero the call is forwarded to libc
45 : : *
46 : : * @param[out] str Destination buffer passed by the caller
47 : : * @param[in] size Destination buffer size
48 : : * @param[in] format printf-style format string
49 : : * @param[in] args Collected variadic argument list
50 : : * @return vsnprintf-compatible result
51 : : */
52 : 56037 : int __wrap_vsnprintf(
53 : : char *str,
54 : : size_t size,
55 : : const char *format,
56 : : va_list args)
57 : : {
58 [ + + ]: 56037 : if(testmocking_vsnprintf_remaining > 0)
59 : : {
60 : 2 : testmocking_vsnprintf_remaining--;
61 : 2 : return -1;
62 : : }
63 : :
64 : 56035 : return __real_vsnprintf(str,size,format,args);
65 : : }
66 : : #endif
|