From e8c81d5072a0fd15607dde18dedae5701f3c03b2 Mon Sep 17 00:00:00 2001 From: William Jon McCann Date: Sun, 9 Dec 2007 17:17:51 +0000 Subject: Add a unit testing framework. Based on a patch from Andrew Ziem 2007-12-09 William Jon McCann * 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 svn path=/trunk/; revision=5545 --- tests/Makefile.am | 38 +++++++++ tests/m-common.c | 74 +++++++++++++++++ tests/s-common-address.c | 210 +++++++++++++++++++++++++++++++++++++++++++++++ tests/s-common-address.h | 28 +++++++ tests/s-common-utils.c | 89 ++++++++++++++++++++ tests/s-common-utils.h | 28 +++++++ 6 files changed, 467 insertions(+) create mode 100644 tests/Makefile.am create mode 100644 tests/m-common.c create mode 100644 tests/s-common-address.c create mode 100644 tests/s-common-address.h create mode 100644 tests/s-common-utils.c create mode 100644 tests/s-common-utils.h (limited to 'tests') 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 + * + * 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 +#include +#include +#include + +#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 + * Copyright (C) 2007 William Jon McCann + * + * 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 +#include +#include +#include +#include +#include + +#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 + * + * 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 + +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 + * Copyright (C) 2007 William Jon McCann + * + * 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 +#include +#include + +#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 + * + * 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 + +Suite *suite_common_utils (void); + +#endif /* __S_COMMON_UTILS_H */ -- cgit v1.2.1