tests: Add reload test for the hosts file
[nss_wrapper.git] / tests / test_getaddrinfo.c
index b200275336d69edc8e1e4f79568593be3afea1bc..438ea12d469f1038b3921e8d076e8f0d3b6f9cfa 100644 (file)
@@ -5,8 +5,10 @@
 #include <setjmp.h>
 #include <cmocka.h>
 
+#include <errno.h>
 #include <stdio.h>
 #include <string.h>
+#include <stdlib.h>
 #include <unistd.h>
 
 #include <sys/socket.h>
@@ -83,6 +85,54 @@ static void test_nwrap_getaddrinfo(void **state)
        freeaddrinfo(res);
 }
 
+/*
+ * The purpose of this test is to verify that reloading of the hosts
+ * file (triggered by a timestamp change) correctly frees and re-creates
+ * the internal data structures, so we do not end up using invalid memory.
+ */
+static void test_nwrap_getaddrinfo_reload(void **state)
+{
+       struct addrinfo hints;
+       struct addrinfo *res = NULL;
+       const char *env;
+       char touch_cmd[1024];
+       int rc;
+
+       (void) state; /* unused */
+
+       /* IPv4 */
+       memset(&hints, 0, sizeof(struct addrinfo));
+       hints.ai_family = AF_UNSPEC;    /* Allow IPv4 or IPv6 */
+       hints.ai_socktype = SOCK_DGRAM; /* Datagram socket */
+       hints.ai_flags = AI_PASSIVE;    /* For wildcard IP address */
+       hints.ai_protocol = 0;          /* Any protocol */
+       hints.ai_canonname = NULL;
+       hints.ai_addr = NULL;
+       hints.ai_next = NULL;
+
+       rc = getaddrinfo("127.0.0.11", NULL, &hints, &res);
+       assert_int_equal(rc, 0);
+       assert_non_null(res);
+
+       freeaddrinfo(res);
+       res = NULL;
+
+       env = getenv("NSS_WRAPPER_HOSTS");
+       assert_non_null(env);
+
+       snprintf(touch_cmd, sizeof(touch_cmd), "touch %s", env);
+
+       rc = system(touch_cmd);
+       assert_return_code(rc, errno);
+
+       rc = getaddrinfo("127.0.0.11", NULL, &hints, &res);
+       assert_int_equal(rc, 0);
+       assert_non_null(res);
+
+
+       freeaddrinfo(res);
+}
+
 static void test_nwrap_getaddrinfo_samba(void **state)
 {
        struct addrinfo hints;
@@ -661,6 +711,7 @@ int main(void) {
 
        const struct CMUnitTest tests[] = {
                cmocka_unit_test(test_nwrap_getaddrinfo),
+               cmocka_unit_test(test_nwrap_getaddrinfo_reload),
                cmocka_unit_test(test_nwrap_getaddrinfo_any),
                cmocka_unit_test(test_nwrap_getaddrinfo_local),
                cmocka_unit_test(test_nwrap_getaddrinfo_name),