Asterisk - The Open Source Telephony Project GIT-master-f36a736
test_xml_escape.c
Go to the documentation of this file.
1/*
2 * Asterisk -- An open source telephony toolkit.
3 *
4 * Copyright (C) 2013, Digium, Inc.
5 *
6 * David M. Lee, II <dlee@digium.com>
7 *
8 * See http://www.asterisk.org for more information about
9 * the Asterisk project. Please do not directly contact
10 * any of the maintainers of this project for assistance;
11 * the project provides a web site, mailing lists and IRC
12 * channels for your use.
13 *
14 * This program is free software, distributed under the terms of
15 * the GNU General Public License Version 2. See the LICENSE file
16 * at the top of the source tree.
17 */
18
19/*!
20 * \file
21 * \brief Test ast_xml_escape
22 *
23 * \author\verbatim David M. Lee, II <dlee@digium.com> \endverbatim
24 *
25 * \ingroup tests
26 */
27
28/*** MODULEINFO
29 <depend>TEST_FRAMEWORK</depend>
30 <support_level>core</support_level>
31 ***/
32
33#include "asterisk.h"
34
35#include "asterisk/utils.h"
36#include "asterisk/module.h"
37#include "asterisk/test.h"
38
40
41static void test_xml(struct ast_test *test, const char *input, const char *expected, int max_len, int expected_res)
42{
43 char actual[256] = "";
44 int res;
45
46 if (max_len == -1) {
47 max_len = sizeof(actual);
48 }
49
50 res = ast_xml_escape(input, actual, max_len);
51 if (res != expected_res) {
52 ast_test_status_update(test, "Expected result '%d', got '%d'\n", expected_res, res);
54 }
55
56 if (strcmp(expected, actual) != 0) {
57 ast_test_status_update(test, "Expected output '%s', got '%s'\n", expected, actual);
59 }
60}
61
62AST_TEST_DEFINE(xml_escape_test)
63{
64 char *input;
65 char *expected;
66
67 switch (cmd) {
68 case TEST_INIT:
69 info->name = "xml_escape_test";
70 info->category = "/main/xml_escape/";
71 info->summary = "Test XML escaping";
72 info->description =
73 "Test XML escaping";
74 return AST_TEST_NOT_RUN;
75 case TEST_EXECUTE:
76 break;
77 }
78
80
81 /* happy path */
82 input = "encode me: <&>'\"";
83 expected = "encode me: &lt;&amp;&gt;&apos;&quot;";
84 test_xml(test, input, expected, -1, 0);
85
86 /* size 0 should fail without changing anything */
87 input = "foo";
88 expected = "";
89 test_xml(test, input, expected, 0, -1);
90
91 /* truncate chars */
92 input = "<truncated>";
93 expected = "&lt;trunc";
94 test_xml(test, input, expected, 10, -1);
95
96 /* truncate entity */
97 input = "trunc<";
98 expected = "trunc";
99 test_xml(test, input, expected, 9, -1);
100
101 return test_res;
102}
103
104static int unload_module(void)
105{
106 AST_TEST_UNREGISTER(xml_escape_test);
107 return 0;
108}
109
110static int load_module(void)
111{
112 AST_TEST_REGISTER(xml_escape_test);
114}
115
static int input(yyscan_t yyscanner)
Definition: ast_expr2f.c:1570
Asterisk main include file. File version handling, generic pbx functions.
Asterisk module definitions.
#define AST_MODULE_INFO_STANDARD(keystr, desc)
Definition: module.h:581
#define ASTERISK_GPL_KEY
The text the key() function should return.
Definition: module.h:46
@ AST_MODULE_LOAD_SUCCESS
Definition: module.h:70
def info(msg)
Test Framework API.
@ TEST_INIT
Definition: test.h:200
@ TEST_EXECUTE
Definition: test.h:201
#define AST_TEST_REGISTER(cb)
Definition: test.h:127
#define ast_test_status_update(a, b, c...)
Definition: test.h:129
#define AST_TEST_UNREGISTER(cb)
Definition: test.h:128
ast_test_result_state
Definition: test.h:193
@ AST_TEST_PASS
Definition: test.h:195
@ AST_TEST_FAIL
Definition: test.h:196
@ AST_TEST_NOT_RUN
Definition: test.h:194
static void test_xml(struct ast_test *test, const char *input, const char *expected, int max_len, int expected_res)
static enum ast_test_result_state test_res
AST_TEST_DEFINE(xml_escape_test)
static int load_module(void)
static int unload_module(void)
Utility functions.
int ast_xml_escape(const char *string, char *outbuf, size_t buflen)
Escape reserved characters for use in XML.
Definition: utils.c:864