Blame tests/websocket-test.c

rpm-build 4f3c61
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
rpm-build 4f3c61
/*
rpm-build 4f3c61
 * This file was originally part of Cockpit.
rpm-build 4f3c61
 *
rpm-build 4f3c61
 * Copyright (C) 2013 Red Hat, Inc.
rpm-build 4f3c61
 *
rpm-build 4f3c61
 * Cockpit is free software; you can redistribute it and/or modify it
rpm-build 4f3c61
 * under the terms of the GNU Lesser General Public License as published by
rpm-build 4f3c61
 * the Free Software Foundation; either version 2.1 of the License, or
rpm-build 4f3c61
 * (at your option) any later version.
rpm-build 4f3c61
 *
rpm-build 4f3c61
 * Cockpit is distributed in the hope that it will be useful, but
rpm-build 4f3c61
 * WITHOUT ANY WARRANTY; without even the implied warranty of
rpm-build 4f3c61
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
rpm-build 4f3c61
 * Lesser General Public License for more details.
rpm-build 4f3c61
 *
rpm-build 4f3c61
 * You should have received a copy of the GNU Lesser General Public License
rpm-build 4f3c61
 * along with Cockpit; If not, see <http://www.gnu.org/licenses/>.
rpm-build 4f3c61
 */
rpm-build 4f3c61
rpm-build 4f3c61
#include "test-utils.h"
rpm-build 4f3c61
rpm-build 4f3c61
typedef struct {
rpm-build 4f3c61
	GSocket *listener;
rpm-build 4f3c61
	gushort port;
rpm-build 4f3c61
rpm-build 4f3c61
	SoupSession *session;
rpm-build 4f3c61
	SoupMessage *msg;
rpm-build 4f3c61
	SoupWebsocketConnection *client;
rpm-build 4f3c61
	GError *client_error;
rpm-build 4f3c61
rpm-build 4f3c61
	SoupServer *soup_server;
rpm-build 4f3c61
	SoupWebsocketConnection *server;
rpm-build 4f3c61
rpm-build 4f3c61
	gboolean no_server;
rpm-build 4f3c61
	GIOStream *raw_server;
rpm-build 4f3c61
rpm-build 4f3c61
	GMutex mutex;
rpm-build 4f3c61
} Test;
rpm-build 4f3c61
rpm-build 4f3c61
#define WAIT_UNTIL(cond)					\
rpm-build 4f3c61
	G_STMT_START						\
rpm-build 4f3c61
	while (!(cond)) g_main_context_iteration (NULL, TRUE);	\
rpm-build 4f3c61
	G_STMT_END
rpm-build 4f3c61
rpm-build 4f3c61
static void
rpm-build 4f3c61
on_error_not_reached (SoupWebsocketConnection *ws,
rpm-build 4f3c61
                      GError *error,
rpm-build 4f3c61
                      gpointer user_data)
rpm-build 4f3c61
{
rpm-build 4f3c61
	/* At this point we know this will fail, but is informative */
rpm-build 4f3c61
	g_assert_no_error (error);
rpm-build 4f3c61
}
rpm-build 4f3c61
rpm-build 4f3c61
static void
rpm-build 4f3c61
on_error_copy (SoupWebsocketConnection *ws,
rpm-build 4f3c61
               GError *error,
rpm-build 4f3c61
               gpointer user_data)
rpm-build 4f3c61
{
rpm-build 4f3c61
	GError **copy = user_data;
rpm-build 4f3c61
	g_assert (*copy == NULL);
rpm-build 4f3c61
	*copy = g_error_copy (error);
rpm-build 4f3c61
}
rpm-build 4f3c61
rpm-build 4f3c61
static void
rpm-build 4f3c61
setup_listener (Test *test)
rpm-build 4f3c61
{
rpm-build 4f3c61
	GSocketAddress *addr;
rpm-build 4f3c61
	GError *error = NULL;
rpm-build 4f3c61
rpm-build 4f3c61
	test->listener = g_socket_new (G_SOCKET_FAMILY_IPV4,
rpm-build 4f3c61
				       G_SOCKET_TYPE_STREAM,
rpm-build 4f3c61
				       G_SOCKET_PROTOCOL_TCP,
rpm-build 4f3c61
				       &error);
rpm-build 4f3c61
	g_assert_no_error (error);
rpm-build 4f3c61
rpm-build 4f3c61
	addr = g_inet_socket_address_new_from_string ("127.0.0.1", 0);
rpm-build 4f3c61
	g_assert_no_error (error);
rpm-build 4f3c61
rpm-build 4f3c61
	g_socket_bind (test->listener, addr, TRUE, &error);
rpm-build 4f3c61
	g_assert_no_error (error);
rpm-build 4f3c61
	g_object_unref (addr);
rpm-build 4f3c61
rpm-build 4f3c61
	addr = g_socket_get_local_address (test->listener, &error);
rpm-build 4f3c61
	g_assert_no_error (error);
rpm-build 4f3c61
rpm-build 4f3c61
	test->port = g_inet_socket_address_get_port (G_INET_SOCKET_ADDRESS (addr));
rpm-build 4f3c61
	g_object_unref (addr);
rpm-build 4f3c61
rpm-build 4f3c61
	g_socket_listen (test->listener, &error);
rpm-build 4f3c61
	g_assert_no_error (error);
rpm-build 4f3c61
}
rpm-build 4f3c61
rpm-build 4f3c61
static void
rpm-build 4f3c61
direct_connection_complete (GObject *object,
rpm-build 4f3c61
			    GAsyncResult *result,
rpm-build 4f3c61
			    gpointer user_data)
rpm-build 4f3c61
{
rpm-build 4f3c61
	Test *test = user_data;
rpm-build 4f3c61
	GSocketConnection *conn;
rpm-build 4f3c61
	SoupURI *uri;
rpm-build 4f3c61
	GError *error = NULL;
rpm-build 4f3c61
rpm-build 4f3c61
	conn = g_socket_client_connect_to_host_finish (G_SOCKET_CLIENT (object),
rpm-build 4f3c61
						       result, &error);
rpm-build 4f3c61
	g_assert_no_error (error);
rpm-build 4f3c61
rpm-build 4f3c61
	uri = soup_uri_new ("http://127.0.0.1/");
rpm-build 4f3c61
	test->client = soup_websocket_connection_new (G_IO_STREAM (conn), uri,
rpm-build 4f3c61
						      SOUP_WEBSOCKET_CONNECTION_CLIENT,
rpm-build 4f3c61
						      NULL, NULL);
rpm-build 4f3c61
	soup_uri_free (uri);
rpm-build 4f3c61
	g_object_unref (conn);
rpm-build 4f3c61
}
rpm-build 4f3c61
rpm-build 4f3c61
static gboolean
rpm-build 4f3c61
got_connection (GSocket *listener,
rpm-build 4f3c61
		GIOCondition cond,
rpm-build 4f3c61
		gpointer user_data)
rpm-build 4f3c61
{
rpm-build 4f3c61
	Test *test = user_data;
rpm-build 4f3c61
	GSocket *sock;
rpm-build 4f3c61
	GSocketConnection *conn;
rpm-build 4f3c61
	SoupURI *uri;
rpm-build 4f3c61
	GError *error = NULL;
rpm-build 4f3c61
rpm-build 4f3c61
	sock = g_socket_accept (listener, NULL, &error);
rpm-build 4f3c61
	g_assert_no_error (error);
rpm-build 4f3c61
rpm-build 4f3c61
	conn = g_socket_connection_factory_create_connection (sock);
rpm-build 4f3c61
	g_assert (conn != NULL);
rpm-build 4f3c61
	g_object_unref (sock);
rpm-build 4f3c61
rpm-build 4f3c61
	if (test->no_server)
rpm-build 4f3c61
		test->raw_server = G_IO_STREAM (conn);
rpm-build 4f3c61
	else {
rpm-build 4f3c61
		uri = soup_uri_new ("http://127.0.0.1/");
rpm-build 4f3c61
		test->server = soup_websocket_connection_new (G_IO_STREAM (conn), uri,
rpm-build 4f3c61
							      SOUP_WEBSOCKET_CONNECTION_SERVER,
rpm-build 4f3c61
							      NULL, NULL);
rpm-build 4f3c61
		soup_uri_free (uri);
rpm-build 4f3c61
		g_object_unref (conn);
rpm-build 4f3c61
	}
rpm-build 4f3c61
rpm-build 4f3c61
	return FALSE;
rpm-build 4f3c61
}
rpm-build 4f3c61
rpm-build 4f3c61
static void
rpm-build 4f3c61
setup_direct_connection (Test *test,
rpm-build 4f3c61
			 gconstpointer data)
