[glib-networking/mcatanzaro/#18] Verify socket timeouts are respected



commit 32bc5c72d78ad40bb420d7c3f2f1ee0f78f47776
Author: Michael Catanzaro <mcatanzaro gnome org>
Date:   Sun Aug 11 18:29:22 2019 -0500

    Verify socket timeouts are respected
    
    This adds a little test to verify that socket timeouts are respected if
    set. The test is based on test_connection_read_time_out_write(), so it's
    a little redundant, but this is a smaller test with much less going on,
    to more clearly show what it's trying to test.
    
    Note that GSocket has no timeout by default. Same for sockets created
    with GSocketClient or GSocketListener. This isn't glib-networking's
    fault: it's just how the GLib APIs were designed. You need to set a
    timeout yourself if you want a timeout.
    
    Closes #18

 tls/tests/connection.c | 41 +++++++++++++++++++++++++++++++++++++++++
 1 file changed, 41 insertions(+)
---
diff --git a/tls/tests/connection.c b/tls/tests/connection.c
index 0db8cce..9ad333e 100644
--- a/tls/tests/connection.c
+++ b/tls/tests/connection.c
@@ -90,6 +90,7 @@ typedef struct {
   gboolean server_running;
   GTlsCertificate *server_certificate;
   const gchar * const *server_protocols;
+  guint64 incoming_connection_delay;
 
   char buf[128];
   gssize nread, nwrote;
@@ -300,6 +301,9 @@ on_incoming_connection (GSocketService     *service,
   GTlsCertificate *cert;
   GError *error = NULL;
 
+  if (test->incoming_connection_delay != 0)
+    g_usleep (test->incoming_connection_delay);
+
   g_assert_null (test->server_connection);
   test->server_connection = g_tls_server_connection_new (G_IO_STREAM (connection),
                                                          test->server_certificate, &error);
@@ -2388,6 +2392,41 @@ test_sync_op_during_handshake (TestConnection *test,
   g_assert_no_error (test->server_error);
 }
 
+static void
+test_socket_timeout (TestConnection *test,
+                     gconstpointer   data)
+{
+  GIOStream *connection;
+  GSocketClient *client;
+  GError *error = NULL;
+
+  test->incoming_connection_delay = 1.1 * G_USEC_PER_SEC;
+
+  start_async_server_service (test, G_TLS_AUTHENTICATION_NONE, WRITE_THEN_CLOSE);
+
+  client = g_socket_client_new ();
+  g_socket_client_set_timeout (client, 1);
+  connection = G_IO_STREAM (g_socket_client_connect (client, G_SOCKET_CONNECTABLE (test->address),
+                                                     NULL, &error));
+  g_assert_no_error (error);
+  g_object_unref (client);
+
+  test->client_connection = g_tls_client_connection_new (connection, test->identity, &error);
+  g_assert_no_error (error);
+  g_object_unref (connection);
+
+  /* No validation at all in this test */
+  g_tls_client_connection_set_validation_flags (G_TLS_CLIENT_CONNECTION (test->client_connection),
+                                                0);
+
+  read_test_data_async (test);
+  g_main_loop_run (test->loop);
+  wait_until_server_finished (test);
+
+  g_assert_error (test->read_error, G_IO_ERROR, G_IO_ERROR_TIMED_OUT);
+  g_assert_error (test->server_error, G_TLS_ERROR, G_TLS_ERROR_NOT_TLS);
+}
+
 int
 main (int   argc,
       char *argv[])
@@ -2473,6 +2512,8 @@ main (int   argc,
               setup_connection, test_alpn_server_only, teardown_connection);
   g_test_add ("/tls/" BACKEND "/connection/sync-op-during-handshake", TestConnection, NULL,
               setup_connection, test_sync_op_during_handshake, teardown_connection);
+  g_test_add ("/tls/" BACKEND "/connection/socket-timeout", TestConnection, NULL,
+              setup_connection, test_socket_timeout, teardown_connection);
 
   ret = g_test_run ();
 


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]