]> git.saurik.com Git - wxWidgets.git/blobdiff - tests/base64/base64.cpp
Add CombineURIs implementation for wxWebFileProtocolHandler. Update the IE backend...
[wxWidgets.git] / tests / base64 / base64.cpp
index d0febfae89f3f9a5ba10499a0fd89e010ca7f8f6..9d7475a62715df6be74c779e7038d7098d76e50f 100644 (file)
@@ -59,7 +59,7 @@ static void generateRandomData(void* buff, size_t len)
 
 static void generateGibberish(void* buff, size_t len)
 {
-    static const unsigned char cb64[] = 
+    static const unsigned char cb64[] =
         "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
 
     unsigned char *cbuff = (unsigned char *)buff;
@@ -90,6 +90,7 @@ private:
         CPPUNIT_TEST( EncodeDecodePatternB );
         CPPUNIT_TEST( EncodeDecodePatternC );
         CPPUNIT_TEST( EncodeDecodeRandom );
+        CPPUNIT_TEST( DecodeInvalid );
     CPPUNIT_TEST_SUITE_END();
 
     void EncodeDecodeEmpty();
@@ -102,6 +103,7 @@ private:
     void EncodeDecodePatternB();
     void EncodeDecodePatternC();
     void EncodeDecodeRandom();
+    void DecodeInvalid();
 
     DECLARE_NO_COPY_CLASS(Base64TestCase)
 };
@@ -109,7 +111,7 @@ private:
 // register in the unnamed registry so that these tests are run by default
 CPPUNIT_TEST_SUITE_REGISTRATION( Base64TestCase );
 
-// also include in it's own registry so that these tests can be run alone
+// also include in its own registry so that these tests can be run alone
 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( Base64TestCase, "Base64TestCase" );
 
 void Base64TestCase::EncodeDecodeEmpty()
@@ -125,11 +127,11 @@ void Base64TestCase::EncodeDecodeEmpty()
     CPPUNIT_ASSERT_EQUAL('\0', shouldBeEmpty[0]);
 
     wxMemoryBuffer bufmt;
-    wxString resultmt = wxBase64Encode(bufmt);
-    CPPUNIT_ASSERT(resultmt.empty());
+    wxString resultEmpty = wxBase64Encode(bufmt);
+    CPPUNIT_ASSERT(resultEmpty.empty());
 
-    bufmt = wxBase64Decode(resultmt);
-    CPPUNIT_ASSERT_EQUAL(0u, bufmt.GetDataLen());
+    bufmt = wxBase64Decode(resultEmpty);
+    CPPUNIT_ASSERT_EQUAL(0, bufmt.GetDataLen());
 }
 
 void Base64TestCase::EncodeDecodeA()
@@ -138,8 +140,15 @@ void Base64TestCase::EncodeDecodeA()
     CPPUNIT_ASSERT_EQUAL(wxString("QQ=="), str);
 
     wxMemoryBuffer buf = wxBase64Decode(str);
-    CPPUNIT_ASSERT_EQUAL(1u, buf.GetDataLen());
+    CPPUNIT_ASSERT_EQUAL(1, buf.GetDataLen());
     CPPUNIT_ASSERT_EQUAL('A', *(char *)buf.GetData());
+
+    char cbuf[10];
+    memset(cbuf, (char)-1, sizeof(cbuf));
+    CPPUNIT_ASSERT_EQUAL( 1, wxBase64Decode(cbuf, 1, str) );
+    CPPUNIT_ASSERT_EQUAL( 'A', cbuf[0] );
+    CPPUNIT_ASSERT_EQUAL( (char)-1, cbuf[1] );
+    CPPUNIT_ASSERT_EQUAL( (char)-1, cbuf[2] );
 }
 
 void Base64TestCase::EncodeDecodeAB()
@@ -148,9 +157,18 @@ void Base64TestCase::EncodeDecodeAB()
     CPPUNIT_ASSERT_EQUAL(wxString("QUI="), str);
 
     wxMemoryBuffer buf = wxBase64Decode(str);
-    CPPUNIT_ASSERT_EQUAL(2u, buf.GetDataLen());
+    CPPUNIT_ASSERT_EQUAL(2, buf.GetDataLen());
     CPPUNIT_ASSERT_EQUAL('A', buf[0]);
     CPPUNIT_ASSERT_EQUAL('B', buf[1]);
+
+    char cbuf[10];
+    memset(cbuf, (char)-1, sizeof(cbuf));
+    CPPUNIT_ASSERT_EQUAL( wxCONV_FAILED, wxBase64Decode(cbuf, 1, str) );
+    CPPUNIT_ASSERT_EQUAL( 2, wxBase64Decode(cbuf, 2, str) );
+    CPPUNIT_ASSERT_EQUAL( 'A', cbuf[0] );
+    CPPUNIT_ASSERT_EQUAL( 'B', cbuf[1] );
+    CPPUNIT_ASSERT_EQUAL( (char)-1, cbuf[2] );
+    CPPUNIT_ASSERT_EQUAL( (char)-1, cbuf[3] );
 }
 
 void Base64TestCase::EncodeDecodeABC()
