From: Václav Slavík Date: Thu, 24 Jun 2010 10:34:36 +0000 (+0000) Subject: Add tests for printf arguments validation code. X-Git-Url: https://git.saurik.com/wxWidgets.git/commitdiff_plain/ad6e5c47de7c299dc9fb67da52ba94db3d80f754?ds=sidebyside Add tests for printf arguments validation code. git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@64713 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775 --- diff --git a/tests/strings/vararg.cpp b/tests/strings/vararg.cpp index a5677c6c68..5e165d5f16 100644 --- a/tests/strings/vararg.cpp +++ b/tests/strings/vararg.cpp @@ -42,6 +42,7 @@ private: #endif CPPUNIT_TEST( Sscanf ); CPPUNIT_TEST( RepeatedPrintf ); + CPPUNIT_TEST( ArgsValidation ); CPPUNIT_TEST_SUITE_END(); void StringPrintf(); @@ -51,6 +52,7 @@ private: #endif void Sscanf(); void RepeatedPrintf(); + void ArgsValidation(); 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); } + +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 + VarArgTestCase& somePOD = *this; + WX_ASSERT_FAILS_WITH_ASSERT( wxString::Format("%s", somePOD) ); +#endif +}