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 
41 unsigned int x_debug_level = 0; /* Default to (almost) no debugging output */
42 bool fatal_error_triggered = false;
43 
44 void
46 {
47  x_debug_level = level;
48 }
49 
50 int
52 {
53  return x_debug_level;
54 }
55 
56 void
57 x_msg_va(const unsigned int flags, const char *format,
58  va_list arglist)
59 {
60  if (flags & M_FATAL)
61  {
62  fatal_error_triggered = true;
63  printf("FATAL ERROR:");
64  }
65  vprintf(format, arglist);
66  printf("\n");
67 }
68 
69 void
70 x_msg(const unsigned int flags, const char *format, ...)
71 {
72  va_list arglist;
73  va_start(arglist, format);
74  x_msg_va(flags, format, arglist);
75  va_end(arglist);
76 }
77 
78 /* Allow to use mock_msg.c outside of UT */
79 #ifndef NO_CMOCKA
80 void
81 assert_failed(const char *filename, int line, const char *condition)
82 {
83  mock_assert(false, condition ? condition : "", filename, line);
84  /* Keep compiler happy. Should not happen, mock_assert() does not return */
85  exit(1);
86 }
87 #else /* ifndef NO_CMOCKA */
88 void
89 assert_failed(const char *filename, int line, const char *condition)
90 {
91  msg(M_FATAL, "Assertion failed at %s:%d (%s)", filename, line, condition ? condition : "");
92  _exit(1);
93 }
94 #endif
95 
96 
97 /*
98  * Fail memory allocation. Don't use msg() because it tries
99  * to allocate memory as part of its operation.
100  */
101 void
103 {
104  fprintf(stderr, "Out of Memory\n");
105  exit(1);
106 }
107 
108 bool
109 dont_mute(unsigned int flags)
110 {
111  return true;
112 }
out_of_memory
void out_of_memory(void)
Definition: mock_msg.c:102
x_msg_va
void x_msg_va(const unsigned int flags, const char *format, va_list arglist)
Definition: mock_msg.c:57
M_FATAL
#define M_FATAL
Definition: error.h:89
fatal_error_triggered
bool fatal_error_triggered
Definition: mock_msg.c:42
errlevel.h
dont_mute
bool dont_mute(unsigned int flags)
Check muting filter.
Definition: mock_msg.c:109
x_debug_level
unsigned int x_debug_level
Definition: mock_msg.c:41
x_msg
void x_msg(const unsigned int flags, const char *format,...)
Definition: mock_msg.c:70
assert_failed
void assert_failed(const char *filename, int line, const char *condition)
Definition: mock_msg.c:81
get_debug_level
int get_debug_level(void)
Definition: mock_msg.c:51
config.h
mock_set_debug_level
void mock_set_debug_level(int level)
Mock debug level defaults to 0, which gives clean(-ish) test reports.
Definition: mock_msg.c:45
msg
#define msg(flags,...)
Definition: error.h:144