testHtmlQuote.cc
Go to the documentation of this file.
1 /*
2  * Copyright (C) 1996-2023 The Squid Software Foundation and contributors
3  *
4  * Squid software is distributed under GPLv2+ license and includes
5  * contributions from numerous individuals and organizations.
6  * Please see the COPYING and CONTRIBUTORS files for details.
7  */
8 
9 #include "squid.h"
10 #include "compat/cppunit.h"
11 #include "html/Quoting.h"
12 #include "unitTestMain.h"
13 
14 #include <cstring>
15 #include <iostream>
16 
17 class TestHtmlQuote: public CPPUNIT_NS::TestFixture
18 {
22 
23 protected:
24  void test_html_quote_cstr();
25  void testPerformance();
26 };
27 
29 
30 void
32 {
33  CPPUNIT_ASSERT_EQUAL(std::string(""), std::string(html_quote("")));
34  CPPUNIT_ASSERT_EQUAL(std::string("bar"), std::string(html_quote("bar")));
35  CPPUNIT_ASSERT_EQUAL(std::string("foo&lt;bar&gt;gazonk"), std::string(html_quote("foo<bar>gazonk")));
36  CPPUNIT_ASSERT_EQUAL(std::string("foo&amp;bar"), std::string(html_quote("foo&bar")));
37  CPPUNIT_ASSERT_EQUAL(std::string("some&apos;thing"), std::string(html_quote("some'thing")));
38  CPPUNIT_ASSERT_EQUAL(std::string("some&quot;thing"), std::string(html_quote("some\"thing")));
39  CPPUNIT_ASSERT_EQUAL(std::string("&lt;&gt;&quot;&amp;&apos;"), std::string(html_quote("<>\"&'")));
40  CPPUNIT_ASSERT_EQUAL(std::string("&gt;"), std::string(html_quote(">")));
41  CPPUNIT_ASSERT_EQUAL(std::string("&#163;"), std::string(html_quote("\xa3")));
42 
43  for (unsigned char ch = 1; ch < 0xff; ++ch) {
44  unsigned char buf[2] = {ch, '\0'};
45  auto quoted = html_quote(reinterpret_cast<char *>(buf));
46 
47  if (strlen(quoted) == 1) {
48  CPPUNIT_ASSERT_EQUAL(static_cast<int>(ch), static_cast<int>(quoted[0]));
49  } else {
50  CPPUNIT_ASSERT(strlen(quoted) >= 3);
51  CPPUNIT_ASSERT_EQUAL('&', quoted[0]);
52  CPPUNIT_ASSERT_EQUAL(';', quoted[strlen(quoted)-1]);
53  if (quoted[1] == '#') {
54  CPPUNIT_ASSERT(strlen(quoted) > 3);
55  CPPUNIT_ASSERT(strlen(quoted) <= 6);
56  }
57  }
58  }
59 }
60 
61 int
62 main(int argc, char *argv[])
63 {
64  return TestProgram().run(argc, argv);
65 }
void test_html_quote_cstr()
implements test program's main() function while enabling customization
Definition: unitTestMain.h:25
int run(int argc, char *argv[])
Definition: unitTestMain.h:44
int main(int argc, char *argv[])
CPPUNIT_TEST_SUITE_REGISTRATION(TestHtmlQuote)
char * html_quote(const char *string)
Definition: Quoting.cc:42
CPPUNIT_TEST(test_html_quote_cstr)
void testPerformance()
CPPUNIT_TEST_SUITE(TestHtmlQuote)

 

Introduction

Documentation

Support

Miscellaneous

Web Site Translations

Mirrors