blob: 85e6d9b6c5627be7a95c9d924740e754c53b91dd [file] [log] [blame]
Brian Waters13d96012017-12-08 16:53:31 -06001/*********************************************************************************************************
2* Software License Agreement (BSD License) *
3* Author: Sebastien Decugis <sdecugis@freediameter.net> *
4* *
5* Copyright (c) 2013, WIDE Project and NICT *
6* All rights reserved. *
7* *
8* Redistribution and use of this software in source and binary forms, with or without modification, are *
9* permitted provided that the following conditions are met: *
10* *
11* * Redistributions of source code must retain the above *
12* copyright notice, this list of conditions and the *
13* following disclaimer. *
14* *
15* * Redistributions in binary form must reproduce the above *
16* copyright notice, this list of conditions and the *
17* following disclaimer in the documentation and/or other *
18* materials provided with the distribution. *
19* *
20* * Neither the name of the WIDE Project or NICT nor the *
21* names of its contributors may be used to endorse or *
22* promote products derived from this software without *
23* specific prior written permission of WIDE Project and *
24* NICT. *
25* *
26* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED *
27* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A *
28* PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR *
29* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT *
30* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS *
31* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR *
32* TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF *
33* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. *
34*********************************************************************************************************/
35
36/* This file contains the definition of our test harness.
37 * The harness is very simple yet.
38 * It may be interessant to go to dejagnu later...
39 *
40 */
41#ifndef _TESTS_H
42#define _TESTS_H
43
44#include "fdproto-internal.h"
45#include "fdcore-internal.h"
46
47#include <pthread.h>
48#include <errno.h>
49GCC_DIAG_OFF("-Wdeprecated-declarations")
50#include <gcrypt.h>
51GCC_DIAG_ON("-Wdeprecated-declarations")
52
53/* Test timeout duration, unless -n is passed on the command line */
54#ifndef TEST_TIMEOUT
55#define TEST_TIMEOUT 120 /* in seconds */
56#endif /* TEST_TIMEOUT */
57
58/* Standard includes */
59#include <getopt.h>
60#include <time.h>
61#include <libgen.h>
62#include <signal.h>
63
64/* Define the return code values */
65#define PASS 0
66#define FAIL 1
67
68/* Define the macro to fail a test with a message */
69#define FAILTEST( message... ){ \
70 LOG_F(message); \
71 LOG_F("FAILED: %s ", __STRIPPED_FILE__); \
72 free(tbuf); \
73 exit(FAIL); \
74}
75
76/* Define the macro to pass a test */
77#define PASSTEST( ){ \
78 LOG_N("PASS: %s", __STRIPPED_FILE__); \
79 (void)fd_core_shutdown(); \
80 (void)fd_core_wait_shutdown_complete(); \
81 (void)fd_thr_term(&signal_thr); \
82 free(tbuf); \
83 exit(PASS); \
84}
85
86static struct fd_config conf;
87extern struct fd_config * fd_g_config;
88
89/* for dumps */
90static char * tbuf = NULL; size_t tbuflen = 0;
91#define FD_DUMP_TEST_PARAMS &tbuf, &tbuflen, NULL
92
93
94/* Define the standard check routines */
95#define CHECK( _val, _assert ){ \
96 LOG_D("CHECK( %s == %s )", \
97 #_assert, \
98 #_val); \
99 { \
100 __typeof__ (_val) __ret = (_assert); \
101 if (__ret != (_val)) { \
102 FAILTEST( "%s:%d: CHECK FAILED : %s == %lx != %lx", \
103 __STRIPPED_FILE__, \
104 __LINE__, \
105 #_assert, \
106 (unsigned long)__ret, \
107 (unsigned long)(_val)); \
108 }} \
109}
110
111static pthread_t signal_thr;
112static void * signal_catch(void * arg)
113{
114 int sig;
115 sigset_t ss;
116 fd_log_threadname ( "Signal catcher" );
117
118 sigemptyset(&ss);
119
120 /* We use SIGALRM */
121 sigaddset(&ss, SIGALRM);
122
123 /* Unblock any other signal for this thread, so that default handler is enabled */
124 CHECK_SYS_DO( pthread_sigmask( SIG_SETMASK, &ss, NULL ), );
125
126 /* Now wait for sigwait or cancelation */
127 CHECK_POSIX_DO( sigwait(&ss, &sig), );
128 FAILTEST("The timeout (" _stringize(TEST_TIMEOUT) " sec) was reached. Use -n or change TEST_TIMEOUT if the test needs more time to execute.");
129
130 return NULL;
131}
132
133
134GCRY_THREAD_OPTION_PTHREAD_IMPL;
135
136/* gnutls debug. */
137static void fd_gnutls_debug(int level, const char * str) {
138 const char * __thn = ((char *)pthread_getspecific(fd_log_thname) ?: "unnamed");
139 fd_log_debug("tid:%-20s[gnutls:%d] %s", __thn, level, str);
140}
141static int gnutls_debug = 0;
142
143static int test_parameter = 0;
144
145static inline void parse_cmdline(int argc, char * argv[]) {
146 int c;
147 int no_timeout = 0;
148 while ((c = getopt (argc, argv, "dqnf:F:g:p:")) != -1) {
149 switch (c) {
150 case 'd': /* Increase verbosity of debug messages. */
151 fd_g_debug_lvl--;
152 break;
153
154 case 'q': /* Decrease verbosity. */
155 fd_g_debug_lvl++;
156 break;
157
158 case 'n': /* Disable the timeout of the test. */
159 no_timeout = 1;
160 break;
161
162 case 'f': /* Full debug for the function with this name. */
163 #ifdef DEBUG
164 fd_debug_one_function = optarg;
165 #else /* DEBUG */
166 TRACE_DEBUG(INFO, "Error: must compile with DEBUG support to use this feature");
167 #endif /* DEBUG */
168 break;
169
170 case 'F': /* Full debug for the functions in file with this name. */
171 #ifdef DEBUG
172 fd_debug_one_file = optarg;
173 #else /* DEBUG */
174 TRACE_DEBUG(INFO, "Error: must compile with DEBUG support to use this feature");
175 #endif /* DEBUG */
176 break;
177
178 case 'g': /* Set a debug level and function for GNU TLS calls. */
179 gnutls_debug = (int)atoi(optarg);
180 break;
181
182 case 'p': /* Set a debug level and function for GNU TLS calls. */
183 test_parameter = (int)atoi(optarg);
184 break;
185
186 default: /* bug: option not considered. */
187 return;
188 }
189 }
190 if (!no_timeout) {
191 alarm(TEST_TIMEOUT);
192 }
193 CHECK( 0, pthread_create(&signal_thr, NULL, signal_catch, NULL) );
194}
195
196static inline void test_init(int argc, char * argv[], char *fname)
197{
198 sigset_t sig_all;
199 sigfillset(&sig_all);
200
201 CHECK( 0, pthread_sigmask(SIG_BLOCK, &sig_all, NULL));
202
203 fd_g_config = &conf;
204 memset(fd_g_config, 0, sizeof(struct fd_config));
205
206 CHECK( 0, fd_libproto_init() );
207
208 CHECK( 0, fd_hooks_init() );
209
210 fd_log_threadname(fname);
211
212 /* Parse the command line */
213 parse_cmdline(argc, argv);
214
215 /* Initialize gcrypt and gnutls */
216 (void) gcry_control (GCRYCTL_SET_THREAD_CBS, &gcry_threads_pthread);
217 (void) gcry_control (GCRYCTL_ENABLE_QUICK_RANDOM, 0);
218 CHECK( 0, gnutls_global_init());
219 /* Set gnutls debug level ? */
220 if (gnutls_debug) {
221 gnutls_global_set_log_function((gnutls_log_func)fd_gnutls_debug);
222 gnutls_global_set_log_level (gnutls_debug);
223 TRACE_DEBUG(INFO, "Enabled GNUTLS debug at level %d", gnutls_debug);
224 }
225
226 /* Initialize the config */
227 CHECK( 0, fd_conf_init() );
228
229 /* Add definitions of the base protocol */
230 CHECK( 0, fd_dict_base_protocol(fd_g_config->cnf_dict) );
231
232 /* Initialize only the sessions */
233 CHECK( 0, fd_sess_start() );
234
235 return;
236}
237#define INIT_FD() test_init(argc, argv, __STRIPPED_FILE__)
238
239#endif /* _TESTS_H */