testConfigParser.cc
Go to the documentation of this file.
1 /*
2  * Copyright (C) 1996-2017 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 
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 
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 
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 
void eventAdd(const char *name, EVH *func, void *arg, double when, int, bool cbdata)
int cmp(char const *) const
Definition: String.cc:269
Definition: cbdata.cc:60
void EVH void * arg
Definition: stub_event.cc:16
void EVH(void *)
Definition: event.h:19
static void SetCfgLine(char *line)
Set the configuration file line to parse.
char const * termedBuf() const
Definition: SquidString.h:90
static char * NextToken()
static const char * QuoteString(const String &var)
CPPUNIT_TEST_SUITE_REGISTRATION(testConfigParser)
void EVH * func
Definition: stub_event.cc:16
bool doParseQuotedTest(const char *, const char *)

 

Introduction

Documentation

Support

Miscellaneous

Web Site Translations

Mirrors