]> git.ipfire.org Git - thirdparty/openvpn.git/commitdiff
Ensure that all unit tests use unbuffered stdout and stderr
authorArne Schwabe <arne@rfc2549.org>
Tue, 23 Jan 2024 10:43:58 +0000 (11:43 +0100)
committerGert Doering <gert@greenie.muc.de>
Tue, 23 Jan 2024 15:34:50 +0000 (16:34 +0100)
stderr is normally always unbuffered but stdout can be buffered. Especially,
when stdout is redirected it will become buffered while it is normally
unbuffered when connected to a terminal. This mean that if the unit exits
prematurely, the output in the buffered output will be lost.

As the unit test x_msg mock implementation prints even fatal on stdout
we ensure with this setup method that stdout is also unbuffered.

Change-Id: I5c06dc13e9d8ab73997f79b13c30ee8949e5e993
Acked-by: Frank Lichtenheld <frank@lichtenheld.com>
Message-Id: <20240123104358.495517-1-frank@lichtenheld.com>
URL: https://www.mail-archive.com/openvpn-devel@lists.sourceforge.net/msg28122.html
Signed-off-by: Gert Doering <gert@greenie.muc.de>
14 files changed:
tests/unit_tests/openvpn/test_argv.c
tests/unit_tests/openvpn/test_auth_token.c
tests/unit_tests/openvpn/test_buffer.c
tests/unit_tests/openvpn/test_common.h [new file with mode: 0644]
tests/unit_tests/openvpn/test_crypto.c
tests/unit_tests/openvpn/test_cryptoapi.c
tests/unit_tests/openvpn/test_misc.c
tests/unit_tests/openvpn/test_ncp.c
tests/unit_tests/openvpn/test_packet_id.c
tests/unit_tests/openvpn/test_pkcs11.c
tests/unit_tests/openvpn/test_pkt.c
tests/unit_tests/openvpn/test_provider.c
tests/unit_tests/openvpn/test_ssl.c
tests/unit_tests/openvpn/test_tls_crypt.c

index 1b18ac0d4aa6bd27f66aa899d6b470b873df925d..33b3dec001bc29b24f60289e75b5b314d677e19f 100644 (file)
@@ -12,6 +12,7 @@
 
 #include "argv.h"
 #include "buffer.h"
+#include "test_common.h"
 
 /* Defines for use in the tests and the mock parse_line() */
 #define PATH1       "/s p a c e"
