From patchwork Mon Jan 22 13:27:48 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Gert Doering X-Patchwork-Id: 3587 Return-Path: Delivered-To: patchwork@openvpn.net Received: by 2002:a05:7301:2802:b0:101:b91e:57cc with SMTP id hj2csp2573145dyb; Mon, 22 Jan 2024 05:28:28 -0800 (PST) X-Google-Smtp-Source: AGHT+IHA/09OItOSa+MiGNPdZ2SzfkxHRg9g70xiA1P47iQumoEP9rYiyBsVHuULvuwtW46Bb2FP X-Received: by 2002:a5d:8e0b:0:b0:7bf:4758:2a12 with SMTP id e11-20020a5d8e0b000000b007bf47582a12mr6096597iod.0.1705930108713; Mon, 22 Jan 2024 05:28:28 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1705930108; cv=none; d=google.com; s=arc-20160816; b=eCvWLMXRIL/MO6n7uqtYQpD7+nn8WWXEZVUxdn86dZrMSY95gFtoAgMsm523TmKCTj rPHxvWyQbETfzO3YVy7FfcFmNggMcwZUT+JmZa/4NIWp7gby8565RruR0W0unznZ+pwa blsYzo6z9gkxnYTLEqYfxwvLDZ9NDNRF9OvU3O1hV2L1heIYFmDXAMM2Bg/gsBk0BTLI bTXLf0eNjGidsR5XQ3MTzJj31Z36MLK43XdNbpZ7JcNTUp7w3TKeAGmd6za50trmxcJp lE0Mm0c7luHZL30Ywo9AnFxZqJ2bAF7kJrSz8LC846hXLQppM1BGSGUhlZQzTjHyoXsh DgNg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=errors-to:content-transfer-encoding:list-subscribe:list-help :list-post:list-archive:list-unsubscribe:list-id:precedence:subject :mime-version:references:in-reply-to:message-id:date:to:from :dkim-signature:dkim-signature; bh=x5JWoMIoAN28NeyU3t2g7HukRcwpligfIHEe/wdcHIc=; fh=4NbAC/LsuMLI0S0hprUlLSLCiHwg6SCAifhH718Jh0Q=; b=I8ukEcKWHzsV99/MHK8SjNfYQBngvB9fHbwYeb90lGOjtx3R8c0rq9Ct54ocr7wrh4 2qfvflGLOgKggz6WeuiIpbEbjjQ2No/7Iu/MP54lyou98ILKI3CUt0oQAToIvmyJVk2f Wh/hVZjG6lQV9BNt0RNIwiDCFge+L3x76hDD+hq/c8f+8tITz1ySmf9CYxVRUEI6te3U iqqM6dY4CD87hakULz1UAEVa0nwLM2N4EqvPWTWejdjBx36KjphA11O75FeJZCLOTuau TuvwO75ouDu5Tv2JBGr5pb2R4Akb9xOFtsYJKSyRMG2UiU6304oA1V3cwxDwE6X/szj/ lSog== ARC-Authentication-Results: i=1; mx.google.com; dkim=neutral (body hash did not verify) header.i=@sourceforge.net header.s=x header.b=TV3LPSMx; dkim=neutral (body hash did not verify) header.i=@sf.net header.s=x header.b="TQ/Tvjrr"; spf=pass (google.com: domain of openvpn-devel-bounces@lists.sourceforge.net designates 216.105.38.7 as permitted sender) smtp.mailfrom=openvpn-devel-bounces@lists.sourceforge.net; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=muc.de Received: from lists.sourceforge.net (lists.sourceforge.net. [216.105.38.7]) by mx.google.com with ESMTPS id c10-20020a65420a000000b005cf0abf8e79si7885958pgq.275.2024.01.22.05.28.28 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Mon, 22 Jan 2024 05:28:28 -0800 (PST) Received-SPF: pass (google.com: domain of openvpn-devel-bounces@lists.sourceforge.net designates 216.105.38.7 as permitted sender) client-ip=216.105.38.7; Authentication-Results: mx.google.com; dkim=neutral (body hash did not verify) header.i=@sourceforge.net header.s=x header.b=TV3LPSMx; dkim=neutral (body hash did not verify) header.i=@sf.net header.s=x header.b="TQ/Tvjrr"; spf=pass (google.com: domain of openvpn-devel-bounces@lists.sourceforge.net designates 216.105.38.7 as permitted sender) smtp.mailfrom=openvpn-devel-bounces@lists.sourceforge.net; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=muc.de Received: from [127.0.0.1] (helo=sfs-ml-2.v29.lw.sourceforge.com) by sfs-ml-2.v29.lw.sourceforge.com with esmtp (Exim 4.95) (envelope-from ) id 1rRuLM-00011e-EI; Mon, 22 Jan 2024 13:28:04 +0000 Received: from [172.30.20.202] (helo=mx.sourceforge.net) by sfs-ml-2.v29.lw.sourceforge.com with esmtps (TLS1.2) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.95) (envelope-from ) id 1rRuLJ-00011Q-VK for openvpn-devel@lists.sourceforge.net; Mon, 22 Jan 2024 13:28:02 +0000 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=sourceforge.net; s=x; h=Content-Transfer-Encoding:MIME-Version:References: In-Reply-To:Message-ID:Date:Subject:To:From:Sender:Reply-To:Cc:Content-Type: Content-ID:Content-Description:Resent-Date:Resent-From:Resent-Sender: Resent-To:Resent-Cc:Resent-Message-ID:List-Id:List-Help:List-Unsubscribe: List-Subscribe:List-Post:List-Owner:List-Archive; bh=w9TRze8OO5jBSJLrJLYcuE2HWlxzcnccgTZw6tUkPuU=; b=TV3LPSMxZcv3AKDBT3029Npgoq 2owP5S7e/5OMryMah9kwgX5BlkFkiEY8EmerhShAWcJdvzX9TIZlzK4wr1NNCiptUspb56jLG8uPM xz8YLqOwvIe95hO2teNcm9i8FRcI6ZAM6p214Dhy6kYRrqANzqqJPxeONVeNxmhxqV9I=; DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=sf.net; s=x ; h=Content-Transfer-Encoding:MIME-Version:References:In-Reply-To:Message-ID: Date:Subject:To:From:Sender:Reply-To:Cc:Content-Type:Content-ID: Content-Description:Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc :Resent-Message-ID:List-Id:List-Help:List-Unsubscribe:List-Subscribe: List-Post:List-Owner:List-Archive; bh=w9TRze8OO5jBSJLrJLYcuE2HWlxzcnccgTZw6tUkPuU=; b=TQ/TvjrrppLXIP4xQfZi20fkwM n3J1QKUxJ8i7rahPSoc8STlNDEIcrssIi24l0tBTTVGBcH0X7GnZQlK0pQwT2DEZCDSrHzyCmbna+ KOowTY6jh+0Uu+3Fm8XyQtsilCgleR3JsFgdDQY+gg9Ahl09x0xQkDr8ct/WOsRQB7Y4=; Received: from dhcp-174.greenie.muc.de ([193.149.48.174] helo=blue.greenie.muc.de) by sfi-mx-2.v28.lw.sourceforge.com with esmtps (TLS1.2:ECDHE-RSA-AES256-GCM-SHA384:256) (Exim 4.95) id 1rRuLI-0004TF-7E for openvpn-devel@lists.sourceforge.net; Mon, 22 Jan 2024 13:28:02 +0000 Received: from blue.greenie.muc.de (localhost [127.0.0.1]) by blue.greenie.muc.de (8.17.1.9/8.17.1.9) with ESMTP id 40MDRnDd022551 for ; Mon, 22 Jan 2024 14:27:49 +0100 Received: (from gert@localhost) by blue.greenie.muc.de (8.17.1.9/8.17.1.9/Submit) id 40MDRnrG022550 for openvpn-devel@lists.sourceforge.net; Mon, 22 Jan 2024 14:27:49 +0100 From: Gert Doering To: openvpn-devel@lists.sourceforge.net Date: Mon, 22 Jan 2024 14:27:48 +0100 Message-ID: <20240122132748.22541-1-gert@greenie.muc.de> X-Mailer: git-send-email 2.43.0 In-Reply-To: References: MIME-Version: 1.0 X-Spam-Score: -0.0 (/) X-Spam-Report: Spam detection software, running on the system "util-spamd-1.v13.lw.sourceforge.com", has NOT identified this incoming email as spam. The original message has been attached to this so you can view it or label similar future email. If you have any questions, see the administrator of that system for details. Content preview: From: Arne Schwabe 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 t [...] Content analysis details: (-0.0 points, 6.0 required) pts rule name description ---- ---------------------- -------------------------------------------------- -0.0 SPF_PASS SPF: sender matches SPF record -0.0 SPF_HELO_PASS SPF: HELO matches SPF record -0.0 T_SCC_BODY_TEXT_LINE No description available. X-Headers-End: 1rRuLI-0004TF-7E Subject: [Openvpn-devel] [PATCH v3] Ensure that all unit tests use unbuffered stdout and stderr X-BeenThere: openvpn-devel@lists.sourceforge.net X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: openvpn-devel-bounces@lists.sourceforge.net X-getmail-retrieved-from-mailbox: Inbox X-GMAIL-THRID: =?utf-8?q?1788797369366146813?= X-GMAIL-MSGID: =?utf-8?q?1788797369366146813?= From: Arne Schwabe 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 --- This change was reviewed on Gerrit and approved by at least one developer. I request to merge it to master. Gerrit URL: https://gerrit.openvpn.net/c/openvpn/+/504 This mail reflects revision 3 of this Change. Acked-by according to Gerrit (reflected above): Frank Lichtenheld diff --git a/tests/unit_tests/openvpn/test_argv.c b/tests/unit_tests/openvpn/test_argv.c index 1b18ac0..33b3dec 100644 --- a/tests/unit_tests/openvpn/test_argv.c +++ b/tests/unit_tests/openvpn/test_argv.c @@ -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 @@ 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), diff --git a/tests/unit_tests/openvpn/test_auth_token.c b/tests/unit_tests/openvpn/test_auth_token.c index a027330..3a3cb69 100644 --- a/tests/unit_tests/openvpn/test_auth_token.c +++ b/tests/unit_tests/openvpn/test_auth_token.c @@ -35,6 +35,7 @@ #include #include "auth_token.c" +#include "test_common.h" struct test_context { struct tls_multi multi; @@ -407,6 +408,7 @@ 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), diff --git a/tests/unit_tests/openvpn/test_buffer.c b/tests/unit_tests/openvpn/test_buffer.c index ee84c1f..52ffb54 100644 --- a/tests/unit_tests/openvpn/test_buffer.c +++ b/tests/unit_tests/openvpn/test_buffer.c @@ -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 @@ 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 index 0000000..ee211bd --- /dev/null +++ b/tests/unit_tests/openvpn/test_common.h @@ -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. + * + * 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 +#include +#include + +/** + * 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); +} diff --git a/tests/unit_tests/openvpn/test_crypto.c b/tests/unit_tests/openvpn/test_crypto.c index 08c9c44..01c16c9 100644 --- a/tests/unit_tests/openvpn/test_crypto.c +++ b/tests/unit_tests/openvpn/test_crypto.c @@ -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 @@ 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), diff --git a/tests/unit_tests/openvpn/test_cryptoapi.c b/tests/unit_tests/openvpn/test_cryptoapi.c index d90bfc3..e0479a8 100644 --- a/tests/unit_tests/openvpn/test_cryptoapi.c +++ b/tests/unit_tests/openvpn/test_cryptoapi.c @@ -40,6 +40,7 @@ #include #include #include +#include "test_common.h" #include #include /* pull-in the whole file to test static functions */ @@ -486,6 +487,7 @@ int main(void) { + openvpn_unit_test_setup(); const struct CMUnitTest tests[] = { cmocka_unit_test(test_parse_hexstring), cmocka_unit_test(import_certs), diff --git a/tests/unit_tests/openvpn/test_misc.c b/tests/unit_tests/openvpn/test_misc.c index 43ae96b..193f131 100644 --- a/tests/unit_tests/openvpn/test_misc.c +++ b/tests/unit_tests/openvpn/test_misc.c @@ -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 @@ int main(void) { + openvpn_unit_test_setup(); return cmocka_run_group_tests(misc_tests, NULL, NULL); } diff --git a/tests/unit_tests/openvpn/test_ncp.c b/tests/unit_tests/openvpn/test_ncp.c index 72a1c3b..ecf1e39 100644 --- a/tests/unit_tests/openvpn/test_ncp.c +++ b/tests/unit_tests/openvpn/test_ncp.c @@ -35,6 +35,7 @@ #include #include "ssl_ncp.c" +#include "test_common.h" /* Defines for use in the tests and the mock parse_line() */ @@ -272,6 +273,7 @@ int main(void) { + openvpn_unit_test_setup(); #if defined(ENABLE_CRYPTO_OPENSSL) OpenSSL_add_all_algorithms(); #endif diff --git a/tests/unit_tests/openvpn/test_packet_id.c b/tests/unit_tests/openvpn/test_packet_id.c index 2a2a973..ff3f788 100644 --- a/tests/unit_tests/openvpn/test_packet_id.c +++ b/tests/unit_tests/openvpn/test_packet_id.c @@ -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 @@ 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, diff --git a/tests/unit_tests/openvpn/test_pkcs11.c b/tests/unit_tests/openvpn/test_pkcs11.c index b6c130e..81cdf88 100644 --- a/tests/unit_tests/openvpn/test_pkcs11.c +++ b/tests/unit_tests/openvpn/test_pkcs11.c @@ -37,11 +37,13 @@ #include #include +#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 @@ int main(void) { + openvpn_unit_test_setup(); const struct CMUnitTest tests[] = { cmocka_unit_test_setup_teardown(test_pkcs11_ids, setup_pkcs11, teardown_pkcs11), diff --git a/tests/unit_tests/openvpn/test_pkt.c b/tests/unit_tests/openvpn/test_pkt.c index 7f05184..1084d66 100644 --- a/tests/unit_tests/openvpn/test_pkt.c +++ b/tests/unit_tests/openvpn/test_pkt.c @@ -33,6 +33,7 @@ #include #include #include +#include "test_common.h" #include "crypto.h" #include "options.h" @@ -628,6 +629,7 @@ 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), diff --git a/tests/unit_tests/openvpn/test_provider.c b/tests/unit_tests/openvpn/test_provider.c index 335fca2..94deab6 100644 --- a/tests/unit_tests/openvpn/test_provider.c +++ b/tests/unit_tests/openvpn/test_provider.c @@ -40,6 +40,8 @@ #include #include +#include "test_common.h" + struct management *management; /* global */ static int mgmt_callback_called; @@ -119,6 +121,7 @@ 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); diff --git a/tests/unit_tests/openvpn/test_ssl.c b/tests/unit_tests/openvpn/test_ssl.c index d0c3df7..18b9ec8 100644 --- a/tests/unit_tests/openvpn/test_ssl.c +++ b/tests/unit_tests/openvpn/test_ssl.c @@ -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 @@ int main(void) { + openvpn_unit_test_setup(); + const struct CMUnitTest tests[] = { cmocka_unit_test(crypto_pem_encode_certificate) }; diff --git a/tests/unit_tests/openvpn/test_tls_crypt.c b/tests/unit_tests/openvpn/test_tls_crypt.c index 465543a..a01fbe5 100644 --- a/tests/unit_tests/openvpn/test_tls_crypt.c +++ b/tests/unit_tests/openvpn/test_tls_crypt.c @@ -34,6 +34,7 @@ #include #include +#include "test_common.h" #include "tls_crypt.c" /* Define this function here as dummy since including the ssl_*.c files @@ -673,6 +674,7 @@ int main(void) { + openvpn_unit_test_setup(); const struct CMUnitTest tests[] = { cmocka_unit_test_setup_teardown(tls_crypt_loopback, test_tls_crypt_setup,