rpm-build 4f3c61
{
rpm-build 4f3c61
	GSocketClient *client;
rpm-build 4f3c61
	GSource *listen_source;
rpm-build 4f3c61
rpm-build 4f3c61
	setup_listener (test);
rpm-build 4f3c61
rpm-build 4f3c61
	client = g_socket_client_new ();
rpm-build 4f3c61
	g_socket_client_connect_to_host_async (client, "127.0.0.1", test->port,
rpm-build 4f3c61
					       NULL, direct_connection_complete, test);
rpm-build 4f3c61
rpm-build 4f3c61
	listen_source = g_socket_create_source (test->listener, G_IO_IN, NULL);
rpm-build 4f3c61
	g_source_set_callback (listen_source, (GSourceFunc) got_connection, test, NULL);
rpm-build 4f3c61
	g_source_attach (listen_source, NULL);
rpm-build 4f3c61
rpm-build 4f3c61
	while (test->client == NULL || (test->server == NULL && test->raw_server == NULL))
rpm-build 4f3c61
 		g_main_context_iteration (NULL, TRUE);
rpm-build 4f3c61
	
rpm-build 4f3c61
	g_source_destroy (listen_source);
rpm-build 4f3c61
	g_source_unref (listen_source);
rpm-build 4f3c61
	g_object_unref (client);
rpm-build 4f3c61
}
rpm-build 4f3c61
rpm-build 4f3c61
static void
rpm-build 4f3c61
setup_half_direct_connection (Test *test,
rpm-build 4f3c61
			      gconstpointer data)
rpm-build 4f3c61
{
rpm-build 4f3c61
	test->no_server = TRUE;
rpm-build 4f3c61
	setup_direct_connection (test, data);
rpm-build 4f3c61
}
rpm-build 4f3c61
rpm-build 4f3c61
static void
rpm-build 4f3c61
teardown_direct_connection (Test *test,
rpm-build 4f3c61
			    gconstpointer data)
rpm-build 4f3c61
{
rpm-build 4f3c61
	g_clear_object (&test->listener);
rpm-build 4f3c61
	g_clear_object (&test->client);
rpm-build 4f3c61
	g_clear_object (&test->server);
rpm-build 4f3c61
	g_clear_object (&test->raw_server);
rpm-build 4f3c61
}
rpm-build 4f3c61
rpm-build 4f3c61
static void
rpm-build 4f3c61
setup_soup_server (Test *test,
rpm-build 4f3c61
		   const char *origin,
rpm-build 4f3c61
		   const char **protocols,
rpm-build 4f3c61
		   SoupServerWebsocketCallback callback,
rpm-build 4f3c61
		   gpointer user_data)
rpm-build 4f3c61
{
rpm-build 4f3c61
	GError *error = NULL;
rpm-build 4f3c61
rpm-build 4f3c61
	setup_listener (test);
rpm-build 4f3c61
rpm-build 4f3c61
	test->soup_server = soup_test_server_new (SOUP_TEST_SERVER_IN_THREAD);
rpm-build 4f3c61
	soup_server_listen_socket (test->soup_server, test->listener, 0, &error);
rpm-build 4f3c61
	g_assert_no_error (error);
rpm-build 4f3c61
rpm-build 4f3c61
	soup_server_add_websocket_handler (test->soup_server, "/unix",
rpm-build 4f3c61
					   origin, (char **) protocols,
rpm-build 4f3c61
					   callback, user_data, NULL);
rpm-build 4f3c61
}
rpm-build 4f3c61
rpm-build 4f3c61
static void
rpm-build 4f3c61
client_connect (Test *test,
rpm-build 4f3c61
		const char *origin,
rpm-build 4f3c61
		const char **protocols,
rpm-build 4f3c61
		GAsyncReadyCallback callback,
rpm-build 4f3c61
		gpointer user_data)
rpm-build 4f3c61
{
rpm-build 4f3c61
	char *url;
rpm-build 4f3c61
rpm-build 4f3c61
	if (!test->session)
rpm-build 4f3c61
		test->session = soup_test_session_new (SOUP_TYPE_SESSION, NULL);
rpm-build 4f3c61
rpm-build 4f3c61
	url = g_strdup_printf ("ws://127.0.0.1:%u/unix", test->port);
rpm-build 4f3c61
	test->msg = soup_message_new ("GET", url);
rpm-build 4f3c61
	g_free (url);
rpm-build 4f3c61
rpm-build 4f3c61
	soup_session_websocket_connect_async (test->session, test->msg,
rpm-build 4f3c61
					      origin, (char **) protocols,
rpm-build 4f3c61
					      NULL, callback, user_data);
rpm-build 4f3c61
}
rpm-build 4f3c61
rpm-build 4f3c61
static void
rpm-build 4f3c61
got_server_connection (SoupServer              *server,
rpm-build 4f3c61
		       SoupWebsocketConnection *connection,
rpm-build 4f3c61
		       const char              *path,
rpm-build 4f3c61
		       SoupClientContext       *client,
rpm-build 4f3c61
		       gpointer                 user_data)
rpm-build 4f3c61
{
rpm-build 4f3c61
	Test *test = user_data;
rpm-build 4f3c61
rpm-build 4f3c61
	test->server = g_object_ref (connection);
rpm-build 4f3c61
}
rpm-build 4f3c61
rpm-build 4f3c61
static void
rpm-build 4f3c61
got_client_connection (GObject *object,
rpm-build 4f3c61
		       GAsyncResult *result,
rpm-build 4f3c61
		       gpointer user_data)
rpm-build 4f3c61
{
rpm-build 4f3c61
	Test *test = user_data;
rpm-build 4f3c61
rpm-build 4f3c61
	test->client = soup_session_websocket_connect_finish (SOUP_SESSION (object),
rpm-build 4f3c61
							      result, &test->client_error);
rpm-build 4f3c61
}
rpm-build 4f3c61
rpm-build 4f3c61
static void
rpm-build 4f3c61
setup_soup_connection (Test *test,
rpm-build 4f3c61
		       gconstpointer data)
rpm-build 4f3c61
{
rpm-build 4f3c61
	setup_soup_server (test, NULL, NULL, got_server_connection, test);
rpm-build 4f3c61
	client_connect (test, NULL, NULL, got_client_connection, test);
rpm-build 4f3c61
	WAIT_UNTIL (test->server != NULL);
rpm-build 4f3c61
	WAIT_UNTIL (test->client != NULL || test->client_error != NULL);
rpm-build 4f3c61
	g_assert_no_error (test->client_error);
rpm-build 4f3c61
}
rpm-build 4f3c61
rpm-build 4f3c61
static void
rpm-build 4f3c61
teardown_soup_connection (Test *test,
rpm-build 4f3c61
			  gconstpointer data)
