[glib/tls-database] Add certificate creation tests and refactor things.
- From: Stefan Walter <stefw src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [glib/tls-database] Add certificate creation tests and refactor things.
- Date: Tue, 18 Jan 2011 16:24:02 +0000 (UTC)
commit d311b1ce2a7f9c000101325040ca15b295f891df
Author: Stef Walter <stefw collabora co uk>
Date: Mon Jan 17 21:01:48 2011 -0800
Add certificate creation tests and refactor things.
gio/tests/tls-tests/server.der | Bin 0 -> 554 bytes
gio/tests/tls-tests/server.pem | 14 ++++
gio/tests/tls.c | 166 ++++++++++++++++++++++++++++++----------
3 files changed, 140 insertions(+), 40 deletions(-)
---
diff --git a/gio/tests/tls-tests/server.der b/gio/tests/tls-tests/server.der
new file mode 100644
index 0000000..cf2de65
Binary files /dev/null and b/gio/tests/tls-tests/server.der differ
diff --git a/gio/tests/tls-tests/server.pem b/gio/tests/tls-tests/server.pem
new file mode 100644
index 0000000..d4bd526
--- /dev/null
+++ b/gio/tests/tls-tests/server.pem
@@ -0,0 +1,14 @@
+-----BEGIN CERTIFICATE-----
+MIICJjCCAY+gAwIBAgIBBzANBgkqhkiG9w0BAQUFADCBhjETMBEGCgmSJomT8ixk
+ARkWA0NPTTEXMBUGCgmSJomT8ixkARkWB0VYQU1QTEUxHjAcBgNVBAsTFUNlcnRp
+ZmljYXRlIEF1dGhvcml0eTEXMBUGA1UEAxMOY2EuZXhhbXBsZS5jb20xHTAbBgkq
+hkiG9w0BCQEWDmNhQGV4YW1wbGUuY29tMB4XDTExMDExNzE5NDcxN1oXDTIxMDEx
+NDE5NDcxN1owSzETMBEGCgmSJomT8ixkARkWA0NPTTEXMBUGCgmSJomT8ixkARkW
+B0VYQU1QTEUxGzAZBgNVBAMTEnNlcnZlci5leGFtcGxlLmNvbTBcMA0GCSqGSIb3
+DQEBAQUAA0sAMEgCQQDYScTxk55XBmbDM9zzwO+grVySE4rudWuzH2PpObIonqbf
+hRoAalKVluG9jvbHI81eXxCdSObv1KBP1sbN5RzpAgMBAAGjIjAgMAkGA1UdEwQC
+MAAwEwYDVR0lBAwwCgYIKwYBBQUHAwEwDQYJKoZIhvcNAQEFBQADgYEAYx6fMqT1
+Gvo0jq88E8mc+bmp4LfXD4wJ7KxYeadQxt75HFRpj4FhFO3DOpVRFgzHlOEo3Fwk
+PZOKjvkT0cbcoEq5whLH25dHoQxGoVQgFyAP5s+7Vp5AlHh8Y/vAoXeEVyy/RCIH
+QkhUlAflfDMcrrYjsmwoOPSjhx6Mm/AopX4=
+-----END CERTIFICATE-----
diff --git a/gio/tests/tls.c b/gio/tests/tls.c
index 8f0f945..3f0fbc1 100644
--- a/gio/tests/tls.c
+++ b/gio/tests/tls.c
@@ -23,6 +23,11 @@
#include <gio/gio.h>
#include <sys/types.h>
+#include <string.h>
+
+/* -----------------------------------------------------------------------------
+ * CONNECTION AND DATABASE TESTS
+ */
#define TEST_DATA "You win again, gravity!\n"
#define TEST_DATA_LENGTH 24
@@ -34,7 +39,39 @@ typedef struct {
GIOStream *client_connection;
GSocketConnectable *identity;
GSocketAddress *address;
-} Test;
+} TestConnection;
+
+static void
+setup_connection (TestConnection *test, gconstpointer data)
+{
+ GInetAddress *inet;
+ guint16 port;
+
+ test->loop = g_main_loop_new (NULL, FALSE);
+
+ /* This is where the server listens and the client connects */
+ port = g_random_int_range (50000, 65000);
+ inet = g_inet_address_new_from_string ("127.0.0.1");
+ test->address = G_SOCKET_ADDRESS (g_inet_socket_address_new (inet, port));
+ g_object_unref (inet);
+
+ /* The identity matches the server certificate */
+ test->identity = g_network_address_new ("server.example.com", port);
+}
+
+static void
+teardown_connection (TestConnection *test, gconstpointer data)
+{
+ if (test->service)
+ g_object_unref (test->service);
+ if (test->server_connection)
+ g_object_unref (test->server_connection);
+ if (test->client_connection)
+ g_object_unref (test->client_connection);
+ g_object_unref (test->address);
+ g_object_unref (test->identity);
+ g_main_loop_unref (test->loop);
+}
static void
on_output_close_finish (GObject *object,
@@ -51,7 +88,7 @@ on_output_write_finish (GObject *object,
GAsyncResult *res,
gpointer user_data)
{
- Test *test = user_data;
+ TestConnection *test = user_data;
GError *error = NULL;
g_output_stream_write_finish (G_OUTPUT_STREAM (object), res, &error);
g_assert_no_error (error);
@@ -66,7 +103,7 @@ on_incoming_connection (GSocketService *service,
GObject *source_object,
gpointer user_data)
{
- Test *test = user_data;
+ TestConnection *test = user_data;
GOutputStream *stream;
GTlsCertificate *cert;
GError *error = NULL;
@@ -91,7 +128,7 @@ on_incoming_connection (GSocketService *service,
}
static void
-start_server_service (Test *test)
+start_server_service (TestConnection *test)
{
GError *error = NULL;
@@ -106,7 +143,7 @@ start_server_service (Test *test)
}
static GIOStream*
-start_server_and_connect_to_it (Test *test)
+start_server_and_connect_to_it (TestConnection *test)
{
GSocketClient *client;
GError *error = NULL;
@@ -128,7 +165,7 @@ on_input_read_finish (GObject *object,
GAsyncResult *res,
gpointer user_data)
{
- Test *test = user_data;
+ TestConnection *test = user_data;
GError *error = NULL;
gchar *line, *check;
@@ -147,7 +184,7 @@ on_input_read_finish (GObject *object,
}
static void
-read_test_data_async (Test *test)
+read_test_data_async (TestConnection *test)
{
GDataInputStream *stream;
@@ -160,7 +197,8 @@ read_test_data_async (Test *test)
}
static void
-test_basic_connection (Test *test, gconstpointer data)
+test_basic_connection (TestConnection *test,
+ gconstpointer data)
{
GIOStream *connection;
GError *error = NULL;
@@ -178,7 +216,8 @@ test_basic_connection (Test *test, gconstpointer data)
}
static void
-test_verified_connection (Test *test, gconstpointer data)
+test_verified_connection (TestConnection *test,
+ gconstpointer data)
{
GIOStream *connection;
GTlsDatabase *database;
@@ -207,47 +246,89 @@ test_verified_connection (Test *test, gconstpointer data)
g_main_loop_run (test->loop);
}
+/* -----------------------------------------------------------------------------
+ * CERTIFICATE TESTS
+ */
+
+typedef struct {
+ gchar *pem;
+ gsize pem_length;
+ GByteArray *der;
+} TestCertificate;
+
static void
-setup (Test *test, gconstpointer data)
+setup_certificate (TestCertificate *test, gconstpointer data)
{
- GInetAddress *inet;
- guint16 port;
+ GError *error = NULL;
+ gchar *path;
+ gchar *contents;
+ gsize length;
- test->loop = g_main_loop_new (NULL, FALSE);
+ path = g_build_filename (SRCDIR, "tls-tests", "server.pem", NULL);
+ g_file_get_contents (path, &test->pem, &test->pem_length, &error);
+ g_assert_no_error (error);
+ g_free (path);
- /* This is where the server listens and the client connects */
- port = g_random_int_range (50000, 65000);
- inet = g_inet_address_new_from_string ("127.0.0.1");
- test->address = G_SOCKET_CONNECTABLE (g_inet_socket_address_new (inet, port));
- g_object_unref (inet);
+ path = g_build_filename (SRCDIR, "tls-tests", "server.der", NULL);
+ g_file_get_contents (path, &contents, &length, &error);
+ g_assert_no_error (error);
+ g_free (path);
- /* The identity matches the server certificate */
- test->identity = g_network_address_new ("server.example.com", port);
+ test->der = g_byte_array_new ();
+ g_byte_array_append (test->der, (guint8*)contents, length);
+ g_free (contents);
}
static void
-teardown (Test *test, gconstpointer data)
+teardown_certificate (TestCertificate *test, gconstpointer data)
{
- if (test->service)
- g_object_unref (test->service);
- test->service = NULL;
+ g_free (test->pem);
+ g_byte_array_free (test->der, TRUE);
+}
- if (test->server_connection)
- g_object_unref (test->server_connection);
- test->server_connection = NULL;
+static void
+test_create_destroy_certificate_pem (TestCertificate *test, gconstpointer data)
+{
+ GTlsCertificate *cert;
+ gchar *pem = NULL;
+ GError *error = NULL;
- if (test->client_connection)
- g_object_unref (test->client_connection);
- test->client_connection = NULL;
+ cert = g_tls_certificate_new_from_pem (test->pem, test->pem_length, &error);
+ g_assert_no_error (error);
+ g_assert (G_IS_TLS_CERTIFICATE (cert));
- g_object_unref (test->address);
- test->address = NULL;
+ g_object_get (cert, "certificate-pem", &pem, NULL);
+ g_assert_cmpstr (pem, ==, test->pem);
+ g_free (pem);
- g_object_unref (test->identity);
- test->identity = NULL;
+ g_object_unref (cert);
+ g_assert (!G_IS_TLS_CERTIFICATE (cert));
+}
- g_main_loop_unref (test->loop);
- test->loop = NULL;
+static void
+test_create_destroy_certificate_der (TestCertificate *test, gconstpointer data)
+{
+ GTlsCertificate *cert;
+ GByteArray *der = NULL;
+ GError *error = NULL;
+ GTlsBackend *backend;
+
+ backend = g_tls_backend_get_default ();
+ cert = g_initable_new (g_tls_backend_get_certificate_type (backend),
+ NULL, &error,
+ "certificate", test->der,
+ NULL);
+ g_assert_no_error (error);
+ g_assert (G_IS_TLS_CERTIFICATE (cert));
+
+ g_object_get (cert, "certificate", &der, NULL);
+ g_assert (der);
+ g_assert_cmpuint (der->len, ==, test->der->len);
+ g_assert (memcmp (der->data, test->der->data, der->len) == 0);
+ g_byte_array_unref (der);
+
+ g_object_unref (cert);
+ g_assert (!G_IS_TLS_CERTIFICATE (cert));
}
int
@@ -257,10 +338,15 @@ main (int argc,
g_type_init ();
g_test_init (&argc, &argv, NULL);
- g_test_add ("/tls/basic-connection", Test, NULL,
- setup, test_basic_connection, teardown);
- g_test_add ("/tls/verified-connection", Test, NULL,
- setup, test_verified_connection, teardown);
+ g_test_add ("/tls/connection/basic", TestConnection, NULL,
+ setup_connection, test_basic_connection, teardown_connection);
+ g_test_add ("/tls/connection/verified", TestConnection, NULL,
+ setup_connection, test_verified_connection, teardown_connection);
+
+ g_test_add ("/tls/certificate/create-destroy-pem", TestCertificate, NULL,
+ setup_certificate, test_create_destroy_certificate_pem, teardown_certificate);
+ g_test_add ("/tls/certificate/create-destroy-der", TestCertificate, NULL,
+ setup_certificate, test_create_destroy_certificate_der, teardown_certificate);
return g_test_run();
}
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]