Fix string hexdecoding
authorMichael Andres <ma@suse.de>
Thu, 5 Sep 2013 14:53:57 +0000 (16:53 +0200)
committerMichael Andres <ma@suse.de>
Thu, 5 Sep 2013 14:53:57 +0000 (16:53 +0200)
tests/zypp/base/String_test.cc
zypp/base/String.cc

index e01bb00..0d42046 100644 (file)
@@ -276,8 +276,30 @@ BOOST_AUTO_TEST_CASE(hexencode_hexdecode)
   }
 
   std::string d( str::hexdecode( e ) );
+  // decoded equals original
   BOOST_CHECK( o == d );
-//   for ( unsigned i = 0; i < 255; ++i )
-//     if ( o[i] != d[i] )
-//       WAR << i << " " << unsigned(o[i]) << " != " << unsigned(d[i]) << endl;
+
+  // Test %XX is decoded for hexdigits only
+  const char *const dig = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
+  for ( const char * d1 = dig; *d1; ++d1 )
+    for ( const char * d2 = dig; *d2; ++d2 )
+    {
+      std::string eu( "%" );
+      eu += *d1; eu += *d2;
+      std::string el( str::toLower(eu) );
+
+      std::string u( str::hexdecode( eu ) );
+      std::string l( str::hexdecode( el ) );
+
+      if ( *d1 <= 'F' &&  *d2 <= 'F' )
+      {
+       BOOST_CHECK_EQUAL( u, l );              // no matter if upper or lower case hexdigit
+       BOOST_CHECK_EQUAL( u.size(), 1 );       // size 1 == decoded
+      }
+      else
+      {
+       BOOST_CHECK_EQUAL( u, eu );             // no hexdigits remain unchanged
+       BOOST_CHECK_EQUAL( l, el );
+     }
+    }
 }
index f332d3f..8ec8d6e 100644 (file)
@@ -102,10 +102,10 @@ namespace zypp
       {
         if ( '0' <= ch && ch <= '9' )
           return( ch - '0' );
-        if ( 'A' <= ch && ch <= 'Z' )
-          return( ch - 'A' + 10 );
-        if ( 'a' <= ch && ch <= 'z' )
+        if ( 'A' <= ch && ch <= 'F' )
           return( ch - 'A' + 10 );
+        if ( 'a' <= ch && ch <= 'f' )
+          return( ch - 'a' + 10 );
         return -1;
       }
     }