diff options
author | William Jon McCann <mccann@jhu.edu> | 2007-12-09 17:17:51 +0000 |
---|---|---|
committer | William Jon McCann <mccann@src.gnome.org> | 2007-12-09 17:17:51 +0000 |
commit | e8c81d5072a0fd15607dde18dedae5701f3c03b2 (patch) | |
tree | 293d8301c25ed8f5eaa266cbeec7a39a7c580c89 | |
parent | 5337a296f0668e5f1e9e378a171d33029f838414 (diff) | |
download | gdm-e8c81d5072a0fd15607dde18dedae5701f3c03b2.tar.gz |
Add a unit testing framework. Based on a patch from Andrew Ziem
2007-12-09 William Jon McCann <mccann@jhu.edu>
* Makefile.am:
* common/gdm-address.c: (gdm_address_equal),
(gdm_address_is_loopback):
* common/gdm-common.c: (gdm_string_hex_encode),
(gdm_string_hex_decode):
* configure.ac:
* tests/Makefile.am:
* tests/m-common.c: (main):
* tests/s-common-address.c: (setup), (teardown), (START_TEST),
(suite_common_address):
* tests/s-common-address.h:
* tests/s-common-utils.c: (START_TEST), (suite_common_utils):
* tests/s-common-utils.h:
Add a unit testing framework.
Based on a patch from Andrew Ziem <ahz001@gmail.com>
svn path=/trunk/; revision=5545
-rw-r--r-- | ChangeLog | 20 | ||||
-rw-r--r-- | Makefile.am | 1 | ||||
-rw-r--r-- | common/gdm-address.c | 9 | ||||
-rw-r--r-- | common/gdm-common.c | 11 | ||||
-rw-r--r-- | configure.ac | 10 | ||||
-rw-r--r-- | tests/Makefile.am | 38 | ||||
-rw-r--r-- | tests/m-common.c | 74 | ||||
-rw-r--r-- | tests/s-common-address.c | 210 | ||||
-rw-r--r-- | tests/s-common-address.h | 28 | ||||
-rw-r--r-- | tests/s-common-utils.c | 89 | ||||
-rw-r--r-- | tests/s-common-utils.h | 28 |
11 files changed, 514 insertions, 4 deletions
@@ -1,10 +1,28 @@ +2007-12-09 William Jon McCann <mccann@jhu.edu> + + * Makefile.am: + * common/gdm-address.c: (gdm_address_equal), + (gdm_address_is_loopback): + * common/gdm-common.c: (gdm_string_hex_encode), + (gdm_string_hex_decode): + * configure.ac: + * tests/Makefile.am: + * tests/m-common.c: (main): + * tests/s-common-address.c: (setup), (teardown), (START_TEST), + (suite_common_address): + * tests/s-common-address.h: + * tests/s-common-utils.c: (START_TEST), (suite_common_utils): + * tests/s-common-utils.h: + Add a unit testing framework. + Based on a patch from Andrew Ziem <ahz001@gmail.com> + 2007-11-30 Ray Strode <rstrode@redhat.com> Fix some problems in the previous commit, spotted by Mike Oliver <Mike.Oliver@sun.com> * common/gdm-common.[ch] (gdm_generate_random_bytes): - don't leak fd + don't leak fd (_read_bytes): initialize bytes_left_to_read 2007-11-30 Ray Strode <rstrode@redhat.com> diff --git a/Makefile.am b/Makefile.am index 42df0ae8..9290c8fe 100644 --- a/Makefile.am +++ b/Makefile.am @@ -7,6 +7,7 @@ SUBDIRS = \ gui \ utils \ po \ + tests \ $(NULL) # add these when help gets added back diff --git a/common/gdm-address.c b/common/gdm-address.c index ec488b23..75ecad43 100644 --- a/common/gdm-address.c +++ b/common/gdm-address.c @@ -166,8 +166,10 @@ gdm_address_equal (GdmAddress *a, guint8 fam_a; guint8 fam_b; - g_return_val_if_fail (a != NULL || a->ss != NULL, FALSE); - g_return_val_if_fail (b != NULL || b->ss != NULL, FALSE); + g_return_val_if_fail (a != NULL, FALSE); + g_return_val_if_fail (a->ss != NULL, FALSE); + g_return_val_if_fail (b != NULL, FALSE); + g_return_val_if_fail (b->ss != NULL, FALSE); fam_a = a->ss->ss_family; fam_b = b->ss->ss_family; @@ -259,7 +261,8 @@ gdm_address_get_numeric_info (GdmAddress *address, gboolean gdm_address_is_loopback (GdmAddress *address) { - g_return_val_if_fail (address != NULL || address->ss != NULL, FALSE); + g_return_val_if_fail (address != NULL, FALSE); + g_return_val_if_fail (address->ss != NULL, FALSE); switch (address->ss->ss_family){ #ifdef AF_INET6 diff --git a/common/gdm-common.c b/common/gdm-common.c index a43ccff9..2e783470 100644 --- a/common/gdm-common.c +++ b/common/gdm-common.c @@ -119,6 +119,11 @@ gdm_string_hex_encode (const GString *source, const unsigned char *end; gboolean retval; + g_return_val_if_fail (source != NULL, FALSE); + g_return_val_if_fail (dest != NULL, FALSE); + g_return_val_if_fail (source != dest, FALSE); + g_return_val_if_fail (start >= 0, FALSE); + g_return_val_if_fail (dest >= 0, FALSE); g_assert (start <= source->len); result = g_string_new (NULL); @@ -166,6 +171,12 @@ gdm_string_hex_decode (const GString *source, gboolean retval; gboolean high_bits; + g_return_val_if_fail (source != NULL, FALSE); + g_return_val_if_fail (dest != NULL, FALSE); + g_return_val_if_fail (source != dest, FALSE); + g_return_val_if_fail (start >= 0, FALSE); + g_return_val_if_fail (dest >= 0, FALSE); + g_assert (start <= source->len); result = g_string_new (NULL); diff --git a/configure.ac b/configure.ac index cdb21815..14c486ec 100644 --- a/configure.ac +++ b/configure.ac @@ -119,6 +119,15 @@ AC_SUBST(PLUGIN_LIBTOOL_FLAGS) AC_PATH_PROG(GLIB_GENMARSHAL, glib-genmarshal) AC_PATH_XTRA +# Unit testing framework +PKG_CHECK_MODULES(CHECK,[check >= 0.9.4],:,[ + ifdef([AM_PATH_CHECK], + [AM_PATH_CHECK], + [AC_MSG_RESULT([no, testing is disabled])]) + ]) + +AM_CONDITIONAL([HAVE_CHECK],[test "x$CHECK_CFLAGS" != "x"]) + dnl --------------------------------------------------------------------------- dnl - Configuration file stuff dnl --------------------------------------------------------------------------- @@ -1322,6 +1331,7 @@ data/pixmaps/32x32/Makefile data/pixmaps/48x48/Makefile common/Makefile po/Makefile.in +tests/Makefile ]) dnl --------------------------------------------------------------------------- diff --git a/tests/Makefile.am b/tests/Makefile.am new file mode 100644 index 00000000..998c57f4 --- /dev/null +++ b/tests/Makefile.am @@ -0,0 +1,38 @@ +NULL = + +INCLUDES = \ + -I. \ + -I.. \ + -I$(top_srcdir)/common \ + $(COMMON_CFLAGS) \ + $(NULL) + +TESTS = \ + m-common \ + $(NULL) + +if HAVE_CHECK +noinst_PROGRAMS = \ + $(TESTS) \ + $(NULL) +endif + +m_common_SOURCES = \ + m-common.c \ + s-common-address.c \ + s-common-address.h \ + s-common-utils.c \ + s-common-utils.h \ + $(NULL) + +m_common_CFLAGS = \ + @CHECK_CFLAGS@ \ + $(COMMON_CFLAGS) \ + $(NULL) + +m_common_LDADD = \ + @CHECK_LIBS@ \ + $(COMMON_LIBS) \ + $(top_builddir)/common/libgdmcommon.la \ + $(NULL) + diff --git a/tests/m-common.c b/tests/m-common.c new file mode 100644 index 00000000..3339189e --- /dev/null +++ b/tests/m-common.c @@ -0,0 +1,74 @@ +/* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 8 -*- + * + * Copyright (C) 2007 William Jon McCann <mccann@jhu.edu> + * + * This 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. + * + * This 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 this library; if not, write to the + * Free Software Foundation, Inc., 59 Temple Place - Suite 330, + * Boston, MA 02111-1307, USA. + */ + +#include <stdlib.h> +#include <glib/gi18n.h> +#include <glib.h> +#include <glib-object.h> + +#include "s-common-address.h" +#include "s-common-utils.h" + +static gboolean no_fork = FALSE; +static gboolean verbose = FALSE; + +static GOptionEntry entries[] = { + {"no-fork", 0, 0, G_OPTION_ARG_NONE, &no_fork, "Don't fork individual tests", NULL}, + {"verbose", 0, 0, G_OPTION_ARG_NONE, &verbose, "Enable verbose output", NULL}, + {NULL} +}; + +int +main (int argc, char **argv) +{ + GOptionContext *context; + SRunner *r; + int failed; + GError *error; + + failed = 0; + + g_type_init (); + + context = g_option_context_new (""); + g_option_context_add_main_entries (context, entries, NULL); + error = NULL; + g_option_context_parse (context, &argc, &argv, &error); + g_option_context_free (context); + + if (error != NULL) { + g_warning ("%s", error->message); + g_error_free (error); + exit (1); + } + + r = srunner_create (suite_common_utils ()); + srunner_add_suite (r, suite_common_address ()); + + if (no_fork) { + srunner_set_fork_status (r, CK_NOFORK); + } + + srunner_run_all (r, verbose ? CK_VERBOSE : CK_NORMAL); + failed = srunner_ntests_failed (r); + srunner_free (r); + + return failed != 0; +} diff --git a/tests/s-common-address.c b/tests/s-common-address.c new file mode 100644 index 00000000..353bb73d --- /dev/null +++ b/tests/s-common-address.c @@ -0,0 +1,210 @@ +/* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 8 -*- + * + * Copyright (C) 2007 Andrew Ziem <ahz001@gmail.com> + * Copyright (C) 2007 William Jon McCann <mccann@jhu.edu> + * + * This 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. + * + * This 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 this library; if not, write to the + * Free Software Foundation, Inc., 59 Temple Place - Suite 330, + * Boston, MA 02111-1307, USA. + */ + +#include <stdlib.h> +#include <netinet/in.h> +#include <string.h> +#include <stdio.h> +#include <glib.h> +#include <check.h> + +#include "gdm-address.h" +#include "s-common-address.h" + +static GdmAddress *ga; +static GdmAddress *ga192; +static struct sockaddr sa; +static struct sockaddr_in *s_in; + +#ifdef ENABLE_IPV6 +static GdmAddress *ga6; +static struct sockaddr_storage sa6; +static struct sockaddr_in6 *s_in6; +#endif + +static void +setup (void) +{ + s_in = (struct sockaddr_in *) &sa; + s_in->sin_family = AF_INET; + s_in->sin_port = htons (25); + s_in->sin_addr.s_addr = htonl (INADDR_LOOPBACK); + + ga = gdm_address_new_from_sockaddr (&sa, sizeof (sa)); + fail_unless (NULL != ga); + + s_in->sin_addr.s_addr = htonl (0xc0a80001); /* 192.168.0.1 */ + ga192 = gdm_address_new_from_sockaddr (&sa, sizeof (sa)); + fail_unless (NULL != (ga192)); + +#ifdef ENABLE_IPV6 + s_in6 = (struct sockaddr_in6 *) &sa6; + s_in6->sin6_family = AF_INET6; + s_in6->sin6_port = htons (25); + s_in6->sin6_addr = in6addr_loopback; + ga6 = gdm_address_new_from_sockaddr ((struct sockaddr*)&sa6, sizeof(sa6)); + fail_unless (NULL != ga6); +#endif +} + +static void +teardown (void) +{ + gdm_address_free (ga); + gdm_address_free (ga192); +#ifdef ENABLE_IPV6 + gdm_address_free (ga6); +#endif +} + + +/* + * GType gdm_address_get_type (void); + */ +START_TEST (test_gdm_address_get_type) +{ + GType g; + g = gdm_address_get_type (); + /* it did not crash! :) */ + +} +END_TEST + + +START_TEST (test_gdm_address_new_from_sockaddr) +{ + + GdmAddress *_ga; +#ifdef ENABLE_IPV6 + GdmAddress *_ga6; +#endif + + _ga = gdm_address_new_from_sockaddr ((struct sockaddr *) &sa, sizeof (sa)); + fail_unless (NULL != _ga); + gdm_address_free (_ga); + +#ifdef ENABLE_IPV6 + _ga6 = gdm_address_new_from_sockaddr((struct sockaddr *) &sa6, sizeof (sa6)); + fail_unless (NULL != _ga6); + gdm_address_free (_ga6); +#endif + +#ifndef NO_INVALID_INPUT + /* invalid input */ + fail_unless (NULL == gdm_address_new_from_sockaddr ((struct sockaddr *) &sa, 1), NULL ); + fail_unless (NULL == gdm_address_new_from_sockaddr (NULL, 0), NULL); +#endif +} +END_TEST + + +START_TEST (test_gdm_address_get_family_type) +{ + fail_unless (AF_INET == gdm_address_get_family_type (ga), NULL); + +#ifdef ENABLE_IPV6 + fail_unless (AF_INET6 == gdm_address_get_family_type (ga6), NULL); +#endif + +#ifndef NO_INVALID_INPUT + /* invalid input */ + fail_unless (-1 == gdm_address_get_family_type (NULL), NULL); +#endif + +} +END_TEST + + +START_TEST (test_gdm_address_is_loopback) +{ + fail_unless (TRUE == gdm_address_is_loopback (ga)); + fail_unless (FALSE == gdm_address_is_loopback (ga192)); + +#ifdef ENABLE_IPV6 + fail_unless (TRUE == gdm_address_is_loopback (ga6)); + /* FIXME: add more addresses */ +#endif + +#ifndef NO_INVALID_INPUT + /* invalid input */ + fail_unless (FALSE == gdm_address_is_loopback (NULL)); +#endif +} +END_TEST + + +START_TEST (test_gdm_address_equal) +{ + GdmAddress *gdm1; + struct sockaddr sa1; + struct sockaddr_in *sin1; + + /* should be inequal */ + sin1 = (struct sockaddr_in *) &sa1; + sin1->sin_family = AF_INET; + sin1->sin_addr.s_addr = htonl (0xc0a80001); /* 192.168.0.1 */ + gdm1 = gdm_address_new_from_sockaddr (&sa1, sizeof (sa1)); + fail_unless (gdm_address_equal (ga, ga192) == FALSE, NULL); + + /* should be equal */ + fail_unless (TRUE == gdm_address_equal (ga192, gdm1), NULL); + + gdm_address_free (gdm1); + +#ifdef ENABLE_IPV6 + /* should be inequal */ + fail_unless (FALSE == gdm_address_equal (ga6, ga), NULL); + fail_unless (FALSE == gdm_address_equal (ga6, ga192), NULL); + fail_unless (FALSE == gdm_address_equal (ga6, gdm1), NULL); + + /* should be equal */ + /* FIXME: ipv6 version too */ +#endif + +#ifndef NO_INVALID_INPUT + /* invalid input */ + fail_unless (FALSE == gdm_address_equal (NULL, NULL), NULL); + fail_unless (FALSE == gdm_address_equal (ga, NULL), NULL); + fail_unless (FALSE == gdm_address_equal (NULL, ga), NULL); +#endif +} +END_TEST + + +Suite * +suite_common_address (void) +{ + Suite *s; + TCase *tc_core; + + s = suite_create ("gdm-address"); + tc_core = tcase_create ("core"); + + tcase_add_checked_fixture (tc_core, setup, teardown); + tcase_add_test (tc_core, test_gdm_address_get_type); + tcase_add_test (tc_core, test_gdm_address_new_from_sockaddr); + tcase_add_test (tc_core, test_gdm_address_get_family_type); + tcase_add_test (tc_core, test_gdm_address_is_loopback); + tcase_add_test (tc_core, test_gdm_address_equal); + suite_add_tcase (s, tc_core); + + return s; +} diff --git a/tests/s-common-address.h b/tests/s-common-address.h new file mode 100644 index 00000000..b172caba --- /dev/null +++ b/tests/s-common-address.h @@ -0,0 +1,28 @@ +/* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 8 -*- + * + * Copyright (C) 2007 William Jon McCann <mccann@jhu.edu> + * + * This 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. + * + * This 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 this library; if not, write to the + * Free Software Foundation, Inc., 59 Temple Place - Suite 330, + * Boston, MA 02111-1307, USA. + */ + +#ifndef __S_COMMON_ADDRESS_H +#define __S_COMMON_ADDRESS_H + +#include <check.h> + +Suite *suite_common_address (void); + +#endif /* __S_COMMON_ADDRESS_H */ diff --git a/tests/s-common-utils.c b/tests/s-common-utils.c new file mode 100644 index 00000000..0fd32f62 --- /dev/null +++ b/tests/s-common-utils.c @@ -0,0 +1,89 @@ +/* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 8 -*- + * + * Copyright (C) 2007 Andrew Ziem <ahz001@gmail.com> + * Copyright (C) 2007 William Jon McCann <mccann@jhu.edu> + * + * This 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. + * + * This 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 this library; if not, write to the + * Free Software Foundation, Inc., 59 Temple Place - Suite 330, + * Boston, MA 02111-1307, USA. + */ + +#include <stdlib.h> +#include <string.h> +#include <glib.h> + +#include "s-common-utils.h" +#include "gdm-common.h" + +START_TEST (test_gdm_string_hex_encode) +{ + GString *a; + GString *b; + + a = g_string_new ("foo"); + b = g_string_sized_new (100); + fail_unless (TRUE == gdm_string_hex_encode (a, 0, b, 0), NULL); + fail_unless (0 == strncmp (b->str, "666f6f", 7), NULL); + +#ifndef NO_INVALID_INPUT + /* invalid input */ + fail_unless (FALSE == gdm_string_hex_encode (a, -1, b, -1), NULL); + fail_unless (FALSE == gdm_string_hex_encode (NULL, 0, NULL, 0), NULL); + fail_unless (FALSE == gdm_string_hex_encode (a, 0, a, 0), NULL); +#endif + + g_string_free (a, TRUE); + g_string_free (b, TRUE); +} +END_TEST + + +START_TEST (test_gdm_string_hex_decode) + GString *a; + GString *b; + + a = g_string_new ("666f6f"); + b = g_string_sized_new (100); + + fail_unless (TRUE == gdm_string_hex_decode (a, 0, NULL, b, 0), NULL); + + fail_unless (0 == strncmp (b->str, "foo", 7), NULL); + +#ifndef NO_INVALID_INPUT + /* invalid input */ + fail_unless (FALSE == gdm_string_hex_decode (a, -1, NULL, b, -1), NULL); + fail_unless (FALSE == gdm_string_hex_decode (NULL, 0, NULL, NULL, 0), NULL); + fail_unless (FALSE == gdm_string_hex_decode (a, 0, NULL, a, 0), NULL); +#endif + + g_string_free (a, TRUE); + g_string_free (b, TRUE); +END_TEST + +Suite * +suite_common_utils (void) +{ + Suite *s; + TCase *tc_core; + + s = suite_create ("gdm-common"); + tc_core = tcase_create ("core"); + + tcase_add_test (tc_core, test_gdm_string_hex_encode); + tcase_add_test (tc_core, test_gdm_string_hex_decode); + + suite_add_tcase (s, tc_core); + + return s; +} diff --git a/tests/s-common-utils.h b/tests/s-common-utils.h new file mode 100644 index 00000000..fdffb9a0 --- /dev/null +++ b/tests/s-common-utils.h @@ -0,0 +1,28 @@ +/* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 8 -*- + * + * Copyright (C) 2007 William Jon McCann <mccann@jhu.edu> + * + * This 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. + * + * This 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 this library; if not, write to the + * Free Software Foundation, Inc., 59 Temple Place - Suite 330, + * Boston, MA 02111-1307, USA. + */ + +#ifndef __S_COMMON_UTILS_H +#define __S_COMMON_UTILS_H + +#include <check.h> + +Suite *suite_common_utils (void); + +#endif /* __S_COMMON_UTILS_H */ |