- Add testcase for CheckSum
authorDuncan Mac-Vicar P <dmacvicar@suse.de>
Fri, 30 Jun 2006 13:40:59 +0000 (13:40 +0000)
committerDuncan Mac-Vicar P <dmacvicar@suse.de>
Fri, 30 Jun 2006 13:40:59 +0000 (13:40 +0000)
- start using Boost::Test for complex testcases

testsuite/src/utils/TestUtils.cc
testsuite/src/utils/TestUtils.h
testsuite/utils/Makefile.am [deleted file]
testsuite/utils/TestUtils.cc [deleted file]
testsuite/utils/TestUtils.h [deleted file]
testsuite/zypp/tests/CheckSum.cc [new file with mode: 0644]
testsuite/zypp/tests/Makefile.am
testsuite/zypp/tests/PublicKey.cc
zypp/CheckSum.cc
zypp/CheckSum.h

index 35cc6c4..882606e 100644 (file)
@@ -1,5 +1,6 @@
 
 #include <iostream>
+#include "zypp/base/Exception.h"
 #include "zypp/Dependencies.h"
 #include "zypp/Capability.h"
 #include "zypp/ResObject.h"
@@ -15,6 +16,14 @@ namespace zypp
   {
     namespace utils
     {
+      void assert_equal( const std::string &got, const std::string &expected)
+      {
+        if (got != expected)
+        {
+          ZYPP_THROW(Exception("Expected '" + expected + "', got '" + got + "'"));
+        }
+      }
+
       static void dumpCapSet( const CapSet &caps, const std::string &deptype)
       {
         cout << "   " << "<" << deptype << ">" << std::endl;
index cf291bb..53fa9e3 100644 (file)
@@ -12,6 +12,7 @@ namespace zypp
   {
     namespace utils
     {
+      void assert_equal( const std::string &got, const std::string &expected);
       void dump( const ResStore &store, bool descr = false, bool deps = false );  
       void dump( const std::list<ResObject::Ptr> &list, bool descr = false, bool deps = false );  
     }  
diff --git a/testsuite/utils/Makefile.am b/testsuite/utils/Makefile.am
deleted file mode 100644 (file)
index 9e2f46f..0000000
+++ /dev/null
@@ -1,32 +0,0 @@
-## Process this file with automake to produce Makefile.in
-## ##################################################
-
-SUBDIRS =
-
-INCLUDES = -DZYPP_BASE_LOGGER_LOGGROUP=\"testsuite_utils\"
-
-## ##################################################
-
-##targetincludedir = $(pkgincludedir)/target
-
-noinst_HEADERS =                       \
-       TestUtils.h
-
-## ##################################################
-
-noinst_LTLIBRARIES =   lib@PACKAGE@_testsuite_utils.la
-
-## ##################################################
-
-lib@PACKAGE@_testsuite_utils_la_SOURCES =      \
-       TestUtils.cc
-       
-lib@PACKAGE@_testsuite_utils_la_LDFLAGS = @LIBZYPP_VERSION_INFO@
-
-##lib@PACKAGE@_target_la_LIBADD = \
-##     rpm/lib@PACKAGE@_target_rpm.la  \
-##     hal/lib@PACKAGE@_target_hal.la \
-##     modalias/lib@PACKAGE@_target_modalias.la \
-##     store/lib@PACKAGE@_target_store.la
-
-## ##################################################
diff --git a/testsuite/utils/TestUtils.cc b/testsuite/utils/TestUtils.cc
deleted file mode 100644 (file)
index 596d0b5..0000000
+++ /dev/null
@@ -1,105 +0,0 @@
-
-#include <iostream>
-#include "zypp/Dependencies.h"
-#include "zypp/Capability.h"
-#include "zypp/ResObject.h"
-#include "zypp/Patch.h"
-#include "zypp/ResStore.h"
-#include "testsuite/utils/TestUtils.h"
-
-using namespace std;
-
-namespace zypp
-{
-  namespace testsuite
-  {
-    namespace utils
-    {
-      static void dumpCapSet( const CapSet &caps, const std::string &deptype)
-      {
-        cout << "   " << "<" << deptype << ">" << std::endl;
-        CapSet::iterator it = caps.begin();
-        for ( ; it != caps.end(); ++it)
-        {
-          cout << "    <capability kind=\"" << (*it).kind() << "\" refers=\"" << (*it).refers() << "\">" << (*it).asString() << "</capability>" << std::endl;
-        }
-        cout << "   " << "</" << deptype << ">" << std::endl;
-      }
-
-      static void dumpDeps( const Dependencies &deps )
-      {
-        dumpCapSet( deps[Dep::PROVIDES], "provides" );
-        dumpCapSet( deps[Dep::PREREQUIRES], "prerequires" );
-        dumpCapSet( deps[Dep::CONFLICTS], "conflicts" );
-        dumpCapSet( deps[Dep::OBSOLETES], "obsoletes" );
-        dumpCapSet( deps[Dep::FRESHENS], "freshens" );
-        dumpCapSet( deps[Dep::REQUIRES], "requires" );
-        dumpCapSet( deps[Dep::RECOMMENDS], "recommends" );
-        dumpCapSet( deps[Dep::ENHANCES], "enhances" );
-        dumpCapSet( deps[Dep::SUPPLEMENTS], "supplements" );
-        dumpCapSet( deps[Dep::SUGGESTS], "suggests" );
-      }
-
-      static std::string xml_escape( const std::string &s )
-      {
-        return s;
-      }
-      
-      static void dumpResObject( const ResObject::Ptr r, bool descr, bool deps )
-      {
-        std::string resolvable_line = "[" + r->kind().asString() + "]" + r->name() + " " + r->edition().asString() + " " + r->arch().asString();
-          cout << " <resolvable kind=\"" << r->kind() << "\">" << std::endl;
-          cout <<  "  <name>" << r->name() << "</name>" << std::endl;
-          cout <<  "  <edition>" << r->edition() << "</edition>" << std::endl;
-          cout <<  "  <arch>" << r->arch() << "</arch>" << std::endl;
-          if ( descr )
-          {
-            cout <<  "  <summary>" << r->summary() << "</summary>" << std::endl;
-            cout <<  "  <description>" << r->description() << "</description>" << std::endl;
-          }
-          
-          if ( deps )
-          {
-            cout <<  "  <dependencies>" << std::endl;
-            dumpDeps(r->deps());
-            cout <<  "  </dependencies>" << std::endl;
-          }
-          cout << "  </resolvable>" << std::endl;
-    //std::cout << (**it).deps() << endl;
-      }
-      
-      void dump( const std::list<ResObject::Ptr> &list, bool descr, bool deps )
-      {
-        std::list<std::string> resolvables;
-        cout << "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" << std::endl;
-        cout << "<resolvable-list xmlns=\"http://www.novell.com/zypp/testcases/resolvable-list\">" << std::endl;
-        for (std::list<ResObject::Ptr>::const_iterator it = list.begin(); it != list.end(); it++)
-        {
-          dumpResObject(*it, descr, deps);
-        }
-        cout << "</resolvable-list>" << std::endl;
-      }
-      
-      void dump( const ResStore &store, bool descr, bool deps )
-      {
-        std::list<std::string> resolvables;
-        cout << "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" << std::endl;
-        cout << "<resolvable-list xmlns=\"http://www.novell.com/zypp/testcases/resolvable-list\">" << std::endl;
-        for (ResStore::const_iterator it = store.begin(); it != store.end(); it++)
-        {
-          dumpResObject(*it, descr, deps);
-        }
-        cout << "</resolvable-list>" << std::endl;
-  //std::sort( resolvables.begin(), resolvables.end(), CustomSort );
-  //for (std::list<std::string>::const_iterator it = resolvables.begin(); it != resolvables.end(); ++it)
-  //{
- //   cout << *it << std::endl;
-  //}
-      }
-  
-      
-      
-    }  
-  }
-}
-
diff --git a/testsuite/utils/TestUtils.h b/testsuite/utils/TestUtils.h
deleted file mode 100644 (file)
index cf291bb..0000000
+++ /dev/null
@@ -1,22 +0,0 @@
-
-#ifndef ZYPP_TESTSUITE_TESTUTILS
-#define ZYPP_TESTSUITE_TESTUTILS
-
-#include "zypp/Dependencies.h"
-#include "zypp/Capability.h"
-#include "zypp/ResStore.h"
-
-namespace zypp
-{
-  namespace testsuite
-  {
-    namespace utils
-    {
-      void dump( const ResStore &store, bool descr = false, bool deps = false );  
-      void dump( const std::list<ResObject::Ptr> &list, bool descr = false, bool deps = false );  
-    }  
-  }
-}
-
-#endif
-
diff --git a/testsuite/zypp/tests/CheckSum.cc b/testsuite/zypp/tests/CheckSum.cc
new file mode 100644 (file)
index 0000000..1b93307
--- /dev/null
@@ -0,0 +1,44 @@
+
+#include <iostream>
+#include <list>
+#include <string>
+
+// Boost.Test
+#include <boost/test/floating_point_comparison.hpp>
+#include <boost/test/unit_test.hpp>
+
+using boost::unit_test::test_suite;
+using boost::unit_test::test_case;
+using boost::test_tools::close_at_tolerance;
+
+// parameterized test
+// http://www.boost.org/libs/test/example/unit_test_example4.cpp
+
+#include "zypp/base/Logger.h"
+#include "zypp/base/Exception.h"
+#include "zypp/ZYppFactory.h"
+#include "zypp/ZYpp.h"
+
+using namespace std;
+using namespace zypp;
+
+
+// most frequently you implement test cases as a free functions
+void checksum_test()
+{
+  BOOST_CHECK_THROW( CheckSum( "sha1", "dsdsads" ), Exception ); // wrong size
+  BOOST_CHECK_THROW( CheckSum( "sha256", "dsdsads" ), Exception ); // wrong size
+  BOOST_CHECK_THROW( CheckSum( "md5", "dsdsads" ), Exception ); // wrong size
+  BOOST_CHECK_THROW( CheckSum( "md4", "dsdsads" ), Exception ); // wrong size
+  BOOST_CHECK_THROW( CheckSum( "md2", "dsdsads" ), Exception ); // wrong size
+
+}
+
+test_suite*
+init_unit_test_suite( int, char* [] )
+{
+    test_suite* test= BOOST_TEST_SUITE( "CheckSumTest" );
+    test->add( BOOST_TEST_CASE( &checksum_test ), 0 /* expected zero error */ );
+    return test;
+}
+
index 65295b9..4425954 100644 (file)
@@ -12,11 +12,12 @@ noinst_PROGRAMS = Arch Url1 Url2 Url3 Url4 Url5 Url \
        Edition         \
        Capabilities    \
        PtrTypes \
-       TranslatedText
+       TranslatedText \
+       CheckSum
 
 ## ##################################################
 
-LDADD =        $(top_srcdir)/zypp/lib@PACKAGE@.la
+LDADD =        $(top_srcdir)/zypp/lib@PACKAGE@.la -lboost_unit_test_framework
 
 ## ##################################################
 
@@ -28,6 +29,8 @@ Url4_SOURCES = Url4.cc
 Url5_SOURCES = Url5.cc
 Url_SOURCES  = Url.cc
 TranslatedText_SOURCES  = TranslatedText.cc
+PublicKey_SOURCES = PublicKey.cc
+CheckSum_SOURCES = CheckSum.cc
 RWPtr_SOURCES = RWPtr.cc
 Edition_SOURCES = Edition.cc
 Capabilities_SOURCES = Capabilities.cc
index beac5e6..ec16368 100644 (file)
@@ -8,17 +8,11 @@
 #include "zypp/PublicKey.h"
 #include "zypp/ZYppFactory.h"
 #include "zypp/ZYpp.h"
+#include "testsuite/src/utils/TestUtils.h"
 
 using namespace std;
 using namespace zypp;
-
-static void assert_equal( const std::string &got, const std::string &expected)
-{
-  if (got != expected)
-  {
-    ZYPP_THROW(Exception("Expected '" + expected + "', got '" + got + "'"));
-  }
-}
+using namespace zypp::testsuite::utils;
 
 int main( int argc, char * argv[] )
 {
index 3cb4551..172d5cd 100644 (file)
@@ -37,9 +37,12 @@ namespace zypp
     }
 
     if (
-          ( (str::toLower(type) == "md5"   ) && (checksum.size() != 32) ) ||
-          ( (str::toLower(type) == "sha1"  ) && (checksum.size() != 40) ) ||
-          ( (str::toLower(type) == "sha256") && (checksum.size() != 64) ) )
+          ( (str::toLower(type) == "md2"     ) && (checksum.size() != 32) ) ||
+          ( (str::toLower(type) == "md4"     ) && (checksum.size() != 32) ) ||
+          ( (str::toLower(type) == "md5"     ) && (checksum.size() != 32) ) ||
+          ( (str::toLower(type) == "sha1"    ) && (checksum.size() != 40) ) ||
+          ( (str::toLower(type) == "rmd160"  ) && (checksum.size() != 40) ) ||
+          ( (str::toLower(type) == "sha256"  ) && (checksum.size() != 64) ) )
     {
       ZYPP_THROW(Exception("Bad checksum, " + type + " algorithm of size " + str::numstring(checksum.size())));
     }
@@ -59,16 +62,16 @@ namespace zypp
   { return (checksum().empty() || type().empty()); }
   
   /** \relates CheckSum Stream output. */
-  inline std::ostream & operator<<( std::ostream & str, const CheckSum & obj )
+  std::ostream & operator<<( std::ostream & str, const CheckSum & obj )
   { return str << (obj.empty() ? std::string("NoCheckSum")
                                : obj.type()+"-"+obj.checksum()  ); }
 
   /** \relates CheckSum */
-  inline bool operator==( const CheckSum & lhs, const CheckSum & rhs )
+  bool operator==( const CheckSum & lhs, const CheckSum & rhs )
   { return lhs.checksum() == rhs.checksum() && lhs.type() == rhs.type(); }
 
   /** \relates CheckSum */
-  inline bool operator!=( const CheckSum & lhs, const CheckSum & rhs )
+  bool operator!=( const CheckSum & lhs, const CheckSum & rhs )
   { return ! ( lhs == rhs ); }
 
 } // namespace zypp
index b6d3994..ea308de 100644 (file)
@@ -41,13 +41,13 @@ namespace zypp
   };
 
   /** \relates CheckSum Stream output. */
-  inline std::ostream & operator<<( std::ostream & str, const CheckSum & obj );
+  std::ostream & operator<<( std::ostream & str, const CheckSum & obj );
 
   /** \relates CheckSum */
-  inline bool operator==( const CheckSum & lhs, const CheckSum & rhs );
+  bool operator==( const CheckSum & lhs, const CheckSum & rhs );
 
   /** \relates CheckSum */
-  inline bool operator!=( const CheckSum & lhs, const CheckSum & rhs );
+  bool operator!=( const CheckSum & lhs, const CheckSum & rhs );
 
 } // namespace zypp
 ///////////////////////////////////////////////////////////////////