Attention is currently required from: plaisthos.

Hello plaisthos,

I'd like you to do a code review.
Please visit

    http://gerrit.openvpn.net/c/openvpn/+/1459?usp=email

to review the following change.


Change subject: openvpnserv: Add a first unit test
......................................................................

openvpnserv: Add a first unit test

This adds the required build infrastructure
and adds tests for two functions related to
GetItfDnsDomains().

Change-Id: I33583e51e1143c53fbe0aef16546fa3f602b17c0
Signed-off-by: Frank Lichtenheld <[email protected]>
---
M src/openvpnserv/CMakeLists.txt
M tests/unit_tests/Makefile.am
M tests/unit_tests/openvpn/test_common.h
A tests/unit_tests/openvpnserv/Makefile.am
A tests/unit_tests/openvpnserv/test_openvpnserv.c
5 files changed, 172 insertions(+), 2 deletions(-)



  git pull ssh://gerrit.openvpn.net:29418/openvpn refs/changes/59/1459/1

diff --git a/src/openvpnserv/CMakeLists.txt b/src/openvpnserv/CMakeLists.txt
index 2c8f310..67a89ef 100644
--- a/src/openvpnserv/CMakeLists.txt
+++ b/src/openvpnserv/CMakeLists.txt
@@ -74,3 +74,44 @@
 endif()

 add_dependencies(openvpnserv msg_mc_gen)
+
+if (BUILD_TESTING)
+    set(unit_tests
+        "test_openvpnserv"
+    )
+
+    foreach (test_name ${unit_tests})
+        add_test(${test_name} ${test_name})
+        add_executable(${test_name}
+            ../../tests/unit_tests/openvpnserv/${test_name}.c
+            ${MC_GEN_DIR}/eventmsg.h
+        )
+
+        target_link_libraries(${test_name} PUBLIC ${CMOCKA_LIBRARIES})
+        target_include_directories(${test_name} PRIVATE
+            .
+            ../../include
+            ../openvpn
+            ${MC_GEN_DIR}
+            ../../tests/unit_tests/openvpn
+        )
+        target_compile_options(${test_name} PRIVATE
+            -D_UNICODE
+            -UNTDDI_VERSION
+            -D_WIN32_WINNT=_WIN32_WINNT_VISTA
+        )
+        target_link_libraries(${test_name} PRIVATE
+            advapi32.lib userenv.lib iphlpapi.lib fwpuclnt.lib rpcrt4.lib
+            shlwapi.lib netapi32.lib ws2_32.lib ntdll.lib ole32.lib 
pathcch.lib)
+        if (MINGW)
+            target_compile_options(${test_name} PRIVATE -municode)
+            target_link_options(${test_name} PRIVATE -municode)
+        endif ()
+    endforeach()
+
+    target_sources(test_openvpnserv PRIVATE
+        common.c
+        validate.c
+        ../openvpn/wfp_block.c ../openvpn/wfp_block.h
+    )
+endif ()
diff --git a/tests/unit_tests/Makefile.am b/tests/unit_tests/Makefile.am
index 33fefaa..ce6278e 100644
--- a/tests/unit_tests/Makefile.am
+++ b/tests/unit_tests/Makefile.am
@@ -1,5 +1,5 @@
 AUTOMAKE_OPTIONS = foreign

 if ENABLE_UNITTESTS
-SUBDIRS = example_test openvpn plugins
+SUBDIRS = example_test openvpn openvpnserv plugins
 endif
diff --git a/tests/unit_tests/openvpn/test_common.h 
b/tests/unit_tests/openvpn/test_common.h
index 3a0598d..4153ca3 100644
--- a/tests/unit_tests/openvpn/test_common.h
+++ b/tests/unit_tests/openvpn/test_common.h
@@ -20,8 +20,9 @@
  *  with this program; if not, see <https://www.gnu.org/licenses/>.
  */

-#include <stdio.h>
 #include <setjmp.h>
+#include <stdio.h>
+#include <stdlib.h>
 #include <cmocka.h>

 /* Do we use cmocka < 2.0.0? */