rpm-build 4f3c61
{
rpm-build 4f3c61
	teardown_direct_connection (test, data);
rpm-build 4f3c61
rpm-build 4f3c61
	g_clear_object (&test->msg);
rpm-build 4f3c61
	g_clear_error (&test->client_error);
rpm-build 4f3c61
	g_clear_pointer (&test->session, soup_test_session_abort_unref);
rpm-build 4f3c61
	g_clear_pointer (&test->soup_server, soup_test_server_quit_unref);
rpm-build 4f3c61
}
rpm-build 4f3c61
rpm-build 4f3c61
rpm-build 4f3c61
static void
rpm-build 4f3c61
on_text_message (SoupWebsocketConnection *ws,
rpm-build 4f3c61
                 SoupWebsocketDataType type,
rpm-build 4f3c61
                 GBytes *message,
rpm-build 4f3c61
                 gpointer user_data)
rpm-build 4f3c61
{
rpm-build 4f3c61
	GBytes **receive = user_data;
rpm-build 4f3c61
rpm-build 4f3c61
	g_assert_cmpint (type, ==, SOUP_WEBSOCKET_DATA_TEXT);
rpm-build 4f3c61
	g_assert (*receive == NULL);
rpm-build 4f3c61
	g_assert (message != NULL);
rpm-build 4f3c61
rpm-build 4f3c61
	*receive = g_bytes_ref (message);
rpm-build 4f3c61
}
rpm-build 4f3c61
rpm-build 4f3c61
static void
rpm-build 4f3c61
on_close_set_flag (SoupWebsocketConnection *ws,
rpm-build 4f3c61
                   gpointer user_data)
rpm-build 4f3c61
{
rpm-build 4f3c61
	gboolean *flag = user_data;
rpm-build 4f3c61
rpm-build 4f3c61
	g_assert (*flag == FALSE);
rpm-build 4f3c61
rpm-build 4f3c61
	*flag = TRUE;
rpm-build 4f3c61
}
rpm-build 4f3c61
rpm-build 4f3c61
rpm-build 4f3c61
static void
rpm-build 4f3c61
test_handshake (Test *test,
rpm-build 4f3c61
                gconstpointer data)
rpm-build 4f3c61
{
rpm-build 4f3c61
	g_assert_cmpint (soup_websocket_connection_get_state (test->client), ==, SOUP_WEBSOCKET_STATE_OPEN);
rpm-build 4f3c61
	g_assert_cmpint (soup_websocket_connection_get_state (test->server), ==, SOUP_WEBSOCKET_STATE_OPEN);
rpm-build 4f3c61
}
rpm-build 4f3c61
rpm-build 4f3c61
#define TEST_STRING "this is a test"
rpm-build 4f3c61
rpm-build 4f3c61
static void
rpm-build 4f3c61
test_send_client_to_server (Test *test,
rpm-build 4f3c61
                            gconstpointer data)
