From ce5c0bc802be4ca450bb3015a7fd87ae356b8c82 Mon Sep 17 00:00:00 2001 From: Kari Argillander Date: Thu, 16 May 2024 16:58:58 +0300 Subject: [PATCH] Update string unittests to test valid/invalid nullptr We have nullptr validation in these functions. Test also that. Test also that with length 0 we do not get AssertionViolationException. --- Foundation/testsuite/src/StringTest.cpp | 6 ++++++ Foundation/testsuite/src/TextConverterTest.cpp | 17 +++++++++++++++++ Foundation/testsuite/src/TextConverterTest.h | 1 + Foundation/testsuite/src/UTF8StringTest.cpp | 9 +++++++++ 4 files changed, 33 insertions(+) diff --git a/Foundation/testsuite/src/StringTest.cpp b/Foundation/testsuite/src/StringTest.cpp index 031ce13b35e..87c47b3052a 100644 --- a/Foundation/testsuite/src/StringTest.cpp +++ b/Foundation/testsuite/src/StringTest.cpp @@ -324,6 +324,12 @@ void StringTest::testIcompare() assertTrue (icompare(ss1, 2, 2, "bb") < 0); assertTrue (icompare(ss1, 2, "aaa") > 0); + + assertTrue (icompare(ss1, 0, 0, nullptr) == 0); + try { + icompare(ss1, 0, 1, nullptr); + fail ("must fail"); + } catch (Poco::AssertionViolationException&) {}; } diff --git a/Foundation/testsuite/src/TextConverterTest.cpp b/Foundation/testsuite/src/TextConverterTest.cpp index c905953c26a..1103155df39 100644 --- a/Foundation/testsuite/src/TextConverterTest.cpp +++ b/Foundation/testsuite/src/TextConverterTest.cpp @@ -20,6 +20,7 @@ #include "Poco/Windows1251Encoding.h" #include "Poco/Windows1252Encoding.h" #include "Poco/UTF8Encoding.h" +#include "Poco/Exception.h" using namespace Poco; @@ -326,6 +327,21 @@ void TextConverterTest::testErrors() } +void TextConverterTest::testPointerSafety() +{ + UTF8Encoding utf8Encoding; + Latin1Encoding latin1Encoding; + TextConverter converter(utf8Encoding, latin1Encoding); + + std::string result; + try { + converter.convert(nullptr, 1, result); + fail ("must fail"); + } catch (AssertionViolationException&) {}; + converter.convert(nullptr, 0, result); +} + + void TextConverterTest::setUp() { } @@ -350,6 +366,7 @@ CppUnit::Test* TextConverterTest::suite() CppUnit_addTest(pSuite, TextConverterTest, testCP1251toUTF8); CppUnit_addTest(pSuite, TextConverterTest, testCP1252toUTF8); CppUnit_addTest(pSuite, TextConverterTest, testErrors); + CppUnit_addTest(pSuite, TextConverterTest, testPointerSafety); return pSuite; } diff --git a/Foundation/testsuite/src/TextConverterTest.h b/Foundation/testsuite/src/TextConverterTest.h index 8ab849e3315..bed7c8695d4 100644 --- a/Foundation/testsuite/src/TextConverterTest.h +++ b/Foundation/testsuite/src/TextConverterTest.h @@ -34,6 +34,7 @@ class TextConverterTest: public CppUnit::TestCase void testCP1251toUTF8(); void testCP1252toUTF8(); void testErrors(); + void testPointerSafety(); void setUp(); void tearDown(); diff --git a/Foundation/testsuite/src/UTF8StringTest.cpp b/Foundation/testsuite/src/UTF8StringTest.cpp index 6799ff35d30..66e80230822 100644 --- a/Foundation/testsuite/src/UTF8StringTest.cpp +++ b/Foundation/testsuite/src/UTF8StringTest.cpp @@ -12,6 +12,7 @@ #include "CppUnit/TestCaller.h" #include "CppUnit/TestSuite.h" #include "Poco/UTF8String.h" +#include "Poco/Exception.h" using Poco::UTF8; @@ -58,6 +59,14 @@ void UTF8StringTest::testCompare() std::string b6("\303\234\303\226\303\204"); // "U"O"A assertTrue (UTF8::icompare(a6, b6) == 0); + + std::string a7("a"); + + assertTrue (UTF8::icompare(a7, 0, 0, nullptr) == 0); + try { + UTF8::icompare(a7, 0, 1, nullptr); + fail ("must fail"); + } catch (Poco::AssertionViolationException&) {}; }