From 778ecf1762b91649ba810c450ae2dc6d599fed0a Mon Sep 17 00:00:00 2001 From: Søren Sandmann Date: Wed, 8 Oct 2008 15:07:16 -0400 Subject: Add tls-test.c --- tests/tls-test.c | 126 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 126 insertions(+) create mode 100644 tests/tls-test.c diff --git a/tests/tls-test.c b/tests/tls-test.c new file mode 100644 index 0000000..8c83edb --- /dev/null +++ b/tests/tls-test.c @@ -0,0 +1,126 @@ +/* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- */ + +/* Lac - Library for asynchronous communication + * Copyright (C) 2002 Søren Sandmann (sandmann@daimi.au.dk) + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Library General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Library General Public License for more details. + * + * You should have received a copy of the GNU Library General Public + * License along with this library; if not, write to the + * Free Software Foundation, Inc., 59 Temple Place - Suite 330, + * Boston, MA 02111-1307, USA. + */ +#include +#include + +GMainLoop *main_loop; +int n_connections = 0; +const int port = 443; + +static void +connection_write (LacConnection *connection, gchar *str) +{ +#if 0 + g_print ("%s", str); +#endif + lac_connection_write (connection, str, strlen (str)); +} + +static void +conn_callback (LacConnection *connection, const LacConnectionEvent *event) +{ + gchar *s; + + g_print ("connection to %s:%d: ", (gchar *)lac_connection_get_data (connection), port); + switch (event->type) + { + case LAC_CONNECTION_EVENT_CONNECT: + g_print ("CONNECT\n"); + connection_write (connection, "GET / HTTP/1.1\r\n"); + connection_write (connection, "Host: "); + connection_write (connection, lac_connection_get_data (connection)); + connection_write (connection, "\r\n\r\n"); + break; + + case LAC_CONNECTION_EVENT_READ: + g_print ("READ (%d bytes)\n", event->read.len); + s = g_new (guchar, event->read.len + 1); + strncpy (s, event->read.data, event->read.len); + s[event->read.len] = '\0'; + g_print ("%s\n", s); + lac_connection_close (connection); + g_free (s); + break; + + case LAC_CONNECTION_EVENT_CLOSE: + g_print ("CLOSE (%s)\n", event->close.remote_closed? + "remote" : "local"); + lac_connection_unref (connection); + g_main_quit (main_loop); + break; + + case LAC_CONNECTION_EVENT_ERROR: + g_print ("ERROR (%s)\n", event->error.err->message); + lac_connection_unref (connection); + break; + + default: + g_print ("event->type == %d?\n", event->type); + g_assert_not_reached (); + break; + } +} + +static void +callback (const LacAddress *addr, gpointer data, const GError *err) +{ + if (err) + { + g_print (" (%p) ", err->message); + g_print ("%s\n", err->message); + } + else + { + LacConnection *connection; + + connection = lac_connection_new_tls (addr, port, conn_callback, data); + } +} + +int +main (int argc, char *argv[]) +{ + int i; + +#if 0 + lac_set_verbose (TRUE); +#endif + + if (argc > 1) + { + for (i = 1; i < argc; ++i) + { + ++n_connections; + lac_address_new_lookup_from_name (argv[i], callback, argv[i]); + } + } + else + { + g_print ("usage %s \n", argv[0]); + return 1; + } + + main_loop = g_main_loop_new (NULL, TRUE); + g_assert (main_loop); + g_main_loop_run (main_loop); + + return 0; +} -- cgit v1.2.3