From patchwork Mon Jan 27 12:01:46 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "plaisthos (Code Review)" X-Patchwork-Id: 4082 Return-Path: Delivered-To: patchwork@openvpn.net Received: by 2002:a05:7000:c127:b0:5e7:b9eb:58e8 with SMTP id jm39csp2266799mab; Mon, 27 Jan 2025 04:02:07 -0800 (PST) X-Forwarded-Encrypted: i=2; AJvYcCXQgL7Q5AIM+N3CTOeaOcjHkHTrVLmuYHrx2BxzrLy7xgrZZpdle3zqyETDUQEUVDSMg0qh+FotrKA=@openvpn.net X-Google-Smtp-Source: AGHT+IFaKSp6sjcONZtWCZu5Ujai6oT5BlUhPOZPjRNwe/kqm4LHeRq2bo1J3EjKHvEUA8q58R09 X-Received: by 2002:a05:6808:1b8b:b0:3eb:39f5:de8b with SMTP id 5614622812f47-3f19fcd7a35mr29370549b6e.32.1737979327678; Mon, 27 Jan 2025 04:02:07 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1737979327; cv=none; d=google.com; s=arc-20240605; b=HDnFqeuISJod4dKKLJITQGxAQ8t5yJLuudsziEQlmeonHMMJBLXFWCyDboMZpLo7Cc XxJ1LRgBzS1mEl673OCPqwI7theEUOOAjv1Dn07AmmBTOKQD3T4sTsyr7XFokw7ATfnw veUp6yiKTqSb5pkktWP6jotqxVqPJxygcA5/0kVcTRknGfjuo+x+Yk8Beqb2aATFxjQh 2YZ0YEw6kJ+tu1J0utTBrj2keqPJoaylW+v3cgj5ptg6Qr7huRTVN0Aaa2gTbnNUfVbc EKY/wWMOzGvbZNs/7RuwUmZABAtmJl31VzJ5JTzDxYREU3iga3hhGuXRl/zuUimBv6kF Jb5g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20240605; h=errors-to:cc:reply-to:list-subscribe:list-help:list-post :list-archive:list-unsubscribe:list-id:precedence:subject:user-agent :mime-version:message-id:references:auto-submitted:to:date:from :dkim-signature:dkim-signature:dkim-signature; bh=KJIwPZBFeXiu69vtDppNFrZNZT6x17A5nbq3Dht727s=; fh=lm0MLPW7DntlrDqRECIiC9JlE1uPxhepE0URYHIf+eE=; b=ldJeeS714B8BOt/jxCC5E5PtbhYAfeI6y7ygfLHEvtg2tqkXknbET5gI/m0rOA4Ruq SZQsxtH4xAu6M4ofR7SiiotCz5rLPkloI6aGINJKOGXWC6kq8cNtQPg/d8UQvgpzbx90 SKcqmKTG2dDl7DAhfs7utHYMfw/XWT6rDZAeVwkQ0PitewwwQVqqJwsbX+rhKCjCw4rl AP99JC+K8hTky7bbvO64mrYT6zVzB64wrj4MV+jkwN29lpnLf0SvZ4QM+ay0j+ySIqUj nwnzj0oRGU6kHdVdAT9iFu6MsTJGfFwXrN25ZQHWpXTd0AXDrmRzEdAuz56Vjo9UkZq7 3UtQ==; 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=m1H5XMcw; dkim=neutral (body hash did not verify) header.i=@sf.net header.s=x header.b=F9MtdM70; dkim=neutral (body hash did not verify) header.i=@openvpn.net header.s=google header.b=NbMUHW3C; 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=openvpn.net; dara=fail header.i=@openvpn.net Received: from lists.sourceforge.net (lists.sourceforge.net. [216.105.38.7]) by mx.google.com with ESMTPS id 46e09a7af769-724ecfb5e43si6770131a34.274.2025.01.27.04.02.07 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Mon, 27 Jan 2025 04:02:07 -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=m1H5XMcw; dkim=neutral (body hash did not verify) header.i=@sf.net header.s=x header.b=F9MtdM70; dkim=neutral (body hash did not verify) header.i=@openvpn.net header.s=google header.b=NbMUHW3C; 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=openvpn.net; dara=fail header.i=@openvpn.net 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 1tcNoX-0004UQ-Ni; Mon, 27 Jan 2025 12:02:01 +0000 Received: from [172.30.29.66] (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 1tcNoW-0004UC-N4 for openvpn-devel@lists.sourceforge.net; Mon, 27 Jan 2025 12:02:00 +0000 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=sourceforge.net; s=x; h=Content-Type:Content-Transfer-Encoding:MIME-Version :Message-ID:Reply-To:References:Subject:List-Unsubscribe:List-Id:Cc:To:Date: From:Sender:Content-ID:Content-Description:Resent-Date:Resent-From: Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:In-Reply-To:List-Help: List-Subscribe:List-Post:List-Owner:List-Archive; bh=T4jZDjZWmha/VC535MVO/Vzmq1wGLHCLkG+sWb+qYCg=; b=m1H5XMcw3Pd0/1ujAeGYF305o7 cjlFSoSmHmHHaXqcUnmBbq5F0dtEV91I+dVwZswy7e4wcZ4uZMZU7PU55+iUxa7zI3a1PnynGFzE1 0THjNOy4J8wFrfkGzOeWP5g69KAnPLAeYwTl8pujUUyiSN5q6RUIMEXKs0G8Rcyoknhk=; DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=sf.net; s=x ; h=Content-Type:Content-Transfer-Encoding:MIME-Version:Message-ID:Reply-To: References:Subject:List-Unsubscribe:List-Id:Cc:To:Date:From:Sender:Content-ID :Content-Description:Resent-Date:Resent-From:Resent-Sender:Resent-To: Resent-Cc:Resent-Message-ID:In-Reply-To:List-Help:List-Subscribe:List-Post: List-Owner:List-Archive; bh=T4jZDjZWmha/VC535MVO/Vzmq1wGLHCLkG+sWb+qYCg=; b=F 9MtdM703wkM9rs6dtA3OoHfb0skYrTnaW4ejE9x1Z/9m/z2hpEB2g2WmMImo2pMpyAYzFTbaLzmUX xKVanLD6CfIoOH8S5Q+foG3uLNIKU2BzW1Cq8lJg8lxd4Gxv4p1+BZP/jFjFrWBndQp+THhwUDQn9 jt8HyR7BVgseFlFc=; Received: from mail-wr1-f43.google.com ([209.85.221.43]) by sfi-mx-2.v28.lw.sourceforge.com with esmtps (TLS1.2:ECDHE-RSA-AES128-GCM-SHA256:128) (Exim 4.95) id 1tcNoV-0001yU-5l for openvpn-devel@lists.sourceforge.net; Mon, 27 Jan 2025 12:02:00 +0000 Received: by mail-wr1-f43.google.com with SMTP id ffacd0b85a97d-3862d16b4f5so2613588f8f.0 for ; Mon, 27 Jan 2025 04:01:59 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=openvpn.net; s=google; t=1737979307; x=1738584107; darn=lists.sourceforge.net; h=user-agent:content-disposition:content-transfer-encoding :mime-version:message-id:reply-to:references:subject :list-unsubscribe:list-id:auto-submitted:cc:to:date:from:from:to:cc :subject:date:message-id:reply-to; bh=T4jZDjZWmha/VC535MVO/Vzmq1wGLHCLkG+sWb+qYCg=; b=NbMUHW3CGMu3mlrJTtjmfJzPx7A1S+4nMkFF6f3Qw8ul61mI0PWAiYbB4eP4ptAEP6 MuTprX/0lJajvIO6RZoQzWS5MHOgyXbNg79vMrpqMwYLCXyJbJdGh7U/IQjoAXoKH3GB 1/9SNQ4qkWgYFRaEEp9WTzDTMmUvDXrfdwYQ4lGjMyM1xGhki6Da96a7M19SZ9IJfF0R D5H9wzxbNBeQU2DyymH731b1Gm2S82BtzesAnSPtYrrVLiG0VnBLv5NV/DTllx4AQB0E 3pJ7cT5uOuiYRTjpdNjZZWo8/bWT/ZBFa68Fb7HC72nyf6XLxw1VITfrRP+SJ89PU1Fa I4gw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1737979307; x=1738584107; h=user-agent:content-disposition:content-transfer-encoding :mime-version:message-id:reply-to:references:subject :list-unsubscribe:list-id:auto-submitted:cc:to:date:from :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=T4jZDjZWmha/VC535MVO/Vzmq1wGLHCLkG+sWb+qYCg=; b=XStaqMcQzMsZjgUV6PrqEWuEtC++kjZNcpvAN0TzjiBw83KhZinapFQich2PdncmQH azUtUCO/1miBVlK8V9cVFDihQxpPa1s0qQc7iG+2etlmiPgRx1u/JVMqn1Hhzg1JSj7c 57P/N4fW7GLpEfzNjbzH5sW7V2YBaI1QcbybbXbQTiOBQOiDKUbRx2kmfMkRvSLJEvgj X9lyXjfWBl6sToR9uc1Yczgh2hpKYJ9CmOPfxlMPcSlh0bSfUuYJTgyjfu55NeUpJK8J FPNOn7BDYO+yY67rRNmjpgLBufdzWPiC9Xo8So8NtIwpeMPMR9GxaGWFIECg/nErl8tW cWrw== X-Gm-Message-State: AOJu0YxwZFQx8G0jSJFxqLqDR9nlVH85120zHJo69+xtalUw3DaZ6PWw rOSiM7ZPp2WlCBGJjD/mMdP36/32gEpNLQ4tGjjrAtrFX/sipfUU4p3SSSdDKu6PHx2u/fDOnT1 x X-Gm-Gg: ASbGnct6EiKBe8oFFGTzU8SATOTDcXClwgCiS1t2ByEuRdXLD7RWBUBvk8wfQ3TBruf GKyj/D8hepdbchI4ZDp+36A4CaZfV0MRHTen6Jkxch//IaTC7tIu9qma2/SweNXAlvcnqmra9v/ weh4DGz0fTxVT/lDz8sHOgx6Tt+RqNqV0BDrWNKM788sAkJ2hUir9Kbo9xlq6jzm4OaP7a+XQ0C w7ny+GpiwPDjhX38jtKEiwtFhlyFkhQBBrX+qwMmv4VjNYKUXvQG0SAqgDjt0reQsVrsYBgUMd+ /090KQZQjxhrvfo1FcMveq2Fpd6SVxmHAafO9tukN1LFEt9MAqMtNgOslSTF6CJsJSarJXH1 X-Received: by 2002:a5d:58e6:0:b0:38b:ee18:dd46 with SMTP id ffacd0b85a97d-38c22279d0fmr13661290f8f.20.1737979307227; Mon, 27 Jan 2025 04:01:47 -0800 (PST) Received: from gerrit.openvpn.in (ec2-18-159-0-78.eu-central-1.compute.amazonaws.com. [18.159.0.78]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-38c2a1c3fedsm10654312f8f.85.2025.01.27.04.01.46 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 27 Jan 2025 04:01:46 -0800 (PST) From: "plaisthos (Code Review)" X-Google-Original-From: "plaisthos (Code Review)" X-Gerrit-PatchSet: 1 Date: Mon, 27 Jan 2025 12:01:46 +0000 To: flichtenheld Auto-Submitted: auto-generated X-Gerrit-MessageType: newchange X-Gerrit-Change-Id: Ieee368e325d7f9c367fd91fee0fd3e281ee0855d X-Gerrit-Change-Number: 876 X-Gerrit-Project: openvpn X-Gerrit-ChangeURL: X-Gerrit-Commit: 445483c4f71e9d157e0fc4b415becadd8068072d References: Message-ID: <2f6f608cb61cccee430ea675de28ededf19a7e4f-HTML@gerrit.openvpn.net> MIME-Version: 1.0 User-Agent: Gerrit/3.8.2 X-Spam-Score: -0.2 (/) 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: Attention is currently required from: flichtenheld. Hello flichtenheld, I'd like you to do a code review. Please visit Content analysis details: (-0.2 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. [209.85.221.43 listed in sa-trusted.bondedsender.org] 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. [209.85.221.43 listed in bl.score.senderscore.com] 0.0 SPF_HELO_NONE SPF: HELO does not publish an SPF Record -0.0 SPF_PASS SPF: sender matches SPF record 0.0 RCVD_IN_MSPIKE_H2 RBL: Average reputation (+2) [209.85.221.43 listed in wl.mailspike.net] -0.0 RCVD_IN_DNSWL_NONE RBL: Sender listed at https://www.dnswl.org/, no trust [209.85.221.43 listed in list.dnswl.org] 0.0 WEIRD_PORT URI: Uses non-standard port number for HTTP 0.0 HTML_MESSAGE BODY: HTML included in message -0.1 DKIM_VALID Message has at least one valid DKIM or DK signature -0.1 DKIM_VALID_EF Message has a valid DKIM or DK signature from envelope-from domain 0.1 DKIM_SIGNED Message has a DKIM or DK signature, not necessarily valid -0.1 DKIM_VALID_AU Message has a valid DKIM or DK signature from author's domain -0.0 T_SCC_BODY_TEXT_LINE No description available. 0.0 T_KAM_HTML_FONT_INVALID Test for Invalidly Named or Formatted Colors in HTML X-Headers-End: 1tcNoV-0001yU-5l Subject: [Openvpn-devel] [M] Change in openvpn[master]: 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: , Reply-To: arne-openvpn@rfc2549.org, openvpn-devel@lists.sourceforge.net, frank@lichtenheld.com Cc: openvpn-devel Errors-To: openvpn-devel-bounces@lists.sourceforge.net X-getmail-retrieved-from-mailbox: Inbox X-GMAIL-THRID: =?utf-8?q?1822403411822921527?= X-GMAIL-MSGID: =?utf-8?q?1822403411822921527?= X-getmail-filter-classifier: gerrit message type newchange Attention is currently required from: flichtenheld. Hello flichtenheld, I'd like you to do a code review. Please visit http://gerrit.openvpn.net/c/openvpn/+/876?usp=email to review the following change. Change subject: Add unit tests for atoi parsing options helper ...................................................................... Add unit tests for atoi parsing options helper Change-Id: Ieee368e325d7f9c367fd91fee0fd3e281ee0855d --- M tests/unit_tests/openvpn/mock_msg.c M tests/unit_tests/openvpn/mock_msg.h M tests/unit_tests/openvpn/test_misc.c 3 files changed, 108 insertions(+), 3 deletions(-) git pull ssh://gerrit.openvpn.net:29418/openvpn refs/changes/76/876/1 diff --git a/tests/unit_tests/openvpn/mock_msg.c b/tests/unit_tests/openvpn/mock_msg.c index a291f8f..99224f8 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,13 @@ fatal_error_triggered = true; printf("FATAL ERROR:"); } - vprintf(format, arglist); - printf("\n"); + CLEAR(mock_msg_buf); + if ((flags & M_DEBUG_LEVEL) <= print_x_debug_level) + { + vprintf(format, arglist); + printf("\n"); + } + vsnprintf(mock_msg_buf, sizeof(mock_msg_buf), format, arglist); } void diff --git a/tests/unit_tests/openvpn/mock_msg.h b/tests/unit_tests/openvpn/mock_msg.h index be5f2e5..25b7aa2 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 1024 + +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