From 4cbb78cf7d1963d3b874291e00be32787404b0e1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?V=C3=A1clav=20Slav=C3=ADk?= Date: Wed, 5 Feb 2014 18:29:03 +0000 Subject: [PATCH] Add line-endings fuzzing test to TextFileTestCase. Stress-test wxTextFile's handling of unusual CR,LF characters. git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@75800 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775 --- tests/textfile/textfiletest.cpp | 34 ++++++++++++++++++++++++++++++++- 1 file changed, 33 insertions(+), 1 deletion(-) diff --git a/tests/textfile/textfiletest.cpp b/tests/textfile/textfiletest.cpp index 7d585eda9e..5fb53e2706 100644 --- a/tests/textfile/textfiletest.cpp +++ b/tests/textfile/textfiletest.cpp @@ -35,7 +35,10 @@ class TextFileTestCase : public CppUnit::TestCase { public: - TextFileTestCase() { } + TextFileTestCase() + { + srand((unsigned)time(NULL)); + } virtual void tearDown() { unlink(GetTestFileName()); } @@ -49,6 +52,7 @@ private: CPPUNIT_TEST( ReadMac ); CPPUNIT_TEST( ReadMacLast ); CPPUNIT_TEST( ReadMixed ); + CPPUNIT_TEST( ReadMixedWithFuzzing ); CPPUNIT_TEST( ReadCRCRLF ); #if wxUSE_UNICODE CPPUNIT_TEST( ReadUTF8 ); @@ -65,6 +69,7 @@ private: void ReadMac(); void ReadMacLast(); void ReadMixed(); + void ReadMixedWithFuzzing(); void ReadCRCRLF(); #if wxUSE_UNICODE void ReadUTF8(); @@ -212,6 +217,33 @@ void TextFileTestCase::ReadMixed() CPPUNIT_ASSERT_EQUAL( wxString(wxT("baz")), f.GetLastLine() ); } +void TextFileTestCase::ReadMixedWithFuzzing() +{ + // Create a random buffer with lots of newlines. This is intended to catch + // bad parsing in unexpected situations such as the one from ReadCRCRLF() + // (which is so common it deserves a test of its own). + static const char CHOICES[] = {'\r', '\n', 'X'}; + + unsigned linesCnt = 0; + const size_t BUF_LEN = 20000; + char data[BUF_LEN + 1]; + data[0] = 'X'; + data[BUF_LEN] = '\0'; + for ( size_t i = 1; i < BUF_LEN; i++ ) + { + char ch = CHOICES[rand() % WXSIZEOF(CHOICES)]; + data[i] = ch; + if ( ch == '\r' || (ch == '\n' && data[i-1] != '\r') ) + linesCnt++; + } + + CreateTestFile(data); + + wxTextFile f; + CPPUNIT_ASSERT( f.Open(wxString::FromAscii(GetTestFileName())) ); + CPPUNIT_ASSERT_EQUAL( (size_t)linesCnt, f.GetLineCount() ); +} + void TextFileTestCase::ReadCRCRLF() { // Notepad may create files with CRCRLF line endings (see