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