testConfigParser.cc
Go to the documentation of this file.
1/*
2 * Copyright (C) 1996-2022 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 "ConfigParser.h"
11#include "SquidString.h"
13#include "unitTestMain.h"
14
16
18
20{
21}
22
23bool testConfigParser::doParseQuotedTest(const char *s, const char *expectInterp)
24{
25 char cfgline[2048];
26 char cfgparam[2048];
27 snprintf(cfgline, 2048, "%s", s);
28
29 // Keep the initial value on cfgparam. The ConfigParser methods will write on cfgline
30 strncpy(cfgparam, cfgline, sizeof(cfgparam)-1);
31 cfgparam[sizeof(cfgparam)-1] = '\0';
32
33 // Initialize parser to point to the start of quoted string
35 String unEscaped = ConfigParser::NextToken();
36
37 const bool interpOk = (unEscaped.cmp(expectInterp) == 0);
38 if (!interpOk) {
39 printf("%25s: %s\n%25s: %s\n%25s: %s\n",
40 "Raw configuration", cfgparam,
41 "Expected interpretation", expectInterp,
42 "Actual interpretation", unEscaped.termedBuf());
43 }
44
45 const char *quoted = ConfigParser::QuoteString(unEscaped);
46 bool quotedOk = (strcmp(cfgparam, quoted)==0);
47 if (!quotedOk) {
48 printf("%25s: %s\n%25s: %s\n%25s: %s\n",
49 "Raw configuration", cfgparam,
50 "Parsed and quoted", quoted,
51 "parsed value was", unEscaped.termedBuf());
52 }
53
54 return quotedOk && interpOk ;
55}
56
58{
59 // SingleToken
60 CPPUNIT_ASSERT_EQUAL(true, doParseQuotedTest("SingleToken", "SingleToken"));
61
62 // This is a quoted "string" by me
63 CPPUNIT_ASSERT_EQUAL(true, doParseQuotedTest("\"This is a quoted \\\"string\\\" by me\"",
64 "This is a quoted \"string\" by me"));
65
66 // escape sequence test: \\"\"\\"
67 CPPUNIT_ASSERT_EQUAL(true, doParseQuotedTest("\"escape sequence test: \\\\\\\\\\\"\\\\\\\"\\\\\\\\\\\"\"",
68 "escape sequence test: \\\\\"\\\"\\\\\""));
69
70 // \beginning and end test"
71 CPPUNIT_ASSERT_EQUAL(true, doParseQuotedTest("\"\\\\beginning and end test\\\"\"",
72 "\\beginning and end test\""));
73
74 // "
75 CPPUNIT_ASSERT_EQUAL(true, doParseQuotedTest("\"\\\"\"", "\""));
76
77 /* \ */
78 CPPUNIT_ASSERT_EQUAL(true, doParseQuotedTest("\"\\\\\"", "\\"));
79}
80
static void SetCfgLine(char *line)
Set the configuration file line to parse.
static char * NextToken()
static const char * QuoteString(const String &var)
int cmp(char const *) const
Definition: String.cc:255
char const * termedBuf() const
Definition: SquidString.h:92
bool doParseQuotedTest(const char *, const char *)
CPPUNIT_TEST_SUITE_REGISTRATION(testConfigParser)
int shutting_down

 

Introduction

Documentation

Support

Miscellaneous

Web Site Translations

Mirrors