[libsoup] Add a simple regression test for unbound sockets



commit a0ceee0c3c17dde75faeb009bb6376b40780681f
Author: Simon McVittie <simon mcvittie collabora co uk>
Date:   Thu Mar 29 19:17:17 2012 +0100

    Add a simple regression test for unbound sockets
    
    https://bugzilla.gnome.org/show_bug.cgi?id=673083

 tests/Makefile.am   |    3 +
 tests/socket-test.c |  113 +++++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 116 insertions(+), 0 deletions(-)
---
diff --git a/tests/Makefile.am b/tests/Makefile.am
index 995196c..594890b 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -30,6 +30,7 @@ noinst_PROGRAMS =	\
 	simple-httpd	\
 	simple-proxy	\
 	sniffing-test   \
+	socket-test	\
 	ssl-test	\
 	streaming-test	\
 	timeout-test	\
@@ -67,6 +68,7 @@ server_auth_test_SOURCES = server-auth-test.c $(TEST_SRCS)
 simple_httpd_SOURCES = simple-httpd.c
 simple_proxy_SOURCES = simple-proxy.c
 sniffing_test_SOURCES = sniffing-test.c  $(TEST_SRCS)
+socket_test_SOURCES = socket-test.c  $(TEST_SRCS)
 ssl_test_SOURCES = ssl-test.c  $(TEST_SRCS)
 streaming_test_SOURCES = streaming-test.c $(TEST_SRCS)
 timeout_test_SOURCES = timeout-test.c $(TEST_SRCS)
@@ -99,6 +101,7 @@ TESTS =			\
 	redirect-test	\
 	requester-test	\
 	sniffing-test	\
+	socket-test	\
 	ssl-test	\
 	streaming-test	\
 	timeout-test	\
diff --git a/tests/socket-test.c b/tests/socket-test.c
new file mode 100644
index 0000000..60c3e86
--- /dev/null
+++ b/tests/socket-test.c
@@ -0,0 +1,113 @@
+/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
+/*
+ * Copyright 2007-2012 Red Hat, Inc.
+ * Copyright 2012 Nokia Corporation
+ */
+
+#include <libsoup/soup.h>
+
+#include <string.h>
+
+#include "test-utils.h"
+
+static void
+do_unconnected_socket_test (void)
+{
+	SoupAddress *localhost;
+	SoupSocket *sock;
+	SoupSocket *client;
+	SoupAddress *addr;
+	guint res;
+	struct sockaddr_in in_localhost;
+
+	in_localhost.sin_family = AF_INET;
+	in_localhost.sin_port = 0;
+	in_localhost.sin_addr.s_addr = htonl (INADDR_LOOPBACK);
+
+	localhost = soup_address_new_from_sockaddr (
+		(struct sockaddr *) &in_localhost, sizeof (in_localhost));
+	g_assert (localhost != NULL);
+	res = soup_address_resolve_sync (localhost, NULL);
+	g_assert_cmpuint (res, ==, SOUP_STATUS_OK);
+
+	sock = soup_socket_new (
+		SOUP_SOCKET_LOCAL_ADDRESS, localhost,
+		NULL);
+	g_assert (sock != NULL);
+
+	addr = soup_socket_get_local_address (sock);
+	g_assert (addr != NULL);
+	g_assert_cmpstr (soup_address_get_physical (addr), ==, "127.0.0.1");
+	g_assert_cmpuint (soup_address_get_port (addr), ==, 0);
+
+	/* fails with ENOTCONN */
+	expect_warning++;
+	addr = soup_socket_get_remote_address (sock);
+	g_assert (addr == NULL);
+
+	res = soup_socket_listen (sock);
+	g_assert_cmpuint (res, ==, TRUE);
+
+	addr = soup_socket_get_local_address (sock);
+	g_assert (addr != NULL);
+	g_assert_cmpstr (soup_address_get_physical (addr), ==, "127.0.0.1");
+	g_assert_cmpuint (soup_address_get_port (addr), >, 0);
+
+	client = soup_socket_new (
+		SOUP_SOCKET_REMOTE_ADDRESS,
+			soup_socket_get_local_address (sock),
+		NULL);
+	res = soup_socket_connect_sync (client, NULL);
+	g_assert_cmpuint (res, ==, SOUP_STATUS_OK);
+	addr = soup_socket_get_local_address (client);
+	g_assert (addr != NULL);
+	addr = soup_socket_get_remote_address (client);
+	g_assert (addr != NULL);
+	g_assert_cmpstr (soup_address_get_physical (addr), ==, "127.0.0.1");
+	g_assert_cmpuint (soup_address_get_port (addr), >, 0);
+	g_object_unref (client);
+
+	client = soup_socket_new (
+		SOUP_SOCKET_REMOTE_ADDRESS,
+			soup_socket_get_local_address (sock),
+		NULL);
+	/* save it for later */
+
+	/* listening socket fails with ENOTCONN */
+	expect_warning++;
+	addr = soup_socket_get_remote_address (sock);
+	g_assert (addr == NULL);
+
+	soup_socket_disconnect (sock);
+
+	expect_warning++;
+	addr = soup_socket_get_remote_address (sock);
+	g_assert (addr == NULL);
+
+	/* has never been connected */
+	expect_warning++;
+	addr = soup_socket_get_local_address (client);
+	g_assert (addr == NULL);
+
+	res = soup_socket_connect_sync (client, NULL);
+	g_assert_cmpuint (res, ==, SOUP_STATUS_CANT_CONNECT);
+
+	expect_warning++;
+	addr = soup_socket_get_local_address (client);
+	g_assert (addr == NULL);
+
+	g_object_unref (localhost);
+	g_object_unref (client);
+	g_object_unref (sock);
+}
+
+int
+main (int argc, char **argv)
+{
+	test_init (argc, argv, NULL);
+
+	do_unconnected_socket_test ();
+
+	test_cleanup ();
+	return errors != 0;
+}



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