test_uri.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. /*
  2. * Asterisk -- An open source telephony toolkit.
  3. *
  4. * Copyright (C) 2014, Digium, Inc.
  5. *
  6. * Kevin Harwell <kharwell@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 URI Unit Tests
  21. *
  22. * \author Kevin Harwell <kharwell@digium.com>
  23. *
  24. */
  25. /*** MODULEINFO
  26. <depend>TEST_FRAMEWORK</depend>
  27. <support_level>core</support_level>
  28. ***/
  29. #include "asterisk.h"
  30. ASTERISK_FILE_VERSION(__FILE__, "")
  31. #include "asterisk/test.h"
  32. #include "asterisk/module.h"
  33. #include "asterisk/uri.h"
  34. #define CATEGORY "/main/uri/"
  35. static const char *scenarios[][7] = {
  36. {"http://name:pass@localhost", "http", "name:pass", "localhost", NULL, NULL, NULL},
  37. {"http://localhost", "http", NULL, "localhost", NULL, NULL, NULL},
  38. {"http://localhost:80", "http", NULL, "localhost", "80", NULL, NULL},
  39. {"http://localhost/path/", "http", NULL, "localhost", NULL, "path/", NULL},
  40. {"http://localhost/?query", "http", NULL, "localhost", NULL, "", "query"},
  41. {"http://localhost:80/path", "http", NULL, "localhost", "80", "path", NULL},
  42. {"http://localhost:80/?query", "http", NULL, "localhost", "80", "", "query"},
  43. {"http://localhost:80/path?query", "http", NULL, "localhost", "80", "path", "query"},
  44. };
  45. AST_TEST_DEFINE(uri_parse)
  46. {
  47. #define VALIDATE(value, expected_value) \
  48. do { ast_test_validate(test, \
  49. (value == expected_value) || \
  50. (value && expected_value && \
  51. !strcmp(value, expected_value))); \
  52. } while (0)
  53. int i;
  54. switch (cmd) {
  55. case TEST_INIT:
  56. info->name = __func__;
  57. info->category = CATEGORY;
  58. info->summary = "Uri parsing scenarios";
  59. info->description = "For each scenario validate result(s)";
  60. return AST_TEST_NOT_RUN;
  61. case TEST_EXECUTE:
  62. break;
  63. }
  64. for (i = 0; i < ARRAY_LEN(scenarios); ++i) {
  65. RAII_VAR(struct ast_uri *, uri, NULL, ao2_cleanup);
  66. const char **scenario = scenarios[i];
  67. ast_test_validate(test, (uri = ast_uri_parse(scenario[0])));
  68. VALIDATE(ast_uri_scheme(uri), scenario[1]);
  69. VALIDATE(ast_uri_user_info(uri), scenario[2]);
  70. VALIDATE(ast_uri_host(uri), scenario[3]);
  71. VALIDATE(ast_uri_port(uri), scenario[4]);
  72. VALIDATE(ast_uri_path(uri), scenario[5]);
  73. VALIDATE(ast_uri_query(uri), scenario[6]);
  74. }
  75. return AST_TEST_PASS;
  76. }
  77. AST_TEST_DEFINE(uri_default_http)
  78. {
  79. RAII_VAR(struct ast_uri *, uri, NULL, ao2_cleanup);
  80. switch (cmd) {
  81. case TEST_INIT:
  82. info->name = __func__;
  83. info->category = CATEGORY;
  84. info->summary = "parse an http uri with host only";
  85. info->description = info->summary;
  86. return AST_TEST_NOT_RUN;
  87. case TEST_EXECUTE:
  88. break;
  89. }
  90. ast_test_validate(test, (uri = ast_uri_parse_http("localhost")));
  91. ast_test_validate(test, !strcmp(ast_uri_scheme(uri), "http"));
  92. ast_test_validate(test, !strcmp(ast_uri_host(uri), "localhost"));
  93. ast_test_validate(test, !strcmp(ast_uri_port(uri), "80"));
  94. ast_test_validate(test, !ast_uri_is_secure(uri));
  95. return AST_TEST_PASS;
  96. }
  97. AST_TEST_DEFINE(uri_default_http_secure)
  98. {
  99. RAII_VAR(struct ast_uri *, uri, NULL, ao2_cleanup);
  100. switch (cmd) {
  101. case TEST_INIT:
  102. info->name = __func__;
  103. info->category = CATEGORY;
  104. info->summary = "parse an https uri with host only";
  105. info->description = info->summary;
  106. return AST_TEST_NOT_RUN;
  107. case TEST_EXECUTE:
  108. break;
  109. }
  110. ast_test_validate(test, (uri = ast_uri_parse_http("https://localhost")));
  111. ast_test_validate(test, !strcmp(ast_uri_scheme(uri), "https"));
  112. ast_test_validate(test, !strcmp(ast_uri_host(uri), "localhost"));
  113. ast_test_validate(test, !strcmp(ast_uri_port(uri), "443"));
  114. ast_test_validate(test, ast_uri_is_secure(uri));
  115. return AST_TEST_PASS;
  116. }
  117. static int load_module(void)
  118. {
  119. AST_TEST_REGISTER(uri_parse);
  120. AST_TEST_REGISTER(uri_default_http);
  121. AST_TEST_REGISTER(uri_default_http_secure);
  122. return AST_MODULE_LOAD_SUCCESS;
  123. }
  124. static int unload_module(void)
  125. {
  126. AST_TEST_UNREGISTER(uri_default_http_secure);
  127. AST_TEST_UNREGISTER(uri_default_http);
  128. AST_TEST_UNREGISTER(uri_parse);
  129. return 0;
  130. }
  131. AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "URI test module");