connection.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. /*
  2. * Copyright (c) 2018-2019 SignalWire, Inc
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy
  5. * of this software and associated documentation files (the "Software"), to deal
  6. * in the Software without restriction, including without limitation the rights
  7. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. * copies of the Software, and to permit persons to whom the Software is
  9. * furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in all
  12. * copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  20. * SOFTWARE.
  21. */
  22. #include "swclt_test.h"
  23. static uint32_t g_protocol_response_cb_called;
  24. static ks_status_t __on_incoming_cmd(swclt_conn_t conn, swclt_cmd_t cmd, void *cb_data)
  25. {
  26. return KS_STATUS_SUCCESS;
  27. }
  28. static void __on_protocol_response(swclt_cmd_t cmd, void *cb_data)
  29. {
  30. g_protocol_response_cb_called++;
  31. }
  32. void test_async(ks_pool_t *pool)
  33. {
  34. SSL_CTX *ssl = create_ssl_context();
  35. swclt_conn_t conn;
  36. swclt_cmd_t cmd;
  37. SWCLT_CMD_TYPE cmd_type;
  38. const ks_json_t *result;
  39. ks_json_t *channels;
  40. REQUIRE(!swclt_conn_connect(&conn, __on_incoming_cmd, NULL, &g_target_ident, NULL, ssl));
  41. channels = ks_json_create_array_inline(1, BLADE_CHANNEL_MARSHAL(pool, &(blade_channel_t){"a_channel", 0, 0}));
  42. /* Create an async command (bogus command but will generate a reply at least) */
  43. REQUIRE(cmd = CREATE_BLADE_PROTOCOL_PROVIDER_ADD_CMD_ASYNC(
  44. __on_protocol_response,
  45. NULL,
  46. "a_protocol",
  47. 0,
  48. 0,
  49. 0,
  50. NULL,
  51. &channels,
  52. 1,
  53. NULL));
  54. /* And submit it */
  55. REQUIRE(!swclt_conn_submit_request(conn, cmd));
  56. /* Wait for it to respond */
  57. while (KS_TRUE) {
  58. REQUIRE(!swclt_cmd_type(cmd, &cmd_type));
  59. if (cmd_type != SWCLT_CMD_TYPE_REQUEST) {
  60. break;
  61. }
  62. ks_sleep_ms(1000);
  63. }
  64. REQUIRE(cmd_type == SWCLT_CMD_TYPE_RESULT);
  65. REQUIRE(!swclt_cmd_result(cmd, &result));
  66. REQUIRE(g_protocol_response_cb_called == 1);
  67. REQUIRE(ks_handle_valid(cmd));
  68. ks_handle_destroy(&conn);
  69. /* Command should become invalid once we destroy the connection */
  70. REQUIRE(!ks_handle_valid(cmd));
  71. swclt_ssl_destroy_context(&ssl);
  72. }
  73. void test_ttl(ks_pool_t *pool)
  74. {
  75. SSL_CTX *ssl = create_ssl_context();
  76. swclt_conn_t conn;
  77. swclt_cmd_t cmd;
  78. SWCLT_CMD_TYPE cmd_type;
  79. swclt_conn_ctx_t *conn_ctx;
  80. swclt_wss_ctx_t *wss_ctx;
  81. swclt_cmd_ctx_t *cmd_ctx;
  82. ks_json_t *channels;
  83. REQUIRE(!swclt_conn_connect(&conn, __on_incoming_cmd, NULL, &g_target_ident, NULL, ssl));
  84. channels = ks_json_create_array_inline(1, BLADE_CHANNEL_MARSHAL(pool, &(blade_channel_t){"a_channel", 0, 0}));
  85. REQUIRE(cmd = CREATE_BLADE_PROTOCOL_PROVIDER_ADD_CMD_ASYNC(
  86. __on_protocol_response,
  87. NULL,
  88. "a_protocol",
  89. 0,
  90. 0,
  91. 0,
  92. NULL,
  93. &channels,
  94. 1,
  95. NULL));
  96. /* Lock the reader so we never get a response, forcing a timeout */
  97. conn_ctx = conn_get(conn);
  98. wss_ctx = wss_get(conn_ctx->wss);
  99. cmd_ctx = cmd_get(cmd);
  100. REQUIRE(cmd_ctx->response_ttl_ms == BLADE_PROTOCOL_TTL_MS);
  101. REQUIRE(cmd_ctx->flags == BLADE_PROTOCOL_FLAGS);
  102. REQUIRE(!ks_mutex_lock(wss_ctx->write_mutex));
  103. /* And submit it */
  104. REQUIRE(!swclt_conn_submit_request(conn, cmd));
  105. /* Give it 4 seconds */
  106. ks_sleep_ms(4000);
  107. {
  108. ks_status_t failure_status;
  109. const char *failure_message;
  110. /* Better have failed */
  111. REQUIRE(!swclt_cmd_type(cmd, &cmd_type));
  112. REQUIRE(cmd_type == SWCLT_CMD_TYPE_FAILURE);
  113. REQUIRE(!swclt_cmd_failure_info(cmd, &failure_status, &failure_message));
  114. /* One for this test, and the one prior */
  115. REQUIRE(g_protocol_response_cb_called == 2);
  116. REQUIRE(failure_status == KS_STATUS_TIMEOUT);
  117. printf("Validated failure code, message: %s\n", failure_message);
  118. }
  119. /* Don't forget to unlock the poor websocket reader */
  120. REQUIRE(!ks_mutex_unlock(wss_ctx->write_mutex));
  121. cmd_put(&cmd_ctx);
  122. conn_put(&conn_ctx);
  123. wss_put(&wss_ctx);
  124. REQUIRE(ks_handle_valid(cmd));
  125. ks_handle_destroy(&conn);
  126. /* Command should become invalid once we destroy the connection */
  127. REQUIRE(!ks_handle_valid(cmd));
  128. swclt_ssl_destroy_context(&ssl);
  129. }
  130. void test_connection(ks_pool_t *pool)
  131. {
  132. test_async(pool);
  133. test_ttl(pool);
  134. }