diff --git a/tests/unit_tests/openvpnserv/Makefile.am 
b/tests/unit_tests/openvpnserv/Makefile.am
new file mode 100644
index 0000000..5758cd4
--- /dev/null
+++ b/tests/unit_tests/openvpnserv/Makefile.am
@@ -0,0 +1,15 @@
+AUTOMAKE_OPTIONS = foreign
+
+EXTRA_DIST = input
+
+AM_TESTSUITE_SUMMARY_HEADER = ' for $(PACKAGE_STRING) service Unit-Tests'
+
+if WIN32
+test_binaries = openvpnserv_testdriver
+endif
+
+openvpnserv_testdriver_CFLAGS  = -I$(top_srcdir)/include 
-I$(top_srcdir)/src/openvpn -I$(top_srcdir)/src/compat 
-I$(top_srcdir)/src/openvpnserv @TEST_CFLAGS@
+openvpnserv_testdriver_LDFLAGS = @TEST_LDFLAGS@ -L$(top_srcdir)/src/openvpn
+openvpnserv_testdriver_SOURCES = test_openvpnserv.c \
+       $(top_srcdir)/src/openvpnserv/common.c
+
diff --git a/tests/unit_tests/openvpnserv/test_openvpnserv.c 
b/tests/unit_tests/openvpnserv/test_openvpnserv.c
new file mode 100644
index 0000000..348de4f
--- /dev/null
+++ b/tests/unit_tests/openvpnserv/test_openvpnserv.c
@@ -0,0 +1,113 @@
+/*
+ *  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 Frank Lichtenheld <[email protected]>
+ *
+ *  This program is free software; you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by the
+ *  Free Software Foundation, either version 2 of the License,
+ *  or (at your option) any later version.
+ *
+ *  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 <https://www.gnu.org/licenses/>.
+ */
+
+#include <setjmp.h>
+#include <cmocka.h>
+#include "test_common.h"
+
+#include <winsock2.h>
+#include <windows.h>
+
+#include "interactive.c"
+
+BOOL
+ReportStatusToSCMgr(SERVICE_STATUS_HANDLE service, SERVICE_STATUS *status)
+{
+    return TRUE;
+}
+
+static void
+test_list_contains_domain(void **state)
+{
+    PCWSTR domain = L"openvpn.net";
+    size_t domain_len = wcslen(domain);
+    assert_true(ListContainsDomain(domain, domain, domain_len));
+    assert_true(ListContainsDomain(L"openvpn.com,openvpn.net", domain, 
domain_len));
+    assert_true(ListContainsDomain(L"openvpn.net,openvpn.com", domain, 
domain_len));
+
+    assert_false(ListContainsDomain(L"openvpn.com", domain, domain_len));
+    assert_false(ListContainsDomain(L"internal.openvpn.net", domain, 
domain_len));
+}
+
+#define BUF_SIZE 64
+static void
+test_convert_itf_dns_domains(void **state)
+{
+    DWORD size, orig_size, len, res_len;
+    LSTATUS err;
+    const DWORD glyph_size = sizeof(wchar_t);
+
+    wchar_t domains_1[BUF_SIZE] = L"openvpn.com";
+    len = (DWORD)wcslen(domains_1) + 1;
+    size = orig_size = len * glyph_size;
+    wchar_t domains_1_res[BUF_SIZE] = L".openvpn.com";
+    res_len = len + 2; /* adds . and \0 */
+    err = ConvertItfDnsDomains(L"openvpn.net", domains_1, &size, BUF_SIZE);
+    assert_memory_equal(domains_1, domains_1_res, size);
+    assert_int_equal(size, res_len * glyph_size);
+    assert_int_equal(err, NO_ERROR);
+
+    wchar_t domains_2[BUF_SIZE] = L"openvpn.com,openvpn.net";
+    len = (DWORD)wcslen(domains_2) + 1;
+    size = orig_size = len * glyph_size;
+    wchar_t domains_2_res[BUF_SIZE] = L".openvpn.com";
+    res_len = (DWORD)wcslen(domains_2_res) + 2;
+    err = ConvertItfDnsDomains(L"openvpn.net", domains_2, &size, BUF_SIZE);
+    assert_memory_equal(domains_2, domains_2_res, size);
+    assert_int_equal(size, res_len * glyph_size);
+    assert_int_equal(err, NO_ERROR);
+
+    wchar_t domains_3[BUF_SIZE] = L"openvpn.com,openvpn.net";
+    len = (DWORD)wcslen(domains_3) + 1;
+    size = orig_size = len * glyph_size;
+    wchar_t domains_3_res[BUF_SIZE] = L".openvpn.net";
+    res_len = (DWORD)wcslen(domains_3_res) + 2;
+    err = ConvertItfDnsDomains(L"openvpn.com", domains_3, &size, BUF_SIZE);
+    assert_memory_equal(domains_3, domains_3_res, size);
+    assert_int_equal(size, res_len * glyph_size);
+    assert_int_equal(err, NO_ERROR);
+
+    wchar_t domains_4[BUF_SIZE] = L"openvpn.com,openvpn.net";
+    len = (DWORD)wcslen(domains_4) + 1;
+    size = orig_size = len * glyph_size;
+    wchar_t domains_4_res[BUF_SIZE] = L".openvpn.com\0.openvpn.net";
+    res_len = len + 3; /* adds two . and one \0 */
+    err = ConvertItfDnsDomains(NULL, domains_4, &size, BUF_SIZE);
+    assert_memory_equal(domains_4, domains_4_res, size);
+    assert_int_equal(size, res_len * glyph_size);
+    assert_int_equal(err, NO_ERROR);
+}
+
+int
+wmain(void)
+{
+    openvpn_unit_test_setup();
+    const struct CMUnitTest tests[] = {
+        cmocka_unit_test(test_list_contains_domain),
+        cmocka_unit_test(test_convert_itf_dns_domains),
+    };
+
+    int ret = cmocka_run_group_tests_name("openvpnserv tests", tests, NULL, 
NULL);
+
+    return ret;
+}

--
To view, visit http://gerrit.openvpn.net/c/openvpn/+/1459?usp=email
To unsubscribe, or for help writing mail filters, visit 
http://gerrit.openvpn.net/settings?usp=email

Gerrit-MessageType: newchange
Gerrit-Project: openvpn
Gerrit-Branch: master
Gerrit-Change-Id: I33583e51e1143c53fbe0aef16546fa3f602b17c0
Gerrit-Change-Number: 1459
Gerrit-PatchSet: 1
Gerrit-Owner: flichtenheld <[email protected]>
Gerrit-Reviewer: plaisthos <[email protected]>
Gerrit-CC: openvpn-devel <[email protected]>
Gerrit-Attention: plaisthos <[email protected]>
_______________________________________________
Openvpn-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/openvpn-devel

Reply via email to