File: test_escape.cxx

package info (click to toggle)
libpqxx 4.0.1%2Bdfsg3-8
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 16,012 kB
  • ctags: 9,469
  • sloc: sh: 11,289; cpp: 10,801; xml: 1,256; makefile: 287; ansic: 195; python: 159
file content (105 lines) | stat: -rw-r--r-- 2,491 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#include <test_helpers.hxx>

using namespace PGSTD;
using namespace pqxx;

namespace
{
void compare_esc(connection_base &c, transaction_base &t, const char str[])
{
  const size_t len = string(str).size();
  PQXX_CHECK_EQUAL(c.esc(str, len),
	t.esc(str, len),
	"Connection & transaction escape differently.");

  PQXX_CHECK_EQUAL(t.esc(str, len),
	t.esc(str),
	"Length argument to esc() changes result.");

  PQXX_CHECK_EQUAL(t.esc(string(str)),
	t.esc(str),
	"esc(std::string()) differs from esc(const char[]).");

  PQXX_CHECK_EQUAL(str,
	t.exec("SELECT '" + t.esc(str, len) + "'")[0][0].as<string>(),
	"esc() is not idempotent.");

  PQXX_CHECK_EQUAL(t.esc(str, len),
	t.esc(str),
	"Oversized buffer affects esc().");
}


void test_esc(connection_base &c, transaction_base &t)
{
  PQXX_CHECK_EQUAL(t.esc("", 0), "", "Empty string doesn't escape properly.");
  PQXX_CHECK_EQUAL(t.esc("'", 1), "''", "Single quote escaped incorrectly.");
  const char *const escstrings[] = {
    "x",
    " ",
    "",
    NULL
  };
  for (size_t i=0; escstrings[i]; ++i) compare_esc(c, t, escstrings[i]);
}


void test_quote(connection_base &c, transaction_base &t)
{
  PQXX_CHECK_EQUAL(t.quote("x"), "'x'", "Basic quote() fails.");
  PQXX_CHECK_EQUAL(t.quote(1), "'1'", "quote() not dealing with int properly.");
  PQXX_CHECK_EQUAL(t.quote(0), "'0'", "Quoting zero is a problem.");
  const char *const null_ptr = NULL;
  PQXX_CHECK_EQUAL(t.quote(null_ptr), "NULL", "Not quoting NULL correctly.");
  PQXX_CHECK_EQUAL(t.quote(string("'")), "''''", "Escaping quotes goes wrong.");

  PQXX_CHECK_EQUAL(t.quote("x"),
	c.quote("x"),
	"Connection and transaction quote differently.");

  const char *test_strings[] = {
    "",
    "x",
    "\\",
    "\\\\",
    "'",
    "''",
    "\\'",
    "\t",
    "\n",
    NULL
  };

  for (size_t i=0; test_strings[i]; ++i)
  {
    result r = t.exec("SELECT " + t.quote(test_strings[i]));
    PQXX_CHECK_EQUAL(
	r[0][0].as<string>(),
	test_strings[i],
	"Selecting quoted string does not come back equal.");
  }
}


void test_quote_name(transaction_base &t)
{
  PQXX_CHECK_EQUAL(
	"\"A b\"",
	t.quote_name("A b"),
	"Escaped identifier is not as expected.");
  PQXX_CHECK_EQUAL(
	string("A b"),
	t.exec("SELECT 1 AS " + t.quote_name("A b")).column_name(0),
	"Escaped identifier does not work in SQL.");
}


void test_escaping(transaction_base &t)
{
  test_esc(t.conn(), t);
  test_quote(t.conn(), t);
  test_quote_name(t);
}
} // namespace

PQXX_REGISTER_TEST(test_escaping)