Skip to content

Commit d6e5ec5

Browse files
committed
common: Fix test-webserver for environments with only loopback interface
If there is no non-loopback interface (which is common in e. g. mock nspawn environments), leave `TestCase.hostport` as `NULL` instead of crashing with an assertion. Skip the tests (or parts of them) which require an external address. Fixes cockpit-project#11312 Closes cockpit-project#11314
1 parent 9dc37c6 commit d6e5ec5

File tree

1 file changed

+38
-20
lines changed

1 file changed

+38
-20
lines changed

src/common/test-webserver.c

Lines changed: 38 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ typedef struct {
4141
gboolean inet_only;
4242
} TestFixture;
4343

44+
#define SKIP_NO_HOSTPORT if (!tc->hostport) { cockpit_test_skip ("No non-loopback network interface available"); return; }
45+
4446
static void
4547
setup (TestCase *tc,
4648
gconstpointer data)
@@ -49,14 +51,14 @@ setup (TestCase *tc,
4951
GTlsCertificate *cert = NULL;
5052
GError *error = NULL;
5153
GInetAddress *inet;
52-
gchar *str;
54+
gchar *str = NULL;
5355
const gchar *address;
5456
gint port;
5557

5658
inet = cockpit_test_find_non_loopback_address ();
57-
g_assert (inet != NULL);
58-
59-
str = g_inet_address_to_string (inet);
59+
/* this can fail in environments with only localhost */
60+
if (inet != NULL)
61+
str = g_inet_address_to_string (inet);
6062

6163
if (fixture && fixture->cert_file)
6264
{
@@ -80,8 +82,10 @@ setup (TestCase *tc,
8082
/* Automatically chosen by the web server */
8183
g_object_get (tc->web_server, "port", &port, NULL);
8284
tc->localport = g_strdup_printf ("localhost:%d", port);
83-
tc->hostport = g_strdup_printf ("%s:%d", str, port);
84-
g_object_unref (inet);
85+
if (str)
86+
tc->hostport = g_strdup_printf ("%s:%d", str, port);
87+
if (inet)
88+
g_object_unref (inet);
8589
g_free (str);
8690
}
8791

@@ -452,6 +456,8 @@ test_webserver_redirect_notls (TestCase *tc,
452456
{
453457
gchar *resp;
454458

459+
SKIP_NO_HOSTPORT;
460+
455461
g_signal_connect (tc->web_server, "handle-resource", G_CALLBACK (on_shell_index_html), NULL);
456462
resp = perform_http_request (tc->hostport, "GET /shell/index.html HTTP/1.0\r\nHost:test\r\n\r\n", NULL);
457463
cockpit_assert_strmatch (resp, "HTTP/* 301 *\r\nLocation: https://*");
@@ -476,6 +482,8 @@ test_webserver_noredirect_exception (TestCase *tc,
476482
{
477483
gchar *resp;
478484

485+
SKIP_NO_HOSTPORT;
486+
479487
g_object_set (tc->web_server, "ssl-exception-prefix", "/shell", NULL);
480488
g_signal_connect (tc->web_server, "handle-resource", G_CALLBACK (on_shell_index_html), NULL);
481489
resp = perform_http_request (tc->hostport, "GET /shell/index.html HTTP/1.0\r\nHost:test\r\n\r\n", NULL);
@@ -489,6 +497,8 @@ test_webserver_noredirect_override (TestCase *tc,
489497
{
490498
gchar *resp;
491499

500+
SKIP_NO_HOSTPORT;
501+
492502
cockpit_web_server_set_redirect_tls (tc->web_server, FALSE);
493503
g_signal_connect (tc->web_server, "handle-resource", G_CALLBACK (on_shell_index_html), NULL);
494504
resp = perform_http_request (tc->hostport, "GET /shell/index.html HTTP/1.0\r\nHost:test\r\n\r\n", NULL);
@@ -757,10 +767,13 @@ test_handle_resource_url_root (TestCase *tc,
757767
invoked = NULL;
758768

759769
/* Should fail */
760-
resp = perform_http_request (tc->hostport, "GET /oooo HTTP/1.0\r\nHost:test\r\n\r\n", NULL);
761-
cockpit_assert_strmatch (resp, "HTTP/* 404 *\r\n");
762-
g_free (resp);
763-
g_assert (invoked == NULL);
770+
if (tc->hostport)
771+
{
772+
resp = perform_http_request (tc->hostport, "GET /oooo HTTP/1.0\r\nHost:test\r\n\r\n", NULL);
773+
cockpit_assert_strmatch (resp, "HTTP/* 404 *\r\n");
774+
g_free (resp);
775+
g_assert (invoked == NULL);
776+
}
764777
}
765778

766779
static void
@@ -803,19 +816,24 @@ test_address (TestCase *tc,
803816
}
804817
else
805818
{
806-
assert_cannot_connect (tc->localport);
819+
/* If there is only one interface, then cockpit_web_server_new will get a NULL address and thus do listen on loopback */
820+
if (tc->hostport)
821+
assert_cannot_connect (tc->localport);
807822
}
808823

809-
if (fix->inet_only)
810-
{
811-
resp = perform_http_request (tc->hostport, "GET /shell/index.html HTTP/1.0\r\nHost:test\r\n\r\n", NULL);
812-
cockpit_assert_strmatch (resp, "HTTP/* 200 *\r\n*");
813-
g_free (resp);
814-
resp = NULL;
815-
}
816-
else
824+
if (tc->hostport)
817825
{
818-
assert_cannot_connect (tc->hostport);
826+
if (fix->inet_only)
827+
{
828+
resp = perform_http_request (tc->hostport, "GET /shell/index.html HTTP/1.0\r\nHost:test\r\n\r\n", NULL);
829+
cockpit_assert_strmatch (resp, "HTTP/* 200 *\r\n*");
830+
g_free (resp);
831+
resp = NULL;
832+
}
833+
else
834+
{
835+
assert_cannot_connect (tc->hostport);
836+
}
819837
}
820838
}
821839

0 commit comments

Comments
 (0)