From patchwork Tue Jan 28 17:58:55 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Gert Doering X-Patchwork-Id: 4097 Return-Path: Delivered-To: patchwork@openvpn.net Received: by 2002:a05:7000:6a49:b0:5e7:b9eb:58e8 with SMTP id v9csp594283mat; Tue, 28 Jan 2025 09:59:11 -0800 (PST) X-Forwarded-Encrypted: i=2; AJvYcCW5RA4frv74cGbxeWXtrJe/ixA70ftI4b927QN9CYgW+XwRCLPgsnEU/WhFUpHvvsPTkdNWMZHH5vs=@openvpn.net X-Google-Smtp-Source: AGHT+IFfdSoVwi/M4rTQ55FVrws2maN6bqgWbCalfXXU8tWy4QpnSYa+8DRneSQOY+gugcRM3jxk X-Received: by 2002:a05:6808:164d:b0:3f0:403a:3db9 with SMTP id 5614622812f47-3f19fd56ab8mr27920262b6e.35.1738087151443; Tue, 28 Jan 2025 09:59:11 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1738087151; cv=none; d=google.com; s=arc-20240605; b=CR97GUJPWKznDAO9bwAcE9w7lIqaCzMq9oZonsmCjdm6+tQimAGS4H8Xx3O/HDj+4Y YMM/q01wGhuxYQBZ5/1/FbdhODe76BRZDWjYk9Co5TzVavrM83e4jnejMhZpGRuT3uLR 8sgunbCE4B431e2R+qGYHCiVkTEWk0oe1ab1cjbYpLhspkEU16PUe5x2XmWm5Rhx/zn+ 2/xjuo/wfP9mymf19cjmjN8WrS8y6tz+CfXryn1b4YOUaS3jPIqMKWnYUovDISNB8lAb WI9NaQoD1W8JWWKkhSYSIKwqKy/3H/49hxFStgaFLokDSf8s8MnNkBQBQ7h2L90VfpsA pC9g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20240605; 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=u8SKGLuyHB13ygThHaK53BzoifxsETTDp9mRRSDRrEk=; fh=4NbAC/LsuMLI0S0hprUlLSLCiHwg6SCAifhH718Jh0Q=; b=ismtBPgzGWXf/yfKKGO6Gd+nJ5ng+TGjGSlQk00hrG+I0r0gDCpT+NC71ypcQj0smI 1rtz1iI2qeo675jkqVDXmgXw9cWwovGgBKEb8kKPYw/3FnFpAAWUVJr2wP40ESg9PqCU BP3j4QweIzgmp490KZmJPv5T1H2VY5/KNGzKvY2uF1nTqGbu55FJvnd64G4YpfGspCRf 4rW6ZsAQdfnjDunH0IFJ357AEEWDlsHPtqOsEFDBwA2DTu/UIo1zeNpY7KG8sHWNvmZ/ XW2GrC09Bqd57LYNBG8MXsqKA/KDrgaMyTTLnzLE8v2MsA30Kip0cM5kG6enQ96FsDQq 42Hg==; dara=google.com ARC-Authentication-Results: i=1; mx.google.com; dkim=neutral (body hash did not verify) header.i=@sourceforge.net header.s=x header.b=VjTmHGhV; dkim=neutral (body hash did not verify) header.i=@sf.net header.s=x header.b=XuLb5Yad; 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 006d021491bc7-5fa8ba1e593si8047397eaf.72.2025.01.28.09.59.11 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Tue, 28 Jan 2025 09:59:11 -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=VjTmHGhV; dkim=neutral (body hash did not verify) header.i=@sf.net header.s=x header.b=XuLb5Yad; 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-4.v29.lw.sourceforge.com) by sfs-ml-4.v29.lw.sourceforge.com with esmtp (Exim 4.95) (envelope-from ) id 1tcpre-0003GG-Ja; Tue, 28 Jan 2025 17:59:06 +0000 Received: from [172.30.29.66] (helo=mx.sourceforge.net) by sfs-ml-4.v29.lw.sourceforge.com with esmtps (TLS1.2) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.95) (envelope-from ) id 1tcprc-0003G7-Px for openvpn-devel@lists.sourceforge.net; Tue, 28 Jan 2025 17:59:04 +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=sWSLhbtNT4W59EAGJLrofw5YRg4FJdzMSfuXfTQzr/g=; b=VjTmHGhVZj4b7REsi5saCehqHx rLNuVBh+k7bYWDEJPXpzh+9MCkUFWqO70MsQ+Fa7GdhISWezHsyPsp7BwA/Tc/zKO9ZxSE1WMLWQR Z9mFJ8z0rVoLt5gMQ/xfpSdeKf/u7jFm/LKGZMOlZUsTsosrEhRSQlhGiRs/K3gmT+6U=; 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=sWSLhbtNT4W59EAGJLrofw5YRg4FJdzMSfuXfTQzr/g=; b=XuLb5YadEzDO84m23hewenVhfP mZh06+1GqCiFYP8ExCB2o+mnvDMmfuAXOxH692SSTZ3jJgtzv9SclYfXJPcUbQKZ9aFlyn5LZKgRI 1NUobkE7kpNgSTwpVMkRng7nVFA49wVu0W7BZzMR6OgWh1jrQ8A7bk8kr2w1W1OIGUwA=; 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 1tcprb-000603-2P for openvpn-devel@lists.sourceforge.net; Tue, 28 Jan 2025 17:59:04 +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 50SHwuBM012342 for ; Tue, 28 Jan 2025 18:58:56 +0100 Received: (from gert@localhost) by blue.greenie.muc.de (8.17.1.9/8.17.1.9/Submit) id 50SHwueh012298 for openvpn-devel@lists.sourceforge.net; Tue, 28 Jan 2025 18:58:56 +0100 From: Gert Doering To: openvpn-devel@lists.sourceforge.net Date: Tue, 28 Jan 2025 18:58:55 +0100 Message-ID: <20250128175855.12289-1-gert@greenie.muc.de> X-Mailer: git-send-email 2.45.2 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-2.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 Change-Id: Ieee368e325d7f9c367fd91fee0fd3e281ee0855d Signed-off-by: Arne Schwabe Acked-by: Frank Lichtenheld --- This change was reviewed on Gerrit and approved by at least one developer. I request to merge it to master. Content analysis details: (-0.0 points, 6.0 required) pts rule name description ---- ---------------------- -------------------------------------------------- 0.0 RCVD_IN_VALIDITY_SAFE_BLOCKED RBL: ADMINISTRATOR NOTICE: The query to Validity was blocked. See https://knowledge.validity.com/hc/en-us/articles/20961730681243 for more information. [193.149.48.174 listed in sa-accredit.habeas.com] 0.0 RCVD_IN_VALIDITY_RPBL_BLOCKED RBL: ADMINISTRATOR NOTICE: The query to Validity was blocked. See https://knowledge.validity.com/hc/en-us/articles/20961730681243 for more information. [193.149.48.174 listed in bl.score.senderscore.com] -0.0 SPF_HELO_PASS SPF: HELO matches SPF record -0.0 SPF_PASS SPF: sender matches SPF record -0.0 T_SCC_BODY_TEXT_LINE No description available. X-Headers-End: 1tcprb-000603-2P Subject: [Openvpn-devel] [PATCH v2] Add unit tests for atoi parsing options helper 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?1822405275510765463?= X-GMAIL-MSGID: =?utf-8?q?1822516472945957710?= From: Arne Schwabe Change-Id: Ieee368e325d7f9c367fd91fee0fd3e281ee0855d Signed-off-by: Arne Schwabe 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/+/876 This mail reflects revision 2 of this Change. Acked-by according to Gerrit (reflected above): Frank Lichtenheld diff --git a/tests/unit_tests/openvpn/mock_msg.c b/tests/unit_tests/openvpn/mock_msg.c index a291f8f..b6ec744 100644 --- a/tests/unit_tests/openvpn/mock_msg.c +++ b/tests/unit_tests/openvpn/mock_msg.c @@ -37,10 +37,16 @@ #include "errlevel.h" #include "error.h" +#include "mock_msg.h" unsigned int x_debug_level = 0; /* Default to (almost) no debugging output */ +unsigned int print_x_debug_level = 0; + bool fatal_error_triggered = false; +char mock_msg_buf[MOCK_MSG_BUF]; + + void mock_set_debug_level(int level) { @@ -48,6 +54,18 @@ } int +mock_get_debug_level(void) +{ + return x_debug_level; +} + +void +mock_set_print_debug_level(int level) +{ + print_x_debug_level = level; +} + +int get_debug_level(void) { return x_debug_level; @@ -62,8 +80,14 @@ fatal_error_triggered = true; printf("FATAL ERROR:"); } - vprintf(format, arglist); - printf("\n"); + CLEAR(mock_msg_buf); + vsnprintf(mock_msg_buf, sizeof(mock_msg_buf), format, arglist); + + if ((flags & M_DEBUG_LEVEL) <= print_x_debug_level) + { + printf("%s", mock_msg_buf); + printf("\n"); + } } void diff --git a/tests/unit_tests/openvpn/mock_msg.h b/tests/unit_tests/openvpn/mock_msg.h index be5f2e5..bc2ee5d 100644 --- a/tests/unit_tests/openvpn/mock_msg.h +++ b/tests/unit_tests/openvpn/mock_msg.h @@ -31,4 +31,18 @@ */ void mock_set_debug_level(int level); +#define MOCK_MSG_BUF 2048 + +extern bool fatal_error_triggered; +extern char mock_msg_buf[MOCK_MSG_BUF]; + +void +mock_set_debug_level(int level); + +int +mock_get_debug_level(void); + +void +mock_set_print_debug_level(int level); + #endif /* MOCK_MSG */ diff --git a/tests/unit_tests/openvpn/test_misc.c b/tests/unit_tests/openvpn/test_misc.c index 563d4ae..56e9d98 100644 --- a/tests/unit_tests/openvpn/test_misc.c +++ b/tests/unit_tests/openvpn/test_misc.c @@ -38,6 +38,7 @@ #include "options_util.h" #include "test_common.h" #include "list.h" +#include "mock_msg.h" static void test_compat_lzo_string(void **state) @@ -311,13 +312,80 @@ gc_free(&gc); } +static void +test_atoi_variants(void **state) +{ + assert_true(valid_integer("1234", true)); + assert_true(valid_integer("1234", false)); + assert_true(valid_integer("0", false)); + assert_true(valid_integer("0", true)); + assert_true(valid_integer("-777", false)); + assert_false(valid_integer("-777", true)); + + assert_false(valid_integer("-777foo", false)); + assert_false(valid_integer("-777foo", true)); + + assert_false(valid_integer("foo777", true)); + assert_false(valid_integer("foo777", false)); + + /* 2**31 + 5 , just outside of signed int range */ + assert_false(valid_integer("2147483653", true)); + assert_false(valid_integer("2147483653", false)); + assert_false(valid_integer("-2147483653", true)); + assert_false(valid_integer("-2147483653", false)); + + + int msglevel = D_LOW; + int saved_log_level = mock_get_debug_level(); + mock_set_debug_level(D_LOW); + + /* check happy path */ + assert_int_equal(positive_atoi("1234", msglevel), 1234); + assert_int_equal(positive_atoi("0", msglevel), 0); + + assert_int_equal(atoi_warn("1234", msglevel), 1234); + assert_int_equal(atoi_warn("0", msglevel), 0); + assert_int_equal(atoi_warn("-1194", msglevel), -1194); + + CLEAR(mock_msg_buf); + assert_int_equal(positive_atoi("-1234", msglevel), 0); + assert_string_equal(mock_msg_buf, "Cannot parse argument '-1234' as non-negative integer"); + + /* 2**31 + 5 , just outside of signed int range */ + CLEAR(mock_msg_buf); + assert_int_equal(positive_atoi("2147483653", msglevel), 0); + assert_string_equal(mock_msg_buf, "Cannot parse argument '2147483653' as non-negative integer"); + + CLEAR(mock_msg_buf); + assert_int_equal(atoi_warn("2147483653", msglevel), 0); + assert_string_equal(mock_msg_buf, "Cannot parse argument '2147483653' as integer"); + + CLEAR(mock_msg_buf); + assert_int_equal(positive_atoi("foo77", msglevel), 0); + assert_string_equal(mock_msg_buf, "Cannot parse argument 'foo77' as non-negative integer"); + + CLEAR(mock_msg_buf); + assert_int_equal(positive_atoi("77foo", msglevel), 0); + assert_string_equal(mock_msg_buf, "Cannot parse argument '77foo' as non-negative integer"); + + CLEAR(mock_msg_buf); + assert_int_equal(atoi_warn("foo77", msglevel), 0); + assert_string_equal(mock_msg_buf, "Cannot parse argument 'foo77' as integer"); + + CLEAR(mock_msg_buf); + assert_int_equal(atoi_warn("77foo", msglevel), 0); + assert_string_equal(mock_msg_buf, "Cannot parse argument '77foo' as integer"); + + mock_set_debug_level(saved_log_level); +} const struct CMUnitTest misc_tests[] = { cmocka_unit_test(test_compat_lzo_string), cmocka_unit_test(test_auth_fail_temp_no_flags), cmocka_unit_test(test_auth_fail_temp_flags), cmocka_unit_test(test_auth_fail_temp_flags_msg), - cmocka_unit_test(test_list) + cmocka_unit_test(test_list), + cmocka_unit_test(test_atoi_variants) }; int