Il giorno ven 6 nov 2026 alle ore 18:26 Samuel Thibault
<[email protected]> ha scritto:
>
> Hello,
>
> Better auto-enable tests when cmoka and libmicrohttpd are found.

Addressed.

> Rathe make the list of -l flags a variable shared by the root
> Makefile.am and this Makefile.am

Addressed.

> These variables don't seem to be actually used? Do you plan to use them?

I have to declare all these variables because the linker complained
they were undefined, although I want to remove as many global
variables as possible from the translator in future patches.

If instead you mean only TEST_PORT, is a leftover because I wasn't
sure if to include a test case also for open_connection in the first
patch. You can remove it before applying the patch or leave it there
for the time being.

> Why static?

Just a leftover. I have removed the static keyword.

> Samuel

Hello Samuel, I think I have addressed all your comments in a v3 patch
attached to this email.

Thank you again for your feedback.

Gianluca
diff --git a/Makefile.am b/Makefile.am
index 262425d..84b88dc 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -2,7 +2,9 @@
 
 bin_PROGRAMS=httpfs
 
-httpfs_LDADD=$(LIBS) -lhurdbugaddr -lnetfs -lfshelp -lpthread -lports -liohelp -lshouldbeinlibc
+HTTPFS_SHARED_LIBS = -lxml2 -lpthread -lnetfs -lfshelp -lports -liohelp -lshouldbeinlibc -lhurdbugaddr
+
+httpfs_LDADD = $(HTTPFS_SHARED_LIBS) $(LIBS)
 httpfs_SOURCES=httpfs.c \
 	netfs.c \
 	args.c \
@@ -12,3 +14,10 @@ httpfs_SOURCES=httpfs.c \
 	node.c \
 	httpfs.h
 INCLUDES = -I@includedir@
+
+SUBDIRS = . tests
+
+tests:
+	$(MAKE) check
+
+.PHONY: tests
diff --git a/configure.in b/configure.in
index 8de56fb..0643c19 100644
--- a/configure.in
+++ b/configure.in
@@ -6,12 +6,44 @@ AC_PROG_CC
 
 PKG_CHECK_MODULES(HTTPFS, libxml-2.0)
 
+HTTPFS_SHARED_LIBS="-lxml2 -lpthread -lnetfs -lfshelp -lports -liohelp -lshouldbeinlibc -lhurdbugaddr"
+AC_SUBST([HTTPFS_SHARED_LIBS])
+
+# Optional test suite integration (auto-enabled if dependencies are found)
+AC_ARG_ENABLE([test],
+    AS_HELP_STRING([--enable-test], [Enable test compilation (cmocka and libmicrohttpd) [default=auto]]),
+    [enable_test=$enableval],
+    [enable_test=auto])
+
+have_test=no
+AS_IF([test "x$enable_test" != "xno"], [
+    PKG_CHECK_MODULES([MHD], [libmicrohttpd >= 0.9.0], [
+        PKG_CHECK_MODULES([CMOCKA], [cmocka >= 1.1.0], [
+            have_test=yes
+        ], [
+            AS_IF([test "x$enable_test" = "xyes"], [
+                AC_MSG_ERROR([cmocka not found, but --enable-test was requested.])
+            ])
+        ])
+    ], [
+        AS_IF([test "x$enable_test" = "xyes"], [
+            AC_MSG_ERROR([libmicrohttpd not found, but --enable-test was requested.])
+        ])
+    ])
+])
+
+AM_CONDITIONAL([ENABLE_TESTS], [test "x$have_test" = "xyes"])
+
 CFLAGS="$CFLAGS -D_FILE_OFFSET_BITS=64 $HTTPFS_CFLAGS"
 LIBS="$LIBS $HTTPFS_LIBS"
 
 AC_SUBST(LIBS)
