From ab59d4f3a04a2416673099d00c6d117f11288839 Mon Sep 17 00:00:00 2001 From: Gerd v. Egidy Date: Wed, 25 Aug 2004 13:46:57 +0000 Subject: [PATCH] libcnfcheck, libi2ncommon: (gerd) move unit test for ip_range to libi2ncommon (ip_range code is now in libi2ncommon/ipfunc) --- Makefile.am | 7 +- aclocal.m4 | 96 ++++++++++++++- configure.in | 5 +- libi2ncommon.kdevelop | 3 + test/Makefile.am | 7 + test/ip_range.cpp | 341 +++++++++++++++++++++++++++++++++++++++++++++++++ test/test.cpp | 84 ++++++++++++ 7 files changed, 540 insertions(+), 3 deletions(-) create mode 100644 test/Makefile.am create mode 100644 test/ip_range.cpp create mode 100644 test/test.cpp diff --git a/Makefile.am b/Makefile.am index 177cb43..a9dbdef 100644 --- a/Makefile.am +++ b/Makefile.am @@ -2,9 +2,14 @@ # have all needed files, that a GNU package needs AUTOMAKE_OPTIONS = foreign 1.4 -SUBDIRS = src +SUBDIRS = src test # Install the pkg-config file: pkgconfigdir = $(libdir)/pkgconfig pkgconfig_DATA = libi2ncommon.pc +if AUTOCHECK +all: config.h + $(MAKE) $(AM_MAKEFLAGS) all-recursive + $(MAKE) $(AM_MAKEFLAGS) check +endif diff --git a/aclocal.m4 b/aclocal.m4 index cd168bd..478ae5f 100644 --- a/aclocal.m4 +++ b/aclocal.m4 @@ -1,4 +1,4 @@ -dnl aclocal.m4 generated automatically by aclocal 1.4-p5 +dnl ./aclocal.m4 generated automatically by aclocal 1.4-p5 dnl Copyright (C) 1994, 1995-8, 1999, 2001 Free Software Foundation, Inc. dnl This file is free software; the Free Software Foundation @@ -3617,6 +3617,19 @@ AC_DEFUN([AM_PROG_NM], [AC_PROG_NM]) # This is just to silence aclocal about the macro not being used ifelse([AC_DISABLE_FAST_INSTALL]) +# Define a conditional. + +AC_DEFUN([AM_CONDITIONAL], +[AC_SUBST($1_TRUE) +AC_SUBST($1_FALSE) +if $2; then + $1_TRUE= + $1_FALSE='#' +else + $1_TRUE='#' + $1_FALSE= +fi]) + dnl PKG_CHECK_MODULES(GSTUFF, gtk+-2.0 >= 1.3 glib = 1.3.4, action-if, action-not) dnl defines GSTUFF_LIBS, GSTUFF_CFLAGS, see pkg-config man page @@ -3675,3 +3688,84 @@ AC_DEFUN(PKG_CHECK_MODULES, [ +dnl +dnl AM_PATH_CPPUNIT(MINIMUM-VERSION, [ACTION-IF-FOUND [, ACTION-IF-NOT-FOUND]]) +dnl +AC_DEFUN(AM_PATH_CPPUNIT, +[ + +AC_ARG_WITH(cppunit-prefix,[ --with-cppunit-prefix=PFX Prefix where CppUnit is installed (optional)], + cppunit_config_prefix="$withval", cppunit_config_prefix="") +AC_ARG_WITH(cppunit-exec-prefix,[ --with-cppunit-exec-prefix=PFX Exec prefix where CppUnit is installed (optional)], + cppunit_config_exec_prefix="$withval", cppunit_config_exec_prefix="") + + if test x$cppunit_config_exec_prefix != x ; then + cppunit_config_args="$cppunit_config_args --exec-prefix=$cppunit_config_exec_prefix" + if test x${CPPUNIT_CONFIG+set} != xset ; then + CPPUNIT_CONFIG=$cppunit_config_exec_prefix/bin/cppunit-config + fi + fi + if test x$cppunit_config_prefix != x ; then + cppunit_config_args="$cppunit_config_args --prefix=$cppunit_config_prefix" + if test x${CPPUNIT_CONFIG+set} != xset ; then + CPPUNIT_CONFIG=$cppunit_config_prefix/bin/cppunit-config + fi + fi + + AC_PATH_PROG(CPPUNIT_CONFIG, cppunit-config, no) + cppunit_version_min=$1 + + AC_MSG_CHECKING(for Cppunit - version >= $cppunit_version_min) + no_cppunit="" + if test "$CPPUNIT_CONFIG" = "no" ; then + no_cppunit=yes + else + CPPUNIT_CFLAGS=`$CPPUNIT_CONFIG --cflags` + CPPUNIT_LIBS=`$CPPUNIT_CONFIG --libs` + cppunit_version=`$CPPUNIT_CONFIG --version` + + cppunit_major_version=`echo $cppunit_version | \ + sed 's/\([[0-9]]*\).\([[0-9]]*\).\([[0-9]]*\)/\1/'` + cppunit_minor_version=`echo $cppunit_version | \ + sed 's/\([[0-9]]*\).\([[0-9]]*\).\([[0-9]]*\)/\2/'` + cppunit_micro_version=`echo $cppunit_version | \ + sed 's/\([[0-9]]*\).\([[0-9]]*\).\([[0-9]]*\)/\3/'` + + cppunit_major_min=`echo $cppunit_version_min | \ + sed 's/\([[0-9]]*\).\([[0-9]]*\).\([[0-9]]*\)/\1/'` + cppunit_minor_min=`echo $cppunit_version_min | \ + sed 's/\([[0-9]]*\).\([[0-9]]*\).\([[0-9]]*\)/\2/'` + cppunit_micro_min=`echo $cppunit_version_min | \ + sed 's/\([[0-9]]*\).\([[0-9]]*\).\([[0-9]]*\)/\3/'` + + cppunit_version_proper=`expr \ + $cppunit_major_version \> $cppunit_major_min \| \ + $cppunit_major_version \= $cppunit_major_min \& \ + $cppunit_minor_version \> $cppunit_minor_min \| \ + $cppunit_major_version \= $cppunit_major_min \& \ + $cppunit_minor_version \= $cppunit_minor_min \& \ + $cppunit_micro_version \>= $cppunit_micro_min ` + + if test "$cppunit_version_proper" = "1" ; then + AC_MSG_RESULT([$cppunit_major_version.$cppunit_minor_version.$cppunit_micro_version]) + else + AC_MSG_RESULT(no) + no_cppunit=yes + fi + fi + + if test "x$no_cppunit" = x ; then + ifelse([$2], , :, [$2]) + else + CPPUNIT_CFLAGS="" + CPPUNIT_LIBS="" + ifelse([$3], , :, [$3]) + fi + + AC_SUBST(CPPUNIT_CFLAGS) + AC_SUBST(CPPUNIT_LIBS) +]) + + + + diff --git a/configure.in b/configure.in index d9b1c6d..720efb0 100644 --- a/configure.in +++ b/configure.in @@ -7,9 +7,12 @@ AC_LANG_CPLUSPLUS AC_PROG_CXX AM_PROG_LIBTOOL +AM_CONDITIONAL(AUTOCHECK, test "$enable_autocheck" = yes) + PKG_CHECK_MODULES(LIBGETTEXT, libgettext >= 0.0.0) AC_SUBST(LIBGETTEXT_CFLAGS) AC_SUBST(LIBGETTEXT_LIBS) +AM_PATH_CPPUNIT(1.8.0) -AC_OUTPUT(Makefile src/Makefile libi2ncommon.pc) +AC_OUTPUT(Makefile src/Makefile libi2ncommon.pc test/Makefile) diff --git a/libi2ncommon.kdevelop b/libi2ncommon.kdevelop index a7908ae..4f98356 100644 --- a/libi2ncommon.kdevelop +++ b/libi2ncommon.kdevelop @@ -20,6 +20,9 @@ src/libi2ncommon true executable + + + diff --git a/test/Makefile.am b/test/Makefile.am new file mode 100644 index 0000000..e939b4d --- /dev/null +++ b/test/Makefile.am @@ -0,0 +1,7 @@ +INCLUDES = -I$(top_srcdir)/src @CPPUNIT_CFLAGS@ +METASOURCES = AUTO +check_PROGRAMS = test +test_SOURCES = ip_range.cpp test.cpp +test_LDADD = $(top_builddir)/src/libi2ncommon.la @CPPUNIT_LIBS@ + +TESTS = test diff --git a/test/ip_range.cpp b/test/ip_range.cpp new file mode 100644 index 0000000..0b17edc --- /dev/null +++ b/test/ip_range.cpp @@ -0,0 +1,341 @@ +/*************************************************************************** + * Copyright (C) 2004 by Intra2net AG * + * info@intra2net.com * + * * + ***************************************************************************/ + +#include +#include +#include +#include + +#include +#include +#include + +#include + +using namespace std; +using namespace CppUnit; + +class ip_range : public TestFixture +{ + CPPUNIT_TEST_SUITE(ip_range); + + CPPUNIT_TEST(ConstructIP1); + CPPUNIT_TEST(ConstructIP2); + CPPUNIT_TEST(ConstructNet1); + CPPUNIT_TEST(ConstructNet2); + CPPUNIT_TEST(ConstructRange1); + CPPUNIT_TEST(ConstructRange2); + CPPUNIT_TEST(OverlapIP); + CPPUNIT_TEST(OverlapNet1); + CPPUNIT_TEST(OverlapNet2); + CPPUNIT_TEST(OverlapNet3); + CPPUNIT_TEST(OverlapNet4); + CPPUNIT_TEST(OverlapRange1); + CPPUNIT_TEST(OverlapRange2); + CPPUNIT_TEST(OverlapRange3); + CPPUNIT_TEST(OverlapRange4); + CPPUNIT_TEST(OverlapRange5); + CPPUNIT_TEST(OverlapRange6); + CPPUNIT_TEST(OverlapRange7); + CPPUNIT_TEST(WithinNet1); + CPPUNIT_TEST(WithinNet2); + CPPUNIT_TEST(WithinNet3); + CPPUNIT_TEST(WithinNet4); + CPPUNIT_TEST(WithinNet5); + CPPUNIT_TEST(WithinNet6); + CPPUNIT_TEST(WithinNet7); + CPPUNIT_TEST(WithinRange1); + CPPUNIT_TEST(WithinRange2); + CPPUNIT_TEST(WithinRange3); + CPPUNIT_TEST(WithinRange4); + CPPUNIT_TEST(WithinRange5); + CPPUNIT_TEST(WithinRange6); + CPPUNIT_TEST(WithinRange7); + CPPUNIT_TEST(WithinRange8); + CPPUNIT_TEST(WithinRange9); + CPPUNIT_TEST(WithinRange10); + + CPPUNIT_TEST_SUITE_END(); + + public: + void setUp() + { + } + + void tearDown() + { + } + + void ConstructIP1() + { + IP_RANGE i("192.168.1.1"); + CPPUNIT_ASSERT_EQUAL(string("192.168.1.1"),i.to_string()); + } + + void ConstructIP2() + { + IP_RANGE i(ip_type::IP,"192.168.1.1"); + CPPUNIT_ASSERT_EQUAL(string("192.168.1.1"),i.to_string()); + } + + void ConstructNet1() + { + IP_RANGE i("192.168.1.1/255.255.255.0"); + CPPUNIT_ASSERT_EQUAL(string("192.168.1.0/255.255.255.0"),i.to_string()); + } + + void ConstructNet2() + { + IP_RANGE i(ip_type::NETWORK,"192.168.1.1","255.255.255.0"); + CPPUNIT_ASSERT_EQUAL(string("192.168.1.0/255.255.255.0"),i.to_string()); + } + + void ConstructRange1() + { + IP_RANGE i("192.168.1.5-192.168.3.1"); + CPPUNIT_ASSERT_EQUAL(string("192.168.1.5-192.168.3.1"),i.to_string()); + } + + void ConstructRange2() + { + IP_RANGE i(ip_type::RANGE,"192.168.1.5","192.168.3.1"); + CPPUNIT_ASSERT_EQUAL(string("192.168.1.5-192.168.3.1"),i.to_string()); + } + + void OverlapIP() + { + IP_RANGE a("192.168.1.5"); + IP_RANGE b("192.168.1.5"); + + CPPUNIT_ASSERT_EQUAL(true,a.overlapping(b)); + } + + void OverlapNet1() + { + IP_RANGE a("192.168.1.5/255.255.255.0"); + IP_RANGE b("192.168.1.6"); + + CPPUNIT_ASSERT_EQUAL(true,a.overlapping(b)); + } + + void OverlapNet2() + { + IP_RANGE a("192.168.1.5/255.255.255.0"); + IP_RANGE b("192.168.1.44/255.255.255.248"); + + CPPUNIT_ASSERT_EQUAL(true,a.overlapping(b)); + } + + void OverlapNet3() + { + IP_RANGE a("192.168.1.5/255.255.255.0"); + IP_RANGE b("192.168.0.0/255.255.0.0"); + + CPPUNIT_ASSERT_EQUAL(true,a.overlapping(b)); + } + + void OverlapNet4() + { + IP_RANGE a("192.169.1.5/255.255.255.0"); + IP_RANGE b("192.168.0.0/255.255.0.0"); + + CPPUNIT_ASSERT_EQUAL(false,a.overlapping(b)); + } + + void OverlapRange1() + { + IP_RANGE a("192.168.1.5-192.168.1.50"); + IP_RANGE b("192.168.1.6"); + + CPPUNIT_ASSERT_EQUAL(true,a.overlapping(b)); + } + + void OverlapRange2() + { + IP_RANGE a("192.168.1.5-192.168.1.50"); + IP_RANGE b("192.168.1.0/255.255.255.128"); + + CPPUNIT_ASSERT_EQUAL(true,a.overlapping(b)); + } + + void OverlapRange3() + { + IP_RANGE a("192.168.1.5-192.168.1.50"); + IP_RANGE b("192.168.0.0/255.255.0.0"); + + CPPUNIT_ASSERT_EQUAL(true,a.overlapping(b)); + } + + void OverlapRange4() + { + IP_RANGE a("192.168.1.5-192.168.1.50"); + IP_RANGE b("192.168.1.1-192.168.1.5"); + + CPPUNIT_ASSERT_EQUAL(true,a.overlapping(b)); + } + + void OverlapRange5() + { + IP_RANGE a("192.168.1.5-192.168.1.50"); + IP_RANGE b("192.168.1.1-192.168.1.4"); + + CPPUNIT_ASSERT_EQUAL(false,a.overlapping(b)); + } + + void OverlapRange6() + { + IP_RANGE a("192.168.1.5-192.168.1.50"); + IP_RANGE b("192.168.1.1-192.168.2.4"); + + CPPUNIT_ASSERT_EQUAL(true,a.overlapping(b)); + } + + void OverlapRange7() + { + IP_RANGE a("192.168.1.5-192.168.1.50"); + IP_RANGE b("192.168.1.6-192.168.1.7"); + + CPPUNIT_ASSERT_EQUAL(true,a.overlapping(b)); + } + + void WithinNet1() + { + IP_RANGE a("192.168.1.0/255.255.255.0"); + IP_RANGE b("192.168.1.6"); + + CPPUNIT_ASSERT_EQUAL(false,a.is_within(b)); + } + + void WithinNet2() + { + IP_RANGE a("192.168.1.6"); + IP_RANGE b("192.168.1.0/255.255.255.0"); + + CPPUNIT_ASSERT_EQUAL(true,a.is_within(b)); + } + + void WithinNet3() + { + IP_RANGE a("192.168.1.6/255.255.255.248"); + IP_RANGE b("192.168.1.0/255.255.255.0"); + + CPPUNIT_ASSERT_EQUAL(true,a.is_within(b)); + } + + void WithinNet4() + { + IP_RANGE a("192.168.0.0/255.255.0.0"); + IP_RANGE b("192.168.1.0/255.255.255.0"); + + CPPUNIT_ASSERT_EQUAL(false,a.is_within(b)); + } + + void WithinNet5() + { + IP_RANGE a("192.168.1.0/255.255.255.0"); + IP_RANGE b("192.168.1.0/255.255.255.0"); + + CPPUNIT_ASSERT_EQUAL(true,a.is_within(b)); + } + + void WithinNet6() + { + IP_RANGE a("192.168.1.0/255.255.255.0"); + IP_RANGE b("192.168.1.0/255.255.255.128"); + + CPPUNIT_ASSERT_EQUAL(false,a.is_within(b)); + } + + void WithinNet7() + { + IP_RANGE a("192.168.1.0/255.255.255.128"); + IP_RANGE b("192.168.1.0/255.255.255.0"); + + CPPUNIT_ASSERT_EQUAL(true,a.is_within(b)); + } + + void WithinRange1() + { + IP_RANGE a("192.168.1.0/255.255.255.0"); + IP_RANGE b("192.168.1.0-192.168.1.255"); + + CPPUNIT_ASSERT_EQUAL(true,a.is_within(b)); + } + + void WithinRange2() + { + IP_RANGE a("192.168.1.0"); + IP_RANGE b("192.168.1.0-192.168.1.255"); + + CPPUNIT_ASSERT_EQUAL(true,a.is_within(b)); + } + + void WithinRange3() + { + IP_RANGE a("192.168.1.1-192.168.1.1"); + IP_RANGE b("192.168.1.0-192.168.1.255"); + + CPPUNIT_ASSERT_EQUAL(true,a.is_within(b)); + } + + void WithinRange4() + { + IP_RANGE a("192.168.1.0-192.168.1.2"); + IP_RANGE b("192.168.1.1-192.168.1.255"); + + CPPUNIT_ASSERT_EQUAL(false,a.is_within(b)); + } + + void WithinRange5() + { + IP_RANGE a("192.168.1.199-192.168.1.202"); + IP_RANGE b("192.168.1.1-192.168.1.200"); + + CPPUNIT_ASSERT_EQUAL(false,a.is_within(b)); + } + + void WithinRange6() + { + IP_RANGE a("192.168.1.0-192.168.1.202"); + IP_RANGE b("192.168.1.1-192.168.1.200"); + + CPPUNIT_ASSERT_EQUAL(false,a.is_within(b)); + } + + void WithinRange7() + { + IP_RANGE a("192.168.1.0-192.168.1.202"); + IP_RANGE b("192.168.1.0/255.255.255.0"); + + CPPUNIT_ASSERT_EQUAL(true,a.is_within(b)); + } + + void WithinRange8() + { + IP_RANGE a("192.168.0.0-192.168.1.202"); + IP_RANGE b("192.168.1.0/255.255.255.0"); + + CPPUNIT_ASSERT_EQUAL(false,a.is_within(b)); + } + + void WithinRange9() + { + IP_RANGE a("192.168.0.0-192.168.2.202"); + IP_RANGE b("192.168.1.0/255.255.255.0"); + + CPPUNIT_ASSERT_EQUAL(false,a.is_within(b)); + } + + void WithinRange10() + { + IP_RANGE a("192.168.1.10-192.168.2.0"); + IP_RANGE b("192.168.1.0/255.255.255.0"); + + CPPUNIT_ASSERT_EQUAL(false,a.is_within(b)); + } +}; + +CPPUNIT_TEST_SUITE_REGISTRATION(ip_range); diff --git a/test/test.cpp b/test/test.cpp new file mode 100644 index 0000000..4451c48 --- /dev/null +++ b/test/test.cpp @@ -0,0 +1,84 @@ +/*************************************************************************** + * Copyright (C) 2004 by Intra2net AG * + * info@intra2net.com * + * * + ***************************************************************************/ + +#include +#include +#include + +#include +#include + +#include +#include +#include +#include +#include +#include + +class VerboseTimingListener : public CppUnit::TestListener +{ + private: + double start_time; + std::string resultstr; + + double get_time(void) + { + struct timeb tb; + ftime(&tb); + return tb.time+(static_cast(tb.millitm)/1000); + } + + public: + + void startTest( CppUnit::Test *test ) + { + resultstr="OK"; + std::cout << test->getName() << ": "; + start_time=get_time(); + } + + void endTest( CppUnit::Test *test ) + { + double timediff=get_time()-start_time; + + // fix clock unpreciseness for small timespans + if (timediff < 0) timediff=0; + + std::cout << resultstr << " (" + << std::fixed << std::setprecision(3) + << timediff << " sec)" << std::endl; + } + + void addFailure(const CppUnit::TestFailure &failure) + { + if(failure.isError()) + resultstr="ERROR"; + else + resultstr="FAIL"; + } +}; + +int main(int argc, char **argv) +{ + CppUnit::TextTestRunner runner; + CppUnit::TestFactoryRegistry ®istry = CppUnit::TestFactoryRegistry::getRegistry(); + + // set output format that KDevelop can catch errors + CppUnit::CompilerOutputter *op=CppUnit::CompilerOutputter::defaultOutputter(&runner.result(),std::cout); + op->setLocationFormat("%p:%l: error: "); + runner.setOutputter(op); + + // show every test with timing + VerboseTimingListener listener; + runner.eventManager().addListener(&listener); + + runner.addTest(registry.makeTest()); + + // run all tests in registry (not using the default progress listener) + bool wasSucessful = runner.run("",false,true,false); + + return (wasSucessful ? 0 : 1); +} -- 1.7.1