Il giorno gio 5 nov 2026 alle ore 11:02 Gianluca Cannata
<[email protected]> ha scritto:
>
> Hi Alperen,
>
> no need to do the above.
>
> I have removed from the system cmocka 2.0.0 I have installed some time
> ago and use the Debian/Hurd cmocka package version instead and now
> tests are green.
>
> The new patch will follow this email.
>
> Gianluca
Hello,
The patch is attached to this email.
Sincerely,
Gianluca
diff --git a/Makefile.am b/Makefile.am
index 262425d..0e85a89 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -12,3 +12,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..5c6305c 100644
--- a/configure.in
+++ b/configure.in
@@ -6,12 +6,35 @@ AC_PROG_CC
PKG_CHECK_MODULES(HTTPFS, libxml-2.0)
+# Optional dependencies
+# --enable-test (cmocka + libmicrohttpd)
+AC_ARG_ENABLE([test],
+ AS_HELP_STRING([--enable-test], [Enable test compilation (cmocka and libmicrohttpd) [default=no]]),
+ [enable_test=$enableval],
+ [enable_test=no])
+
+AS_IF([test "x$enable_test" = "xyes"], [
+ PKG_CHECK_MODULES([MHD], [libmicrohttpd >= 0.9.0], [], [
+ AC_MSG_ERROR([libmicrohttpd not found. Mandatory for 'make check'.])
+ ])
+
+ PKG_CHECK_MODULES([CMOCKA], [cmocka >= 1.1.0], [], [
+ AC_MSG_ERROR([cmocka not found. Mandatory for 'make check'.])
+ ])
+])
+
+AM_CONDITIONAL([ENABLE_TESTS], [test "x$enable_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
+ tests/Makefile
)
diff --git a/tests/Makefile.am b/tests/Makefile.am
new file mode 100644
index 0000000..121aaf7
--- /dev/null
+++ b/tests/Makefile.am
@@ -0,0 +1,9 @@
+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) -lxml2 -lpthread \
+ -lnetfs -lfshelp -lports -liohelp -lshouldbeinlibc -lhurdbugaddr
+
+TESTS = $(check_PROGRAMS)
diff --git a/tests/test_httpfs.c b/tests/test_httpfs.c
new file mode 100644
index 0000000..fcb9415
--- /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;
+
+ static 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);
+}