/* * Copyright (C) 1996-2023 The Squid Software Foundation and contributors * * Squid software is distributed under GPLv2+ license and includes * contributions from numerous individuals and organizations. * Please see the COPYING and CONTRIBUTORS files for details. */ #include "squid.h" #include "compat/cppunit.h" #include "html/Quoting.h" #include "unitTestMain.h" #include #include class TestHtmlQuote: public CPPUNIT_NS::TestFixture { CPPUNIT_TEST_SUITE(TestHtmlQuote); CPPUNIT_TEST(test_html_quote_cstr); CPPUNIT_TEST_SUITE_END(); protected: void test_html_quote_cstr(); void testPerformance(); }; CPPUNIT_TEST_SUITE_REGISTRATION( TestHtmlQuote ); void TestHtmlQuote::test_html_quote_cstr() { CPPUNIT_ASSERT_EQUAL(std::string(""), std::string(html_quote(""))); CPPUNIT_ASSERT_EQUAL(std::string("bar"), std::string(html_quote("bar"))); CPPUNIT_ASSERT_EQUAL(std::string("foo<bar>gazonk"), std::string(html_quote("foogazonk"))); CPPUNIT_ASSERT_EQUAL(std::string("foo&bar"), std::string(html_quote("foo&bar"))); CPPUNIT_ASSERT_EQUAL(std::string("some'thing"), std::string(html_quote("some'thing"))); CPPUNIT_ASSERT_EQUAL(std::string("some"thing"), std::string(html_quote("some\"thing"))); CPPUNIT_ASSERT_EQUAL(std::string("<>"&'"), std::string(html_quote("<>\"&'"))); CPPUNIT_ASSERT_EQUAL(std::string(">"), std::string(html_quote(">"))); CPPUNIT_ASSERT_EQUAL(std::string("£"), std::string(html_quote("\xa3"))); for (unsigned char ch = 1; ch < 0xff; ++ch) { unsigned char buf[2] = {ch, '\0'}; auto quoted = html_quote(reinterpret_cast(buf)); if (strlen(quoted) == 1) { CPPUNIT_ASSERT_EQUAL(static_cast(ch), static_cast(quoted[0])); } else { CPPUNIT_ASSERT(strlen(quoted) >= 3); CPPUNIT_ASSERT_EQUAL('&', quoted[0]); CPPUNIT_ASSERT_EQUAL(';', quoted[strlen(quoted)-1]); if (quoted[1] == '#') { CPPUNIT_ASSERT(strlen(quoted) > 3); CPPUNIT_ASSERT(strlen(quoted) <= 6); } } } } int main(int argc, char *argv[]) { return TestProgram().run(argc, argv); }