]> git.saurik.com Git - wxWidgets.git/commitdiff
added unit tests for decoding invalid base64 strings; corrected bugs exposed by them
authorVadim Zeitlin <vadim@wxwidgets.org>
Tue, 15 Jan 2008 16:56:17 +0000 (16:56 +0000)
committerVadim Zeitlin <vadim@wxwidgets.org>
Tue, 15 Jan 2008 16:56:17 +0000 (16:56 +0000)
git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@51225 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775

src/common/base64.cpp
tests/base64/base64.cpp

index eeafaa40f40ef06b526bd18239e3228383dcddb5..2832bffb3b8dc9c44fd042f5b56bde938abd4382 100644 (file)
@@ -132,7 +132,7 @@ wxBase64Decode(void *dst_, size_t dstLen,
 
                 // force the loop to stop and an error to be returned
                 n = -1;
 
                 // force the loop to stop and an error to be returned
                 n = -1;
-                srcLen = 0;
+                srcLen = 1;
                 break;
 
             case PAD:
                 break;
 
             case PAD:
@@ -156,7 +156,7 @@ wxBase64Decode(void *dst_, size_t dstLen,
                 {
                     // force the loop terminate with an error
                     n = -1;
                 {
                     // force the loop terminate with an error
                     n = -1;
-                    srcLen = 0;
+                    srcLen = 1;
                 }
                 break;
 
                 }
                 break;
 
@@ -165,7 +165,7 @@ wxBase64Decode(void *dst_, size_t dstLen,
                 {
                     // nothing is allowed after the end so provoke error return
                     n = -1;
                 {
                     // nothing is allowed after the end so provoke error return
                     n = -1;
-                    srcLen = 0;
+                    srcLen = 1;
                     break;
                 }
 
                     break;
                 }
 
@@ -194,7 +194,11 @@ wxBase64Decode(void *dst_, size_t dstLen,
     if ( n )
     {
         if ( posErr )
     if ( n )
     {
         if ( posErr )
-            *posErr = p - src;
+        {
+            // notice that the error was on a previous position as we did one
+            // extra "p++" in the loop line after it
+            *posErr = p - src - 1;
+        }
 
         return wxCONV_FAILED;
     }
 
         return wxCONV_FAILED;
     }
index 7b492d7e69477bcd15c695434d086fd8d5e54578..a63512aca2cdaa73f6a19c122ab3a84cefd7e596 100644 (file)
@@ -90,6 +90,7 @@ private:
         CPPUNIT_TEST( EncodeDecodePatternB );
         CPPUNIT_TEST( EncodeDecodePatternC );
         CPPUNIT_TEST( EncodeDecodeRandom );
         CPPUNIT_TEST( EncodeDecodePatternB );
         CPPUNIT_TEST( EncodeDecodePatternC );
         CPPUNIT_TEST( EncodeDecodeRandom );
+        CPPUNIT_TEST( DecodeInvalid );
     CPPUNIT_TEST_SUITE_END();
 
     void EncodeDecodeEmpty();
     CPPUNIT_TEST_SUITE_END();
 
     void EncodeDecodeEmpty();
@@ -102,6 +103,7 @@ private:
     void EncodeDecodePatternB();
     void EncodeDecodePatternC();
     void EncodeDecodeRandom();
     void EncodeDecodePatternB();
     void EncodeDecodePatternC();
     void EncodeDecodeRandom();
+    void DecodeInvalid();
 
     DECLARE_NO_COPY_CLASS(Base64TestCase)
 };
 
     DECLARE_NO_COPY_CLASS(Base64TestCase)
 };
@@ -233,4 +235,36 @@ void Base64TestCase::EncodeDecodeRandom()
     CPPUNIT_ASSERT(wxBase64Encode(buff2, size, buff2, realsize));
 }
 
     CPPUNIT_ASSERT(wxBase64Encode(buff2, size, buff2, realsize));
 }
 
+void Base64TestCase::DecodeInvalid()
+{
+    size_t rc, posErr;
+    rc = wxBase64Decode(NULL, 0, "one two!", wxNO_LEN,
+                        wxBase64DecodeMode_Strict, &posErr);
+    CPPUNIT_ASSERT_EQUAL( wxCONV_FAILED, rc);
+    WX_ASSERT_SIZET_EQUAL( 3, posErr );
+
+    rc = wxBase64Decode(NULL, 0, "one two!", wxNO_LEN,
+                        wxBase64DecodeMode_SkipWS, &posErr);
+    CPPUNIT_ASSERT_EQUAL( wxCONV_FAILED, rc);
+    WX_ASSERT_SIZET_EQUAL( 7, posErr );
+
+    rc = wxBase64Decode(NULL, 0, "? QQ==", wxNO_LEN,
+                        wxBase64DecodeMode_SkipWS, &posErr);
+    CPPUNIT_ASSERT_EQUAL( wxCONV_FAILED, rc);
+    WX_ASSERT_SIZET_EQUAL( 0, posErr );
+
+    posErr = (size_t)-1;
+    rc = wxBase64Decode(NULL, 0, " QQ==", wxNO_LEN,
+                        wxBase64DecodeMode_SkipWS, &posErr);
+    WX_ASSERT_SIZET_EQUAL( 1, rc );
+    WX_ASSERT_SIZET_EQUAL( -1, posErr );
+
+    rc = wxBase64Decode(NULL, 0, "? QQ==", wxNO_LEN,
+                        wxBase64DecodeMode_Relaxed, &posErr);
+    WX_ASSERT_SIZET_EQUAL( 1, rc );
+    WX_ASSERT_SIZET_EQUAL( -1, posErr );
+
+    CPPUNIT_ASSERT( !wxBase64Decode("wxGetApp()").GetDataLen() );
+}
+
 #endif // wxUSE_BASE64
 #endif // wxUSE_BASE64