test_xml_escape.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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. * \file
  20. * \brief Test ast_xml_escape
  21. *
  22. * \author\verbatim David M. Lee, II <dlee@digium.com> \endverbatim
  23. *
  24. * \ingroup tests
  25. */
  26. /*** MODULEINFO
  27. <depend>TEST_FRAMEWORK</depend>
  28. <support_level>core</support_level>
  29. ***/
  30. #include "asterisk.h"
  31. ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
  32. #include "asterisk/utils.h"
  33. #include "asterisk/module.h"
  34. #include "asterisk/test.h"
  35. static enum ast_test_result_state test_res = AST_TEST_PASS;
  36. static void test_xml(struct ast_test *test, const char *input, const char *expected, int max_len, int expected_res)
  37. {
  38. char actual[256] = "";
  39. int res;
  40. if (max_len == -1) {
  41. max_len = sizeof(actual);
  42. }
  43. res = ast_xml_escape(input, actual, max_len);
  44. if (res != expected_res) {
  45. ast_test_status_update(test, "Expected result '%d', got '%d'\n", expected_res, res);
  46. test_res = AST_TEST_FAIL;
  47. }
  48. if (strcmp(expected, actual) != 0) {
  49. ast_test_status_update(test, "Expected output '%s', got '%s'\n", expected, actual);
  50. test_res = AST_TEST_FAIL;
  51. }
  52. }
  53. AST_TEST_DEFINE(xml_escape_test)
  54. {
  55. char *input;
  56. char *expected;
  57. switch (cmd) {
  58. case TEST_INIT:
  59. info->name = "xml_escape_test";
  60. info->category = "/main/xml_escape/";
  61. info->summary = "Test XML escaping";
  62. info->description =
  63. "Test XML escaping";
  64. return AST_TEST_NOT_RUN;
  65. case TEST_EXECUTE:
  66. break;
  67. }
  68. test_res = AST_TEST_PASS;
  69. /* happy path */
  70. input = "encode me: <&>'\"";
  71. expected = "encode me: &lt;&amp;&gt;&apos;&quot;";
  72. test_xml(test, input, expected, -1, 0);
  73. /* size 0 should fail without changing anything */
  74. input = "foo";
  75. expected = "";
  76. test_xml(test, input, expected, 0, -1);
  77. /* truncate chars */
  78. input = "<truncated>";
  79. expected = "&lt;trunc";
  80. test_xml(test, input, expected, 10, -1);
  81. /* truncate entity */
  82. input = "trunc<";
  83. expected = "trunc";
  84. test_xml(test, input, expected, 9, -1);
  85. return test_res;
  86. }
  87. static int unload_module(void)
  88. {
  89. AST_TEST_UNREGISTER(xml_escape_test);
  90. return 0;
  91. }
  92. static int load_module(void)
  93. {
  94. AST_TEST_REGISTER(xml_escape_test);
  95. return AST_MODULE_LOAD_SUCCESS;
  96. }
  97. AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Skeleton (sample) Test");