+AC_SUBST([CMOCKA_CFLAGS])
+AC_SUBST([CMOCKA_LIBS])
+
 AM_CONFIG_HEADER(config.h)
 
 AC_OUTPUT(
-	Makefile
+    Makefile
+    tests/Makefile
 )
diff --git a/tests/Makefile.am b/tests/Makefile.am
new file mode 100644
index 0000000..47bdcc5
--- /dev/null
+++ b/tests/Makefile.am
@@ -0,0 +1,8 @@
+check_PROGRAMS = test_httpfs
+
+test_httpfs_SOURCES = test_httpfs.c ../http.c ../parsehtml.c ../node.c ../extract.c ../netfs.c ../args.c
+test_httpfs_CFLAGS = -I$(top_srcdir) $(CMOCKA_CFLAGS)
+test_httpfs_LDFLAGS = -Wl,--wrap=gethostbyname -Wl,--export-dynamic
+test_httpfs_LDADD = $(CMOCKA_LIBS) $(MHD_LIBS) $(HTTPFS_SHARED_LIBS)
+
+TESTS = $(check_PROGRAMS)
diff --git a/tests/test_httpfs.c b/tests/test_httpfs.c
new file mode 100644
index 0000000..065217e
--- /dev/null
+++ b/tests/test_httpfs.c
@@ -0,0 +1,102 @@
+/*
+ * httpfs unit tests
+ *
+ * Copyright (C) 2026 Free Software Foundation, Inc.
+ * This file is part of the GNU Hurd.
+ * 
+ * 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, 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, write to the Free Software
+ * Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
+ */
+
+#ifndef _GNU_SOURCE
+#define _GNU_SOURCE 1
+#endif
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <errno.h>
+#include <netdb.h>
+#include <microhttpd.h>
+#include <curl/curl.h>
+#include <stdarg.h>
+#include <stddef.h>
+#include <setjmp.h>
+#include <cmocka.h>
+
+#include "httpfs.h"
+
+#define TEST_PORT 8080
+
+char *url = NULL;
+char *ip_addr = "0.0.0.0";
+int mode = 1;
+unsigned short port = TEST_PORT;
+int debug_flag = 1;
+struct files *list_of_entries = NULL, *this_entry = NULL;
+char *dir_tok[25] = {NULL};
+int no_of_slashes = 0;
+volatile struct mapped_time_value *httpfs_maptime = NULL;
+char *netfs_server_name = HTTPFS_SERVER_NAME;
+char *netfs_server_version = HTTPFS_SERVER_VERSION;
+struct httpfs *httpfs = NULL;
+
+struct hostent *__wrap_gethostbyname(const char *name) {
+    assert_non_null(name);
+
+    if (strcmp(name, "www.example.com") == 0) {
+        return (struct hostent *)mock();
+    }
+
+    return (struct hostent *)mock();
+}
+
+/* http.c unit tests */
+static void test_lookup_host_1(void **state) {
+    (void) state;
+
+    struct hostent mock_hostent;
+    memset(&mock_hostent, 0, sizeof(mock_hostent));
+    mock_hostent.h_name = "www.example.com";
+
+    will_return(__wrap_gethostbyname, &mock_hostent);
+
+    struct hostent *result = NULL;
+    int ret = lookup_host("www.example.com", &result);
+
+    assert_int_equal(ret, 0);
+    assert_non_null(result);
+    assert_string_equal(result->h_name, "www.example.com");
+}
+
+static void test_lookup_host_2(void **state) {
+    (void) state;
+
+    will_return(__wrap_gethostbyname, NULL);
+
+    struct hostent *result = NULL;
+    int ret = lookup_host("invalid.url", &result);
+
+    assert_int_equal(ret, EINVAL);
+    assert_null(result);
+}
+
+int main(void) {
+    const struct CMUnitTest tests[] = {
+        cmocka_unit_test(test_lookup_host_1),
+        cmocka_unit_test(test_lookup_host_2),
+    };
+
+    return cmocka_run_group_tests(tests, NULL, NULL);
+}

Reply via email to