From patchwork Wed May 17 08:44:21 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Frank Lichtenheld X-Patchwork-Id: 3220 Return-Path: Delivered-To: patchwork@openvpn.net Received: by 2002:a05:7300:7b9a:b0:c3:1364:a2a2 with SMTP id j26csp1030446dyk; Wed, 17 May 2023 01:45:34 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ4lNo8mqT6CJeFSPn52By81F8YGwgzWUzjR4VNakK0I3714UmdHHZpqQHGc+1yszqF/MQKi X-Received: by 2002:a5d:8787:0:b0:76f:f462:34d2 with SMTP id f7-20020a5d8787000000b0076ff46234d2mr3947617ion.14.1684313134365; Wed, 17 May 2023 01:45:34 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1684313134; cv=none; d=google.com; s=arc-20160816; b=d6T0xGVXUF146SDVHPS7CY3a093/gPE2DiR6wqJrE039cHsYX4X2nwuhTrGbDUglWT lVCqr7VZM4u/uAnjy49jihoha0hwwpTXlRY10csYBAlAa6b1sGtms7AuxyFnuWauSpfm zXbVWvzMWcEDGAozKy96YG367otOdmtBOTA15zwR00j1kEoaVUL5m0o3NhfuDULWJ2+G V+0wPmaNz79Oyd9LWr5zFwnIapYBugIcMA9HrPx+6Ni+N4A10sp4IphvyI5h7+HyIgUp UrQkYgXC6MV0q1KSvsP7++1Wd/DAFy4mh0O6SOJEqAjFfaX93ZzPVKoGcg5Rbd/DcbgN mv5Q== 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:dkim-signature; bh=GiQRUUdxShAp1JbsAodkcWIL+59wL5mKAb48DCRs1bo=; b=gKOmmaVTP6EfwE74xVmlGa/FeVn0VsW1PJ4xRBOr+MJB4zq8lV1gs1C9a690295D9i BqpXevtbYRFpvGfgbUw/zXktgi9OtSqmHy7wX48YEoakLcdDXtZbHkhHK7nlCTGrfCnC 3wH23AFTlDJBH03BPKeyNUh1IHA5QEbuwM6e8jC309XIg/SaSdNgrT3lQ/Kz21+Nqa49 cwFi8vQKs7q2jXKct5WHeQnDJ2lhwl4bPMzljm6e0SpGa+SrgR/NUkZb1nFX+qWIXGOI pYYK+1KbSrDdbWhjRGhswfu9U8onVVz21mnJUMo9F/bclSuTL5+1qyazwpH8X16+rV8q G7ag== ARC-Authentication-Results: i=1; mx.google.com; dkim=neutral (body hash did not verify) header.i=@sourceforge.net header.s=x header.b=gWQUkFRM; dkim=neutral (body hash did not verify) header.i=@sf.net header.s=x header.b=AObZvQEA; dkim=neutral (body hash did not verify) header.i=@lichtenheld.com header.s=MBO0001 header.b=K5frLljj; 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 Received: from lists.sourceforge.net (lists.sourceforge.net. [216.105.38.7]) by mx.google.com with ESMTPS id k4-20020a5e8904000000b0076fe290b78dsi4668985ioj.43.2023.05.17.01.45.33 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Wed, 17 May 2023 01:45:34 -0700 (PDT) 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=gWQUkFRM; dkim=neutral (body hash did not verify) header.i=@sf.net header.s=x header.b=AObZvQEA; dkim=neutral (body hash did not verify) header.i=@lichtenheld.com header.s=MBO0001 header.b=K5frLljj; 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 Received: from [127.0.0.1] (helo=sfs-ml-1.v29.lw.sourceforge.com) by sfs-ml-1.v29.lw.sourceforge.com with esmtp (Exim 4.95) (envelope-from ) id 1pzCly-0007s4-7P; Wed, 17 May 2023 08:44:39 +0000 Received: from [172.30.20.202] (helo=mx.sourceforge.net) by sfs-ml-1.v29.lw.sourceforge.com with esmtps (TLS1.2) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.95) (envelope-from ) id 1pzClx-0007rc-Bo for openvpn-devel@lists.sourceforge.net; Wed, 17 May 2023 08:44:38 +0000 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=sourceforge.net; s=x; h=Content-Transfer-Encoding:Content-Type:MIME-Version :References:In-Reply-To:Message-Id:Date:Subject:To:From:Sender:Reply-To:Cc: 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=vjusRKFMWy2lT7chV9Gjkh9vwgHIs/JqJ1OPwbbCpGc=; b=gWQUkFRM5FzbpOjuavz6Ivksqa bQ1J+SqiOWXieBQOy3JVRYOSol3g9SE/ofezJzAqYPs8Z5ig3FYH2kgSrZ7WS4V/L21sEHFP3IV+H SxT6zA+Y70dalB3oGl11C/DsBko1HEYFm7hzMm8LD05uGvQ3PyuW0ulZnTE0x+HO63kQ=; DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=sf.net; s=x ; h=Content-Transfer-Encoding:Content-Type:MIME-Version:References: In-Reply-To:Message-Id:Date:Subject:To:From:Sender:Reply-To:Cc: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=vjusRKFMWy2lT7chV9Gjkh9vwgHIs/JqJ1OPwbbCpGc=; b=AObZvQEAquEUQWlNQyhO7+JTuv 6TvRSVfIFvWMIVRjCWP4AnEZseJDc4yx1wsElFs3ss+ia0V+OFSa89MDFlnhfV5orX/ILnWygj08A 3qoEc6bjS7E478Xlll4t/xoRGe5bwUa+hz61Qj6GDcyMNNWF1R74M8I++3Xgsx1MqXCA=; Received: from mout-p-103.mailbox.org ([80.241.56.161]) by sfi-mx-1.v28.lw.sourceforge.com with esmtps (TLS1.2:ECDHE-RSA-AES256-GCM-SHA384:256) (Exim 4.95) id 1pzCls-00ArqG-Mo for openvpn-devel@lists.sourceforge.net; Wed, 17 May 2023 08:44:38 +0000 Received: from smtp2.mailbox.org (smtp2.mailbox.org [10.196.197.2]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange ECDHE (P-384) server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by mout-p-103.mailbox.org (Postfix) with ESMTPS id 4QLmqq4gvtz9sl2 for ; Wed, 17 May 2023 10:44:23 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=lichtenheld.com; s=MBO0001; t=1684313063; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=vjusRKFMWy2lT7chV9Gjkh9vwgHIs/JqJ1OPwbbCpGc=; b=K5frLljjGJzHcjMHWZ+eEeH2EOhtuB8/QXAwjsl5GDVXOkLje0MSTORHRjtbSUHNqBuCXU r1accv/66Tzi53sy1pcete0yNtRHCzCQR+ndTWMmelaZIiZf6wkT/mhlszhswclNRuuKiE ChUDpEe1w8DvpfHbA7f6WpxAmV7VJW8yScQWDcVLKGhDeqL2QLgkET0BVyPjsgtYbaSYjT dQfZdoxypG/9T2jRbdiPBJEb+M3IaOvyCpMvt9MaoE9aUzhdPJRY+nchqSiJwGImkrex8L RgCBErVbKpRqsrHSr1nayd1Z/YlcOefDAY9FBX/XrYbPjIaG9Kl7QFdTFVrVIw== From: Frank Lichtenheld To: openvpn-devel@lists.sourceforge.net Date: Wed, 17 May 2023 10:44:21 +0200 Message-Id: <20230517084422.70547-1-frank@lichtenheld.com> In-Reply-To: <20230329090447.246561-1-frank@lichtenheld.com> References: <20230329090447.246561-1-frank@lichtenheld.com> MIME-Version: 1.0 X-Spam-Score: -0.9 (/) 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: Since we use strlen() to determine the length and then check it ourselves, there is really no point in using strncpy. But the compiler might complain that we use the output of strlen() for the length of strncpy which is usually a sign for bugs: Content analysis details: (-0.9 points, 6.0 required) pts rule name description ---- ---------------------- -------------------------------------------------- -0.7 RCVD_IN_DNSWL_LOW RBL: Sender listed at https://www.dnswl.org/, low trust [80.241.56.161 listed in list.dnswl.org] -0.0 SPF_PASS SPF: sender matches SPF record 0.0 SPF_HELO_NONE SPF: HELO does not publish an SPF Record -0.1 DKIM_VALID_AU Message has a valid DKIM or DK signature from author's domain 0.1 DKIM_SIGNED Message has a DKIM or DK signature, not necessarily valid -0.1 DKIM_VALID_EF Message has a valid DKIM or DK signature from envelope-from domain -0.1 DKIM_VALID Message has at least one valid DKIM or DK signature -0.0 T_SCC_BODY_TEXT_LINE No description available. X-Headers-End: 1pzCls-00ArqG-Mo Subject: [Openvpn-devel] [PATCH 1/2 v3] buffer: use memcpy in buf_catrunc 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?1761624870130583212?= X-GMAIL-MSGID: =?utf-8?q?1766130329265154018?= Since we use strlen() to determine the length and then check it ourselves, there is really no point in using strncpy. But the compiler might complain that we use the output of strlen() for the length of strncpy which is usually a sign for bugs: error: ‘strncpy’ specified bound depends on the length of the source argument [-Werror=stringop-overflow=] Warning was at least triggered for mingw-gcc version 10-win32 20220113. v2: - make len size_t and change code to avoid any theoretical overflows - remove useless casts v3: - fix off-by-one introduced by v2 %) Change-Id: If4a67adac4d2e870fd719b58075d39efcd67c671 Signed-off-by: Frank Lichtenheld --- src/openvpn/buffer.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/openvpn/buffer.c b/src/openvpn/buffer.c index d099795b..36b28afb 100644 --- a/src/openvpn/buffer.c +++ b/src/openvpn/buffer.c @@ -316,10 +316,10 @@ buf_catrunc(struct buffer *buf, const char *str) { if (buf_forward_capacity(buf) <= 1) { - int len = (int) strlen(str) + 1; - if (len < buf_forward_capacity_total(buf)) + size_t len = strlen(str); + if (buf_size_valid(len) && (len + 1 < buf_forward_capacity_total(buf))) { - strncpynt((char *)(buf->data + buf->capacity - len), str, len); + memcpy(buf->data + buf->capacity - len - 1, str, len + 1); } } } From patchwork Wed May 17 08:44:22 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Frank Lichtenheld X-Patchwork-Id: 3221 Return-Path: Delivered-To: patchwork@openvpn.net Received: by 2002:a05:7300:7b9a:b0:c3:1364:a2a2 with SMTP id j26csp1030448dyk; Wed, 17 May 2023 01:45:34 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ4J43B//PAKtbOCItayfD+9UuBpS6kW/RItGFH3zXek95m50M/4M+wPib2ritFx0PWIYMEt X-Received: by 2002:a6b:680d:0:b0:76c:8674:81e7 with SMTP id d13-20020a6b680d000000b0076c867481e7mr4102342ioc.21.1684313134536; Wed, 17 May 2023 01:45:34 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1684313134; cv=none; d=google.com; s=arc-20160816; b=e40GpYYtbzJk9d7pXjP8mDpzepYmNahWcx2AOdZy9CWCN8epyiJV4Hc6rNm8lf1xK+ FMf8xroecjs8RzudexwW1dZJ7A4Yvk8aByG4ZzLm41TyTkTniNY7otneg+56J5J+yByR F5Yi7e8FNDgfb/9APOv1qU69uT3lhJW7YyKVmnNQgwiQsca8hDBSSVW8Wymh/emleeOb LflA0ByLM/Y80lC6QV80cKTHr37d6HjDg839bspAFTLdVl86ixnwgz3YS6Tl1SzXF90p v5xWBUwA7xThHQyHEibFZcDRIv/TSZ+h4NIwog35DXVA80WWMMoTnt3d9scpOCCxbMLl GSHg== 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:dkim-signature; bh=HO4Rhz96KlkPnqciCPC1I7/in4r7w/8Y3z8SfpACOdM=; b=KgNiyAT6e2lbG3etBjv+b8RikKCg00AnNjrpsbYaHtyW+xrs+ongcwM9ee05vDPI3y XMB+OfEmxIiXkvsUDQBhKVFFTUhpTFUsNvgdUNchIVS6K1p8hLYiQTHGtJ2I/Ib0cUgz L1lIqlcoo+FJhajuLt3p1yQQfG3PCKJOiIFN7CyOkrRZ/MCNffHyUskK9GL6wB4DPyZv IWv5Wx/pFcWyi9Ney12rvHRCMEzDNznNrr+cgS3eqfbVxQ+skPqWtCIgtGPYSRvF6skY Zi4Kj9NeqcFOlrFAMluNSq42s6jwQ99vA6oL7Pv3yGe3yn8zEi6mVxR1T0nFJhZc/Pca Wz9A== ARC-Authentication-Results: i=1; mx.google.com; dkim=neutral (body hash did not verify) header.i=@sourceforge.net header.s=x header.b=FRYAFAW2; dkim=neutral (body hash did not verify) header.i=@sf.net header.s=x header.b=ZHLX+332; dkim=neutral (body hash did not verify) header.i=@lichtenheld.com header.s=MBO0001 header.b=zWJc5DT6; 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 Received: from lists.sourceforge.net (lists.sourceforge.net. [216.105.38.7]) by mx.google.com with ESMTPS id s10-20020a6b740a000000b00763c62056b7si14231400iog.61.2023.05.17.01.45.34 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Wed, 17 May 2023 01:45:34 -0700 (PDT) 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=FRYAFAW2; dkim=neutral (body hash did not verify) header.i=@sf.net header.s=x header.b=ZHLX+332; dkim=neutral (body hash did not verify) header.i=@lichtenheld.com header.s=MBO0001 header.b=zWJc5DT6; 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 Received: from [127.0.0.1] (helo=sfs-ml-3.v29.lw.sourceforge.com) by sfs-ml-3.v29.lw.sourceforge.com with esmtp (Exim 4.95) (envelope-from ) id 1pzCm6-0003wy-Jj; Wed, 17 May 2023 08:44:46 +0000 Received: from [172.30.20.202] (helo=mx.sourceforge.net) by sfs-ml-3.v29.lw.sourceforge.com with esmtps (TLS1.2) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.95) (envelope-from ) id 1pzCm4-0003wq-J3 for openvpn-devel@lists.sourceforge.net; Wed, 17 May 2023 08:44:44 +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=iGz6Wn7xbfhTsCLOollDh6GlYHJ4uNMZEMeFsL+xvbU=; b=FRYAFAW2OT111TTXlzshSNT8ig jda+6q83pEZvo4N5TYCezgrMK5os4fGGVlzJVOE9bqLYuUbUNqu1wOT4V1qCCVB9bLO3yKVuSFzgI NjPKYrtNQy+Wq/nS08O9dD3Kp43X5QOgZ9+ArFYRhk70iX5Q8f3kBuzGSEAX/Wi+tf+0=; 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=iGz6Wn7xbfhTsCLOollDh6GlYHJ4uNMZEMeFsL+xvbU=; b=ZHLX+332qNBuZ/5CoANWjTeJze FaJMv56B19wAODLGsqSKyMyy8EulzM7wkjxOH80BdYrTBu2tLRy5ZM+rZFcMUA+0mvwIVzwuT1l/C u07oDbwxCdSkfLn1pRkZldFN6XxLrv3pCYwkQuJuxkltfmdcYnemyEC9v+dnavJg8Tm0=; Received: from mout-p-101.mailbox.org ([80.241.56.151]) by sfi-mx-2.v28.lw.sourceforge.com with esmtps (TLS1.2:ECDHE-RSA-AES256-GCM-SHA384:256) (Exim 4.95) id 1pzClu-0002IP-GH for openvpn-devel@lists.sourceforge.net; Wed, 17 May 2023 08:44:40 +0000 Received: from smtp2.mailbox.org (smtp2.mailbox.org [10.196.197.2]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange ECDHE (P-384) server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by mout-p-101.mailbox.org (Postfix) with ESMTPS id 4QLmqq6W1Gz9sbr for ; Wed, 17 May 2023 10:44:23 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=lichtenheld.com; s=MBO0001; t=1684313063; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=iGz6Wn7xbfhTsCLOollDh6GlYHJ4uNMZEMeFsL+xvbU=; b=zWJc5DT6ANyYKFhgLVA9/8efm2YOofjBO86gwRqBR0meC1L/EW8KDydKhB2zv/aElMZGBM cmdv44Rbo+acH7MO2Xltd+KQY6Bn85qttU8Pb/V43dD9PogW1re4MLqS/Tn8mDWcQcwrXt Jz9y2HX9hz/U1jWBseDiQ/hBObMVo2I+nPwpYUEgze7MgIvqmYF3+w9PlLn2lG5IhRbRFS OBhw05EAkJKmEjtRC0Z1Tif6uquAk5Yl1YodeIcEl5G9LNfmHzxzhtpwBC52trIJmApUEB w9j9XZU3dsL7Q/laaH8baFi8tDHNQKtat/w7pHDJBhGzyctR9TGpDYxSIwNehg== From: Frank Lichtenheld To: openvpn-devel@lists.sourceforge.net Date: Wed, 17 May 2023 10:44:22 +0200 Message-Id: <20230517084422.70547-2-frank@lichtenheld.com> In-Reply-To: <20230517084422.70547-1-frank@lichtenheld.com> References: <20230329090447.246561-1-frank@lichtenheld.com> <20230517084422.70547-1-frank@lichtenheld.com> MIME-Version: 1.0 X-Spam-Score: -0.9 (/) 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: Just some very basic tests. v2: - fix off-by-one Change-Id: I73fc893136387d1da05f4aea98cb37b02d6c3230 Signed-off-by: Frank Lichtenheld --- tests/unit_tests/openvpn/test_buffer.c | 62 ++++++++++++++++++++++++++ 1 file changed, [...] Content analysis details: (-0.9 points, 6.0 required) pts rule name description ---- ---------------------- -------------------------------------------------- -0.7 RCVD_IN_DNSWL_LOW RBL: Sender listed at https://www.dnswl.org/, low trust [80.241.56.151 listed in list.dnswl.org] 0.0 SPF_HELO_NONE SPF: HELO does not publish an SPF Record -0.0 SPF_PASS SPF: sender matches SPF record -0.1 DKIM_VALID_EF Message has a valid DKIM or DK signature from envelope-from domain -0.1 DKIM_VALID_AU Message has a valid DKIM or DK signature from author's domain -0.1 DKIM_VALID Message has at least one valid DKIM or DK signature 0.1 DKIM_SIGNED Message has a DKIM or DK signature, not necessarily valid X-Headers-End: 1pzClu-0002IP-GH Subject: [Openvpn-devel] [PATCH 2/2 v2] test_buffer: add tests for buf_catrunc and its caller format_hex_ex 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?1761692341634278240?= X-GMAIL-MSGID: =?utf-8?q?1766130329082560900?= Just some very basic tests. v2: - fix off-by-one Change-Id: I73fc893136387d1da05f4aea98cb37b02d6c3230 Signed-off-by: Frank Lichtenheld Acked-by: Gert Doering --- tests/unit_tests/openvpn/test_buffer.c | 62 ++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) diff --git a/tests/unit_tests/openvpn/test_buffer.c b/tests/unit_tests/openvpn/test_buffer.c index 9e3b1d2e..bff50567 100644 --- a/tests/unit_tests/openvpn/test_buffer.c +++ b/tests/unit_tests/openvpn/test_buffer.c @@ -56,6 +56,66 @@ test_buffer_strprefix(void **state) assert_int_equal(BLEN(buf), strlen(str)); \ assert_memory_equal(BPTR(buf), str, BLEN(buf)); +static void +test_buffer_printf_catrunc(void **state) +{ + struct gc_arena gc = gc_new(); + struct buffer buf = alloc_buf_gc(16, &gc); + + buf_printf(&buf, "%d", 123); + buf_printf(&buf, "%s", "some text, too long to fit"); + assert_buf_equals_str(&buf, "123some text, t"); + + buf_catrunc(&buf, "..."); + assert_buf_equals_str(&buf, "123some text..."); + + buf_catrunc(&buf, "some other text, much too long to fit"); + assert_buf_equals_str(&buf, "123some text..."); + + buf_catrunc(&buf, "something else"); /* exactly right */ + assert_buf_equals_str(&buf, "1something else"); + + buf_catrunc(&buf, "something other"); /* 1 byte too long */ + assert_buf_equals_str(&buf, "1something else"); + + gc_free(&gc); +} + +static void +test_buffer_format_hex_ex(void **state) +{ + const int input_size = 10; + const uint8_t input[] = { + 0x01, 0x00, 0xff, 0x10, 0xff, 0x00, 0xf0, 0x0f, 0x09, 0x0a + }; + char *output; + struct gc_arena gc = gc_new(); + + int maxoutput = 0; + unsigned int blocksize = 5; + char *separator = " "; + output = format_hex_ex(input, input_size, maxoutput, blocksize, separator, &gc); + assert_string_equal(output, "0100ff10ff 00f00f090a"); + + maxoutput = 14; + output = format_hex_ex(input, input_size, maxoutput, blocksize, separator, &gc); + assert_string_equal(output, "0100[more...]"); + + maxoutput = 11; + output = format_hex_ex(input, input_size, maxoutput, blocksize, separator, &gc); + assert_string_equal(output, "0[more...]"); + + maxoutput = 10; + output = format_hex_ex(input, input_size, maxoutput, blocksize, separator, &gc); + assert_string_equal(output, "0100ff10f"); + + maxoutput = 9; + output = format_hex_ex(input, input_size, maxoutput, blocksize, separator, &gc); + assert_string_equal(output, "0100ff10"); + + gc_free(&gc); +} + struct test_buffer_list_aggregate_ctx { struct buffer_list *empty; struct buffer_list *one_two_three; @@ -267,6 +327,8 @@ main(void) { const struct CMUnitTest tests[] = { cmocka_unit_test(test_buffer_strprefix), + cmocka_unit_test(test_buffer_printf_catrunc), + cmocka_unit_test(test_buffer_format_hex_ex), cmocka_unit_test_setup_teardown(test_buffer_list_aggregate_separator_empty, test_buffer_list_setup, test_buffer_list_teardown),