]> git.ipfire.org Git - thirdparty/squid.git/blame - src/tests/testConfigParser.cc
Source Format Enforcement (#532)
[thirdparty/squid.git] / src / tests / testConfigParser.cc
CommitLineData
4e0938ef 1/*
77b1029d 2 * Copyright (C) 1996-2020 The Squid Software Foundation and contributors
4e0938ef
AJ
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
f7f3304a 9#include "squid.h"
3511674f 10#include "ConfigParser.h"
602d9612 11#include "event.h"
602d9612
A
12#include "SquidString.h"
13#include "testConfigParser.h"
7f861c77
AJ
14#include "unitTestMain.h"
15
3511674f
CT
16CPPUNIT_TEST_SUITE_REGISTRATION( testConfigParser);
17
18/* let this test link sanely */
19void
20eventAdd(const char *name, EVH * func, void *arg, double when, int, bool cbdata)
21{}
22
23void testConfigParser::setUp()
24{
25}
26
27bool testConfigParser::doParseQuotedTest(const char *s, const char *expectInterp)
28{
29 char cfgline[2048];
30 char cfgparam[2048];
2eceb328 31 snprintf(cfgline, 2048, "%s", s);
a0b3b22c 32
3511674f 33 // Keep the initial value on cfgparam. The ConfigParser methods will write on cfgline
2eceb328 34 strncpy(cfgparam, cfgline, sizeof(cfgparam)-1);
a0b3b22c 35 cfgparam[sizeof(cfgparam)-1] = '\0';
3511674f
CT
36
37 // Initialize parser to point to the start of quoted string
2eceb328
CT
38 ConfigParser::SetCfgLine(cfgline);
39 String unEscaped = ConfigParser::NextToken();
71ee0835 40
3511674f
CT
41 const bool interpOk = (unEscaped.cmp(expectInterp) == 0);
42 if (!interpOk) {
43 printf("%25s: %s\n%25s: %s\n%25s: %s\n",
44 "Raw configuration", cfgparam,
45 "Expected interpretation", expectInterp,
46 "Actual interpretation", unEscaped.termedBuf());
47 }
48
49 const char *quoted = ConfigParser::QuoteString(unEscaped);
50 bool quotedOk = (strcmp(cfgparam, quoted)==0);
51 if (!quotedOk) {
52 printf("%25s: %s\n%25s: %s\n%25s: %s\n",
53 "Raw configuration", cfgparam,
54 "Parsed and quoted", quoted,
55 "parsed value was", unEscaped.termedBuf());
56 }
57
58 return quotedOk && interpOk ;
59}
60
61void testConfigParser::testParseQuoted()
62{
63 // SingleToken
5d1dff27 64 CPPUNIT_ASSERT_EQUAL(true, doParseQuotedTest("SingleToken", "SingleToken"));
3511674f
CT
65
66 // This is a quoted "string" by me
5d1dff27 67 CPPUNIT_ASSERT_EQUAL(true, doParseQuotedTest("\"This is a quoted \\\"string\\\" by me\"",
87586126 68 "This is a quoted \"string\" by me"));
3511674f
CT
69
70 // escape sequence test: \\"\"\\"
5d1dff27 71 CPPUNIT_ASSERT_EQUAL(true, doParseQuotedTest("\"escape sequence test: \\\\\\\\\\\"\\\\\\\"\\\\\\\\\\\"\"",
87586126 72 "escape sequence test: \\\\\"\\\"\\\\\""));
71ee0835 73
3511674f 74 // \beginning and end test"
5d1dff27 75 CPPUNIT_ASSERT_EQUAL(true, doParseQuotedTest("\"\\\\beginning and end test\\\"\"",
87586126 76 "\\beginning and end test\""));
3511674f
CT
77
78 // "
5d1dff27 79 CPPUNIT_ASSERT_EQUAL(true, doParseQuotedTest("\"\\\"\"", "\""));
3511674f
CT
80
81 /* \ */
5d1dff27 82 CPPUNIT_ASSERT_EQUAL(true, doParseQuotedTest("\"\\\\\"", "\\"));
3511674f
CT
83}
84