~vcs-imports-ii/znc/master

2402 by Uli Schlachter
Fix CString::Escape_n() and add some tests for it
1
/*
2867.1.1 by Un1matr1x
The same procedure as last year, Miss sophie?
2
 * Copyright (C) 2004-2013  See the AUTHORS file for details.
2402 by Uli Schlachter
Fix CString::Escape_n() and add some tests for it
3
 *
4
 * This program is free software; you can redistribute it and/or modify it
5
 * under the terms of the GNU General Public License version 2 as published
6
 * by the Free Software Foundation.
7
 */
8
2700.1.4 by Wulf C. Krueger
Fix the tests.
9
#include "znc/ZNCDebug.h"
2402 by Uli Schlachter
Fix CString::Escape_n() and add some tests for it
10
11
static int testEqual(const CString& a, const CString& b, const CString& what)
12
{
13
	if (a == b)
14
		return 0;
15
	std::cout << what << " failed for '" << b << "', result is '" << a << "'\n";
16
	return 1;
17
}
18
19
static int testString(const CString& in, const CString& url,
20
		const CString& html, const CString& sql) {
21
	CString out;
22
	int errors = 0;
23
24
	// Encode, then decode again and check we still got the same string
25
26
	out = in.Escape_n(CString::EASCII, CString::EURL);
27
	errors += testEqual(out, url, "EURL encode");
28
	out = out.Escape_n(CString::EURL, CString::EASCII);
29
	errors += testEqual(out, in, "EURL decode");
30
31
	out = in.Escape_n(CString::EASCII, CString::EHTML);
32
	errors += testEqual(out, html, "EHTML encode");
33
	out = out.Escape_n(CString::EHTML, CString::EASCII);
34
	errors += testEqual(out, in, "EHTML decode");
35
36
	out = in.Escape_n(CString::EASCII, CString::ESQL);
37
	errors += testEqual(out, sql, "ESQL encode");
38
	out = out.Escape_n(CString::ESQL, CString::EASCII);
39
	errors += testEqual(out, in, "ESQL decode");
40
41
	return errors;
42
}
43
44
int main() {
45
	unsigned int failed = 0;
46
47
	//                    input      url          html             sql
48
	failed += testString("abcdefg", "abcdefg",   "abcdefg",       "abcdefg");
49
	failed += testString("\n\t\r",  "%0A%09%0D", "\n\t\r",        "\\n\\t\\r");
50
	failed += testString("'\"",     "%27%22",    "'&quot;",       "\\'\\\"");
51
	failed += testString("&<>",     "%26%3C%3E", "&amp;&lt;&gt;", "&<>");
52
53
	return failed;
54
}