[glib-networking] tls: fix installed tests differently
- From: Dan Winship <danw src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [glib-networking] tls: fix installed tests differently
- Date: Wed, 9 Oct 2013 14:05:34 +0000 (UTC)
commit 2e6d9d20f12ba48f71a3974ab3b54ebc7966e1fa
Author: Dan Winship <danw gnome org>
Date: Wed Oct 9 10:02:34 2013 -0400
tls: fix installed tests differently
The previous patch worked for "make check" and for running the
installed tests, but failed when running tests by hand, because
G_TEST_SRCDIR wouldn't be set, so it would just use "./", and then
GFileDatabase would complain that you had passed it a relative path.
Re-fix by adding a helper method to force-absolutify the paths, if
needed. (Also, intern the path names to avoid needed all the g_free()s
from the previous patch.)
tls/tests/certificate.c | 98 +++++++++++++++++++-----------------------
tls/tests/connection.c | 52 ++++++++++++----------
tls/tests/file-database.c | 105 ++++++++++++++++++--------------------------
3 files changed, 116 insertions(+), 139 deletions(-)
---
diff --git a/tls/tests/certificate.c b/tls/tests/certificate.c
index d1818bd..3775af9 100644
--- a/tls/tests/certificate.c
+++ b/tls/tests/certificate.c
@@ -24,6 +24,29 @@
#include <sys/types.h>
#include <string.h>
+static const gchar *
+tls_test_file_path (const char *name)
+{
+ const gchar *const_path;
+ gchar *path;
+
+ path = g_test_build_filename (G_TEST_DIST, "files", name, NULL);
+ if (!g_path_is_absolute (path))
+ {
+ gchar *cwd, *abs;
+
+ cwd = g_get_current_dir ();
+ abs = g_build_filename (cwd, path, NULL);
+ g_free (cwd);
+ g_free (path);
+ path = abs;
+ }
+
+ const_path = g_intern_string (path);
+ g_free (path);
+ return const_path;
+}
+
typedef struct {
GTlsBackend *backend;
GType cert_gtype;
@@ -39,34 +62,30 @@ static void
setup_certificate (TestCertificate *test, gconstpointer data)
{
GError *error = NULL;
- gchar *file, *contents;
+ gchar *contents;
gsize length;
test->backend = g_tls_backend_get_default ();
test->cert_gtype = g_tls_backend_get_certificate_type (test->backend);
- file = g_test_build_filename (G_TEST_DIST, "files/server.pem", NULL);
- g_file_get_contents (file, &test->cert_pem, &test->cert_pem_length, &error);
- g_free (file);
+ g_file_get_contents (tls_test_file_path ("server.pem"), &test->cert_pem,
+ &test->cert_pem_length, &error);
g_assert_no_error (error);
- file = g_test_build_filename (G_TEST_DIST, "files/server.der", NULL);
- g_file_get_contents (file, &contents, &length, &error);
- g_free (file);
+ g_file_get_contents (tls_test_file_path ("server.der"),
+ &contents, &length, &error);
g_assert_no_error (error);
test->cert_der = g_byte_array_new ();
g_byte_array_append (test->cert_der, (guint8 *)contents, length);
g_free (contents);
- file = g_test_build_filename (G_TEST_DIST, "files/server-key.pem", NULL);
- g_file_get_contents (file, &test->key_pem, &test->key_pem_length, &error);
- g_free (file);
+ g_file_get_contents (tls_test_file_path ("server-key.pem"), &test->key_pem,
+ &test->key_pem_length, &error);
g_assert_no_error (error);
- file = g_test_build_filename (G_TEST_DIST, "files/server-key.der", NULL);
- g_file_get_contents (file, &contents, &length, &error);
- g_free (file);
+ g_file_get_contents (tls_test_file_path ("server-key.der"),
+ &contents, &length, &error);
g_assert_no_error (error);
test->key_der = g_byte_array_new ();
@@ -176,11 +195,8 @@ test_create_certificate_with_issuer (TestCertificate *test,
{
GTlsCertificate *cert, *issuer, *check;
GError *error = NULL;
- gchar *file;
- file = g_test_build_filename (G_TEST_DIST, "files/ca.pem", NULL);
- issuer = g_tls_certificate_new_from_file (file, &error);
- g_free (file);
+ issuer = g_tls_certificate_new_from_file (tls_test_file_path ("ca.pem"), &error);
g_assert_no_error (error);
g_assert (G_IS_TLS_CERTIFICATE (issuer));
@@ -220,24 +236,19 @@ setup_verify (TestVerify *test,
gconstpointer data)
{
GError *error = NULL;
- gchar *file;
- file = g_test_build_filename (G_TEST_DIST, "files/server.pem", NULL);
- test->cert = g_tls_certificate_new_from_file (file, &error);
- g_free (file);
+ test->cert = g_tls_certificate_new_from_file (tls_test_file_path ("server.pem"), &error);
g_assert_no_error (error);
g_assert (G_IS_TLS_CERTIFICATE (test->cert));
test->identity = g_network_address_new ("server.example.com", 80);
- file = g_test_build_filename (G_TEST_DIST, "files/ca.pem", NULL);
- test->anchor = g_tls_certificate_new_from_file (file, &error);
+ test->anchor = g_tls_certificate_new_from_file (tls_test_file_path ("ca.pem"), &error);
g_assert_no_error (error);
g_assert (G_IS_TLS_CERTIFICATE (test->anchor));
- test->database = g_tls_file_database_new (file, &error);
+ test->database = g_tls_file_database_new (tls_test_file_path ("ca.pem"), &error);
g_assert_no_error (error);
g_assert (G_IS_TLS_DATABASE (test->database));
- g_free (file);
}
static void
@@ -303,12 +314,9 @@ test_verify_certificate_bad_ca (TestVerify *test,
GTlsCertificateFlags errors;
GTlsCertificate *cert;
GError *error = NULL;
- gchar *file;
/* Use a client certificate as the CA, which is wrong */
- file = g_test_build_filename (G_TEST_DIST, "files/client.pem", NULL);
- cert = g_tls_certificate_new_from_file (file, &error);
- g_free (file);
+ cert = g_tls_certificate_new_from_file (tls_test_file_path ("client.pem"), &error);
g_assert_no_error (error);
g_assert (G_IS_TLS_CERTIFICATE (cert));
@@ -325,12 +333,9 @@ test_verify_certificate_bad_before (TestVerify *test,
GTlsCertificateFlags errors;
GTlsCertificate *cert;
GError *error = NULL;
- gchar *file;
/* This is a certificate in the future */
- file = g_test_build_filename (G_TEST_DIST, "files/client-future.pem", NULL);
- cert = g_tls_certificate_new_from_file (file, &error);
- g_free (file);
+ cert = g_tls_certificate_new_from_file (tls_test_file_path ("client-future.pem"), &error);
g_assert_no_error (error);
g_assert (G_IS_TLS_CERTIFICATE (cert));
@@ -347,12 +352,9 @@ test_verify_certificate_bad_expired (TestVerify *test,
GTlsCertificateFlags errors;
GTlsCertificate *cert;
GError *error = NULL;
- gchar *file;
/* This is a certificate in the future */
- file = g_test_build_filename (G_TEST_DIST, "files/client-past.pem", NULL);
- cert = g_tls_certificate_new_from_file (file, &error);
- g_free (file);
+ cert = g_tls_certificate_new_from_file (tls_test_file_path ("client-past.pem"), &error);
g_assert_no_error (error);
g_assert (G_IS_TLS_CERTIFICATE (cert));
@@ -371,18 +373,13 @@ test_verify_certificate_bad_combo (TestVerify *test,
GSocketConnectable *identity;
GTlsCertificateFlags errors;
GError *error = NULL;
- gchar *file;
- file = g_test_build_filename (G_TEST_DIST, "files/client-past.pem", NULL);
- cert = g_tls_certificate_new_from_file (file, &error);
- g_free (file);
+ cert = g_tls_certificate_new_from_file (tls_test_file_path ("client-past.pem"), &error);
g_assert_no_error (error);
g_assert (G_IS_TLS_CERTIFICATE (cert));
/* Unrelated cert used as certificate authority */
- file = g_test_build_filename (G_TEST_DIST, "files/server-self.pem", NULL);
- cacert = g_tls_certificate_new_from_file (file, &error);
- g_free (file);
+ cacert = g_tls_certificate_new_from_file (tls_test_file_path ("server-self.pem"), &error);
g_assert_no_error (error);
g_assert (G_IS_TLS_CERTIFICATE (cacert));
@@ -410,21 +407,14 @@ test_certificate_is_same (void)
GTlsCertificate *two;
GTlsCertificate *three;
GError *error = NULL;
- gchar *file;
- file = g_test_build_filename (G_TEST_DIST, "files/client.pem", NULL);
- one = g_tls_certificate_new_from_file (file, &error);
- g_free (file);
+ one = g_tls_certificate_new_from_file (tls_test_file_path ("client.pem"), &error);
g_assert_no_error (error);
- file = g_test_build_filename (G_TEST_DIST, "files/client-and-key.pem", NULL);
- two = g_tls_certificate_new_from_file (file, &error);
- g_free (file);
+ two = g_tls_certificate_new_from_file (tls_test_file_path ("client-and-key.pem"), &error);
g_assert_no_error (error);
- file = g_test_build_filename (G_TEST_DIST, "files/server.pem", NULL);
- three = g_tls_certificate_new_from_file (file, &error);
- g_free (file);
+ three = g_tls_certificate_new_from_file (tls_test_file_path ("server.pem"), &error);
g_assert_no_error (error);
g_assert (g_tls_certificate_is_same (one, two) == TRUE);
diff --git a/tls/tests/connection.c b/tls/tests/connection.c
index 8a03519..3c57c38 100644
--- a/tls/tests/connection.c
+++ b/tls/tests/connection.c
@@ -24,6 +24,29 @@
#include <sys/types.h>
#include <string.h>
+static const gchar *
+tls_test_file_path (const char *name)
+{
+ const gchar *const_path;
+ gchar *path;
+
+ path = g_test_build_filename (G_TEST_DIST, "files", name, NULL);
+ if (!g_path_is_absolute (path))
+ {
+ gchar *cwd, *abs;
+
+ cwd = g_get_current_dir ();
+ abs = g_build_filename (cwd, path, NULL);
+ g_free (cwd);
+ g_free (path);
+ path = abs;
+ }
+
+ const_path = g_intern_string (path);
+ g_free (path);
+ return const_path;
+}
+
#define TEST_DATA "You win again, gravity!\n"
#define TEST_DATA_LENGTH 24
@@ -202,11 +225,8 @@ on_incoming_connection (GSocketService *service,
GOutputStream *stream;
GTlsCertificate *cert;
GError *error = NULL;
- gchar *file;
- file = g_test_build_filename (G_TEST_DIST, "files/server-and-key.pem", NULL);
- cert = g_tls_certificate_new_from_file (file, &error);
- g_free (file);
+ cert = g_tls_certificate_new_from_file (tls_test_file_path ("server-and-key.pem"), &error);
g_assert_no_error (error);
test->server_connection = g_tls_server_connection_new (G_IO_STREAM (connection),
@@ -278,11 +298,8 @@ run_echo_server (GThreadedSocketService *service,
GOutputStream *ostream;
gssize nread, nwrote, total;
gchar buf[128];
- gchar *file;
- file = g_test_build_filename (G_TEST_DIST, "files/server-and-key.pem", NULL);
- cert = g_tls_certificate_new_from_file (file, &error);
- g_free (file);
+ cert = g_tls_certificate_new_from_file (tls_test_file_path ("server-and-key.pem"), &error);
g_assert_no_error (error);
test->server_connection = g_tls_server_connection_new (G_IO_STREAM (connection),
@@ -439,11 +456,8 @@ test_verified_connection (TestConnection *test,
{
GIOStream *connection;
GError *error = NULL;
- gchar *file;
- file = g_test_build_filename (G_TEST_DIST, "files/ca-roots.pem", NULL);
- test->database = g_tls_file_database_new (file, &error);
- g_free (file);
+ test->database = g_tls_file_database_new (tls_test_file_path ("ca-roots.pem"), &error);
g_assert_no_error (error);
g_assert (test->database);
@@ -485,11 +499,8 @@ test_client_auth_connection (TestConnection *test,
GTlsCertificate *cert;
GTlsCertificate *peer;
gboolean cas_changed;
- gchar *file;
- file = g_test_build_filename (G_TEST_DIST, "files/ca-roots.pem", NULL);
- test->database = g_tls_file_database_new (file, &error);
- g_free (file);
+ test->database = g_tls_file_database_new (tls_test_file_path ("ca-roots.pem"), &error);
g_assert_no_error (error);
g_assert (test->database);
@@ -501,9 +512,7 @@ test_client_auth_connection (TestConnection *test,
g_tls_connection_set_database (G_TLS_CONNECTION (test->client_connection), test->database);
- file = g_test_build_filename (G_TEST_DIST, "files/client-and-key.pem", NULL);
- cert = g_tls_certificate_new_from_file (file, &error);
- g_free (file);
+ cert = g_tls_certificate_new_from_file (tls_test_file_path ("client-and-key.pem"), &error);
g_assert_no_error (error);
g_tls_connection_set_certificate (G_TLS_CONNECTION (test->client_connection), cert);
@@ -545,11 +554,8 @@ test_client_auth_failure (TestConnection *test,
GIOStream *connection;
GError *error = NULL;
gboolean accepted_changed;
- gchar *file;
- file = g_test_build_filename (G_TEST_DIST, "files/ca-roots.pem", NULL);
- test->database = g_tls_file_database_new (file, &error);
- g_free (file);
+ test->database = g_tls_file_database_new (tls_test_file_path ("ca-roots.pem"), &error);
g_assert_no_error (error);
g_assert (test->database);
diff --git a/tls/tests/file-database.c b/tls/tests/file-database.c
index 0f239b3..1425b9e 100644
--- a/tls/tests/file-database.c
+++ b/tls/tests/file-database.c
@@ -28,6 +28,29 @@
#include <sys/types.h>
#include <string.h>
+static const gchar *
+tls_test_file_path (const char *name)
+{
+ const gchar *const_path;
+ gchar *path;
+
+ path = g_test_build_filename (G_TEST_DIST, "files", name, NULL);
+ if (!g_path_is_absolute (path))
+ {
+ gchar *cwd, *abs;
+
+ cwd = g_get_current_dir ();
+ abs = g_build_filename (cwd, path, NULL);
+ g_free (cwd);
+ g_free (path);
+ path = abs;
+ }
+
+ const_path = g_intern_string (path);
+ g_free (path);
+ return const_path;
+}
+
/* -----------------------------------------------------------------------------
* CERTIFICATE VERIFY
*/
@@ -43,19 +66,14 @@ setup_verify (TestVerify *test,
gconstpointer data)
{
GError *error = NULL;
- gchar *file;
- file = g_test_build_filename (G_TEST_DIST, "files/server.pem", NULL);
- test->cert = g_tls_certificate_new_from_file (file, &error);
- g_free (file);
+ test->cert = g_tls_certificate_new_from_file (tls_test_file_path ("server.pem"), &error);
g_assert_no_error (error);
g_assert (G_IS_TLS_CERTIFICATE (test->cert));
test->identity = g_network_address_new ("server.example.com", 80);
- file = g_test_build_filename (G_TEST_DIST, "files/ca.pem", NULL);
- test->database = g_tls_file_database_new (file, &error);
- g_free (file);
+ test->database = g_tls_file_database_new (tls_test_file_path ("ca.pem"), &error);
g_assert_no_error (error);
g_assert (G_IS_TLS_DATABASE (test->database));
}
@@ -127,12 +145,9 @@ test_verify_database_bad_ca (TestVerify *test,
GTlsCertificateFlags errors;
GTlsCertificate *cert;
GError *error = NULL;
- gchar *file;
/* Use another certificate which isn't in our CA list */
- file = g_test_build_filename (G_TEST_DIST, "files/server-self.pem", NULL);
- cert = g_tls_certificate_new_from_file (file, &error);
- g_free (file);
+ cert = g_tls_certificate_new_from_file (tls_test_file_path ("server-self.pem"), &error);
g_assert_no_error (error);
g_assert (G_IS_TLS_CERTIFICATE (cert));
@@ -152,12 +167,9 @@ test_verify_database_bad_before (TestVerify *test,
GTlsCertificateFlags errors;
GTlsCertificate *cert;
GError *error = NULL;
- gchar *file;
/* This is a certificate in the future */
- file = g_test_build_filename (G_TEST_DIST, "files/client-future.pem", NULL);
- cert = g_tls_certificate_new_from_file (file, &error);
- g_free (file);
+ cert = g_tls_certificate_new_from_file (tls_test_file_path ("client-future.pem"), &error);
g_assert_no_error (error);
g_assert (G_IS_TLS_CERTIFICATE (cert));
@@ -177,12 +189,9 @@ test_verify_database_bad_expired (TestVerify *test,
GTlsCertificateFlags errors;
GTlsCertificate *cert;
GError *error = NULL;
- gchar *file;
/* This is a certificate in the future */
- file = g_test_build_filename (G_TEST_DIST, "files/client-past.pem", NULL);
- cert = g_tls_certificate_new_from_file (file, &error);
- g_free (file);
+ cert = g_tls_certificate_new_from_file (tls_test_file_path ("client-past.pem"), &error);
g_assert_no_error (error);
g_assert (G_IS_TLS_CERTIFICATE (cert));
@@ -203,11 +212,8 @@ test_verify_database_bad_combo (TestVerify *test,
GSocketConnectable *identity;
GTlsCertificateFlags errors;
GError *error = NULL;
- gchar *file;
- file = g_test_build_filename (G_TEST_DIST, "files/server-self.pem", NULL);
- cert = g_tls_certificate_new_from_file (file, &error);
- g_free (file);
+ cert = g_tls_certificate_new_from_file (tls_test_file_path ("server-self.pem"), &error);
g_assert_no_error (error);
g_assert (G_IS_TLS_CERTIFICATE (cert));
@@ -282,21 +288,18 @@ test_verify_with_incorrect_root_in_chain (void)
GTlsCertificate *chain;
GSocketConnectable *identity;
GTlsCertificateFlags errors;
- gchar *file;
/*
* This database contains a single anchor certificate of:
* C = US, O = "VeriSign, Inc.", OU = Class 3 Public Primary Certification Authority
*/
- file = g_test_build_filename (G_TEST_DIST, "files/ca-verisign-sha1.pem", NULL);
- database = g_tls_file_database_new (file, &error);
+ database = g_tls_file_database_new (tls_test_file_path ("ca-verisign-sha1.pem"), &error);
g_assert_no_error (error);
g_assert (G_IS_TLS_DATABASE (database));
- ca_verisign_sha1 = g_tls_certificate_new_from_file (file, &error);
+ ca_verisign_sha1 = g_tls_certificate_new_from_file (tls_test_file_path ("ca-verisign-sha1.pem"), &error);
g_assert_no_error (error);
g_assert (G_IS_TLS_CERTIFICATE (ca_verisign_sha1));
- g_free (file);
/*
* This certificate chain contains a root certificate with that same issuer, public key:
@@ -306,9 +309,7 @@ test_verify_with_incorrect_root_in_chain (void)
* verify this chain as valid, since the issuer fields and signatures should chain up
* to the certificate in our database.
*/
- file = g_test_build_filename (G_TEST_DIST, "files/chain-with-verisign-md2.pem", NULL);
- chain = load_certificate_chain (file, &error);
- g_free (file);
+ chain = load_certificate_chain (tls_test_file_path ("chain-with-verisign-md2.pem"), &error);
g_assert_no_error (error);
g_assert (G_IS_TLS_CERTIFICATE (chain));
@@ -339,7 +340,7 @@ test_verify_with_incorrect_root_in_chain (void)
typedef struct {
GTlsDatabase *database;
- gchar *path;
+ const gchar *path;
} TestFileDatabase;
static void
@@ -348,7 +349,7 @@ setup_file_database (TestFileDatabase *test,
{
GError *error = NULL;
- test->path = g_test_build_filename (G_TEST_DIST, "files/ca-roots.pem", NULL);
+ test->path = tls_test_file_path ("ca-roots.pem");
test->database = g_tls_file_database_new (test->path, &error);
g_assert_no_error (error);
g_assert (G_IS_TLS_DATABASE (test->database));
@@ -358,7 +359,6 @@ static void
teardown_file_database (TestFileDatabase *test,
gconstpointer data)
{
- g_free (test->path);
g_assert (G_IS_TLS_DATABASE (test->database));
g_object_add_weak_pointer (G_OBJECT (test->database),
(gpointer *)&test->database);
@@ -374,7 +374,6 @@ test_file_database_handle (TestFileDatabase *test,
GTlsCertificate *check;
GError *error = NULL;
gchar *handle;
- gchar *file;
/*
* ca.pem is in the ca-roots.pem that the test->database represents.
@@ -382,9 +381,7 @@ test_file_database_handle (TestFileDatabase *test,
* is 'in' the database.
*/
- file = g_test_build_filename (G_TEST_DIST, "files/ca.pem", NULL);
- certificate = g_tls_certificate_new_from_file (file, &error);
- g_free (file);
+ certificate = g_tls_certificate_new_from_file (tls_test_file_path ("ca.pem"), &error);
g_assert_no_error (error);
g_assert (G_IS_TLS_CERTIFICATE (certificate));
@@ -427,18 +424,15 @@ test_anchors_property (void)
GTlsDatabase *database;
gchar *anchor_filename = NULL;
GError *error = NULL;
- gchar *file;
- file = g_test_build_filename (G_TEST_DIST, "files/ca.pem", NULL);
- database = g_tls_file_database_new (file, &error);
+ database = g_tls_file_database_new (tls_test_file_path ("ca.pem"), &error);
g_assert_no_error (error);
g_object_get (database, "anchors", &anchor_filename, NULL);
- g_assert_cmpstr (anchor_filename, ==, file);
+ g_assert_cmpstr (anchor_filename, ==, tls_test_file_path ("ca.pem"));
g_free (anchor_filename);
g_object_unref (database);
- g_free (file);
}
static gboolean
@@ -483,11 +477,8 @@ test_lookup_certificates_issued_by (void)
GByteArray *issuer_dn;
GTlsDatabase *database;
GError *error = NULL;
- gchar *file;
- file = g_test_build_filename (G_TEST_DIST, "files/non-ca.pem", NULL);
- database = g_tls_file_database_new (file, &error);
- g_free (file);
+ database = g_tls_file_database_new (tls_test_file_path ("non-ca.pem"), &error);
g_assert_no_error (error);
issuer_dn = g_byte_array_new ();
@@ -502,21 +493,11 @@ test_lookup_certificates_issued_by (void)
g_assert_cmpuint (g_list_length (certificates), ==, 4);
- file = g_test_build_filename (G_TEST_DIST, "files/client.pem", NULL);
- g_assert (certificate_is_in_list (certificates, file));
- g_free (file);
- file = g_test_build_filename (G_TEST_DIST, "files/client-future.pem", NULL);
- g_assert (certificate_is_in_list (certificates, file));
- g_free (file);
- file = g_test_build_filename (G_TEST_DIST, "files/client-past.pem", NULL);
- g_assert (certificate_is_in_list (certificates, file));
- g_free (file);
- file = g_test_build_filename (G_TEST_DIST, "files/server.pem", NULL);
- g_assert (certificate_is_in_list (certificates, file));
- g_free (file);
- file = g_test_build_filename (G_TEST_DIST, "files/server-self.pem", NULL);
- g_assert (!certificate_is_in_list (certificates, file));
- g_free (file);
+ g_assert (certificate_is_in_list (certificates, tls_test_file_path ("client.pem")));
+ g_assert (certificate_is_in_list (certificates, tls_test_file_path ("client-future.pem")));
+ g_assert (certificate_is_in_list (certificates, tls_test_file_path ("client-past.pem")));
+ g_assert (certificate_is_in_list (certificates, tls_test_file_path ("server.pem")));
+ g_assert (!certificate_is_in_list (certificates, tls_test_file_path ("server-self.pem")));
g_list_free_full (certificates, g_object_unref);
g_object_unref (database);
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]