[glibmm] Tests: Add a basic test for the Tls* API.
- From: José Alburquerque <jaalburqu src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [glibmm] Tests: Add a basic test for the Tls* API.
- Date: Mon, 8 Apr 2013 04:32:19 +0000 (UTC)
commit e9d09efcad4b92ac0fd497eeacf96d988c720319
Author: José Alburquerque <jaalburqu svn gnome org>
Date: Sun Apr 7 17:13:28 2013 -0400
Tests: Add a basic test for the Tls* API.
* tests/giomm_tls_client/main.cc: The test basically works though it
would be good to test more thoroughly the TlsDatabase API.
ChangeLog | 7 ++
tests/Makefile.am | 4 +
tests/giomm_tls_client/main.cc | 144 ++++++++++++++++++++++++++++++++++++++++
3 files changed, 155 insertions(+), 0 deletions(-)
---
diff --git a/ChangeLog b/ChangeLog
index c0d14e6..446bba3 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,12 @@
2013-04-07 José Alburquerque <jaalburquerque gmail com>
+ Tests: Add a basic test for the Tls* API.
+
+ * tests/giomm_tls_client/main.cc: The test basically works though it
+ would be good to test more thoroughly the TlsDatabase API.
+
+2013-04-07 José Alburquerque <jaalburquerque gmail com>
+
Gio::TlsClientConnection: Make the class work correctly.
* tools/m4/class_interface.m4 (_CUSTOM_CTOR_CAST): Add a new macro so
diff --git a/tests/Makefile.am b/tests/Makefile.am
index bfee2a5..615f00a 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -22,6 +22,7 @@ check_PROGRAMS = \
giomm_ioerror_and_iodbuserror/test \
giomm_simple/test \
giomm_asyncresult_sourceobject/test \
+ giomm_tls_client/test \
glibmm_btree/test \
glibmm_date/test \
glibmm_buildfilename/test \
@@ -67,6 +68,9 @@ giomm_simple_test_LDADD = $(giomm_ldadd)
giomm_asyncresult_sourceobject_test_SOURCES = giomm_asyncresult_sourceobject/main.cc
giomm_asyncresult_sourceobject_test_LDADD = $(giomm_ldadd)
+giomm_tls_client_test_SOURCES = giomm_tls_client/main.cc
+giomm_tls_client_test_LDADD = $(giomm_ldadd)
+
glibmm_btree_test_SOURCES = glibmm_btree/main.cc
glibmm_buildfilename_test_SOURCES = glibmm_buildfilename/main.cc
glibmm_date_test_SOURCES = glibmm_date/main.cc
diff --git a/tests/giomm_tls_client/main.cc b/tests/giomm_tls_client/main.cc
new file mode 100644
index 0000000..dc69a4a
--- /dev/null
+++ b/tests/giomm_tls_client/main.cc
@@ -0,0 +1,144 @@
+#include <giomm.h>
+#include <iostream>
+#include <cstdlib>
+
+bool on_accept_certificate(const Glib::RefPtr<const Gio::TlsCertificate>& cert, Gio::TlsCertificateFlags)
+{
+ std::cout << "Handshake is ocurring." << std::endl
+ << "The server is requesting that its certificate be accepted." <<
+ std::endl;
+
+ std::cout << "Outputing certificate data:" << std::endl <<
+ cert->property_certificate_pem().get_value();
+
+ Glib::RefPtr<const Gio::TlsCertificate> issuer = cert->get_issuer();
+
+ std::cout << "Outputing the issuer's certificate data:" << std::endl <<
+ issuer->property_certificate_pem().get_value();
+
+ std::cout << "Accepting the certificate." << std::endl;
+ return true;
+}
+
+int main(int, char**)
+{
+ Gio::init();
+
+ const Glib::ustring test_host = "www.google.com";
+
+ std::vector< Glib::RefPtr<Gio::InetAddress> > inet_addresses =
+ Gio::Resolver::get_default()->lookup_by_name(test_host);
+
+ if(inet_addresses.size() == 0)
+ {
+ std::cout << "Could not resolve test host '" << test_host << "'." <<
+ std::endl;
+ return EXIT_FAILURE;
+ }
+
+ std::cout << "Successfully resolved address of test host '" << test_host <<
+ "'." << std::endl;
+
+ Glib::RefPtr<Gio::InetAddress> first_inet_address = inet_addresses[0];
+
+ std::cout << "First address of test host is " <<
+ first_inet_address->to_string() << "." << std::endl;
+
+ Glib::RefPtr<Gio::Socket> socket =
+ Gio::Socket::create(Gio::SOCKET_FAMILY_IPV4, Gio::SOCKET_TYPE_STREAM,
+ Gio::SOCKET_PROTOCOL_TCP);
+
+ Glib::RefPtr<Gio::InetSocketAddress> address =
+ Gio::InetSocketAddress::create(first_inet_address, 443);
+
+ socket->connect(address);
+
+ if(!socket->is_connected())
+ {
+ std::cout << "Could not connect socket to " <<
+ address->get_address()->to_string() << ":" << address->get_port() <<
+ "." << std::endl;
+ }
+
+ Glib::RefPtr<Gio::TcpConnection> conn =
Glib::RefPtr<Gio::TcpConnection>::cast_dynamic(Gio::SocketConnection::create(socket));
+
+ if(!conn || !conn->is_connected())
+ {
+ std::cout << "Could not establish connection to " <<
+ address->get_address()->to_string() << ":" << address->get_port() <<
+ "." << std::endl;
+ socket->close();
+ return EXIT_FAILURE;
+ }
+
+ std::cout << "Successfully established connection to " <<
+ address->get_address()->to_string() << ":" << address->get_port() <<
+ "." << std::endl;
+
+ Glib::RefPtr<Gio::TlsClientConnection> tls_connection;
+
+ try
+ {
+ Glib::RefPtr<Gio::TlsClientConnection> tls_connection =
+ Gio::TlsClientConnection::create(conn, address);
+
+ tls_connection->signal_accept_certificate().connect(
+ sigc::ptr_fun(&on_accept_certificate));
+
+ tls_connection->handshake();
+
+ Glib::RefPtr<Gio::TlsCertificate> certificate =
+ tls_connection->get_peer_certificate();
+
+ if(!certificate)
+ {
+ std::cout << "Could not get the peer's certificate." << std::endl;
+ }
+
+ std::cout << "Successfully got the peer's certificate." << std::endl;
+ std::cout << "Getting the certificate's issuer." << std::endl;
+
+ Glib::RefPtr<Gio::TlsCertificate> issuer = certificate->get_issuer();
+
+ if(!issuer)
+ {
+ std::cout << "Could not get the peer's certificate." << std::endl;
+ }
+
+ std::cout << "Successfully got the peer's certificate issuer." << std::endl;
+
+ std::cout << "Attempting to use the connection's database." << std::endl;
+
+ Glib::RefPtr<Gio::TlsDatabase> database = tls_connection->get_database();
+
+ Glib::RefPtr<const Gio::SocketConnectable> connectable = address;
+
+ database->verify_chain(certificate, G_TLS_DATABASE_PURPOSE_AUTHENTICATE_SERVER, connectable);
+
+ database->verify_chain(certificate, G_TLS_DATABASE_PURPOSE_AUTHENTICATE_SERVER, Glib::RefPtr<const
Gio::SocketConnectable>::cast_static(address));
+
+ std::cout << "Looking up the main certificate's issuer in the "
+ "database." << std::endl;
+
+ Glib::RefPtr<Gio::TlsCertificate> db_certificate = database->lookup_certificate_issuer(certificate);
+
+ if(!db_certificate)
+ {
+ std::cout << "No certificate found in the database." << std::endl;
+ }
+ else
+ {
+ std::cout << "Successfully found the issuer's certificate in the "
+ "database." << std::endl;
+ }
+ }
+ catch (const Gio::TlsError& error)
+ {
+ std::cout << "Exception caught: " << error.what() << "." << std::endl;
+ return EXIT_FAILURE;
+ }
+
+ conn->close();
+
+ return EXIT_SUCCESS;
+}
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]