From patchwork Sun Aug 31 15:11:26 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Gert Doering X-Patchwork-Id: 4381 Return-Path: Delivered-To: patchwork@openvpn.net Received: by 2002:a05:7000:2a1c:b0:671:5a2c:6455 with SMTP id k28csp680594maz; Sun, 31 Aug 2025 08:11:55 -0700 (PDT) X-Forwarded-Encrypted: i=2; AJvYcCVbUyq+WCYDZ6FNbzJW6hrD9yossY+BaIRtxbcEoQBhZ3QsHQKrU/VVxcIRdQSJvVyEAIEwUSEwOog=@openvpn.net X-Google-Smtp-Source: AGHT+IEoRJhxCrw/QoOarcuODgs6kqJxC3OPmBPATPsS12B/+8pkY9foNm1mAHEZNZr3E91VQg1i X-Received: by 2002:a05:6870:b0a:b0:314:b6a6:685f with SMTP id 586e51a60fabf-3196348086fmr2651719fac.45.1756653114822; Sun, 31 Aug 2025 08:11:54 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1756653114; cv=none; d=google.com; s=arc-20240605; b=iUFCCG+apA2RPaG1bifwH4pabkZHRk8rz9MOJ2dpnJbHkesXtIi8VUVH2l2r5RW8Lu WVzctu2NYIiH0788iFOndpezuT4vTFlf30vKBrB8GgG+r0jWJflMGbwK0jhJcOK3pFYa /yGIN+2e+3T4tPPe15foewx8PBSVtekyEt+v3pK68avFfmnSlCS67OzxPMDPViQh97SS Xpa5hviBzOfGbIcAUu8g2la8V5TkBHxfvZ/fzr1J2vnmrZ2gFloi3thw4RPsPqsdSj+f Tf1T9ynYuEnk6+1dHgNTIlgwrqhiVHYzLl4h1CUB8/avEbcoVEaL2+prUIwCZPcPwrXz r7Pw== 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=ThZynQQlASyfEHsxbmb6VnvJfpRNXPOHQwRQwoJeWiE=; fh=4NbAC/LsuMLI0S0hprUlLSLCiHwg6SCAifhH718Jh0Q=; b=Y2LhBYVz4hKR5iyNBKKzFHZ4iaBJ+FmfdvP8pDBfbYjuly0ek8gjNnCSr9Tk+I4fp9 jjKOjnnvnUfuZb1je6IQ941lVRYZyIM1HWo5ziDS5D/OxpAczZvlnqjLOz8ZdC/rdkuM ofkObcOG0Qld9uLVpbKnQZMJ8CafAkmWTbYnWLTE6d2UdzMcEt3sUT/cnqJTr0FtO9tk uE0/zyGXXEXHph84s8zKSubFBsxvhYwwg8HNY5Qkn6+QAM1hlrB6DyG7EANRO8nxUpmu iH0+gtAqHFtbiyjuhlvOBtRzXobKjB8QWfnIHp3eZthNQtgoP7zL9jF2oFww6a5sDyK1 cfqw==; dara=google.com ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@lists.sourceforge.net header.s=beta header.b="Qfvi/Pyg"; dkim=neutral (body hash did not verify) header.i=@sourceforge.net header.s=x header.b=CNN42qfi; dkim=neutral (body hash did not verify) header.i=@sf.net header.s=x header.b=FEhGn4A+; 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 586e51a60fabf-3196d3738c3si677287fac.253.2025.08.31.08.11.54 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Sun, 31 Aug 2025 08:11:54 -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="Qfvi/Pyg"; dkim=neutral (body hash did not verify) header.i=@sourceforge.net header.s=x header.b=CNN42qfi; dkim=neutral (body hash did not verify) header.i=@sf.net header.s=x header.b=FEhGn4A+; 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=ThZynQQlASyfEHsxbmb6VnvJfpRNXPOHQwRQwoJeWiE=; b=Qfvi/Pyg7xCqxuX2xT/oAf+15j MiBcx40jfOR1lusBsbLNEH+XA5K6Qx0GV7bOn+W0Ync+BW7J2UpqthpTP7ZiwhqgbwFL2MYSWppWg 2kvxNXQTz1nybqQXl/n9AbCKQryHbLw/4+Ugqh5fE2HCVem8c8B4J+wPhrIstcOTsiUU=; 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 1usjif-0007mk-1W; Sun, 31 Aug 2025 15:11:49 +0000 Received: from [172.30.29.66] (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 1usjic-0007me-W7 for openvpn-devel@lists.sourceforge.net; Sun, 31 Aug 2025 15:11:47 +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=eSoNxPEspLLMUDPRn/547Wc+XeInBownq/GtT04GhCQ=; b=CNN42qfiLqEVo6I4OqmlXzjoM1 7d2zDWvFTdQJeqFwr2ssURtoQdiwVZr4Cyi6hIBBDqxpKmjd3XE6A4D5Xs4TVB7od23ckqNFMhlhb zFCF/kmP/VDrV+/oUKS+A85g9HTN1JbKIRqb43n1dU9feR0DUmFI2dx2phL9Fg8HoVRs=; 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=eSoNxPEspLLMUDPRn/547Wc+XeInBownq/GtT04GhCQ=; b=FEhGn4A+En65CyKS4r1B2jVwLV dJR88htbgWgwOolzt0g7LCzoSlky/Fjc3ioTF0Xuj6GpACv1XSlrZJsJvkakV7CbKjfLcqEXEzhGW oXFnPLmoxZH5Dhy6qCKpIbBOnD54FtBEH93YEgUAVjcAIRRmp8MSh+sGqk+SMv6qbADg=; Received: from [193.149.48.143] (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 1usjib-000255-Lk for openvpn-devel@lists.sourceforge.net; Sun, 31 Aug 2025 15:11:46 +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 57VFBXGv025698 for ; Sun, 31 Aug 2025 17:11:33 +0200 Received: (from gert@localhost) by blue.greenie.muc.de (8.18.1/8.18.1/Submit) id 57VFBXW2025697 for openvpn-devel@lists.sourceforge.net; Sun, 31 Aug 2025 17:11:33 +0200 From: Gert Doering To: openvpn-devel@lists.sourceforge.net Date: Sun, 31 Aug 2025 17:11:26 +0200 Message-ID: <20250831151133.25684-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 With a first UT that tests add_in6_addr() (and print_in6_addr implicitly). Change-Id: If546f64a4554b292623bfcfe9ee53bac17dfa803 Signed-off-by: Frank Lichtenheld Acked-by: Gert Doering --- 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: 1usjib-000255-Lk Subject: [Openvpn-devel] [PATCH v4] Add new unit test module test_socket 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?1841984296427945700?= X-GMAIL-MSGID: =?utf-8?q?1841984296427945700?= From: Frank Lichtenheld With a first UT that tests add_in6_addr() (and print_in6_addr implicitly). Change-Id: If546f64a4554b292623bfcfe9ee53bac17dfa803 Signed-off-by: Frank Lichtenheld Acked-by: Gert Doering --- 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/+/1160 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 45044af..35513e9 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -628,9 +628,11 @@ target_compile_options(openvpn PRIVATE -DPLUGIN_LIBDIR=\"${PLUGIN_DIR}\") find_library(resolv resolv) - # some platform like BSDs already include resolver functionality in the libc and not have an extra resolv library + # some platform like BSDs already include resolver functionality in the libc + # and do not have an extra resolv library if (${resolv} OR APPLE) - target_link_libraries(openvpn PUBLIC -lresolv) + set(RESOLV_LIBRARIES resolv) + target_link_libraries(openvpn PUBLIC ${RESOLV_LIBRARIES}) endif () endif () @@ -653,6 +655,7 @@ "test_packet_id" "test_pkt" "test_provider" + "test_socket" "test_ssl" "test_user_pass" "test_push_update_msg" @@ -849,6 +852,16 @@ src/openvpn/base64.c ) + target_link_libraries(test_socket PUBLIC ${RESOLV_LIBRARIES}) + target_sources(test_socket PRIVATE + tests/unit_tests/openvpn/mock_get_random.c + tests/unit_tests/openvpn/mock_management.c + tests/unit_tests/openvpn/mock_win32_execve.c + src/openvpn/env_set.c + src/openvpn/run_command.c + src/openvpn/socket_util.c + ) + target_sources(test_user_pass PRIVATE tests/unit_tests/openvpn/mock_get_random.c tests/unit_tests/openvpn/mock_win32_execve.c diff --git a/tests/unit_tests/openvpn/Makefile.am b/tests/unit_tests/openvpn/Makefile.am index b24e03c..d030185 100644 --- a/tests/unit_tests/openvpn/Makefile.am +++ b/tests/unit_tests/openvpn/Makefile.am @@ -4,23 +4,17 @@ AM_TESTSUITE_SUMMARY_HEADER = ' for $(PACKAGE_STRING) Unit-Tests' -test_binaries= +test_binaries = crypto_testdriver packet_id_testdriver auth_token_testdriver \ + ncp_testdriver misc_testdriver pkt_testdriver ssl_testdriver \ + user_pass_testdriver push_update_msg_testdriver provider_testdriver socket_testdriver if HAVE_LD_WRAP_SUPPORT test_binaries += argv_testdriver buffer_testdriver -endif - -test_binaries += crypto_testdriver packet_id_testdriver auth_token_testdriver ncp_testdriver misc_testdriver \ - pkt_testdriver ssl_testdriver user_pass_testdriver push_update_msg_testdriver - -if HAVE_LD_WRAP_SUPPORT if !WIN32 test_binaries += tls_crypt_testdriver endif endif -test_binaries += provider_testdriver - if WIN32 test_binaries += cryptoapi_testdriver LDADD = -lws2_32 @@ -343,4 +337,22 @@ $(top_srcdir)/src/openvpn/platform.c \ $(top_srcdir)/src/openvpn/push_util.c \ $(top_srcdir)/src/openvpn/options_util.c \ - $(top_srcdir)/src/openvpn/otime.c \ No newline at end of file + $(top_srcdir)/src/openvpn/otime.c + +socket_testdriver_CFLAGS = \ + -I$(top_srcdir)/include -I$(top_srcdir)/src/compat -I$(top_srcdir)/src/openvpn \ + -DSOURCEDIR=\"$(top_srcdir)\" @TEST_CFLAGS@ + +socket_testdriver_LDFLAGS = @TEST_LDFLAGS@ $(SOCKETS_LIBS) + +socket_testdriver_SOURCES = test_socket.c \ + mock_msg.c test_common.h \ + mock_get_random.c \ + mock_management.c \ + $(top_srcdir)/src/openvpn/buffer.c \ + $(top_srcdir)/src/openvpn/win32-util.c \ + $(top_srcdir)/src/openvpn/platform.c \ + $(top_srcdir)/src/openvpn/env_set.c \ + $(top_srcdir)/src/openvpn/run_command.c \ + $(top_srcdir)/src/openvpn/socket_util.c + diff --git a/tests/unit_tests/openvpn/mock_management.c b/tests/unit_tests/openvpn/mock_management.c index b24e4c4..28e541c 100644 --- a/tests/unit_tests/openvpn/mock_management.c +++ b/tests/unit_tests/openvpn/mock_management.c @@ -46,4 +46,18 @@ { return NULL; } + +void +management_set_state(struct management *man, const int state, const char *detail, + const in_addr_t *tun_local_ip, const struct in6_addr *tun_local_ip6, + const struct openvpn_sockaddr *local_addr, + const struct openvpn_sockaddr *remote_addr) +{ +} + #endif + +void +management_sleep(const int n) +{ +} diff --git a/tests/unit_tests/openvpn/test_socket.c b/tests/unit_tests/openvpn/test_socket.c new file mode 100644 index 0000000..2da2529 --- /dev/null +++ b/tests/unit_tests/openvpn/test_socket.c @@ -0,0 +1,126 @@ +/* + * 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) 2021-2025 Arne Schwabe + * + * 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 "socket.h" +#include "win32.h" + +/* stubs for some unused functions instead of pulling in too many dependencies */ +struct signal_info siginfo_static; /* GLOBAL */ + +int +signal_reset(struct signal_info *si, int signum) +{ + assert_true(0); + return 0; +} + +#ifdef _WIN32 +struct win32_signal win32_signal; /* GLOBAL */ + +int +win32_signal_get(struct win32_signal *ws) +{ + assert_true(0); + return 0; +} +#endif + +int +parse_line(const char *line, char **p, const int n, const char *file, const int line_num, + int msglevel, struct gc_arena *gc) +{ + assert_true(0); + return 0; +} + +static void +test_add_in6_addr_tc(const char *orig_str, uint32_t add, const char *expect_str) +{ + struct in6_addr orig, result, expected; + struct gc_arena gc = gc_new(); + assert_int_equal(inet_pton(AF_INET6, orig_str, &orig), 1); + assert_int_equal(inet_pton(AF_INET6, expect_str, &expected), 1); + result = add_in6_addr(orig, add); + const char *result_str = print_in6_addr(result, 0, &gc); + assert_string_equal(result_str, expect_str); + assert_memory_equal(&result, &expected, sizeof(struct in6_addr)); + gc_free(&gc); +} + +static bool +check_mapped_ipv4_address(void) +{ + struct gc_arena gc = gc_new(); + const char *ipv4_output = "::255.255.255.255"; + struct in6_addr addr; + assert_int_equal(inet_pton(AF_INET6, ipv4_output, &addr), 1); + const char *test_output = print_in6_addr(addr, 0, &gc); + bool ret = strcmp(test_output, ipv4_output) == 0; + gc_free(&gc); + return ret; +} + +static void +test_add_in6_addr(void **state) +{ + /* Note that some of the result strings need to account for + print_in6_addr formatting the addresses potentially as IPv4 */ + bool mapped_ipv4 = check_mapped_ipv4_address(); + test_add_in6_addr_tc("::", 1, "::1"); + test_add_in6_addr_tc("::ff", 1, "::100"); + test_add_in6_addr_tc("::ffff", 1, mapped_ipv4 ? "::0.1.0.0" : "::1:0"); + test_add_in6_addr_tc("ffff::ffff", 1, "ffff::1:0"); + test_add_in6_addr_tc("::ffff:ffff", 1, "::1:0:0"); + test_add_in6_addr_tc("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff", 1, "::"); + test_add_in6_addr_tc("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff", 2, "::1"); + + test_add_in6_addr_tc("::", UINT32_MAX, mapped_ipv4 ? "::255.255.255.255" : "::ffff:ffff"); + test_add_in6_addr_tc("::1", UINT32_MAX, "::1:0:0"); + test_add_in6_addr_tc("::ffff", UINT32_MAX, "::1:0:fffe"); + test_add_in6_addr_tc("ffff::ffff", UINT32_MAX, "ffff::1:0:fffe"); + test_add_in6_addr_tc("::ffff:ffff", UINT32_MAX, "::1:ffff:fffe"); + test_add_in6_addr_tc("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff", UINT32_MAX, + mapped_ipv4 ? "::255.255.255.254" : "::ffff:fffe"); +} + +const struct CMUnitTest socket_tests[] = { + cmocka_unit_test(test_add_in6_addr) +}; + +int +main(void) +{ + return cmocka_run_group_tests(socket_tests, NULL, NULL); +}