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