Commit 51896081 authored by Stef Walter's avatar Stef Walter Committed by Stef Walter
Browse files

Add --enable-debug and --enable-coverage, egg tests etc

parent 41e7cb38
......@@ -3,6 +3,8 @@
*.la
*.lo
*.pyc
*.gcno
*.gcda
.deps
.cproject
.libs
......@@ -27,6 +29,12 @@ stamp*
.project
.cproject
/coverage
/po/POTFILES
/egg/tests/test-dh
/egg/tests/test-hkdf
/egg/tests/test-secmem
/library/tests/test-session
......@@ -4,3 +4,36 @@ include $(top_srcdir)/Makefile.decl
ACLOCAL_AMFLAGS = -I m4 ${ACLOCAL_FLAGS}
SUBDIRS = po egg library
DISTCHECK_CONFIGURE_FLAGS = \
--enable-debug=yes \
--disable-coverage
dist-hook:
@if test -d "$(srcdir)/.git"; \
then \
echo Creating ChangeLog && \
( cd "$(top_srcdir)" && \
echo '# Generate automatically. Do not edit.'; echo; \
$(top_srcdir)/missing --run git log --stat --date=short ) > ChangeLog.tmp \
&& mv -f ChangeLog.tmp $(top_distdir)/ChangeLog \
|| ( rm -f ChangeLog.tmp ; \
echo Failed to generate ChangeLog >&2 ); \
else \
echo A git clone is required to generate a ChangeLog >&2; \
fi
if WITH_COVERAGE
check-coverage: clear-coverage check coverage
coverage:
mkdir -p coverage
$(LCOV) --directory . --capture --output-file coverage/coverage.info
$(GENHTML) --output-directory coverage/coverage coverage/coverage.info
$(LCOV) --directory . --zerocounters
@echo "file://$(abs_top_builddir)/coverage/index.html"
clear-coverage:
$(LCOV) --directory . --zerocounters
endif
......@@ -65,16 +65,18 @@ if test "$enable_gcrypt" != "no"; then
AC_SUBST([LIBGCRYPT_CFLAGS])
AC_SUBST([LIBGCRYPT_LIBS])
enable_gcrypt="yes"
gcrypt_status=$GCRYPT_VERSION
enable_gcrypt="yes"
else
gcrypt_status="no"
fi
AM_CONDITIONAL(WITH_GCRYPT, test "$enable_gcrypt" = "yes")
dnl *************************************
dnl *** Warnings to show if using GCC ***
dnl *************************************
# --------------------------------------------------------------------
# Compilation options
#
AC_ARG_ENABLE(more-warnings,
AS_HELP_STRING([--disable-more-warnings], [Inhibit compiler warnings]),
......@@ -105,19 +107,90 @@ AC_ARG_ENABLE(strict, [
AS_HELP_STRING([--enable-strict], [Strict code compilation])
])
AC_MSG_CHECKING([build strict])
if test "$enable_strict" = "yes"; then
CFLAGS="$CFLAGS -Werror \
-DGTK_DISABLE_DEPRECATED \
-DGDK_DISABLE_DEPRECATED \
-DG_DISABLE_DEPRECATED"
-DG_DISABLE_DEPRECATED \
-DGDK_PIXBUF_DISABLE_DEPRECATED"
TEST_MODE="thorough"
else
TEST_MODE="quick"
$enable_strict="no"
fi
AC_MSG_RESULT($enable_strict)
AC_SUBST(TEST_MODE)
AC_MSG_CHECKING([for debug mode])
AC_ARG_ENABLE(debug,
AC_HELP_STRING([--enable-debug=no/default/yes],
[Turn on or off debugging])
)
if test "$enable_debug" != "no"; then
AC_DEFINE_UNQUOTED(WITH_DEBUG, 1, [Print debug output])
AC_DEFINE_UNQUOTED(_DEBUG, 1, [In debug mode])
CFLAGS="$CFLAGS -g"
fi
if test "$enable_debug" = "yes"; then
debug_status="yes"
CFLAGS="$CFLAGS -O0"
elif test "$enable_debug" = "no"; then
debug_status="no"
AC_DEFINE_UNQUOTED(G_DISABLE_ASSERT, 1, [Disable glib assertions])
else
debug_status="default"
fi
AC_MSG_RESULT($debug_status)
AC_MSG_CHECKING([whether to build with gcov testing])
AC_ARG_ENABLE([coverage],
AS_HELP_STRING([--enable-coverage],
[Whether to enable coverage testing ]),
[], [enable_coverage=no])
AC_MSG_RESULT([$enable_coverage])
if test "$enable_coverage" = "yes"; then
if test "$GCC" != "yes"; then
AC_MSG_ERROR(Coverage testing requires GCC)
fi
AC_PATH_PROG(GCOV, gcov, no)
if test "$GCOV" = "no" ; then
AC_MSG_ERROR(gcov tool is not available)
fi
AC_PATH_PROG(LCOV, lcov, no)
if test "$LCOV" = "no" ; then
AC_MSG_ERROR(lcov tool is not installed)
fi
AC_PATH_PROG(GENHTML, genhtml, no)
if test "$GENHTML" = "no" ; then
AC_MSG_ERROR(lcov's genhtml tool is not installed)
fi
CFLAGS="$CFLAGS -O0 -g -fprofile-arcs -ftest-coverage"
LDFLAGS="$LDFLAGS -lgcov"
fi
AM_CONDITIONAL([WITH_COVERAGE], [test "$enable_coverage" = "yes"])
AC_SUBST(LCOV)
AC_SUBST(GCOV)
AC_SUBST(GENHTML)
# ------------------------------------------------------------------------------
# Results
#
dnl *****************************
dnl *** done ***
dnl *****************************
AC_CONFIG_FILES([
Makefile
egg/Makefile
egg/tests/Makefile
po/Makefile.in
po/Makefile
library/Makefile
......@@ -125,13 +198,11 @@ AC_CONFIG_FILES([
])
AC_OUTPUT
# ------------------------------------------------------------------------------
# Summary
#
echo
echo "CFLAGS: $CFLAGS"
echo
echo "OPTIONS:"
echo " libgcrypt: $enable_gcrypt"
echo " libgcrypt: $gcrypt_status"
echo " Debug: $debug_status"
echo " Coverage: $enable_coverage"
echo
SUBDIRS = . tests
noinst_LTLIBRARIES = \
libegg.la
EXTRA_DIST = egg-testing.h
INCLUDES = \
-I$(top_srcdir)
......@@ -15,5 +18,6 @@ endif
libegg_la_SOURCES = \
egg-secure-memory.c egg-secure-memory.h \
egg-testing.c egg-testing.h \
$(ENCRYPTION_SRCS) \
$(BUILT_SOURCES)
\ No newline at end of file
/*
* 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_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);
gint egg_tests_run_in_thread_with_loop (void);
#endif /* EGG_DH_H_ */
INCLUDES = \
-I$(top_builddir) \
-I$(top_srcdir) \
-DSRCDIR="\"@abs_srcdir@\"" \
$(GLIB_CFLAGS)
LDADD = \
$(top_builddir)/egg/libegg.la \
$(LIBGCRYPT_LIBS) \
$(GLIB_LIBS)
TEST_PROGS = \
test-secmem
if WITH_GCRYPT
TEST_PROGS += test-hkdf test-dh
endif
check_PROGRAMS = $(TEST_PROGS)
test: $(TEST_PROGS)
gtester --verbose -m $(TEST_MODE) --g-fatal-warnings $(TEST_PROGS)
check-local: test
all-local: $(check_PROGRAMS)
/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */
/* test-dh.c: Test egg-dh.c
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 "config.h"
#include "egg/egg-dh.h"
#include "egg/egg-secure-memory.h"
#include "egg/egg-testing.h"
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <glib.h>
#include <gcrypt.h>
EGG_SECURE_GLIB_DEFINITIONS ();
static void
test_perform (void)
{
gcry_mpi_t p, g;
gcry_mpi_t x1, X1;
gcry_mpi_t x2, X2;
gpointer k1, k2;
gboolean ret;
gsize n1, n2;
/* Load up the parameters */
if (!egg_dh_default_params ("ietf-ike-grp-modp-768", &p, &g))
g_assert_not_reached ();
/* Generate secrets */
ret = egg_dh_gen_pair (p, g, 0, &X1, &x1);
g_assert (ret);
ret = egg_dh_gen_pair (p, g, 0, &X2, &x2);
g_assert (ret);
/* Calculate keys */
k1 = egg_dh_gen_secret (X2, x1, p, &n1);
g_assert (k1);
k2 = egg_dh_gen_secret (X1, x2, p, &n2);
g_assert (k2);
/* Keys must be the same */
egg_assert_cmpsize (n1, ==, n2);
g_assert (memcmp (k1, k2, n1) == 0);
gcry_mpi_release (p);
gcry_mpi_release (g);
gcry_mpi_release (x1);
gcry_mpi_release (X1);
egg_secure_free (k1);
gcry_mpi_release (x2);
gcry_mpi_release (X2);
egg_secure_free (k2);
}
static void
test_short_pair (void)
{
gcry_mpi_t p, g;
gcry_mpi_t x1, X1;
gboolean ret;
/* Load up the parameters */
ret = egg_dh_default_params ("ietf-ike-grp-modp-1024", &p, &g);
g_assert (ret);
g_assert_cmpuint (gcry_mpi_get_nbits (p), ==, 1024);
/* Generate secrets */
ret = egg_dh_gen_pair (p, g, 512, &X1, &x1);
g_assert (ret);
g_assert_cmpuint (gcry_mpi_get_nbits (x1), <=, 512);
gcry_mpi_release (p);
gcry_mpi_release (g);
gcry_mpi_release (x1);
gcry_mpi_release (X1);
}
static void
check_dh_default (const gchar *name, guint bits)
{
gboolean ret;
gcry_mpi_t p, g, check;
gconstpointer prime, base;
gsize n_prime, n_base;
gcry_error_t gcry;
ret = egg_dh_default_params (name, &p, &g);
g_assert (ret);
g_assert_cmpint (gcry_mpi_get_nbits (p), ==, bits);
g_assert_cmpint (gcry_mpi_get_nbits (g), <, gcry_mpi_get_nbits (p));
ret = egg_dh_default_params_raw (name, &prime, &n_prime, &base, &n_base);
g_assert (ret);
g_assert (prime != NULL);
egg_assert_cmpsize (n_prime, >, 0);
g_assert (base != NULL);
egg_assert_cmpsize (n_base, >, 0);
gcry = gcry_mpi_scan (&check, GCRYMPI_FMT_USG, prime, n_prime, NULL);
g_assert (gcry == 0);
g_assert (gcry_mpi_cmp (check, p) == 0);
gcry_mpi_release (check);
gcry = gcry_mpi_scan (&check, GCRYMPI_FMT_USG, base, n_base, NULL);
g_assert (gcry == 0);
g_assert (gcry_mpi_cmp (check, g) == 0);
gcry_mpi_release (check);
gcry_mpi_release (p);
gcry_mpi_release (g);
}
static void
test_default_768 (void)
{
check_dh_default ("ietf-ike-grp-modp-768", 768);
}
static void
test_default_1024 (void)
{
check_dh_default ("ietf-ike-grp-modp-1024", 1024);
}
static void
test_default_1536 (void)
{
check_dh_default ("ietf-ike-grp-modp-1536", 1536);
}
static void
test_default_2048 (void)
{
check_dh_default ("ietf-ike-grp-modp-2048", 2048);
}
static void
test_default_3072 (void)
{
check_dh_default ("ietf-ike-grp-modp-3072", 3072);
}
static void
test_default_4096 (void)
{
check_dh_default ("ietf-ike-grp-modp-4096", 4096);
}
static void
test_default_8192 (void)
{
check_dh_default ("ietf-ike-grp-modp-8192", 8192);
}
static void
test_default_bad (void)
{
gboolean ret;
gcry_mpi_t p, g;
ret = egg_dh_default_params ("bad-name", &p, &g);
g_assert (!ret);
}
int
main (int argc, char **argv)