testConfigParser.cc
Go to the documentation of this file.
1 /*
2  * Copyright (C) 1996-2025 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 "ConfigParser.h"
12 #include "SquidString.h"
13 #include "unitTestMain.h"
14 
15 /*
16  * test the ConfigParser framework
17  */
18 
19 class TestConfigParser : public CPPUNIT_NS::TestFixture
20 {
24 
25 protected:
26  bool doParseQuotedTest(const char *, const char *);
27  void testParseQuoted();
28 };
29 
31 
32 int shutting_down = 0;
33 
34 bool TestConfigParser::doParseQuotedTest(const char *s, const char *expectInterp)
35 {
36  char cfgline[2048];
37  char cfgparam[2048];
38  snprintf(cfgline, 2048, "%s", s);
39 
40  // Keep the initial value on cfgparam. The ConfigParser methods will write on cfgline
41  strncpy(cfgparam, cfgline, sizeof(cfgparam)-1);
42  cfgparam[sizeof(cfgparam)-1] = '\0';
43 
44  // Initialize parser to point to the start of quoted string
45  ConfigParser::SetCfgLine(cfgline);
46  String unEscaped = ConfigParser::NextToken();
47 
48  const bool interpOk = (unEscaped.cmp(expectInterp) == 0);
49  if (!interpOk) {
50  printf("%25s: %s\n%25s: %s\n%25s: %s\n",
51  "Raw configuration", cfgparam,
52  "Expected interpretation", expectInterp,
53  "Actual interpretation", unEscaped.termedBuf());
54  }
55 
56  const char *quoted = ConfigParser::QuoteString(unEscaped);
57  bool quotedOk = (strcmp(cfgparam, quoted)==0);
58  if (!quotedOk) {
59  printf("%25s: %s\n%25s: %s\n%25s: %s\n",
60  "Raw configuration", cfgparam,
61  "Parsed and quoted", quoted,
62  "parsed value was", unEscaped.termedBuf());
63  }
64 
65  return quotedOk && interpOk ;
66 }
67 
69 {
70  // SingleToken
71  CPPUNIT_ASSERT_EQUAL(true, doParseQuotedTest("SingleToken", "SingleToken"));
72 
73  // This is a quoted "string" by me
74  CPPUNIT_ASSERT_EQUAL(true, doParseQuotedTest("\"This is a quoted \\\"string\\\" by me\"",
75  "This is a quoted \"string\" by me"));
76 
77  // escape sequence test: \\"\"\\"
78  CPPUNIT_ASSERT_EQUAL(true, doParseQuotedTest("\"escape sequence test: \\\\\\\\\\\"\\\\\\\"\\\\\\\\\\\"\"",
79  "escape sequence test: \\\\\"\\\"\\\\\""));
80 
81  // \beginning and end test"
82  CPPUNIT_ASSERT_EQUAL(true, doParseQuotedTest("\"\\\\beginning and end test\\\"\"",
83  "\\beginning and end test\""));
84 
85  // "
86  CPPUNIT_ASSERT_EQUAL(true, doParseQuotedTest("\"\\\"\"", "\""));
87 
88  /* \ */
89  CPPUNIT_ASSERT_EQUAL(true, doParseQuotedTest("\"\\\\\"", "\\"));
90 }
91 
92 int
93 main(int argc, char *argv[])
94 {
95  return TestProgram().run(argc, argv);
96 }
97 
int main(int argc, char *argv[])
implements test program's main() function while enabling customization
Definition: unitTestMain.h:25
int cmp(char const *) const
Definition: String.cc:236
int run(int argc, char *argv[])
Definition: unitTestMain.h:44
bool doParseQuotedTest(const char *, const char *)
static char * NextToken()
static const char * QuoteString(const String &var)
int shutting_down
const char * termedBuf() const
Definition: SquidString.h:92
static void SetCfgLine(char *line)
Set the configuration file line to parse.
CPPUNIT_TEST_SUITE(TestConfigParser)
CPPUNIT_TEST_SUITE_REGISTRATION(TestConfigParser)
CPPUNIT_TEST(testParseQuoted)

 

Introduction

Documentation

Support

Miscellaneous