test_websocket_client.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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 Websocket Client Unit Tests
  21. *
  22. * \author Kevin Harwell <kharwell@digium.com>
  23. *
  24. */
  25. /*** MODULEINFO
  26. <depend>TEST_FRAMEWORK</depend>
  27. <depend>res_http_websocket</depend>
  28. <support_level>core</support_level>
  29. ***/
  30. #include "asterisk.h"
  31. ASTERISK_FILE_VERSION(__FILE__, "")
  32. #include "asterisk/test.h"
  33. #include "asterisk/module.h"
  34. #include "asterisk/astobj2.h"
  35. #include "asterisk/pbx.h"
  36. #include "asterisk/http_websocket.h"
  37. #define CATEGORY "/res/websocket/"
  38. #define REMOTE_URL "ws://127.0.0.1:8088/ws"
  39. AST_TEST_DEFINE(websocket_client_create_and_connect)
  40. {
  41. RAII_VAR(struct ast_websocket *, client, NULL, ao2_cleanup);
  42. enum ast_websocket_result result;
  43. const char write_buf[] = "this is only a test";
  44. RAII_VAR(char *, read_buf, NULL, ast_free);
  45. switch (cmd) {
  46. case TEST_INIT:
  47. info->name = __func__;
  48. info->explicit_only = 1;
  49. info->category = CATEGORY;
  50. info->summary = "test creation and connection of a client websocket";
  51. info->description = "test creation and connection of a client websocket";
  52. return AST_TEST_NOT_RUN;
  53. case TEST_EXECUTE:
  54. break;
  55. }
  56. ast_test_validate(test, (client = ast_websocket_client_create(
  57. REMOTE_URL, "echo", NULL, &result)));
  58. ast_test_validate(test, !ast_websocket_write_string(client, write_buf));
  59. ast_test_validate(test, ast_websocket_read_string(client, &read_buf) > 0);
  60. ast_test_validate(test, !strcmp(write_buf, read_buf));
  61. return AST_TEST_PASS;
  62. }
  63. AST_TEST_DEFINE(websocket_client_bad_url)
  64. {
  65. RAII_VAR(struct ast_websocket *, client, NULL, ao2_cleanup);
  66. enum ast_websocket_result result;
  67. switch (cmd) {
  68. case TEST_INIT:
  69. info->name = __func__;
  70. info->category = CATEGORY;
  71. info->summary = "websocket client - test bad url";
  72. info->description = "pass a bad url and make sure it fails";
  73. return AST_TEST_NOT_RUN;
  74. case TEST_EXECUTE:
  75. break;
  76. }
  77. ast_test_validate(test, !(client = ast_websocket_client_create(
  78. "invalid", NULL, NULL, &result)));
  79. return AST_TEST_PASS;
  80. }
  81. AST_TEST_DEFINE(websocket_client_unsupported_protocol)
  82. {
  83. RAII_VAR(struct ast_websocket *, client, NULL, ao2_cleanup);
  84. enum ast_websocket_result result;
  85. switch (cmd) {
  86. case TEST_INIT:
  87. info->name = __func__;
  88. info->category = CATEGORY;
  89. info->summary = "websocket client - unsupported protocol";
  90. info->description = "fails on an unsupported protocol";
  91. return AST_TEST_NOT_RUN;
  92. case TEST_EXECUTE:
  93. break;
  94. }
  95. ast_test_validate(test, !(client = ast_websocket_client_create(
  96. REMOTE_URL, "unsupported", NULL, &result)));
  97. return AST_TEST_PASS;
  98. }
  99. AST_TEST_DEFINE(websocket_client_multiple_protocols)
  100. {
  101. RAII_VAR(struct ast_websocket *, client, NULL, ao2_cleanup);
  102. const char *accept_protocol;
  103. enum ast_websocket_result result;
  104. switch (cmd) {
  105. case TEST_INIT:
  106. info->name = __func__;
  107. info->category = CATEGORY;
  108. info->summary = "websocket client - test multiple protocols";
  109. info->description = "test multi-protocol client";
  110. return AST_TEST_NOT_RUN;
  111. case TEST_EXECUTE:
  112. break;
  113. }
  114. ast_test_validate(test, (client = ast_websocket_client_create(
  115. REMOTE_URL, "echo,unsupported", NULL, &result)));
  116. accept_protocol = ast_websocket_client_accept_protocol(client);
  117. ast_test_validate(test, accept_protocol && !strcmp(accept_protocol, "echo"));
  118. return AST_TEST_PASS;
  119. }
  120. static int load_module(void)
  121. {
  122. AST_TEST_REGISTER(websocket_client_create_and_connect);
  123. AST_TEST_REGISTER(websocket_client_bad_url);
  124. AST_TEST_REGISTER(websocket_client_unsupported_protocol);
  125. AST_TEST_REGISTER(websocket_client_multiple_protocols);
  126. return AST_MODULE_LOAD_SUCCESS;
  127. }
  128. static int unload_module(void)
  129. {
  130. AST_TEST_UNREGISTER(websocket_client_multiple_protocols);
  131. AST_TEST_UNREGISTER(websocket_client_unsupported_protocol);
  132. AST_TEST_UNREGISTER(websocket_client_bad_url);
  133. AST_TEST_UNREGISTER(websocket_client_create_and_connect);
  134. return 0;
  135. }
  136. AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Websocket client test module");