Branch data Line data Source code
1 : : #include "testmocking.h"
2 : :
3 : : #include <errno.h>
4 : : #include <stddef.h>
5 : : #include <unistd.h>
6 : :
7 : : static size_t testmocking_write_fail_remaining = 0;
8 : : static int testmocking_write_errno = EIO;
9 : :
10 : : /**
11 : : * @brief Arm write mock to fail for the next n calls
12 : : *
13 : : * @param[in] n Number of consecutive write calls that must fail
14 : : * @param[in] err errno value exposed to the tested code
15 : : */
16 : 2 : void testmocking_write_fail_next(
17 : : size_t n,
18 : : int err)
19 : : {
20 : 2 : testmocking_write_fail_remaining = n;
21 : 2 : testmocking_write_errno = err;
22 : 2 : }
23 : :
24 : : /**
25 : : * @brief Disable write mock unconditionally
26 : : */
27 : 3 : void testmocking_write_disable(void)
28 : : {
29 : 3 : testmocking_write_fail_remaining = 0;
30 : 3 : testmocking_write_errno = EIO;
31 : 3 : }
32 : :
33 : : #ifndef EVIL_EMPIRE_OS
34 : : ssize_t __real_write(
35 : : int fd,
36 : : const void *buf,
37 : : size_t count);
38 : :
39 : : /**
40 : : * @brief Linker-redirected entry point for write
41 : : *
42 : : * The mock is transparent by default. When a test arms it, the next selected
43 : : * calls fail with the configured errno value so low-level error-reporting
44 : : * fallback paths can be exercised deterministically
45 : : *
46 : : * @param[in] fd File descriptor passed by the caller
47 : : * @param[in] buf Source buffer passed by the caller
48 : : * @param[in] count Number of bytes requested by the caller
49 : : * @return write-compatible result
50 : : */
51 : 3671 : ssize_t __wrap_write(
52 : : int fd,
53 : : const void *buf,
54 : : size_t count)
55 : : {
56 [ + + ]: 3671 : if(testmocking_write_fail_remaining > 0U)
57 : : {
58 : 3 : testmocking_write_fail_remaining--;
59 : 3 : errno = testmocking_write_errno;
60 : 3 : return(-1);
61 : : }
62 : :
63 : 3668 : return(__real_write(fd,buf,count));
64 : : }
65 : : #endif
|