]> git.ipfire.org Git - thirdparty/squid.git/blob - src/tests/testConfigParser.cc
Docs: Copyright updates for 2018 (#114)
[thirdparty/squid.git] / src / tests / testConfigParser.cc
1 /*
2 * Copyright (C) 1996-2018 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 "event.h"
12 #include "SquidString.h"
13 #include "testConfigParser.h"
14 #include "unitTestMain.h"
15
16 CPPUNIT_TEST_SUITE_REGISTRATION( testConfigParser);
17
18 /* let this test link sanely */
19 void
20 eventAdd(const char *name, EVH * func, void *arg, double when, int, bool cbdata)
21 {}
22
23 void testConfigParser::setUp()
24 {
25 }
26
27 bool testConfigParser::doParseQuotedTest(const char *s, const char *expectInterp)
28 {
29 char cfgline[2048];
30 char cfgparam[2048];
31 snprintf(cfgline, 2048, "%s", s);
32
33 // Keep the initial value on cfgparam. The ConfigParser methods will write on cfgline
34 strncpy(cfgparam, cfgline, sizeof(cfgparam)-1);
35 cfgparam[sizeof(cfgparam)-1] = '\0';
36
37 // Initialize parser to point to the start of quoted string
38 ConfigParser::SetCfgLine(cfgline);
39 String unEscaped = ConfigParser::NextToken();
40
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
61 void testConfigParser::testParseQuoted()
62 {
63 // SingleToken
64 CPPUNIT_ASSERT_EQUAL(true, doParseQuotedTest("SingleToken", "SingleToken"));
65
66 // This is a quoted "string" by me
67 CPPUNIT_ASSERT_EQUAL(true, doParseQuotedTest("\"This is a quoted \\\"string\\\" by me\"",
68 "This is a quoted \"string\" by me"));
69
70 // escape sequence test: \\"\"\\"
71 CPPUNIT_ASSERT_EQUAL(true, doParseQuotedTest("\"escape sequence test: \\\\\\\\\\\"\\\\\\\"\\\\\\\\\\\"\"",
72 "escape sequence test: \\\\\"\\\"\\\\\""));
73
74 // \beginning and end test"
75 CPPUNIT_ASSERT_EQUAL(true, doParseQuotedTest("\"\\\\beginning and end test\\\"\"",
76 "\\beginning and end test\""));
77
78 // "
79 CPPUNIT_ASSERT_EQUAL(true, doParseQuotedTest("\"\\\"\"", "\""));
80
81 /* \ */
82 CPPUNIT_ASSERT_EQUAL(true, doParseQuotedTest("\"\\\\\"", "\\"));
83 }
84