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, write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 */
23
24#ifdef HAVE_CONFIG_H
25#include "config.h"
26#endif
27
28#include <stdarg.h>
29#include <stddef.h>
30#include <stdio.h>
31#include <stdlib.h>
32#include <setjmp.h>
33#include <stdint.h>
34#ifndef NO_CMOCKA
35#include <cmocka.h>
36#endif
37
38#include "errlevel.h"
39#include "error.h"
40#include "mock_msg.h"
41
42unsigned int x_debug_level = 0; /* Default to (almost) no debugging output */
43unsigned int print_x_debug_level = 0;
44
46
48
49
50void
52{
53 x_debug_level = level;
54}
55
56int
58{
59 return x_debug_level;
60}
61
62void
64{
65 print_x_debug_level = level;
66}
67
68int
70{
71 return x_debug_level;
72}
73
74void
75x_msg_va(const unsigned int flags, const char *format,
76 va_list arglist)
77{
78 if (flags & M_FATAL)
79 {
81 printf("FATAL ERROR:");
82 }
84 vsnprintf(mock_msg_buf, sizeof(mock_msg_buf), format, arglist);
85
86 if ((flags & M_DEBUG_LEVEL) <= print_x_debug_level)
87 {
88 printf("%s", mock_msg_buf);
89 printf("\n");
90 }
91}
92
93void
94x_msg(const unsigned int flags, const char *format, ...)
95{
96 va_list arglist;
97 va_start(arglist, format);
98 x_msg_va(flags, format, arglist);
99 va_end(arglist);
100}
101
102/* Allow to use mock_msg.c outside of UT */
103#ifndef NO_CMOCKA
104void
105assert_failed(const char *filename, int line, const char *condition)
106{
107 mock_assert(false, condition ? condition : "", filename, line);
108 /* Keep compiler happy. Should not happen, mock_assert() does not return */
109 exit(1);
110}
111#else /* ifndef NO_CMOCKA */
112void
113assert_failed(const char *filename, int line, const char *condition)
114{
115 msg(M_FATAL, "Assertion failed at %s:%d (%s)", filename, line, condition ? condition : "");
116 _exit(1);
117}
118#endif
119
120
121/*
122 * Fail memory allocation. Don't use msg() because it tries
123 * to allocate memory as part of its operation.
124 */
125void
127{
128 fprintf(stderr, "Out of Memory\n");
129 exit(1);
130}
131
132bool
133dont_mute(unsigned int flags)
134{
135 return true;
136}
unsigned int x_debug_level
Definition mock_msg.c:42
void out_of_memory(void)
Definition mock_msg.c:126
int mock_get_debug_level(void)
Definition mock_msg.c:57
bool dont_mute(unsigned int flags)
Check muting filter.
Definition mock_msg.c:133
unsigned int print_x_debug_level
Definition mock_msg.c:43
void x_msg(const unsigned int flags, const char *format,...)
Definition mock_msg.c:94
int get_debug_level(void)
Definition mock_msg.c:69
void x_msg_va(const unsigned int flags, const char *format, va_list arglist)
Definition mock_msg.c:75
void assert_failed(const char *filename, int line, const char *condition)
Definition mock_msg.c:105
bool fatal_error_triggered
Definition mock_msg.c:45
void mock_set_print_debug_level(int level)
Definition mock_msg.c:63
char mock_msg_buf[MOCK_MSG_BUF]
Definition mock_msg.c:47
void mock_set_debug_level(int level)
Mock debug level defaults to 0, which gives clean(-ish) test reports.
Definition mock_msg.c:51
#define MOCK_MSG_BUF
Definition mock_msg.h:34
#define CLEAR(x)
Definition basic.h:33
#define M_DEBUG_LEVEL
Definition error.h:87
#define M_FATAL
Definition error.h:89
#define msg(flags,...)
Definition error.h:144