From patchwork Mon Oct 13 15:47:53 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Gert Doering X-Patchwork-Id: 4503 Return-Path: Delivered-To: patchwork@openvpn.net Received: by 2002:a05:7000:7d42:b0:72f:f16c:e055 with SMTP id fr2csp1997241mab; Mon, 13 Oct 2025 08:48:14 -0700 (PDT) X-Forwarded-Encrypted: i=2; AJvYcCW7JnyspA1OCur0LMb8eHP8hxrxRb2Feg1cNzXlqrmtWVqVcTXAydzGB+V7ebrguNHl404azDPcwpE=@openvpn.net X-Google-Smtp-Source: AGHT+IGZ9Ot6K/aEsen8rLVMXQtPIeFNFDkygKXLf1wrpe/yzKKwUlKf7aprvEwbfZLFnPvspJH4 X-Received: by 2002:a05:6808:201f:b0:438:3def:446a with SMTP id 5614622812f47-4417b3fc68amr9895483b6e.45.1760370494656; Mon, 13 Oct 2025 08:48:14 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1760370494; cv=none; d=google.com; s=arc-20240605; b=iCLjC3w0hlwY1GLsvvhYyX5BFArHgM6v9GesvCrpWUxd8qiYunQwTaPDDFv8tclkzw ShbJrauRzB4Ouo+kcZTNhpWht9+iZkZNV93LUIC/BL3CP1SLOL34Rd1y4uZTlRHyAy8u PBZiUmIIZa39Fa6HCMvShokeN/yXSawubtzcqzo+WrT6/ucxa2uPoNa42cbBNYDXW6eU A31sSgk4Jl996bPjN1Eu+dxtH3MaUdNcKVC+erzrpHb9UGjvkUFJmfV/+AV/d6a78nz/ 9nZM0qbUEmj+FwrJLiA77NfEyzvEBOGZkrsyhBwCqDC9CjzK8NmRAh38OXp1h1zLu92N hIbA== 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:dkim-signature; bh=8m6diQ+4mNm2RBJNnaUszd956NrCuMpg9VNFg89Jg2I=; fh=4NbAC/LsuMLI0S0hprUlLSLCiHwg6SCAifhH718Jh0Q=; b=hsZXqIcccdGEKlD5bf0djRVXuFfGLZ34ZCLBp9KJbHpWL7r5GiPxW+5lyOkWDWs5N5 6+PoaQqyzJrcam++lD2lPggrVPs8hzB3KwvKcKLKnao5d67ZGvWy8sc/4fcPImEB6JpO b4M7De7eaInLOjRUN5BCxBK4oVL0GYSIQckQYh5MNG6/5/MFMCaul4tsn3+HUOEHmRS9 89GbrB4+q0GbNYt2p+VL0jgBJFNFKoKuUTZpePctd+JzZneG2J8tC8YHT4ii6w3DEyUZ wJ9Fx5rmKICha4fzJB/m3CyKw2AXgFT6abekFb2nifiwynYWH5GwS6KLy5a/FgfGTlgw Zi4w==; dara=google.com ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@lists.sourceforge.net header.s=beta header.b=OjdBTG4X; dkim=neutral (body hash did not verify) header.i=@sourceforge.net header.s=x header.b=cyiv3FZ3; dkim=neutral (body hash did not verify) header.i=@sf.net header.s=x header.b=ccOBl+bh; 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 5614622812f47-441d7f185a1si650850b6e.7.2025.10.13.08.48.14 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Mon, 13 Oct 2025 08:48:14 -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=pass header.i=@lists.sourceforge.net header.s=beta header.b=OjdBTG4X; dkim=neutral (body hash did not verify) header.i=@sourceforge.net header.s=x header.b=cyiv3FZ3; dkim=neutral (body hash did not verify) header.i=@sf.net header.s=x header.b=ccOBl+bh; 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 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.sourceforge.net; s=beta; h=Content-Transfer-Encoding:Content-Type: List-Subscribe:List-Help:List-Post:List-Archive:List-Unsubscribe:List-Id: Subject:MIME-Version:References:In-Reply-To:Message-ID:Date:To:From:Sender: Reply-To:Cc:Content-ID:Content-Description:Resent-Date:Resent-From: Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Owner; bh=8m6diQ+4mNm2RBJNnaUszd956NrCuMpg9VNFg89Jg2I=; b=OjdBTG4X8l6DkjHaZ1vPaUlYRX bCOHTQHBxvDeQ0zmiUOHpvAOqx+h311bRn1PKjNoGhs0ySfKH7TnKNwlYhAHhxdOuQpydrzsjSM1o clQyZp+qWaihskwNGnGX49CTGgTel0+7dCga3ZFkfKlwqH1QW4S9ScIqr/koV3j3hRsk=; 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 1v8KmR-0002gX-T9; Mon, 13 Oct 2025 15:48:12 +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 1v8KmR-0002gR-3D for openvpn-devel@lists.sourceforge.net; Mon, 13 Oct 2025 15:48:11 +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=vOw4/9fDtSfvfzVUHBCklMBK3sZ/wuFB8zoG7P7fSxQ=; b=cyiv3FZ3zgrabjyZA6hjNAI2Eg M01GCJwmQV38rWEEt3Ze3/jv8tlA/E5vLP2AH5IYYPKQQM5AW25ft+/nlevY+f6Q7l0gNgyvKYWNF Fg6bE0oHi5Nf1+NO4dbOXSbSNKPSRbzqQl/4hVOwjrmoAhRrHo1liFBj7qLCdH4q+Gpw=; 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=vOw4/9fDtSfvfzVUHBCklMBK3sZ/wuFB8zoG7P7fSxQ=; b=ccOBl+bhJ+RdhnIZgjEEZ/fHZ6 uzToTYuNHVbU8uuxVjeLvorFFxtpl9dLI8OpBBnFuz+z8ALyOCDTJ+eAr12Say4trhkPsrLa9T2Kb Ms3b83tz10RvTuhiUCFy+lizOab/PV8M2CEy/TQ9P2ZQBgYlTCaBsLoPqGuh+q6IhXOM=; Received: from [193.149.48.134] (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 1v8KmQ-0007SO-PH for openvpn-devel@lists.sourceforge.net; Mon, 13 Oct 2025 15:48:11 +0000 Received: from blue.greenie.muc.de (localhost [127.0.0.1]) by blue.greenie.muc.de (8.18.1/8.18.1) with ESMTP id 59DFlxLh021708 for ; Mon, 13 Oct 2025 17:47:59 +0200 Received: (from gert@localhost) by blue.greenie.muc.de (8.18.1/8.18.1/Submit) id 59DFlwGC021707 for openvpn-devel@lists.sourceforge.net; Mon, 13 Oct 2025 17:47:58 +0200 From: Gert Doering To: openvpn-devel@lists.sourceforge.net Date: Mon, 13 Oct 2025 17:47:53 +0200 Message-ID: <20251013154758.21695-1-gert@greenie.muc.de> X-Mailer: git-send-email 2.49.1 In-Reply-To: References: MIME-Version: 1.0 X-Spam-Score: 1.3 (+) X-Spam-Report: Spam detection software, running on the system "sfi-spamd-2.hosts.colo.sdot.me", 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: Frank Lichtenheld Use extra define to allow testing code only used on Windows but not actually dependent on Windows. Change-Id: I08e50030b1b692d351509f541e5c0b03b5170615 Signed-off-by: Frank Lichtenheld Acked-by: Gert Doering Gerrit URL: https://gerrit.openvpn.net/c/open [...] Content analysis details: (1.3 points, 5.0 required) pts rule name description ---- ---------------------- -------------------------------------------------- 1.3 RDNS_NONE Delivered to internal network by a host with no rDNS X-Headers-End: 1v8KmQ-0007SO-PH Subject: [Openvpn-devel] [PATCH v4] test_dhcp: Start a dhcp helper functions UT 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?1845882251718636373?= X-GMAIL-MSGID: =?utf-8?q?1845882251718636373?= From: Frank Lichtenheld Use extra define to allow testing code only used on Windows but not actually dependent on Windows. Change-Id: I08e50030b1b692d351509f541e5c0b03b5170615 Signed-off-by: Frank Lichtenheld Acked-by: Gert Doering Gerrit URL: https://gerrit.openvpn.net/c/openvpn/+/1267 --- 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/+/1267 This mail reflects revision 4 of this Change. Acked-by according to Gerrit (reflected above): Gert Doering diff --git a/CMakeLists.txt b/CMakeLists.txt index 9e0b46e..b773dcb 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -658,6 +658,7 @@ "test_auth_token" "test_buffer" "test_crypto" + "test_dhcp" "test_misc" "test_ncp" "test_options_parse" @@ -910,6 +911,11 @@ ) endif () + target_compile_definitions(test_dhcp PRIVATE DHCP_UNIT_TEST) + target_sources(test_dhcp PRIVATE + tests/unit_tests/openvpn/mock_get_random.c + ) + if (TARGET test_networking) target_link_options(test_networking PRIVATE -Wl,--wrap=parse_line) target_compile_options(test_networking PRIVATE -UNDEBUG) diff --git a/src/openvpn/dhcp.c b/src/openvpn/dhcp.c index 56f03f2..0893ec7 100644 --- a/src/openvpn/dhcp.c +++ b/src/openvpn/dhcp.c @@ -186,7 +186,7 @@ return 0; } -#if defined(_WIN32) +#if defined(_WIN32) || defined(DHCP_UNIT_TEST) #if defined(__GNUC__) || defined(__clang__) #pragma GCC diagnostic push diff --git a/src/openvpn/dhcp.h b/src/openvpn/dhcp.h index 3fcd2b6..2ad31a6 100644 --- a/src/openvpn/dhcp.h +++ b/src/openvpn/dhcp.h @@ -93,7 +93,7 @@ in_addr_t dhcp_extract_router_msg(struct buffer *ipbuf); -#if defined(_WIN32) +#if defined(_WIN32) || defined(DHCP_UNIT_TEST) #include "tun.h" bool build_dhcp_options_string(struct buffer *buf, const struct tuntap_options *o); diff --git a/src/openvpn/tun.h b/src/openvpn/tun.h index 6562c22..741798d 100644 --- a/src/openvpn/tun.h +++ b/src/openvpn/tun.h @@ -59,7 +59,7 @@ #define DCO_WIN_REFERENCE_STRING "ovpn-dco" #endif -#if defined(_WIN32) || defined(TARGET_ANDROID) +#if defined(_WIN32) || defined(TARGET_ANDROID) || defined(DHCP_UNIT_TEST) #define TUN_ADAPTER_INDEX_INVALID ((DWORD)-1) diff --git a/tests/unit_tests/openvpn/Makefile.am b/tests/unit_tests/openvpn/Makefile.am index 50f4a11..17aa0ce 100644 --- a/tests/unit_tests/openvpn/Makefile.am +++ b/tests/unit_tests/openvpn/Makefile.am @@ -6,7 +6,7 @@ AM_TESTS_ENVIRONMENT = export LSAN_OPTIONS=suppressions=$(srcdir)/input/leak_suppr.txt; -test_binaries = argv_testdriver buffer_testdriver crypto_testdriver packet_id_testdriver auth_token_testdriver \ +test_binaries = argv_testdriver buffer_testdriver crypto_testdriver dhcp_testdriver packet_id_testdriver auth_token_testdriver \ ncp_testdriver misc_testdriver options_parse_testdriver pkt_testdriver ssl_testdriver \ user_pass_testdriver push_update_msg_testdriver provider_testdriver socket_testdriver @@ -72,6 +72,15 @@ $(top_srcdir)/src/openvpn/win32-util.c \ $(top_srcdir)/src/openvpn/mss.c +dhcp_testdriver_CFLAGS = -I$(top_srcdir)/src/openvpn -I$(top_srcdir)/src/compat @TEST_CFLAGS@ -DDHCP_UNIT_TEST +dhcp_testdriver_LDFLAGS = @TEST_LDFLAGS@ -L$(top_srcdir)/src/openvpn +dhcp_testdriver_SOURCES = test_dhcp.c \ + mock_msg.c mock_msg.h test_common.h \ + mock_get_random.c \ + $(top_srcdir)/src/openvpn/platform.c \ + $(top_srcdir)/src/openvpn/buffer.c \ + $(top_srcdir)/src/openvpn/win32-util.c + ssl_testdriver_CFLAGS = \ -I$(top_srcdir)/include -I$(top_srcdir)/src/compat -I$(top_srcdir)/src/openvpn \ @TEST_CFLAGS@ diff --git a/tests/unit_tests/openvpn/mock_msg.h b/tests/unit_tests/openvpn/mock_msg.h index c6321dc..62afde5 100644 --- a/tests/unit_tests/openvpn/mock_msg.h +++ b/tests/unit_tests/openvpn/mock_msg.h @@ -23,6 +23,8 @@ #ifndef MOCK_MSG_H #define MOCK_MSG_H +#include "error.h" + /** * Mock debug level defaults to 0, which gives clean(-ish) test reports. Call * this function from your test driver to increase debug output when you diff --git a/tests/unit_tests/openvpn/test_dhcp.c b/tests/unit_tests/openvpn/test_dhcp.c new file mode 100644 index 0000000..729ca58 --- /dev/null +++ b/tests/unit_tests/openvpn/test_dhcp.c @@ -0,0 +1,134 @@ +/* + * 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) 2025 OpenVPN Inc + * + * 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, see . + */ + +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + +#include "syshead.h" + +#include +#include +#include +#include +#include +#include + +#include "test_common.h" +#include "mock_msg.h" + +#include "dhcp.c" + +uint16_t +ip_checksum(const sa_family_t af, const uint8_t *payload, const int len_payload, + const uint8_t *src_addr, const uint8_t *dest_addr, const int proto) +{ + return 0; +} +const char * +print_in_addr_t(in_addr_t addr, unsigned int flags, struct gc_arena *gc) +{ + return "dummy"; +} + +static void +test_write_dhcp_search_str(void **state) +{ + struct gc_arena gc = gc_new(); + struct buffer out_buf = alloc_buf_gc(512, &gc); + struct buffer clear_buf = alloc_buf_gc(512, &gc); + buf_clear(&clear_buf); + bool error = false; + +#define LONGDOMAIN "a-reaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaally-long-domain" + const char *search_list[] = { + "openvpn.net", + "openvpn.org", + LONGDOMAIN, + "subdomain." LONGDOMAIN ".top123", /* maximum length */ + "subdomain-" LONGDOMAIN "-top123", /* maximum length */ + "subdomain." LONGDOMAIN ".top1234", /* too long */ + "sub..tld", /* invalid */ + }; + const unsigned char output_1[28] = "\x77\x1a\x07openvpn\x03net\x00\x07openvpn\x03org"; + buf_clear(&out_buf); + write_dhcp_search_str(&out_buf, DHCP_DOMAIN_SEARCH, search_list, 2, &error); + assert_memory_equal(BPTR(&out_buf), output_1, sizeof(output_1)); + assert_false(error); + + /* buf too small */ + struct buffer small_buf = alloc_buf_gc(sizeof(output_1) - 1, &gc); + buf_clear(&small_buf); + write_dhcp_search_str(&small_buf, DHCP_DOMAIN_SEARCH, search_list, 2, &error); + assert_memory_equal(BPTR(&small_buf), BPTR(&clear_buf), buf_forward_capacity_total(&small_buf)); + assert_true(error); + error = false; + + const unsigned char output_2[0xEC + 3 + 1] = "\x77\xEE\xEC" LONGDOMAIN; + buf_clear(&out_buf); + write_dhcp_search_str(&out_buf, DHCP_DOMAIN_SEARCH, search_list + 2, 1, &error); + assert_memory_equal(BPTR(&out_buf), output_2, sizeof(output_2)); + assert_false(error); + + const unsigned char output_3[0xEC + 3 + 10 + 7 + 1] = "\x77\xFF\x09subdomain\xEC" LONGDOMAIN "\x06top123"; + buf_clear(&out_buf); + write_dhcp_search_str(&out_buf, DHCP_DOMAIN_SEARCH, search_list + 3, 1, &error); + assert_memory_equal(BPTR(&out_buf), output_3, sizeof(output_3)); + assert_false(error); + + const unsigned char output_4[0xEC + 3 + 10 + 7 + 1] = "\x77\xFF\xFDsubdomain-" LONGDOMAIN "-top123"; + buf_clear(&out_buf); + write_dhcp_search_str(&out_buf, DHCP_DOMAIN_SEARCH, search_list + 4, 1, &error); + assert_memory_equal(BPTR(&out_buf), output_4, sizeof(output_4)); + assert_false(error); + + buf_clear(&out_buf); + write_dhcp_search_str(&out_buf, DHCP_DOMAIN_SEARCH, search_list + 5, 1, &error); + assert_memory_equal(BPTR(&out_buf), BPTR(&clear_buf), buf_forward_capacity_total(&clear_buf)); + assert_true(error); + error = false; + + buf_clear(&out_buf); + write_dhcp_search_str(&out_buf, DHCP_DOMAIN_SEARCH, search_list, 3, &error); + assert_memory_equal(BPTR(&out_buf), BPTR(&clear_buf), buf_forward_capacity_total(&clear_buf)); + assert_true(error); + error = false; + + /* FIXME: should probably throw an error instead adding that \x00 ? */ + const char output_5[12] = "\x77\x0a\x03sub\x00\x03tld"; + buf_clear(&out_buf); + write_dhcp_search_str(&out_buf, DHCP_DOMAIN_SEARCH, search_list + 6, 1, &error); + assert_memory_equal(BPTR(&out_buf), output_5, sizeof(output_5)); + assert_false(error); + + gc_free(&gc); +} + +int +main(void) +{ + const struct CMUnitTest tests[] = { + cmocka_unit_test(test_write_dhcp_search_str), + }; + + return cmocka_run_group_tests_name("dhcp", tests, NULL, NULL); +}