This patch introduces the initial unit testing infrastructure under
the tests/ directory for httpfs, focusing on validating lookup_host
success and failure handling.

You can find the source code here: https://github.com/aculnaig/httpfs/tree/cvs

The patch is attached to this email.

Best regards,

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..146118f 100644
--- a/configure.in
+++ b/configure.in
@@ -5,13 +5,22 @@ AM_INIT_AUTOMAKE
 AC_PROG_CC
 
 PKG_CHECK_MODULES(HTTPFS, libxml-2.0)
+PKG_CHECK_MODULES([CMOCKA], [cmocka], [
+	have_cmocka=yes
+], [
+	AC_MSG_ERROR([cmocka library is required for unit tests])
+])
 
 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..2dcd57c
--- /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_FLAGS)
+test_httpfs_LDFLAGS = -Wl,--wrap=gethostbyname
+test_httpfs_LDADD = $(CMOCKA_LIBS) -lmicrohttpd -lcurl -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);
+}

Reply via email to