Hello,
Gianluca Cannata, le jeu. 05 nov. 2026 11:13:18 +0100, a ecrit:
> +# 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])
Better auto-enable tests when cmoka and libmicrohttpd are found.
> +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"
>
> 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
Rathe make the list of -l flags a variable shared by the root
Makefile.am and this Makefile.am
> +
> +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;
These variables don't seem to be actually used? Do you plan to use them?
> +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;
Why static?
> + 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);
> +}
Samuel