]> git.ipfire.org Git - thirdparty/dovecot/core.git/commitdiff
lib-dns: Test that background cache refresh works when lookup starts failing
authorAki Tuomi <aki.tuomi@open-xchange.com>
Fri, 25 Nov 2022 13:28:22 +0000 (15:28 +0200)
committerAki Tuomi <aki.tuomi@open-xchange.com>
Wed, 30 Nov 2022 06:29:05 +0000 (08:29 +0200)
src/lib-dns/test-dns-lookup.c

index 3ca66fa34b44cdfdcea554fbddf70339b1560b0f..52cb3a7f8cf34d3818fbc56465685705c810ce60 100644 (file)
@@ -21,6 +21,7 @@ static const struct {
 } replies[] = {
        { "localhost", "0\t127.0.0.1\t::1\n" },
        { "127.0.0.1", "0\tlocalhost\n" },
+       { "once-host", "0\t127.0.0.2\n" },
 };
 
 static struct test_server {
@@ -29,6 +30,7 @@ static struct test_server {
        struct timeout *to;
        int fd;
        unsigned int lookup_counter;
+       bool once_host_seen;
 } test_server;
 
 struct test_expect_result {
@@ -64,6 +66,13 @@ test_dns_client_input_args(struct connection *client, const char *const *args)
                        timeout_add_short(msecs, server_handle_timeout, client);
                return 1;
        }
+       if (strcmp(args[1], "once-host") == 0) {
+               if (test_server.once_host_seen) {
+                       o_stream_nsend_str(client->output, "-1\tUnresolved\n");
+                       return 1;
+               }
+               test_server.once_host_seen = TRUE;
+       }
        for (size_t i = 0; i < N_ELEMENTS(replies); i++) {
                if (strcmp(args[1], replies[i].name) == 0) {
                        o_stream_nsend_str(client->output, replies[i].reply);
@@ -324,6 +333,49 @@ static void test_dns_lookup_cached(void)
        io_loop_run(current_ioloop);
        test_assert_cmp(test_server.lookup_counter, ==, 5);
 
+       /* Test that lookup failures do not crash client */
+       ctx.result = "127.0.0.2";
+       ctx.ret = 0;
+
+       /* should cause only one lookup */
+       test_assert(dns_client_lookup(client, "once-host", event,
+                                     test_callback_ips, &ctx, &lookup) == 0);
+       io_loop_run(current_ioloop);
+       test_assert(dns_client_lookup(client, "once-host", event,
+                                     test_callback_ips, &ctx, &lookup) == 0);
+       io_loop_run(current_ioloop);
+       test_assert_cmp(test_server.lookup_counter, ==, 6);
+
+       to = timeout_add(3*1000, io_loop_stop, test_server.loop);
+       io_loop_run(current_ioloop);
+       timeout_remove(&to);
+
+       /* Ensure failure does not crash anything (it will not be returned yet) */
+       test_assert(dns_client_lookup(client, "once-host", event,
+                                     test_callback_ips, &ctx, &lookup) == 0);
+       io_loop_run(current_ioloop);
+       while (dns_client_has_pending_queries(client)) {
+               io_loop_handler_run(current_ioloop);
+               io_loop_set_running(current_ioloop);
+       }
+       test_assert_cmp(test_server.lookup_counter, ==, 7);
+
+       to = timeout_add(3*1000, io_loop_stop, test_server.loop);
+       io_loop_run(current_ioloop);
+       timeout_remove(&to);
+
+       ctx.result = NULL;
+       ctx.ret = -1;
+       /* Now it finally returns error */
+       test_assert(dns_client_lookup(client, "once-host", event,
+                                     test_callback_ips, &ctx, &lookup) == 0);
+       io_loop_run(current_ioloop);
+       while (dns_client_has_pending_queries(client)) {
+               io_loop_handler_run(current_ioloop);
+               io_loop_set_running(current_ioloop);
+       }
+       test_assert_cmp(test_server.lookup_counter, ==, 8);
+
        dns_client_deinit(&client);
        destroy_dns_server(&test_server);
        event_unref(&event);