@@ -252,6 +253,7 @@ argv_insert_head__non_empty_argv__head_added(void **state)
 int
 main(void)
 {
+    openvpn_unit_test_setup();
     const struct CMUnitTest tests[] = {
         cmocka_unit_test(argv_printf__multiple_spaces_in_format__parsed_as_one),
         cmocka_unit_test(argv_printf_cat__multiple_spaces_in_format__parsed_as_one),
index a027330586436766ba36f548496165f4f0eace78..3a3cb69b5b00f0da93e65e37f621fc1ae29c8b5d 100644 (file)
@@ -35,6 +35,7 @@
 #include <cmocka.h>
 
 #include "auth_token.c"
+#include "test_common.h"
 
 struct test_context {
     struct tls_multi multi;
@@ -407,6 +408,7 @@ auth_token_test_key_load(void **state)
 int
 main(void)
 {
+    openvpn_unit_test_setup();
     const struct CMUnitTest tests[] = {
         cmocka_unit_test_setup_teardown(auth_token_basic_test, setup, teardown),
         cmocka_unit_test_setup_teardown(auth_token_fail_invalid_key, setup, teardown),
index ee84c1f6dd0886c831f3a89bad7bc423221dc117..52ffb540c5b71e852ddec6edb8de8f86ccf3f843 100644 (file)
@@ -32,6 +32,7 @@
 
 #include "buffer.h"
 #include "buffer.c"
+#include "test_common.h"
 
 static void
 test_buffer_strprefix(void **state)
@@ -356,6 +357,7 @@ test_character_class(void **state)
 int
 main(void)
 {
+    openvpn_unit_test_setup();
     const struct CMUnitTest tests[] = {
         cmocka_unit_test(test_buffer_strprefix),
         cmocka_unit_test(test_buffer_printf_catrunc),
diff --git a/tests/unit_tests/openvpn/test_common.h b/tests/unit_tests/openvpn/test_common.h
new file mode 100644 (file)
index 0000000..50e16d6
--- /dev/null
@@ -0,0 +1,40 @@
+/*
+ *  OpenVPN -- An application to securely tunnel IP networks
+ *             over a single UDP port, with support for SSL/TLS-based
+ *             session authentication and key exchange,
+ *             packet encryption, packet authentication, and
+ *             packet compression.
+ *
+ *  Copyright (C) 2016-2021 Fox Crypto B.V. <openvpn@foxcrypto.com>
+ *
+ *  This program is free software; you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License version 2
+ *  as published by the Free Software Foundation.
+ *
+ *  This program is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License along
+ *  with this program; if not, write to the Free Software Foundation, Inc.,
+ *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ */
+
+#include <stdio.h>
+#include <setjmp.h>
+#include <cmocka.h>
+
+/**
+ * Sets up the environment for unit tests like making both stderr and stdout
+ * non-buffered to avoid messages getting lost if the program exits early.
+ *
+ * This has a openvpn prefix to avoid confusion with cmocka's unit_test_setup_*
+ * methods
+ */
+void
+openvpn_unit_test_setup()
+{
+    assert_int_equal(setvbuf(stdout, NULL, _IONBF, BUFSIZ), 0);
+    assert_int_equal(setvbuf(stderr, NULL, _IONBF, BUFSIZ), 0);
+}
index 08c9c4431cf6e14f921284e8ffeeb32e5000f346..01c16c90b42cfa2dbd1d49e9a9d839537797c75b 100644 (file)
@@ -39,6 +39,7 @@
 #include "ssl_backend.h"
 
 #include "mss.h"
+#include "test_common.h"
 
 static const char testtext[] = "Dummy text to test PEM encoding";
 
@@ -445,6 +446,7 @@ test_mssfix_mtu_calculation(void **state)
 int
 main(void)
 {
+    openvpn_unit_test_setup();
     const struct CMUnitTest tests[] = {
         cmocka_unit_test(crypto_pem_encode_decode_loopback),
         cmocka_unit_test(crypto_translate_cipher_names),
index d90bfc35424069437e3b5de23be014a128e95d6e..e0479a8ced87c0cfb0cba80dad63f26b94b8a21b 100644 (file)
@@ -40,6 +40,7 @@
 #include <openssl/core_names.h>
 #include <openssl/evp.h>
 #include <openssl/pkcs12.h>
+#include "test_common.h"
 
 #include <cryptoapi.h>
 #include <cryptoapi.c> /* pull-in the whole file to test static functions */
@@ -486,6 +487,7 @@ test_parse_hexstring(void **state)
 int
 main(void)
 {
+    openvpn_unit_test_setup();
     const struct CMUnitTest tests[] = {
         cmocka_unit_test(test_parse_hexstring),
         cmocka_unit_test(import_certs),
index 43ae96b6a1cf7ac31f403967604e811eed4efcc8..193f13194284c7df36ca37683df2a88f55270f50 100644 (file)
@@ -36,6 +36,7 @@
 
 #include "ssl_util.h"
 #include "options_util.h"
+#include "test_common.h"
 
 static void
 test_compat_lzo_string(void **state)
@@ -117,5 +118,6 @@ const struct CMUnitTest misc_tests[] = {
 int
 main(void)
 {
+    openvpn_unit_test_setup();
     return cmocka_run_group_tests(misc_tests, NULL, NULL);
 }
index 72a1c3b09de5210864332714083942819331999d..ecf1e3998632a7531c13cc048ae152cf72ce99ba 100644 (file)
@@ -35,6 +35,7 @@
 #include <cmocka.h>
 
 #include "ssl_ncp.c"
+#include "test_common.h"
 
 /* Defines for use in the tests and the mock parse_line() */
 
@@ -272,6 +273,7 @@ const struct CMUnitTest ncp_tests[] = {
 int
 main(void)
 {
+    openvpn_unit_test_setup();
 #if defined(ENABLE_CRYPTO_OPENSSL)
     OpenSSL_add_all_algorithms();
 #endif
index 2a2a9736b6f837b7e863403926814d1d4f99990f..ff3f7886d11d54a2e02cd9993091bc45cfda5b7f 100644 (file)
@@ -35,6 +35,7 @@
 
 #include "packet_id.h"
 #include "reliable.h"
+#include "test_common.h"
 
 struct test_packet_id_write_data {
     struct {
@@ -273,6 +274,7 @@ test_copy_acks_to_lru(void **state)
 int
 main(void)
 {
+    openvpn_unit_test_setup();
     const struct CMUnitTest tests[] = {
         cmocka_unit_test_setup_teardown(test_packet_id_write_short,
                                         test_packet_id_write_setup,
index b6c130ece4dc4a85712b3274cd72b365971e6fd3..81cdf882d7d4ddc77665702b0172380583c0e654 100644 (file)
 
 #include <setjmp.h>
 #include <cmocka.h>
+#include "test_common.h"
 
 #define token_name "Test Token"
 #define PIN "12345"
 #define HASHSIZE 20
 
+
 struct management *management; /* global */
 
 /* replacement for crypto_print_openssl_errors() */
@@ -459,6 +461,7 @@ test_tls_ctx_use_pkcs11__management(void **state)
 int
 main(void)
 {
+    openvpn_unit_test_setup();
     const struct CMUnitTest tests[] = {
         cmocka_unit_test_setup_teardown(test_pkcs11_ids, setup_pkcs11,
                                         teardown_pkcs11),
index 7f0518414ad0ce777957abf1d004be1f21659d0a..1084d66d5987749814cb65b5645d769a3ba38479 100644 (file)
@@ -33,6 +33,7 @@
 #include <string.h>
 #include <setjmp.h>
 #include <cmocka.h>
+#include "test_common.h"
 
 #include "crypto.h"
 #include "options.h"
@@ -628,6 +629,7 @@ test_generate_reset_packet_tls_auth(void **ut_state)
 int
 main(void)
 {
+    openvpn_unit_test_setup();
     const struct CMUnitTest tests[] = {
         cmocka_unit_test(test_tls_decrypt_lite_none),
         cmocka_unit_test(test_tls_decrypt_lite_auth),
index 335fca2d3579210dcb41111c6e11835b4ccc8adc..94deab6f47e0b25ae596829fc4d06afbfa4f22ca 100644 (file)
@@ -40,6 +40,8 @@
 #include <openssl/core_names.h>
 #include <openssl/evp.h>
 
+#include "test_common.h"
+
 struct management *management; /* global */
 static int mgmt_callback_called;
 
@@ -119,6 +121,7 @@ load_pubkey(const char *pem)
 static void
 init_test()
 {
+    openvpn_unit_test_setup();
     prov[0] = OSSL_PROVIDER_load(NULL, "default");
     OSSL_PROVIDER_add_builtin(NULL, prov_name, xkey_provider_init);
     prov[1] = OSSL_PROVIDER_load(NULL, prov_name);
index d0c3df763afb3223e65d45c87536e01f87cea8a1..18b9ec8431435e16138d69b3027eaa582b309caf 100644 (file)
@@ -43,6 +43,7 @@
 #include "mss.h"
 #include "ssl_verify_backend.h"
 #include "win32.h"
+#include "test_common.h"
 
 /* Mock function to be allowed to include win32.c which is required for
  * getting the temp directory */
@@ -122,6 +123,8 @@ crypto_pem_encode_certificate(void **state)
 int
 main(void)
 {
+    openvpn_unit_test_setup();
+
     const struct CMUnitTest tests[] = {
         cmocka_unit_test(crypto_pem_encode_certificate)
     };
index 465543a740ea7cae4e41b8f6f96057c7556f2685..a01fbe504303223b44ccdc11902c822686e93b1e 100644 (file)
@@ -34,6 +34,7 @@
 #include <setjmp.h>
 #include <cmocka.h>
 
+#include "test_common.h"
 #include "tls_crypt.c"
 
 /* Define this function here as dummy since including the ssl_*.c files
@@ -673,6 +674,7 @@ test_tls_crypt_v2_write_client_key_file_metadata(void **state)
 int
 main(void)
 {
+    openvpn_unit_test_setup();
     const struct CMUnitTest tests[] = {
         cmocka_unit_test_setup_teardown(tls_crypt_loopback,
                                         test_tls_crypt_setup,