test.cxx
Go to the documentation of this file.
1 // =============================================================================
2 //! \file
3 //! \brief Test framework
4 //!
5 //! Copyright (c) 2012-2020 by the developers. See the LICENSE file for details.
6 
7 
8 // =============================================================================
9 // Include headers
10 
11 // C++98
12 #include <cstdlib>
13 #include <iostream>
14 
15 // Local
16 extern "C"
17 {
18 #include "conf.h"
19 #include "config.h"
20 #include "digest.h"
21 #include "hmac.h"
22 #include "test.h"
23 #include "test_base64.h"
24 #include "test_cancellock.h"
25 #include "test_compression.h"
26 #include "test_inet_pton.h"
27 #include "test_regex.h"
28 #include "test_snprintf.h"
29 #include "test_strcasecmp_l.h"
30 #include "test_timestamp.h"
31 #include "test_unicode.h"
32 }
33 #include "compression.hxx"
34 #include "tls.hxx"
35 
36 
37 // =============================================================================
38 //! \defgroup TEST TEST: Testsuite
39 //!
40 //! The tests can be executed with "make test".
41 //! @{
42 
43 
44 // =============================================================================
45 // Macros
46 
47 //! \brief Check return value of test
48 #define CHECK_RV(rv) \
49 { \
50  std::clog << std::flush; \
51  if(rv) \
52  { \
53  std::cout << "A problem was detected!" << std::endl << std::flush; \
54  } \
55  else { std::cout << "OK" << std::endl << std::flush; } \
56  if(!res && rv) { res = rv; } \
57 }
58 
59 
60 //! \brief Skip test and print info message
61 #define SKIP_TEST(s) \
62 { \
63  std::cout << "Skipped (" << s << ")" << std::endl << std::flush; \
64 }
65 
66 
67 // =============================================================================
68 // Variables
69 
70 extern "C"
71 {
72 //! Enable additional debug output if nonzero
73 int main_debug = 0;
74 
75 //! Configuration directory path from command line option (always \c NULL )
76 const char* main_confprefix = NULL;
77 }
78 
79 static int first_error = 1;
80 
81 
82 // =============================================================================
83 // Forward declarations (do not include main.h)
84 
85 extern "C"
86 {
87 void ts_environ_init(void);
88 void ts_environ_exit(void);
89 }
90 
91 
92 // =============================================================================
93 //! \brief Print error message
94 //!
95 //! Exported as C style function
96 //!
97 //! \param[in] msg Message to display on stderr
98 
99 void print_error(const char* msg)
100 {
101  std::clog << std::flush;
102  std::cout << std::flush;
103 
104  if(first_error)
105  {
106  std::cout << "Failed" << std::endl << std::flush;
107  first_error = 0;
108  }
109 
110  std::clog << TEST_TAB << msg << std::endl << std::flush;
111 }
112 
113 
114 // =============================================================================
115 //! \brief Test entry point
116 //!
117 //! \param[in] argc Number of command line arguments
118 //! \param[in] argv Array containing command line argument strings
119 //!
120 //! Exit status of program:
121 //! - \c EXIT_SUCCESS on success
122 //! - \c EXIT_FAILURE on error
123 
124 int main(int argc, char** argv)
125 {
126  int res = EXIT_SUCCESS;
127  int rv;
128 
129  ts_environ_init();
130 
131  // Load configuration
132  first_error = 1;
133  std::cout << "Load configuration ... " << std::flush;
134  rv = conf_load(config);
135  CHECK_RV(rv);
136  if(res)
137  {
138  goto exit;
139  }
140 
141  // Initialize modules
142 #if !CFG_CMPR_DISABLE
143  if(0 > cmpr_init()) { res = EXIT_FAILURE; }
144 #endif // !CFG_CMPR_DISABLE
145 #if CFG_USE_TLS
146  if(0 > tls_init()) { res = EXIT_FAILURE; }
147 #endif // CFG_USE_TLS
148  digest_init();
149  hmac_init();
150  if(EXIT_FAILURE == res)
151  {
152  std::clog << "Initialization of modules failed" << std::endl;
153  }
154  else
155  {
156  std::cout << "========================================"
157  "========================================" << std::endl;
158 
159  // Test strcasecmp_l()
160  first_error = 1;
161  std::cout << "Test 'posix_strcasecmp_l()' ... " << std::flush;
162  rv = test_strcasecmp_l();
163  CHECK_RV(rv);
164 
165  // Test snprintf()
166  first_error = 1;
167  std::cout << "Test 'posix_snprintf()' ... " << std::flush;
168  rv = test_snprintf();
169  CHECK_RV(rv);
170 
171  // Test inet_pton()
172  first_error = 1;
173  std::cout << "Test 'posix_inet_pton()' ... " << std::flush;
174  rv = test_inet_pton();
175  CHECK_RV(rv);
176 
177  // Test regcomp() and regexec()
178  first_error = 1;
179  std::cout << "Test 'posix_regcomp()' and 'posix_regexec()' ... "
180  << std::flush;
181 #if CFG_USE_CLB || CFG_USE_XSI
182  rv = test_regex();
183  CHECK_RV(rv);
184 #else // CFG_USE_CLB || CFG_USE_XSI
185  SKIP_TEST("no CLB or X/Open XSI");
186 #endif // CFG_USE_CLB || CFG_USE_XSI
187 
188  // Test Cancel-Lock implementation
189  // Note: Test base64 encoder first because it is used here
190  first_error = 1;
191  std::cout << "Test 'core_get_cancel_lock()' ... " << std::flush;
192 #if CFG_USE_TLS
193  rv = test_cancellock();
194  CHECK_RV(rv);
195 #else // CFG_USE_TLS
196  // Cryptographic functions are shared with TLS module
197  SKIP_TEST("no TLS support");
198 #endif // CFG_USE_TLS
199 
200  // Test compression
201  first_error = 1;
202  std::cout << "Test 'cmpr_send()' and 'cmpr_recv()' ... " << std::flush;
203 #if !CFG_CMPR_DISABLE
204 # if CFG_USE_ZLIB && (CFG_USE_POSIX_API >= 200112 || CFG_USE_XSI)
205  rv = test_compression();
206  CHECK_RV(rv);
207 # else // CFG_USE_ZLIB && (CFG_USE_POSIX_API >= 200112 || CFG_USE_XSI)
208 # if ! (CFG_USE_POSIX_API >= 200112 || CFG_USE_XSI)
209  SKIP_TEST("no POSIX.1-2001 or X/Open XSI");
210 # else // ! (CFG_USE_POSIX_API >= 200112 || CFG_USE_XSI)
211  SKIP_TEST("no ZLIB support");
212 # endif // ! (CFG_USE_POSIX_API >= 200112 || CFG_USE_XSI)
213 # endif // CFG_USE_ZLIB && (CFG_USE_POSIX_API >= 200112 || CFG_USE_XSI)
214 #else // ! CFG_CMPR_DISABLE
215  SKIP_TEST("disabled by configuration");
216 #endif // ! CFG_CMPR_DISABLE
217 
218  // Test timestamp decode
219  first_error = 1;
220  std::cout << "Test 'enc_timestamp_decode()' ... " << std::flush;
221  rv = test_timestamp();
222  CHECK_RV(rv);
223 
224  // Test base64 encoder
225  first_error = 1;
226  std::cout << "Test 'enc_mime_encode_base64()' ... " << std::flush;
227  rv = test_base64();
228  CHECK_RV(rv);
229 
230  // Test Unicode normalization (this is slow => execute at the end)
231  first_error = 1;
232  std::cout << "Test 'enc_convert_to_utf8_nfc()' ... " << std::flush;
233  rv = test_unicode();
234  CHECK_RV(rv);
235 
236  std::cout << "========================================"
237  "========================================" << std::endl;
238  }
239 
240  // Shutdown modules
241  hmac_exit();
242  digest_exit();
243 #if CFG_USE_TLS
244  tls_exit();
245 #endif // CFG_USE_TLS
246 #if CFG_USE_ZLIB
247  cmpr_exit();
248 #endif // CFG_USE_ZLIB
249 
250  // Destroy configuration data
252 
253  // Exit
254  if(res)
255  {
256  std::clog << "Error: One or more tests have failed!" << std::endl;
257  }
258 exit:
259  ts_environ_exit();
260  exit(res);
261 }
262 
263 
264 //! @}
265 
266 
267 // =============================================================================
268 // Lock UI thread stub
269 //
270 // Exported as C style function
271 //
272 // The UI is not used for testing.
273 //
274 // \return
275 // - 0 (NOP and success)
276 
277 int ts_lock_ui(void)
278 {
279  return(0);
280 }
281 
282 
283 // =============================================================================
284 // Unlock UI thread stub
285 //
286 // Exported as C style function
287 //
288 // The UI is not used for testing.
289 //
290 // \return
291 // - 0 (NOP and success)
292 
293 int ts_unlock_ui(void)
294 {
295  return(0);
296 }
297 
298 
299 // =============================================================================
300 // Wakeup UI thread stub
301 //
302 // Exported as C style function
303 //
304 // The UI is not used for testing.
305 
306 void ui_wakeup(unsigned int)
307 {
308  return;
309 }
310 
311 
312 // EOF
tls_exit
void tls_exit(void)
Shutdown TLS subsystem.
Definition: tls.c:1813
TEST_TAB
#define TEST_TAB
Tabulator to indent messages from test programs.
Definition: test.h:13
test_base64
int test_base64(void)
Test enc_mime_encode_base64() implementation.
Definition: test_base64.c:39
ui_wakeup
void ui_wakeup(unsigned int)
Wakeup callback (called by core thread after operation has finished)
Definition: test.cxx:306
print_error
void print_error(const char *msg)
Print error message.
Definition: test.cxx:99
digest_exit
void digest_exit(void)
Shutdown message digest module.
Definition: digest.c:200
SKIP_TEST
#define SKIP_TEST(s)
Skip test and print info message.
Definition: test.cxx:61
test_unicode
int test_unicode(void)
Test enc_convert_to_utf8_nfc() implementation.
Definition: test_unicode.c:347
main_confprefix
const char * main_confprefix
Configuration directory path from command line option (always NULL )
Definition: test.cxx:76
config
struct conf config[CONF_NUM]
Global configuration.
Definition: conf.c:63
test_cancellock
int test_cancellock(void)
Test core_get_cancel_lock() implementation.
Definition: test_cancellock.c:38
hmac_exit
void hmac_exit(void)
Shutdown HMAC module.
Definition: hmac.c:269
test_inet_pton
int test_inet_pton(void)
Test posix_inet_pton() implementation.
Definition: test_inet_pton.c:40
hmac_init
void hmac_init(void)
Initialize HMAC module.
Definition: hmac.c:260
test_snprintf
int test_snprintf(void)
Test posix_snprintf() implementation.
Definition: test_snprintf.c:50
tls_init
int tls_init(void)
Init TLS subsystem.
Definition: tls.c:1762
CHECK_RV
#define CHECK_RV(rv)
Check return value of test.
Definition: test.cxx:48
main_debug
int main_debug
Enable additional debug output if nonzero.
Definition: test.cxx:73
conf_delete
void conf_delete(struct conf *cfg)
Delete configuration.
Definition: conf.c:726
test_timestamp
int test_timestamp(void)
Test enc_timestamp_decode() implementation.
Definition: test_timestamp.c:44
cmpr_exit
void cmpr_exit(void)
Shutdown compress module.
Definition: compression.c:870
cmpr_init
int cmpr_init(void)
Initialize compression module.
Definition: compression.c:821
ts_unlock_ui
int ts_unlock_ui(void)
Unlock UI thread.
Definition: test.cxx:293
conf_load
int conf_load(struct conf *cfg)
Load configuration from config file.
Definition: conf.c:762
test_strcasecmp_l
int test_strcasecmp_l(void)
Test posix_strcasecmp_l() implementation.
Definition: test_strcasecmp_l.c:44
test_compression
int test_compression(void)
Test cmpr_send() and cmpr_recv() implementation.
Definition: test_compression.c:82
main
int main(int argc, char **argv)
Test entry point.
Definition: test.cxx:124
digest_init
void digest_init(void)
Initialize message digest module.
Definition: digest.c:191
ts_environ_exit
void ts_environ_exit(void)
Destroy copy of environment variables.
Definition: ts_functions.c:107
ts_lock_ui
int ts_lock_ui(void)
Lock UI thread.
Definition: test.cxx:277
ts_environ_init
void ts_environ_init(void)
Copy environment variables.
Definition: ts_functions.c:58
test_regex
int test_regex(void)
Test posix_regcomp() and posix_regexec() implementation.
Definition: test_regex.c:79

Generated at 2024-04-27 using  doxygen