rpm-build 4f3c61
{
rpm-build 4f3c61
	GBytes *received = NULL;
rpm-build 4f3c61
	const char *contents;
rpm-build 4f3c61
	gsize len;
rpm-build 4f3c61
rpm-build 4f3c61
	g_signal_connect (test->server, "message", G_CALLBACK (on_text_message), &received);
rpm-build 4f3c61
rpm-build 4f3c61
	soup_websocket_connection_send_text (test->client, TEST_STRING);
rpm-build 4f3c61
rpm-build 4f3c61
	WAIT_UNTIL (received != NULL);
rpm-build 4f3c61
rpm-build 4f3c61
	/* Received messages should be null terminated (outside of len) */
rpm-build 4f3c61
	contents = g_bytes_get_data (received, &len;;
rpm-build 4f3c61
	g_assert_cmpstr (contents, ==, TEST_STRING);
rpm-build 4f3c61
	g_assert_cmpint (len, ==, strlen (TEST_STRING));
rpm-build 4f3c61
rpm-build 4f3c61
	g_bytes_unref (received);
rpm-build 4f3c61
}
rpm-build 4f3c61
rpm-build 4f3c61
static void
rpm-build 4f3c61
test_send_server_to_client (Test *test,
rpm-build 4f3c61
                            gconstpointer data)
rpm-build 4f3c61
{
rpm-build 4f3c61
	GBytes *received = NULL;
rpm-build 4f3c61
	const char *contents;
rpm-build 4f3c61
	gsize len;
rpm-build 4f3c61
rpm-build 4f3c61
	g_signal_connect (test->client, "message", G_CALLBACK (on_text_message), &received);
rpm-build 4f3c61
rpm-build 4f3c61
	soup_websocket_connection_send_text (test->server, TEST_STRING);
rpm-build 4f3c61
rpm-build 4f3c61
	WAIT_UNTIL (received != NULL);
rpm-build 4f3c61
rpm-build 4f3c61
	/* Received messages should be null terminated (outside of len) */
rpm-build 4f3c61
	contents = g_bytes_get_data (received, &len;;
rpm-build 4f3c61
	g_assert_cmpstr (contents, ==, TEST_STRING);
rpm-build 4f3c61
	g_assert_cmpint (len, ==, strlen (TEST_STRING));
rpm-build 4f3c61
rpm-build 4f3c61
	g_bytes_unref (received);
rpm-build 4f3c61
}
rpm-build 4f3c61
rpm-build 4f3c61
static void
rpm-build 4f3c61
test_send_big_packets (Test *test,
rpm-build 4f3c61
                       gconstpointer data)
rpm-build 4f3c61
{
rpm-build 4f3c61
	GBytes *sent = NULL;
rpm-build 4f3c61
	GBytes *received = NULL;
rpm-build 4f3c61
rpm-build 4f3c61
	g_signal_connect (test->client, "message", G_CALLBACK (on_text_message), &received);
rpm-build 4f3c61
rpm-build 4f3c61
	sent = g_bytes_new_take (g_strnfill (400, '!'), 400);
rpm-build 4f3c61
	soup_websocket_connection_send_text (test->server, g_bytes_get_data (sent, NULL));
rpm-build 4f3c61
	WAIT_UNTIL (received != NULL);
rpm-build 4f3c61
	g_assert (g_bytes_equal (sent, received));
rpm-build 4f3c61
	g_bytes_unref (sent);
rpm-build 4f3c61
	g_bytes_unref (received);
rpm-build 4f3c61
	received = NULL;
rpm-build 4f3c61
rpm-build 4f3c61
	sent = g_bytes_new_take (g_strnfill (100 * 1000, '?'), 100 * 1000);
rpm-build 4f3c61
	soup_websocket_connection_send_text (test->server, g_bytes_get_data (sent, NULL));
rpm-build 4f3c61
	WAIT_UNTIL (received != NULL);
rpm-build 4f3c61
	g_assert (g_bytes_equal (sent, received));
rpm-build 4f3c61
	g_bytes_unref (sent);
rpm-build 4f3c61
	g_bytes_unref (received);
rpm-build 4f3c61
	received = NULL;
rpm-build 4f3c61
rpm-build 4f3c61
	soup_websocket_connection_set_max_incoming_payload_size (test->client, 1000 * 1000 + 1);
rpm-build 4f3c61
	g_assert (soup_websocket_connection_get_max_incoming_payload_size (test->client) == (1000 * 1000 + 1));
rpm-build 4f3c61
	soup_websocket_connection_set_max_incoming_payload_size (test->server, 1000 * 1000 + 1);
rpm-build 4f3c61
	g_assert (soup_websocket_connection_get_max_incoming_payload_size (test->server) == (1000 * 1000 + 1));
rpm-build 4f3c61
rpm-build 4f3c61
	sent = g_bytes_new_take (g_strnfill (1000 * 1000, '?'), 1000 * 1000);
rpm-build 4f3c61
	soup_websocket_connection_send_text (test->server, g_bytes_get_data (sent, NULL));
rpm-build 4f3c61
	WAIT_UNTIL (received != NULL);
rpm-build 4f3c61
	g_assert (g_bytes_equal (sent, received));
rpm-build 4f3c61
	g_bytes_unref (sent);
rpm-build 4f3c61
	g_bytes_unref (received);
rpm-build 4f3c61
}
rpm-build 4f3c61
rpm-build 4f3c61
static void
rpm-build 4f3c61
test_send_bad_data (Test *test,
rpm-build 4f3c61
                    gconstpointer unused)
rpm-build 4f3c61
{
rpm-build 4f3c61
	GError *error = NULL;
rpm-build 4f3c61
	GIOStream *io;
rpm-build 4f3c61
	gsize written;
rpm-build 4f3c61
	const char *frame;
rpm-build 4f3c61
rpm-build 4f3c61
	g_signal_handlers_disconnect_by_func (test->server, on_error_not_reached, NULL);
rpm-build 4f3c61
	g_signal_connect (test->server, "error", G_CALLBACK (on_error_copy), &error);
rpm-build 4f3c61
rpm-build 4f3c61
	io = soup_websocket_connection_get_io_stream (test->client);
rpm-build 4f3c61
rpm-build 4f3c61
	/* Bad UTF-8 frame */
rpm-build 4f3c61
	frame = "\x81\x04\xEE\xEE\xEE\xEE";
rpm-build 4f3c61
	if (!g_output_stream_write_all (g_io_stream_get_output_stream (io),
rpm-build 4f3c61
					frame, 6, &written, NULL, NULL))
rpm-build 4f3c61
		g_assert_not_reached ();
rpm-build 4f3c61
	g_assert_cmpuint (written, ==, 6);
rpm-build 4f3c61
rpm-build 4f3c61
	WAIT_UNTIL (error != NULL);
rpm-build 4f3c61
	g_assert_error (error, SOUP_WEBSOCKET_ERROR, SOUP_WEBSOCKET_CLOSE_BAD_DATA);
rpm-build 4f3c61
	g_clear_error (&error);
rpm-build 4f3c61
rpm-build 4f3c61
	WAIT_UNTIL (soup_websocket_connection_get_state (test->client) == SOUP_WEBSOCKET_STATE_CLOSED);
rpm-build 4f3c61
rpm-build 4f3c61
	g_assert_cmpuint (soup_websocket_connection_get_close_code (test->client), ==, SOUP_WEBSOCKET_CLOSE_BAD_DATA);
rpm-build 4f3c61
}
rpm-build 4f3c61
rpm-build 4f3c61
static const char *negotiate_client_protocols[] = { "bbb", "ccc", NULL };
rpm-build 4f3c61
static const char *negotiate_server_protocols[] = { "aaa", "bbb", "ccc", NULL };
rpm-build 4f3c61
static const char *negotiated_protocol = "bbb";
rpm-build 4f3c61
rpm-build 4f3c61
static void
rpm-build 4f3c61
test_protocol_negotiate_direct (Test *test,
rpm-build 4f3c61
				gconstpointer unused)
rpm-build 4f3c61
{
rpm-build 4f3c61
	SoupMessage *msg;
rpm-build 4f3c61
	gboolean ok;
rpm-build 4f3c61
	const char *protocol;
rpm-build 4f3c61
	GError *error = NULL;
rpm-build 4f3c61
rpm-build 4f3c61
	msg = soup_message_new ("GET", "http://127.0.0.1");
rpm-build 4f3c61
	soup_websocket_client_prepare_handshake (msg, NULL,
rpm-build 4f3c61
						 (char **) negotiate_client_protocols);
rpm-build 4f3c61
rpm-build 4f3c61
	ok = soup_websocket_server_check_handshake (msg, NULL,
rpm-build 4f3c61
						    (char **) negotiate_server_protocols,
rpm-build 4f3c61
						    &error);
rpm-build 4f3c61
	g_assert_no_error (error);
rpm-build 4f3c61
	g_assert_true (ok);
rpm-build 4f3c61
rpm-build 4f3c61
	ok = soup_websocket_server_process_handshake (msg, NULL,
rpm-build 4f3c61
						      (char **) negotiate_server_protocols);
rpm-build 4f3c61
	g_assert_true (ok);
rpm-build 4f3c61
rpm-build 4f3c61
	protocol = soup_message_headers_get_one (msg->response_headers, "Sec-WebSocket-Protocol");
rpm-build 4f3c61
	g_assert_cmpstr (protocol, ==, negotiated_protocol);
rpm-build 4f3c61
rpm-build 4f3c61
	ok = soup_websocket_client_verify_handshake (msg, &error);
rpm-build 4f3c61
	g_assert_no_error (error);
rpm-build 4f3c61
	g_assert_true (ok);
rpm-build 4f3c61
rpm-build 4f3c61
	g_object_unref (msg);
rpm-build 4f3c61
}
rpm-build 4f3c61
rpm-build 4f3c61
static void
rpm-build 4f3c61
test_protocol_negotiate_soup (Test *test,
rpm-build 4f3c61
			      gconstpointer unused)
rpm-build 4f3c61
{
rpm-build 4f3c61
	setup_soup_server (test, NULL, negotiate_server_protocols, got_server_connection, test);
rpm-build 4f3c61
	client_connect (test, NULL, negotiate_client_protocols, got_client_connection, test);
rpm-build 4f3c61
	WAIT_UNTIL (test->server != NULL);
rpm-build 4f3c61
	WAIT_UNTIL (test->client != NULL || test->client_error != NULL);
rpm-build 4f3c61
	g_assert_no_error (test->client_error);
rpm-build 4f3c61
rpm-build 4f3c61
	g_assert_cmpstr (soup_websocket_connection_get_protocol (test->client), ==, negotiated_protocol);
rpm-build 4f3c61
	g_assert_cmpstr (soup_websocket_connection_get_protocol (test->server), ==, negotiated_protocol);
rpm-build 4f3c61
}
rpm-build 4f3c61
rpm-build 4f3c61
static const char *mismatch_client_protocols[] = { "ddd", NULL };
rpm-build 4f3c61
static const char *mismatch_server_protocols[] = { "aaa", "bbb", "ccc", NULL };
rpm-build 4f3c61
rpm-build 4f3c61
static void
rpm-build 4f3c61
test_protocol_mismatch_direct (Test *test,
rpm-build 4f3c61
			       gconstpointer unused)
rpm-build 4f3c61
{
rpm-build 4f3c61
	SoupMessage *msg;
rpm-build 4f3c61
	gboolean ok;
rpm-build 4f3c61
	const char *protocol;
rpm-build 4f3c61
	GError *error = NULL;
rpm-build 4f3c61
rpm-build 4f3c61
	msg = soup_message_new ("GET", "http://127.0.0.1");
rpm-build 4f3c61
	soup_websocket_client_prepare_handshake (msg, NULL,
rpm-build 4f3c61
						 (char **) mismatch_client_protocols);
rpm-build 4f3c61
rpm-build 4f3c61
	ok = soup_websocket_server_check_handshake (msg, NULL,
rpm-build 4f3c61
						    (char **) mismatch_server_protocols,
rpm-build 4f3c61
						    &error);
rpm-build 4f3c61
	g_assert_error (error, SOUP_WEBSOCKET_ERROR, SOUP_WEBSOCKET_ERROR_BAD_HANDSHAKE);
rpm-build 4f3c61
	g_clear_error (&error);
rpm-build 4f3c61
	g_assert_false (ok);
rpm-build 4f3c61
rpm-build 4f3c61
	ok = soup_websocket_server_process_handshake (msg, NULL,
rpm-build 4f3c61
						      (char **) mismatch_server_protocols);
rpm-build 4f3c61
	g_assert_false (ok);
rpm-build 4f3c61
	soup_test_assert_message_status (msg, SOUP_STATUS_BAD_REQUEST);
rpm-build 4f3c61
rpm-build 4f3c61
	protocol = soup_message_headers_get_one (msg->response_headers, "Sec-WebSocket-Protocol");
rpm-build 4f3c61
	g_assert_cmpstr (protocol, ==, NULL);
rpm-build 4f3c61
rpm-build 4f3c61
	ok = soup_websocket_client_verify_handshake (msg, &error);
rpm-build 4f3c61
	g_assert_error (error, SOUP_WEBSOCKET_ERROR, SOUP_WEBSOCKET_ERROR_BAD_HANDSHAKE);
rpm-build 4f3c61
	g_clear_error (&error);
rpm-build 4f3c61
	g_assert_false (ok);
rpm-build 4f3c61
rpm-build 4f3c61
	g_object_unref (msg);
rpm-build 4f3c61
}
rpm-build 4f3c61
rpm-build 4f3c61
static void
rpm-build 4f3c61
test_protocol_mismatch_soup (Test *test,
rpm-build 4f3c61
			     gconstpointer unused)
rpm-build 4f3c61
{
rpm-build 4f3c61
	setup_soup_server (test, NULL, mismatch_server_protocols, got_server_connection, test);
rpm-build 4f3c61
	client_connect (test, NULL, mismatch_client_protocols, got_client_connection, test);
rpm-build 4f3c61
	WAIT_UNTIL (test->client_error != NULL);
rpm-build 4f3c61
rpm-build 4f3c61
	g_assert_error (test->client_error, SOUP_WEBSOCKET_ERROR, SOUP_WEBSOCKET_ERROR_NOT_WEBSOCKET);
rpm-build 4f3c61
}
rpm-build 4f3c61
rpm-build 4f3c61
static const char *all_protocols[] = { "aaa", "bbb", "ccc", NULL };
rpm-build 4f3c61
rpm-build 4f3c61
static void
rpm-build 4f3c61
test_protocol_server_any_direct (Test *test,
rpm-build 4f3c61
				 gconstpointer unused)
rpm-build 4f3c61
{
rpm-build 4f3c61
	SoupMessage *msg;
rpm-build 4f3c61
	gboolean ok;
rpm-build 4f3c61
	const char *protocol;
rpm-build 4f3c61
	GError *error = NULL;
rpm-build 4f3c61
rpm-build 4f3c61
	msg = soup_message_new ("GET", "http://127.0.0.1");
rpm-build 4f3c61
	soup_websocket_client_prepare_handshake (msg, NULL, (char **) all_protocols);
rpm-build 4f3c61
rpm-build 4f3c61
	ok = soup_websocket_server_check_handshake (msg, NULL, NULL, &error);
rpm-build 4f3c61
	g_assert_no_error (error);
rpm-build 4f3c61
	g_assert_true (ok);
rpm-build 4f3c61
rpm-build 4f3c61
	ok = soup_websocket_server_process_handshake (msg, NULL, NULL);
rpm-build 4f3c61
	g_assert_true (ok);
rpm-build 4f3c61
rpm-build 4f3c61
	protocol = soup_message_headers_get_one (msg->response_headers, "Sec-WebSocket-Protocol");
rpm-build 4f3c61
	g_assert_cmpstr (protocol, ==, NULL);
rpm-build 4f3c61
rpm-build 4f3c61
	ok = soup_websocket_client_verify_handshake (msg, &error);
rpm-build 4f3c61
	g_assert_no_error (error);
rpm-build 4f3c61
	g_assert_true (ok);
rpm-build 4f3c61
rpm-build 4f3c61
	g_object_unref (msg);
rpm-build 4f3c61
}
rpm-build 4f3c61
rpm-build 4f3c61
static void
rpm-build 4f3c61
test_protocol_server_any_soup (Test *test,
rpm-build 4f3c61
			       gconstpointer unused)
rpm-build 4f3c61
{
rpm-build 4f3c61
	setup_soup_server (test, NULL, NULL, got_server_connection, test);
rpm-build 4f3c61
	client_connect (test, NULL, all_protocols, got_client_connection, test);
rpm-build 4f3c61
	WAIT_UNTIL (test->server != NULL);
rpm-build 4f3c61
	WAIT_UNTIL (test->client != NULL || test->client_error != NULL);
rpm-build 4f3c61
	g_assert_no_error (test->client_error);
rpm-build 4f3c61
rpm-build 4f3c61
	g_assert_cmpstr (soup_websocket_connection_get_protocol (test->client), ==, NULL);
rpm-build 4f3c61
	g_assert_cmpstr (soup_websocket_connection_get_protocol (test->server), ==, NULL);
rpm-build 4f3c61
	g_assert_cmpstr (soup_message_headers_get_one (test->msg->response_headers, "Sec-WebSocket-Protocol"), ==, NULL);
rpm-build 4f3c61
}
rpm-build 4f3c61
rpm-build 4f3c61
static void
rpm-build 4f3c61
test_protocol_client_any_direct (Test *test,
rpm-build 4f3c61
				 gconstpointer unused)
rpm-build 4f3c61
{
rpm-build 4f3c61
	SoupMessage *msg;
rpm-build 4f3c61
	gboolean ok;
rpm-build 4f3c61
	const char *protocol;
rpm-build 4f3c61
	GError *error = NULL;
rpm-build 4f3c61
rpm-build 4f3c61
	msg = soup_message_new ("GET", "http://127.0.0.1");
rpm-build 4f3c61
	soup_websocket_client_prepare_handshake (msg, NULL, NULL);
rpm-build 4f3c61
rpm-build 4f3c61
	ok = soup_websocket_server_check_handshake (msg, NULL, (char **) all_protocols, &error);
rpm-build 4f3c61
	g_assert_no_error (error);
rpm-build 4f3c61
	g_assert_true (ok);
rpm-build 4f3c61
rpm-build 4f3c61
	ok = soup_websocket_server_process_handshake (msg, NULL, (char **) all_protocols);
rpm-build 4f3c61
	g_assert_true (ok);
rpm-build 4f3c61
rpm-build 4f3c61
	protocol = soup_message_headers_get_one (msg->response_headers, "Sec-WebSocket-Protocol");
rpm-build 4f3c61
	g_assert_cmpstr (protocol, ==, NULL);
rpm-build 4f3c61
rpm-build 4f3c61
	ok = soup_websocket_client_verify_handshake (msg, &error);
rpm-build 4f3c61
	g_assert_no_error (error);
rpm-build 4f3c61
	g_assert_true (ok);
rpm-build 4f3c61
rpm-build 4f3c61
	g_object_unref (msg);
rpm-build 4f3c61
}
rpm-build 4f3c61
rpm-build 4f3c61
static void
rpm-build 4f3c61
test_protocol_client_any_soup (Test *test,
rpm-build 4f3c61
			       gconstpointer unused)
rpm-build 4f3c61
{
rpm-build 4f3c61
	setup_soup_server (test, NULL, all_protocols, got_server_connection, test);
rpm-build 4f3c61
	client_connect (test, NULL, NULL, got_client_connection, test);
rpm-build 4f3c61
	WAIT_UNTIL (test->server != NULL);
rpm-build 4f3c61
	WAIT_UNTIL (test->client != NULL || test->client_error != NULL);
rpm-build 4f3c61
	g_assert_no_error (test->client_error);
rpm-build 4f3c61
rpm-build 4f3c61
	g_assert_cmpstr (soup_websocket_connection_get_protocol (test->client), ==, NULL);
rpm-build 4f3c61
	g_assert_cmpstr (soup_websocket_connection_get_protocol (test->server), ==, NULL);
rpm-build 4f3c61
	g_assert_cmpstr (soup_message_headers_get_one (test->msg->response_headers, "Sec-WebSocket-Protocol"), ==, NULL);
rpm-build 4f3c61
}
rpm-build 4f3c61
rpm-build 4f3c61
static void
rpm-build 4f3c61
test_close_clean_client (Test *test,
rpm-build 4f3c61
                         gconstpointer data)
rpm-build 4f3c61
{
rpm-build 4f3c61
	gboolean close_event_client = FALSE;
rpm-build 4f3c61
	gboolean close_event_server = FALSE;
rpm-build 4f3c61
rpm-build 4f3c61
	g_signal_connect (test->client, "closed", G_CALLBACK (on_close_set_flag), &close_event_client);
rpm-build 4f3c61
	g_signal_connect (test->server, "closed", G_CALLBACK (on_close_set_flag), &close_event_server);
rpm-build 4f3c61
rpm-build 4f3c61
	soup_websocket_connection_close (test->client, SOUP_WEBSOCKET_CLOSE_GOING_AWAY, "give me a reason");
rpm-build 4f3c61
	g_assert_cmpint (soup_websocket_connection_get_state (test->client), ==, SOUP_WEBSOCKET_STATE_CLOSING);
rpm-build 4f3c61
rpm-build 4f3c61
	WAIT_UNTIL (soup_websocket_connection_get_state (test->server) == SOUP_WEBSOCKET_STATE_CLOSED);
rpm-build 4f3c61
	WAIT_UNTIL (soup_websocket_connection_get_state (test->client) == SOUP_WEBSOCKET_STATE_CLOSED);
rpm-build 4f3c61
rpm-build 4f3c61
	g_assert (close_event_client);
rpm-build 4f3c61
	g_assert (close_event_server);
rpm-build 4f3c61
rpm-build 4f3c61
	g_assert_cmpint (soup_websocket_connection_get_close_code (test->client), ==, SOUP_WEBSOCKET_CLOSE_GOING_AWAY);
rpm-build 4f3c61
	g_assert_cmpint (soup_websocket_connection_get_close_code (test->server), ==, SOUP_WEBSOCKET_CLOSE_GOING_AWAY);
rpm-build 4f3c61
	g_assert_cmpstr (soup_websocket_connection_get_close_data (test->server), ==, "give me a reason");
rpm-build 4f3c61
}
rpm-build 4f3c61
rpm-build 4f3c61
static void
rpm-build 4f3c61
test_close_clean_server (Test *test,
rpm-build 4f3c61
                         gconstpointer data)
rpm-build 4f3c61
{
rpm-build 4f3c61
	gboolean close_event_client = FALSE;
rpm-build 4f3c61
	gboolean close_event_server = FALSE;
rpm-build 4f3c61
rpm-build 4f3c61
	g_signal_connect (test->client, "closed", G_CALLBACK (on_close_set_flag), &close_event_client);
rpm-build 4f3c61
	g_signal_connect (test->server, "closed", G_CALLBACK (on_close_set_flag), &close_event_server);
rpm-build 4f3c61
rpm-build 4f3c61
	soup_websocket_connection_close (test->server, SOUP_WEBSOCKET_CLOSE_GOING_AWAY, "another reason");
rpm-build 4f3c61
	g_assert_cmpint (soup_websocket_connection_get_state (test->server), ==, SOUP_WEBSOCKET_STATE_CLOSING);
rpm-build 4f3c61
rpm-build 4f3c61
	WAIT_UNTIL (soup_websocket_connection_get_state (test->server) == SOUP_WEBSOCKET_STATE_CLOSED);
rpm-build 4f3c61
	WAIT_UNTIL (soup_websocket_connection_get_state (test->client) == SOUP_WEBSOCKET_STATE_CLOSED);
rpm-build 4f3c61
rpm-build 4f3c61
	g_assert (close_event_client);
rpm-build 4f3c61
	g_assert (close_event_server);
rpm-build 4f3c61
rpm-build 4f3c61
	g_assert_cmpint (soup_websocket_connection_get_close_code (test->server), ==, SOUP_WEBSOCKET_CLOSE_GOING_AWAY);
rpm-build 4f3c61
	g_assert_cmpint (soup_websocket_connection_get_close_code (test->client), ==, SOUP_WEBSOCKET_CLOSE_GOING_AWAY);
rpm-build 4f3c61
	g_assert_cmpstr (soup_websocket_connection_get_close_data (test->client), ==, "another reason");
rpm-build 4f3c61
}
rpm-build 4f3c61
rpm-build 4f3c61
static gboolean
rpm-build 4f3c61
on_closing_send_message (SoupWebsocketConnection *ws,
rpm-build 4f3c61
                         gpointer data)
rpm-build 4f3c61
{
rpm-build 4f3c61
	GBytes *message = data;
rpm-build 4f3c61
rpm-build 4f3c61
	soup_websocket_connection_send_text (ws, g_bytes_get_data (message, NULL));
rpm-build 4f3c61
	g_signal_handlers_disconnect_by_func (ws, on_closing_send_message, data);
rpm-build 4f3c61
	return TRUE;
rpm-build 4f3c61
}
rpm-build 4f3c61
rpm-build 4f3c61
static void
rpm-build 4f3c61
test_message_after_closing (Test *test,
rpm-build 4f3c61
                            gconstpointer data)
rpm-build 4f3c61
{
rpm-build 4f3c61
	gboolean close_event_client = FALSE;
rpm-build 4f3c61
	gboolean close_event_server = FALSE;
rpm-build 4f3c61
	GBytes *received = NULL;
rpm-build 4f3c61
	GBytes *message;
rpm-build 4f3c61
rpm-build 4f3c61
	message = g_bytes_new_static ("another test because", strlen ("another test because"));
rpm-build 4f3c61
	g_signal_connect (test->client, "closed", G_CALLBACK (on_close_set_flag), &close_event_client);
rpm-build 4f3c61
	g_signal_connect (test->client, "message", G_CALLBACK (on_text_message), &received);
rpm-build 4f3c61
	g_signal_connect (test->server, "closed", G_CALLBACK (on_close_set_flag), &close_event_server);
rpm-build 4f3c61
	g_signal_connect (test->server, "closing", G_CALLBACK (on_closing_send_message), message);
rpm-build 4f3c61
rpm-build 4f3c61
	soup_websocket_connection_close (test->client, SOUP_WEBSOCKET_CLOSE_GOING_AWAY, "another reason");
rpm-build 4f3c61
	g_assert_cmpint (soup_websocket_connection_get_state (test->client), ==, SOUP_WEBSOCKET_STATE_CLOSING);
rpm-build 4f3c61
rpm-build 4f3c61
	WAIT_UNTIL (soup_websocket_connection_get_state (test->server) == SOUP_WEBSOCKET_STATE_CLOSED);
rpm-build 4f3c61
	WAIT_UNTIL (soup_websocket_connection_get_state (test->client) == SOUP_WEBSOCKET_STATE_CLOSED);
rpm-build 4f3c61
rpm-build 4f3c61
	g_assert (close_event_client);
rpm-build 4f3c61
	g_assert (close_event_server);
rpm-build 4f3c61
rpm-build 4f3c61
	g_assert (received != NULL);
rpm-build 4f3c61
	g_assert (g_bytes_equal (message, received));
rpm-build 4f3c61
rpm-build 4f3c61
	g_bytes_unref (received);
rpm-build 4f3c61
	g_bytes_unref (message);
rpm-build 4f3c61
}
rpm-build 4f3c61
rpm-build 4f3c61
static gpointer
rpm-build 4f3c61
timeout_server_thread (gpointer user_data)
rpm-build 4f3c61
{
rpm-build 4f3c61
	Test *test = user_data;
rpm-build 4f3c61
	GError *error = NULL;
rpm-build 4f3c61
rpm-build 4f3c61
	/* don't close until the client has timed out */
rpm-build 4f3c61
	g_mutex_lock (&test->mutex);
rpm-build 4f3c61
	g_mutex_unlock (&test->mutex);
rpm-build 4f3c61
rpm-build 4f3c61
	g_io_stream_close (test->raw_server, NULL, &error);
rpm-build 4f3c61
	g_assert_no_error (error);
rpm-build 4f3c61
rpm-build 4f3c61
	return NULL;
rpm-build 4f3c61
}
rpm-build 4f3c61
rpm-build 4f3c61
static void
rpm-build 4f3c61
test_close_after_timeout (Test *test,
rpm-build 4f3c61
			  gconstpointer data)
rpm-build 4f3c61
{
rpm-build 4f3c61
	gboolean close_event = FALSE;
rpm-build 4f3c61
	GThread *thread;
rpm-build 4f3c61
rpm-build 4f3c61
	g_mutex_lock (&test->mutex);
rpm-build 4f3c61
rpm-build 4f3c61
	/* Note that no real server is around in this test, so no close happens */
rpm-build 4f3c61
	thread = g_thread_new ("timeout-thread", timeout_server_thread, test);
rpm-build 4f3c61
rpm-build 4f3c61
	g_signal_connect (test->client, "closed", G_CALLBACK (on_close_set_flag), &close_event);
rpm-build 4f3c61
	g_signal_connect (test->client, "error", G_CALLBACK (on_error_not_reached), NULL);
rpm-build 4f3c61
rpm-build 4f3c61
	/* Now try and close things */
rpm-build 4f3c61
	soup_websocket_connection_close (test->client, 0, NULL);
rpm-build 4f3c61
	g_assert_cmpint (soup_websocket_connection_get_state (test->client), ==, SOUP_WEBSOCKET_STATE_CLOSING);
rpm-build 4f3c61
rpm-build 4f3c61
	WAIT_UNTIL (soup_websocket_connection_get_state (test->client) == SOUP_WEBSOCKET_STATE_CLOSED);
rpm-build 4f3c61
rpm-build 4f3c61
	g_assert (close_event == TRUE);
rpm-build 4f3c61
rpm-build 4f3c61
	/* Now actually close the server side stream */
rpm-build 4f3c61
	g_mutex_unlock (&test->mutex);
rpm-build 4f3c61
	g_thread_join (thread);
rpm-build 4f3c61
}
rpm-build 4f3c61
rpm-build 4f3c61
static gpointer
rpm-build 4f3c61
send_fragments_server_thread (gpointer user_data)
rpm-build 4f3c61
{
rpm-build 4f3c61
	Test *test = user_data;
rpm-build 4f3c61
	gsize written;
rpm-build 4f3c61
	const char fragments[] = "\x01\x04""one "   /* !fin | opcode */
rpm-build 4f3c61
		"\x00\x04""two "   /* !fin | no opcode */
rpm-build 4f3c61
		"\x80\x05""three"; /* fin  | no opcode */
rpm-build 4f3c61
	GError *error = NULL;
rpm-build 4f3c61
rpm-build 4f3c61
	g_output_stream_write_all (g_io_stream_get_output_stream (test->raw_server),
rpm-build 4f3c61
				   fragments, sizeof (fragments) -1, &written, NULL, &error);
rpm-build 4f3c61
	g_assert_no_error (error);
rpm-build 4f3c61
	g_assert_cmpuint (written, ==, sizeof (fragments) - 1);
rpm-build 4f3c61
	g_io_stream_close (test->raw_server, NULL, &error);
rpm-build 4f3c61
	g_assert_no_error (error);
rpm-build 4f3c61
rpm-build 4f3c61
	return NULL;
rpm-build 4f3c61
}
rpm-build 4f3c61
rpm-build 4f3c61
static void
rpm-build 4f3c61
test_receive_fragmented (Test *test,
rpm-build 4f3c61
			 gconstpointer data)
rpm-build 4f3c61
{
rpm-build 4f3c61
	GThread *thread;
rpm-build 4f3c61
	GBytes *received = NULL;
rpm-build 4f3c61
	GBytes *expect;
rpm-build 4f3c61
rpm-build 4f3c61
	thread = g_thread_new ("fragment-thread", send_fragments_server_thread, test);
rpm-build 4f3c61
rpm-build 4f3c61
	g_signal_connect (test->client, "error", G_CALLBACK (on_error_not_reached), NULL);
rpm-build 4f3c61
	g_signal_connect (test->client, "message", G_CALLBACK (on_text_message), &received);
rpm-build 4f3c61
rpm-build 4f3c61
	WAIT_UNTIL (received != NULL);
rpm-build 4f3c61
	expect = g_bytes_new ("one two three", 13);
rpm-build 4f3c61
	g_assert (g_bytes_equal (expect, received));
rpm-build 4f3c61
	g_bytes_unref (expect);
rpm-build 4f3c61
	g_bytes_unref (received);
rpm-build 4f3c61
rpm-build 4f3c61
	g_thread_join (thread);
rpm-build 4f3c61
rpm-build 4f3c61
	WAIT_UNTIL (soup_websocket_connection_get_state (test->client) == SOUP_WEBSOCKET_STATE_CLOSED);
rpm-build 4f3c61
}
rpm-build 4f3c61
rpm-build 4f3c61
static void
rpm-build 4f3c61
test_client_context_got_server_connection (SoupServer              *server,
rpm-build 4f3c61
					   SoupWebsocketConnection *connection,
rpm-build 4f3c61
					   const char              *path,
rpm-build 4f3c61
					   SoupClientContext       *client,
rpm-build 4f3c61
					   gpointer                 user_data)
rpm-build 4f3c61
{
rpm-build 4f3c61
	Test *test = user_data;
rpm-build 4f3c61
	GSocketAddress *addr;
rpm-build 4f3c61
	GInetAddress *iaddr;
rpm-build 4f3c61
	char *str;
rpm-build 4f3c61
	const char *remote_ip;
rpm-build 4f3c61
rpm-build 4f3c61
	addr = soup_client_context_get_local_address (client);
rpm-build 4f3c61
	iaddr = g_inet_socket_address_get_address (G_INET_SOCKET_ADDRESS (addr));
rpm-build 4f3c61
	str = g_inet_address_to_string (iaddr);
rpm-build 4f3c61
	if (g_inet_address_get_family (iaddr) == G_SOCKET_FAMILY_IPV4)
rpm-build 4f3c61
		g_assert_cmpstr (str, ==, "127.0.0.1");
rpm-build 4f3c61
	else
rpm-build 4f3c61
		g_assert_cmpstr (str, ==, "::1");
rpm-build 4f3c61
	g_free (str);
rpm-build 4f3c61
rpm-build 4f3c61
	addr = soup_client_context_get_remote_address (client);
rpm-build 4f3c61
	iaddr = g_inet_socket_address_get_address (G_INET_SOCKET_ADDRESS (addr));
rpm-build 4f3c61
	str = g_inet_address_to_string (iaddr);
rpm-build 4f3c61
	if (g_inet_address_get_family (iaddr) == G_SOCKET_FAMILY_IPV4)
rpm-build 4f3c61
		g_assert_cmpstr (str, ==, "127.0.0.1");
rpm-build 4f3c61
	else
rpm-build 4f3c61
		g_assert_cmpstr (str, ==, "::1");
rpm-build 4f3c61
rpm-build 4f3c61
	remote_ip = soup_client_context_get_host (client);
rpm-build 4f3c61
	g_assert_cmpstr (remote_ip, ==, str);
rpm-build 4f3c61
	g_free (str);
rpm-build 4f3c61
rpm-build 4f3c61
	test->server = g_object_ref (connection);
rpm-build 4f3c61
}
rpm-build 4f3c61
rpm-build 4f3c61
static void
rpm-build 4f3c61
test_client_context (Test *test,
rpm-build 4f3c61
		     gconstpointer unused)
rpm-build 4f3c61
{
rpm-build 4f3c61
	setup_soup_server (test, NULL, NULL, test_client_context_got_server_connection, test);
rpm-build 4f3c61
	client_connect (test, NULL, NULL, got_client_connection, test);
rpm-build 4f3c61
	WAIT_UNTIL (test->server != NULL);
rpm-build 4f3c61
	WAIT_UNTIL (test->client != NULL || test->client_error != NULL);
rpm-build 4f3c61
	g_assert_no_error (test->client_error);
rpm-build 4f3c61
}
rpm-build 4f3c61
rpm-build 4f3c61
int
rpm-build 4f3c61
main (int argc,
rpm-build 4f3c61
      char *argv[])
rpm-build 4f3c61
{
rpm-build 4f3c61
	int ret;
rpm-build 4f3c61
rpm-build 4f3c61
	test_init (argc, argv, NULL);
rpm-build 4f3c61
rpm-build 4f3c61
	g_test_add ("/websocket/soup/handshake", Test, NULL, 
rpm-build 4f3c61
		    setup_soup_connection,
rpm-build 4f3c61
		    test_handshake,
rpm-build 4f3c61
		    teardown_soup_connection);
rpm-build 4f3c61
rpm-build 4f3c61
	g_test_add ("/websocket/direct/send-client-to-server", Test, NULL,
rpm-build 4f3c61
		    setup_direct_connection,
rpm-build 4f3c61
		    test_send_client_to_server,
rpm-build 4f3c61
		    teardown_direct_connection);
rpm-build 4f3c61
	g_test_add ("/websocket/soup/send-client-to-server", Test, NULL, 
rpm-build 4f3c61
		    setup_soup_connection,
rpm-build 4f3c61
		    test_send_client_to_server,
rpm-build 4f3c61
		    teardown_soup_connection);
rpm-build 4f3c61
rpm-build 4f3c61
	g_test_add ("/websocket/direct/send-server-to-client", Test, NULL,
rpm-build 4f3c61
		    setup_direct_connection,
rpm-build 4f3c61
		    test_send_server_to_client,
rpm-build 4f3c61
		    teardown_direct_connection);
rpm-build 4f3c61
	g_test_add ("/websocket/soup/send-server-to-client", Test, NULL,
rpm-build 4f3c61
		    setup_soup_connection,
rpm-build 4f3c61
		    test_send_server_to_client,
rpm-build 4f3c61
		    teardown_soup_connection);
rpm-build 4f3c61
rpm-build 4f3c61
	g_test_add ("/websocket/direct/send-big-packets", Test, NULL,
rpm-build 4f3c61
		    setup_direct_connection,
rpm-build 4f3c61
		    test_send_big_packets,
rpm-build 4f3c61
		    teardown_direct_connection);
rpm-build 4f3c61
	g_test_add ("/websocket/soup/send-big-packets", Test, NULL,
rpm-build 4f3c61
		    setup_soup_connection,
rpm-build 4f3c61
		    test_send_big_packets,
rpm-build 4f3c61
		    teardown_soup_connection);
rpm-build 4f3c61
rpm-build 4f3c61
	g_test_add ("/websocket/direct/send-bad-data", Test, NULL,
rpm-build 4f3c61
		    setup_direct_connection,
rpm-build 4f3c61
		    test_send_bad_data,
rpm-build 4f3c61
		    teardown_direct_connection);
rpm-build 4f3c61
	g_test_add ("/websocket/soup/send-bad-data", Test, NULL,
rpm-build 4f3c61
		    setup_soup_connection,
rpm-build 4f3c61
		    test_send_bad_data,
rpm-build 4f3c61
		    teardown_soup_connection);
rpm-build 4f3c61
rpm-build 4f3c61
	g_test_add ("/websocket/direct/close-clean-client", Test, NULL,
rpm-build 4f3c61
		    setup_direct_connection,
rpm-build 4f3c61
		    test_close_clean_client,
rpm-build 4f3c61
		    teardown_direct_connection);
rpm-build 4f3c61
	g_test_add ("/websocket/soup/close-clean-client", Test, NULL,
rpm-build 4f3c61
		    setup_soup_connection,
rpm-build 4f3c61
		    test_close_clean_client,
rpm-build 4f3c61
		    teardown_soup_connection);
rpm-build 4f3c61
rpm-build 4f3c61
	g_test_add ("/websocket/direct/close-clean-server", Test, NULL,
rpm-build 4f3c61
		    setup_direct_connection,
rpm-build 4f3c61
		    test_close_clean_server,
rpm-build 4f3c61
		    teardown_direct_connection);
rpm-build 4f3c61
	g_test_add ("/websocket/soup/close-clean-server", Test, NULL,
rpm-build 4f3c61
		    setup_soup_connection,
rpm-build 4f3c61
		    test_close_clean_server,
rpm-build 4f3c61
		    teardown_soup_connection);
rpm-build 4f3c61
rpm-build 4f3c61
	g_test_add ("/websocket/direct/message-after-closing", Test, NULL,
rpm-build 4f3c61
		    setup_direct_connection,
rpm-build 4f3c61
		    test_message_after_closing,
rpm-build 4f3c61
		    teardown_direct_connection);
rpm-build 4f3c61
	g_test_add ("/websocket/soup/message-after-closing", Test, NULL,
rpm-build 4f3c61
		    setup_soup_connection,
rpm-build 4f3c61
		    test_message_after_closing,
rpm-build 4f3c61
		    teardown_soup_connection);
rpm-build 4f3c61
rpm-build 4f3c61
rpm-build 4f3c61
	g_test_add ("/websocket/direct/protocol-negotiate", Test, NULL, NULL,
rpm-build 4f3c61
		    test_protocol_negotiate_direct,
rpm-build 4f3c61
		    NULL);
rpm-build 4f3c61
	g_test_add ("/websocket/soup/protocol-negotiate", Test, NULL, NULL,
rpm-build 4f3c61
		    test_protocol_negotiate_soup,
rpm-build 4f3c61
		    teardown_soup_connection);
rpm-build 4f3c61
rpm-build 4f3c61
	g_test_add ("/websocket/direct/protocol-mismatch", Test, NULL, NULL,
rpm-build 4f3c61
		    test_protocol_mismatch_direct,
rpm-build 4f3c61
		    NULL);
rpm-build 4f3c61
	g_test_add ("/websocket/soup/protocol-mismatch", Test, NULL, NULL,
rpm-build 4f3c61
		    test_protocol_mismatch_soup,
rpm-build 4f3c61
		    teardown_soup_connection);
rpm-build 4f3c61
rpm-build 4f3c61
	g_test_add ("/websocket/direct/protocol-server-any", Test, NULL, NULL,
rpm-build 4f3c61
		    test_protocol_server_any_direct,
rpm-build 4f3c61
		    NULL);
rpm-build 4f3c61
	g_test_add ("/websocket/soup/protocol-server-any", Test, NULL, NULL,
rpm-build 4f3c61
		    test_protocol_server_any_soup,
rpm-build 4f3c61
		    teardown_soup_connection);
rpm-build 4f3c61
rpm-build 4f3c61
	g_test_add ("/websocket/direct/protocol-client-any", Test, NULL, NULL,
rpm-build 4f3c61
		    test_protocol_client_any_direct,
rpm-build 4f3c61
		    NULL);
rpm-build 4f3c61
	g_test_add ("/websocket/soup/protocol-client-any", Test, NULL, NULL,
rpm-build 4f3c61
		    test_protocol_client_any_soup,
rpm-build 4f3c61
		    teardown_soup_connection);
rpm-build 4f3c61
rpm-build 4f3c61
rpm-build 4f3c61
	g_test_add ("/websocket/direct/receive-fragmented", Test, NULL,
rpm-build 4f3c61
		    setup_half_direct_connection,
rpm-build 4f3c61
		    test_receive_fragmented,
rpm-build 4f3c61
		    teardown_direct_connection);
rpm-build 4f3c61
rpm-build 4f3c61
	if (g_test_slow ()) {
rpm-build 4f3c61
		g_test_add ("/websocket/direct/close-after-timeout", Test, NULL,
rpm-build 4f3c61
			    setup_half_direct_connection,
rpm-build 4f3c61
			    test_close_after_timeout,
rpm-build 4f3c61
			    teardown_direct_connection);
rpm-build 4f3c61
	}
rpm-build 4f3c61
rpm-build 4f3c61
	g_test_add ("/websocket/soup/client-context", Test, NULL, NULL,
rpm-build 4f3c61
		    test_client_context,
rpm-build 4f3c61
		    teardown_soup_connection);
rpm-build 4f3c61
rpm-build 4f3c61
	ret = g_test_run ();
rpm-build 4f3c61
rpm-build 4f3c61
	test_cleanup ();
rpm-build 4f3c61
	return ret;
rpm-build 4f3c61
}