Branch data Line data Source code
1 : : #include "testmocking.h"
2 : :
3 : : #include <errno.h>
4 : : #include <stddef.h>
5 : : #include <time.h>
6 : : #include <sys/time.h>
7 : :
8 : : static size_t testmocking_gettimeofday_remaining = 0;
9 : : static size_t testmocking_localtime_r_remaining = 0;
10 : :
11 : : /**
12 : : * @brief Arm gettimeofday mock to fail for the next n calls
13 : : *
14 : : * @param[in] n Number of consecutive gettimeofday calls that must fail
15 : : */
16 : 1 : void testmocking_gettimeofday_fail_next(size_t n)
17 : : {
18 : 1 : testmocking_gettimeofday_remaining = n;
19 : 1 : }
20 : :
21 : : /**
22 : : * @brief Disable gettimeofday mock unconditionally
23 : : */
24 : 2 : void testmocking_gettimeofday_disable(void)
25 : : {
26 : 2 : testmocking_gettimeofday_remaining = 0;
27 : 2 : }
28 : :
29 : : /**
30 : : * @brief Arm localtime_r mock to fail for the next n calls
31 : : *
32 : : * @param[in] n Number of consecutive localtime_r calls that must fail
33 : : */
34 : 1 : void testmocking_localtime_r_fail_next(size_t n)
35 : : {
36 : 1 : testmocking_localtime_r_remaining = n;
37 : 1 : }
38 : :
39 : : /**
40 : : * @brief Disable localtime_r mock unconditionally
41 : : */
42 : 2 : void testmocking_localtime_r_disable(void)
43 : : {
44 : 2 : testmocking_localtime_r_remaining = 0;
45 : 2 : }
46 : :
47 : : #ifndef EVIL_EMPIRE_OS
48 : : int __real_gettimeofday(
49 : : struct timeval *tv,
50 : : void *tz);
51 : :
52 : : /**
53 : : * @brief Linker-redirected entry point for gettimeofday
54 : : *
55 : : * When the mock is armed each call decrements the remaining counter, sets
56 : : * errno to EIO, and fails. When the counter reaches zero the call is forwarded
57 : : * to libc
58 : : *
59 : : * @param[out] tv Time value destination passed by the caller
60 : : * @param[in] tz Obsolete timezone argument passed by the caller
61 : : * @return gettimeofday-compatible result
62 : : */
63 : 173 : int __wrap_gettimeofday(
64 : : struct timeval *tv,
65 : : void *tz)
66 : : {
67 [ + + ]: 173 : if(testmocking_gettimeofday_remaining > 0)
68 : : {
69 : 1 : testmocking_gettimeofday_remaining--;
70 : 1 : errno = EIO;
71 : 1 : return -1;
72 : : }
73 : :
74 : 172 : return __real_gettimeofday(tv,tz);
75 : : }
76 : :
77 : : struct tm *__real_localtime_r(
78 : : const time_t *timer,
79 : : struct tm *result);
80 : :
81 : : /**
82 : : * @brief Linker-redirected entry point for localtime_r
83 : : *
84 : : * When the mock is armed each call decrements the remaining counter and
85 : : * returns NULL. When the counter reaches zero the call is forwarded to libc
86 : : *
87 : : * @param[in] timer Time value passed by the caller
88 : : * @param[out] result Destination structure passed by the caller
89 : : * @return localtime_r-compatible result
90 : : */
91 : 329 : struct tm *__wrap_localtime_r(
92 : : const time_t *timer,
93 : : struct tm *result)
94 : : {
95 [ + + ]: 329 : if(testmocking_localtime_r_remaining > 0)
96 : : {
97 : 1 : testmocking_localtime_r_remaining--;
98 : 1 : return NULL;
99 : : }
100 : :
101 : 328 : return __real_localtime_r(timer,result);
102 : : }
103 : : #endif
|