]> git.saurik.com Git - wxWidgets.git/commitdiff
Add tests for printf arguments validation code.
authorVáclav Slavík <vslavik@fastmail.fm>
Thu, 24 Jun 2010 10:34:36 +0000 (10:34 +0000)
committerVáclav Slavík <vslavik@fastmail.fm>
Thu, 24 Jun 2010 10:34:36 +0000 (10:34 +0000)
git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@64713 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775

tests/strings/vararg.cpp

index a5677c6c6819044277f53d1554aa3e4c338a728c..5e165d5f1637759ca9dc5c6d57650849bfc8cdb5 100644 (file)
@@ -42,6 +42,7 @@ private:
 #endif
         CPPUNIT_TEST( Sscanf );
         CPPUNIT_TEST( RepeatedPrintf );
 #endif
         CPPUNIT_TEST( Sscanf );
         CPPUNIT_TEST( RepeatedPrintf );
+        CPPUNIT_TEST( ArgsValidation );
     CPPUNIT_TEST_SUITE_END();
 
     void StringPrintf();
     CPPUNIT_TEST_SUITE_END();
 
     void StringPrintf();
@@ -51,6 +52,7 @@ private:
 #endif
     void Sscanf();
     void RepeatedPrintf();
 #endif
     void Sscanf();
     void RepeatedPrintf();
+    void ArgsValidation();
 
     DECLARE_NO_COPY_CLASS(VarArgTestCase)
 };
 
     DECLARE_NO_COPY_CLASS(VarArgTestCase)
 };
@@ -183,3 +185,34 @@ void VarArgTestCase::RepeatedPrintf()
     s = wxString::Format("buffer %s, len %d", buffer, (int)wxStrlen(buffer));
     CPPUNIT_ASSERT_EQUAL("buffer hi, len 2", s);
 }
     s = wxString::Format("buffer %s, len %d", buffer, (int)wxStrlen(buffer));
     CPPUNIT_ASSERT_EQUAL("buffer hi, len 2", s);
 }
+
+void VarArgTestCase::ArgsValidation()
+{
+    void *ptr = this;
+    int written;
+    short int swritten;
+
+    // these are valid:
+    wxString::Format("a string(%s,%s), ptr %p, int %i",
+                     wxString(), "foo", "char* as pointer", 1);
+
+    wxString::Format("foo%i%n", 42, &written);
+    CPPUNIT_ASSERT_EQUAL( 5, written );
+
+    // but these are not:
+    WX_ASSERT_FAILS_WITH_ASSERT( wxString::Format("%i: too many arguments", 42, 1, 2, 3) );
+    WX_ASSERT_FAILS_WITH_ASSERT( wxString::Format("%i", "foo") );
+    WX_ASSERT_FAILS_WITH_ASSERT( wxString::Format("%s", (void*)this) );
+
+    WX_ASSERT_FAILS_WITH_ASSERT( wxString::Format("%d", ptr) );
+
+    WX_ASSERT_FAILS_WITH_ASSERT( wxString::Format("foo%i%n", &written) );
+    WX_ASSERT_FAILS_WITH_ASSERT( wxString::Format("foo%n", ptr) );
+    WX_ASSERT_FAILS_WITH_ASSERT( wxString::Format("foo%i%n", 42, &swritten) );
+
+#if !defined(HAVE_TYPE_TRAITS) && !defined(HAVE_TR1_TYPE_TRAITS)
+    // this fails at compile-time with <type_traits>
+    VarArgTestCase& somePOD = *this;
+    WX_ASSERT_FAILS_WITH_ASSERT( wxString::Format("%s", somePOD) );
+#endif
+}