test_websocket_client.c 4.5 KB

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