OpenVPN
mock_msg.c
Go to the documentation of this file.
1/*
2 * OpenVPN -- An application to securely tunnel IP networks
3 * over a single UDP port, with support for SSL/TLS-based
4 * session authentication and key exchange,
5 * packet encryption, packet authentication, and
6 * packet compression.
7 *
8 * Copyright (C) 2016-2021 Sentyron B.V. <openvpn@sentyron.com>
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License version 2
12 * as published by the Free Software Foundation.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, see <https://www.gnu.org/licenses/>.
21 */
22
23#ifdef HAVE_CONFIG_H
24#include "config.h"
25#endif
26
27#include <stdarg.h>
28#include <stddef.h>
29#include <stdio.h>
30#include <stdlib.h>
31#include <setjmp.h>
32#include <stdint.h>
33#ifndef NO_CMOCKA
34#include <cmocka.h>
35#endif
36
37#include "errlevel.h"
38#include "error.h"
39#include "mock_msg.h"
40
41msglvl_t x_debug_level = 0; /* Default to (almost) no debugging output */
43
44
46
47
48void
50{
51 x_debug_level = level;
52}
53
56{
57 return x_debug_level;
58}
59
60void
65
68{
69 return x_debug_level;
70}
71
72void
73x_msg_va(const msglvl_t flags, const char *format, va_list arglist)
74{
75 if (flags & M_FATAL)
76 {
77 printf("FATAL ERROR:");
78 }
80 vsnprintf(mock_msg_buf, sizeof(mock_msg_buf), format, arglist);
81
82 if ((flags & M_DEBUG_LEVEL) <= print_x_debug_level)
83 {
84 printf("%s", mock_msg_buf);
85 printf("\n");
86 }
87#ifndef NO_CMOCKA
88 if (flags & M_FATAL)
89 {
90 mock_assert(false, "FATAL ERROR", __FILE__, __LINE__);
91 }
92#endif
93}
94
95void
96x_msg(const msglvl_t flags, const char *format, ...)
97{
98 va_list arglist;
99 va_start(arglist, format);
100 x_msg_va(flags, format, arglist);
101 va_end(arglist);
102}
103
104/* Allow to use mock_msg.c outside of UT */
105#ifndef NO_CMOCKA
106void
107assert_failed(const char *filename, int line, const char *condition)
108{
109 mock_assert(false, condition ? condition : "", filename, line);
110 /* Keep compiler happy. Should not happen, mock_assert() does not return */
111 exit(1);
112}
113#else /* ifndef NO_CMOCKA */
114void
115assert_failed(const char *filename, int line, const char *condition)
116{
117 msg(M_FATAL, "Assertion failed at %s:%d (%s)", filename, line, condition ? condition : "");
118 _exit(1);
119}
120#endif
121
122
123/*
124 * Fail memory allocation. Don't use msg() because it tries
125 * to allocate memory as part of its operation.
126 */
127void
129{
130 fprintf(stderr, "Out of Memory\n");
131 exit(1);
132}
133
134bool
136{
137 return true;
138}
void out_of_memory(void)
Definition mock_msg.c:128
msglvl_t get_debug_level(void)
Definition mock_msg.c:67
msglvl_t mock_get_debug_level(void)
Definition mock_msg.c:55
void x_msg(const msglvl_t flags, const char *format,...)
Definition mock_msg.c:96
void mock_set_debug_level(msglvl_t level)
Mock debug level defaults to 0, which gives clean(-ish) test reports.
Definition mock_msg.c:49
msglvl_t x_debug_level
Definition mock_msg.c:41
void mock_set_print_debug_level(msglvl_t level)
Definition mock_msg.c:61
bool dont_mute(msglvl_t flags)
Check muting filter.
Definition mock_msg.c:135
void assert_failed(const char *filename, int line, const char *condition)
Definition mock_msg.c:107
msglvl_t print_x_debug_level
Definition mock_msg.c:42
char mock_msg_buf[MOCK_MSG_BUF]
Definition mock_msg.c:45
void x_msg_va(const msglvl_t flags, const char *format, va_list arglist)
Definition mock_msg.c:73
#define MOCK_MSG_BUF
Definition mock_msg.h:35
#define CLEAR(x)
Definition basic.h:32
#define M_DEBUG_LEVEL
Definition error.h:88
#define M_FATAL
Definition error.h:90
#define msg(flags,...)
Definition error.h:152
unsigned int msglvl_t
Definition error.h:77