@@ -159,10 +177,20 @@ void Base64TestCase::EncodeDecodeABC()
     CPPUNIT_ASSERT_EQUAL(wxString("QUJD"), str);
 
     wxMemoryBuffer buf = wxBase64Decode(str);
-    CPPUNIT_ASSERT_EQUAL(3u, buf.GetDataLen());
+    CPPUNIT_ASSERT_EQUAL(3, buf.GetDataLen());
     CPPUNIT_ASSERT_EQUAL('A', buf[0]);
     CPPUNIT_ASSERT_EQUAL('B', buf[1]);
     CPPUNIT_ASSERT_EQUAL('C', buf[2]);
+
+    char cbuf[10];
+    memset(cbuf, (char)-1, sizeof(cbuf));
+    CPPUNIT_ASSERT_EQUAL( wxCONV_FAILED, wxBase64Decode(cbuf, 2, str) );
+    CPPUNIT_ASSERT_EQUAL( 3, wxBase64Decode(cbuf, 3, str) );
+    CPPUNIT_ASSERT_EQUAL( 'A', cbuf[0] );
+    CPPUNIT_ASSERT_EQUAL( 'B', cbuf[1] );
+    CPPUNIT_ASSERT_EQUAL( 'C', cbuf[2] );
+    CPPUNIT_ASSERT_EQUAL( (char)-1, cbuf[3] );
+    CPPUNIT_ASSERT_EQUAL( (char)-1, cbuf[4] );
 }
 
 void Base64TestCase::EncodeDecodeABCD()
@@ -171,11 +199,22 @@ void Base64TestCase::EncodeDecodeABCD()
     CPPUNIT_ASSERT_EQUAL(wxString("QUJDRA=="), str);
 
     wxMemoryBuffer buf = wxBase64Decode(str);
-    CPPUNIT_ASSERT_EQUAL(4u, buf.GetDataLen());
+    CPPUNIT_ASSERT_EQUAL(4, buf.GetDataLen());
     CPPUNIT_ASSERT_EQUAL('A', buf[0]);
     CPPUNIT_ASSERT_EQUAL('B', buf[1]);
     CPPUNIT_ASSERT_EQUAL('C', buf[2]);
     CPPUNIT_ASSERT_EQUAL('D', buf[3]);
+
+    char cbuf[10];
+    memset(cbuf, (char)-1, sizeof(cbuf));
+    CPPUNIT_ASSERT_EQUAL( wxCONV_FAILED, wxBase64Decode(cbuf, 3, str) );
+    CPPUNIT_ASSERT_EQUAL( 4, wxBase64Decode(cbuf, 4, str) );
+    CPPUNIT_ASSERT_EQUAL( 'A', cbuf[0] );
+    CPPUNIT_ASSERT_EQUAL( 'B', cbuf[1] );
+    CPPUNIT_ASSERT_EQUAL( 'C', cbuf[2] );
+    CPPUNIT_ASSERT_EQUAL( 'D', cbuf[3] );
+    CPPUNIT_ASSERT_EQUAL( (char)-1, cbuf[4] );
+    CPPUNIT_ASSERT_EQUAL( (char)-1, cbuf[5] );
 }
 
 void Base64TestCase::EncodeDecode0to255()
@@ -233,4 +272,36 @@ void Base64TestCase::EncodeDecodeRandom()
     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);
+    CPPUNIT_ASSERT_EQUAL( 3, posErr );
+
+    rc = wxBase64Decode(NULL, 0, "one two!", wxNO_LEN,
+                        wxBase64DecodeMode_SkipWS, &posErr);
+    CPPUNIT_ASSERT_EQUAL( wxCONV_FAILED, rc);
+    CPPUNIT_ASSERT_EQUAL( 7, posErr );
+
+    rc = wxBase64Decode(NULL, 0, "? QQ==", wxNO_LEN,
+                        wxBase64DecodeMode_SkipWS, &posErr);
+    CPPUNIT_ASSERT_EQUAL( wxCONV_FAILED, rc);
+    CPPUNIT_ASSERT_EQUAL( 0, posErr );
+
+    posErr = (size_t)-1;
+    rc = wxBase64Decode(NULL, 0, " QQ==", wxNO_LEN,
+                        wxBase64DecodeMode_SkipWS, &posErr);
+    CPPUNIT_ASSERT_EQUAL( 1, rc );
+    CPPUNIT_ASSERT_EQUAL( -1, posErr );
+
+    rc = wxBase64Decode(NULL, 0, "? QQ==", wxNO_LEN,
+                        wxBase64DecodeMode_Relaxed, &posErr);
+    CPPUNIT_ASSERT_EQUAL( 1, rc );
+    CPPUNIT_ASSERT_EQUAL( -1, posErr );
+
+    CPPUNIT_ASSERT( !wxBase64Decode("wxGetApp()").GetDataLen() );
+}
+
 #endif // wxUSE_BASE64