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-2026 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
47
48
49void
51{
52 x_debug_level = level;
53}
54
57{
58 return x_debug_level;
59}
60
61void
66
69{
70 return x_debug_level;
71}
72
73void
74x_msg_va(const msglvl_t flags, const char *format, va_list arglist)
75{
76 if (flags & M_FATAL)
77 {
78 printf("FATAL ERROR:");
79 }
80
81 if (flags & M_MSG_VIRT_OUT)
82 {
84 vsnprintf(mock_managment_buf, sizeof(mock_managment_buf), format, arglist);
85 return;
86 }
87
89 vsnprintf(mock_msg_buf, sizeof(mock_msg_buf), format, arglist);
90
91 if ((flags & M_DEBUG_LEVEL) <= print_x_debug_level)
92 {
93 printf("%s", mock_msg_buf);
94 printf("\n");
95 }
96#ifndef NO_CMOCKA
97 if (flags & M_FATAL)
98 {
99 mock_assert(false, "FATAL ERROR", __FILE__, __LINE__);
100 }
101#endif
102}
103
104void
105x_msg(const msglvl_t flags, const char *format, ...)
106{
107 va_list arglist;
108 va_start(arglist, format);
109 x_msg_va(flags, format, arglist);
110 va_end(arglist);
111}
112
113/* Allow to use mock_msg.c outside of UT */
114#ifndef NO_CMOCKA
115void
116assert_failed(const char *filename, int line, const char *condition)
117{
118 mock_assert(false, condition ? condition : "", filename, line);
119 /* Keep compiler happy. Should not happen, mock_assert() does not return */
120 exit(1);
121}
122#else /* ifndef NO_CMOCKA */
123void
124assert_failed(const char *filename, int line, const char *condition)
125{
126 msg(M_FATAL, "Assertion failed at %s:%d (%s)", filename, line, condition ? condition : "");
127 _exit(1);
128}
129#endif
130
131
132/*
133 * Fail memory allocation. Don't use msg() because it tries
134 * to allocate memory as part of its operation.
135 */
136void
138{
139 fprintf(stderr, "Out of Memory\n");
140 exit(1);
141}
142
143bool
145{
146 return true;
147}
void out_of_memory(void)
Definition mock_msg.c:137
msglvl_t get_debug_level(void)
Definition mock_msg.c:68
msglvl_t mock_get_debug_level(void)
Definition mock_msg.c:56
void x_msg(const msglvl_t flags, const char *format,...)
Definition mock_msg.c:105
void mock_set_debug_level(msglvl_t level)
Mock debug level defaults to 0, which gives clean(-ish) test reports.
Definition mock_msg.c:50
msglvl_t x_debug_level
Definition mock_msg.c:41
char mock_managment_buf[MOCK_MSG_BUF]
Definition mock_msg.c:46
void mock_set_print_debug_level(msglvl_t level)
Definition mock_msg.c:62
bool dont_mute(msglvl_t flags)
Check muting filter.
Definition mock_msg.c:144
void assert_failed(const char *filename, int line, const char *condition)
Definition mock_msg.c:116
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:74
#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
#define M_MSG_VIRT_OUT
Definition error.h:100