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