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 #elif defined(_MSC_VER)
27 #include "config-msvc.h"
28 #endif
29 
30 #include <stdarg.h>
31 #include <stddef.h>
32 #include <stdio.h>
33 #include <stdlib.h>
34 #include <setjmp.h>
35 #include <cmocka.h>
36 
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
mock_set_debug_level(int level)45 mock_set_debug_level(int level)
46 {
47     x_debug_level = level;
48 }
49 
50 void
x_msg_va(const unsigned int flags,const char * format,va_list arglist)51 x_msg_va(const unsigned int flags, const char *format,
52          va_list arglist)
53 {
54     if (flags & M_FATAL)
55     {
56         fatal_error_triggered = true;
57         printf("FATAL ERROR:");
58     }
59     vprintf(format, arglist);
60     printf("\n");
61 }
62 
63 void
x_msg(const unsigned int flags,const char * format,...)64 x_msg(const unsigned int flags, const char *format, ...)
65 {
66     va_list arglist;
67     va_start(arglist, format);
68     x_msg_va(flags, format, arglist);
69     va_end(arglist);
70 }
71 
72 void
assert_failed(const char * filename,int line,const char * condition)73 assert_failed(const char *filename, int line, const char *condition)
74 {
75     mock_assert(false, condition ? condition : "", filename, line);
76     /* Keep compiler happy.  Should not happen, mock_assert() does not return */
77     exit(1);
78 }
79 
80 /*
81  * Fail memory allocation.  Don't use msg() because it tries
82  * to allocate memory as part of its operation.
83  */
84 void
out_of_memory(void)85 out_of_memory(void)
86 {
87     fprintf(stderr, "Out of Memory\n");
88     exit(1);
89 }
90 
91 bool
dont_mute(unsigned int flags)92 dont_mute(unsigned int flags)
93 {
94     return true;
95 }
96