Commit adb9d8de authored by Stef Walter's avatar Stef Walter

Use more modern gtest style for egg tests.

parent e7e9103b
......@@ -79,3 +79,15 @@ run-tests
# Old directories
/gp11
/docs/reference/gp11
/egg/tests/test-asn1
/egg/tests/test-cleanup
/egg/tests/test-dh
/egg/tests/test-dn
/egg/tests/test-hex
/egg/tests/test-oid
/egg/tests/test-openssl
/egg/tests/test-padding
/egg/tests/test-secmem
/egg/tests/test-spawn
/egg/tests/test-symkey
......@@ -39,6 +39,7 @@ libegg_la_SOURCES = \
egg-secure-memory.c egg-secure-memory.h \
egg-spawn.c egg-spawn.h \
egg-symkey.c egg-symkey.h \
egg-testing.c egg-testing.h \
egg-asn1-defs.h \
$(BUILT_SOURCES)
......
/*
* gnome-keyring
*
* Copyright (C) 2011 Collabora Ltd.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU Lesser General License as
* published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
*
* This program 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
* Lesser General License for more details.
*
* You should have received a copy of the GNU Lesser General
* License along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
* 02111-1307, USA.
*
* Author: Stef Walter <stefw@collabora.co.uk>
*/
#ifndef EGG_HKDF_H_
#define EGG_HKDF_H_
#include <glib.h>
gboolean egg_hkdf_perform (const gchar *hash_algo,
gconstpointer input,
gsize n_input,
gconstpointer salt,
gsize n_salt,
gconstpointer info,
gsize n_info,
gpointer output,
gsize n_output);
#endif /* EGG_HKDF_H_ */
/*
* gnome-keyring
*
* Copyright (C) 2011 Collabora Ltd.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU Lesser General License as
* published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
*
* This program 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
* Lesser General License for more details.
*
* You should have received a copy of the GNU Lesser General
* License along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
* 02111-1307, USA.
*
* Stef Walter <stefw@collabora.co.uk>
*/
#include "config.h"
#include "egg-testing.h"
#include <errno.h>
#include <unistd.h>
static GCond *wait_condition = NULL;
static GCond *wait_start = NULL;
static GMutex *wait_mutex = NULL;
static gboolean wait_waiting = FALSE;
static const char HEXC[] = "0123456789ABCDEF";
static gchar*
hex_dump (const guchar *data, gsize n_data)
{
GString *result;
gsize i;
guchar j;
g_assert (data);
result = g_string_sized_new (n_data * 2 + 1);
for (i = 0; i < n_data; ++i) {
g_string_append (result, "\\x");
j = data[i] >> 4 & 0xf;
g_string_append_c (result, HEXC[j]);
j = data[i] & 0xf;
g_string_append_c (result, HEXC[j]);
}
return g_string_free (result, FALSE);
}
void
egg_assertion_message_cmpmem (const char *domain,
const char *file,
int line,
const char *func,
const char *expr,
gconstpointer arg1,
gsize n_arg1,
const char *cmp,
gconstpointer arg2,
gsize n_arg2)
{
char *a1, *a2, *s;
a1 = arg1 ? hex_dump (arg1, n_arg1) : g_strdup ("NULL");
a2 = arg2 ? hex_dump (arg2, n_arg2) : g_strdup ("NULL");
s = g_strdup_printf ("assertion failed (%s): (%s %s %s)", expr, a1, cmp, a2);
g_free (a1);
g_free (a2);
g_assertion_message (domain, file, line, func, s);
g_free (s);
}
void
egg_tests_chdir_base (gchar* argv0)
{
gchar *dir, *base;
dir = g_path_get_dirname (argv0);
if (chdir (dir) < 0)
g_warning ("couldn't change directory to: %s: %s",
dir, g_strerror (errno));
base = g_path_get_basename (dir);
if (strcmp (base, ".libs") == 0) {
if (chdir ("..") < 0)
g_warning ("couldn't change directory to ..: %s",
g_strerror (errno));
}
g_free (base);
g_free (dir);
}
void
egg_test_wait_stop (void)
{
GTimeVal tv;
g_get_current_time (&tv);
g_time_val_add (&tv, 1000);
g_assert (wait_mutex);
g_assert (wait_condition);
g_mutex_lock (wait_mutex);
if (!wait_waiting)
g_cond_timed_wait (wait_start, wait_mutex, &tv);
g_assert (wait_waiting);
g_cond_broadcast (wait_condition);
g_mutex_unlock (wait_mutex);
}
gboolean
egg_test_wait_until (int timeout)
{
GTimeVal tv;
gboolean ret;
g_get_current_time (&tv);
g_time_val_add (&tv, timeout * 1000);
g_assert (wait_mutex);
g_assert (wait_condition);
g_mutex_lock (wait_mutex);
g_assert (!wait_waiting);
wait_waiting = TRUE;
g_cond_broadcast (wait_start);
ret = g_cond_timed_wait (wait_condition, wait_mutex, &tv);
g_assert (wait_waiting);
wait_waiting = FALSE;
g_mutex_unlock (wait_mutex);
return ret;
}
static gpointer
testing_thread (gpointer loop)
{
/* Must have been defined by the test including this file */
gint ret = g_test_run ();
g_main_loop_quit (loop);
return GINT_TO_POINTER (ret);
}
gint
egg_tests_run_in_thread_with_loop (void)
{
GThread *thread;
GMainLoop *loop;
gpointer ret;
g_thread_init (NULL);
loop = g_main_loop_new (NULL, FALSE);
wait_condition = g_cond_new ();
wait_start = g_cond_new ();
wait_mutex = g_mutex_new ();
thread = g_thread_create (testing_thread, loop, TRUE, NULL);
g_assert (thread);
g_main_loop_run (loop);
ret = g_thread_join (thread);
g_main_loop_unref (loop);
g_cond_free (wait_condition);
g_mutex_free (wait_mutex);
return GPOINTER_TO_INT (ret);
}
/*
* gnome-keyring
*
* Copyright (C) 2011 Stefan Walter
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU Lesser General License as
* published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
*
* This program 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
* Lesser General License for more details.
*
* You should have received a copy of the GNU Lesser General
* License along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
* 02111-1307, USA.
*
* Author: Stef Walter <stefw@collabora.co.uk>
*/
#ifndef EGG_TESTING_H_
#define EGG_TESTING_H_
#include <glib.h>
#include <string.h>
#define egg_assert_cmpsize(a, o, b) \
g_assert_cmpuint ((guint)(a), o, (guint)(b))
#define egg_assert_cmpmem(a, na, cmp, b, nb) \
do { gconstpointer __p1 = (a), __p2 = (b); gsize __n1 = (na), __n2 = (nb); \
if (__n1 cmp __n2 && memcmp (__p1, __p2, __n1) cmp 0) ; else \
egg_assertion_message_cmpmem (G_LOG_DOMAIN, __FILE__, __LINE__, \
G_STRFUNC, #a "[" #na"] " #cmp " " #b "[" #nb "]", \
__p1, __n1, #cmp, __p2, __n2); } while (0)
void egg_assertion_message_cmpmem (const char *domain, const char *file,
int line, const char *func,
const char *expr, gconstpointer arg1,
gsize n_arg1, const char *cmp,
gconstpointer arg2, gsize n_arg2);
void egg_test_wait_stop (void);
gboolean egg_test_wait_until (int timeout);
void egg_tests_chdir_base (gchar* argv0);
gint egg_tests_run_in_thread_with_loop (void);
#endif /* EGG_DH_H_ */
asn1-def-test.c: test.asn
$(ASN1PARSER) -o asn1-def-test.c $(srcdir)/test.asn
TESTING_SOURCES = \
asn1-def-test.c
# Test files should be listed in order they need to run
TESTING_FILES = \
INCLUDES = \
-I$(top_srcdir)/egg \
-DSRCDIR=$(srcdir) \
$(GLIB_CFLAGS)
LDADD = \
$(top_builddir)/egg/libegg.la \
$(LIBGCRYPT_LIBS) \
$(GTHREAD_LIBS) \
$(GLIB_LIBS)
TEST_PROGS = \
test-asn1 \
test-dn \
test-cleanup \
test-hex \
test-oid \
test-secmem \
test-padding \
test-symkey \
test-openssl \
test-dh \
test-spawn
test_asn1_SOURCES = \
test-asn1.c \
unit-test-asn1.c \
test-dn.c \
unit-test-cleanup.c \
unit-test-hex.c \
unit-test-oid.c \
test-padding.c \
unit-test-secmem.c \
unit-test-symkey.c \
unit-test-openssl.c \
unit-test-dh.c \
unit-test-spawn.c
UNIT_PROMPT =
TESTING_LIBS = \
$(top_builddir)/egg/libegg.la
include $(top_srcdir)/testing/testing.make
BUILT_SOURCES += \
asn1-def-test.c
EXTRA_DIST += \
check_PROGRAMS = $(TEST_PROGS)
test: $(TEST_PROGS)
SRCDIR='$(srcdir)' gtester -k --verbose ${TEST_PROGS}
check-local: test
if WITH_TESTS
all-local: $(check_PROGRAMS)
endif
EXTRA_DIST = \
test.asn \
test-data
files
# ------------------------------------------------------------------------------
noinst_PROGRAMS += \
noinst_PROGRAMS = \
test-asn1x
test_asn1x_SOURCES = \
......@@ -43,4 +55,5 @@ test_asn1x_SOURCES = \
test_asn1x_LDADD = \
$(top_builddir)/egg/libegg-asn1x.la \
$(LIBTASN1_LIBS)
$(LIBTASN1_LIBS) \
$(LDADD)
This diff is collapsed.
/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */
/* test-asn1.c: Test ASN1 stuf
#include "egg/egg-asn1x.h"
#include "testing/testing.h"
Copyright (C) 2009 Stefan Walter
The Gnome Keyring 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.
The Gnome Keyring 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 the Gnome Library; see the file COPYING.LIB. If not,
write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
Boston, MA 02111-1307, USA.
Author: Stef Walter <stef@memberwebs.com>
*/
#include "egg-asn1x.h"
#include <pwd.h>
#include <stdlib.h>
#include <unistd.h>
#include "egg/egg-asn1-defs.h"
#include "egg-asn1-defs.h"
#include "egg-testing.h"
#if 0
static void
......@@ -47,7 +69,8 @@ test_some_asn1_stuff (const ASN1_ARRAY_TYPE *defs, const gchar *file, const gcha
gpointer data, encoded;
gsize n_data, n_encoded;
data = testing_data_read (file, &n_data);
if (!g_file_get_contents (file, (gchar**)&data, &n_data, NULL))
g_assert_not_reached ();
asn = egg_asn1x_create (defs, identifier);
egg_asn1x_dump (asn);
......@@ -68,25 +91,23 @@ test_some_asn1_stuff (const ASN1_ARRAY_TYPE *defs, const gchar *file, const gcha
g_free (data);
}
static int
run (void)
int
main (int argc, char **argv)
{
/* Build up a personal name, which is a set */
#if 0
build_personal_name ();
#endif
egg_tests_chdir_base (argv[0]);
test_some_asn1_stuff (pkix_asn1_tab, "test-certificate-1.der", "Certificate");
test_some_asn1_stuff (pkix_asn1_tab, "test-pkcs8-1.der", "pkcs-8-PrivateKeyInfo");
test_some_asn1_stuff (pk_asn1_tab, "test-rsakey-1.der", "RSAPrivateKey");
test_some_asn1_stuff (pkix_asn1_tab, "test-personalname-1.der", "PersonalName");
test_some_asn1_stuff (pkix_asn1_tab, "test-pkcs7-1.der", "pkcs-7-ContentInfo");
test_some_asn1_stuff (pkix_asn1_tab, "test-pkcs7-2.der", "pkcs-7-ContentInfo");
test_some_asn1_stuff (pkix_asn1_tab, "test-pkcs12-1.der", "pkcs-12-PFX");
test_some_asn1_stuff (pkix_asn1_tab, "files/test-certificate-1.der", "Certificate");
test_some_asn1_stuff (pkix_asn1_tab, "files/test-pkcs8-1.der", "pkcs-8-PrivateKeyInfo");
test_some_asn1_stuff (pk_asn1_tab, "files/test-rsakey-1.der", "RSAPrivateKey");
test_some_asn1_stuff (pkix_asn1_tab, "files/test-personalname-1.der", "PersonalName");
test_some_asn1_stuff (pkix_asn1_tab, "files/test-pkcs7-1.der", "pkcs-7-ContentInfo");
test_some_asn1_stuff (pkix_asn1_tab, "files/test-pkcs7-2.der", "pkcs-7-ContentInfo");
test_some_asn1_stuff (pkix_asn1_tab, "files/test-pkcs12-1.der", "pkcs-12-PFX");
return 0;
}
#include "testing/testing.c"
......@@ -25,9 +25,7 @@
#include <stdio.h>
#include <string.h>
#include "test-suite.h"
#include "egg/egg-cleanup.h"
#include "egg-cleanup.h"
#define DATA "some string"
......@@ -35,29 +33,30 @@ typedef struct _CleanupParam {
gpointer value;
} CleanupParam;
static void
static void
cleanup_callback (gpointer user_data)
{
{
CleanupParam *param = (CleanupParam*)user_data;
g_assert (param->value && strcmp(param->value, DATA) == 0);
param->value = NULL;
}
TESTING_TEST(cleanup)
static void
test_cleanup (void)
{
CleanupParam param;
param.value = DATA;
egg_cleanup_register (cleanup_callback, &param);
egg_cleanup_perform ();
g_assert (param.value == NULL);
}
/* -----------------------------------------------------------------------------
* Cleanup handlers are called in the opposite order as installed
* Cleanup handlers are called in the opposite order as installed
*/
static gint order_value = 0;
......@@ -66,9 +65,9 @@ typedef struct _OrderParam {
gint reference;
} OrderParam;
static void
static void
order_callback (gpointer user_data)
{
{
OrderParam *param = (OrderParam*)user_data;
/* cleanup handler called out of order */
g_assert_cmpint (order_value, ==, param->reference);
......@@ -76,24 +75,25 @@ order_callback (gpointer user_data)
--order_value;
}
TESTING_TEST(order)
static void
test_order (void)
{
OrderParam param[8];
int i;
for (i = 0; i < 8; ++i) {
param[i].reference = i;
param[i].reference = i;
egg_cleanup_register (order_callback, &param[i]);
}
order_value = i - 1;
egg_cleanup_perform ();
for (i = 0; i < 8; ++i)
/* "cleanup handler not called" */
g_assert (param[i].reference == -1);
/* "not all cleanup handlers called" */
g_assert_cmpint (order_value, ==, -1);
}
......@@ -101,7 +101,7 @@ TESTING_TEST(order)
/* -----------------------------------------------------------------------------
* A cleanup handler might cause another to be registered.
*/
static gboolean cleaned_up = FALSE;
static void
......@@ -109,45 +109,60 @@ second_callback (gpointer user_data)
{
cleaned_up = TRUE;
}
static void
reregister_callback (gpointer user_data)
{
egg_cleanup_register (second_callback, NULL);
}
}
TESTING_TEST(reregister)
static void
test_reregister (void)
{
cleaned_up = FALSE;
egg_cleanup_register (reregister_callback, NULL);
egg_cleanup_perform ();
/* "second cleanup handler not called" */
g_assert (cleaned_up == TRUE);
}
/* -----------------------------------------------------------------------------
* Cleanup handlers can be removed
* Cleanup handlers can be removed
*/
static gboolean test_cleaned_up = FALSE;
static void
static void
remove_callback (gpointer user_data)
{
test_cleaned_up = TRUE;
test_cleaned_up = TRUE;
}
TESTING_TEST(remove)
static void
test_remove (void)
{
egg_cleanup_register (remove_callback, NULL);
egg_cleanup_register (remove_callback, DATA);
egg_cleanup_unregister (remove_callback, DATA);
egg_cleanup_unregister (remove_callback, NULL);
egg_cleanup_perform ();
/* "removed callback was called" */
g_assert (test_cleaned_up == FALSE);
}
int
main (int argc, char **argv)
{
g_test_init (&argc, &argv, NULL);
g_test_add_func ("/cleanup/cleanup", test_cleanup);
g_test_add_func ("/cleanup/order", test_order);
g_test_add_func ("/cleanup/reregister", test_reregister);
g_test_add_func ("/cleanup/remove", test_remove);
return g_test_run ();
}
......@@ -21,18 +21,22 @@
Author: Stef Walter <stef@memberwebs.com>
*/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "test-suite.h"
#include "config.h"
#include "egg-dh.h"
#include "egg-secure-memory.h"
#include "egg-testing.h"
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <gcrypt.h>
TESTING_TEST(dh_perform)
EGG_SECURE_GLIB_DEFINITIONS ();
static void
test_perform (void)
{
gcry_mpi_t p, g;
gcry_mpi_t x1, X1;
......@@ -69,7 +73,8 @@ TESTING_TEST(dh_perform)
egg_secure_free (k2);
}
TESTING_TEST(dh_short_pair)
static void
test_short_pair (void)
{
gcry_mpi_t p, g;
gcry_mpi_t x1, X1;
......@@ -108,9 +113,9 @@ test_dh_default (const gchar *name, guint bits)
ret = egg_dh_default_params_raw (name, &prime, &n_prime, &base, &n_base);
g_assert (ret);
g_assert (prime != NULL);
g_assert_cmpsize (n_prime, >, 0);
egg_assert_cmpsize (n_prime, >, 0);
g_assert (base != NULL);
g_assert_cmpsize (n_base, >, 0);
egg_assert_cmpsize (n_base, >, 0);
gcry = gcry_mpi_scan (&check, GCRYMPI_FMT_USG, prime, n_prime, NULL);
g_assert (gcry == 0);
......@@ -126,43 +131,51 @@ test_dh_default (const gchar *name, guint bits)
gcry_mpi_release (g);
}
TESTING_TEST(dh_default_768)
static void
test_default_768 (void)
{
test_dh_default ("ietf-ike-grp-modp-768", 768);
}
TESTING_TEST(dh_default_1024)
static void
test_default_1024 (void)
{
test_dh_default ("ietf-ike-grp-modp-1024", 1024);
}
TESTING_TEST(dh_default_1536)
static void
test_default_1536 (void)
{
test_dh_default ("ietf-ike-grp-modp-1536", 1536);
}
TESTING_TEST(dh_default_2048)
static void
test_default_2048 (void)
{
test_dh_default ("ietf-ike-grp-modp-2048", 2048);
}
TESTING_TEST(dh_default_3072)
static void
test_default_3072 (void)
{
test_dh_default ("ietf-ike-grp-modp-3072", 3072);
}
TESTING_TEST(dh_default_4096)
static void
test_default_4096 (void)
{
test_dh_default ("ietf-ike-grp-modp-4096", 4096);
}
TESTING_TEST(dh_default_8192)
static void